From 42862fbfe6954861c8aa8a44daaf15fa20579b4c Mon Sep 17 00:00:00 2001 From: Naoki Takezoe Date: Thu, 17 Aug 2023 14:22:03 +0900 Subject: [PATCH] sql (fix): ResolvedIdentifier can wrongly have a qualifier (#3142) --- .../wvlet/airframe/sql/analyzer/TypeResolver.scala | 2 +- .../airframe/sql/analyzer/TypeResolverTest.scala | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/airframe-sql/src/main/scala/wvlet/airframe/sql/analyzer/TypeResolver.scala b/airframe-sql/src/main/scala/wvlet/airframe/sql/analyzer/TypeResolver.scala index 9b21f00a38..179cee655b 100644 --- a/airframe-sql/src/main/scala/wvlet/airframe/sql/analyzer/TypeResolver.scala +++ b/airframe-sql/src/main/scala/wvlet/airframe/sql/analyzer/TypeResolver.scala @@ -451,7 +451,7 @@ object TypeResolver extends LogSupport { val results = expr match { case i: Identifier => - lookup(i.value, context).map(toResolvedAttribute(i.value, _)) + lookup(i.value, context).map(toResolvedAttribute(i.value, _).withQualifier(None)) case u @ UnresolvedAttribute(qualifier, name, _, _) => lookup(u.fullName, context).map(toResolvedAttribute(name, _).withQualifier(qualifier)) case a @ AllColumns(qualifier, None, _, _) => diff --git a/airframe-sql/src/test/scala/wvlet/airframe/sql/analyzer/TypeResolverTest.scala b/airframe-sql/src/test/scala/wvlet/airframe/sql/analyzer/TypeResolverTest.scala index c294cce208..50a69c5a1a 100644 --- a/airframe-sql/src/test/scala/wvlet/airframe/sql/analyzer/TypeResolverTest.scala +++ b/airframe-sql/src/test/scala/wvlet/airframe/sql/analyzer/TypeResolverTest.scala @@ -1138,4 +1138,15 @@ class TypeResolverTest extends AirSpec with ResolverTestHelper { col.sourceColumn.head.fullName shouldBe "A.id" } } + + test("Resolve identifier refers to column alias for qualified column") { + val p1 = analyze("select count(xid) from (select n1.id as xid from A n1 inner join B on n1.id = B.id)") + p1.outputAttributes shouldMatch { case List(SingleColumn(f: FunctionCall, None, None, _)) => + f.functionName shouldBe "count" + f.args shouldMatch { case Seq(col: ResolvedAttribute) => + col.fullName shouldBe "xid" + col.sourceColumn.map(_.column) shouldBe Some(a1) + } + } + } }