Skip to content

Commit

Permalink
Merge pull request #171 from collerek/fix_create_schema
Browse files Browse the repository at this point in the history
Fix create schema
  • Loading branch information
collerek committed Jun 22, 2021
2 parents 0a27871 + 3fd11ae commit 6e48336
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
4 changes: 2 additions & 2 deletions sql_metadata/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -874,9 +874,9 @@ def _determine_opening_parenthesis_type(self, token: SQLToken):
token.is_column_definition_start = True
elif token.previous_token.normalized == "AS":
token.is_with_query_start = True
elif (
elif token.last_keyword_normalized == "TABLE" and (
token.get_nth_previous(2).normalized == "TABLE"
and token.get_nth_previous(3).normalized == "CREATE"
or token.get_nth_previous(4).normalized == "TABLE"
):
token.is_create_table_columns_declaration_start = True
else:
Expand Down
26 changes: 25 additions & 1 deletion test/test_create_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_create_table():
def test_simple_create_table_as_select():
parser = Parser(
"""
CREATE table abc.foo
CREATE table abc.foo
as SELECT pqr.foo1 , ab.foo2
FROM foo pqr, bar ab;
"""
Expand Down Expand Up @@ -116,3 +116,27 @@ def test_create_table_as_select_in_parentheses():
assert parser.query_type == QueryType.CREATE
assert parser.columns == ["t.id", "t.name", "e.name", "t.e_id", "e.id"]
assert parser.tables == ["records", "t", "e"]


def test_create_table_with_schema_name():
query = """
CREATE TABLE myschema.mytable (
code INTEGER NOT NULL,
short_name CHAR(9)
);
"""
parser = Parser(query)
assert parser.query_type == QueryType.CREATE
assert parser.columns == ["code", "short_name"]
assert parser.tables == ["myschema.mytable"]


def test_create_table_as_select_in_parentheses_with_schema():
qry = """
CREATE TABLE mysuper_secret_schema.records AS
(SELECT t.id, t.name, e.name as energy FROM t JOIN e ON t.e_id = e.id)
"""
parser = Parser(qry)
assert parser.query_type == QueryType.CREATE
assert parser.columns == ["t.id", "t.name", "e.name", "t.e_id", "e.id"]
assert parser.tables == ["mysuper_secret_schema.records", "t", "e"]

0 comments on commit 6e48336

Please sign in to comment.