From e26dc0b00412f8e0f47e0b52621955aaefaf7a72 Mon Sep 17 00:00:00 2001 From: macbre Date: Wed, 4 Mar 2026 19:46:59 +0000 Subject: [PATCH] Parser.columns drops column named 'source' when it is the last column in a SELECT statement Resolves #594 --- sql_metadata/token.py | 2 +- test/test_getting_columns.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/sql_metadata/token.py b/sql_metadata/token.py index 52686f86..8a0eeb40 100644 --- a/sql_metadata/token.py +++ b/sql_metadata/token.py @@ -176,7 +176,7 @@ def is_keyword_column_name(self) -> bool: self.is_keyword and self.normalized not in RELEVANT_KEYWORDS and self.previous_token.normalized in [",", "SELECT"] - and self.next_token.normalized in [",", "AS"] + and self.next_token.normalized in [",", "AS", "FROM"] ) @property diff --git a/test/test_getting_columns.py b/test/test_getting_columns.py index ab891480..06733204 100644 --- a/test/test_getting_columns.py +++ b/test/test_getting_columns.py @@ -541,3 +541,24 @@ def test_double_inner_join(): parser = Parser(query) assert "loan.account_id" in parser.columns assert parser.tables == ["loan", "account"] + + +def test_keyword_column_source(): + """ + https://github.com/macbre/sql-metadata/issues/594 + """ + # Test with 'source' as last column + parser = Parser("select foo, bar, source from my_table") + assert parser.columns == ["foo", "bar", "source"] + + # Test with 'source' in the middle + parser = Parser("select foo, source, bar from my_table") + assert parser.columns == ["foo", "source", "bar"] + + # Test with 'source' as first column + parser = Parser("select source, foo, bar from my_table") + assert parser.columns == ["source", "foo", "bar"] + + # Test with 'source' as only column + parser = Parser("select source from my_table") + assert parser.columns == ["source"]