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.

missing tables returned by sql_metadata.get_query_tables

See original GitHub issue

Hello,

I have 80 long queries, and for 27 of them, results are not correct for “sql_metadata.get_query_tables(ddl)”

Here is an example:

ddl_for_sql_metadata.sql

SELECT
  "attr1"
FROM
  (
   SELECT
     "attr2"
   FROM
     (database1.table1 "aliasTable"
   LEFT JOIN (
      SELECT
        "attr3"
      FROM
        ((
         SELECT
           "attr4"
         FROM
           database2.table2
         GROUP BY 1, 2, 3, 4
      )  "aliasTable2"
      LEFT JOIN (
         SELECT
           "attr5"
         FROM
           database3.table3
         GROUP BY 1, 2
      )  "X" ON ("table"."attr" = "table"."attr"))
   )  "Y" ON ("table"."attr" = "table"."attr"))
)  "Z"
WHERE (myCondition)
GROUP BY 1, 2, 3, 4, 5

Quick way to test:

import sql_metadata

def read_from_file(file_path):
    return open(file_path, "r").read()

ddl = read_from_file("ddl_for_sql_metadata.sql")
tables = sql_metadata.get_query_tables(ddl)
print(tables)

It returns only [‘database2.table2’, ‘database3.table3’] and it should return database1.table1

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
macbrecommented, Mar 23, 2021

🙂

As said above, WHEN tests are still marked as skipped - sql-metadata internals need to mark the table as being used with WITH keyword and ignore them when returning the list of “real” tables.

I’ll add the test that you suggested 👌🏻. In the meantime, stay tuned 🙂

1reaction
macbrecommented, Mar 23, 2021

And a test case from the MySQL docs:

WITH
  cte1 AS (SELECT a, b FROM table1),
  cte2 AS (SELECT c, d FROM table2)
SELECT b, d FROM cte1 JOIN cte2
WHERE cte1.a = cte2.c;
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why aren't my tables "QueryTables"? - TechNet - Microsoft
According to Microsoft, a QueryTable "Represents a worksheet table built from data returned from an external data source, such as an SQL ...
Read more >
macbre/sql-metadata: Uses tokenized query returned ... - GitHub
Uses tokenized query returned by python-sqlparse and generates query metadata. Extracts column names and tables used by the query. Automatically conduct column ...
Read more >
How to extract table names and column names from sql query?
This metadata can return column and table names from your supplied SQL query. Here are a couple of example from the sql-metadata github ......
Read more >
Find Mismatched SQL Server Data Between Two Tables
In this tip we look at various ways to find mismatched SQL Server data between two tables using LEFT JOIN, EXCEPT, NOT IN...
Read more >
Missing Records in Joined Queries. Show All ... - YouTube
Have you ever put two related tables together in a query (like customers and contacts) and you wanted to print out a full...
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