forked from databricks/databricks-sql-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PECO-1016-2] Add handling for special types (databricks#158)
In this PR, we add further handling to allow for special types to be set via the DBSQLParams variable.
- Loading branch information
Showing
5 changed files
with
200 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package dbsql | ||
|
||
import ( | ||
"database/sql/driver" | ||
"strconv" | ||
"testing" | ||
"time" | ||
|
||
"github.com/databricks/databricks-sql-go/internal/cli_service" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestParameter_Inference(t *testing.T) { | ||
t.Run("Should infer types correctly", func(t *testing.T) { | ||
values := [5]driver.NamedValue{{Name: "", Value: float32(5.1)}, {Name: "", Value: time.Now()}, {Name: "", Value: int64(5)}, {Name: "", Value: true}, {Name: "", Value: DBSqlParam{Value: "6.2", Type: Decimal}}} | ||
parameters := convertNamedValuesToSparkParams(values[:]) | ||
assert.Equal(t, strconv.FormatFloat(float64(5.1), 'f', -1, 64), *parameters[0].Value.StringValue) | ||
assert.NotNil(t, parameters[1].Value.StringValue) | ||
assert.Equal(t, string("TIMESTAMP"), *parameters[1].Type) | ||
assert.Equal(t, &cli_service.TSparkParameterValue{StringValue: strPtr("5")}, parameters[2].Value) | ||
assert.Equal(t, string("true"), *parameters[3].Value.StringValue) | ||
assert.Equal(t, string("DECIMAL"), *parameters[4].Type) | ||
assert.Equal(t, string("6.2"), *parameters[4].Value.StringValue) | ||
}) | ||
} | ||
func TestParameters_Names(t *testing.T) { | ||
t.Run("Should infer types correctly", func(t *testing.T) { | ||
values := [2]driver.NamedValue{{Name: "1", Value: int(26)}, {Name: "", Value: DBSqlParam{Name: "2", Type: Decimal, Value: "6.2"}}} | ||
parameters := convertNamedValuesToSparkParams(values[:]) | ||
assert.Equal(t, string("1"), *parameters[0].Name) | ||
assert.Equal(t, cli_service.TSparkParameterValue{StringValue: strPtr("26")}, *parameters[0].Value) | ||
assert.Equal(t, string("2"), *parameters[1].Name) | ||
assert.Equal(t, cli_service.TSparkParameterValue{StringValue: strPtr("6.2")}, *parameters[1].Value) | ||
assert.Equal(t, string("DECIMAL"), *parameters[1].Type) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package dbsql | ||
|
||
import ( | ||
"database/sql/driver" | ||
"fmt" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/databricks/databricks-sql-go/internal/cli_service" | ||
) | ||
|
||
type DBSqlParam struct { | ||
Name string | ||
Type SqlType | ||
Value any | ||
} | ||
|
||
type SqlType int64 | ||
|
||
const ( | ||
String SqlType = iota | ||
Date | ||
Timestamp | ||
Float | ||
Decimal | ||
Double | ||
Integer | ||
BigInt | ||
SmallInt | ||
TinyInt | ||
Boolean | ||
IntervalMonth | ||
IntervalDay | ||
) | ||
|
||
func (s SqlType) String() string { | ||
switch s { | ||
case String: | ||
return "STRING" | ||
case Date: | ||
return "DATE" | ||
case Timestamp: | ||
return "TIMESTAMP" | ||
case Float: | ||
return "FLOAT" | ||
case Decimal: | ||
return "DECIMAL" | ||
case Double: | ||
return "DOUBLE" | ||
case Integer: | ||
return "INTEGER" | ||
case BigInt: | ||
return "BIGINT" | ||
case SmallInt: | ||
return "SMALLINT" | ||
case TinyInt: | ||
return "TINYINT" | ||
case Boolean: | ||
return "BOOLEAN" | ||
case IntervalMonth: | ||
return "INTERVAL MONTH" | ||
case IntervalDay: | ||
return "INTERVAL DAY" | ||
} | ||
return "unknown" | ||
} | ||
|
||
func valuesToDBSQLParams(namedValues []driver.NamedValue) []DBSqlParam { | ||
var params []DBSqlParam | ||
for i := range namedValues { | ||
namedValue := namedValues[i] | ||
param := *new(DBSqlParam) | ||
param.Name = namedValue.Name | ||
param.Value = namedValue.Value | ||
params = append(params, param) | ||
} | ||
return params | ||
} | ||
|
||
func inferTypes(params []DBSqlParam) { | ||
for i := range params { | ||
param := ¶ms[i] | ||
switch value := param.Value.(type) { | ||
case bool: | ||
param.Value = strconv.FormatBool(value) | ||
param.Type = Boolean | ||
case string: | ||
param.Value = value | ||
param.Type = String | ||
case int: | ||
param.Value = strconv.Itoa(value) | ||
param.Type = Integer | ||
case uint: | ||
param.Value = strconv.FormatUint(uint64(value), 10) | ||
param.Type = Integer | ||
case int8: | ||
param.Value = strconv.Itoa(int(value)) | ||
param.Type = Integer | ||
case uint8: | ||
param.Value = strconv.FormatUint(uint64(value), 10) | ||
param.Type = Integer | ||
case int16: | ||
param.Value = strconv.Itoa(int(value)) | ||
param.Type = Integer | ||
case uint16: | ||
param.Value = strconv.FormatUint(uint64(value), 10) | ||
param.Type = Integer | ||
case int32: | ||
param.Value = strconv.Itoa(int(value)) | ||
param.Type = Integer | ||
case uint32: | ||
param.Value = strconv.FormatUint(uint64(value), 10) | ||
param.Type = Integer | ||
case int64: | ||
param.Value = strconv.Itoa(int(value)) | ||
param.Type = Integer | ||
case uint64: | ||
param.Value = strconv.FormatUint(uint64(value), 10) | ||
param.Type = Integer | ||
case float32: | ||
param.Value = strconv.FormatFloat(float64(value), 'f', -1, 32) | ||
param.Type = Float | ||
case time.Time: | ||
param.Value = value.String() | ||
param.Type = Timestamp | ||
case DBSqlParam: | ||
param.Name = value.Name | ||
param.Value = value.Value | ||
param.Type = value.Type | ||
default: | ||
s := fmt.Sprintf("%s", value) | ||
param.Value = s | ||
param.Type = String | ||
} | ||
} | ||
} | ||
func convertNamedValuesToSparkParams(values []driver.NamedValue) []*cli_service.TSparkParameter { | ||
var sparkParams []*cli_service.TSparkParameter | ||
|
||
sqlParams := valuesToDBSQLParams(values) | ||
inferTypes(sqlParams) | ||
for i := range sqlParams { | ||
sqlParam := sqlParams[i] | ||
sparkParamValue := sqlParam.Value.(string) | ||
sparkParamType := sqlParam.Type.String() | ||
sparkParam := cli_service.TSparkParameter{Name: &sqlParam.Name, Type: &sparkParamType, Value: &cli_service.TSparkParameterValue{StringValue: &sparkParamValue}} | ||
sparkParams = append(sparkParams, &sparkParam) | ||
} | ||
return sparkParams | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters