Repository: macbre/sql-metadata

Issue:
get_query_tables() CTE Case Sensitive for SELECT
For the following query:
`import sql_metadata`
`sql_metadata.get_query_tables("create table abc.foo as SELECT pqr.foo1 , ab.foo2 FROM foo pqr, bar ab")`

get_query_tables return the expected output

`['abc.foo', 'foo', 'bar']`

But when SELECT is in lowercase - 
`import sql_metadata`
`sql_metadata.get_query_tables("create table abc.foo as select pqr.foo1 , ab.foo2 FROM foo pqr, bar ab")`

then the select columns get added in the 
`['pqr.foo1', 'ab.foo2', 'foo', 'bar']`

Discussion:

Patch:
diff --git a/sql_metadata.py b/sql_metadata.py
index cd983cf..c6dd9e5 100644
--- a/sql_metadata.py
+++ b/sql_metadata.py
@@ -288,7 +288,7 @@ def get_query_tables(query: str) -> List[str]:
         elif str(token) == "(":
             # reset the last_keyword for INSERT `foo` VALUES(id, bar) ...
             last_keyword = None
-        elif token.is_keyword and str(token) in ["FORCE", "ORDER", "GROUP BY"]:
+        elif token.is_keyword and str(token).upper() in ["FORCE", "ORDER", "GROUP BY"]:
             # reset the last_keyword for queries like:
             # "SELECT x FORCE INDEX"
             # "SELECT x ORDER BY"
@@ -296,7 +296,7 @@ def get_query_tables(query: str) -> List[str]:
             last_keyword = None
         elif (
             token.is_keyword
-            and str(token) == "SELECT"
+            and str(token).upper() == "SELECT"
             and last_keyword in ["INTO", "TABLE"]
         ):
             # reset the last_keyword for "INSERT INTO SELECT" and "INSERT TABLE SELECT" queries


Test patch:
diff --git a/test/test_query.py b/test/test_query.py
index 4d92db0..2c2f13d 100644
--- a/test/test_query.py
+++ b/test/test_query.py
@@ -207,6 +207,26 @@ def test_get_query_tables():
     )
 
 
+def test_case_insensitive():
+    # case-insensitive handling
+    # https://github.com/macbre/sql-metadata/issues/71
+    assert ["abc.foo", "foo", "bar"] == get_query_tables(
+        "create table abc.foo as SELECT pqr.foo1 , ab.foo2 FROM foo pqr, bar ab"
+    )
+
+    assert ["abc.foo", "foo", "bar"] == get_query_tables(
+        "create table abc.foo as select pqr.foo1 , ab.foo2 FROM foo pqr, bar ab"
+    )
+
+    assert ["pqr.foo1", "ab.foo2"] == get_query_columns(
+        "create table abc.foo as SELECT pqr.foo1 , ab.foo2 FROM foo pqr, bar ab"
+    )
+
+    assert ["pqr.foo1", "ab.foo2"] == get_query_columns(
+        "create table abc.foo as select pqr.foo1 , ab.foo2 FROM foo pqr, bar ab"
+    )
+
+
 def test_joins():
     assert ["redirect", "page"] == get_query_tables(
         "SELECT  page_title  FROM `redirect` INNER JOIN `page` "