Skip to content

Commit 6688bce

Browse files
committed
fix: enforce 255-character identifier length limit for Databricks relations
Add relation_max_name_length() returning 255 and a __post_init__ validation to DatabricksRelation, following the same pattern used by the Postgres adapter. This catches overly-long table names (commonly generated by store_failures for tests with verbose names) at relation creation time with a clear error message instead of a cryptic runtime DatabricksExecutionError from the SQL engine. Closes #1309
1 parent b1047b5 commit 6688bce

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

dbt/adapters/databricks/relation.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818

1919
KEY_TABLE_PROVIDER = "Provider"
2020

21+
# Databricks (Unity Catalog / Hive Metastore) enforces a 255-character limit
22+
# on table and view names. This constant is used by relation_max_name_length()
23+
# to validate identifier lengths at relation creation time rather than at
24+
# SQL execution time, providing a clear error message instead of a cryptic
25+
# DatabricksExecutionError.
26+
MAX_CHARACTERS_IN_IDENTIFIER = 255
27+
2128

2229
@dataclass
2330
class DatabricksQuotePolicy(Policy):
@@ -86,6 +93,27 @@ class DatabricksRelation(BaseRelation):
8693
databricks_table_type: Optional[DatabricksTableType] = None
8794
temporary: Optional[bool] = False
8895

96+
def __post_init__(self):
97+
# Validate identifier length against Databricks' 255-character limit.
98+
# Check self.type to exclude test relation identifiers that may not
99+
# have a type set yet.
100+
if (
101+
self.identifier is not None
102+
and self.type is not None
103+
and len(self.identifier) > self.relation_max_name_length()
104+
):
105+
raise DbtRuntimeError(
106+
f"Relation name '{self.identifier}' "
107+
f"is longer than {self.relation_max_name_length()} characters. "
108+
f"Databricks has a maximum identifier length of "
109+
f"{self.relation_max_name_length()} characters. "
110+
f"If this is a store_failures table, consider using a shorter "
111+
f"test name or setting a custom alias."
112+
)
113+
114+
def relation_max_name_length(self):
115+
return MAX_CHARACTERS_IN_IDENTIFIER
116+
89117
@classmethod
90118
def __pre_deserialize__(cls, data: dict[Any, Any]) -> dict[Any, Any]:
91119
data = super().__pre_deserialize__(data)

0 commit comments

Comments
 (0)