Skip to content

feat: Allow local arithmetic execution in hybrid engine #1906

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

Merged
merged 4 commits into from
Jul 17, 2025
Merged
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
18 changes: 14 additions & 4 deletions bigframes/core/compile/polars/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import bigframes.operations.comparison_ops as comp_ops
import bigframes.operations.generic_ops as gen_ops
import bigframes.operations.numeric_ops as num_ops
import bigframes.operations.string_ops as string_ops

polars_installed = True
if TYPE_CHECKING:
Expand Down Expand Up @@ -146,6 +147,14 @@ def _(self, op: ops.ScalarOp, input: pl.Expr) -> pl.Expr:
def _(self, op: ops.ScalarOp, input: pl.Expr) -> pl.Expr:
return input.abs()

@compile_op.register(num_ops.FloorOp)
def _(self, op: ops.ScalarOp, input: pl.Expr) -> pl.Expr:
return input.floor()

@compile_op.register(num_ops.CeilOp)
def _(self, op: ops.ScalarOp, input: pl.Expr) -> pl.Expr:
return input.ceil()

@compile_op.register(num_ops.PosOp)
def _(self, op: ops.ScalarOp, input: pl.Expr) -> pl.Expr:
return input.__pos__()
Expand Down Expand Up @@ -182,10 +191,6 @@ def _(self, op: ops.ScalarOp, l_input: pl.Expr, r_input: pl.Expr) -> pl.Expr:
def _(self, op: ops.ScalarOp, l_input: pl.Expr, r_input: pl.Expr) -> pl.Expr:
return l_input // r_input

@compile_op.register(num_ops.FloorDivOp)
def _(self, op: ops.ScalarOp, l_input: pl.Expr, r_input: pl.Expr) -> pl.Expr:
return l_input // r_input

@compile_op.register(num_ops.ModOp)
def _(self, op: ops.ScalarOp, l_input: pl.Expr, r_input: pl.Expr) -> pl.Expr:
return l_input % r_input
Expand Down Expand Up @@ -270,6 +275,11 @@ def _(self, op: ops.ScalarOp, input: pl.Expr) -> pl.Expr:
# eg. We want "True" instead of "true" for bool to strin
return input.cast(_DTYPE_MAPPING[op.to_type], strict=not op.safe)

@compile_op.register(string_ops.StrConcatOp)
def _(self, op: ops.ScalarOp, l_input: pl.Expr, r_input: pl.Expr) -> pl.Expr:
assert isinstance(op, string_ops.StrConcatOp)
return pl.concat_str(l_input, r_input)

@dataclasses.dataclass(frozen=True)
class PolarsAggregateCompiler:
scalar_compiler = PolarsExpressionCompiler()
Expand Down
258 changes: 248 additions & 10 deletions bigframes/core/compile/polars/lowering.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,259 @@ def lower(self, expr: expression.OpExpression) -> expression.Expression:
return expr.op.as_expr(larg, rarg)


class LowerAddRule(op_lowering.OpLoweringRule):
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you help me understand why it's calling "lower" here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@property
def op(self) -> type[ops.ScalarOp]:
return numeric_ops.AddOp

def lower(self, expr: expression.OpExpression) -> expression.Expression:
assert isinstance(expr.op, numeric_ops.AddOp)
larg, rarg = expr.children[0], expr.children[1]

if (
larg.output_type == dtypes.BOOL_DTYPE
and rarg.output_type == dtypes.BOOL_DTYPE
):
int_result = expr.op.as_expr(
ops.AsTypeOp(to_type=dtypes.INT_DTYPE).as_expr(larg),
ops.AsTypeOp(to_type=dtypes.INT_DTYPE).as_expr(rarg),
)
return ops.AsTypeOp(to_type=dtypes.BOOL_DTYPE).as_expr(int_result)

if dtypes.is_string_like(larg.output_type) and dtypes.is_string_like(
rarg.output_type
):
return ops.strconcat_op.as_expr(larg, rarg)

if larg.output_type == dtypes.BOOL_DTYPE:
larg = ops.AsTypeOp(to_type=dtypes.INT_DTYPE).as_expr(larg)
if rarg.output_type == dtypes.BOOL_DTYPE:
rarg = ops.AsTypeOp(to_type=dtypes.INT_DTYPE).as_expr(rarg)

if (
larg.output_type == dtypes.DATE_DTYPE
and rarg.output_type == dtypes.TIMEDELTA_DTYPE
):
larg = ops.AsTypeOp(to_type=dtypes.DATETIME_DTYPE).as_expr(larg)

if (
larg.output_type == dtypes.TIMEDELTA_DTYPE
and rarg.output_type == dtypes.DATE_DTYPE
):
rarg = ops.AsTypeOp(to_type=dtypes.DATETIME_DTYPE).as_expr(rarg)

return expr.op.as_expr(larg, rarg)
Copy link
Contributor

Choose a reason for hiding this comment

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

Will the function throw an error if an argument has an invalid type? Or you just let Polar throws errors?



class LowerSubRule(op_lowering.OpLoweringRule):
@property
def op(self) -> type[ops.ScalarOp]:
return numeric_ops.SubOp

