@@ -19,8 +19,8 @@ package org.apache.spark.sql
1919
2020import scala .util .Random
2121
22- import org .apache . spark . sql . catalyst . expressions .{ Alias , Literal }
23- import org . apache . spark . sql . catalyst . expressions . aggregate . Count
22+ import org .scalatest . Matchers . the
23+
2424import org .apache .spark .sql .execution .WholeStageCodegenExec
2525import org .apache .spark .sql .execution .aggregate .{HashAggregateExec , ObjectHashAggregateExec , SortAggregateExec }
2626import org .apache .spark .sql .execution .exchange .ShuffleExchangeExec
@@ -687,4 +687,29 @@ class DataFrameAggregateSuite extends QueryTest with SharedSQLContext {
687687 }
688688 }
689689 }
690+
691+ test(" SPARK-21896: Window functions inside aggregate functions" ) {
692+ def checkWindowError (df : => DataFrame ): Unit = {
693+ val thrownException = the [AnalysisException ] thrownBy {
694+ df.queryExecution.analyzed
695+ }
696+ assert(thrownException.message.contains(" not allowed to use a window function" ))
697+ }
698+
699+ checkWindowError(testData2.select(min(avg(' b ).over(Window .partitionBy(' a )))))
700+ checkWindowError(testData2.agg(sum(' b ), max(rank().over(Window .orderBy(' a )))))
701+ checkWindowError(testData2.groupBy(' a ).agg(sum(' b ), max(rank().over(Window .orderBy(' b )))))
702+ checkWindowError(testData2.groupBy(' a ).agg(max(sum(sum(' b )).over(Window .orderBy(' b )))))
703+
704+ checkWindowError(
705+ sql(" SELECT MAX(RANK() OVER(ORDER BY b)) FROM testData2 GROUP BY a HAVING SUM(b) = 3" ))
706+ checkWindowError(
707+ sql(" SELECT MAX(RANK() OVER(ORDER BY a)) FROM testData2" ))
708+ checkWindowError(
709+ sql(" SELECT MAX(RANK() OVER(ORDER BY b)) FROM testData2 GROUP BY a" ))
710+ checkAnswer(
711+ sql(" SELECT a, MAX(b), RANK() OVER(ORDER BY a) FROM testData2 GROUP BY a HAVING SUM(b) = 3" ),
712+ Row (1 , 2 , 1 ) :: Row (2 , 2 , 2 ) :: Row (3 , 2 , 3 ) :: Nil )
713+ }
714+
690715}
0 commit comments