Skip to content

Commit

Permalink
refactor(ir): unify ibis.common.pattern builders and `ibis.expr.def…
Browse files Browse the repository at this point in the history
…erred`

The unified implementation now lives in `ibis.expr.deferred` and is used by
`ibis.common.patterns` to construct replacement patterns and by `ibis.expr`
to construct deferred expressions.

The new implementation is more flexible and allows for more complex patterns
to be constructed. Also supports more placeholder variables in a single
deferred expression.

Supplemental but related changes:
- disallow type coercions in `pattern()` factory function by default, this
  prevents unintentional value coercions during pattern matching
- propagate rewritten children when matching on parent nodes, allowing to
  replace dependent nodes in a single pass
- push context variables as keyword arguments to factory functions
  • Loading branch information
kszucs authored and jcrist committed Oct 11, 2023
1 parent 6f77df9 commit 652ceab
Show file tree
Hide file tree
Showing 32 changed files with 1,846 additions and 1,258 deletions.
12 changes: 4 additions & 8 deletions ibis/backends/clickhouse/compiler/core.py
Expand Up @@ -23,21 +23,17 @@

import sqlglot as sg

import ibis.expr.analysis as an
import ibis.expr.operations as ops
import ibis.expr.types as ir
from ibis.backends.clickhouse.compiler.relations import translate_rel
from ibis.backends.clickhouse.compiler.values import translate_val
from ibis.common.patterns import Call, _
from ibis.expr.analysis import c, p, x, y
from ibis.common.deferred import _
from ibis.expr.analysis import c, find_first_base_table, p, x, y

if TYPE_CHECKING:
from collections.abc import Mapping


a = Call.namespace(an)


def _translate_node(node, **kwargs):
if isinstance(node, ops.Value):
return translate_val(node, **kwargs)
Expand Down Expand Up @@ -88,14 +84,14 @@ def fn(node, _, **kwargs):
# `translate_val` rule
params = {param.op(): value for param, value in params.items()}
replace_literals = p.ScalarParameter(dtype=x) >> (
lambda op, ctx: ops.Literal(value=params[op], dtype=ctx[x])
lambda _, x: ops.Literal(value=params[_], dtype=x)
)

# replace the right side of InColumn into a scalar subquery for sql
# backends
replace_in_column_with_table_array_view = p.InColumn(..., y) >> _.copy(
options=c.TableArrayView(
c.Selection(table=a.find_first_base_table(y), selections=(y,))
c.Selection(table=lambda _, y: find_first_base_table(y), selections=(y,))
),
)

Expand Down
2 changes: 1 addition & 1 deletion ibis/backends/polars/__init__.py
Expand Up @@ -386,7 +386,7 @@ def compile(
params = {param.op(): value for param, value in params.items()}
rule = Replace(
ops.ScalarParameter,
lambda op, ctx: ops.Literal(value=params[op], dtype=op.dtype),
lambda _: ops.Literal(value=params[_], dtype=_.dtype),
)
node = node.replace(rule)
expr = node.to_expr()
Expand Down

0 comments on commit 652ceab

Please sign in to comment.