Skip to content

fix(duckdb): Correct RANGE to Spark SEQUENCE transpilation for single-element ranges#7294

Merged
georgesittas merged 1 commit intotobymao:mainfrom
ShubhamKapoor992:duck-to-spark
Mar 16, 2026
Merged

fix(duckdb): Correct RANGE to Spark SEQUENCE transpilation for single-element ranges#7294
georgesittas merged 1 commit intotobymao:mainfrom
ShubhamKapoor992:duck-to-spark

Conversation

@ShubhamKapoor992
Copy link
Copy Markdown
Contributor

@ShubhamKapoor992 ShubhamKapoor992 commented Mar 16, 2026

Fixes #7291

Summary

Fixes incorrect transpilation of DuckDB's RANGE(start, stop) to Spark's SEQUENCE when the range contains exactly one element.

Problem

When transpiling RANGE(1, 2) from DuckDB to Spark, the output was an empty array [] instead of [1].

Example:

-- DuckDB
SELECT RANGE(1, 2)  -- Returns [1]

-- Spark (before fix)
SELECT ARRAY()  -- Returns [] ❌

-- Spark (after fix)
SELECT SEQUENCE(1, 1)  -- Returns [1] ✅

Root Cause

The condition for determining when to return an empty array used >= instead of >:

Before: IF((stop - 1) >= start, ARRAY(), SEQUENCE(...))
After: IF((stop - 1) > start, ARRAY(), SEQUENCE(...))
When stop = 2 and start = 1:

Before: (2 - 1) >= 1 → 1 >= 1 → TRUE → returns ARRAY() ❌
After: (2 - 1) > 1 → 1 > 1 → FALSE → returns SEQUENCE(1, 1) ✅

Changes

sqlglot/dialects/dialect.py (lines 2280, 2284):

Changed exp.GTE to exp.GT for positive step condition
Changed exp.LTE to exp.LT for negative step condition

tests/dialects/test_duckdb.py:
Updated existing test with correct expected output
Added 2 new test cases for edge case validation

Testing

✅ All existing DuckDB tests pass (349 subtests)
✅ All Spark tests pass
✅ New edge case tests added and passing
✅ Verified with multiple range values (1-1, 1-2, 1-3, 1-5, 0-10)

@georgesittas georgesittas merged commit 4cb1a7f into tobymao:main Mar 16, 2026
11 checks passed
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.

duckdb's RANGE incorrectly transpiles to spark

3 participants