Skip to content

Commit 6f32e53

Browse files
authored
Feat: support non-strict qualify_columns (#4243)
* Feat: support qualify_columns non-strictly * add new flag
1 parent fcc05c9 commit 6f32e53

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

sqlglot/optimizer/qualify.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def qualify(
2727
infer_schema: t.Optional[bool] = None,
2828
isolate_tables: bool = False,
2929
qualify_columns: bool = True,
30+
allow_partial_qualification: bool = False,
3031
validate_qualify_columns: bool = True,
3132
quote_identifiers: bool = True,
3233
identify: bool = True,
@@ -56,6 +57,7 @@ def qualify(
5657
infer_schema: Whether to infer the schema if missing.
5758
isolate_tables: Whether to isolate table selects.
5859
qualify_columns: Whether to qualify columns.
60+
allow_partial_qualification: Whether to allow partial qualification.
5961
validate_qualify_columns: Whether to validate columns.
6062
quote_identifiers: Whether to run the quote_identifiers step.
6163
This step is necessary to ensure correctness for case sensitive queries.
@@ -90,6 +92,7 @@ def qualify(
9092
expand_alias_refs=expand_alias_refs,
9193
expand_stars=expand_stars,
9294
infer_schema=infer_schema,
95+
allow_partial_qualification=allow_partial_qualification,
9396
)
9497

9598
if quote_identifiers:

sqlglot/optimizer/qualify_columns.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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:

tests/fixtures/optimizer/qualify_columns.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ SELECT x._col_0 AS _col_0, x._col_1 AS _col_1 FROM (VALUES (1, 2)) AS x(_col_0,
190190
SELECT SOME_UDF(data).* FROM t;
191191
SELECT SOME_UDF(t.data).* FROM t AS t;
192192

193+
# execute: false
194+
# allow_partial_qualification: true
195+
# validate_qualify_columns: false
196+
SELECT a + 1 AS i, missing_column FROM x;
197+
SELECT x.a + 1 AS i, missing_column AS missing_column FROM x AS x;
198+
193199
--------------------------------------
194200
-- Derived tables
195201
--------------------------------------

0 commit comments

Comments
 (0)