[GitHub] spark pull request #17331: [SPARK-19994][SQL] Wrong outputOrdering for right...

2017-03-20 Thread asfgit
Github user asfgit closed the pull request at:

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


---
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 #17331: [SPARK-19994][SQL] Wrong outputOrdering for right...

2017-03-18 Thread gatorsmile
Github user gatorsmile commented on a diff in the pull request:

https://github.com/apache/spark/pull/17331#discussion_r106798699
  
--- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/joins/SortMergeJoinExec.scala
 ---
@@ -80,7 +80,18 @@ case class SortMergeJoinExec(
   override def requiredChildDistribution: Seq[Distribution] =
 ClusteredDistribution(leftKeys) :: ClusteredDistribution(rightKeys) :: 
Nil
 
-  override def outputOrdering: Seq[SortOrder] = requiredOrders(leftKeys)
+  override def outputOrdering: Seq[SortOrder] = joinType match {
+case RightOuter =>
+  // For right outer join, values of the left key will be filled with 
nulls if it can't
+  // match the value of the right key, so `nullOrdering` of the left 
key can't be guaranteed.
+  // We should output right key order here.
+  requiredOrders(rightKeys)
+case FullOuter =>
+  // Neither left key nor right key guarantees `nullOrdering` after 
full outer join.
+  Nil
+case _ =>
--- End diff --

If possible, please use the white list. Otherwise, we might forget to 
update this when adding new join types. Then, we should issue the exception for 
the default case. 


---
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 #17331: [SPARK-19994][SQL] Wrong outputOrdering for right...

2017-03-18 Thread wzhfy
Github user wzhfy commented on a diff in the pull request:

https://github.com/apache/spark/pull/17331#discussion_r106797733
  
--- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/joins/SortMergeJoinExec.scala
 ---
@@ -80,7 +80,18 @@ case class SortMergeJoinExec(
   override def requiredChildDistribution: Seq[Distribution] =
 ClusteredDistribution(leftKeys) :: ClusteredDistribution(rightKeys) :: 
Nil
 
-  override def outputOrdering: Seq[SortOrder] = requiredOrders(leftKeys)
+  override def outputOrdering: Seq[SortOrder] = joinType match {
+case RightOuter =>
+  // For right outer join, values of the left key will be filled with 
nulls if it can't
+  // match the value of the right key, so `nullOrdering` of the left 
key can't be guaranteed.
+  // We should output right key order here.
--- End diff --

OK I'll use those comments in the original 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 #17331: [SPARK-19994][SQL] Wrong outputOrdering for right...

2017-03-18 Thread gatorsmile
Github user gatorsmile commented on a diff in the pull request:

https://github.com/apache/spark/pull/17331#discussion_r106797512
  
--- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/joins/SortMergeJoinExec.scala
 ---
@@ -80,7 +80,18 @@ case class SortMergeJoinExec(
   override def requiredChildDistribution: Seq[Distribution] =
 ClusteredDistribution(leftKeys) :: ClusteredDistribution(rightKeys) :: 
Nil
 
-  override def outputOrdering: Seq[SortOrder] = requiredOrders(leftKeys)
+  override def outputOrdering: Seq[SortOrder] = joinType match {
+case RightOuter =>
+  // For right outer join, values of the left key will be filled with 
nulls if it can't
+  // match the value of the right key, so `nullOrdering` of the left 
key can't be guaranteed.
+  // We should output right key order here.
--- End diff --

> // For left and right outer joins, the output is ordered by the streamed 
input's join keys.


---
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 #17331: [SPARK-19994][SQL] Wrong outputOrdering for right...

2017-03-18 Thread gatorsmile
Github user gatorsmile commented on a diff in the pull request:

https://github.com/apache/spark/pull/17331#discussion_r106797457
  
--- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/joins/SortMergeJoinExec.scala
 ---
@@ -80,7 +80,18 @@ case class SortMergeJoinExec(
   override def requiredChildDistribution: Seq[Distribution] =
 ClusteredDistribution(leftKeys) :: ClusteredDistribution(rightKeys) :: 
Nil
 
-  override def outputOrdering: Seq[SortOrder] = requiredOrders(leftKeys)
+  override def outputOrdering: Seq[SortOrder] = joinType match {
+case RightOuter =>
+  // For right outer join, values of the left key will be filled with 
nulls if it can't
+  // match the value of the right key, so `nullOrdering` of the left 
key can't be guaranteed.
+  // We should output right key order here.
--- End diff --

This comment is misleading. The output ordering is mainly affected by how 
we implement SortMergeJoinExec. 


---
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 #17331: [SPARK-19994][SQL] Wrong outputOrdering for right...

2017-03-18 Thread gatorsmile
Github user gatorsmile commented on a diff in the pull request:

https://github.com/apache/spark/pull/17331#discussion_r106797210
  
--- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/joins/SortMergeJoinExec.scala
 ---
@@ -80,7 +80,18 @@ case class SortMergeJoinExec(
   override def requiredChildDistribution: Seq[Distribution] =
 ClusteredDistribution(leftKeys) :: ClusteredDistribution(rightKeys) :: 
Nil
 
-  override def outputOrdering: Seq[SortOrder] = requiredOrders(leftKeys)
+  override def outputOrdering: Seq[SortOrder] = joinType match {
+case RightOuter =>
+  // For right outer join, values of the left key will be filled with 
nulls if it can't
+  // match the value of the right key, so `nullOrdering` of the left 
key can't be guaranteed.
+  // We should output right key order here.
+  requiredOrders(rightKeys)
--- End diff --

This is output ordering, right? The join result will be returned based on 
right keys?


---
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 #17331: [SPARK-19994][SQL] Wrong outputOrdering for right...

2017-03-18 Thread wzhfy
Github user wzhfy commented on a diff in the pull request:

https://github.com/apache/spark/pull/17331#discussion_r106782184
  
--- Diff: 
sql/core/src/test/scala/org/apache/spark/sql/execution/PlannerSuite.scala ---
@@ -557,6 +470,130 @@ class PlannerSuite extends SharedSQLContext {
   fail(s"Should have only two shuffles:\n$outputPlan")
 }
   }
+
+  
///
+  // Unit tests of EnsureRequirements for Sort
+  
///
+
+  private val exprA = Literal(1)
+  private val exprB = Literal(2)
+  private val orderingA = SortOrder(exprA, Ascending)
+  private val orderingB = SortOrder(exprB, Ascending)
+  private val planA = DummySparkPlan(outputOrdering = Seq(orderingA),
+outputPartitioning = HashPartitioning(exprA :: Nil, 5))
+  private val planB = DummySparkPlan(outputOrdering = Seq(orderingB),
+outputPartitioning = HashPartitioning(exprB :: Nil, 5))
+
+  assert(orderingA != orderingB)
+
+  private def assertSortRequirementsAreSatisfied(
+  childPlan: SparkPlan,
+  requiredOrdering: Seq[SortOrder],
+  shouldHaveSort: Boolean): Unit = {
+val inputPlan = DummySparkPlan(
+  children = childPlan :: Nil,
+  requiredChildOrdering = Seq(requiredOrdering),
+  requiredChildDistribution = Seq(UnspecifiedDistribution)
+)
+val outputPlan = 
EnsureRequirements(spark.sessionState.conf).apply(inputPlan)
+assertDistributionRequirementsAreSatisfied(outputPlan)
+if (shouldHaveSort) {
+  if (outputPlan.collect { case s: SortExec => true }.isEmpty) {
+fail(s"Sort should have been added:\n$outputPlan")
+  }
+} else {
+  if (outputPlan.collect { case s: SortExec => true }.nonEmpty) {
+fail(s"No sorts should have been added:\n$outputPlan")
+  }
+}
+  }
+
+  test("EnsureRequirements for sort operator after left outer sort merge 
join") {
--- End diff --

I added three test cases for left/right/full outer join. For other tests, I 
moved tests about sort together and extract common logic to a method and some 
private fields.


---
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 #17331: [SPARK-19994][SQL] Wrong outputOrdering for right...

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

https://github.com/apache/spark/pull/17331#discussion_r106775256
  
--- Diff: 
sql/core/src/test/scala/org/apache/spark/sql/execution/PlannerSuite.scala ---
@@ -557,6 +470,130 @@ class PlannerSuite extends SharedSQLContext {
   fail(s"Should have only two shuffles:\n$outputPlan")
 }
   }
+
+  
///
+  // Unit tests of EnsureRequirements for Sort
+  
///
+
+  private val exprA = Literal(1)
+  private val exprB = Literal(2)
+  private val orderingA = SortOrder(exprA, Ascending)
+  private val orderingB = SortOrder(exprB, Ascending)
+  private val planA = DummySparkPlan(outputOrdering = Seq(orderingA),
+outputPartitioning = HashPartitioning(exprA :: Nil, 5))
+  private val planB = DummySparkPlan(outputOrdering = Seq(orderingB),
+outputPartitioning = HashPartitioning(exprB :: Nil, 5))
+
+  assert(orderingA != orderingB)
+
+  private def assertSortRequirementsAreSatisfied(
+  childPlan: SparkPlan,
+  requiredOrdering: Seq[SortOrder],
+  shouldHaveSort: Boolean): Unit = {
+val inputPlan = DummySparkPlan(
+  children = childPlan :: Nil,
+  requiredChildOrdering = Seq(requiredOrdering),
+  requiredChildDistribution = Seq(UnspecifiedDistribution)
+)
+val outputPlan = 
EnsureRequirements(spark.sessionState.conf).apply(inputPlan)
+assertDistributionRequirementsAreSatisfied(outputPlan)
+if (shouldHaveSort) {
+  if (outputPlan.collect { case s: SortExec => true }.isEmpty) {
+fail(s"Sort should have been added:\n$outputPlan")
+  }
+} else {
+  if (outputPlan.collect { case s: SortExec => true }.nonEmpty) {
+fail(s"No sorts should have been added:\n$outputPlan")
+  }
+}
+  }
+
+  test("EnsureRequirements for sort operator after left outer sort merge 
join") {
--- End diff --

are we just moving test around?


---
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 #17331: [SPARK-19994][SQL] Wrong outputOrdering for right...

2017-03-17 Thread wzhfy
GitHub user wzhfy opened a pull request:

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

[SPARK-19994][SQL] Wrong outputOrdering for right/full outer smj

## What changes were proposed in this pull request?

For right outer join, values of the left key will be filled with nulls if 
it can't match the value of the right key, so `nullOrdering` of the left key 
can't be guaranteed. We should output right key order.

For full outer join, neither left key nor right key guarantees 
`nullOrdering`. We should not output any ordering.

This patch also reorganizes code in `PlannerSuite` by separating tests for 
`Exchange` and `Sort`, and also extracts common logic in Sort tests into a 
method.

## How was this patch tested?

Corresponding test cases added.


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

$ git pull https://github.com/wzhfy/spark wrongOrdering

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

https://github.com/apache/spark/pull/17331.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 #17331


commit 8dc2c046d32df84626470303ea97180a1091684b
Author: wangzhenhua 
Date:   2017-03-17T06:58:10Z

wrong outputOrdering for right/full outer smj




---
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