Skip to content

fix(snowflake): map JSON type to VARIANT in Snowflake generator [CLAUDE]#7496

Merged
georgesittas merged 1 commit intotobymao:mainfrom
maxa-jonathan-hallee:fix/snowflake-json-to-variant-type-mapping
Apr 14, 2026
Merged

fix(snowflake): map JSON type to VARIANT in Snowflake generator [CLAUDE]#7496
georgesittas merged 1 commit intotobymao:mainfrom
maxa-jonathan-hallee:fix/snowflake-json-to-variant-type-mapping

Conversation

@maxa-jonathan-hallee
Copy link
Copy Markdown
Contributor

Summary

Snowflake has no native JSON data type — VARIANT is the equivalent. When transpiling CAST(x AS JSON) from DuckDB (or other dialects) to Snowflake, the JSON type was passed through unchanged, producing invalid SQL.

  • Add exp.DType.JSON: "VARIANT" to the Snowflake generator's TYPE_MAPPING
  • Add cross-dialect write test in test_duckdb.py verifying CAST(x AS JSON)CAST(x AS VARIANT) for Snowflake

Follows the same pattern as Fabric's JSON → VARCHAR mapping in generators/fabric.py.

References

  • Snowflake Semi-structured Data Types: "Snowflake can convert data from JSON format to an internal hierarchy of ARRAY, OBJECT, and VARIANT data and store that hierarchical data directly in a VARIANT value."

Snowflake has no native JSON data type — VARIANT is the equivalent.
When transpiling CAST(x AS JSON) from DuckDB (or other dialects) to
Snowflake, the JSON type was passed through unchanged, producing
invalid SQL.

Add exp.DType.JSON: "VARIANT" to the Snowflake generator's
TYPE_MAPPING, following the same pattern as Fabric's JSON→VARCHAR
mapping.

Ref: https://docs.snowflake.com/en/sql-reference/data-types-semistructured
"Snowflake can convert data from JSON format to an internal hierarchy
of ARRAY, OBJECT, and VARIANT data and store that hierarchical data
directly in a VARIANT value."
@georgesittas georgesittas merged commit c16630f into tobymao:main Apr 14, 2026
8 checks passed
maxa-jonathan-hallee added a commit to maxa-jonathan-hallee/sqlglot that referenced this pull request Apr 16, 2026
…ON_ARRAY [CLAUDE]

DuckDB JSON_ARRAY returns type JSON, which we map to Snowflake VARIANT
(see tobymao#7496). TO_JSON returns VARCHAR, breaking downstream JSON access
operators like JSON_EXTRACT_PATH_TEXT. TO_VARIANT preserves the
semi-structured semantics.
georgesittas pushed a commit that referenced this pull request Apr 17, 2026
…E] (#7497)

* fix(snowflake): transpile DuckDB JSON_ARRAY to ARRAY_CONSTRUCT [CLAUDE]

DuckDB's JSON_ARRAY function has no Snowflake equivalent — Snowflake
uses ARRAY_CONSTRUCT instead (JSON_ARRAY is not a recognized function).

- Add JSON_ARRAY → exp.JSONArray mapping in DuckDB parser FUNCTIONS
- Add exp.JSONArray → ARRAY_CONSTRUCT transform in Snowflake generator

Ref: https://docs.snowflake.com/en/sql-reference/functions/array_construct

* fix(snowflake): wrap JSON_ARRAY transpilation with TO_JSON for correct return type [CLAUDE]

DuckDB JSON_ARRAY returns a JSON string, not a native array. ARRAY_CONSTRUCT
alone returns a native array which breaks downstream JSON access operators.

* fix(snowflake): use TO_VARIANT instead of TO_JSON when transpiling JSON_ARRAY [CLAUDE]

DuckDB JSON_ARRAY returns type JSON, which we map to Snowflake VARIANT
(see #7496). TO_JSON returns VARCHAR, breaking downstream JSON access
operators like JSON_EXTRACT_PATH_TEXT. TO_VARIANT preserves the
semi-structured semantics.
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 this pull request may close these issues.

2 participants