Skip to content

Commit 3cb3d79

Browse files
committed
chore: compare SQLs generated by Ibis and sqlglot
1 parent ebdcd02 commit 3cb3d79

File tree

33 files changed

+534
-737
lines changed
  • bigframes/testing
  • tests/unit/core/compile/sqlglot
    • expressions/snapshots
    • snapshots
      • test_compile_aggregate
      • test_compile_concat/test_compile_concat
      • test_compile_explode
        • test_compile_explode_dataframe
        • test_compile_explode_series
      • test_compile_filter/test_compile_filter
      • test_compile_join
      • test_compile_readlocal
        • test_compile_readlocal_w_json_df
        • test_compile_readlocal_w_lists_df
        • test_compile_readlocal_w_structs_df
        • test_compile_readlocal
      • test_compile_readtable
        • test_compile_readtable_w_json_types
        • test_compile_readtable_w_limit
        • test_compile_readtable_w_nested_structs_types
        • test_compile_readtable_w_ordering
        • test_compile_readtable_w_repeated_types
        • test_compile_readtable

33 files changed

+534
-737
lines changed

bigframes/testing/compiler_session.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,15 @@
1616
import typing
1717

1818
import bigframes.core
19-
import bigframes.core.compile.sqlglot as sqlglot
19+
from bigframes.core.compile import configs
20+
import bigframes.core.compile.compiler as compiler
2021
import bigframes.session.executor
2122

2223

2324
@dataclasses.dataclass
2425
class SQLCompilerExecutor(bigframes.session.executor.Executor):
2526
"""Executor for SQL compilation using sqlglot."""
2627

