Skip to content

Commit

Permalink
[SPARK-15122] [SQL] Fix TPC-DS 41 - Normalize predicates before pulli…
Browse files Browse the repository at this point in the history
…ng them out

## What changes were proposed in this pull request?
The official TPC-DS 41 query currently fails because it contains a scalar subquery with a disjunctive correlated predicate (the correlated predicates were nested in ORs). This makes the `Analyzer` pull out the entire predicate which is wrong and causes the following (correct) analysis exception: `The correlated scalar subquery can only contain equality predicates`

This PR fixes this by first simplifing (or normalizing) the correlated predicates before pulling them out of the subquery.

## How was this patch tested?
Manual testing on TPC-DS 41, and added a test to SubquerySuite.

Author: Herman van Hovell <[email protected]>

Closes #12954 from hvanhovell/SPARK-15122.

(cherry picked from commit df89f1d)
Signed-off-by: Davies Liu <[email protected]>
  • Loading branch information
hvanhovell authored and davies committed May 7, 2016
1 parent 4ccc564 commit 49e6661
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.apache.spark.sql.catalyst.catalog.{InMemoryCatalog, SessionCatalog}
import org.apache.spark.sql.catalyst.encoders.OuterScopes
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.expressions.aggregate._
import org.apache.spark.sql.catalyst.optimizer.BooleanSimplification
import org.apache.spark.sql.catalyst.planning.IntegerIndex
import org.apache.spark.sql.catalyst.plans._
import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, _}
Expand Down Expand Up @@ -958,7 +959,8 @@ class Analyzer(
localPredicateReferences -- p.outputSet
}

val transformed = sub transformUp {
// Simplify the predicates before pulling them out.
val transformed = BooleanSimplification(sub) transformUp {
case f @ Filter(cond, child) =>
// Find all predicates with an outer reference.
val (correlated, local) = splitConjunctivePredicates(cond).partition(containsOuter)
Expand Down
12 changes: 12 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/SubquerySuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,16 @@ class SubquerySuite extends QueryTest with SharedSQLContext {
assert(msg1.getMessage.contains(
"The correlated scalar subquery can only contain equality predicates"))
}

test("disjunctive correlated scalar subquery") {
checkAnswer(
sql("""
|select a
|from l
|where (select count(*)
| from r
| where (a = c and d = 2.0) or (a = c and d = 1.0)) > 0
""".stripMargin),
Row(3) :: Nil)
}
}

0 comments on commit 49e6661

Please sign in to comment.