Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SELECT
*
FROM `t` AS `t0`
QUALIFY
LOGICAL_OR(`t0`.`a` = 'hello') OVER (PARTITION BY `t0`.`x` ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
2 changes: 1 addition & 1 deletion ibis/backends/bigquery/tests/unit/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,4 +710,4 @@ def test_unreasonably_long_name():
com.IbisError,
match="BigQuery does not allow column names longer than 300 characters",
):
ibis.to_sql(expr, dialect="bigquery")
ibis.to_sql(expr, dialect="bigquery")
8 changes: 4 additions & 4 deletions ibis/backends/sql/compilers/bigquery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
FirstValue,
LastValue,
exclude_unsupported_window_frame_from_ops,
exclude_unsupported_window_frame_from_rank,
exclude_unsupported_window_frame_from_row_number,
rank_one_to_zero_index,
row_number_one_to_zero_index,
lower_sample,
split_select_distinct_with_order_by,
)
Expand Down Expand Up @@ -113,8 +113,8 @@ class BigQueryCompiler(SQLGlotCompiler):

rewrites = (
exclude_unsupported_window_frame_from_ops,
exclude_unsupported_window_frame_from_row_number,
exclude_unsupported_window_frame_from_rank,
row_number_one_to_zero_index,
rank_one_to_zero_index,
*SQLGlotCompiler.rewrites,
)
post_rewrites = (split_select_distinct_with_order_by,)
Expand Down
12 changes: 6 additions & 6 deletions ibis/backends/sql/compilers/exasol.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
FirstValue,
LastValue,
exclude_unsupported_window_frame_from_ops,
exclude_unsupported_window_frame_from_rank,
exclude_unsupported_window_frame_from_row_number,
rewrite_empty_order_by_window,
rank_one_to_zero_index,
row_number_one_to_zero_index,
add_order_by_to_empty_ranking_window_functions,
split_select_distinct_with_order_by,
)

Expand All @@ -27,9 +27,9 @@ class ExasolCompiler(SQLGlotCompiler):
type_mapper = ExasolType
rewrites = (
exclude_unsupported_window_frame_from_ops,
exclude_unsupported_window_frame_from_rank,
exclude_unsupported_window_frame_from_row_number,
rewrite_empty_order_by_window,
rank_one_to_zero_index,
row_number_one_to_zero_index,
add_order_by_to_empty_ranking_window_functions,
*SQLGlotCompiler.rewrites,
)

Expand Down
8 changes: 4 additions & 4 deletions ibis/backends/sql/compilers/flink.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
from ibis.backends.sql.dialects import Flink
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,
rank_one_to_zero_index,
row_number_one_to_zero_index,
split_select_distinct_with_order_by,
)

Expand Down Expand Up @@ -61,9 +61,9 @@ class FlinkCompiler(SQLGlotCompiler):
agg = FlinkAggGen()

rewrites = (
exclude_unsupported_window_frame_from_row_number,
row_number_one_to_zero_index,
exclude_unsupported_window_frame_from_ops,
exclude_unsupported_window_frame_from_rank,
rank_one_to_zero_index,
*SQLGlotCompiler.rewrites,
)
post_rewrites = (split_select_distinct_with_order_by,)
Expand Down
4 changes: 2 additions & 2 deletions ibis/backends/sql/compilers/impala.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
FirstValue,
LastValue,
lower_sample,
rewrite_empty_order_by_window,
add_order_by_to_empty_ranking_window_functions,
split_select_distinct_with_order_by,
)

Expand All @@ -27,7 +27,7 @@ class ImpalaCompiler(SQLGlotCompiler):
dialect = Impala
type_mapper = ImpalaType
rewrites = (
rewrite_empty_order_by_window,
add_order_by_to_empty_ranking_window_functions,
*SQLGlotCompiler.rewrites,
)
post_rewrites = (split_select_distinct_with_order_by,)
Expand Down
8 changes: 4 additions & 4 deletions ibis/backends/sql/compilers/mssql.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
FirstValue,
LastValue,
exclude_unsupported_window_frame_from_ops,
exclude_unsupported_window_frame_from_rank,
exclude_unsupported_window_frame_from_row_number,
rank_one_to_zero_index,
row_number_one_to_zero_index,
lower_sample,
p,
replace,
Expand Down Expand Up @@ -70,8 +70,8 @@ class MSSQLCompiler(SQLGlotCompiler):
type_mapper = MSSQLType
rewrites = (
exclude_unsupported_window_frame_from_ops,
exclude_unsupported_window_frame_from_row_number,
exclude_unsupported_window_frame_from_rank,
row_number_one_to_zero_index,
rank_one_to_zero_index,
rewrite_rows_range_order_by_window,
*SQLGlotCompiler.rewrites,
)
Expand Down
12 changes: 6 additions & 6 deletions ibis/backends/sql/compilers/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
from ibis.backends.sql.dialects import MySQL
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,
rewrite_empty_order_by_window,
rank_one_to_zero_index,
row_number_one_to_zero_index,
add_order_by_to_empty_ranking_window_functions,
)
from ibis.common.patterns import replace
from ibis.expr.rewrites import p
Expand Down Expand Up @@ -48,9 +48,9 @@ class MySQLCompiler(SQLGlotCompiler):
rewrites = (
rewrite_limit,
exclude_unsupported_window_frame_from_ops,
exclude_unsupported_window_frame_from_rank,
exclude_unsupported_window_frame_from_row_number,
rewrite_empty_order_by_window,
rank_one_to_zero_index,
row_number_one_to_zero_index,
add_order_by_to_empty_ranking_window_functions,
*SQLGlotCompiler.rewrites,
)

Expand Down
12 changes: 7 additions & 5 deletions ibis/backends/sql/compilers/oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
FirstValue,
LastValue,
exclude_unsupported_window_frame_from_ops,
exclude_unsupported_window_frame_from_row_number,
row_number_one_to_zero_index,
lower_log2,
lower_log10,
lower_sample,
rewrite_empty_order_by_window,
add_order_by_to_empty_ranking_window_functions,
split_select_distinct_with_order_by,
)

Expand All @@ -30,9 +30,9 @@ class OracleCompiler(SQLGlotCompiler):
dialect = Oracle
type_mapper = OracleType
rewrites = (
exclude_unsupported_window_frame_from_row_number,
row_number_one_to_zero_index,
exclude_unsupported_window_frame_from_ops,
rewrite_empty_order_by_window,
add_order_by_to_empty_ranking_window_functions,
*SQLGlotCompiler.rewrites,
)

Expand Down Expand Up @@ -419,7 +419,7 @@ def visit_WindowFunction(self, op, *, how, func, start, end, group_by, order_by)

if type(op.func) in (
# TODO: figure out REGR_* functions and also manage this list better
# Allowed windowing clause functions
# Allowed windowing clause functions.
ops.Mean, # "avg",
ops.Correlation, # "corr",
ops.Count, # "count",
Expand All @@ -432,6 +432,8 @@ def visit_WindowFunction(self, op, *, how, func, start, end, group_by, order_by)
ops.StandardDev, # "stddev","stddev_pop","stddev_samp",
ops.Sum, # "sum",
ops.Variance, # "var_pop","var_samp","variance",
ops.Any, # uses "max"
ops.All # uses "min"
):
if start is None:
start = {}
Expand Down
12 changes: 6 additions & 6 deletions ibis/backends/sql/compilers/singlestoredb.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
from ibis.backends.sql.datatypes import SingleStoreDBType
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,
rewrite_empty_order_by_window,
rank_one_to_zero_index,
row_number_one_to_zero_index,
add_order_by_to_empty_ranking_window_functions,
)
from ibis.common.patterns import replace
from ibis.expr.rewrites import p
Expand Down Expand Up @@ -47,9 +47,9 @@ class SingleStoreDBCompiler(MySQLCompiler):
rewrites = (
rewrite_limit,
exclude_unsupported_window_frame_from_ops,
exclude_unsupported_window_frame_from_rank,
exclude_unsupported_window_frame_from_row_number,
rewrite_empty_order_by_window,
rank_one_to_zero_index,
row_number_one_to_zero_index,
add_order_by_to_empty_ranking_window_functions,
*MySQLCompiler.rewrites,
)

Expand Down
8 changes: 4 additions & 4 deletions ibis/backends/sql/compilers/snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
FirstValue,
LastValue,
exclude_unsupported_window_frame_from_ops,
exclude_unsupported_window_frame_from_row_number,
row_number_one_to_zero_index,
lower_log2,
lower_log10,
lower_sample,
rewrite_empty_order_by_window,
add_order_by_to_empty_ranking_window_functions,
split_select_distinct_with_order_by,
x,
)
Expand Down Expand Up @@ -84,9 +84,9 @@ class SnowflakeCompiler(SQLGlotCompiler):
agg = AggGen(supports_order_by=True)

rewrites = (
exclude_unsupported_window_frame_from_row_number,
row_number_one_to_zero_index,
exclude_unsupported_window_frame_from_ops,
rewrite_empty_order_by_window,
add_order_by_to_empty_ranking_window_functions,
multiple_args_to_zipped_struct_field_access,
*SQLGlotCompiler.rewrites,
)
Expand Down
8 changes: 4 additions & 4 deletions ibis/backends/sql/compilers/trino.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
FirstValue,
LastValue,
exclude_unsupported_window_frame_from_ops,
exclude_unsupported_window_frame_from_rank,
exclude_unsupported_window_frame_from_row_number,
rank_one_to_zero_index,
row_number_one_to_zero_index,
lower_sample,
split_select_distinct_with_order_by,
)
Expand All @@ -42,8 +42,8 @@ class TrinoCompiler(SQLGlotCompiler):
agg = AggGen(supports_filter=True, supports_order_by=True)

rewrites = (
exclude_unsupported_window_frame_from_row_number,
exclude_unsupported_window_frame_from_rank,
row_number_one_to_zero_index,
rank_one_to_zero_index,
exclude_unsupported_window_frame_from_ops,
*SQLGlotCompiler.rewrites,
)
Expand Down
25 changes: 9 additions & 16 deletions ibis/backends/sql/rewrites.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,30 +507,23 @@ def add_one_to_nth_value_input(_, **kwargs):
return _.copy(nth=nth)


@replace(p.WindowFunction(order_by=()))
def rewrite_empty_order_by_window(_, **kwargs):
return _.copy(order_by=(ops.NULL,))


@replace(p.WindowFunction(p.RowNumber | p.NTile))
def exclude_unsupported_window_frame_from_row_number(_, **kwargs):
return ops.Subtract(_.copy(start=None, end=0), 1)
def row_number_one_to_zero_index(_, **kwargs):
return ops.Subtract(_, 1)


@replace(p.WindowFunction(p.MinRank | p.DenseRank, start=None))
def exclude_unsupported_window_frame_from_rank(_, **kwargs):
@replace(p.WindowFunction(p.MinRank | p.DenseRank))
def rank_one_to_zero_index(_, **kwargs):
return ops.Subtract(
_.copy(start=None, end=0, order_by=_.order_by or (ops.NULL,)), 1
_, 1
)


@replace(
p.WindowFunction(
p.Lag | p.Lead | p.PercentRank | p.CumeDist | p.Any | p.All, start=None
)
)
@replace(p.WindowFunction(p.Lag | p.Lead | p.PercentRank | p.CumeDist))
def exclude_unsupported_window_frame_from_ops(_, **kwargs):
return _.copy(start=None, end=0, order_by=_.order_by or (ops.NULL,))
""" Some window functions don't make sense with a frame, e.g.
navigation functions, so set an unbounded frame. """
return _.copy(start=None, end=None)


# Rewrite rules for lowering a high-level operation into one composed of more
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT
BOOL_AND("t0"."a") OVER (PARTITION BY "t0"."x" ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS "All(a)"
FROM "t" AS "t0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT
LOGICAL_AND(`t0`.`a`) OVER (PARTITION BY `t0`.`x` ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS `All_a`
FROM `t` AS `t0`
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT
MIN("t0"."a") OVER (PARTITION BY "t0"."x" ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS "All(a)"
FROM "t" AS "t0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT
BOOL_AND(`t0`.`a`) OVER (PARTITION BY `t0`.`x` ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS `All(a)`
FROM `t` AS `t0`
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT
BOOL_AND("t0"."a") OVER (PARTITION BY "t0"."x" ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS "All(a)"
FROM "t" AS "t0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT
BOOL_AND("t0"."a") OVER (PARTITION BY "t0"."x" ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS "All(a)"
FROM "t" AS "t0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT
BOOL_AND(CAST("t0"."a" AS BOOLEAN)) OVER (PARTITION BY "t0"."x" ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS "All(a)"
FROM "t" AS "t0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT
MIN("t0"."a") OVER (PARTITION BY "t0"."x" ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS "All(a)"
FROM "t" AS "t0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT
MIN(`t0`.`a`) OVER (PARTITION BY `t0`.`x` ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS `All(a)`
FROM `t` AS `t0`
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT
MIN(`t0`.`a`) OVER (PARTITION BY `t0`.`x` ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS `All(a)`
FROM `t` AS `t0`
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT
BOOL_AND("t0"."a") OVER (PARTITION BY "t0"."x" ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS "All(a)"
FROM "t" AS "t0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SELECT
IIF([t1].[All(a)] <> 0, 1, 0) AS [All(a)]
FROM (
SELECT
MIN(IIF([t0].[a] <> 0, 1, 0)) OVER (PARTITION BY [t0].[x] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS [All(a)]
FROM [t] AS [t0]
) AS [t1]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT
MIN(`t0`.`a`) OVER (PARTITION BY `t0`.`x` ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS `All(a)`
FROM `t` AS `t0`
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT
MIN("t0"."a") OVER (PARTITION BY "t0"."x" ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS "All(a)"
FROM "t" "t0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT
BOOL_AND("t0"."a") OVER (PARTITION BY "t0"."x" ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS "All(a)"
FROM "t" AS "t0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT
BOOL_AND(`t0`.`a`) OVER (PARTITION BY `t0`.`x` ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS `All(a)`
FROM `t` AS `t0`
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT
BOOL_AND("t0"."a") OVER (PARTITION BY "t0"."x" ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS "All(a)"
FROM "t" AS "t0"
Loading
Loading