Skip to content

Commit a9581cf

Browse files
authored
Fix: alias queries even if not optimized (#895)
1 parent d40b40e commit a9581cf

File tree

6 files changed

+20
-16
lines changed

6 files changed

+20
-16
lines changed

sqlmesh/core/model/definition.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ def columns_to_types(self) -> t.Dict[str, exp.DataType]:
599599

600600
if self._columns_to_types is None:
601601
self._columns_to_types = {
602-
expression.alias_or_name: expression.type or exp.DataType.build("unknown")
602+
expression.output_name: expression.type or exp.DataType.build("unknown")
603603
for expression in self._query_renderer.render().expressions
604604
}
605605

@@ -632,7 +632,7 @@ def validate_definition(self) -> None:
632632

633633
name_counts: t.Dict[str, int] = {}
634634
for expression in projection_list:
635-
alias = expression.alias_or_name
635+
alias = expression.output_name
636636
if alias == "*":
637637
continue
638638
if not alias:

sqlmesh/core/renderer.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,12 +280,16 @@ def update_cache(
280280
def _optimize_query(self, query: exp.Expression) -> exp.Expression:
281281
schema = ensure_schema(self.schema, dialect=self._dialect)
282282

283-
query = query.copy()
283+
query = t.cast(exp.Subqueryable, query.copy())
284284
lower_identities(query)
285285
qualify_tables(query)
286286

287287
try:
288-
if not schema.empty:
288+
if schema.empty:
289+
for select in query.selects:
290+
if not isinstance(select, exp.Alias) and select.output_name not in ("*", ""):
291+
select.replace(exp.alias_(select, select.output_name))
292+
else:
289293
qualify_columns(query, schema=schema, infer_schema=False)
290294
except SqlglotError as ex:
291295
raise_config_error(

tests/core/test_audit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def test_number_of_rows_audit(model: Model):
215215
)
216216
assert (
217217
rendered_query.sql()
218-
== """SELECT 1 FROM (SELECT * FROM db.test_model AS test_model WHERE ds <= '1970-01-01' AND ds >= '1970-01-01') AS _q_0 HAVING COUNT(*) <= 0 LIMIT 1"""
218+
== """SELECT 1 AS "1" FROM (SELECT * FROM db.test_model AS test_model WHERE ds <= '1970-01-01' AND ds >= '1970-01-01') AS _q_0 HAVING COUNT(*) <= 0 LIMIT 1"""
219219
)
220220

221221

tests/core/test_model.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ def test_load(assert_exp_eq):
8585
]
8686
assert model.depends_on == {"db.other_table"}
8787
assert_exp_eq(
88-
model.query,
88+
model.render_query(),
8989
"""
9090
SELECT
9191
TRY_CAST(1 AS INT) AS a,
9292
TRY_CAST(2 AS DOUBLE) AS b,
93-
TRY_CAST(c AS BOOL),
93+
TRY_CAST(c AS BOOL) AS c,
9494
TRY_CAST(1 AS INT) AS d, -- d
9595
TRY_CAST(2 AS DOUBLE) AS e, -- e
96-
TRY_CAST(f AS BOOL), -- f
96+
TRY_CAST(f AS BOOL) AS f, -- f
9797
FROM
9898
db.other_table t1
9999
LEFT JOIN
@@ -576,7 +576,7 @@ def test_render_query(assert_exp_eq):
576576
model.render_query(start="2020-10-28", end="2020-10-28"),
577577
"""
578578
SELECT
579-
y
579+
y AS y
580580
FROM x AS x
581581
WHERE
582582
y <= '2020-10-28'
@@ -594,7 +594,7 @@ def test_render_query(assert_exp_eq):
594594
*
595595
FROM (
596596
SELECT
597-
y
597+
y AS y
598598
FROM x AS x
599599
WHERE
600600
y <= '2020-10-28'
@@ -889,7 +889,7 @@ def test_parse(assert_exp_eq):
889889
"""
890890
SELECT
891891
CAST(id AS INT) AS id,
892-
ds
892+
ds AS ds
893893
FROM x AS x
894894
WHERE
895895
ds <= '1970-01-01' AND ds >= '1970-01-01'

tests/core/test_snapshot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ def test_fingerprint(model: Model, parent_model: Model):
358358
fingerprint = fingerprint_from_model(model, models={})
359359

360360
original_fingerprint = SnapshotFingerprint(
361-
data_hash="2044395723",
361+
data_hash="1204843539",
362362
metadata_hash="382750147",
363363
)
364364

@@ -440,7 +440,7 @@ def test_fingerprint_jinja_macros(model: Model):
440440
fingerprint = fingerprint_from_model(model, models={})
441441

442442
original_fingerprint = SnapshotFingerprint(
443-
data_hash="2646014410",
443+
data_hash="3334558345",
444444
metadata_hash="382750147",
445445
)
446446

tests/dbt/test_config.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,19 +189,19 @@ def test_variables(assert_exp_eq, sushi_test_project):
189189
# Case 2: using a defined variable without a default value
190190
defined_variables["foo"] = 6
191191
context.variables = defined_variables
192-
assert_exp_eq(model_config.to_sqlmesh(**kwargs).render_query(), "SELECT 6")
192+
assert_exp_eq(model_config.to_sqlmesh(**kwargs).render_query(), 'SELECT 6 AS "6"')
193193

194194
# Case 3: using a defined variable with a default value
195195
model_config.sql = "SELECT {{ var('foo', 5) }}"
196196
model_config._sql_no_config = None
197197

198-
assert_exp_eq(model_config.to_sqlmesh(**kwargs).render_query(), "SELECT 6")
198+
assert_exp_eq(model_config.to_sqlmesh(**kwargs).render_query(), 'SELECT 6 AS "6"')
199199

200200
# Case 4: using an undefined variable with a default value
201201
del defined_variables["foo"]
202202
context.variables = defined_variables
203203

204-
assert_exp_eq(model_config.to_sqlmesh(**kwargs).render_query(), "SELECT 5")
204+
assert_exp_eq(model_config.to_sqlmesh(**kwargs).render_query(), 'SELECT 5 AS "5"')
205205

206206
# Finally, check that variable scoping & overwriting (some_var) works as expected
207207
expected_sushi_variables = {

0 commit comments

Comments
 (0)