Skip to content

Commit

Permalink
chore: unalias everything that should not have expr AS name
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Sep 19, 2023
1 parent ab09157 commit 19f9918
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
22 changes: 13 additions & 9 deletions ibis/backends/duckdb/compiler/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import ibis.common.exceptions as com
import ibis.expr.datatypes as dt
import ibis.expr.operations as ops
from ibis.backends.base.sqlglot import unalias
from ibis.backends.duckdb.compiler.values import translate_val


Expand Down Expand Up @@ -54,18 +55,17 @@ def _selection(op: ops.Selection, *, table, needs_alias=False, aliases, **kw):
if predicates := op.predicates:
if join is not None:
sel = sg.select("*").from_(sel.subquery(aliases[op.table]))
sel = sel.where(sg.and_(*map(tr_val, predicates)))
sel = sel.where(sg.and_(*map(tr_val, map(unalias, predicates))))

if sort_keys := op.sort_keys:
sel = sel.order_by(*map(tr_val, sort_keys))
sel = sel.order_by(*map(tr_val, map(unalias, sort_keys)))

return sel


@translate_rel.register(ops.Aggregation)
def _aggregation(op: ops.Aggregation, *, table, **kw):
tr_val = partial(translate_val, **kw)
tr_val_no_alias = partial(translate_val, **kw)

by = tuple(map(tr_val, op.by))
metrics = tuple(map(tr_val, op.metrics))
Expand All @@ -84,13 +84,13 @@ def _aggregation(op: ops.Aggregation, *, table, **kw):
)

if predicates := op.predicates:
sel = sel.where(*map(tr_val_no_alias, predicates))
sel = sel.where(*map(tr_val, map(unalias, predicates)))

if having := op.having:
sel = sel.having(*map(tr_val_no_alias, having))
sel = sel.having(*map(tr_val, map(unalias, having)))

if sort_keys := op.sort_keys:
sel = sel.order_by(*map(tr_val_no_alias, sort_keys))
sel = sel.order_by(*map(tr_val, map(unalias, sort_keys)))

Check warning on line 93 in ibis/backends/duckdb/compiler/relations.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/duckdb/compiler/relations.py#L93

Added line #L93 was not covered by tests

return sel

Expand All @@ -111,7 +111,11 @@ def _aggregation(op: ops.Aggregation, *, table, **kw):
def _join(op: ops.Join, *, left, right, **kw):
predicates = op.predicates

on = sg.and_(*map(partial(translate_val, **kw), predicates)) if predicates else None
on = (
sg.and_(*map(partial(translate_val, **kw), map(unalias, predicates)))
if predicates
else None
)

join_type = _JOIN_TYPES[type(op)]
try:
Expand Down Expand Up @@ -220,9 +224,9 @@ def _dropna(op: ops.DropNa, *, table, **kw):
tr_val = partial(translate_val, **kw)
predicate = tr_val(raw_predicate)
try:
return table.where(predicate)
return table.where(unalias(predicate))
except AttributeError:
return sg.select("*").from_(table).where(predicate)
return sg.select("*").from_(table).where(unalias(predicate))


@translate_rel.register
Expand Down
6 changes: 3 additions & 3 deletions ibis/backends/duckdb/compiler/values.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ def _not(op, **kw):
def _apply_agg_filter(expr, *, where, **kw):
if where is not None:
return sg.exp.Filter(
this=expr, expression=sg.exp.Where(this=translate_val(where, **kw))
this=expr, expression=sg.exp.Where(this=translate_val(unalias(where), **kw))
)
return expr

Expand Down Expand Up @@ -870,7 +870,7 @@ def _array_repeat_op(op, **kw):
return sg.func(
"flatten",
sg.select(
sg.func("array", sg.select(arg).from_(sg.func("range", times)))
sg.func("array", sg.select(arg).from_(sg.func("range", unalias(times))))
).subquery(),
)

Expand Down Expand Up @@ -1167,7 +1167,7 @@ def _exists_subquery(op, **kw):
if isinstance(foreign_table, sg.exp.Select):
foreign_table = foreign_table.subquery()

predicate = sg.and_(*map(partial(translate_val, **kw), op.predicates))
predicate = sg.and_(*map(partial(translate_val, **kw), map(unalias, op.predicates)))
return sg.exp.Exists(this=sg.select(1).from_(foreign_table).where(predicate))


Expand Down

0 comments on commit 19f9918

Please sign in to comment.