Skip to content

Commit 686c22e

Browse files
viiryayhuai
authored andcommitted
[SPARK-10960] [SQL] SQL with windowing function should be able to refer column in inner select
JIRA: https://issues.apache.org/jira/browse/SPARK-10960 When accessing a column in inner select from a select with window function, `AnalysisException` will be thrown. For example, an query like this: select area, rank() over (partition by area order by tmp.month) + tmp.tmp1 as c1 from (select month, area, product, 1 as tmp1 from windowData) tmp Currently, the rule `ExtractWindowExpressions` in `Analyzer` only extracts regular expressions from `WindowFunction`, `WindowSpecDefinition` and `AggregateExpression`. We need to also extract other attributes as the one in `Alias` as shown in the above query. Author: Liang-Chi Hsieh <[email protected]> Closes #9011 from viirya/fix-window-inner-column. (cherry picked from commit fcb37a0) Signed-off-by: Yin Huai <[email protected]>
1 parent e2ff491 commit 686c22e

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,10 @@ class Analyzer(
715715
val withName = Alias(agg, s"_w${extractedExprBuffer.length}")()
716716
extractedExprBuffer += withName
717717
withName.toAttribute
718+
719+
// Extracts other attributes
720+
case attr: Attribute => extractExpr(attr)
721+
718722
}.asInstanceOf[NamedExpression]
719723
}
720724

sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLQuerySuite.scala

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,33 @@ class SQLQuerySuite extends QueryTest {
709709
).map(i => Row(i._1, i._2, i._3)))
710710
}
711711

712+
test("window function: refer column in inner select block") {
713+
val data = Seq(
714+
WindowData(1, "a", 5),
715+
WindowData(2, "a", 6),
716+
WindowData(3, "b", 7),
717+
WindowData(4, "b", 8),
718+
WindowData(5, "c", 9),
719+
WindowData(6, "c", 10)
720+
)
721+
sparkContext.parallelize(data).toDF().registerTempTable("windowData")
722+
723+
checkAnswer(
724+
sql(
725+
"""
726+
|select area, rank() over (partition by area order by tmp.month) + tmp.tmp1 as c1
727+
|from (select month, area, product, 1 as tmp1 from windowData) tmp
728+
""".stripMargin),
729+
Seq(
730+
("a", 2),
731+
("a", 3),
732+
("b", 2),
733+
("b", 3),
734+
("c", 2),
735+
("c", 3)
736+
).map(i => Row(i._1, i._2)))
737+
}
738+
712739
test("window function: partition and order expressions") {
713740
val data = Seq(
714741
WindowData(1, "a", 5),

0 commit comments

Comments
 (0)