Git Product home page Git Product logo

Comments (4)

VaggelisD avatar VaggelisD commented on August 24, 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.

from sqlglot.

VaggelisD avatar VaggelisD commented on August 24, 2024

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?

from sqlglot.

Leonti avatar Leonti commented on August 24, 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.

from sqlglot.

VaggelisD avatar VaggelisD commented on August 24, 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.

from sqlglot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.