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

snowflake object_construct is converted to duckdb map with values of single type #1699

Closed
tekumara opened this issue May 28, 2023 · 3 comments

Comments

@tekumara
Copy link
Contributor

tekumara commented May 28, 2023

In Snowflake, OBJECT_CONSTRUCT can be used to construct an object from the given key-value pairs. Keys must be strings, but values can be any type and can differ from each other.

When converting OBJECT_CONSTRUCT to duckdb, it ends up as a map. Maps must have a single type for all keys, and a single type for all values.

eg:

sqlglot.parse_one("SELECT OBJECT_CONSTRUCT('a',1,'b','BBBB', 'c',null)", read="snowflake").sql(dialect="duckdb")

"SELECT MAP(LIST_VALUE('a', 'b', 'c'), LIST_VALUE(1, 'BBBB', NULL))"

Because the map/list must be of the same type, in this example the integer 1 becomes the string "1" when the expression is executed in duckdb:

D SELECT to_json(MAP(LIST_VALUE('a', 'b', 'c'), LIST_VALUE(1, 'BBBB', NULL)));
┌──────────────────────────────────────────────────────────────────────┐
│ to_json(map(list_value('a', 'b', 'c'), list_value(1, 'BBBB', NULL))) │
│                                 json                                 │
├──────────────────────────────────────────────────────────────────────┤
│ {"a":"1","b":"BBBB","c":null}                                        │
└──────────────────────────────────────────────────────────────────────┘

NB: to_json is used here to show that the value for thea key is a string.

@georgesittas
Copy link
Collaborator

Do you know if there's an equivalent concept in DuckDB to improve the transpilation? For example, would DuckDB's structs be more suitable here, or is there a difference in semantics?

@pkit
Copy link
Contributor

pkit commented May 28, 2023

Yup, struct should behave as needed, I think. As struct is the only type (besides list) that can contain other structs.
And in Snowflake OBJECT can contain other objects too.

@georgesittas
Copy link
Collaborator

georgesittas commented May 28, 2023

Ok, thanks for confirming. I'll look into this a bit closer and try to fix it on Monday.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants