Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-22548][SQL] Incorrect nested AND expression pushed down to JDBC data source #19776

Closed
wants to merge 10 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,19 @@ object DataSourceStrategy {
Some(sources.IsNotNull(a.name))

case expressions.And(left, right) =>
(translateFilter(left) ++ translateFilter(right)).reduceOption(sources.And)
// See SPARK-12218 for detailed discussion
// It is not safe to just convert one side if we do not understand the
// other side. Here is an example used to explain the reason.
// Let's say we have (a = 2 AND trim(b) = 'blah') OR (c > 0)
// and we do not understand how to convert trim(b) = 'blah'.
// If we only convert a = 2, we will end up with
// (a = 2) OR (c > 0), which will generate wrong results.
// Pushing one leg of AND down is only safe to do at the top level.
// You can see ParquetFilters' createFilter for more details.
for {
Copy link
Member

Choose a reason for hiding this comment

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

Let's add a small comment like the PR you pointed out.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure. Will do. Thanks.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. Just did that as you suggested.

leftFilter <- translateFilter(left)
rightFilter <- translateFilter(right)
} yield sources.And(leftFilter, rightFilter)
Copy link
Contributor

Choose a reason for hiding this comment

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

do we still need SPARK-12218 after this?

Copy link
Contributor Author

@jliwork jliwork Nov 20, 2017

Choose a reason for hiding this comment

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

I would think so. SPARK-12218 put fixes into ParquetFilters.createFilter and OrcFilters.createFilter. They're similar to DataSourceStrategy.translateFilter but have different signature customized for Parquet and ORC. For all datasources including JDBC, Parquet, etc, translateFilter is called to determine if a predicate Expression can be pushed down as a Filter or not. Next for Parquet and ORC, Filters get mapped to Parquet or ORC specific filters with their own createFilter method.

So this PR does help all data sources to get the correct set of push down predicates. Without this PR we simply got lucky with Parquet and ORC in terms of result correctness because 1) it looks like we always apply Filter on top of scan; 2) we end up with same number of or more rows returned with one leg missing from AND.

JDBC data source does not always come with Filter on top of scan therefore exposed the bug.

Copy link
Member

Choose a reason for hiding this comment

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

We do not need to clean up the codes in this PR. Let us minimize the code changes and it can simplify the backport.

Copy link
Member

Choose a reason for hiding this comment

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

Although Catalyst predicate expressions are all converted to sources.Filter when we try to push down them. Not all convertible filters can be handled by Parquet and ORC. So I think we still can face the case only one sub-filter of AND can be pushed down by the file format.


