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
55 changes: 45 additions & 10 deletions bigframes/core/compile/sqlglot/expressions/binary_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,51 @@ def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
)


@BINARY_OP_REGISTRATION.register(ops.div_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.func("IEEE_DIVIDE", left_expr, right_expr)
if left.dtype == dtypes.TIMEDELTA_DTYPE and dtypes.is_numeric(right.dtype):
return sge.Cast(this=sge.Floor(this=result), to="INT64")
else:
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)


@BINARY_OP_REGISTRATION.register(ops.JSONSet)
def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
return sge.func("JSON_SET", left.expr, sge.convert(op.json_path), right.expr)


@BINARY_OP_REGISTRATION.register(ops.mul_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.Mul(this=left_expr, expression=right_expr)

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


@BINARY_OP_REGISTRATION.register(ops.sub_op)
def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
if dtypes.is_numeric(left.dtype) and dtypes.is_numeric(right.dtype):
Expand Down Expand Up @@ -113,13 +158,3 @@ def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
raise TypeError(
f"Cannot subtract type {left.dtype} and {right.dtype}. {constants.FEEDBACK_LINK}"
)


@BINARY_OP_REGISTRATION.register(ops.ge_op)
def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
return sge.GTE(this=left.expr, expression=right.expr)


@BINARY_OP_REGISTRATION.register(ops.JSONSet)
def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
return sge.func("JSON_SET", left.expr, sge.convert(op.json_path), right.expr)
6 changes: 3 additions & 3 deletions tests/system/small/engines/test_numeric_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def test_engines_project_sub(
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_mul(
scalars_array_value: array_value.ArrayValue,
engine,
Expand All @@ -80,7 +80,7 @@ def test_engines_project_mul(
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_div(scalars_array_value: array_value.ArrayValue, engine):
# TODO: Duration div is sensitive to zeroes
# TODO: Numeric col is sensitive to scale shifts
Expand All @@ -90,7 +90,7 @@ def test_engines_project_div(scalars_array_value: array_value.ArrayValue, engine
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_div_durations(
scalars_array_value: array_value.ArrayValue, engine
):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
WITH `bfcte_0` AS (
SELECT
`bool_col` AS `bfcol_0`,
`int64_col` AS `bfcol_1`,
`rowindex` AS `bfcol_2`
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
), `bfcte_1` AS (
SELECT
*,
`bfcol_2` AS `bfcol_6`,
`bfcol_1` AS `bfcol_7`,
`bfcol_0` AS `bfcol_8`,
IEEE_DIVIDE(`bfcol_1`, `bfcol_1`) AS `bfcol_9`
FROM `bfcte_0`
), `bfcte_2` AS (
SELECT
*,
`bfcol_6` AS `bfcol_14`,
`bfcol_7` AS `bfcol_15`,
`bfcol_8` AS `bfcol_16`,
`bfcol_9` AS `bfcol_17`,
IEEE_DIVIDE(`bfcol_7`, 1) AS `bfcol_18`
FROM `bfcte_1`
), `bfcte_3` AS (
SELECT
*,
`bfcol_14` AS `bfcol_24`,
`bfcol_15` AS `bfcol_25`,
`bfcol_16` AS `bfcol_26`,
`bfcol_17` AS `bfcol_27`,
`bfcol_18` AS `bfcol_28`,
IEEE_DIVIDE(`bfcol_15`, CAST(`bfcol_16` AS INT64)) AS `bfcol_29`
FROM `bfcte_2`
), `bfcte_4` AS (
SELECT
*,
`bfcol_24` AS `bfcol_36`,
`bfcol_25` AS `bfcol_37`,
`bfcol_26` AS `bfcol_38`,
`bfcol_27` AS `bfcol_39`,
`bfcol_28` AS `bfcol_40`,
`bfcol_29` AS `bfcol_41`,
IEEE_DIVIDE(CAST(`bfcol_26` AS INT64), `bfcol_25`) AS `bfcol_42`
FROM `bfcte_3`
)
SELECT
`bfcol_36` AS `rowindex`,
`bfcol_37` AS `int64_col`,
`bfcol_38` AS `bool_col`,
`bfcol_39` AS `int_div_int`,
`bfcol_40` AS `int_div_1`,
`bfcol_41` AS `int_div_bool`,
`bfcol_42` AS `bool_div_int`
FROM `bfcte_4`
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
WITH `bfcte_0` AS (
SELECT
`int64_col` AS `bfcol_0`,
`rowindex` AS `bfcol_1`,
`timestamp_col` AS `bfcol_2`
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
), `bfcte_1` AS (
SELECT
*,
`bfcol_1` AS `bfcol_6`,
`bfcol_2` AS `bfcol_7`,
`bfcol_0` AS `bfcol_8`,
CAST(FLOOR(IEEE_DIVIDE(86400000000, `bfcol_0`)) AS INT64) AS `bfcol_9`
FROM `bfcte_0`
)
SELECT
`bfcol_6` AS `rowindex`,
`bfcol_7` AS `timestamp_col`,
`bfcol_8` AS `int64_col`,
`bfcol_9` AS `timedelta_div_numeric`
FROM `bfcte_1`
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
WITH `bfcte_0` AS (
SELECT
`bool_col` AS `bfcol_0`,
`int64_col` AS `bfcol_1`,
`rowindex` AS `bfcol_2`
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
), `bfcte_1` AS (
SELECT
*,
`bfcol_2` AS `bfcol_6`,
`bfcol_1` AS `bfcol_7`,
`bfcol_0` AS `bfcol_8`,
`bfcol_1` * `bfcol_1` AS `bfcol_9`
FROM `bfcte_0`
), `bfcte_2` AS (
SELECT
*,
`bfcol_6` AS `bfcol_14`,
`bfcol_7` AS `bfcol_15`,
`bfcol_8` AS `bfcol_16`,
`bfcol_9` AS `bfcol_17`,
`bfcol_7` * 1 AS `bfcol_18`
FROM `bfcte_1`
), `bfcte_3` AS (
SELECT
*,
`bfcol_14` AS `bfcol_24`,
`bfcol_15` AS `bfcol_25`,
`bfcol_16` AS `bfcol_26`,
`bfcol_17` AS `bfcol_27`,
`bfcol_18` AS `bfcol_28`,
`bfcol_15` * CAST(`bfcol_16` AS INT64) AS `bfcol_29`
FROM `bfcte_2`
), `bfcte_4` AS (
SELECT
*,
`bfcol_24` AS `bfcol_36`,
`bfcol_25` AS `bfcol_37`,
`bfcol_26` AS `bfcol_38`,
`bfcol_27` AS `bfcol_39`,
`bfcol_28` AS `bfcol_40`,
`bfcol_29` AS `bfcol_41`,
CAST(`bfcol_26` AS INT64) * `bfcol_25` AS `bfcol_42`
FROM `bfcte_3`
)
SELECT
`bfcol_36` AS `rowindex`,
`bfcol_37` AS `int64_col`,
`bfcol_38` AS `bool_col`,
`bfcol_39` AS `int_mul_int`,
`bfcol_40` AS `int_mul_1`,
`bfcol_41` AS `int_mul_bool`,
`bfcol_42` AS `bool_mul_int`
FROM `bfcte_4`
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
WITH `bfcte_0` AS (
SELECT
`int64_col` AS `bfcol_0`,
`rowindex` AS `bfcol_1`,
`timestamp_col` AS `bfcol_2`
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
), `bfcte_1` AS (
SELECT
*,
`bfcol_1` AS `bfcol_6`,
`bfcol_2` AS `bfcol_7`,
`bfcol_0` AS `bfcol_8`,
CAST(FLOOR(86400000000 * `bfcol_0`) AS INT64) AS `bfcol_9`
FROM `bfcte_0`
), `bfcte_2` AS (
SELECT
*,
`bfcol_6` AS `bfcol_14`,
`bfcol_7` AS `bfcol_15`,
`bfcol_8` AS `bfcol_16`,
`bfcol_9` AS `bfcol_17`,
CAST(FLOOR(`bfcol_8` * 86400000000) AS INT64) AS `bfcol_18`
FROM `bfcte_1`
)
SELECT
`bfcol_14` AS `rowindex`,
`bfcol_15` AS `timestamp_col`,
`bfcol_16` AS `int64_col`,
`bfcol_17` AS `timedelta_mul_numeric`,
`bfcol_18` AS `numeric_mul_timedelta`
FROM `bfcte_2`
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,26 @@ def test_add_unsupported_raises(scalar_types_df: bpd.DataFrame):
_apply_binary_op(scalar_types_df, ops.add_op, "int64_col", "string_col")


def test_div_numeric(scalar_types_df: bpd.DataFrame, snapshot):
bf_df = scalar_types_df[["int64_col", "bool_col"]]

bf_df["int_div_int"] = bf_df["int64_col"] / bf_df["int64_col"]
bf_df["int_div_1"] = bf_df["int64_col"] / 1

bf_df["int_div_bool"] = bf_df["int64_col"] / bf_df["bool_col"]
bf_df["bool_div_int"] = bf_df["bool_col"] / bf_df["int64_col"]

snapshot.assert_match(bf_df.sql, "out.sql")


def test_div_timedelta(scalar_types_df: bpd.DataFrame, snapshot):
bf_df = scalar_types_df[["timestamp_col", "int64_col"]]
timedelta = pd.Timedelta(1, unit="d")
bf_df["timedelta_div_numeric"] = timedelta / bf_df["int64_col"]

snapshot.assert_match(bf_df.sql, "out.sql")


def test_json_set(json_types_df: bpd.DataFrame, snapshot):
bf_df = json_types_df[["json_col"]]
sql = _apply_binary_op(
Expand Down Expand Up @@ -122,3 +142,25 @@ def test_sub_unsupported_raises(scalar_types_df: bpd.DataFrame):

with pytest.raises(TypeError):
_apply_binary_op(scalar_types_df, ops.sub_op, "int64_col", "string_col")


def test_mul_numeric(scalar_types_df: bpd.DataFrame, snapshot):
bf_df = scalar_types_df[["int64_col", "bool_col"]]

bf_df["int_mul_int"] = bf_df["int64_col"] * bf_df["int64_col"]
bf_df["int_mul_1"] = bf_df["int64_col"] * 1

bf_df["int_mul_bool"] = bf_df["int64_col"] * bf_df["bool_col"]
bf_df["bool_mul_int"] = bf_df["bool_col"] * bf_df["int64_col"]

snapshot.assert_match(bf_df.sql, "out.sql")


def test_mul_timedelta(scalar_types_df: bpd.DataFrame, snapshot):
bf_df = scalar_types_df[["timestamp_col", "int64_col"]]
timedelta = pd.Timedelta(1, unit="d")

bf_df["timedelta_mul_numeric"] = timedelta * bf_df["int64_col"]
bf_df["numeric_mul_timedelta"] = bf_df["int64_col"] * timedelta

snapshot.assert_match(bf_df.sql, "out.sql")