From fc2c3fd136c1885ffe149523bc4bf58aaa8329f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petar=20Niki=C4=87?= Date: Wed, 15 Apr 2026 11:45:45 +0000 Subject: [PATCH] Replace legacy error condition for window frame not ordered --- .../resources/error/error-conditions.json | 11 +++++---- .../sql/errors/QueryCompilationErrors.scala | 6 +++-- .../analyzer-results/udf/udf-window.sql.out | 6 +++-- .../sql-tests/analyzer-results/window.sql.out | 21 +++++++++++++++-- .../resources/sql-tests/inputs/window.sql | 4 ++++ .../sql-tests/results/udf/udf-window.sql.out | 6 +++-- .../sql-tests/results/window.sql.out | 23 +++++++++++++++++-- .../sql/DataFrameWindowFunctionsSuite.scala | 10 ++++---- 8 files changed, 68 insertions(+), 19 deletions(-) diff --git a/common/utils/src/main/resources/error/error-conditions.json b/common/utils/src/main/resources/error/error-conditions.json index c0e86df41299a..3373fcf251b72 100644 --- a/common/utils/src/main/resources/error/error-conditions.json +++ b/common/utils/src/main/resources/error/error-conditions.json @@ -8530,6 +8530,12 @@ ], "sqlState" : "42K0E" }, + "WINDOW_FUNCTION_FRAME_NOT_ORDERED" : { + "message" : [ + "Window function requires the window to be ordered. You need to add an ORDER BY clause. For example: SELECT OVER (PARTITION BY window_partition ORDER BY window_ordering) FROM table." + ], + "sqlState" : "42601" + }, "WINDOW_FUNCTION_NOT_ALLOWED_IN_CLAUSE" : { "message" : [ "It is not allowed to use window functions inside clause." @@ -8801,11 +8807,6 @@ "Window Frame must match the required frame ." ] }, - "_LEGACY_ERROR_TEMP_1037" : { - "message" : [ - "Window function requires window to be ordered, please add ORDER BY clause. For example SELECT (value_expr) OVER (PARTITION BY window_partition ORDER BY window_ordering) from table." - ] - }, "_LEGACY_ERROR_TEMP_1039" : { "message" : [ "Multiple time/session window expressions would result in a cartesian product of rows, therefore they are currently not supported." diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala index 94c76976f6cd1..0e0bebe0d5fee 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala @@ -884,8 +884,10 @@ private[sql] object QueryCompilationErrors extends QueryErrorsBase with Compilat def windowFunctionWithWindowFrameNotOrderedError(wf: WindowFunction): Throwable = { new AnalysisException( - errorClass = "_LEGACY_ERROR_TEMP_1037", - messageParameters = Map("wf" -> wf.toString)) + errorClass = "WINDOW_FUNCTION_FRAME_NOT_ORDERED", + messageParameters = Map( + "wf_name" -> wf.prettyName, + "wf_expr" -> wf.sql)) } def multiTimeWindowExpressionsNotSupportedError(t: TreeNode[_]): Throwable = { diff --git a/sql/core/src/test/resources/sql-tests/analyzer-results/udf/udf-window.sql.out b/sql/core/src/test/resources/sql-tests/analyzer-results/udf/udf-window.sql.out index 1ceb67f58d69d..1c0fd9343ab93 100644 --- a/sql/core/src/test/resources/sql-tests/analyzer-results/udf/udf-window.sql.out +++ b/sql/core/src/test/resources/sql-tests/analyzer-results/udf/udf-window.sql.out @@ -421,9 +421,11 @@ SELECT udf(val), cate, row_number() OVER(PARTITION BY cate) FROM testData ORDER -- !query analysis org.apache.spark.sql.AnalysisException { - "errorClass" : "_LEGACY_ERROR_TEMP_1037", + "errorClass" : "WINDOW_FUNCTION_FRAME_NOT_ORDERED", + "sqlState" : "42601", "messageParameters" : { - "wf" : "row_number()" + "wf_expr" : "row_number()", + "wf_name" : "row_number" } } diff --git a/sql/core/src/test/resources/sql-tests/analyzer-results/window.sql.out b/sql/core/src/test/resources/sql-tests/analyzer-results/window.sql.out index 11240c52e9c86..c830d01c07d7b 100644 --- a/sql/core/src/test/resources/sql-tests/analyzer-results/window.sql.out +++ b/sql/core/src/test/resources/sql-tests/analyzer-results/window.sql.out @@ -616,9 +616,26 @@ SELECT val, cate, row_number() OVER(PARTITION BY cate) FROM testData ORDER BY ca -- !query analysis org.apache.spark.sql.AnalysisException { - "errorClass" : "_LEGACY_ERROR_TEMP_1037", + "errorClass" : "WINDOW_FUNCTION_FRAME_NOT_ORDERED", + "sqlState" : "42601", "messageParameters" : { - "wf" : "row_number()" + "wf_expr" : "row_number()", + "wf_name" : "row_number" + } +} + + +-- !query +SELECT lead(t) OVER () +FROM VALUES ('A'), ('B'), ('C') AS tbl(t) +-- !query analysis +org.apache.spark.sql.AnalysisException +{ + "errorClass" : "WINDOW_FUNCTION_FRAME_NOT_ORDERED", + "sqlState" : "42601", + "messageParameters" : { + "wf_expr" : "lead(tbl.t, 1, NULL)", + "wf_name" : "lead" } } diff --git a/sql/core/src/test/resources/sql-tests/inputs/window.sql b/sql/core/src/test/resources/sql-tests/inputs/window.sql index 3a453e1c80e7e..162dd6c395bd0 100644 --- a/sql/core/src/test/resources/sql-tests/inputs/window.sql +++ b/sql/core/src/test/resources/sql-tests/inputs/window.sql @@ -158,6 +158,10 @@ SELECT val, cate, avg(null) OVER(PARTITION BY cate ORDER BY val) FROM testData O -- OrderBy not specified SELECT val, cate, row_number() OVER(PARTITION BY cate) FROM testData ORDER BY cate, val; +-- OrderBy not specified for lead +SELECT lead(t) OVER () +FROM VALUES ('A'), ('B'), ('C') AS tbl(t); + -- Over clause is empty SELECT val, cate, sum(val) OVER(), avg(val) OVER() FROM testData ORDER BY cate, val; diff --git a/sql/core/src/test/resources/sql-tests/results/udf/udf-window.sql.out b/sql/core/src/test/resources/sql-tests/results/udf/udf-window.sql.out index 40e24e7b4e873..27293d5de452d 100644 --- a/sql/core/src/test/resources/sql-tests/results/udf/udf-window.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/udf/udf-window.sql.out @@ -421,9 +421,11 @@ struct<> -- !query output org.apache.spark.sql.AnalysisException { - "errorClass" : "_LEGACY_ERROR_TEMP_1037", + "errorClass" : "WINDOW_FUNCTION_FRAME_NOT_ORDERED", + "sqlState" : "42601", "messageParameters" : { - "wf" : "row_number()" + "wf_expr" : "row_number()", + "wf_name" : "row_number" } } diff --git a/sql/core/src/test/resources/sql-tests/results/window.sql.out b/sql/core/src/test/resources/sql-tests/results/window.sql.out index 3ee7673df6410..5505d43eb7357 100644 --- a/sql/core/src/test/resources/sql-tests/results/window.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/window.sql.out @@ -599,9 +599,28 @@ struct<> -- !query output org.apache.spark.sql.AnalysisException { - "errorClass" : "_LEGACY_ERROR_TEMP_1037", + "errorClass" : "WINDOW_FUNCTION_FRAME_NOT_ORDERED", + "sqlState" : "42601", "messageParameters" : { - "wf" : "row_number()" + "wf_expr" : "row_number()", + "wf_name" : "row_number" + } +} + + +-- !query +SELECT lead(t) OVER () +FROM VALUES ('A'), ('B'), ('C') AS tbl(t) +-- !query schema +struct<> +-- !query output +org.apache.spark.sql.AnalysisException +{ + "errorClass" : "WINDOW_FUNCTION_FRAME_NOT_ORDERED", + "sqlState" : "42601", + "messageParameters" : { + "wf_expr" : "lead(tbl.t, 1, NULL)", + "wf_name" : "lead" } } diff --git a/sql/core/src/test/scala/org/apache/spark/sql/DataFrameWindowFunctionsSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/DataFrameWindowFunctionsSuite.scala index 6e9f338557158..1f4e930bd4f48 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/DataFrameWindowFunctionsSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/DataFrameWindowFunctionsSuite.scala @@ -71,10 +71,12 @@ class DataFrameWindowFunctionsSuite extends QueryTest 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")) + checkError( + exception = intercept[AnalysisException]( + // Here we missed .orderBy("key")! + df.select(row_number().over(Window.partitionBy("value"))).collect()), + condition = "WINDOW_FUNCTION_FRAME_NOT_ORDERED", + parameters = Map("wf_name" -> "row_number", "wf_expr" -> "row_number()")) } test("corr, covar_pop, stddev_pop functions in specific window") {