Skip to content

chore: implement floordiv_op compiler #1995

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

Open
wants to merge 1 commit 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
44 changes: 41 additions & 3 deletions bigframes/core/compile/sqlglot/expressions/binary_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@

from __future__ import annotations

import bigframes_vendored.constants as constants
import bigframes_vendored.constants as bf_constants
import sqlglot.expressions as sge

from bigframes import dtypes
from bigframes import operations as ops
import bigframes.core.compile.sqlglot.expressions.constants as constants
from bigframes.core.compile.sqlglot.expressions.op_registration import OpRegistration
from bigframes.core.compile.sqlglot.expressions.typed_expr import TypedExpr

Expand Down Expand Up @@ -69,7 +70,7 @@ def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
return sge.Add(this=left.expr, expression=right.expr)

raise TypeError(
f"Cannot add type {left.dtype} and {right.dtype}. {constants.FEEDBACK_LINK}"
f"Cannot add type {left.dtype} and {right.dtype}. {bf_constants.FEEDBACK_LINK}"
)


Expand All @@ -89,6 +90,43 @@ def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
return result


@BINARY_OP_REGISTRATION.register(ops.floordiv_op)
def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
left_expr = left.expr
if left.dtype == dtypes.BOOL_DTYPE:
left_expr = sge.Cast(this=left_expr, to="INT64")
right_expr = right.expr
if right.dtype == dtypes.BOOL_DTYPE:
right_expr = sge.Cast(this=right_expr, to="INT64")

result: sge.Expression = sge.Cast(
this=sge.Floor(this=sge.func("IEEE_DIVIDE", left_expr, right_expr)), to="INT64"
)

# DIV(N, 0) will error in bigquery, but needs to return `0` for int, and
# `inf`` for float in BQ so we short-circuit in this case.
Comment on lines +106 to +107
Copy link
Collaborator

Choose a reason for hiding this comment

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

It'd be worth mentioning that this is also why we can't use SAFE_DIVIDE.

# Multiplying left by zero propogates nulls.
zero_result = (
constants._INF
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need to do -inf for negative values?

if (left.dtype == dtypes.FLOAT_DTYPE or right.dtype == dtypes.FLOAT_DTYPE)
else constants._ZERO
)
result = sge.Case(
ifs=[
sge.If(
this=sge.EQ(this=right_expr, expression=constants._ZERO),
true=zero_result * left_expr,
)
],
default=result,
)

if dtypes.is_numeric(right.dtype) and left.dtype == dtypes.TIMEDELTA_DTYPE:
result = sge.Cast(this=sge.Floor(this=result), to="INT64")

return result


@BINARY_OP_REGISTRATION.register(ops.ge_op)
def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
return sge.GTE(this=left.expr, expression=right.expr)
Expand Down Expand Up @@ -156,5 +194,5 @@ def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
return sge.Sub(this=left.expr, expression=right.expr)

raise TypeError(
f"Cannot subtract type {left.dtype} and {right.dtype}. {constants.FEEDBACK_LINK}"
f"Cannot subtract type {left.dtype} and {right.dtype}. {bf_constants.FEEDBACK_LINK}"
)
24 changes: 24 additions & 0 deletions bigframes/core/compile/sqlglot/expressions/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sqlglot.expressions as sge

_ZERO = sge.Cast(this=sge.convert(0), to="INT64")
_NAN = sge.Cast(this=sge.convert("NaN"), to="FLOAT64")
_INF = sge.Cast(this=sge.convert("Infinity"), to="FLOAT64")

# Approx Highest number you can pass in to EXP function and get a valid FLOAT64 result
# FLOAT64 has 11 exponent bits, so max values is about 2**(2**10)
# ln(2**(2**10)) == (2**10)*ln(2) ~= 709.78, so EXP(x) for x>709.78 will overflow.
_FLOAT64_EXP_BOUND = sge.convert(709.78)
39 changes: 16 additions & 23 deletions bigframes/core/compile/sqlglot/expressions/unary_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,10 @@
import sqlglot.expressions as sge

from bigframes import operations as ops
import bigframes.core.compile.sqlglot.expressions.constants as constants
from bigframes.core.compile.sqlglot.expressions.op_registration import OpRegistration
from bigframes.core.compile.sqlglot.expressions.typed_expr import TypedExpr

_NAN = sge.Cast(this=sge.convert("NaN"), to="FLOAT64")
_INF = sge.Cast(this=sge.convert("Infinity"), to="FLOAT64")

