question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Resolve subqueries in where clause

See original GitHub issue

Hi,

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:open
  • Created 2 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
valentingarniercommented, Oct 15, 2021

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!

1reaction
collerekcommented, Jul 9, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Subqueries to Solve Queries - Tutorialspoint
A multiple-column subquery returns more than one column to the outer query and can be listed in the outer query's FROM, WHERE, or...
Read more >
Subqueries in the WHERE Clause
A subquery in a WHERE clause can be used to qualify a column against a set of rows. For example, the following subquery...
Read more >
SQL Subqueries - w3resource
A subquery is usually added within the WHERE Clause of another SQL SELECT statement. You can use the comparison operators, such as >,...
Read more >
3.2. Restrictions on Subqueries in WHERE Clauses
All unqualified references to columns in a subquery must resolve to tables in the subquery. Correlated subqueries cannot contain windowing clauses.
Read more >
How to fix multiple subqueries in where clause - Stack Overflow
Don't use EXISTS() . Use the subqueries as expressions in the WHERE clause directly. It's also possible to do the two calculations in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found