Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ namedWindow

windowSpec
: name=identifier #windowRef
| '('name=identifier')' #windowRef
| '('
( CLUSTER BY partition+=expression (',' partition+=expression)*
| ((PARTITION | DISTRIBUTE) BY partition+=expression (',' partition+=expression)*)?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.apache.spark.sql.{AnalysisException, QueryTest, Row}
import org.apache.spark.sql.test.SharedSQLContext

case class WindowData(month: Int, area: String, product: Int)
case class EmpSalary(empno: Int, depname: String, salary: Double)


/**
Expand All @@ -31,6 +32,19 @@ class SQLWindowFunctionSuite extends QueryTest with SharedSQLContext {

import testImplicits._

val empSalaryData = Seq(

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The table and data here is from: https://www.postgresql.org/docs/9.1/tutorial-window.html

The naming and data is easier to be understood. In case we need to compare more behavior, we can use this one as alternative.
I can use the WindowData and remove this as well.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we just add a new test in window.sql?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We really just need a simple test that proves over (w) works.

EmpSalary(11, "develop", 5200D),
EmpSalary(7, "develop", 4200D),
EmpSalary(9, "develop", 4500D),
EmpSalary(8, "develop", 6000D),
EmpSalary(10, "develop", 5200D),
EmpSalary(5, "personnel", 3500D),
EmpSalary(2, "personnel", 3900D),
EmpSalary(3, "sales", 4800D),
EmpSalary(1, "sales", 5000D),
EmpSalary(4, "sales", 4800D)
)

test("window function: udaf with aggregate expression") {
val data = Seq(
WindowData(1, "a", 5),
Expand Down Expand Up @@ -173,6 +187,30 @@ class SQLWindowFunctionSuite extends QueryTest with SharedSQLContext {
).map(i => Row(i._1, i._2, i._3, i._4)))
}

test("window function: allow parentheses around window reference") {
sparkContext.parallelize(empSalaryData).toDF().createOrReplaceTempView("empsalary")

checkAnswer(
sql(
"""
|SELECT sum(salary) OVER (w), avg(salary) OVER w
|FROM empsalary
|WINDOW w AS (PARTITION BY depname ORDER BY salary DESC)
""".stripMargin),
Seq(
(6000, 6000),
(16400, 5466.6666666666666667D),
(16400, 5466.6666666666666667),
(20900, 5225),
(25100, 5020),
(3900, 3900),
(7400, 3700),
(5000, 5000),
(14600, 4866.6666666666666667),
(14600, 4866.6666666666666667)
).map(i => Row(i._1, i._2)))
}

test("window function: distinct should not be silently ignored") {
val data = Seq(
WindowData(1, "a", 5),
Expand Down