Skip to content

Commit e0ac5e4

Browse files
vidigoatclaude
andauthored
fix: keep the last value of an exclusive series with a negative step [CLAUDE] (#8757)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent a9f1309 commit e0ac5e4

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

‎sqlglot/dialects/dialect.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1617,7 +1617,11 @@ def _generate_series_sql(self: Generator, expression: exp.GenerateSeries) -> str
16171617
if expression.args.get("is_end_exclusive"):
16181618
if exclusive_func_name:
16191619
return self.func(exclusive_func_name, start, end, step)
1620-
adjusted_end = exp.Sub(this=end, expression=exp.Literal.number(1))
1620+
# The last value is one before the end in the direction of the step
1621+
if step and step.is_int and t.cast(int, step.to_py()) < 0:
1622+
adjusted_end: exp.Expr = exp.Add(this=end, expression=exp.Literal.number(1))
1623+
else:
1624+
adjusted_end = exp.Sub(this=end, expression=exp.Literal.number(1))
16211625
return self.func(func_name, start, adjusted_end, step)
16221626

16231627
return self.func(func_name, start, end, step)

‎tests/dialects/test_duckdb.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,9 @@ def test_duckdb(self):
400400
self.validate_all(
401401
"SELECT RANGE(5, 1, -1)",
402402
write={
403+
"bigquery": "SELECT GENERATE_ARRAY(5, 1 + 1, -1)",
403404
"duckdb": "SELECT RANGE(5, 1, -1)",
405+
"postgres": "SELECT GENERATE_SERIES(5, 1 + 1, -1)",
404406
"spark": "SELECT SEQUENCE(5, 2, -1)",
405407
"snowflake": "SELECT ARRAY_GENERATE_RANGE(5, 1, -1)",
406408
},

0 commit comments

Comments
 (0)