Skip to content

Commit

Permalink
refactor(sql): add LOWERED_OPS mapping for cleaner handling of oper…
Browse files Browse the repository at this point in the history
…ations implemented by "lowering" to simpler operations
  • Loading branch information
jcrist committed May 7, 2024
1 parent 9e11957 commit 7a9b4b6
Show file tree
Hide file tree
Showing 21 changed files with 185 additions and 201 deletions.
4 changes: 0 additions & 4 deletions ibis/backends/bigquery/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
exclude_unsupported_window_frame_from_ops,
exclude_unsupported_window_frame_from_rank,
exclude_unsupported_window_frame_from_row_number,
rewrite_sample_as_filter,
)
from ibis.common.temporal import DateUnit, IntervalUnit, TimestampUnit, TimeUnit
from ibis.expr.rewrites import rewrite_stringslice

_NAME_REGEX = re.compile(r'[^!"$()*,./;?@[\\\]^`{}~\n]+')

Expand All @@ -31,11 +29,9 @@ class BigQueryCompiler(SQLGlotCompiler):
type_mapper = BigQueryType
udf_type_mapper = BigQueryUDFType
rewrites = (
rewrite_sample_as_filter,
exclude_unsupported_window_frame_from_ops,
exclude_unsupported_window_frame_from_row_number,
exclude_unsupported_window_frame_from_rank,
rewrite_stringslice,
*SQLGlotCompiler.rewrites,
)

Expand Down
7 changes: 0 additions & 7 deletions ibis/backends/clickhouse/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,13 @@
from ibis.backends.sql.compiler import NULL, STAR, SQLGlotCompiler
from ibis.backends.sql.datatypes import ClickHouseType
from ibis.backends.sql.dialects import ClickHouse
from ibis.backends.sql.rewrites import rewrite_sample_as_filter
from ibis.expr.rewrites import rewrite_stringslice


class ClickHouseCompiler(SQLGlotCompiler):
__slots__ = ()

dialect = ClickHouse
type_mapper = ClickHouseType
rewrites = (
rewrite_sample_as_filter,
rewrite_stringslice,
*SQLGlotCompiler.rewrites,
)

