Skip to content

Commit

Permalink
Restrict conversion to whole numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
kaklakariada committed Jun 27, 2024
1 parent 71916de commit 78377e6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 6 additions & 1 deletion pkg/connection/result_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"database/sql/driver"
"io"
"math"
"reflect"
"sync"

Expand Down Expand Up @@ -111,7 +112,7 @@ func (results *QueryResults) getColumnValue(columnIndex int) driver.Value {
// See https://github.com/exasol/exasol-driver-go/issues/113 for details.
func convertValue(value any, columnType types.SqlQueryColumnType) driver.Value {
if isIntegerColumn(columnType) {
if floatValue, ok := value.(float64); ok {
if floatValue, ok := value.(float64); ok && isIntegerValue(floatValue) {
return int64(floatValue)
}
}
Expand All @@ -122,6 +123,10 @@ func isIntegerColumn(columnType types.SqlQueryColumnType) bool {
return columnType.Type == "DECIMAL" && columnType.Scale != nil && *columnType.Scale == 0
}

func isIntegerValue(value float64) bool {
return value == math.Trunc(value)
}

func (results *QueryResults) fetchNextRowChunk() error {
chunk := &types.SqlQueryResponseResultSetData{}
err := results.con.Send(context.Background(), &types.FetchCommand{
Expand Down
6 changes: 4 additions & 2 deletions pkg/connection/result_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,10 @@ func (suite *ResultSetTestSuite) TestConvertValue() {
columnType types.SqlQueryColumnType
expectedValue driver.Value
}{
{float64(1.1), decimalTypeZeroScale, int64(1)}, // Only this combination will convert the value
{1.1, decimalTypeZeroScale, int64(1)},
{float64(1), decimalTypeZeroScale, int64(1)}, // Only this combination will convert the value
{float64(-1), decimalTypeZeroScale, int64(-1)}, // Only this combination will convert the value
{float64(10000000000), decimalTypeZeroScale, int64(10000000000)}, // Only this combination will convert the value
{1.1, decimalTypeZeroScale, float64(1.1)},
{float32(1.1), decimalTypeZeroScale, float32(1.1)},
{"string", decimalTypeZeroScale, "string"},
{true, decimalTypeZeroScale, true},
Expand Down

0 comments on commit 78377e6

Please sign in to comment.