-
Notifications
You must be signed in to change notification settings - Fork 608
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use rewrite rules to handle fillna/dropna in sql backends
- Loading branch information
Showing
3 changed files
with
66 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
"""Some common rewrite functions to be shared between backends.""" | ||
from __future__ import annotations | ||
|
||
import functools | ||
from collections.abc import Mapping | ||
|
||
import ibis.expr.datatypes as dt | ||
import ibis.expr.operations as ops | ||
from ibis.common.patterns import pattern, replace | ||
from ibis.util import Namespace | ||
|
||
p = Namespace(pattern, module=ops) | ||
|
||
|
||
@replace(p.FillNa) | ||
def rewrite_fillna(_): | ||
"""Rewrite FillNa expressions to use more common operations.""" | ||
if isinstance(_.replacements, Mapping): | ||
mapping = _.replacements | ||
else: | ||
mapping = { | ||
name: _.replacements | ||
for name, type in _.table.schema.items() | ||
if type.nullable | ||
} | ||
|
||
if not mapping: | ||
return _.table | ||
|
||
selections = [] | ||
for name in _.table.schema.names: | ||
col = ops.TableColumn(_.table, name) | ||
if (value := mapping.get(name)) is not None: | ||
col = ops.Alias(ops.Coalesce((col, value)), name) | ||
selections.append(col) | ||
|
||
return ops.Selection(_.table, selections, (), ()) | ||
|
||
|
||
@replace(p.DropNa) | ||
def rewrite_dropna(_): | ||
"""Rewrite DropNa expressions to use more common operations.""" | ||
if _.subset is None: | ||
columns = [ops.TableColumn(_.table, name) for name in _.table.schema.names] | ||
else: | ||
columns = _.subset | ||
|
||
if columns: | ||
preds = [ | ||
functools.reduce( | ||
ops.And if _.how == "any" else ops.Or, | ||
[ops.NotNull(c) for c in columns], | ||
) | ||
] | ||
elif _.how == "all": | ||
preds = [ops.Literal(False, dtype=dt.bool)] | ||
else: | ||
return _.table | ||
|
||
return ops.Selection(_.table, (), preds, ()) |