druid-io / pydruid

A Python connector for Druid
Other
505 stars 194 forks source link

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

Closed betodealmeida closed 2 months ago

betodealmeida commented 2 months ago

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.