Skip to content

Commit ca22468

Browse files
Address review comment: use consistent formatting for explicit Parameter values
Use strconv.FormatFloat for float32/float64 and strconv.FormatInt/FormatUint for int64/uint64 in convertNamedValuesToSparkParams to ensure consistent formatting (decimal notation) instead of fmt.Sprintf which uses scientific notation for large floats. Added test cases to verify large float64 values use decimal notation. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent eff5cc8 commit ca22468

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

parameter_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,31 @@ func TestParameter_Float(t *testing.T) {
146146
require.Equal(t, "DOUBLE", *parameters[0].Type)
147147
require.Equal(t, "3.14159", *parameters[0].Value.StringValue)
148148
})
149+
150+
t.Run("Should format large float64 consistently when using explicit type", func(t *testing.T) {
151+
// This tests that explicit Parameter with large float64 uses decimal notation
152+
// (strconv.FormatFloat) instead of scientific notation (fmt.Sprintf)
153+
value := float64(1e20)
154+
values := []driver.NamedValue{
155+
{Value: Parameter{Type: SqlDouble, Value: value}},
156+
}
157+
parameters, err := convertNamedValuesToSparkParams(values)
158+
require.NoError(t, err)
159+
require.Equal(t, "DOUBLE", *parameters[0].Type)
160+
// Should be decimal notation, not "1e+20"
161+
require.Equal(t, "100000000000000000000", *parameters[0].Value.StringValue)
162+
})
163+
164+
t.Run("Should format float32 consistently when using explicit type", func(t *testing.T) {
165+
value := float32(3.14159)
166+
values := []driver.NamedValue{
167+
{Value: Parameter{Type: SqlFloat, Value: value}},
168+
}
169+
parameters, err := convertNamedValuesToSparkParams(values)
170+
require.NoError(t, err)
171+
require.Equal(t, "FLOAT", *parameters[0].Type)
172+
require.Equal(t, "3.14159", *parameters[0].Value.StringValue)
173+
})
149174
}
150175

151176
func TestParameters_ConvertToSpark(t *testing.T) {

parameters.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,14 @@ func convertNamedValuesToSparkParams(values []driver.NamedValue) ([]*cli_service
183183
switch v := sqlParam.Value.(type) {
184184
case string:
185185
stringValue = v
186+
case float32:
187+
stringValue = strconv.FormatFloat(float64(v), 'f', -1, 32)
188+
case float64:
189+
stringValue = strconv.FormatFloat(v, 'f', -1, 64)
190+
case int64:
191+
stringValue = strconv.FormatInt(v, 10)
192+
case uint64:
193+
stringValue = strconv.FormatUint(v, 10)
186194
default:
187195
stringValue = fmt.Sprintf("%v", sqlParam.Value)
188196
}

0 commit comments

Comments
 (0)