Skip to content

Commit b2689af

Browse files
committed
chore: implement ge, gt, le, gt compilers
1 parent 621bbf0 commit b2689af

File tree

7 files changed

+285
-2
lines changed

7 files changed

+285
-2
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,33 @@ def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
107107

108108
@BINARY_OP_REGISTRATION.register(ops.ge_op)
109109
def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
110-
return sge.GTE(this=left.expr, expression=right.expr)
110+
left_expr, right_expr = _coerce_bools(left, right)
111+
return sge.GTE(this=left_expr, expression=right_expr)
112+
113+
114+
@BINARY_OP_REGISTRATION.register(ops.gt_op)
115+
def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
116+
left_expr, right_expr = _coerce_bools(left, right)
117+
return sge.GT(this=left_expr, expression=right_expr)
111118

112119

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

117124

125+
@BINARY_OP_REGISTRATION.register(ops.lt_op)
126+
def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
127+
left_expr, right_expr = _coerce_bools(left, right)
128+
return sge.LT(this=left_expr, expression=right_expr)
129+
130+
131+
@BINARY_OP_REGISTRATION.register(ops.le_op)
132+
def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
133+
left_expr, right_expr = _coerce_bools(left, right)
134+
return sge.LTE(this=left_expr, expression=right_expr)
135+
136+
118137
@BINARY_OP_REGISTRATION.register(ops.mul_op)
119138
def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
120139
left_expr, right_expr = _coerce_bools(left, right)

tests/system/small/engines/test_comparison_ops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def apply_op_pairwise(
4848
return new_arr
4949

5050

51-
@pytest.mark.parametrize("engine", ["polars", "bq"], indirect=True)
51+
@pytest.mark.parametrize("engine", ["polars", "bq", "bq-sqlglot"], indirect=True)
5252
@pytest.mark.parametrize(
5353
"op",
5454
[
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_gt_int`,
51+
`bfcol_40` AS `int_gt_1`,
52+
`bfcol_41` AS `int_gt_bool`,
53+
`bfcol_42` AS `bool_gt_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`
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_lt_int`,
51+
`bfcol_40` AS `int_lt_1`,
52+
`bfcol_41` AS `int_lt_bool`,
53+
`bfcol_42` AS `bool_lt_int`
54+
FROM `bfcte_4`

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

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

122122

123+
def test_gt_numeric(scalar_types_df: bpd.DataFrame, snapshot):
124+
bf_df = scalar_types_df[["int64_col", "bool_col"]]
125+
126+
bf_df["int_gt_int"] = bf_df["int64_col"] > bf_df["int64_col"]
127+
bf_df["int_gt_1"] = bf_df["int64_col"] > 1
128+
129+
bf_df["int_gt_bool"] = bf_df["int64_col"] > bf_df["bool_col"]
130+
bf_df["bool_gt_int"] = bf_df["bool_col"] > bf_df["int64_col"]
131+
132+
snapshot.assert_match(bf_df.sql, "out.sql")
133+
134+
135+
def test_ge_numeric(scalar_types_df: bpd.DataFrame, snapshot):
136+
bf_df = scalar_types_df[["int64_col", "bool_col"]]
137+
138+
bf_df["int_ge_int"] = bf_df["int64_col"] >= bf_df["int64_col"]
139+
bf_df["int_ge_1"] = bf_df["int64_col"] >= 1
140+
141+
bf_df["int_ge_bool"] = bf_df["int64_col"] >= bf_df["bool_col"]
142+
bf_df["bool_ge_int"] = bf_df["bool_col"] >= bf_df["int64_col"]
143+
144+
snapshot.assert_match(bf_df.sql, "out.sql")
145+
146+
123147
def test_json_set(json_types_df: bpd.DataFrame, snapshot):
124148
bf_df = json_types_df[["json_col"]]
125149
sql = _apply_binary_op(
@@ -129,6 +153,30 @@ def test_json_set(json_types_df: bpd.DataFrame, snapshot):
129153
snapshot.assert_match(sql, "out.sql")
130154

131155

156+
def test_lt_numeric(scalar_types_df: bpd.DataFrame, snapshot):
157+
bf_df = scalar_types_df[["int64_col", "bool_col"]]
158+
159+
bf_df["int_lt_int"] = bf_df["int64_col"] < bf_df["int64_col"]
160+
bf_df["int_lt_1"] = bf_df["int64_col"] < 1
161+
162+
bf_df["int_lt_bool"] = bf_df["int64_col"] < bf_df["bool_col"]
163+
bf_df["bool_lt_int"] = bf_df["bool_col"] < bf_df["int64_col"]
164+
165+
snapshot.assert_match(bf_df.sql, "out.sql")
166+
167+
168+
def test_le_numeric(scalar_types_df: bpd.DataFrame, snapshot):
169+
bf_df = scalar_types_df[["int64_col", "bool_col"]]
170+
171+
bf_df["int_le_int"] = bf_df["int64_col"] <= bf_df["int64_col"]
172+
bf_df["int_le_1"] = bf_df["int64_col"] <= 1
173+
174+
bf_df["int_le_bool"] = bf_df["int64_col"] <= bf_df["bool_col"]
175+
bf_df["bool_le_int"] = bf_df["bool_col"] <= bf_df["int64_col"]
176+
177+
snapshot.assert_match(bf_df.sql, "out.sql")
178+
179+
132180
def test_sub_numeric(scalar_types_df: bpd.DataFrame, snapshot):
133181
bf_df = scalar_types_df[["int64_col", "bool_col"]]
134182

0 commit comments

Comments
 (0)