Skip to content

Commit 5b7403e

Browse files
committed
get CTE elim working
1 parent ce0cdc7 commit 5b7403e

File tree

5 files changed

+17
-5
lines changed

5 files changed

+17
-5
lines changed

data_algebra/db_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,7 @@ def table_def_to_near_sql(
11331133
query_name=view_name,
11341134
quoted_query_name=self.quote_identifier(view_name),
11351135
sub_sql=subsql.to_bound_near_sql(columns=using),
1136-
ops_key=f"table({table_def.node_name}, {terms.keys()})",
1136+
ops_key=f"table({table_def.table_name}, {terms.keys()})",
11371137
)
11381138
return near_sql
11391139

data_algebra/near_sql.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,9 @@ def to_with_form_stub(
204204
assert isinstance(v[1], NearSQLContainer)
205205
if not stub.is_table:
206206
# first check cache
207-
ops_key = f"{self.near_sql.ops_key}_{self.columns}"
207+
ops_key = f"{self.near_sql.ops_key}"
208+
if self.columns is not None:
209+
ops_key = f"{ops_key}_{list(self.columns)}"
208210
if (cte_cache is not None) and (ops_key is not None):
209211
try:
210212
retrieved_cte = cte_cache[ops_key]

data_algebra/sql_format_options.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class SQLFormatOptions(SimpleNamespace):
1515
:param initial_commas: bool = False, if True write initial commas instead of after commas
1616
:param warn_on_method_support: bool = True, if True warn on translation to untrusted methods
1717
:param warn_on_novel_methods: bool = True, if True warn on translation to unrecognized methods
18-
:param use_cte_elim: bool = False, if True optimzie SQL by re-using common table expressions (experimental! NOT WORKING YET)
18+
:param use_cte_elim: bool = False, if True optimize SQL by re-using common table expressions (experimental)
1919
"""
2020

2121
def __init__(
@@ -37,7 +37,7 @@ def __init__(
3737
:param initial_commas: bool = False, if True write initial commas instead of after commas
3838
:param warn_on_method_support: bool = True, if True warn on translation to untrusted methods
3939
:param warn_on_novel_methods: bool = True, if True warn on translation to unrecognized methods
40-
:param use_cte_elim: bool = False, if True optimize SQL by re-using common table expressions (experimental! NOT WORKING YET)
40+
:param use_cte_elim: bool = False, if True optimize SQL by re-using common table expressions (experimental)
4141
"""
4242
assert isinstance(use_with, bool)
4343
assert isinstance(annotate, bool)

data_algebra/test_util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ def check_transform_on_handles(
413413
for initial_commas in [True, False]:
414414
for use_with in [True, False]:
415415
for annotate in [True, False]:
416-
for use_cte_elim in [False]: # [True, False]: # TODO: put back
416+
for use_cte_elim in [True, False]:
417417
sql_format_options = SQLFormatOptions(
418418
use_with=use_with,
419419
annotate=annotate,

tests/test_sqlite_joins.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ def test_sqlite_joins_simulate_full_join():
9494
.natural_join(b=descr(d1=d1), by=join_columns, jointype="left")
9595
.natural_join(b=descr(d2=d2), by=join_columns, jointype="left")
9696
)
97+
db_model = data_algebra.BigQuery.BigQueryModel()
98+
sql = db_model.to_sql(
99+
ops_simulate,
100+
sql_format_options=data_algebra.sql_format_options.SQLFormatOptions(
101+
use_with=True,
102+
annotate=False,
103+
use_cte_elim=True,
104+
)
105+
)
106+
assert isinstance(sql, str)
97107
data_algebra.test_util.check_transform(
98108
ops=ops_simulate,
99109
data={"d1": d1, "d2": d2},

0 commit comments

Comments
 (0)