diff options
author | Sean Zhong <seanzhong@databricks.com> | 2016-05-26 14:50:00 -0700 |
---|---|---|
committer | Reynold Xin <rxin@databricks.com> | 2016-05-26 14:50:00 -0700 |
commit | b5859e0bb8cc147858cb28d8bdb5ca3b4a2cec77 (patch) | |
tree | 21dca103136153a2d53e41221d26b61678c03a53 /sql | |
parent | b0a03feef2cf4daa7642ec7f4dc479dbd473b581 (diff) | |
download | spark-b5859e0bb8cc147858cb28d8bdb5ca3b4a2cec77.tar.gz spark-b5859e0bb8cc147858cb28d8bdb5ca3b4a2cec77.tar.bz2 spark-b5859e0bb8cc147858cb28d8bdb5ca3b4a2cec77.zip |
[SPARK-13445][SQL] Improves error message and add test coverage for Window function
## What changes were proposed in this pull request?
Add more verbose error message when order by clause is missed when using Window function.
## How was this patch tested?
Unit test.
Author: Sean Zhong <seanzhong@databricks.com>
Closes #13333 from clockfly/spark-13445.
Diffstat (limited to 'sql')
-rw-r--r-- | sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala | 4 | ||||
-rw-r--r-- | sql/core/src/test/scala/org/apache/spark/sql/DataFrameWindowSuite.scala | 8 |
2 files changed, 11 insertions, 1 deletions
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala index 9a92330f75..bf221e0d7c 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala @@ -1795,7 +1795,9 @@ class Analyzer( def apply(plan: LogicalPlan): LogicalPlan = plan transform { case logical: LogicalPlan => logical transformExpressions { case WindowExpression(wf: WindowFunction, spec) if spec.orderSpec.isEmpty => - failAnalysis(s"WindowFunction $wf requires window to be ordered") + failAnalysis(s"Window function $wf requires window to be ordered, please add ORDER BY " + + s"clause. For example SELECT $wf(value_expr) OVER (PARTITION BY window_partition " + + s"ORDER BY window_ordering) from table") case WindowExpression(rank: RankLike, spec) if spec.resolved => val order = spec.orderSpec.map(_.child) WindowExpression(rank.withOrder(order), spec) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/DataFrameWindowSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/DataFrameWindowSuite.scala index 07aad3c406..9a1aa46947 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/DataFrameWindowSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/DataFrameWindowSuite.scala @@ -110,6 +110,14 @@ class DataFrameWindowSuite extends QueryTest with SharedSQLContext { Row(2, 2, 1, 5.0d / 3.0d, 3, 5, 2, 3, 2, 2, 1.0d, 0.5d) :: Nil) } + test("window function should fail if order by clause is not specified") { + val df = Seq((1, "1"), (2, "2"), (1, "2"), (2, "2")).toDF("key", "value") + val e = intercept[AnalysisException]( + // Here we missed .orderBy("key")! + df.select(row_number().over(Window.partitionBy("value"))).collect()) + assert(e.message.contains("requires window to be ordered")) + } + test("aggregation and rows between") { val df = Seq((1, "1"), (2, "1"), (2, "2"), (1, "1"), (2, "2")).toDF("key", "value") df.createOrReplaceTempView("window_table") |