Repository: macbre/sql-metadata

Issue:
get_query_columns does not return nothing if parsing SELECT DISTINCT
Hi, with the following query, I do not get any results:

>>> print(sql_metadata.get_query_columns("SELECT DISTINCT DATA.ASSAY_ID FROM foo"))
[]
>>> print(sql_metadata.get_query_columns("SELECT DATA.ASSAY_ID FROM foo"))
['DATA.ASSAY_ID']

Is this normal?

Regards

Discussion:

Patch:
diff --git a/sql_metadata.py b/sql_metadata.py
index c6dd9e5..532ad02 100644
--- a/sql_metadata.py
+++ b/sql_metadata.py
@@ -97,6 +97,8 @@ def get_query_columns(query: str) -> List[str]:
         "LIKE",
         "CASE",
         "WHEN",
+        "DISTINCT",
+        "UNIQUE",
     ]
 
     # these keywords are followed by columns reference


Test patch:
diff --git a/test/test_query.py b/test/test_query.py
index 2c2f13d..1dfa537 100644
--- a/test/test_query.py
+++ b/test/test_query.py
@@ -546,3 +546,15 @@ def test_queries_with_null_conditions():
     assert get_query_columns(
         "SELECT id FROM cm WHERE cm.status = 1 AND cm.OPERATIONDATE IS NOT NULL AND cm.OID IN(123123);"
     ) == ["id", "cm.status", "cm.OPERATIONDATE", "cm.OID"]
+
+
+def test_queries_with_distinct():
+    assert get_query_columns("SELECT DISTINCT DATA.ASSAY_ID FROM foo") == [
+        "DATA.ASSAY_ID"
+    ]
+
+    assert get_query_columns("SELECT UNIQUE DATA.ASSAY_ID FROM foo") == [
+        "DATA.ASSAY_ID"
+    ]
+
+    assert get_query_tables("SELECT DISTINCT DATA.ASSAY_ID FROM foo") == ["foo"]