You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 4d9468c
Browse filesBrowse the repository at this point in the historyBrowse files
[fix](subquery) Resolve correlated qualified columns before dereference (apache#67438)
### What problem does this PR solve?
Issue Number: None
Related PR: None
Problem Summary:
In a correlated subquery, a multipart reference can represent either a
relation-qualified column (`table_alias.column`) or a nested-field
dereference (`column.field`). Nereids previously searched the inner
scope completely before checking the outer scope. If an inner table had
a physical column with the same name as an outer table alias, the outer
reference could therefore be interpreted as a nested field of that inner
column.
For scalar inner columns this raised a `No such field` analysis error.
For complex inner columns it could bind successfully to the wrong
expression and produce incorrect results.
#### Example
```sql
CREATE TABLE outer_events (
id INT,
`@event_name` VARCHAR(32)
)
DISTRIBUTED BY HASH(id) BUCKETS 1
PROPERTIES ("replication_num" = "1");
CREATE TABLE inner_events (
id INT,
t1 INT
)
DISTRIBUTED BY HASH(id) BUCKETS 1
PROPERTIES ("replication_num" = "1");
INSERT INTO outer_events VALUES (1, 'blocked'), (2, 'kept');
INSERT INTO inner_events VALUES (1, 0);
SELECT t1.id, t1.`@event_name`
FROM outer_events t1
WHERE NOT EXISTS (
SELECT 1
FROM inner_events inner_alias
WHERE t1.`@event_name` = 'blocked'
)
ORDER BY t1.id;
```
Before this fix, the outer alias `t1` conflicted with the physical inner
column `inner_events.t1`. Nereids treated ``t1.`@event_name``` as a
nested-field access on the inner scalar column and failed during
analysis:
```text
No such field '@event_name' in 't1'
```
After this fix, ``t1.`@event_name``` is correctly bound to the outer
relation alias and the query returns:
```text
+------+-------------+
| id | @event_name |
+------+-------------+
| 2 | kept |
+------+-------------+
```
This change resolves relation-qualified columns in the current and outer
scopes before falling back to first-part-as-column dereference. The
relation-only phase follows each analyzer's complete local scope order
before searching the outer scope, so custom HAVING and QUALIFY scopes
preserve normal nearest-relation shadowing.
Lambda lexical scope is preserved: in `array_map(x -> x.value,
x.items)`, the first `x` inside the lambda resolves to the lambda
argument while the second `x` resolves to the enclosing table alias.
When an outer qualified reference contains nested fields, such as
`outer_alias.payload.k`, the underlying `payload` slot is also recorded
as a correlated slot.
### Release note
Fix incorrect Nereids column binding when an outer table alias conflicts
with an inner column name in a correlated subquery.
### Check List (For Author)
- Test: Regression test / Unit Test
- `./run-fe-ut.sh --run
org.apache.doris.nereids.rules.analysis.TestDereference`
- Result: 10 tests passed, 0 failures
- Added `query_p0/test_dereference` regression coverage for scalar and
complex inner columns, lambda lexical scope, outer nested-field
correlation, reused inner/outer aliases in HAVING and QUALIFY, and local
qualifier shadowing
- `./run-regression-test.sh --run -d query_p0 -s test_dereference`
- Result: suite passed against the generated snapshot
- Behavior changed: Yes (relation-qualified correlated references now
take priority over inner column-field dereference while preserving
complete local-scope precedence)
- Does this need documentation: No
0 commit comments