Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sql_metadata/column_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def _is_date_part_unit(node: exp.Column) -> bool:
parent = node.parent
if (
isinstance(parent, exp.Anonymous)
and parent.this.lower() in _DATE_PART_FUNCTIONS
and parent.name.lower() in _DATE_PART_FUNCTIONS
):
exprs = parent.expressions
return len(exprs) > 0 and exprs[0] is node
Expand Down
16 changes: 16 additions & 0 deletions test/test_getting_columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,22 @@ def test_dateadd_unit_not_column():
assert parser.columns_dict == {"select": ["DateReleased"]}


def test_quoted_anonymous_function_name():
# solved: https://github.com/macbre/sql-metadata/issues/688
# a quoted function name is an Identifier, not a str, in sqlglot's AST
query = """
SELECT date_trunc('DAY', "timezone"('UTC', tbl.data_timestamp))
FROM tbl
"""
parser = Parser(query)
assert parser.tables == ["tbl"]
assert parser.columns == ["tbl.data_timestamp"]

# date-part unit is still skipped when the function name is quoted
parser = Parser('SELECT "dateadd"(dd, 30, DateReleased) FROM test')
assert parser.tables == ["test"]
assert parser.columns == ["DateReleased"]

def test_backtick_column_with_operation():
# solved: https://github.com/macbre/sql-metadata/issues/448
query = "SELECT `col1 with space` / `col2_anything` FROM table1"
Expand Down
Loading