def lower(self, expr: expression.OpExpression) -> expression.Expression:
assert isinstance(expr.op, numeric_ops.SubOp)
larg, rarg = expr.children[0], expr.children[1]

if (
larg.output_type == dtypes.BOOL_DTYPE
and rarg.output_type == dtypes.BOOL_DTYPE
):
int_result = expr.op.as_expr(
ops.AsTypeOp(to_type=dtypes.INT_DTYPE).as_expr(larg),
ops.AsTypeOp(to_type=dtypes.INT_DTYPE).as_expr(rarg),
)
return ops.AsTypeOp(to_type=dtypes.BOOL_DTYPE).as_expr(int_result)

if larg.output_type == dtypes.BOOL_DTYPE:
larg = ops.AsTypeOp(to_type=dtypes.INT_DTYPE).as_expr(larg)
if rarg.output_type == dtypes.BOOL_DTYPE:
rarg = ops.AsTypeOp(to_type=dtypes.INT_DTYPE).as_expr(rarg)

if (
larg.output_type == dtypes.DATE_DTYPE
and rarg.output_type == dtypes.TIMEDELTA_DTYPE
):
larg = ops.AsTypeOp(to_type=dtypes.DATETIME_DTYPE).as_expr(larg)

return expr.op.as_expr(larg, rarg)


@dataclasses.dataclass
class LowerMulRule(op_lowering.OpLoweringRule):
@property
def op(self) -> type[ops.ScalarOp]:
return numeric_ops.MulOp

def lower(self, expr: expression.OpExpression) -> expression.Expression:
assert isinstance(expr.op, numeric_ops.MulOp)
larg, rarg = expr.children[0], expr.children[1]

if (
larg.output_type == dtypes.BOOL_DTYPE
and rarg.output_type == dtypes.BOOL_DTYPE
):
int_result = expr.op.as_expr(
ops.AsTypeOp(to_type=dtypes.INT_DTYPE).as_expr(larg),
ops.AsTypeOp(to_type=dtypes.INT_DTYPE).as_expr(rarg),
)
return ops.AsTypeOp(to_type=dtypes.BOOL_DTYPE).as_expr(int_result)

if (
larg.output_type == dtypes.BOOL_DTYPE
and rarg.output_type != dtypes.BOOL_DTYPE
):
larg = ops.AsTypeOp(to_type=dtypes.INT_DTYPE).as_expr(larg)
if (
rarg.output_type == dtypes.BOOL_DTYPE
and larg.output_type != dtypes.BOOL_DTYPE
):
rarg = ops.AsTypeOp(to_type=dtypes.INT_DTYPE).as_expr(rarg)

return expr.op.as_expr(larg, rarg)


class LowerDivRule(op_lowering.OpLoweringRule):
@property
def op(self) -> type[ops.ScalarOp]:
return numeric_ops.DivOp

def lower(self, expr: expression.OpExpression) -> expression.Expression:
assert isinstance(expr.op, numeric_ops.DivOp)

dividend = expr.children[0]
divisor = expr.children[1]

if dividend.output_type == dtypes.TIMEDELTA_DTYPE and dtypes.is_numeric(
divisor.output_type
):
# exact same as floordiv impl for timedelta
numeric_result = ops.floordiv_op.as_expr(
ops.AsTypeOp(to_type=dtypes.INT_DTYPE).as_expr(dividend), divisor
)
int_result = ops.AsTypeOp(to_type=dtypes.INT_DTYPE).as_expr(numeric_result)
return ops.AsTypeOp(to_type=dtypes.TIMEDELTA_DTYPE).as_expr(int_result)

if (
dividend.output_type == dtypes.BOOL_DTYPE
and divisor.output_type == dtypes.BOOL_DTYPE
):
int_result = expr.op.as_expr(
ops.AsTypeOp(to_type=dtypes.INT_DTYPE).as_expr(dividend),
ops.AsTypeOp(to_type=dtypes.INT_DTYPE).as_expr(divisor),
)
return ops.AsTypeOp(to_type=dtypes.BOOL_DTYPE).as_expr(int_result)

# polars divide doesn't like bools, convert to int always
# convert numerics to float always
if dividend.output_type == dtypes.BOOL_DTYPE:
dividend = ops.AsTypeOp(to_type=dtypes.INT_DTYPE).as_expr(dividend)
elif dividend.output_type in (dtypes.BIGNUMERIC_DTYPE, dtypes.NUMERIC_DTYPE):
dividend = ops.AsTypeOp(to_type=dtypes.FLOAT_DTYPE).as_expr(dividend)
if divisor.output_type == dtypes.BOOL_DTYPE:
divisor = ops.AsTypeOp(to_type=dtypes.INT_DTYPE).as_expr(divisor)

return numeric_ops.div_op.as_expr(dividend, divisor)


class LowerFloorDivRule(op_lowering.OpLoweringRule):
@property
def op(self) -> type[ops.ScalarOp]:
return numeric_ops.FloorDivOp

def lower(self, expr: expression.OpExpression) -> expression.Expression:
assert isinstance(expr.op, numeric_ops.FloorDivOp)

