Skip to content

Commit 4b2a651

Browse files
committed
implement le
1 parent f485d26 commit 4b2a651

File tree

4 files changed

+150
-1
lines changed

4 files changed

+150
-1
lines changed

bigframes/core/compile/sqlglot/expressions/binary_compiler.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,13 @@ def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
122122

123123
@BINARY_OP_REGISTRATION.register(ops.ge_op)
124124
def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
125-
return sge.GTE(this=left.expr, expression=right.expr)
125+
left_expr = left.expr
126+
if left.dtype == dtypes.BOOL_DTYPE:
127+
left_expr = sge.Cast(this=left_expr, to="INT64")
128+
right_expr = right.expr
129+
if right.dtype == dtypes.BOOL_DTYPE:
130+
right_expr = sge.Cast(this=right_expr, to="INT64")
131+
return sge.GTE(this=left_expr, expression=right_expr)
126132

127133

128134
@BINARY_OP_REGISTRATION.register(ops.gt_op)
@@ -152,6 +158,17 @@ def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
152158
return sge.LT(this=left_expr, expression=right_expr)
153159

154160

161+
@BINARY_OP_REGISTRATION.register(ops.le_op)
162+
def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
163+
left_expr = left.expr
164+
if left.dtype == dtypes.BOOL_DTYPE:
165+
left_expr = sge.Cast(this=left_expr, to="INT64")
166+
right_expr = right.expr
167+
if right.dtype == dtypes.BOOL_DTYPE:
168+
right_expr = sge.Cast(this=right_expr, to="INT64")
169+
return sge.LTE(this=left_expr, expression=right_expr)
170+
171+
155172
@BINARY_OP_REGISTRATION.register(ops.mul_op)
156173
def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
157174
left_expr = left.expr
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
WITH `bfcte_0` AS (
2+
SELECT
3+
`bool_col` AS `bfcol_0`,
4+
`int64_col` AS `bfcol_1`,
5+
`rowindex` AS `bfcol_2`
6+
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
7+
), `bfcte_1` AS (
8+
SELECT
9+
*,
10+
`bfcol_2` AS `bfcol_6`,
11+
`bfcol_1` AS `bfcol_7`,
12+
`bfcol_0` AS `bfcol_8`,
13+
`bfcol_1` >= `bfcol_1` AS `bfcol_9`
14+
FROM `bfcte_0`
15+
), `bfcte_2` AS (
16+
SELECT
17+
*,
18+
`bfcol_6` AS `bfcol_14`,
19+
`bfcol_7` AS `bfcol_15`,
20+
`bfcol_8` AS `bfcol_16`,
21+
`bfcol_9` AS `bfcol_17`,
22+
`bfcol_7` >= 1 AS `bfcol_18`
23+
FROM `bfcte_1`
24+
), `bfcte_3` AS (
25+
SELECT
26+
*,
27+
`bfcol_14` AS `bfcol_24`,
28+
`bfcol_15` AS `bfcol_25`,
29+
`bfcol_16` AS `bfcol_26`,
30+
`bfcol_17` AS `bfcol_27`,
31+
`bfcol_18` AS `bfcol_28`,
32+
`bfcol_15` >= CAST(`bfcol_16` AS INT64) AS `bfcol_29`
33+
FROM `bfcte_2`
34+
), `bfcte_4` AS (
35+
SELECT
36+
*,
37+
`bfcol_24` AS `bfcol_36`,
38+
`bfcol_25` AS `bfcol_37`,
39+
`bfcol_26` AS `bfcol_38`,
40+
`bfcol_27` AS `bfcol_39`,
41+
`bfcol_28` AS `bfcol_40`,
42+
`bfcol_29` AS `bfcol_41`,
43+
CAST(`bfcol_26` AS INT64) >= `bfcol_25` AS `bfcol_42`
44+
FROM `bfcte_3`
45+
)
46+
SELECT
47+
`bfcol_36` AS `rowindex`,
48+
`bfcol_37` AS `int64_col`,
49+
`bfcol_38` AS `bool_col`,
50+
`bfcol_39` AS `int_ge_int`,
51+
`bfcol_40` AS `int_ge_1`,
52+
`bfcol_41` AS `int_ge_bool`,
53+
`bfcol_42` AS `bool_ge_int`
54+
FROM `bfcte_4`
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
WITH `bfcte_0` AS (
2+
SELECT
3+
`bool_col` AS `bfcol_0`,
4+
`int64_col` AS `bfcol_1`,
5+
`rowindex` AS `bfcol_2`
6+
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
7+
), `bfcte_1` AS (
8+
SELECT
9+
*,
10+
`bfcol_2` AS `bfcol_6`,
11+
`bfcol_1` AS `bfcol_7`,
12+
`bfcol_0` AS `bfcol_8`,
13+
`bfcol_1` <= `bfcol_1` AS `bfcol_9`
14+
FROM `bfcte_0`
15+
), `bfcte_2` AS (
16+
SELECT
17+
*,
18+
`bfcol_6` AS `bfcol_14`,
19+
`bfcol_7` AS `bfcol_15`,
20+
`bfcol_8` AS `bfcol_16`,
21+
`bfcol_9` AS `bfcol_17`,
22+
`bfcol_7` <= 1 AS `bfcol_18`
23+
FROM `bfcte_1`
24+
), `bfcte_3` AS (
25+
SELECT
26+
*,
27+
`bfcol_14` AS `bfcol_24`,
28+
`bfcol_15` AS `bfcol_25`,
29+
`bfcol_16` AS `bfcol_26`,
30+
`bfcol_17` AS `bfcol_27`,
31+
`bfcol_18` AS `bfcol_28`,
32+
`bfcol_15` <= CAST(`bfcol_16` AS INT64) AS `bfcol_29`
33+
FROM `bfcte_2`
34+
), `bfcte_4` AS (
35+
SELECT
36+
*,
37+
`bfcol_24` AS `bfcol_36`,
38+
`bfcol_25` AS `bfcol_37`,
39+
`bfcol_26` AS `bfcol_38`,
40+
`bfcol_27` AS `bfcol_39`,
41+
`bfcol_28` AS `bfcol_40`,
42+
`bfcol_29` AS `bfcol_41`,
43+
CAST(`bfcol_26` AS INT64) <= `bfcol_25` AS `bfcol_42`
44+
FROM `bfcte_3`
45+
)
46+
SELECT
47+
`bfcol_36` AS `rowindex`,
48+
`bfcol_37` AS `int64_col`,
49+
`bfcol_38` AS `bool_col`,
50+
`bfcol_39` AS `int_le_int`,
51+
`bfcol_40` AS `int_le_1`,
52+
`bfcol_41` AS `int_le_bool`,
53+
`bfcol_42` AS `bool_le_int`
54+
FROM `bfcte_4`

