Skip to content

Commit 6b80ffc

Browse files
committed
fix: unicode in model name databricks
1 parent a255e17 commit 6b80ffc

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

sqlmesh/core/engine_adapter/databricks.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class DatabricksEngineAdapter(SparkEngineAdapter):
3434
SUPPORTS_CLONING = True
3535
SUPPORTS_MATERIALIZED_VIEWS = True
3636
SUPPORTS_MATERIALIZED_VIEW_SCHEMA = True
37+
# Spark has this set to false for compatibility when mixing with Trino but that isn't a concern with Databricks
38+
QUOTE_IDENTIFIERS_IN_VIEWS = True
3739
SCHEMA_DIFFER_KWARGS = {
3840
"support_positional_add": True,
3941
"nested_support": NestedSupport.ALL,

tests/core/engine_adapter/integration/test_integration.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3990,3 +3990,39 @@ def _set_config(gateway: str, config: Config) -> None:
39903990
was_evaluated=True,
39913991
day_delta=4,
39923992
)
3993+
3994+
3995+
def test_unicode_characters(ctx: TestContext, tmp_path: Path):
3996+
if ctx.dialect in ["spark", "trino"]:
3997+
# It is possible that Trino could support this if we changed `QUOTE_IDENTIFIERS_IN_VIEWS` but that would
3998+
# break the compatibility it has when be mixed with Spark for compute
3999+
pytest.skip("Skipping as these engines have issues with unicode characters in model names")
4000+
4001+
model_name = "客户数据"
4002+
table = ctx.table(model_name).sql(dialect=ctx.dialect)
4003+
(tmp_path / "models").mkdir(exist_ok=True)
4004+
4005+
model_def = f"""
4006+
MODEL (
4007+
name {table},
4008+
kind FULL,
4009+
dialect '{ctx.dialect}'
4010+
);
4011+
SELECT 1 as id
4012+
"""
4013+
4014+
(tmp_path / "models" / "客户数据.sql").write_text(model_def)
4015+
4016+
context = ctx.create_context(path=tmp_path)
4017+
context.plan(auto_apply=True, no_prompts=True)
4018+
4019+
results = ctx.get_metadata_results()
4020+
assert len(results.views) == 1
4021+
assert results.views[0].lower() == model_name
4022+
4023+
schema = d.to_schema(ctx.schema(), dialect=ctx.dialect)
4024+
schema_name = schema.args["db"].this
4025+
schema.args["db"].set("this", "sqlmesh__" + schema_name)
4026+
table_results = ctx.get_metadata_results(schema)
4027+
assert len(table_results.tables) == 1
4028+
assert table_results.tables[0].lower().startswith(schema_name.lower() + "________")

tests/core/engine_adapter/test_databricks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def test_materialized_view_properties(mocker: MockFixture, make_mocked_engine_ad
195195
sql_calls = to_sql_calls(adapter)
196196
# https://docs.databricks.com/en/sql/language-manual/sql-ref-syntax-ddl-create-materialized-view.html#syntax
197197
assert sql_calls == [
198-
"CREATE OR REPLACE MATERIALIZED VIEW test_table PARTITIONED BY (ds) AS SELECT 1",
198+
"CREATE OR REPLACE MATERIALIZED VIEW `test_table` PARTITIONED BY (`ds`) AS SELECT 1",
199199
]
200200

201201

0 commit comments

Comments
 (0)