27-
compiler = sqlglot
28-
2928
def to_sql(
3029
self,
3130
array_value: bigframes.core.ArrayValue,
@@ -38,6 +37,6 @@ def to_sql(
3837

3938
# Compared with BigQueryCachingExecutor, SQLCompilerExecutor skips
4039
# caching the subtree.
41-
return self.compiler.SQLGlotCompiler().compile(
42-
array_value.node, ordered=ordered
43-
)
40+
request = configs.CompileRequest(array_value.node, sort_rows=ordered)
41+
compiled = compiler.compile_sql(request)
42+
return compiled.sql
Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,4 @@
1-
WITH `bfcte_0` AS (
2-
SELECT
3-
`int64_col` AS `bfcol_0`,
4-
`rowindex` AS `bfcol_1`
5-
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
6-
), `bfcte_1` AS (
7-
SELECT
8-
*,
9-
`bfcol_1` AS `bfcol_4`,
10-
`bfcol_0` + `bfcol_0` AS `bfcol_5`
11-
FROM `bfcte_0`
12-
)
131
SELECT
14-
`bfcol_4` AS `rowindex`,
15-
`bfcol_5` AS `int64_col`
16-
FROM `bfcte_1`
2+
`t0`.`rowindex`,
3+
`t0`.`int64_col` + `t0`.`int64_col` AS `int64_col`
4+
FROM `bigframes-dev.sqlglot_test.scalar_types` AS `t0`
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,4 @@
1-
WITH `bfcte_0` AS (
2-
SELECT
3-
`int64_col` AS `bfcol_0`,
4-
`rowindex` AS `bfcol_1`
5-
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
6-
), `bfcte_1` AS (
7-
SELECT
8-
*,
9-
`bfcol_1` AS `bfcol_4`,
10-
`bfcol_0` + 1 AS `bfcol_5`
11-
FROM `bfcte_0`
12-
)
131
SELECT
14-
`bfcol_4` AS `rowindex`,
15-
`bfcol_5` AS `int64_col`
16-
FROM `bfcte_1`
2+
`t0`.`rowindex`,
3+
`t0`.`int64_col` + 1 AS `int64_col`
4+
FROM `bigframes-dev.sqlglot_test.scalar_types` AS `t0`
Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,4 @@
1-
WITH `bfcte_0` AS (
2-
SELECT
3-
`rowindex` AS `bfcol_0`,
4-
`string_col` AS `bfcol_1`
5-
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
6-
), `bfcte_1` AS (
7-
SELECT
8-
*,
9-
`bfcol_0` AS `bfcol_4`,
10-
CONCAT(`bfcol_1`, 'a') AS `bfcol_5`
11-
FROM `bfcte_0`
12-
)
131
SELECT
14-
`bfcol_4` AS `rowindex`,
15-
`bfcol_5` AS `string_col`
16-
FROM `bfcte_1`
2+
`t0`.`rowindex`,
3+
CONCAT(`t0`.`string_col`, 'a') AS `string_col`
4+
FROM `bigframes-dev.sqlglot_test.scalar_types` AS `t0`
Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
1-
WITH `bfcte_0` AS (
2-
SELECT
3-
`rowindex` AS `bfcol_0`,
4-
`json_col` AS `bfcol_1`
5-
FROM `bigframes-dev`.`sqlglot_test`.`json_types`
6-
), `bfcte_1` AS (
7-
SELECT
8-
*,
9-
JSON_SET(`bfcol_1`, '$.a', 100) AS `bfcol_4`
10-
FROM `bfcte_0`
11-
), `bfcte_2` AS (
12-
SELECT
13-
*,
14-
JSON_SET(`bfcol_4`, '$.b', 'hi') AS `bfcol_7`
15-
FROM `bfcte_1`
16-
)
171
SELECT
18-
`bfcol_0` AS `rowindex`,
19-
`bfcol_7` AS `json_col`
20-
FROM `bfcte_2`
2+
`t1`.`rowindex`,
3+
json_set(json_set(`t1`.`json_col`, '$.a', 100), '$.b', 'hi') AS `json_col`
4+
FROM (
5+
SELECT
6+
*
7+
FROM `bigframes-dev.sqlglot_test.json_types` AS `t0`
8+
) AS `t1`
Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
1-
WITH `bfcte_0` AS (
2-
SELECT
3-
`rowindex` AS `bfcol_0`,
4-
`string_list_col` AS `bfcol_1`
5-
FROM `bigframes-dev`.`sqlglot_test`.`repeated_types`
6-
), `bfcte_1` AS (
7-
SELECT
8-
*,
9-
`bfcol_1`[SAFE_OFFSET(1)] AS `bfcol_4`
10-
FROM `bfcte_0`
11-
)
121
SELECT
13-
`bfcol_0` AS `rowindex`,
14-
`bfcol_4` AS `string_list_col`
15-
FROM `bfcte_1`
2+
`t0`.`rowindex`,
3+
`t0`.`string_list_col`[safe_offset(1)] AS `string_list_col`
4+
FROM `bigframes-dev.sqlglot_test.repeated_types` AS `t0`
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
1-
WITH `bfcte_0` AS (
2-
SELECT
3-
`rowindex` AS `bfcol_0`,
4-
`string_list_col` AS `bfcol_1`
5-
FROM `bigframes-dev`.`sqlglot_test`.`repeated_types`
6-
), `bfcte_1` AS (
7-
SELECT
8-
*,
9-
ARRAY(
10-
SELECT
11-
el
12-
FROM UNNEST(`bfcol_1`) AS el WITH OFFSET AS slice_idx
13-
WHERE
14-
slice_idx >= 1
15-
) AS `bfcol_4`
16-
FROM `bfcte_0`
17-
)
181
SELECT
19-
`bfcol_0` AS `rowindex`,
20-
`bfcol_4` AS `string_list_col`
21-
FROM `bfcte_1`
2+
`t0`.`rowindex`,
3+
ARRAY(
4+
SELECT
5+
el
6+
FROM UNNEST(`t0`.`string_list_col`) AS el WITH OFFSET AS bq_arr_slice
7+
WHERE
8+
bq_arr_slice >= IF(1 < 0, ARRAY_LENGTH(`t0`.`string_list_col`) + 1, 1)
9+
) AS `string_list_col`
10+
FROM `bigframes-dev.sqlglot_test.repeated_types` AS `t0`
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
1-
WITH `bfcte_0` AS (
2-
SELECT
3-
`rowindex` AS `bfcol_0`,
4-
`string_list_col` AS `bfcol_1`
5-
FROM `bigframes-dev`.`sqlglot_test`.`repeated_types`
6-
), `bfcte_1` AS (
7-
SELECT
8-
*,
9-
ARRAY(
10-
SELECT
11-
el
12-
FROM UNNEST(`bfcol_1`) AS el WITH OFFSET AS slice_idx
13-
WHERE
14-
slice_idx >= 1 AND slice_idx < 5
15-
) AS `bfcol_4`
16-
FROM `bfcte_0`
17-
)
181
SELECT
19-
`bfcol_0` AS `rowindex`,
20-
`bfcol_4` AS `string_list_col`
21-
FROM `bfcte_1`
2+
`t0`.`rowindex`,
3+
ARRAY(
4+
SELECT
5+
el
6+
FROM UNNEST(`t0`.`string_list_col`) AS el WITH OFFSET AS bq_arr_slice
7+
WHERE
8+
bq_arr_slice >= IF(1 < 0, ARRAY_LENGTH(`t0`.`string_list_col`) + 1, 1)
9+
AND bq_arr_slice < IF(5 < 0, ARRAY_LENGTH(`t0`.`string_list_col`) + 5, 5)
10+
) AS `string_list_col`
11+
FROM `bigframes-dev.sqlglot_test.repeated_types` AS `t0`
Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
1-
WITH `bfcte_0` AS (
2-
SELECT
3-
`rowindex` AS `bfcol_0`,
4-
`string_list_col` AS `bfcol_1`
5-
FROM `bigframes-dev`.`sqlglot_test`.`repeated_types`
6-
), `bfcte_1` AS (
7-
SELECT
8-
*,
9-
ARRAY_TO_STRING(`bfcol_1`, '.') AS `bfcol_4`
10-
FROM `bfcte_0`
11-
)
121
SELECT
13-
`bfcol_0` AS `rowindex`,
14-
`bfcol_4` AS `string_list_col`
15-
FROM `bfcte_1`
2+
`t0`.`rowindex`,
3+
ARRAY_TO_STRING(`t0`.`string_list_col`, '.') AS `string_list_col`
4+
FROM `bigframes-dev.sqlglot_test.repeated_types` AS `t0`
Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
WITH `bfcte_0` AS (
2-
SELECT
3-
`rowindex` AS `bfcol_0`,
4-
`string_col` AS `bfcol_1`
5-
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
6-
), `bfcte_1` AS (
7-
SELECT
8-
*,
9-
JSON_VALUE(`bfcol_1`, '$') AS `bfcol_4`
10-
FROM `bfcte_0`
11-
)
121
SELECT
13-
`bfcol_0` AS `rowindex`,
14-
`bfcol_4` AS `string_col`
15-
FROM `bfcte_1`
2+
`t1`.`rowindex`,
3+
json_value(`t1`.`string_col`, '$') AS `string_col`
4+
FROM (
5+
SELECT
6+
`t0`.`rowindex`,
7+
`t0`.`string_col`
8+
FROM `bigframes-dev.sqlglot_test.scalar_types` AS `t0`
9+
) AS `t1`

0 commit comments

Comments
 (0)