[GitHub] spark pull request #18634: [SPARK-21414] Refine SlidingWindowFunctionFrame t...

2017-07-19 Thread asfgit
Github user asfgit closed the pull request at:

https://github.com/apache/spark/pull/18634


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

-
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org



[GitHub] spark pull request #18634: [SPARK-21414] Refine SlidingWindowFunctionFrame t...

2017-07-19 Thread cloud-fan
Github user cloud-fan commented on a diff in the pull request:

https://github.com/apache/spark/pull/18634#discussion_r128228043
  
--- Diff: 
sql/core/src/test/scala/org/apache/spark/sql/execution/SQLWindowFunctionSuite.scala
 ---
@@ -359,37 +359,41 @@ class SQLWindowFunctionSuite extends QueryTest with 
SharedSQLContext {
   test("window function: mutiple window expressions specified by range in 
a single expression") {
 val nums = sparkContext.parallelize(1 to 10).map(x => (x, x % 
2)).toDF("x", "y")
 nums.createOrReplaceTempView("nums")
-
-val expected =
-  Row(1, 1, 1, 4, 25) ::
-Row(1, 3, 4, 9, 24) ::
-Row(1, 5, 9, 15, 21) ::
-Row(1, 7, 16, 21, 16) ::
-Row(1, 9, 25, 16, 9) ::
-Row(0, 2, 2, 6, 30) ::
-Row(0, 4, 6, 12, 28) ::
-Row(0, 6, 12, 18, 24) ::
-Row(0, 8, 20, 24, 18) ::
-Row(0, 10, 30, 18, 10) ::
-Nil
-
-val actual = sql(
-  """
-|SELECT
-|  y,
-|  x,
-|  sum(x) over w1 as history_sum,
-|  sum(x) over w2 as period_sum,
-|  sum(x) over w3 as future_sum
-|FROM nums
-|WINDOW w1 AS (PARTITION BY y ORDER BY x RANGE BETWEEN UNBOUNDED 
PRECEDING AND CURRENT ROW),
-|   w2 AS (PARTITION BY y ORDER BY x RANGE BETWEEN 2 PRECEDING 
AND 2 FOLLOWING),
-|   w3 AS (PARTITION BY y ORDER BY x RANGE BETWEEN CURRENT ROW 
AND UNBOUNDED FOLLOWING)
-  """.stripMargin
-)
-
-checkAnswer(actual, expected)
-spark.catalog.dropTempView("nums")
+withTempView("nums") {
+  val expected =
+Row(1, 1, 1, 4, null, 8, 25) ::
--- End diff --

null is better, which matches the behavior in Aggregate.


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

-
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org



[GitHub] spark pull request #18634: [SPARK-21414] Refine SlidingWindowFunctionFrame t...

2017-07-19 Thread jinxing64
Github user jinxing64 commented on a diff in the pull request:

https://github.com/apache/spark/pull/18634#discussion_r128227194
  
--- Diff: 
sql/core/src/test/scala/org/apache/spark/sql/execution/SQLWindowFunctionSuite.scala
 ---
@@ -356,6 +356,46 @@ class SQLWindowFunctionSuite extends QueryTest with 
SharedSQLContext {
 spark.catalog.dropTempView("nums")
   }
 
+  test("window function: mutiple window expressions specified by range in 
a single expression") {
+val nums = sparkContext.parallelize(1 to 10).map(x => (x, x % 
2)).toDF("x", "y")
+nums.createOrReplaceTempView("nums")
+withTempView("nums") {
+  val expected =
+Row(1, 1, 1, 4, null, 8, 25) ::
+  Row(1, 3, 4, 9, 1, 12, 24) ::
+  Row(1, 5, 9, 15, 4, 16, 21) ::
+  Row(1, 7, 16, 21, 8, 9, 16) ::
+  Row(1, 9, 25, 16, 12, null, 9) ::
+  Row(0, 2, 2, 6, null, 10, 30) ::
+  Row(0, 4, 6, 12, 2, 14, 28) ::
+  Row(0, 6, 12, 18, 6, 18, 24) ::
+  Row(0, 8, 20, 24, 10, 10, 18) ::
+  Row(0, 10, 30, 18, 14, null, 10) ::
--- End diff --

`expected` is calculated manually. This test is to verify there is no 
behavior change.


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

-
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org



[GitHub] spark pull request #18634: [SPARK-21414] Refine SlidingWindowFunctionFrame t...

2017-07-19 Thread cloud-fan
Github user cloud-fan commented on a diff in the pull request:

https://github.com/apache/spark/pull/18634#discussion_r128220833
  
--- Diff: 
sql/core/src/test/scala/org/apache/spark/sql/execution/SQLWindowFunctionSuite.scala
 ---
@@ -356,6 +356,46 @@ class SQLWindowFunctionSuite extends QueryTest with 
SharedSQLContext {
 spark.catalog.dropTempView("nums")
   }
 
+  test("window function: mutiple window expressions specified by range in 
a single expression") {
+val nums = sparkContext.parallelize(1 to 10).map(x => (x, x % 
2)).toDF("x", "y")
+nums.createOrReplaceTempView("nums")
+withTempView("nums") {
+  val expected =
+Row(1, 1, 1, 4, null, 8, 25) ::
+  Row(1, 3, 4, 9, 1, 12, 24) ::
+  Row(1, 5, 9, 15, 4, 16, 21) ::
+  Row(1, 7, 16, 21, 8, 9, 16) ::
+  Row(1, 9, 25, 16, 12, null, 9) ::
+  Row(0, 2, 2, 6, null, 10, 30) ::
+  Row(0, 4, 6, 12, 2, 14, 28) ::
+  Row(0, 6, 12, 18, 6, 18, 24) ::
+  Row(0, 8, 20, 24, 10, 10, 18) ::
+  Row(0, 10, 30, 18, 14, null, 10) ::
--- End diff --

BTW, please make sure there is no behavior change, i.e. the result should 
be same with or without this PR.


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

-
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org



[GitHub] spark pull request #18634: [SPARK-21414] Refine SlidingWindowFunctionFrame t...

2017-07-19 Thread jinxing64
Github user jinxing64 commented on a diff in the pull request:

https://github.com/apache/spark/pull/18634#discussion_r128215724
  
--- Diff: 
sql/core/src/test/scala/org/apache/spark/sql/execution/SQLWindowFunctionSuite.scala
 ---
@@ -359,37 +359,41 @@ class SQLWindowFunctionSuite extends QueryTest with 
SharedSQLContext {
   test("window function: mutiple window expressions specified by range in 
a single expression") {
 val nums = sparkContext.parallelize(1 to 10).map(x => (x, x % 
2)).toDF("x", "y")
 nums.createOrReplaceTempView("nums")
-
-val expected =
-  Row(1, 1, 1, 4, 25) ::
-Row(1, 3, 4, 9, 24) ::
-Row(1, 5, 9, 15, 21) ::
-Row(1, 7, 16, 21, 16) ::
-Row(1, 9, 25, 16, 9) ::
-Row(0, 2, 2, 6, 30) ::
-Row(0, 4, 6, 12, 28) ::
-Row(0, 6, 12, 18, 24) ::
-Row(0, 8, 20, 24, 18) ::
-Row(0, 10, 30, 18, 10) ::
-Nil
-
-val actual = sql(
-  """
-|SELECT
-|  y,
-|  x,
-|  sum(x) over w1 as history_sum,
-|  sum(x) over w2 as period_sum,
-|  sum(x) over w3 as future_sum
-|FROM nums
-|WINDOW w1 AS (PARTITION BY y ORDER BY x RANGE BETWEEN UNBOUNDED 
PRECEDING AND CURRENT ROW),
-|   w2 AS (PARTITION BY y ORDER BY x RANGE BETWEEN 2 PRECEDING 
AND 2 FOLLOWING),
-|   w3 AS (PARTITION BY y ORDER BY x RANGE BETWEEN CURRENT ROW 
AND UNBOUNDED FOLLOWING)
-  """.stripMargin
-)
-
-checkAnswer(actual, expected)
-spark.catalog.dropTempView("nums")
+withTempView("nums") {
+  val expected =
+Row(1, 1, 1, 4, null, 8, 25) ::
--- End diff --

@cloud-fan 
This is null and do you think 0 is better?


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

-
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org



[GitHub] spark pull request #18634: [SPARK-21414] Refine SlidingWindowFunctionFrame t...

2017-07-18 Thread jinxing64
Github user jinxing64 commented on a diff in the pull request:

https://github.com/apache/spark/pull/18634#discussion_r128142152
  
--- Diff: 
sql/core/src/test/scala/org/apache/spark/sql/execution/SQLWindowFunctionSuite.scala
 ---
@@ -356,6 +356,42 @@ class SQLWindowFunctionSuite extends QueryTest with 
SharedSQLContext {
 spark.catalog.dropTempView("nums")
   }
 
+  test("window function: mutiple window expressions specified by range in 
a single expression") {
+val nums = sparkContext.parallelize(1 to 10).map(x => (x, x % 
2)).toDF("x", "y")
+nums.createOrReplaceTempView("nums")
--- End diff --

Sure, I will add it later today.


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

-
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org



[GitHub] spark pull request #18634: [SPARK-21414] Refine SlidingWindowFunctionFrame t...

2017-07-17 Thread jiangxb1987
Github user jiangxb1987 commented on a diff in the pull request:

https://github.com/apache/spark/pull/18634#discussion_r127882623
  
--- Diff: 
sql/core/src/test/scala/org/apache/spark/sql/execution/SQLWindowFunctionSuite.scala
 ---
@@ -356,6 +356,42 @@ class SQLWindowFunctionSuite extends QueryTest with 
SharedSQLContext {
 spark.catalog.dropTempView("nums")
   }
 
+  test("window function: mutiple window expressions specified by range in 
a single expression") {
+val nums = sparkContext.parallelize(1 to 10).map(x => (x, x % 
2)).toDF("x", "y")
+nums.createOrReplaceTempView("nums")
--- End diff --

And this test case doesn't cover when CurrentRow is not in the window 
frame. We'd better add that senario.


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

-
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org



[GitHub] spark pull request #18634: [SPARK-21414] Refine SlidingWindowFunctionFrame t...

2017-07-17 Thread cloud-fan
Github user cloud-fan commented on a diff in the pull request:

https://github.com/apache/spark/pull/18634#discussion_r127882430
  
--- Diff: 
sql/core/src/test/scala/org/apache/spark/sql/execution/SQLWindowFunctionSuite.scala
 ---
@@ -356,6 +356,42 @@ class SQLWindowFunctionSuite extends QueryTest with 
SharedSQLContext {
 spark.catalog.dropTempView("nums")
   }
 
+  test("window function: mutiple window expressions specified by range in 
a single expression") {
+val nums = sparkContext.parallelize(1 to 10).map(x => (x, x % 
2)).toDF("x", "y")
+nums.createOrReplaceTempView("nums")
--- End diff --

BTW this test is not very related to this PR, just adds test coverage for 
range window frame.


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

-
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org



[GitHub] spark pull request #18634: [SPARK-21414] Refine SlidingWindowFunctionFrame t...

2017-07-17 Thread cloud-fan
Github user cloud-fan commented on a diff in the pull request:

https://github.com/apache/spark/pull/18634#discussion_r127882358
  
--- Diff: 
sql/core/src/test/scala/org/apache/spark/sql/execution/SQLWindowFunctionSuite.scala
 ---
@@ -356,6 +356,42 @@ class SQLWindowFunctionSuite extends QueryTest with 
SharedSQLContext {
 spark.catalog.dropTempView("nums")
   }
 
+  test("window function: mutiple window expressions specified by range in 
a single expression") {
+val nums = sparkContext.parallelize(1 to 10).map(x => (x, x % 
2)).toDF("x", "y")
+nums.createOrReplaceTempView("nums")
--- End diff --

wrap your test with `withTempView`, which can drop the view automatically.


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

-
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org



[GitHub] spark pull request #18634: [SPARK-21414] Refine SlidingWindowFunctionFrame t...

2017-07-17 Thread jiangxb1987
Github user jiangxb1987 commented on a diff in the pull request:

https://github.com/apache/spark/pull/18634#discussion_r127696829
  
--- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/window/WindowFunctionFrame.scala
 ---
@@ -212,6 +203,21 @@ private[window] final class SlidingWindowFunctionFrame(
   bufferUpdated = true
 }
 
+// Add all rows to the buffer for which the input row value is equal 
to or less than
+// the output row upper bound.
+while (nextRow != null && ubound.compare(nextRow, inputHighIndex, 
current, index) <= 0) {
--- End diff --

How about:
```
 while (nextRow != null && ubound.compare(nextRow, inputHighIndex, 
current, index) <= 0) {
   if (lbound.compare(nextRow, inputLowIndex, current, index) < 0) {
 inputLowIndex += 1
   } else {
 buffer.add(nextRow.copy())
 bufferUpdated = true
   }
   nextRow = WindowFunctionFrame.getNextOrNull(inputIterator)
   inputHighIndex += 1
 }
```
?


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

-
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org



[GitHub] spark pull request #18634: [SPARK-21414] Refine SlidingWindowFunctionFrame t...

2017-07-14 Thread jinxing64
GitHub user jinxing64 opened a pull request:

https://github.com/apache/spark/pull/18634

[SPARK-21414] Refine SlidingWindowFunctionFrame to avoid OOM.

## What changes were proposed in this pull request?

In `SlidingWindowFunctionFrame`, it is now adding all rows to the buffer 
for which the input row value is equal to or less than the output row upper 
bound, then drop all rows from the buffer for which the input row value is 
smaller than the output row lower bound.
This could result in the buffer is very big though the window is small.
For example: 
```
select a, b, sum(a) 
over (partition by b order by a range between 100 following and 101 
following) 
from table
```
We can refine the logic and just add the qualified rows into buffer.

## How was this patch tested?
Added test.


You can merge this pull request into a Git repository by running:

$ git pull https://github.com/jinxing64/spark SPARK-21414

Alternatively you can review and apply these changes as the patch at:

https://github.com/apache/spark/pull/18634.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

This closes #18634


commit 5103ae80d456ca26c7b853f99e73c4a2c152ad41
Author: jinxing 
Date:   2017-07-12T12:02:28Z

Refine SlidingWindowFunctionFrame to avoid OOM.




---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

-
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org