Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sql (fix): Resolve UNNEST earlier to prevent cyclic references (#3312)
## Case 1 ```sql SELECT id, t.name FROM A CROSS JOIN UNNEST (name) AS t (name) ``` This SQL is valid but fails to resolve with the following error: ``` wvlet.airframe.sql.SQLError: [SyntaxError] line 1:45 name is ambiguous: - *name:string <- A.name - *name:? ``` Because the output attribute of AliasedRelation (`t (name)`) is included in the source attributes when resolve `UNNEST (name)` here: https://github.com/wvlet/airframe/blob/49d31bad29eccd2459eb4c02593d371c1b55082a/airframe-sql/src/main/scala/wvlet/airframe/sql/analyzer/TypeResolver.scala#L319-L321 - `r` is CrossJoin - `r.inputAttributes` is `left.outputAttributes + right.outputAttributes` - `right` is AliasedRelation(Unnest) I think output attributes of AliasedRelation shouldn't be in the scope when Unnest is resolved but I didn't come up with an easy fix for this issue. ## Case 2 ```sql SELECT id, t.name FROM A CROSS JOIN UNNEST (A.name) AS t (name) ``` This SQL is also valid but fails to resolve with the following error: ``` Found unresolved expressions in: [sql] SELECT id, t.name FROM A CROSS JOIN UNNEST (A.name) AS t (name) [plan] [Project]: (id:long, name:string, name:string) => (id:long, name:?) - *id:long <- A.id - UnresolvedAttribute(t.name) [CrossJoin]: (id:long, name:string, name:string) => (id:long, name:string, name:string) - NaturalJoin [TableScan] default.A: => (id:long, name:string) [AliasedRelation]: => (name:string) - ResolvedIdentifier(Id(t)) [Unnest]: => (name:string) - *A.name:string <- A.name ``` Maybe this is because the resolved UNNEST column has a qualifier. This case might not be UNNEST specific.
- Loading branch information