@@ -22,6 +22,7 @@ def qualify_columns(
2222 expand_alias_refs : bool = True ,
2323 expand_stars : bool = True ,
2424 infer_schema : t .Optional [bool ] = None ,
25+ allow_partial_qualification : bool = False ,
2526) -> exp .Expression :
2627 """
2728 Rewrite sqlglot AST to have fully qualified columns.
@@ -41,6 +42,7 @@ def qualify_columns(
4142 for most of the optimizer's rules to work; do not set to False unless you
4243 know what you're doing!
4344 infer_schema: Whether to infer the schema if missing.
45+ allow_partial_qualification: Whether to allow partial qualification.
4446
4547 Returns:
4648 The qualified expression.
@@ -68,7 +70,7 @@ def qualify_columns(
6870 )
6971
7072 _convert_columns_to_dots (scope , resolver )
71- _qualify_columns (scope , resolver )
73+ _qualify_columns (scope , resolver , allow_partial_qualification = allow_partial_qualification )
7274
7375 if not schema .empty and expand_alias_refs :
7476 _expand_alias_refs (scope , resolver )
@@ -441,15 +443,20 @@ def _convert_columns_to_dots(scope: Scope, resolver: Resolver) -> None:
441443 scope .clear_cache ()
442444
443445
444- def _qualify_columns (scope : Scope , resolver : Resolver ) -> None :
446+ def _qualify_columns (scope : Scope , resolver : Resolver , allow_partial_qualification : bool ) -> None :
445447 """Disambiguate columns, ensuring each column specifies a source"""
446448 for column in scope .columns :
447449 column_table = column .table
448450 column_name = column .name
449451
450452 if column_table and column_table in scope .sources :
451453 source_columns = resolver .get_source_columns (column_table )
452- if source_columns and column_name not in source_columns and "*" not in source_columns :
454+ if (
455+ not allow_partial_qualification
456+ and source_columns
457+ and column_name not in source_columns
458+ and "*" not in source_columns
459+ ):
453460 raise OptimizeError (f"Unknown column: { column_name } " )
454461
455462 if not column_table :
0 commit comments