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 nested fields are incorrectly translated using JSON_EXTRACT #3713

Closed
Leonti opened this issue Jun 28, 2024 · 4 comments · Fixed by #3725
Closed

Snowflake nested fields are incorrectly translated using JSON_EXTRACT #3713

Leonti opened this issue Jun 28, 2024 · 4 comments · Fixed by #3725
Assignees

Comments

@Leonti
Copy link

Leonti commented Jun 28, 2024

Before you file an issue

  • Make sure you specify the "read" dialect eg. parse_one(sql, read="spark")
  • Make sure you specify the "write" dialect eg. ast.sql(dialect="duckdb")
  • Check if the issue still exists on main

Fully reproducible code snippet

print(sqlglot.transpile("select payload:testField from snowflake_table;", read="snowflake", write="trino", pretty=True)[0])

Output:

SELECT
  JSON_EXTRACT(payload, '$.testField')
FROM snowflake_table

Expected output:

SELECT
  payload.testField
FROM snowflake_table

payload:testField is how nested fields of VARIANT type are accessed in Snowflake, they should be translated into payload.testField for Trino, since they would be or row type in Trino and not JSON.
Official Documentation
Querying VARIANT nested fields in Snowflake
Trino nested field query example

@VaggelisD VaggelisD self-assigned this Jun 28, 2024
@VaggelisD
Copy link
Collaborator

VaggelisD commented Jun 28, 2024

Hey @Leonti, thanks for reporting this. Right now, SQLGlot generates mostly JSONPath functions when parsing semi-structured accesses (e.g. GET_PATH for VARIANT in Snowflake) instead of preserving the colon/dot notation.

However, I think the Snowflake -> Trino transpilation is more nuanced and will probably require type inference to work properly, so the fix will be a best-effort attempt without a schema.

@VaggelisD
Copy link
Collaborator

Quick follow up to gain more context, could you provide an example of how your data is structured in Snowflake and in what way would they be transformed to Trino's ROW type instead of JSON?

@Leonti
Copy link
Author

Leonti commented Jun 29, 2024

Hi @VaggelisD!
Thanks for looking into it so quickly!
I have an Iceberg table in S3 which is integrated with Snowflake.
The table has a couple of columns which are of type STRUCT in Iceberg, which translates into VARIANT when integrated with Snowflake. If I need to get access to nested fields using Snowflake I would do something like:

select payload:nestedField:nestedNestedField from iceberg_table;

because it's a VARIANT type.
Because fields are nested within a a STRUCT I've created a "convenience view" in snowflake that flattens the structure, something like this:

select 
payload:nestedField:nestedNestedField as field,
payload:anotherField as field2,
-- and so on, potentially tens of fields
from iceberg_table;

Since it's an Iceberg table it can also be queried directly from Athena/Trino. In Trino Iceberg/Parquet STRUCT is represented as ROW type, and instead of using : one would use . notation to access nested fields.
Since I already have a bunch of "convenience views" for Snowflake, I was hoping to use sqlglot to convert them to query the Iceberg table using Athena/Trino instead of converting them by hand.
When using Iceberg or Parquet on S3 the STRUCT type gets converted into Snowflake's VARIANT or Trino's ROW directly, so JSON is not involved there at all.

@VaggelisD
Copy link
Collaborator

VaggelisD commented Jul 2, 2024

Thanks for the detailed explanation! Did some research over the last days and came to the conclusion that there's ambiguity in transpiling Snowflake's VARIANT as it can store practically any data type. For example, it can store semi-structured objects such as JSON (in which case, the transpilation to Trino's JSON_EXTRACT is correct) or a structured OBJECT in the case of Iceberg's STRUCT, which should be transpiled as you've explained.

Since SQLGlot cannot analyze the data to infer what's stored under VARIANT, I'll attempt to make the transpilation configurable such that you can generate both versions (JSON_EXTRACT and dot notation, depending on the setting) on Snowflake -> Presto/Trino, hopefully that will help.

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

Successfully merging a pull request may close this issue.

2 participants