Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions ibis/expr/types/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1988,14 +1988,26 @@ def mutate(
│ Adelie │ 2007 │ -7.22193 │
└─────────┴───────┴────────────────┘
"""

# the implementation of `mutate` should be kept in sync with that of `select`
# with the exception that mutate does not call bind on the fields already in this table (node.fields)
from ibis.expr.rewrites import rewrite_project_input
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a comment here to keep the implementation of .mutate() and .select() in sync except for the **node.fields part? This might help preserving feature parity between the two methods.


# string and integer inputs are going to be coerced to literals instead
# of interpreted as column references like in select
node = self.op()
values = self.bind(*exprs, **mutations)
values = self.bind(*exprs, **mutations) # bind new expressions/mutations
values = unwrap_aliases(values)
# allow overriding of fields, hence the mutation behavior
values = {**node.fields, **values}
return self.select(**values)

# we need to detect reductions which are either turned into window functions
# or scalar subqueries depending on whether they are originating from self
values = {
k: rewrite_project_input(v, relation=self.op()) for k, v in values.items()
}

# note that existing fields in node.fields will skip bind&dereferencing to improve performance
# (unless overridden by mutations in **values)
return ops.Project(self, {**node.fields, **values}).to_expr()

def select(
self,
Expand Down Expand Up @@ -2174,6 +2186,8 @@ def select(
│ 43.92193 │ 17.15117 │ 200.915205 │ 4201.754386 │
└────────────────┴───────────────┴───────────────────┴─────────────┘
"""
# note that if changes are made to implementation of select,
# corresponding changes may be needed in `.mutate()`
from ibis.expr.rewrites import rewrite_project_input

values = self.bind(*exprs, **named_exprs)
Expand Down
Loading