Skip to content

Commit a0e5ce1

Browse files
chore: bump sqlglot (#351)
1 parent 579e047 commit a0e5ce1

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

dev-requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# This file is autogenerated by pip-compile with Python 3.8
2+
# This file is autogenerated by pip-compile with Python 3.11
33
# by the following command:
44
#
55
# pip-compile --no-annotate dev-requirements.in
@@ -67,7 +67,7 @@ rich==12.5.1
6767
six==1.16.0
6868
soupsieve==2.3.2.post1
6969
sqlalchemy==1.4.40
70-
sqlglot==20.7.1
70+
sqlglot==26.23.0
7171
tabulate==0.8.10
7272
toml==0.10.2
7373
tomli==2.0.1

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# This file is autogenerated by pip-compile with Python 3.9
2+
# This file is autogenerated by pip-compile with Python 3.11
33
# by the following command:
44
#
55
# pip-compile --no-annotate
@@ -40,7 +40,7 @@ rich==12.3.0
4040
six==1.16.0
4141
soupsieve==2.3.2.post1
4242
sqlalchemy==1.4.35
43-
sqlglot==20.7.1
43+
sqlglot==26.23.0
4444
tabulate==0.8.9
4545
typing-extensions==4.2.0
4646
urllib3==1.26.9

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ install_requires =
7171
requests>=2.26.0
7272
rich>=12.3.0
7373
sqlalchemy>=1.4,<2
74-
sqlglot>=19,<23
74+
sqlglot>=26
7575
tabulate>=0.8.9
7676
typing-extensions>=4.0.1
7777
yarl>=1.7.2

src/preset_cli/cli/superset/sync/dbt/metrics.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,13 @@
5151
}
5252

5353

54-
# pylint: disable=too-many-locals
54+
JINJAPATTERN = re.compile(
55+
r"(\s*(?:\{\{[\s\S]*?\}\}|\{%[\s\S]*?%\}))+",
56+
re.DOTALL,
57+
)
58+
59+
60+
# pylint: disable=too-many-locals, too-many-branches
5561
def get_metric_expression(metric_name: str, metrics: Dict[str, OGMetricSchema]) -> str:
5662
"""
5763
Return a SQL expression for a given dbt metric using sqlglot.
@@ -93,6 +99,11 @@ def get_metric_expression(metric_name: str, metrics: Dict[str, OGMetricSchema])
9399
if metric.get("skip_parsing"):
94100
return sql.strip()
95101

102+
# if the metric expression contains Jinja syntax, we can't parse it as SQL;
103+
# instead we fallback to the regex method
104+
if re.search(JINJAPATTERN, sql):
105+
return replace_metric_syntax(sql, metric["depends_on"], metrics)
106+
96107
try:
97108
expression = sqlglot.parse_one(sql, dialect=metric["dialect"])
98109
tokens = expression.find_all(exp.Column)
@@ -108,8 +119,7 @@ def get_metric_expression(metric_name: str, metrics: Dict[str, OGMetricSchema])
108119

109120
return expression.sql(dialect=metric["dialect"])
110121
except ParseError:
111-
sql = replace_metric_syntax(sql, metric["depends_on"], metrics)
112-
return sql
122+
return replace_metric_syntax(sql, metric["depends_on"], metrics)
113123

114124
sorted_metric = dict(sorted(metric.items()))
115125
raise Exception(f"Unable to generate metric expression from: {sorted_metric}")
@@ -300,7 +310,7 @@ def convert_query_to_projection(sql: str, dialect: MFSQLEngine) -> str:
300310
)
301311

302312
# replace aliases with their original expressions
303-
for node, _, _ in metric_expression.walk():
313+
for node in metric_expression.walk():
304314
if isinstance(node, Identifier) and node.sql() in aliases:
305315
node.replace(parse_one(aliases[node.sql()]))
306316

@@ -310,12 +320,12 @@ def convert_query_to_projection(sql: str, dialect: MFSQLEngine) -> str:
310320

311321
# Remove DISTINCT from metric to avoid conficting with CASE
312322
distinct = False
313-
for node, _, _ in metric_expression.this.walk():
323+
for node in metric_expression.this.walk():
314324
if isinstance(node, Distinct):
315325
distinct = True
316326
node.replace(node.expressions[0])
317327

318-
for node, _, _ in where_expression.walk():
328+
for node in where_expression.walk():
319329
if isinstance(node, Identifier) and node.sql() in aliases:
320330
node.replace(parse_one(aliases[node.sql()]))
321331

0 commit comments

Comments
 (0)