Skip to content

Comments

feat(snowflake)!: transpile NTH_VALUE from Snowflake to DuckDB#6882

Merged
georgesittas merged 2 commits intomainfrom
transpile_nth_value_from_snowflake_to_duckdb
Feb 3, 2026
Merged

feat(snowflake)!: transpile NTH_VALUE from Snowflake to DuckDB#6882
georgesittas merged 2 commits intomainfrom
transpile_nth_value_from_snowflake_to_duckdb

Conversation

@fivetran-felixhuang
Copy link
Collaborator

@fivetran-felixhuang fivetran-felixhuang commented Jan 29, 2026

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

To transpile NTH_VALUE, we need to handle the following

  1. NTH_VALUE can have an optional FROM LAST or FROM FIRST. However, DuckDB doesn't have a way to support FROM LAST (counting from the last item). In this case we just ignore the FROM LAST part
  2. Snowflake's default frame for ranking window function is ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING (https://docs.snowflake.com/en/sql-reference/functions-window-syntax#usage-notes-for-window-frames), while in DuckDB the default frame is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW when an ORDER BY clause is present. So we want to set the default Snowflake window frame during transpilation. Will handle this in a follow up PR
  3. NULLS FIRST/LAST is not properly handled during tranpilation of Window functions.
    if isinstance(window, exp.Window) and window.args.get("spec"):
  4. The syntax for NTH_VALUE in DuckDB is nth_value(expr, nth[ ORDER BY ordering][ IGNORE NULLS]). However, the [ ORDER BY ordering] part is not parsed correctly. Will need to address this in a different PR

@fivetran-felixhuang fivetran-felixhuang marked this pull request as draft January 29, 2026 15:30
@github-actions
Copy link
Contributor

github-actions bot commented Jan 29, 2026

SQLGlot Integration Test Results

Comparing:

  • this branch (sqlglot:transpile_nth_value_from_snowflake_to_duckdb, sqlglot version: transpile_nth_value_from_snowflake_to_duckdb)
  • baseline (main, sqlglot version: 28.9.1.dev5)

⚠️ Limited to dialects: bigquery, duckdb, snowflake

By Dialect

dialect main sqlglot:transpile_nth_value_from_snowflake_to_duckdb difference links
bigquery -> bigquery 2592/2624 passed (98.8%) 2592/2624 passed (98.8%) No change full result / delta
bigquery -> duckdb 1847/2623 passed (70.4%) 1847/2623 passed (70.4%) No change full result / delta
duckdb -> duckdb 4003/4003 passed (100.0%) 4003/4003 passed (100.0%) No change full result / delta
snowflake -> duckdb 1315/2623 passed (50.1%) 1284/2623 passed (49.0%) ⬇ regressed by 1.2% full result / delta
snowflake -> snowflake 2606/2623 passed (99.4%) 2553/2623 passed (97.3%) ⬇ regressed by 2.0% full result / delta

Overall

main: 14496 total, 12363 passed (pass rate: 85.3%), sqlglot version: 28.9.1.dev5

sqlglot:transpile_nth_value_from_snowflake_to_duckdb: 14496 total, 12279 passed (pass rate: 84.7%), sqlglot version: transpile_nth_value_from_snowflake_to_duckdb

Difference: ⬇ regressed by 0.6%

@georgesittas
Copy link
Collaborator

@treysp @fivetran-felixhuang can we double-check these reported regressions in the test suite? Are they noise or?

@@ -1447,8 +1445,8 @@
self.validate_all(
"SELECT a FROM test AS t QUALIFY ROW_NUMBER() OVER (PARTITION BY a ORDER BY Z) = 1",
Copy link
Collaborator Author

@fivetran-felixhuang fivetran-felixhuang Jan 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently NULLS LAST/FIRST transpilation is not properly handled in window function, will need to investigate

if isinstance(window, exp.Window) and window.args.get("spec"):

Copy link
Collaborator

@georgesittas georgesittas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is still in draft mode, but wanted to raise awareness on the integration test suite regressions again and block the merge until we investigate them.

@fivetran-felixhuang fivetran-felixhuang force-pushed the transpile_nth_value_from_snowflake_to_duckdb branch from b4080c9 to 55f4798 Compare February 2, 2026 20:26
@fivetran-felixhuang fivetran-felixhuang force-pushed the transpile_nth_value_from_snowflake_to_duckdb branch from 55f4798 to 55cfc38 Compare February 2, 2026 22:38
@fivetran-felixhuang fivetran-felixhuang marked this pull request as ready for review February 2, 2026 22:39
@georgesittas georgesittas merged commit 84af655 into main Feb 3, 2026
1 of 7 checks passed
@georgesittas georgesittas deleted the transpile_nth_value_from_snowflake_to_duckdb branch February 3, 2026 12:42
georgesittas added a commit that referenced this pull request Feb 3, 2026
* support transpile NTH_VALUE from Snowflake to DuckDB

* Update sqlglot/dialects/snowflake.py

---------

Co-authored-by: Jo <46752250+georgesittas@users.noreply.github.com>
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