@@ -643,13 +643,17 @@ def _override(klass: t.Type[Tokenizer | Parser], func: t.Callable) -> None:
643643
644644
645645def format_model_expressions (
646- expressions : t .List [exp .Expression ], dialect : t .Optional [str ] = None , ** kwargs : t .Any
646+ expressions : t .List [exp .Expression ],
647+ dialect : t .Optional [str ] = None ,
648+ rewrite_casts : bool = True ,
649+ ** kwargs : t .Any ,
647650) -> str :
648651 """Format a model's expressions into a standardized format.
649652
650653 Args:
651654 expressions: The model's expressions, must be at least model def + query.
652655 dialect: The dialect to render the expressions as.
656+ rewrite_casts: Whether to rewrite all casts to use the :: syntax.
653657 **kwargs: Additional keyword arguments to pass to the sql generator.
654658
655659 Returns:
@@ -660,26 +664,28 @@ def format_model_expressions(
660664
661665 * statements , query = expressions
662666
663- def cast_to_colon (node : exp .Expression ) -> exp .Expression :
664- if isinstance (node , exp .Cast ) and not any (
665- # Only convert CAST into :: if it doesn't have additional args set, otherwise this
666- # conversion could alter the semantics (eg. changing SAFE_CAST in BigQuery to CAST)
667- arg
668- for name , arg in node .args .items ()
669- if name not in ("this" , "to" )
670- ):
671- this = node .this
667+ if rewrite_casts :
672668
673- if not isinstance (this , (exp .Binary , exp .Unary )) or isinstance (this , exp .Paren ):
674- cast = DColonCast (this = this , to = node .to )
675- cast .comments = node .comments
676- node = cast
669+ def cast_to_colon (node : exp .Expression ) -> exp .Expression :
670+ if isinstance (node , exp .Cast ) and not any (
671+ # Only convert CAST into :: if it doesn't have additional args set, otherwise this
672+ # conversion could alter the semantics (eg. changing SAFE_CAST in BigQuery to CAST)
673+ arg
674+ for name , arg in node .args .items ()
675+ if name not in ("this" , "to" )
676+ ):
677+ this = node .this
677678
678- exp .replace_children (node , cast_to_colon )
679- return node
679+ if not isinstance (this , (exp .Binary , exp .Unary )) or isinstance (this , exp .Paren ):
680+ cast = DColonCast (this = this , to = node .to )
681+ cast .comments = node .comments
682+ node = cast
683+
684+ exp .replace_children (node , cast_to_colon )
685+ return node
680686
681- query = query .copy ()
682- exp .replace_children (query , cast_to_colon )
687+ query = query .copy ()
688+ exp .replace_children (query , cast_to_colon )
683689
684690 return ";\n \n " .join (
685691 [
0 commit comments