Skip to content

Commit 449b97a

Browse files
committed
PR feedback
1 parent 5ff525c commit 449b97a

3 files changed

Lines changed: 85 additions & 5 deletions

File tree

sqlmesh/core/engine_adapter/trino.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
SourceQuery,
2828
set_catalog,
2929
)
30-
from sqlmesh.utils.errors import SQLMeshError
3130
from sqlmesh.core.schema_diff import SchemaDiffer
3231
from sqlmesh.utils.date import TimeLike
3332

@@ -98,10 +97,8 @@ def session(self, properties: SessionProperties) -> t.Iterator[None]:
9897
yield
9998
return
10099

101-
if isinstance(authorization, str):
100+
if not isinstance(authorization, exp.Expression):
102101
authorization = exp.Literal.string(authorization)
103-
if not (isinstance(authorization, exp.Expression) and authorization.is_string):
104-
raise SQLMeshError(f"Invalid authorization: '{authorization}'")
105102

106103
authorization_sql = authorization.sql(dialect=self.dialect)
107104

sqlmesh/core/model/meta.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,9 @@ def session_properties_validator(cls, v: t.Any, info: ValidationInfo) -> t.Any:
319319
return parsed_session_properties
320320

321321
for eq in parsed_session_properties:
322-
if eq.name == "query_label":
322+
prop_name = eq.left.name
323+
324+
if prop_name == "query_label":
323325
query_label = eq.right
324326
if not (
325327
isinstance(query_label, exp.Array)
@@ -345,6 +347,12 @@ def session_properties_validator(cls, v: t.Any, info: ValidationInfo) -> t.Any:
345347
raise ConfigError(
346348
"Invalid entry in `session_properties.query_label`. Must be tuples of string literals with length 2."
347349
)
350+
elif prop_name == "authorization":
351+
authorization = eq.right
352+
if not (isinstance(authorization, exp.Literal) and authorization.is_string):
353+
raise ConfigError(
354+
"Invalid value for `session_properties.authorization`. Must be a string literal."
355+
)
348356

349357
return parsed_session_properties
350358

tests/core/test_model.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4531,6 +4531,81 @@ def test_model_session_properties(sushi_context):
45314531
)
45324532

45334533

4534+
def test_session_properties_authorization_validation():
4535+
model = load_sql_based_model(
4536+
d.parse(
4537+
"""
4538+
MODEL (
4539+
name test_schema.test_model,
4540+
session_properties (
4541+
authorization = 'test_user'
4542+
)
4543+
);
4544+
SELECT a FROM tbl;
4545+
""",
4546+
default_dialect="trino",
4547+
)
4548+
)
4549+
assert model.session_properties == {"authorization": "test_user"}
4550+
4551+
with pytest.raises(
4552+
ConfigError,
4553+
match=r"Invalid value for `session_properties.authorization`. Must be a string literal.",
4554+
):
4555+
load_sql_based_model(
4556+
d.parse(
4557+
"""
4558+
MODEL (
4559+
name test_schema.test_model,
4560+
session_properties (
4561+
authorization = 123
4562+
)
4563+
);
4564+
SELECT a FROM tbl;
4565+
""",
4566+
default_dialect="trino",
4567+
)
4568+
)
4569+
4570+
with pytest.raises(
4571+
ConfigError,
4572+
match=r"Invalid value for `session_properties.authorization`. Must be a string literal.",
4573+
):
4574+
load_sql_based_model(
4575+
d.parse(
4576+
"""
4577+
MODEL (
4578+
name test_schema.test_model,
4579+
session_properties (
4580+
authorization = some_column
4581+
)
4582+
);
4583+
SELECT a FROM tbl;
4584+
""",
4585+
default_dialect="trino",
4586+
)
4587+
)
4588+
4589+
with pytest.raises(
4590+
ConfigError,
4591+
match=r"Invalid value for `session_properties.authorization`. Must be a string literal.",
4592+
):
4593+
load_sql_based_model(
4594+
d.parse(
4595+
"""
4596+
MODEL (
4597+
name test_schema.test_model,
4598+
session_properties (
4599+
authorization = CONCAT('user', '_suffix')
4600+
)
4601+
);
4602+
SELECT a FROM tbl;
4603+
""",
4604+
default_dialect="trino",
4605+
)
4606+
)
4607+
4608+
45344609
def test_model_jinja_macro_rendering():
45354610
expressions = d.parse(
45364611
"""

0 commit comments

Comments
 (0)