Github user yhuai commented on a diff in the pull request:
https://github.com/apache/spark/pull/7057#discussion_r34848738
--- Diff:
sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/WindowSuite.scala
---
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.hive.execution
+
+import org.apache.spark.sql.{Row, QueryTest}
+import org.apache.spark.sql.expressions.Window
+import org.apache.spark.sql.functions._
+import org.apache.spark.sql.hive.test.TestHive.implicits._
+
+/**
+ * Window expressions are tested extensively by the following test suites:
+ * [[org.apache.spark.sql.hive.HiveDataFrameWindowSuite]]
+ *
[[org.apache.spark.sql.hive.execution.HiveWindowFunctionQueryWithoutCodeGenSuite]]
+ *
[[org.apache.spark.sql.hive.execution.HiveWindowFunctionQueryFileWithoutCodeGenSuite]]
+ * However these suites do not cover all possible (i.e. more exotic)
settings. This suite fill
+ * this gap.
+ *
+ * TODO Move this class to the sql/core project when we move to Native
Spark UDAFs.
+ */
+class WindowSuite extends QueryTest {
+
+ test("reverse sliding range frame") {
+ val df = Seq(
+ (1, "Thin", "Cell Phone", 6000),
+ (2, "Normal", "Tablet", 1500),
+ (3, "Mini", "Tablet", 5500),
+ (4, "Ultra thin", "Cell Phone", 5500),
+ (5, "Very thin", "Cell Phone", 6000),
+ (6, "Big", "Tablet", 2500),
+ (7, "Bendable", "Cell Phone", 3000),
+ (8, "Foldable", "Cell Phone", 3000),
+ (9, "Pro", "Tablet", 4500),
+ (10, "Pro2", "Tablet", 6500)).
+ toDF("id", "product", "category", "revenue")
+ checkAnswer(
+ df.select(
+ $"id",
+ avg($"revenue").over(Window.
+ partitionBy($"category").
+ orderBy($"revenue".desc).
+ rangeBetween(-2000L, 1000L)).
+ cast("int")),
+ Row(1, 5833) :: Row(2, 2000) :: Row(3, 5500) ::
+ Row(4, 5833) :: Row(5, 5833) :: Row(6, 2000) ::
+ Row(7, 3000) :: Row(8, 3000) :: Row(9, 4166) ::
+ Row(10, 5500) :: Nil)
--- End diff --
Actually, this result seems not correct. For example, when we process `(10,
"Pro2", "Tablet", 6500)`. The range of revenue for the frame will be [5500,
8500]. Since the ordering direction is `desc`, `2000 preceding` means `8500`
and `1000 following` means `5500`. So, the avg value will be `6000`.
The result I got with Spark 1.4 and Hive is.
```
+--+----------+----------+-------+----+
|id| product| category|revenue| avg|
+--+----------+----------+-------+----+
|10| Pro2| Tablet| 6500|6000|
| 3| Mini| Tablet| 5500|5500|
| 9| Pro| Tablet| 4500|5500|
| 6| Big| Tablet| 2500|2833|
| 2| Normal| Tablet| 1500|2000|
| 1| Thin|Cell Phone| 6000|5833|
| 5| Very thin|Cell Phone| 6000|5833|
| 4|Ultra thin|Cell Phone| 5500|5833|
| 7| Bendable|Cell Phone| 3000|3000|
| 8| Foldable|Cell Phone| 3000|3000|
+--+----------+----------+-------+----+
```
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]