UNSUPPORTED_OPS = (
ops.RowID,
Expand Down
7 changes: 0 additions & 7 deletions ibis/backends/datafusion/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
from ibis.backends.sql.compiler import FALSE, NULL, STAR, SQLGlotCompiler
from ibis.backends.sql.datatypes import DataFusionType
from ibis.backends.sql.dialects import DataFusion
from ibis.backends.sql.rewrites import rewrite_sample_as_filter
from ibis.common.temporal import IntervalUnit, TimestampUnit
from ibis.expr.operations.udf import InputType
from ibis.expr.rewrites import rewrite_stringslice
from ibis.formats.pyarrow import PyArrowType


Expand All @@ -26,11 +24,6 @@ class DataFusionCompiler(SQLGlotCompiler):

dialect = DataFusion
type_mapper = DataFusionType
rewrites = (
rewrite_sample_as_filter,
rewrite_stringslice,
*SQLGlotCompiler.rewrites,
)

UNSUPPORTED_OPS = (
ops.ArgMax,
Expand Down
16 changes: 2 additions & 14 deletions ibis/backends/druid/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,15 @@
from ibis.backends.sql.compiler import NULL, SQLGlotCompiler
from ibis.backends.sql.datatypes import DruidType
from ibis.backends.sql.dialects import Druid
from ibis.backends.sql.rewrites import (
rewrite_capitalize,
rewrite_sample_as_filter,
)
from ibis.expr.rewrites import rewrite_stringslice


class DruidCompiler(SQLGlotCompiler):
__slots__ = ()

dialect = Druid
type_mapper = DruidType
rewrites = (
rewrite_sample_as_filter,
rewrite_stringslice,
*(
rewrite
for rewrite in SQLGlotCompiler.rewrites
if rewrite is not rewrite_capitalize
),
)

LOWERED_OPS = {ops.Capitalize: None}

UNSUPPORTED_OPS = (
ops.ApproxMedian,
Expand Down
5 changes: 5 additions & 0 deletions ibis/backends/duckdb/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class DuckDBCompiler(SQLGlotCompiler):
dialect = DuckDB
type_mapper = DuckDBType

LOWERED_OPS = {
ops.Sample: None,
ops.StringSlice: None,
}

SIMPLE_OPS = {
ops.Arbitrary: "any_value",
ops.ArrayPosition: "list_indexof",
Expand Down
4 changes: 0 additions & 4 deletions ibis/backends/exasol/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
exclude_unsupported_window_frame_from_rank,
exclude_unsupported_window_frame_from_row_number,
rewrite_empty_order_by_window,
rewrite_sample_as_filter,
)
from ibis.expr.rewrites import rewrite_stringslice


class ExasolCompiler(SQLGlotCompiler):
Expand All @@ -25,12 +23,10 @@ class ExasolCompiler(SQLGlotCompiler):
dialect = Exasol
type_mapper = ExasolType
rewrites = (
rewrite_sample_as_filter,
exclude_unsupported_window_frame_from_ops,
exclude_unsupported_window_frame_from_rank,
exclude_unsupported_window_frame_from_row_number,
rewrite_empty_order_by_window,
rewrite_stringslice,
*SQLGlotCompiler.rewrites,
)

Expand Down
4 changes: 0 additions & 4 deletions ibis/backends/flink/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,17 @@
exclude_unsupported_window_frame_from_ops,
exclude_unsupported_window_frame_from_rank,
exclude_unsupported_window_frame_from_row_number,
rewrite_sample_as_filter,
)
from ibis.expr.rewrites import rewrite_stringslice


class FlinkCompiler(SQLGlotCompiler):
quoted = True
dialect = Flink
type_mapper = FlinkType
rewrites = (
rewrite_sample_as_filter,
exclude_unsupported_window_frame_from_row_number,
exclude_unsupported_window_frame_from_ops,
exclude_unsupported_window_frame_from_rank,
rewrite_stringslice,
*SQLGlotCompiler.rewrites,
)

Expand Down
4 changes: 0 additions & 4 deletions ibis/backends/impala/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
from ibis.backends.sql.dialects import Impala
from ibis.backends.sql.rewrites import (
rewrite_empty_order_by_window,
rewrite_sample_as_filter,
)
from ibis.expr.rewrites import rewrite_stringslice


class ImpalaCompiler(SQLGlotCompiler):
Expand All @@ -23,9 +21,7 @@ class ImpalaCompiler(SQLGlotCompiler):
dialect = Impala
type_mapper = ImpalaType
rewrites = (
rewrite_sample_as_filter,
rewrite_empty_order_by_window,
rewrite_stringslice,
*SQLGlotCompiler.rewrites,
)

Expand Down
4 changes: 0 additions & 4 deletions ibis/backends/mssql/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
exclude_unsupported_window_frame_from_row_number,
p,
replace,
rewrite_sample_as_filter,
)
from ibis.common.deferred import var
from ibis.expr.rewrites import rewrite_stringslice

y = var("y")
start = var("start")
Expand Down Expand Up @@ -59,11 +57,9 @@ class MSSQLCompiler(SQLGlotCompiler):
dialect = MSSQL
type_mapper = MSSQLType
rewrites = (
rewrite_sample_as_filter,
exclude_unsupported_window_frame_from_ops,
exclude_unsupported_window_frame_from_row_number,
rewrite_rows_range_order_by_window,
rewrite_stringslice,
*SQLGlotCompiler.rewrites,
)
copy_func_args = True
Expand Down
5 changes: 1 addition & 4 deletions ibis/backends/mysql/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
exclude_unsupported_window_frame_from_rank,
exclude_unsupported_window_frame_from_row_number,
rewrite_empty_order_by_window,
rewrite_sample_as_filter,
)
from ibis.common.patterns import replace
from ibis.expr.rewrites import p, rewrite_stringslice
from ibis.expr.rewrites import p


@replace(p.Limit)
Expand Down Expand Up @@ -50,12 +49,10 @@ class MySQLCompiler(SQLGlotCompiler):
type_mapper = MySQLType
rewrites = (
rewrite_limit,
rewrite_sample_as_filter,
exclude_unsupported_window_frame_from_ops,
exclude_unsupported_window_frame_from_rank,
exclude_unsupported_window_frame_from_row_number,
rewrite_empty_order_by_window,
rewrite_stringslice,
*SQLGlotCompiler.rewrites,
)

Expand Down
15 changes: 7 additions & 8 deletions ibis/backends/oracle/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
LastValue,
exclude_unsupported_window_frame_from_ops,
exclude_unsupported_window_frame_from_row_number,
replace_log2,
replace_log10,
lower_log2,
lower_log10,
rewrite_empty_order_by_window,
rewrite_sample_as_filter,
)
from ibis.expr.rewrites import rewrite_stringslice


@public
Expand All @@ -33,10 +31,6 @@ class OracleCompiler(SQLGlotCompiler):
exclude_unsupported_window_frame_from_row_number,
exclude_unsupported_window_frame_from_ops,
rewrite_empty_order_by_window,
rewrite_sample_as_filter,
rewrite_stringslice,
replace_log2,
replace_log10,
*SQLGlotCompiler.rewrites,
)

Expand All @@ -49,6 +43,11 @@ class OracleCompiler(SQLGlotCompiler):
NEG_INF = sge.Literal.number("-binary_double_infinity")
"""Backend's negative infinity literal."""