tests/unit/core/compile/sqlglot/expressions/test_binary_compiler.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,18 @@ def test_gt_numeric(scalar_types_df: bpd.DataFrame, snapshot):
120120
snapshot.assert_match(bf_df.sql, "out.sql")
121121

122122

123+
def test_ge_numeric(scalar_types_df: bpd.DataFrame, snapshot):
124+
bf_df = scalar_types_df[["int64_col", "bool_col"]]
125+
126+
bf_df["int_ge_int"] = bf_df["int64_col"] >= bf_df["int64_col"]
127+
bf_df["int_ge_1"] = bf_df["int64_col"] >= 1
128+
129+
bf_df["int_ge_bool"] = bf_df["int64_col"] >= bf_df["bool_col"]
130+
bf_df["bool_ge_int"] = bf_df["bool_col"] >= bf_df["int64_col"]
131+
132+
snapshot.assert_match(bf_df.sql, "out.sql")
133+
134+
123135
def test_json_set(json_types_df: bpd.DataFrame, snapshot):
124136
bf_df = json_types_df[["json_col"]]
125137
sql = _apply_binary_op(
@@ -141,6 +153,18 @@ def test_lt_numeric(scalar_types_df: bpd.DataFrame, snapshot):
141153
snapshot.assert_match(bf_df.sql, "out.sql")
142154

143155

156+
def test_le_numeric(scalar_types_df: bpd.DataFrame, snapshot):
157+
bf_df = scalar_types_df[["int64_col", "bool_col"]]
158+
159+
bf_df["int_le_int"] = bf_df["int64_col"] <= bf_df["int64_col"]
160+
bf_df["int_le_1"] = bf_df["int64_col"] <= 1
161+
162+
bf_df["int_le_bool"] = bf_df["int64_col"] <= bf_df["bool_col"]
163+
bf_df["bool_le_int"] = bf_df["bool_col"] <= bf_df["int64_col"]
164+
165+
snapshot.assert_match(bf_df.sql, "out.sql")
166+
167+
144168
def test_sub_numeric(scalar_types_df: bpd.DataFrame, snapshot):
145169
bf_df = scalar_types_df[["int64_col", "bool_col"]]
146170

0 commit comments

Comments
 (0)