-
Notifications
You must be signed in to change notification settings - Fork 28.3k
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
Changes from 6 commits
58de88c
e540790
635768e
3bb7d3c
0aebdfb
ba06181
fc34568
0cbb528
a0b3d4e
7a19ac6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
leftFilter <- translateFilter(left) | ||
rightFilter <- translateFilter(right) | ||
} yield sources.And(leftFilter, rightFilter) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we still need SPARK-12218 after this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would think so. SPARK-12218 put fixes into 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 JDBC data source does not always come with There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Although Catalyst predicate expressions are all converted to |
||
|
||
case expressions.Or(left, right) => | ||
for { | ||
|
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._ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As you import Because you always write There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to call
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. Thanks! |
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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))) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
) | ||
)) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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')") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 " + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to create a new There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. I can help. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I went ahead and added a new |
||
|
||
|
||
def checkNotPushdown(df: DataFrame): DataFrame = { | ||
val parentPlan = df.queryExecution.executedPlan | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Will do. Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. Follow what @yhuai wrote in the PR https://github.com/apache/spark/pull/10362/files
There was a problem hiding this comment.
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.