Skip to content

Commit d0e6fc6

Browse files
authored
Parser.columns drops column named 'source' when it is the last column in a SELECT statement (#603)
Resolves #594
1 parent bed7826 commit d0e6fc6

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

sql_metadata/token.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def is_keyword_column_name(self) -> bool:
176176
self.is_keyword
177177
and self.normalized not in RELEVANT_KEYWORDS
178178
and self.previous_token.normalized in [",", "SELECT"]
179-
and self.next_token.normalized in [",", "AS"]
179+
and self.next_token.normalized in [",", "AS", "FROM"]
180180
)
181181

182182
@property

test/test_getting_columns.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,3 +541,24 @@ def test_double_inner_join():
541541
parser = Parser(query)
542542
assert "loan.account_id" in parser.columns
543543
assert parser.tables == ["loan", "account"]
544+
545+
546+
def test_keyword_column_source():
547+
"""
548+
https://github.com/macbre/sql-metadata/issues/594
549+
"""
550+
# Test with 'source' as last column
551+
parser = Parser("select foo, bar, source from my_table")
552+
assert parser.columns == ["foo", "bar", "source"]
553+
554+
# Test with 'source' in the middle
555+
parser = Parser("select foo, source, bar from my_table")
556+
assert parser.columns == ["foo", "source", "bar"]
557+
558+
# Test with 'source' as first column
559+
parser = Parser("select source, foo, bar from my_table")
560+
assert parser.columns == ["source", "foo", "bar"]
561+
562+
# Test with 'source' as only column
563+
parser = Parser("select source from my_table")
564+
assert parser.columns == ["source"]

0 commit comments

Comments
 (0)