Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions common/utils/src/main/resources/error/error-conditions.json
Original file line number Diff line number Diff line change
Expand Up @@ -8530,6 +8530,12 @@
],
"sqlState" : "42K0E"
},
"WINDOW_FUNCTION_FRAME_NOT_ORDERED" : {
"message" : [
"Window function <wf_name> requires the window to be ordered. You need to add an ORDER BY clause. For example: SELECT <wf_expr> 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 <clauseName> clause."
Expand Down Expand Up @@ -8801,11 +8807,6 @@
"Window Frame <wf> must match the required frame <required>."
]
},
"_LEGACY_ERROR_TEMP_1037" : {
"message" : [
"Window function <wf> requires window to be ordered, please add ORDER BY clause. For example SELECT <wf>(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."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

Expand Down
4 changes: 4 additions & 0 deletions sql/core/src/test/resources/sql-tests/inputs/window.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

Expand Down
23 changes: 21 additions & 2 deletions sql/core/src/test/resources/sql-tests/results/window.sql.out
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down