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

fix(SQLAlchemy): queries w/o parameters #317

Merged

Conversation

betodealmeida
Copy link
Contributor

@betodealmeida betodealmeida commented Apr 29, 2024

Because the DB driver is using pyformat for paramstyle, queries compiled by SQLAlchemy will double any percents signs in the text:

SELECT * FROM t WHERE name LIKE '%%a' AND job = '%s'

So that when running the query via:

cursor.execute(sql, ("designer",))

The parameters are applied to the SQL via:

sql = "SELECT * FROM t WHERE name LIKE '%%a' AND job = '%s'"
parameters = ("designer",)
final_query = sql % parameters

Becoming:

SELECT * FROM t WHERE name LIKE '%a' AND job = 'designer'

The problem is that if no parameters are present the function apply_parameters will return the SQL immediately, with any double percents remaining in the final query. This produces valid queries (LIKE '%%a' is valid), but for some reason the performance of these queries is much worse compared to the correct query (LIKE '%a').

This PR fixes the problem by always removing duplicate percents, even when no parameters are passed.

@betodealmeida betodealmeida force-pushed the fix-parameterless-queries branch from 35739d5 to 679cb47 Compare April 29, 2024 14:46
Copy link

@Vitor-Avila Vitor-Avila left a comment

Choose a reason for hiding this comment

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

Thank you so much for working on this! LGTM

@betodealmeida betodealmeida merged commit 715d075 into druid-io:master Apr 29, 2024
betodealmeida added a commit to betodealmeida/pydruid that referenced this pull request Apr 29, 2024
@betodealmeida betodealmeida mentioned this pull request Apr 29, 2024
betodealmeida added a commit that referenced this pull request Apr 29, 2024
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.

2 participants