Skip to content

Commit 57723a3

Browse files
Named parameter support and update Thrift
Updated thrift client code to latest version. Added support for named query parameters. Signed-off-by: Raymond Cypher <raymond.cypher@databricks.com>
1 parent 7e079fd commit 57723a3

5 files changed

Lines changed: 10854 additions & 5740 deletions

File tree

connection.go

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package dbsql
33
import (
44
"context"
55
"database/sql/driver"
6+
"fmt"
67
"time"
78

89
"github.com/databricks/databricks-sql-go/driverctx"
@@ -100,9 +101,6 @@ func (c *conn) ExecContext(ctx context.Context, query string, args []driver.Name
100101
defer log.Duration(msg, start)
101102

102103
ctx = driverctx.NewContextWithConnId(ctx, c.id)
103-
if len(args) > 0 {
104-
return nil, dbsqlerrint.NewDriverError(ctx, dbsqlerr.ErrParametersNotSupported, nil)
105-
}
106104
exStmtResp, opStatusResp, err := c.runQuery(ctx, query, args)
107105

108106
if exStmtResp != nil && exStmtResp.OperationHandle != nil {
@@ -142,9 +140,7 @@ func (c *conn) QueryContext(ctx context.Context, query string, args []driver.Nam
142140
msg, start := log.Track("QueryContext")
143141

144142
ctx = driverctx.NewContextWithConnId(ctx, c.id)
145-
if len(args) > 0 {
146-
return nil, dbsqlerrint.NewDriverError(ctx, dbsqlerr.ErrParametersNotSupported, nil)
147-
}
143+
148144
// first we try to get the results synchronously.
149145
// at any point in time that the context is done we must cancel and return
150146
exStmtResp, opStatusResp, err := c.runQuery(ctx, query, args)
@@ -284,6 +280,7 @@ func (c *conn) executeStatement(ctx context.Context, query string, args []driver
284280
MaxRows: int64(c.cfg.MaxRows),
285281
},
286282
CanDecompressLZ4Result_: &c.cfg.UseLz4Compression,
283+
Parameters: namedValuesToTSparkParams(args),
287284
}
288285

289286
if c.cfg.UseArrowBatches {
@@ -337,6 +334,83 @@ func (c *conn) executeStatement(ctx context.Context, query string, args []driver
337334
return resp, err
338335
}
339336

337+
func namedValuesToTSparkParams(args []driver.NamedValue) []*cli_service.TSparkParameter {
338+
var ts []string = []string{"STRING", "DOUBLE", "BOOLEAN"}
339+
var params []*cli_service.TSparkParameter
340+
for i := range args {
341+
arg := args[i]
342+
param := cli_service.TSparkParameter{Value: &cli_service.TSparkParameterValue{}}
343+
if arg.Name != "" {
344+
param.Name = &arg.Name
345+
} else {
346+
i := int32(arg.Ordinal)
347+
param.Ordinal = &i
348+
}
349+
350+
switch t := arg.Value.(type) {
351+
case bool:
352+
b := arg.Value.(bool)
353+
param.Value.BooleanValue = &b
354+
param.Type = &ts[2]
355+
case string:
356+
s := arg.Value.(string)
357+
param.Value.StringValue = &s
358+
param.Type = &ts[0]
359+
case int:
360+
f := float64(t)
361+
param.Value.DoubleValue = &f
362+
param.Type = &ts[1]
363+
case uint:
364+
f := float64(t)
365+
param.Value.DoubleValue = &f
366+
param.Type = &ts[1]
367+
case int8:
368+
f := float64(t)
369+
param.Value.DoubleValue = &f
370+
param.Type = &ts[1]
371+
case uint8:
372+
f := float64(t)
373+
param.Value.DoubleValue = &f
374+
param.Type = &ts[1]
375+
case int16:
376+
f := float64(t)
377+
param.Value.DoubleValue = &f
378+
param.Type = &ts[1]
379+
case uint16:
380+
f := float64(t)
381+
param.Value.DoubleValue = &f
382+
param.Type = &ts[1]
383+
case int32:
384+
f := float64(t)
385+
param.Value.DoubleValue = &f
386+
param.Type = &ts[1]
387+
case uint32:
388+
f := float64(t)
389+
param.Value.DoubleValue = &f
390+
param.Type = &ts[1]
391+
case int64:
392+
f := float64(t)
393+
param.Value.DoubleValue = &f
394+
param.Type = &ts[1]
395+
case uint64:
396+
f := float64(t)
397+
param.Value.DoubleValue = &f
398+
param.Type = &ts[1]
399+
case float32:
400+
f := float64(t)
401+
param.Value.DoubleValue = &f
402+
param.Type = &ts[1]
403+
default:
404+
s := fmt.Sprintf("%s", arg.Value)
405+
param.Value.StringValue = &s
406+
param.Type = &ts[0]
407+
}
408+
409+
params = append(params, &param)
410+
}
411+
return params
412+
}
413+
340414
func (c *conn) pollOperation(ctx context.Context, opHandle *cli_service.TOperationHandle) (*cli_service.TGetOperationStatusResp, error) {
341415
corrId := driverctx.CorrelationIdFromContext(ctx)
342416
log := logger.WithContext(c.id, corrId, client.SprintGuid(opHandle.OperationId.GUID))

internal/cli_service/README.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

internal/cli_service/cli_service-consts.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)