Skip to content

Commit

Permalink
fix(sql)!: simplify_query no longer strips table names
Browse files Browse the repository at this point in the history
This functionality both produced queries that were invalid, and involved
futzing with the sqlalchemy compiler, which appeared to break for
certain sqlalchemy versions.
  • Loading branch information
machow committed Sep 28, 2022
1 parent 1f8b251 commit 11c8327
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 26 deletions.
7 changes: 5 additions & 2 deletions siuba/dply/verbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2447,11 +2447,14 @@ def tbl(src, *args, **kwargs):
You can analyze a mock table
>>> from sqlalchemy import create_mock_engine
>>> from siuba import _
>>> mock_engine = create_mock_engine("postgresql:///", lambda *args, **kwargs: None)
>>> tbl_mock = tbl(mock_engine, "some_table", columns = ["a", "b", "c"])
>>> q = tbl_mock >> count(_.a) >> show_query() # doctest: +NORMALIZE_WHITESPACE
SELECT a, count(*) AS n
FROM some_table AS some_table_1 GROUP BY a ORDER BY n DESC
SELECT some_table_1.a, count(*) AS n
FROM some_table AS some_table_1 GROUP BY some_table_1.a ORDER BY n DESC
"""

return src
Expand Down
18 changes: 0 additions & 18 deletions siuba/sql/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,21 +190,3 @@ def simplify_sel(sel):

return clone_el


@contextmanager
def _use_simple_names():
from sqlalchemy import sql
from sqlalchemy.ext.compiler import compiles, deregister

get_col_name = lambda el, *args, **kwargs: str(el.element.name)
get_lab_name = lambda el, *args, **kwargs: str(el.element.name)
get_col_name = lambda el, *args, **kwargs: str(el.name)
compiles(sql.compiler._CompileLabel)(get_lab_name)
compiles(sql.elements.ColumnClause)(get_col_name)
compiles(sql.schema.Column)(get_col_name)
try:
yield 1
except:
pass
finally:
deregister(sql.compiler._CompileLabel)
4 changes: 1 addition & 3 deletions siuba/sql/verbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
_sql_add_columns,
_sql_with_only_columns,
_sql_simplify_select,
_use_simple_names,
MockConnection
)

Expand Down Expand Up @@ -465,8 +464,7 @@ def _show_query(tbl, simplify = False, return_table = True):
# try to strip table names and labels where unnecessary
simple_sel = _sql_simplify_select(tbl.last_select)

with _use_simple_names():
explained = compile_query(simple_sel)
explained = compile_query(simple_sel)
else:
# use a much more verbose query
explained = compile_query(tbl.last_select)
Expand Down
6 changes: 3 additions & 3 deletions siuba/tests/test_verb_show_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ def test_show_query_basic_simplify(df_tiny):
q = df_tiny >> mutate(a = _.x.mean()) >> show_query(return_table = False, simplify=True)

assert rename_source(str(q), df_tiny) == """\
SELECT *, avg(x) OVER () AS a
SELECT *, avg(SRC_TBL.x) OVER () AS a
FROM SRC_TBL"""

def test_show_query_complex_simplify(df_wide):
q = df_wide >> mutate(a = _.x.mean(), b = _.a.mean())
res = q >> show_query(return_table = False, simplify=True)

assert rename_source(str(res), df_wide) == """\
SELECT *, avg(a) OVER () AS b
FROM (SELECT *, avg(x) OVER () AS a
SELECT *, avg(anon_1.a) OVER () AS b
FROM (SELECT *, avg(SRC_TBL.x) OVER () AS a
FROM SRC_TBL) AS anon_1"""

0 comments on commit 11c8327

Please sign in to comment.