@@ -106,6 +106,7 @@ class EngineAdapter:
106106 SUPPORTS_REPLACE_TABLE = True
107107 DEFAULT_CATALOG_TYPE = DIALECT
108108 QUOTE_IDENTIFIERS_IN_VIEWS = True
109+ MAX_IDENTIFIER_LENGTH : t .Optional [int ] = None
109110
110111 def __init__ (
111112 self ,
@@ -2138,14 +2139,12 @@ def execute(
21382139 )
21392140 with self .transaction ():
21402141 for e in ensure_list (expressions ):
2141- sql = t .cast (
2142- str ,
2143- (
2144- self ._to_sql (e , quote = quote_identifiers , ** to_sql_kwargs )
2145- if isinstance (e , exp .Expression )
2146- else e
2147- ),
2148- )
2142+ if isinstance (e , exp .Expression ):
2143+ self ._check_identifier_length (e , check_only_ddl = True )
2144+ sql = self ._to_sql (e , quote = quote_identifiers , ** to_sql_kwargs )
2145+ else :
2146+ sql = t .cast (str , e )
2147+
21492148 self ._log_sql (
21502149 sql ,
21512150 expression = e if isinstance (e , exp .Expression ) else None ,
@@ -2516,6 +2515,22 @@ def ping(self) -> None:
25162515 def _select_columns (cls , columns : t .Iterable [str ]) -> exp .Select :
25172516 return exp .select (* (exp .column (c , quoted = True ) for c in columns ))
25182517
2518+ def _check_identifier_length (
2519+ self , expression : exp .Expression , check_only_ddl : bool = True
2520+ ) -> None :
2521+ if self .MAX_IDENTIFIER_LENGTH is None or (
2522+ check_only_ddl and not isinstance (expression , exp .DDL )
2523+ ):
2524+ return
2525+
2526+ for identifier in expression .find_all (exp .Identifier ):
2527+ name = identifier .name
2528+ name_length = len (name )
2529+ if name_length > self .MAX_IDENTIFIER_LENGTH :
2530+ raise SQLMeshError (
2531+ f"Identifier name { name } (length { name_length } ) exceeds { self .dialect .capitalize ()} 's max identifier limit of { self .MAX_IDENTIFIER_LENGTH } characters"
2532+ )
2533+
25192534
25202535class EngineAdapterWithIndexSupport (EngineAdapter ):
25212536 SUPPORTS_INDEXES = True
0 commit comments