LOWERED_OPS = {
ops.Log2: lower_log2,
ops.Log10: lower_log10,
}

UNSUPPORTED_OPS = (
ops.ArgMax,
ops.ArgMin,
Expand Down
4 changes: 2 additions & 2 deletions ibis/backends/pandas/rewrites.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from ibis.common.collections import FrozenDict
from ibis.common.patterns import InstanceOf, replace
from ibis.common.typing import VarTuple # noqa: TCH001
from ibis.expr.rewrites import p, replace_parameter, rewrite_stringslice
from ibis.expr.rewrites import lower_stringslice, p, replace_parameter
from ibis.expr.schema import Schema
from ibis.util import gen_name

Expand Down Expand Up @@ -354,7 +354,7 @@ def plan(node, backend, params):
| rewrite_join
| rewrite_limit
| replace_parameter
| rewrite_stringslice
| lower_stringslice
| bind_unbound_table,
context=ctx,
)
Expand Down
4 changes: 2 additions & 2 deletions ibis/backends/polars/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
)
from ibis.backends.polars.compiler import translate
from ibis.backends.sql.dialects import Polars
from ibis.expr.rewrites import rewrite_stringslice
from ibis.expr.rewrites import lower_stringslice
from ibis.formats.polars import PolarsSchema
from ibis.util import gen_name, normalize_filename

Expand Down Expand Up @@ -406,7 +406,7 @@ def compile(

node = expr.as_table().op()
node = node.replace(
rewrite_join | replace_parameter | bind_unbound_table | rewrite_stringslice,
rewrite_join | replace_parameter | bind_unbound_table | lower_stringslice,
context={"params": params, "backend": self},
)

Expand Down
7 changes: 0 additions & 7 deletions ibis/backends/postgres/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
from ibis.backends.sql.compiler import NULL, STAR, SQLGlotCompiler
from ibis.backends.sql.datatypes import PostgresType
from ibis.backends.sql.dialects import Postgres
from ibis.backends.sql.rewrites import rewrite_sample_as_filter
from ibis.expr.rewrites import rewrite_stringslice


class PostgresUDFNode(ops.Value):
Expand All @@ -28,11 +26,6 @@ class PostgresCompiler(SQLGlotCompiler):

dialect = Postgres
type_mapper = PostgresType
rewrites = (
rewrite_sample_as_filter,
*SQLGlotCompiler.rewrites,
rewrite_stringslice,
)

NAN = sge.Literal.number("'NaN'::double precision")
POS_INF = sge.Literal.number("'Inf'::double precision")
Expand Down
7 changes: 5 additions & 2 deletions ibis/backends/pyspark/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from ibis.backends.sql.rewrites import FirstValue, LastValue, p
from ibis.common.patterns import replace
from ibis.config import options
from ibis.expr.rewrites import rewrite_stringslice
from ibis.util import gen_name


Expand Down Expand Up @@ -51,14 +50,18 @@ class PySparkCompiler(SQLGlotCompiler):

dialect = PySpark
type_mapper = PySparkType
rewrites = (offset_to_filter, *SQLGlotCompiler.rewrites, rewrite_stringslice)
rewrites = (offset_to_filter, *SQLGlotCompiler.rewrites)

UNSUPPORTED_OPS = (
ops.RowID,
ops.TimestampBucket,
ops.RandomUUID,
)

LOWERED_OPS = {
ops.Sample: None,
}

SIMPLE_OPS = {
ops.ArrayDistinct: "array_distinct",
ops.ArrayFlatten: "flatten",
Expand Down
14 changes: 8 additions & 6 deletions ibis/backends/snowflake/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
from ibis.backends.sql.rewrites import (
exclude_unsupported_window_frame_from_ops,
exclude_unsupported_window_frame_from_row_number,
replace_log2,
replace_log10,
lower_log2,
lower_log10,
rewrite_empty_order_by_window,
)
from ibis.expr.rewrites import rewrite_stringslice


class SnowflakeFuncGen(FuncGen):
Expand All @@ -39,12 +38,15 @@ class SnowflakeCompiler(SQLGlotCompiler):
exclude_unsupported_window_frame_from_row_number,
exclude_unsupported_window_frame_from_ops,
rewrite_empty_order_by_window,
rewrite_stringslice,
replace_log2,
replace_log10,
*SQLGlotCompiler.rewrites,
)

LOWERED_OPS = {
ops.Log2: lower_log2,
ops.Log10: lower_log10,
ops.Sample: None,
}

UNSUPPORTED_OPS = (
ops.ArrayMap,
ops.ArrayFilter,
Expand Down
Loading

0 comments on commit 7a9b4b6

Please sign in to comment.