Skip to content

Commit

Permalink
perf(rename): avoid unnecessary rewrites and dereferencing in rename
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Jul 20, 2024
1 parent 7e31f91 commit ceb0032
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions ibis/expr/types/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def __polars_result__(self, df: pl.DataFrame) -> Any:

return PolarsData.convert_table(df, self.schema())

def bind(self, *args, **kwargs):
def _fast_bind(self, *args, **kwargs):
# allow the first argument to be either a dictionary or a list of values
if len(args) == 1:
if isinstance(args[0], dict):
Expand All @@ -236,7 +236,10 @@ def bind(self, *args, **kwargs):
)
(value,) = bindings
values.append(value.name(key))
return values

def bind(self, *args, **kwargs):
values = self._fast_bind(*args, **kwargs)
# dereference the values to `self`
dm = DerefMap.from_targets(self.op())
result = []
Expand Down Expand Up @@ -2342,7 +2345,7 @@ def rename(
for new_name, old_name in substitutions.items():
col = self[old_name]
if old_name not in renamed:
renamed[old_name] = col.name(new_name)
renamed[old_name] = (new_name, col.op())
else:
raise ValueError(
"duplicate new names passed for renaming {old_name!r}"
Expand Down Expand Up @@ -2391,17 +2394,19 @@ def rename(name):
else:
rename = method

exprs = []
exprs = {}
fields = self.op().fields
for c in self.columns:
if c in renamed:
expr = renamed[c]
if (new_name_op := renamed.get(c)) is not None:
new_name, op = new_name_op
else:
expr = self[c]
if (name := rename(c)) is not None:
expr = expr.name(name)
exprs.append(expr)
op = fields[c]
if (new_name := rename(c)) is None or c == new_name:
new_name = c

exprs[new_name] = op

return self.select(exprs)
return ops.Project(self, exprs).to_expr()

def drop(self, *fields: str | Selector) -> Table:
"""Remove fields from a table.
Expand Down

0 comments on commit ceb0032

Please sign in to comment.