Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sql): add LazyTbl.last_select to simplify queries #449

Merged
merged 2 commits into from
Sep 28, 2022

Conversation

machow
Copy link
Owner

@machow machow commented Sep 27, 2022

Addresses #346 for simplifying sql queries. The main issue was that siuba's LazyTbl was starting each query out using a full select statement, rather than just the sqlalchemy table object.

The original reason for always using select statements is that in sqlalchemy select statements can do more (e.g. have a with_only_columns method) than table objects (which mostly go in from clauses, are used to start select statements, have columns).

This PR adds a new property last_select to complement last_op. If last_op would return a table, then last_select will make it a select statement. This allows verbs that e.g. use with_only_columns to ensure they get a select statement.

from siuba.data import cars_sql
from siuba import _, inner_join, show_query

inner_join(cars_sql, cars_sql, "cyl") >> show_query()
SELECT cars_1.cyl, cars_1.mpg AS mpg_x, cars_1.hp AS hp_x, cars_2.mpg AS mpg_y, cars_2.hp AS hp_y
FROM cars AS cars_1 JOIN cars AS cars_2 ON cars_1.cyl = cars_2.cyl

This functionality both produced queries that were invalid, and involved
futzing with the sqlalchemy compiler, which appeared to break for
certain sqlalchemy versions.
@machow machow marked this pull request as ready for review September 28, 2022 14:16
@machow machow merged commit ddc63af into main Sep 28, 2022
@machow machow deleted the feat-sql-last-select branch September 28, 2022 14:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant