Skip to content

Commit 0dccfdc

Browse files
fix(fabric): Use exp.Table to extract extract schema name
1 parent 9a720c8 commit 0dccfdc

File tree

1 file changed

+68
-91
lines changed

1 file changed

+68
-91
lines changed

sqlmesh/core/engine_adapter/fabric.py

Lines changed: 68 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -390,11 +390,18 @@ def drop_schema(
390390
"""
391391
logger.debug(f"drop_schema called with: {schema_name} (type: {type(schema_name)})")
392392

393-
# If it's a string with a dot, assume it's catalog.schema format
394-
if isinstance(schema_name, str) and "." in schema_name:
395-
parts = schema_name.split(".", 1) # Split only on first dot
396-
catalog_name = parts[0].strip('"[]') # Remove quotes/brackets
397-
schema_only = parts[1].strip('"[]')
393+
# Parse schema_name into an exp.Table to properly handle both string and Table cases
394+
table = exp.to_table(schema_name)
395+
396+
if table.catalog:
397+
# 3-part name detected (catalog.db.table) - this shouldn't happen for schema operations
398+
raise SQLMeshError(
399+
f"Invalid schema name format: {schema_name}. Expected 'schema' or 'catalog.schema'"
400+
)
401+
elif table.db:
402+
# Catalog-qualified schema: catalog.schema
403+
catalog_name = table.db
404+
schema_only = table.name
398405
logger.debug(
399406
f"Detected catalog.schema format: catalog='{catalog_name}', schema='{schema_only}'"
400407
)
@@ -421,11 +428,18 @@ def create_schema(
421428
"""
422429
logger.debug(f"create_schema called with: {schema_name} (type: {type(schema_name)})")
423430

424-
# If it's a string with a dot, assume it's catalog.schema format
425-
if isinstance(schema_name, str) and "." in schema_name:
426-
parts = schema_name.split(".", 1) # Split only on first dot
427-
catalog_name = parts[0].strip('"[]') # Remove quotes/brackets
428-
schema_only = parts[1].strip('"[]')
431+
# Parse schema_name into an exp.Table to properly handle both string and Table cases
432+
table = exp.to_table(schema_name)
433+
434+
if table.catalog:
435+
# 3-part name detected (catalog.db.table) - this shouldn't happen for schema operations
436+
raise SQLMeshError(
437+
f"Invalid schema name format: {schema_name}. Expected 'schema' or 'catalog.schema'"
438+
)
439+
elif table.db:
440+
# Catalog-qualified schema: catalog.schema
441+
catalog_name = table.db
442+
schema_only = table.name
429443
logger.debug(
430444
f"Detected catalog.schema format: catalog='{catalog_name}', schema='{schema_only}'"
431445
)
@@ -572,89 +586,52 @@ def create_view(
572586
"""
573587
logger.debug(f"create_view called with: {view_name} (type: {type(view_name)})")
574588

589+
# Parse view_name into an exp.Table to properly handle both string and Table cases
590+
table = exp.to_table(view_name)
591+
575592
# Ensure schema exists for the view
576-
if isinstance(view_name, exp.Table):
577-
self._ensure_schema_exists(view_name)
578-
elif isinstance(view_name, str):
579-
# Parse string to table for schema extraction
580-
parsed_table = exp.to_table(view_name)
581-
self._ensure_schema_exists(parsed_table)
582-
583-
# Handle exp.Table objects that might be catalog-qualified
584-
if isinstance(view_name, exp.Table):
585-
if view_name.catalog:
586-
# Has catalog qualification - switch to catalog and use schema.table
587-
catalog_name = view_name.catalog
588-
schema_name = view_name.db or ""
589-
table_name = view_name.name
590-
591-
logger.debug(
592-
f"Detected exp.Table with catalog: catalog='{catalog_name}', schema='{schema_name}', table='{table_name}'"
593-
)
593+
self._ensure_schema_exists(table)
594594

595-
# Switch to the catalog first
596-
self.set_current_catalog(catalog_name)
597-
598-
# Create new Table expression without catalog
599-
unqualified_view = exp.Table(this=table_name, db=schema_name)
600-
601-
super().create_view(
602-
unqualified_view,
603-
query_or_df,
604-
columns_to_types,
605-
replace,
606-
materialized,
607-
materialized_properties,
608-
table_description,
609-
column_descriptions,
610-
view_properties,
611-
**create_kwargs,
612-
)
613-
return
595+
if table.catalog:
596+
# 3-part name: catalog.schema.view
597+
catalog_name = table.catalog
598+
schema_name = table.db or ""
599+
view_only = table.name
614600

615-
# Handle string view names that might be catalog-qualified
616-
elif isinstance(view_name, str):
617-
# Check if it's in catalog.schema.view format
618-
parts = view_name.split(".")
619-
if len(parts) == 3:
620-
# catalog.schema.view format
621-
catalog_name = parts[0].strip('"[]')
622-
schema_name = parts[1].strip('"[]')
623-
view_only = parts[2].strip('"[]')
624-
unqualified_view_str = f"{schema_name}.{view_only}"
625-
logger.debug(
626-
f"Detected catalog.schema.view format: catalog='{catalog_name}', unqualified='{unqualified_view_str}'"
627-
)
601+
logger.debug(
602+
f"Detected catalog.schema.view format: catalog='{catalog_name}', schema='{schema_name}', view='{view_only}'"
603+
)
628604

629-
# Switch to the catalog first
630-
self.set_current_catalog(catalog_name)
631-
632-
# Use just the schema.view name
633-
super().create_view(
634-
unqualified_view_str,
635-
query_or_df,
636-
columns_to_types,
637-
replace,
638-
materialized,
639-
materialized_properties,
640-
table_description,
641-
column_descriptions,
642-
view_properties,
643-
**create_kwargs,
644-
)
645-
return
605+
# Switch to the catalog first
606+
self.set_current_catalog(catalog_name)
646607

647-
# No catalog qualification, use as-is
648-
logger.debug(f"No catalog detected, using original: {view_name}")
649-
super().create_view(
650-
view_name,
651-
query_or_df,
652-
columns_to_types,
653-
replace,
654-
materialized,
655-
materialized_properties,
656-
table_description,
657-
column_descriptions,
658-
view_properties,
659-
**create_kwargs,
660-
)
608+
# Create new Table expression without catalog
609+
unqualified_view = exp.Table(this=view_only, db=schema_name)
610+
611+
super().create_view(
612+
unqualified_view,
613+
query_or_df,
614+
columns_to_types,
615+
replace,
616+
materialized,
617+
materialized_properties,
618+
table_description,
619+
column_descriptions,
620+
view_properties,
621+
**create_kwargs,
622+
)
623+
else:
624+
# No catalog qualification, use as-is
625+
logger.debug(f"No catalog detected, using original: {view_name}")
626+
super().create_view(
627+
view_name,
628+
query_or_df,
629+
columns_to_types,
630+
replace,
631+
materialized,
632+
materialized_properties,
633+
table_description,
634+
column_descriptions,
635+
view_properties,
636+
**create_kwargs,
637+
)

0 commit comments

Comments
 (0)