# Approx Highest number you can pass in to EXP function and get a valid FLOAT64 result
# FLOAT64 has 11 exponent bits, so max values is about 2**(2**10)
# ln(2**(2**10)) == (2**10)*ln(2) ~= 709.78, so EXP(x) for x>709.78 will overflow.
_FLOAT64_EXP_BOUND = sge.convert(709.78)

UNARY_OP_REGISTRATION = OpRegistration()


Expand All @@ -51,7 +44,7 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
ifs=[
sge.If(
this=expr.expr < sge.convert(1),
true=_NAN,
true=constants._NAN,
)
],
default=sge.func("ACOSH", expr.expr),
Expand All @@ -64,7 +57,7 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
ifs=[
sge.If(
this=sge.func("ABS", expr.expr) > sge.convert(1),
true=_NAN,
true=constants._NAN,
)
],
default=sge.func("ACOS", expr.expr),
Expand All @@ -77,7 +70,7 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
ifs=[
sge.If(
this=sge.func("ABS", expr.expr) > sge.convert(1),
true=_NAN,
true=constants._NAN,
)
],
default=sge.func("ASIN", expr.expr),
Expand All @@ -100,7 +93,7 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
ifs=[
sge.If(
this=sge.func("ABS", expr.expr) > sge.convert(1),
true=_NAN,
true=constants._NAN,
)
],
default=sge.func("ATANH", expr.expr),
Expand Down Expand Up @@ -176,7 +169,7 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
ifs=[
sge.If(
this=sge.func("ABS", expr.expr) > sge.convert(709.78),
true=_INF,
true=constants._INF,
)
],
default=sge.func("COSH", expr.expr),
Expand Down Expand Up @@ -221,8 +214,8 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
return sge.Case(
ifs=[
sge.If(
this=expr.expr > _FLOAT64_EXP_BOUND,
true=_INF,
this=expr.expr > constants._FLOAT64_EXP_BOUND,
true=constants._INF,
)
],
default=sge.func("EXP", expr.expr),
Expand All @@ -234,8 +227,8 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
return sge.Case(
ifs=[
sge.If(
this=expr.expr > _FLOAT64_EXP_BOUND,
true=_INF,
this=expr.expr > constants._FLOAT64_EXP_BOUND,
true=constants._INF,
)
],
default=sge.func("EXP", expr.expr),
Expand Down Expand Up @@ -382,7 +375,7 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
ifs=[
sge.If(
this=expr.expr < sge.convert(0),
true=_NAN,
true=constants._NAN,
)
],
default=sge.Ln(this=expr.expr),
Expand All @@ -395,7 +388,7 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
ifs=[
sge.If(
this=expr.expr < sge.convert(0),
true=_NAN,
true=constants._NAN,
)
],
default=sge.Log(this=expr.expr, expression=sge.convert(10)),
Expand All @@ -408,7 +401,7 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
ifs=[
sge.If(
this=expr.expr < sge.convert(-1),
true=_NAN,
true=constants._NAN,
)
],
default=sge.Ln(this=sge.convert(1) + expr.expr),
Expand Down Expand Up @@ -476,7 +469,7 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
ifs=[
sge.If(
this=expr.expr < sge.convert(0),
true=_NAN,
true=constants._NAN,
)
],
default=sge.Sqrt(this=expr.expr),
Expand Down Expand Up @@ -523,8 +516,8 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
return sge.Case(
ifs=[
sge.If(
this=sge.func("ABS", expr.expr) > _FLOAT64_EXP_BOUND,
true=sge.func("SIGN", expr.expr) * _INF,
this=sge.func("ABS", expr.expr) > constants._FLOAT64_EXP_BOUND,
true=sge.func("SIGN", expr.expr) * constants._INF,
)
],
default=sge.func("SINH", expr.expr),
Expand Down
4 changes: 2 additions & 2 deletions tests/system/small/engines/test_numeric_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def test_engines_project_div_durations(
assert_equivalence_execution(arr.node, REFERENCE_ENGINE, engine)


@pytest.mark.parametrize("engine", ["polars", "bq"], indirect=True)
@pytest.mark.parametrize("engine", ["polars", "bq", "bq-sqlglot"], indirect=True)
def test_engines_project_floordiv(
scalars_array_value: array_value.ArrayValue,
engine,
Expand All @@ -130,7 +130,7 @@ def test_engines_project_floordiv(
assert_equivalence_execution(arr.node, REFERENCE_ENGINE, engine)


@pytest.mark.parametrize("engine", ["polars", "bq"], indirect=True)
@pytest.mark.parametrize("engine", ["polars", "bq", "bq-sqlglot"], indirect=True)
def test_engines_project_floordiv_durations(
scalars_array_value: array_value.ArrayValue, engine
):
Expand Down
Loading