dividend = expr.children[0]
divisor = expr.children[1]
using_floats = (dividend.output_type == dtypes.FLOAT_DTYPE) or (
divisor.output_type == dtypes.FLOAT_DTYPE
)
inf_or_zero = (
expression.const(float("INF")) if using_floats else expression.const(0)
)
zero_result = ops.mul_op.as_expr(inf_or_zero, dividend)
divisor_is_zero = ops.eq_op.as_expr(divisor, expression.const(0))
return ops.where_op.as_expr(zero_result, divisor_is_zero, expr)

if (
dividend.output_type == dtypes.TIMEDELTA_DTYPE
and divisor.output_type == dtypes.TIMEDELTA_DTYPE
):
int_result = expr.op.as_expr(
ops.AsTypeOp(to_type=dtypes.INT_DTYPE).as_expr(dividend),
ops.AsTypeOp(to_type=dtypes.INT_DTYPE).as_expr(divisor),
)
return int_result
if dividend.output_type == dtypes.TIMEDELTA_DTYPE and dtypes.is_numeric(
divisor.output_type
):
# this is pretty fragile as zero will break it, and must fit back into int
numeric_result = expr.op.as_expr(
ops.AsTypeOp(to_type=dtypes.INT_DTYPE).as_expr(dividend), divisor
)
int_result = ops.AsTypeOp(to_type=dtypes.INT_DTYPE).as_expr(numeric_result)
return ops.AsTypeOp(to_type=dtypes.TIMEDELTA_DTYPE).as_expr(int_result)

if dividend.output_type == dtypes.BOOL_DTYPE:
dividend = ops.AsTypeOp(to_type=dtypes.INT_DTYPE).as_expr(dividend)
if divisor.output_type == dtypes.BOOL_DTYPE:
divisor = ops.AsTypeOp(to_type=dtypes.INT_DTYPE).as_expr(divisor)

if expr.output_type != dtypes.FLOAT_DTYPE:
# need to guard against zero divisor
# multiply dividend in this case to propagate nulls
return ops.where_op.as_expr(
ops.mul_op.as_expr(dividend, expression.const(0)),
ops.eq_op.as_expr(divisor, expression.const(0)),
numeric_ops.floordiv_op.as_expr(dividend, divisor),
)
else:
return expr.op.as_expr(dividend, divisor)


class LowerModRule(op_lowering.OpLoweringRule):
@property
def op(self) -> type[ops.ScalarOp]:
return numeric_ops.ModOp

def lower(self, expr: expression.OpExpression) -> expression.Expression:
og_expr = expr
assert isinstance(expr.op, numeric_ops.ModOp)
larg, rarg = expr.children[0], expr.children[1]

if (
larg.output_type == dtypes.TIMEDELTA_DTYPE
and rarg.output_type == dtypes.TIMEDELTA_DTYPE
):
larg_int = ops.AsTypeOp(to_type=dtypes.INT_DTYPE).as_expr(larg)
rarg_int = ops.AsTypeOp(to_type=dtypes.INT_DTYPE).as_expr(rarg)
int_result = expr.op.as_expr(larg_int, rarg_int)
w_zero_handling = ops.where_op.as_expr(
int_result,
ops.ne_op.as_expr(rarg_int, expression.const(0)),
ops.mul_op.as_expr(rarg_int, expression.const(0)),
)
return ops.AsTypeOp(to_type=dtypes.TIMEDELTA_DTYPE).as_expr(w_zero_handling)

if larg.output_type == dtypes.BOOL_DTYPE:
larg = ops.AsTypeOp(to_type=dtypes.INT_DTYPE).as_expr(larg)
if rarg.output_type == dtypes.BOOL_DTYPE:
rarg = ops.AsTypeOp(to_type=dtypes.INT_DTYPE).as_expr(rarg)

wo_bools = expr.op.as_expr(larg, rarg)

if og_expr.output_type == dtypes.INT_DTYPE:
return ops.where_op.as_expr(
wo_bools,
ops.ne_op.as_expr(rarg, expression.const(0)),
ops.mul_op.as_expr(rarg, expression.const(0)),
)
return wo_bools


def _coerce_comparables(expr1: expression.Expression, expr2: expression.Expression):
def _coerce_comparables(
expr1: expression.Expression,
expr2: expression.Expression,
*,
bools_only: bool = False
):
if bools_only:
if (
expr1.output_type != dtypes.BOOL_DTYPE
and expr2.output_type != dtypes.BOOL_DTYPE
):
return expr1, expr2

target_type = dtypes.coerce_to_common(expr1.output_type, expr2.output_type)
if expr1.output_type != target_type:
Expand Down Expand Up @@ -90,7 +323,12 @@ def _lower_cast(cast_op: ops.AsTypeOp, arg: expression.Expression):

POLARS_LOWERING_RULES = (
*LOWER_COMPARISONS,
LowerAddRule(),
LowerSubRule(),
LowerMulRule(),
LowerDivRule(),
LowerFloorDivRule(),
LowerModRule(),
)


Expand Down
Loading