Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Prevent deferring filters with remote functions #1220

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions bigframes/core/compile/compiled.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import bigframes.core.sql
from bigframes.core.window_spec import RangeWindowBounds, RowsWindowBounds, WindowSpec
import bigframes.dtypes
import bigframes.operations
import bigframes.operations.aggregations as agg_ops

ORDER_ID_COLUMN = "bigframes_ordering_id"
Expand Down Expand Up @@ -132,12 +133,22 @@ def projection(
expression_id_pairs: typing.Tuple[typing.Tuple[ex.Expression, str], ...],
) -> T:
"""Apply an expression to the ArrayValue and assign the output to a column."""

# Remote ops are expensive so force reprojection before to ensure filters get applied first
any_remote_op = any(
ex.contains_op(expr, bigframes.operations.RemoteFunctionOp)
Copy link
Collaborator

@tswast tswast Dec 27, 2024

Choose a reason for hiding this comment

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

I'm a little bit concerned about hard coding remote function op here. Would it be better to flag each scalar operator as "expensive" / "cheap"?

Certainly there's a spectrum of costs possible, but a distinction could be made if there's an associated cost (e.g. cloud run CPU in this case) per call.

For example, LLM-style ops, BQML prediction (though that's not a scalar in BigQuery terms, though maybe could be in Bigframes eventually), could similarly have an "there's an extra cost associated here" flag.

for expr, _ in expression_id_pairs
)
if any_remote_op and len(self._predicates) > 0:
return self._reproject_to_table().projection(expression_id_pairs)

bindings = {col: self._get_ibis_column(col) for col in self.column_ids}
new_values = [
op_compiler.compile_expression(expression, bindings).name(id)
for expression, id in expression_id_pairs
]
result = self._select(tuple([*self._columns, *new_values])) # type: ignore

return result

def selection(
Expand Down
9 changes: 9 additions & 0 deletions bigframes/core/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ def free_var(id: str) -> UnboundVariableExpression:
return UnboundVariableExpression(id)


def contains_op(expr: Expression, op: type[bigframes.operations.ScalarOp]):
if isinstance(expr, OpExpression):
if isinstance(expr.op, op):
return True
else:
return any(contains_op(subexpr, op) for subexpr in expr.inputs)
return False


@dataclasses.dataclass(frozen=True)
class Aggregation(abc.ABC):
"""Represents windowing or aggregation over a column."""
Expand Down
Loading