Skip to content
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
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.
# 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The expression zero_result * left_expr below is able to correctly handle the -inf case. This is demonstrated by the screenshot, which compares the results from the Ibis and SQLGlot frameworks. The Ibis results are shown on the left, while the SQLGlot results are on the right. To be mentioned, the sqlglot SQL is not compact and pending for optimizations.

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,7 +194,7 @@ 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}"
)


Expand Down
25 changes: 25 additions & 0 deletions bigframes/core/compile/sqlglot/expressions/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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")
_NEG_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 @@ -23,17 +23,10 @@

from bigframes import operations as ops
from bigframes.core.compile.constants import UNIT_TO_US_CONVERSION_FACTORS
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 @@ -52,7 +45,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 @@ -65,7 +58,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 @@ -78,7 +71,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 @@ -101,7 +94,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 @@ -177,7 +170,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 @@ -222,8 +215,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 @@ -235,8 +228,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 @@ -403,7 +396,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 @@ -416,7 +409,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 @@ -429,7 +422,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 @@ -512,7 +505,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 All @@ -534,8 +527,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