Skip to content

Commit

Permalink
fix(mssql): exclude window frame from rank (#10302)
Browse files Browse the repository at this point in the history
## Description of changes

Resolves an error where the RANK window function added a `ROWS BETWEEN
UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING` window frame, which is
unsupported by MSSQL.

> The <rows or range clause/> of the OVER clause cannot be specified for
the RANK function. For more information, see [OVER Clause
(Transact-SQL)](https://learn.microsoft.com/en-us/sql/t-sql/queries/select-over-clause-transact-sql?view=sql-server-ver16).

## Issues closed

Resolves #10291
  • Loading branch information
IndexSeek authored Oct 13, 2024
1 parent 41ea0c7 commit 244876a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SELECT
[t0].[color],
[t0].[price],
RANK() OVER (PARTITION BY [t0].[color] ORDER BY CASE WHEN [t0].[price] IS NULL THEN 1 ELSE 0 END, [t0].[price] ASC) - 1 AS [MinRank()]
FROM [diamonds_sample] AS [t0]
11 changes: 11 additions & 0 deletions ibis/backends/mssql/tests/test_window.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from __future__ import annotations

import ibis


def test_rank_no_window_frame(con, snapshot):
t = ibis.table(schema=dict(color=str, price=int), name="diamonds_sample")
expr = t.mutate(ibis.rank().over(group_by="color", order_by="price"))
sql = ibis.to_sql(expr, dialect="mssql")

snapshot.assert_match(sql, "out.sql")
2 changes: 2 additions & 0 deletions ibis/backends/sql/compilers/mssql.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from ibis.backends.sql.dialects import MSSQL
from ibis.backends.sql.rewrites import (
exclude_unsupported_window_frame_from_ops,
exclude_unsupported_window_frame_from_rank,
exclude_unsupported_window_frame_from_row_number,
lower_sample,
p,
Expand Down Expand Up @@ -68,6 +69,7 @@ class MSSQLCompiler(SQLGlotCompiler):
rewrites = (
exclude_unsupported_window_frame_from_ops,
exclude_unsupported_window_frame_from_row_number,
exclude_unsupported_window_frame_from_rank,
rewrite_rows_range_order_by_window,
*SQLGlotCompiler.rewrites,
)
Expand Down

0 comments on commit 244876a

Please sign in to comment.