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

Refactor _process_results to support new DB engines #1054

Merged
merged 1 commit into from
Jul 21, 2024

Conversation

dantownsend
Copy link
Member

Resolves #1053

@dantownsend dantownsend added the enhancement New feature or request label Jul 21, 2024
Comment on lines -253 to 258
"$".join(
".".join(
t.cast(str, i._meta.db_column_name)
for i in self.call_chain
)
+ f"${column_name}"
+ f".{column_name}"
)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a small risk changing this.

In the past, the following query:

>>> await Band.select(Band.name, Band.manager.name)
[{'name': 'Pythonistas', 'manager.name': 'Guido'},
 {'name': 'Rustaceans', 'manager.name': 'Graydon'},
 {'name': 'C-Sharps', 'manager.name': 'Anders'}]

Executed this SQL:

>>> print(Band.select(Band.name, Band.manager.name))
"""
SELECT ALL
    "band"."name" AS "name", 
    "band$manager"."name" AS "manager$name"    <- NOTE THIS
FROM "band"
    LEFT JOIN "manager" "band$manager"
    ON ("band"."manager" = "band$manager"."id")
"""

Note how we have$ in the aliases - AS "manager$name".

$ was chosen because it's incredibly unlikely that someone would use $ in their column names, and it still works even without the quotes around it:

SELECT name AS foo$bar from band;
-- Returns [{"foo$bar": "Pythonistas"}, ...]

We can use . instead, as long as we're careful to wrap all of our aliases in quotes (otherwise the query fails).

SELECT name AS foo.bar from band;
-- Error!

So our query now becomes:

SELECT ALL
    "band"."name" AS "name", 
    "band$manager"."name" AS "manager.name"    -- <- NOTE THIS
FROM "band"
    LEFT JOIN "manager" "band$manager"
    ON ("band"."manager" = "band$manager"."id")

This means we don't have to convert from manager$name to manager.name in Piccolo, which saves some overhead.

I checked the codebase, and it looks like we wrap all aliases in double quotes. If there's an edge case where we don't, it's something we'll have to fix.

@dantownsend dantownsend merged commit 4ed6938 into master Jul 21, 2024
46 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Refactor _process_results to support new DB engines
1 participant