fix: Store list-typed key columns as varchar in DuckDB by erezrokah · Pull Request #22976 · cloudquery/cloudquery · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions plugins/destination/duckdb/client/client_test.go
7 changes: 5 additions & 2 deletions plugins/destination/duckdb/client/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ func (*Client) normalizeColumns(tables schema.Tables) schema.Tables {
normalizedTable := *table
normalizedTable.Columns = make(schema.ColumnList, len(table.Columns))
for i := range table.Columns {
normalizedColumn := table.Columns[i]
if keyListColumn(normalizedColumn) {
normalizedColumn.Type = duckDBToArrow("varchar")
}
// In DuckDB, a PK column must be NOT NULL, so we need to make sure that the schema we're comparing to has the same
// constraint.
normalizedColumn := table.Columns[i]
if normalizedColumn.PrimaryKey {
normalizedColumn.NotNull = true
}
Expand Down Expand Up @@ -185,7 +188,7 @@ func (c *Client) createTableIfNotExist(ctx context.Context, tableName string, ta

var pks []string
for i, col := range table.Columns {
sqlType := arrowToDuckDB(col.Type)
sqlType := duckDBType(col)
fieldDef := sanitizeID(col.Name) + ` ` + sqlType
if col.PrimaryKey && !skipConstraints {
pks = append(pks, col.Name)
Expand Down
4 changes: 4 additions & 0 deletions plugins/destination/duckdb/client/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ func reverseTransformArray(dt arrow.DataType, arr arrow.Array) arrow.Array {
0, // we use 0 as offset for struct arrays, as the child arrays would already be sliced properly
))
case arrow.ListLikeType: // also handles maps
// Key list columns are stored as varchar, so they come back as strings.
if sarr, ok := arr.(*array.String); ok {
return reverseTransformFromString(dt, sarr)
}
if mapdt, ok := dt.(*arrow.MapType); ok {
if sarr, ok := arr.(*array.Binary); ok {
return reverseTransformMap(mapdt, sarr)
Expand Down
7 changes: 6 additions & 1 deletion plugins/destination/duckdb/client/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import (
func transformRecord(sc *arrow.Schema, rec arrow.RecordBatch) arrow.RecordBatch {
cols := make([]arrow.Array, rec.NumCols())
for i := 0; i < int(rec.NumCols()); i++ {
cols[i] = transformArray(rec.Column(i))
col := rec.Column(i)
if _, isList := col.DataType().(arrow.ListLikeType); isList && sc.Field(i).Type.ID() == arrow.STRING {
cols[i] = transformToStringArray(col)
continue
}
cols[i] = transformArray(col)
}
return array.NewRecordBatch(sc, cols, rec.NumRows())
}
Expand Down
12 changes: 10 additions & 2 deletions plugins/destination/duckdb/client/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@ import (
"strings"

"github.com/apache/arrow-go/v18/arrow"
"github.com/cloudquery/plugin-sdk/v4/schema"
"github.com/cloudquery/plugin-sdk/v4/types"
)

func transformSchemaForWriting(sc *arrow.Schema) *arrow.Schema {
func transformSchemaForWriting(table *schema.Table) *arrow.Schema {
sc := table.ToArrowSchema()
md := arrow.MetadataFrom(sc.Metadata().ToMap())
return arrow.NewSchema(transformFieldsForWriting(sc.Fields()), &md)
fields := transformFieldsForWriting(sc.Fields())
for i := range table.Columns {
if keyListColumn(table.Columns[i]) {
fields[i].Type = arrow.BinaryTypes.String
Comment thread
erezrokah marked this conversation as resolved.
}
}
return arrow.NewSchema(fields, &md)
}

func transformFieldsForWriting(fields []arrow.Field) []arrow.Field {
Expand Down
33 changes: 22 additions & 11 deletions plugins/destination/duckdb/client/write.go
Loading