Resolve subqueries in where clause
See original GitHub issueHi,
I think I have found a bug regarding subqueries, namely a subquery that seems intentionally left out in (https://github.com/macbre/sql-metadata/blob/master/sql_metadata/keywords_lists.py):
query = "SELECT a.model FROM CAR_NAMES a JOIN CARS_DATA b ON a.MakeId = b.Id WHERE b.Weight < (SELECT avg(Weight) FROM CARS_DATA)"
print(query.columns, "\n", query.columns_aliases_names, "\n", query.tables, "\n", query.tables_aliases, "\n", query.subqueries)
I expected this to result in recognition of the subquery (SELECT avg(Weight) FROM CARS_DATA), however it is not recognized, the output is:
['car_names.model', 'car_names.makeid', 'cars_data.id', 'cars_data.weight', 'weight']
[]
['car_names', 'cars_data']
{'a': 'car_names', 'b': 'cars_data'}
{}
Something else, which I think is a different issue; I would expect the column ‘weight’ of the subquery to be recognized as ‘cars_data.weight’ as well.
I think it would be useful when the complete query has more than one table found, to append the table name before any column found.
Issue Analytics
- State:
- Created 2 years ago
- Comments:8
Top GitHub Comments
Hi,
I saw that in order to spot subqueries you rely on having an alias for the whole subquery. Problem with the WHERE clause is that usually subqueries inside doesn’t have any alias. Any idea on how to find a way around for spotting subqueries inside WHERE clause? I was thinking about working with pairs of parentheses but subqueries are not the only one surrounded by parentheses.
Thanks!
If there is no prefix the column name has to be unique in the scope of the query -> which means that you should be able to resolve it with db access -> either by quering system tables or by auto-reflecting tables i.e. with
sqlalchemy
.So you can extract all tables used with
sql-metadata
but then you need the actual db to fetch the list of columns per table.