Skip to content

Commit

Permalink
Fix resolving CTE columns in AliasedRelation
Browse files Browse the repository at this point in the history
  • Loading branch information
takezoe committed Jul 11, 2023
1 parent 93034eb commit 946258b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,7 @@ object TypeResolver extends LogSupport {
a.copy(expr = resolved)
}
case SingleColumn(a: Attribute, qualifier, _) if a.resolved =>
// Optimizes the nested attributes, but preserves qualifier in the parent
a.setQualifierIfEmpty(qualifier)
a.withQualifier(qualifier)
case m: MultiSourceColumn =>
var changed = false
val resolvedInputs = m.inputs.map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1070,4 +1070,20 @@ class TypeResolverTest extends AirSpec with ResolverTestHelper {
analyze("select \"prénom\" from (select name as \"prénom\" from A)")
// No error
}

test("resolve CTE in AliasedRelation") {
val p1 = analyze("with t1 as (select id from A) select id from (select id from t1) t2")
p1.outputAttributes shouldMatch {
case List(col: ResolvedAttribute) =>
col.fullName shouldBe "id"
col.sourceColumn.head.fullName shouldBe "A.id"
}

val p2 = analyze("with t1 as (select id from A) select count(id) from (select id from t1) t2")
p2.outputAttributes shouldMatch {
case List(SingleColumn(FunctionCall("count", Seq(col: ResolvedAttribute), _, _, _, _), _, _)) =>
col.fullName shouldBe "t2.id"
col.sourceColumn.head.fullName shouldBe "A.id"
}
}
}

0 comments on commit 946258b

Please sign in to comment.