Skip to content

Commit

Permalink
Druid driver: Cast integer types to int (#4632)
Browse files Browse the repository at this point in the history
* Druid driver: Cast integer types to int

* Fix lint
  • Loading branch information
begelundmuller committed Apr 17, 2024
1 parent d5d718d commit 978647c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
78 changes: 75 additions & 3 deletions runtime/drivers/druid/druidsqldriver/druid_api_sql_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io"
"net/http"
"reflect"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -97,7 +98,44 @@ func (c *sqlConnection) QueryContext(ctx context.Context, query string, args []d
transformers := make([]func(any) (any, error), len(columns))
for i, c := range types {
transformers[i] = identityTransformer
if c == "TIMESTAMP" {
switch c {
case "TINYINT":
transformers[i] = func(v any) (any, error) {
switch v := v.(type) {
case float64:
return int8(v), nil
default:
return v, nil
}
}
case "SMALLINT":
transformers[i] = func(v any) (any, error) {
switch v := v.(type) {
case float64:
return int16(v), nil
default:
return v, nil
}
}
case "INTEGER":
transformers[i] = func(v any) (any, error) {
switch v := v.(type) {
case float64:
return int32(v), nil
default:
return v, nil
}
}
case "BIGINT":
transformers[i] = func(v any) (any, error) {
switch v := v.(type) {
case float64:
return int64(v), nil
default:
return v, nil
}
}
case "TIMESTAMP":
transformers[i] = func(v any) (any, error) {
t, err := time.Parse(time.RFC3339, v.(string))
if err != nil {
Expand All @@ -106,7 +144,7 @@ func (c *sqlConnection) QueryContext(ctx context.Context, query string, args []d

return t, nil
}
} else if c == "ARRAY" {
case "ARRAY":
transformers[i] = func(v any) (any, error) {
var l []any
err := json.Unmarshal([]byte(v.(string)), &l)
Expand All @@ -115,7 +153,7 @@ func (c *sqlConnection) QueryContext(ctx context.Context, query string, args []d
}
return l, nil
}
} else if c == "OTHER" {
case "OTHER":
transformers[i] = func(v any) (any, error) {
var l map[string]any
err := json.Unmarshal([]byte(v.(string)), &l)
Expand Down Expand Up @@ -182,6 +220,40 @@ func (dr *druidRows) Next(dest []driver.Value) error {
return nil
}

func (dr *druidRows) ColumnTypeScanType(index int) reflect.Type {
switch dr.types[index] {
case "BOOLEAN":
return reflect.TypeOf(true)
case "TINYINT":
return reflect.TypeOf(int8(0))
case "SMALLINT":
return reflect.TypeOf(int16(0))
case "INTEGER":
return reflect.TypeOf(int32(0))
case "BIGINT":
return reflect.TypeOf(int64(0))
case "FLOAT":
return reflect.TypeOf(float32(0))
case "DOUBLE":
return reflect.TypeOf(float64(0))
case "REAL":
return reflect.TypeOf(float64(0))
case "DECIMAL":
return reflect.TypeOf(float64(0))
case "CHAR":
return reflect.TypeOf("")
case "VARCHAR":
return reflect.TypeOf("")
case "TIMESTAMP":
return reflect.TypeOf(time.Time{})
case "DATE":
return reflect.TypeOf(time.Time{})
case "OTHER":
return reflect.TypeOf("")
}
return nil
}

func (dr *druidRows) ColumnTypeDatabaseTypeName(index int) string {
return dr.types[index]
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/queries/metricsview_aggregation.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func (q *MetricsViewAggregation) pivotDruid(ctx context.Context, rows *drivers.R
}
err = appender.AppendRow(appendValues...)
if err != nil {
return err
return fmt.Errorf("duckdb append failed: %w", err)
}
count++
if count > maxCount {
Expand Down

1 comment on commit 978647c

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 Published on https://ui.rilldata.com as production
🚀 Deployed on https://661f9d67365e339b4acbb597--rill-ui.netlify.app

Please sign in to comment.