case expressions.Or(left, right) =>
for {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,306 @@
/*
* 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.execution.datasources

import org.apache.spark.sql.catalyst.expressions
import org.apache.spark.sql.catalyst.expressions._
Copy link
Member

Choose a reason for hiding this comment

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

As you import expressions._, I think we can write EqualTo instead of expressions.EqualTo for catalyst predicates below?

Because you always write sources.EqualTo`, I think we don't confuse with them?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion. Fixed.

import org.apache.spark.sql.catalyst.plans.PlanTest
import org.apache.spark.sql.sources
import org.apache.spark.sql.test.SharedSQLContext
import org.apache.spark.sql.types._


class DataSourceStrategySuite extends PlanTest with SharedSQLContext {

test("translate simple expression") {
val attrInt = AttributeReference("cint", IntegerType)()
val attrStr = AttributeReference("cstr", StringType)()
Copy link
Member

Choose a reason for hiding this comment

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

import org.apache.spark.sql.catalyst.dsl.expressions._

You can simplify your test cases.

    val attrInt = 'cint.int
    val attrStr = 'cstr.string

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed. Thanks!


assertResult(Some(sources.EqualTo("cint", 1))) {
DataSourceStrategy.translateFilter(
expressions.EqualTo(attrInt, Literal(1)))
Copy link
Member

Choose a reason for hiding this comment

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

No need to call Literal here. It will be implicitly casted to Literal

expressions.EqualTo(attrInt,1))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed. Thanks!

}
Copy link
Member

Choose a reason for hiding this comment

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

Looks like we can have a small helper function:

def testTranslateFilter(catalystFilter: Expression, result: Option[sources.Filter]): Unit = {
  assertResult(result) {
    DataSourceStrategy.translateFilter(catalystFilter)
  }
}

So the tests can be rewritten as:

testTranslateFilter(expressions.EqualTo(attrInt, 1), Some(sources.EqualTo("cint", 1)))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! I've followed your suggestion and the test suite looks cleaner now.

assertResult(Some(sources.EqualTo("cint", 1))) {
DataSourceStrategy.translateFilter(
expressions.EqualTo(Literal(1), attrInt))
}

assertResult(Some(sources.EqualNullSafe("cstr", null))) {
DataSourceStrategy.translateFilter(
expressions.EqualNullSafe(attrStr, Literal(null)))
}
assertResult(Some(sources.EqualNullSafe("cstr", null))) {
DataSourceStrategy.translateFilter(
expressions.EqualNullSafe(Literal(null), attrStr))
}

assertResult(Some(sources.GreaterThan("cint", 1))) {
DataSourceStrategy.translateFilter(
expressions.GreaterThan(attrInt, Literal(1)))
}
assertResult(Some(sources.GreaterThan("cint", 1))) {
DataSourceStrategy.translateFilter(
expressions.LessThan(Literal(1), attrInt))
}

assertResult(Some(sources.LessThan("cint", 1))) {
DataSourceStrategy.translateFilter(
expressions.LessThan(attrInt, Literal(1)))
}
assertResult(Some(sources.LessThan("cint", 1))) {
DataSourceStrategy.translateFilter(
expressions.GreaterThan(Literal(1), attrInt))
}

assertResult(Some(sources.GreaterThanOrEqual("cint", 1))) {
DataSourceStrategy.translateFilter(
expressions.GreaterThanOrEqual(attrInt, Literal(1)))
}
assertResult(Some(sources.GreaterThanOrEqual("cint", 1))) {
DataSourceStrategy.translateFilter(
expressions.LessThanOrEqual(Literal(1), attrInt))
}

assertResult(Some(sources.LessThanOrEqual("cint", 1))) {
DataSourceStrategy.translateFilter(
expressions.LessThanOrEqual(attrInt, Literal(1)))
}
assertResult(Some(sources.LessThanOrEqual("cint", 1))) {
DataSourceStrategy.translateFilter(
expressions.GreaterThanOrEqual(Literal(1), attrInt))
}

assertResult(Some(sources.In("cint", Array(1, 2, 3)))) {
DataSourceStrategy.translateFilter(
expressions.InSet(attrInt, Set(1, 2, 3)))
}

assertResult(Some(sources.In("cint", Array(1, 2, 3)))) {
DataSourceStrategy.translateFilter(
expressions.In(attrInt, Seq(Literal(1), Literal(2), Literal(3))))
}

assertResult(Some(sources.IsNull("cint"))) {
DataSourceStrategy.translateFilter(
expressions.IsNull(attrInt))
}
assertResult(Some(sources.IsNotNull("cint"))) {
DataSourceStrategy.translateFilter(
expressions.IsNotNull(attrInt))
}

assertResult(Some(sources.And(
sources.GreaterThan("cint", 1),
sources.LessThan("cint", 10)))) {
DataSourceStrategy.translateFilter(expressions.And(
expressions.GreaterThan(attrInt, Literal(1)),
expressions.LessThan(attrInt, Literal(10))
))
}

assertResult(Some(sources.Or(
sources.GreaterThanOrEqual("cint", 8),
sources.LessThanOrEqual("cint", 2)))) {
DataSourceStrategy.translateFilter(expressions.Or(
expressions.GreaterThanOrEqual(attrInt, Literal(8)),
expressions.LessThanOrEqual(attrInt, Literal(2))
))
}

assertResult(Some(sources.Not(
sources.GreaterThanOrEqual("cint", 8)))) {
DataSourceStrategy.translateFilter(
expressions.Not(expressions.GreaterThanOrEqual(attrInt, Literal(8))
))
}

assertResult(Some(sources.StringStartsWith("cstr", "a"))) {
DataSourceStrategy.translateFilter(
expressions.StartsWith(attrStr, Literal("a")
))
}

assertResult(Some(sources.StringEndsWith("cstr", "a"))) {
DataSourceStrategy.translateFilter(
expressions.EndsWith(attrStr, Literal("a")
))
}

assertResult(Some(sources.StringContains("cstr", "a"))) {
DataSourceStrategy.translateFilter(
expressions.Contains(attrStr, Literal("a")
))
}
}

test("translate complex expression") {
val attrInt = AttributeReference("cint", IntegerType)()

assertResult(None) {
DataSourceStrategy.translateFilter(
expressions.LessThanOrEqual(
expressions.Subtract(expressions.Abs(attrInt), Literal(2)), Literal(1)))
}

assertResult(Some(sources.Or(
sources.And(
sources.GreaterThan("cint", 1),
sources.LessThan("cint", 10)),
sources.And(
sources.GreaterThan("cint", 50),
sources.LessThan("cint", 100))))) {
DataSourceStrategy.translateFilter(expressions.Or(
expressions.And(
expressions.GreaterThan(attrInt, Literal(1)),
expressions.LessThan(attrInt, Literal(10))
),
expressions.And(
expressions.GreaterThan(attrInt, Literal(50)),
expressions.LessThan(attrInt, Literal(100))
)
))
}
// SPARK-22548 Incorrect nested AND expression pushed down to JDBC data source
assertResult(None) {
DataSourceStrategy.translateFilter(expressions.Or(
expressions.And(
expressions.GreaterThan(attrInt, Literal(1)),
expressions.LessThan(
expressions.Abs(attrInt),
Literal(10))
),
expressions.And(
expressions.GreaterThan(attrInt, Literal(50)),
expressions.LessThan(attrInt, Literal(100))
)
))
}
assertResult(None) {
DataSourceStrategy.translateFilter(
expressions.Not(expressions.And(
expressions.Or(
expressions.LessThanOrEqual(attrInt, Literal(1)),
expressions.GreaterThanOrEqual(
expressions.Abs(attrInt),
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

Literal(10))
),
expressions.Or(
expressions.LessThanOrEqual(attrInt, Literal(50)),
expressions.GreaterThanOrEqual(attrInt, Literal(100))
)
)))
}

assertResult(Some(sources.Or(
sources.Or(
sources.EqualTo("cint", 1),
sources.EqualTo("cint", 10)),
sources.Or(
sources.GreaterThan("cint", 0),
sources.LessThan("cint", -10))))) {
DataSourceStrategy.translateFilter(expressions.Or(
expressions.Or(
expressions.EqualTo(attrInt, Literal(1)),
expressions.EqualTo(attrInt, Literal(10))
),
expressions.Or(
expressions.GreaterThan(attrInt, Literal(0)),
expressions.LessThan(attrInt, Literal(-10))
)
))
}
assertResult(None) {
DataSourceStrategy.translateFilter(expressions.Or(
expressions.Or(
expressions.EqualTo(attrInt, Literal(1)),
expressions.EqualTo(
expressions.Abs(attrInt),
Literal(10))
),
expressions.Or(
expressions.GreaterThan(attrInt, Literal(0)),
expressions.LessThan(attrInt, Literal(-10))
)
))
}

assertResult(Some(sources.And(
sources.And(
sources.GreaterThan("cint", 1),
sources.LessThan("cint", 10)),
sources.And(
sources.EqualTo("cint", 6),
sources.IsNotNull("cint"))))) {
DataSourceStrategy.translateFilter(expressions.And(
expressions.And(
expressions.GreaterThan(attrInt, Literal(1)),
expressions.LessThan(attrInt, Literal(10))
),
expressions.And(
expressions.EqualTo(attrInt, Literal(6)),
expressions.IsNotNull(attrInt)
)
))
}
assertResult(None) {
DataSourceStrategy.translateFilter(expressions.And(
expressions.And(
expressions.GreaterThan(attrInt, Literal(1)),
expressions.LessThan(attrInt, Literal(10))
),
expressions.And(
expressions.EqualTo(expressions.Abs(attrInt),
Literal(6)),
expressions.IsNotNull(attrInt)
)
))
}

assertResult(Some(sources.And(
sources.Or(
sources.GreaterThan("cint", 1),
sources.LessThan("cint", 10)),
sources.Or(
sources.EqualTo("cint", 6),
sources.IsNotNull("cint"))))) {
DataSourceStrategy.translateFilter(expressions.And(
expressions.Or(
expressions.GreaterThan(attrInt, Literal(1)),
expressions.LessThan(attrInt, Literal(10))
),
expressions.Or(
expressions.EqualTo(attrInt, Literal(6)),
expressions.IsNotNull(attrInt)
)
))
}
assertResult(None) {
DataSourceStrategy.translateFilter(expressions.And(
expressions.Or(
expressions.GreaterThan(attrInt, Literal(1)),
expressions.LessThan(attrInt, Literal(10))
),
expressions.Or(
expressions.EqualTo(expressions.Abs(attrInt),
Literal(6)),
expressions.IsNotNull(attrInt)
)
))
}
}
}
25 changes: 25 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/jdbc/JDBCSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,33 @@ class JDBCSuite extends SparkFunSuite
// The older versions of spark have this kind of bugs in parquet data source.
val df1 = sql("SELECT * FROM foobar WHERE NOT (THEID != 2 AND NAME != 'mary')")
Copy link
Member

@viirya viirya Nov 21, 2017

Choose a reason for hiding this comment

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

As I leave the comment in #10468 (comment), the above test doesn't actually test against SPARK-12218 issue. Maybe we can simply drop it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

val df2 = sql("SELECT * FROM foobar WHERE NOT (THEID != 2) OR NOT (NAME != 'mary')")
val df3 = sql("SELECT * FROM foobar WHERE (THEID > 0 AND NAME = 'mary') OR (NAME = 'fred')")
val df4 = sql("SELECT * FROM foobar " +
"WHERE (THEID > 0 AND TRIM(NAME) = 'mary') OR (NAME = 'fred')")
val df5 = sql("SELECT * FROM foobar " +
"WHERE THEID > 0 AND TRIM(NAME) = 'mary' AND LENGTH(NAME) > 3")
val df6 = sql("SELECT * FROM foobar " +
"WHERE THEID < 0 OR NAME = 'mary' OR NAME = 'fred'")
val df7 = sql("SELECT * FROM foobar " +
"WHERE THEID < 0 OR TRIM(NAME) = 'mary' OR NAME = 'fred'")
val df8 = sql("SELECT * FROM foobar " +
"WHERE NOT((THEID < 0 OR NAME != 'mary') AND (THEID != 1 OR NAME != 'fred'))")
val df9 = sql("SELECT * FROM foobar " +
"WHERE NOT((THEID < 0 OR NAME != 'mary') AND (THEID != 1 OR TRIM(NAME) != 'fred'))")
val df10 = sql("SELECT * FROM foobar " +
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we need to test so many cases? as an end-to-end test, I think we only need a typical case.

"WHERE (NOT(THEID < 0 OR TRIM(NAME) != 'mary')) OR (THEID = 1 AND NAME = 'fred')")

assert(df1.collect.toSet === Set(Row("mary", 2)))
assert(df2.collect.toSet === Set(Row("mary", 2)))
assert(df3.collect.toSet === Set(Row("fred", 1), Row("mary", 2)))
assert(df4.collect.toSet === Set(Row("fred", 1), Row("mary", 2)))
assert(df5.collect.toSet === Set(Row("mary", 2)))
assert(df6.collect.toSet === Set(Row("fred", 1), Row("mary", 2)))
assert(df7.collect.toSet === Set(Row("fred", 1), Row("mary", 2)))
assert(df8.collect.toSet === Set(Row("fred", 1), Row("mary", 2)))
assert(df9.collect.toSet === Set(Row("fred", 1), Row("mary", 2)))
assert(df10.collect.toSet === Set(Row("fred", 1), Row("mary", 2)))
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd like to create a new DataSourceStrategySuite to test the translateFilter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure. I can help.

Copy link
Member

Choose a reason for hiding this comment

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

They are end-to-end test cases.

If you can, we should also add such a unit test suite. In the future, we can add more unit test cases for verifying more complex cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I went ahead and added a new DataSourceStrategySuite to test the translateFilter. Please free feel to let me know of any further comments. Thanks!



def checkNotPushdown(df: DataFrame): DataFrame = {
val parentPlan = df.queryExecution.executedPlan
Expand Down