Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runtime: fix uuid type for postgres connector #4965

Merged
merged 2 commits into from
May 28, 2024
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
5 changes: 3 additions & 2 deletions runtime/drivers/duckdb/transporter_postgres_to_duckDB_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
var sqlStmt = `CREATE TYPE country AS ENUM ('IND', 'AUS', 'SA', 'NZ');
CREATE TABLE all_datatypes (
id serial PRIMARY KEY,
uuid UUID,
name text,
age integer,
is_married boolean,
Expand All @@ -45,9 +46,9 @@ var sqlStmt = `CREATE TYPE country AS ENUM ('IND', 'AUS', 'SA', 'NZ');
emp_salary NUMERIC,
country country
);
INSERT INTO all_datatypes (name, age, is_married, date_of_birth, time_of_day, created_at, json, json_data, bit,bit_varying, character, character_varying, bpchar, smallint, text, timestamptz, float4, float8, int2, int4, int8, int8_array, timestamptz_array, emp_salary, country)
INSERT INTO all_datatypes (uuid, name, age, is_married, date_of_birth, time_of_day, created_at, json, json_data, bit,bit_varying, character, character_varying, bpchar, smallint, text, timestamptz, float4, float8, int2, int4, int8, int8_array, timestamptz_array, emp_salary, country)
VALUES
('John Doe', 30, true, '1983-03-08', '12:35:00', '2023-09-12 12:46:55', '{"name": "John Doe", "age": 30, "salary": 100000}', '{"name": "John Doe", "age": 30, "salary": 100000}', b'1',b'10101010', 'a', 'ab', 'abcd', 123, 'This is a text string.', '2023-09-12 12:46:55+05:30', 23.2, 123.45, 1, 1234, 1234567, Array[1234567, 7654312], Array[timestamp'2023-09-12 12:46:55+05:30', timestamp'2023-10-12 12:46:55+05:30'], 38500000000000.71256565656563, 'IND');
(gen_random_uuid(), 'John Doe', 30, true, '1983-03-08', '12:35:00', '2023-09-12 12:46:55', '{"name": "John Doe", "age": 30, "salary": 100000}', '{"name": "John Doe", "age": 30, "salary": 100000}', b'1',b'10101010', 'a', 'ab', 'abcd', 123, 'This is a text string.', '2023-09-12 12:46:55+05:30', 23.2, 123.45, 1, 1234, 1234567, Array[1234567, 7654312], Array[timestamp'2023-09-12 12:46:55+05:30', timestamp'2023-10-12 12:46:55+05:30'], 38500000000000.71256565656563, 'IND');
`

func TestTransfer(t *testing.T) {
Expand Down
18 changes: 18 additions & 0 deletions runtime/drivers/duckdb/transporter_sqlstore_to_duckDB.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ func (s *sqlStoreToDuckDB) transferFromRowIterator(ctx context.Context, iter dri
}
return err
}
if err := convert(row, schema); err != nil { // duckdb specific datatype conversion
return err
}

if err := a.AppendRow(row...); err != nil {
return err
Expand Down Expand Up @@ -224,6 +227,21 @@ func CreateTableQuery(schema *runtimev1.StructType, name string) (string, error)
return query, nil
}

func convert(row []driver.Value, schema *runtimev1.StructType) error {
for i, v := range row {
if schema.Fields[i].Type.Code == runtimev1.Type_CODE_UUID {
val, ok := v.([16]byte)
if !ok {
return fmt.Errorf("unknown type for UUID field %s: %T", schema.Fields[i].Name, v)
}
var uuid duckdb.UUID
copy(uuid[:], val[:])
row[i] = uuid
}
}
return nil
}

func pbTypeToDuckDB(t *runtimev1.Type) (string, error) {
code := t.Code
switch code {
Expand Down
7 changes: 1 addition & 6 deletions runtime/drivers/postgres/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"
"time"

"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1"
)
Expand Down Expand Up @@ -248,11 +247,7 @@ func (m *uuidMapper) runtimeType() *runtimev1.Type {
func (m *uuidMapper) value(pgxVal any) (any, error) {
switch b := pgxVal.(type) {
case [16]byte:
id, err := uuid.FromBytes(b[:])
if err != nil {
return nil, err
}
return id.String(), nil
return b, nil
default:
return nil, fmt.Errorf("uuidMapper: unsupported type %v", b)
}
Expand Down
Loading