-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-22548][SQL] Incorrect nested AND expression pushed down to JDBC data source #19776
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
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
58de88c
[SPARK-22548][SQL] Incorrect nested AND expression pushed down to JDB…
jliwork e540790
address comment to add PR discussion number for reference
jliwork 635768e
address comment to explain the fix with example
jliwork 3bb7d3c
address comment to leave just JIRA number there
jliwork 0aebdfb
address comment to add a new DataSourceStrategySuite to test the tran…
jliwork ba06181
address comment to extend QueryTest
jliwork fc34568
address comment to improve test suite
jliwork 0cbb528
address comments to improve tests
jliwork a0b3d4e
address comments to polish test suite
jliwork 7a19ac6
address comments to fix a typo in test case
jliwork File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
231 changes: 231 additions & 0 deletions
231
...e/src/test/scala/org/apache/spark/sql/execution/datasources/DataSourceStrategySuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,231 @@ | ||
| /* | ||
| * 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.dsl.expressions._ | ||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.plans.PlanTest | ||
| import org.apache.spark.sql.sources | ||
| import org.apache.spark.sql.test.SharedSQLContext | ||
|
|
||
| class DataSourceStrategySuite extends PlanTest with SharedSQLContext { | ||
|
|
||
| test("translate simple expression") { | ||
| val attrInt = 'cint.int | ||
| val attrStr = 'cstr.string | ||
|
|
||
| testTranslateFilter(EqualTo(attrInt, 1), Some(sources.EqualTo("cint", 1))) | ||
| testTranslateFilter(EqualTo(1, attrInt), Some(sources.EqualTo("cint", 1))) | ||
|
|
||
| testTranslateFilter(EqualNullSafe(attrStr, Literal(null)), | ||
| Some(sources.EqualNullSafe("cstr", null))) | ||
| testTranslateFilter(EqualNullSafe(Literal(null), attrStr), | ||
| Some(sources.EqualNullSafe("cstr", null))) | ||
|
|
||
| testTranslateFilter(GreaterThan(attrInt, 1), Some(sources.GreaterThan("cint", 1))) | ||
| testTranslateFilter(GreaterThan(1, attrInt), Some(sources.LessThan("cint", 1))) | ||
|
|
||
| testTranslateFilter(LessThan(attrInt, 1), Some(sources.LessThan("cint", 1))) | ||
| testTranslateFilter(LessThan(1, attrInt), Some(sources.GreaterThan("cint", 1))) | ||
|
|
||
| testTranslateFilter(GreaterThanOrEqual(attrInt, 1), Some(sources.GreaterThanOrEqual("cint", 1))) | ||
| testTranslateFilter(GreaterThanOrEqual(1, attrInt), Some(sources.LessThanOrEqual("cint", 1))) | ||
|
|
||
| testTranslateFilter(LessThanOrEqual(attrInt, 1), Some(sources.LessThanOrEqual("cint", 1))) | ||
| testTranslateFilter(LessThanOrEqual(1, attrInt), Some(sources.GreaterThanOrEqual("cint", 1))) | ||
|
|
||
| testTranslateFilter(InSet(attrInt, Set(1, 2, 3)), Some(sources.In("cint", Array(1, 2, 3)))) | ||
|
|
||
| testTranslateFilter(In(attrInt, Seq(1, 2, 3)), Some(sources.In("cint", Array(1, 2, 3)))) | ||
|
|
||
| testTranslateFilter(IsNull(attrInt), Some(sources.IsNull("cint"))) | ||
| testTranslateFilter(IsNotNull(attrInt), Some(sources.IsNotNull("cint"))) | ||
|
|
||
| // cint > 1 AND cint < 10 | ||
| testTranslateFilter(And( | ||
| GreaterThan(attrInt, 1), | ||
| LessThan(attrInt, 10)), | ||
| Some(sources.And( | ||
| sources.GreaterThan("cint", 1), | ||
| sources.LessThan("cint", 10)))) | ||
|
|
||
| // cint >= 8 OR cint <= 2 | ||
| testTranslateFilter(Or( | ||
| GreaterThanOrEqual(attrInt, 8), | ||
| LessThanOrEqual(attrInt, 2)), | ||
| Some(sources.Or( | ||
| sources.GreaterThanOrEqual("cint", 8), | ||
| sources.LessThanOrEqual("cint", 2)))) | ||
|
|
||
| testTranslateFilter(Not(GreaterThanOrEqual(attrInt, 8)), | ||
| Some(sources.Not(sources.GreaterThanOrEqual("cint", 8)))) | ||
|
|
||
| testTranslateFilter(StartsWith(attrStr, "a"), Some(sources.StringStartsWith("cstr", "a"))) | ||
|
|
||
| testTranslateFilter(EndsWith(attrStr, "a"), Some(sources.StringEndsWith("cstr", "a"))) | ||
|
|
||
| testTranslateFilter(Contains(attrStr, "a"), Some(sources.StringContains("cstr", "a"))) | ||
| } | ||
|
|
||
| test("translate complex expression") { | ||
| val attrInt = 'cint.int | ||
|
|
||
| // ABS(cint) - 2 <= 1 | ||
| testTranslateFilter(LessThanOrEqual( | ||
| // Expressions are not supported | ||
| // Functions such as 'Abs' are not supported | ||
| Subtract(Abs(attrInt), 2), 1), None) | ||
|
|
||
| // (cin1 > 1 AND cint < 10) OR (cint > 50 AND cint > 100) | ||
| testTranslateFilter(Or( | ||
| And( | ||
| GreaterThan(attrInt, 1), | ||
| LessThan(attrInt, 10) | ||
| ), | ||
| And( | ||
| GreaterThan(attrInt, 50), | ||
| LessThan(attrInt, 100))), | ||
| Some(sources.Or( | ||
| sources.And( | ||
| sources.GreaterThan("cint", 1), | ||
| sources.LessThan("cint", 10)), | ||
| sources.And( | ||
| sources.GreaterThan("cint", 50), | ||
| sources.LessThan("cint", 100))))) | ||
|
|
||
| // SPARK-22548 Incorrect nested AND expression pushed down to JDBC data source | ||
| // (cint > 1 AND ABS(cint) < 10) OR (cint < 50 AND cint > 100) | ||
| testTranslateFilter(Or( | ||
| And( | ||
| GreaterThan(attrInt, 1), | ||
| // Functions such as 'Abs' are not supported | ||
| LessThan(Abs(attrInt), 10) | ||
| ), | ||
| And( | ||
| GreaterThan(attrInt, 50), | ||
| LessThan(attrInt, 100))), None) | ||
|
|
||
| // NOT ((cint <= 1 OR ABS(cint) >= 10) AND (cint <= 50 OR cint >= 100)) | ||
| testTranslateFilter(Not(And( | ||
| Or( | ||
| LessThanOrEqual(attrInt, 1), | ||
| // Functions such as 'Abs' are not supported | ||
| GreaterThanOrEqual(Abs(attrInt), 10) | ||
| ), | ||
| Or( | ||
| LessThanOrEqual(attrInt, 50), | ||
| GreaterThanOrEqual(attrInt, 100)))), None) | ||
|
|
||
| // (cint = 1 OR cint = 10) OR (cint > 0 OR cint < -10) | ||
| testTranslateFilter(Or( | ||
| Or( | ||
| EqualTo(attrInt, 1), | ||
| EqualTo(attrInt, 10) | ||
| ), | ||
| Or( | ||
| GreaterThan(attrInt, 0), | ||
| LessThan(attrInt, -10))), | ||
| Some(sources.Or( | ||
| sources.Or( | ||
| sources.EqualTo("cint", 1), | ||
| sources.EqualTo("cint", 10)), | ||
| sources.Or( | ||
| sources.GreaterThan("cint", 0), | ||
| sources.LessThan("cint", -10))))) | ||
|
|
||
| // (cint = 1 OR ABS(cint) = 10) OR (cint > 0 OR cint < -10) | ||
| testTranslateFilter(Or( | ||
| Or( | ||
| EqualTo(attrInt, 1), | ||
| // Functions such as 'Abs' are not supported | ||
| EqualTo(Abs(attrInt), 10) | ||
| ), | ||
| Or( | ||
| GreaterThan(attrInt, 0), | ||
| LessThan(attrInt, -10))), None) | ||
|
|
||
| // In end-to-end testing, conjunctive predicate should has been split | ||
| // before reaching DataSourceStrategy.translateFilter. | ||
| // This is for UT purpose to test each [[case]]. | ||
| // (cint > 1 AND cint < 10) AND (cint = 6 AND cint IS NOT NULL) | ||
| testTranslateFilter(And( | ||
| And( | ||
| GreaterThan(attrInt, 1), | ||
| LessThan(attrInt, 10) | ||
| ), | ||
| And( | ||
| EqualTo(attrInt, 6), | ||
| IsNotNull(attrInt))), | ||
| Some(sources.And( | ||
| sources.And( | ||
| sources.GreaterThan("cint", 1), | ||
| sources.LessThan("cint", 10)), | ||
| sources.And( | ||
| sources.EqualTo("cint", 6), | ||
| sources.IsNotNull("cint"))))) | ||
|
|
||
| // (cint > 1 AND cint < 10) AND (ABS(cint) = 6 AND cint IS NOT NULL) | ||
| testTranslateFilter(And( | ||
| And( | ||
| GreaterThan(attrInt, 1), | ||
| LessThan(attrInt, 10) | ||
| ), | ||
| And( | ||
| // Functions such as 'Abs' are not supported | ||
| EqualTo(Abs(attrInt), 6), | ||
| IsNotNull(attrInt))), None) | ||
|
|
||
| // (cint > 1 OR cint < 10) AND (cint = 6 OR cint IS NOT NULL) | ||
| testTranslateFilter(And( | ||
| Or( | ||
| GreaterThan(attrInt, 1), | ||
| LessThan(attrInt, 10) | ||
| ), | ||
| Or( | ||
| EqualTo(attrInt, 6), | ||
| IsNotNull(attrInt))), | ||
| Some(sources.And( | ||
| sources.Or( | ||
| sources.GreaterThan("cint", 1), | ||
| sources.LessThan("cint", 10)), | ||
| sources.Or( | ||
| sources.EqualTo("cint", 6), | ||
| sources.IsNotNull("cint"))))) | ||
|
|
||
| // (cint > 1 OR cint < 10) AND (cint = 6 OR cint IS NOT NULL) | ||
| testTranslateFilter(And( | ||
| Or( | ||
| GreaterThan(attrInt, 1), | ||
| LessThan(attrInt, 10) | ||
| ), | ||
| Or( | ||
| // Functions such as 'Abs' are not supported | ||
| EqualTo(Abs(attrInt), 6), | ||
| IsNotNull(attrInt))), None) | ||
| } | ||
|
|
||
| /** | ||
| * Translate the given Catalyst [[Expression]] into data source [[sources.Filter]] | ||
| * then verify against the given [[sources.Filter]]. | ||
| */ | ||
| def testTranslateFilter(catalystFilter: Expression, result: Option[sources.Filter]): Unit = { | ||
| assertResult(result) { | ||
| DataSourceStrategy.translateFilter(catalystFilter) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
do we still need SPARK-12218 after this?
Uh oh!
There was an error while loading. Please reload this page.
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.
I would think so. SPARK-12218 put fixes into
ParquetFilters.createFilterandOrcFilters.createFilter. They're similar toDataSourceStrategy.translateFilterbut have different signature customized for Parquet and ORC. For all datasources including JDBC, Parquet, etc,translateFilteris called to determine if a predicateExpressioncan be pushed down as aFilteror not. Next for Parquet and ORC, Filters get mapped to Parquet or ORC specific filters with their owncreateFiltermethod.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
Filteron top of scan; 2) we end up with same number of or more rows returned with one leg missing fromAND.JDBC data source does not always come with
Filteron top of scan therefore exposed the bug.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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Although Catalyst predicate expressions are all converted to
sources.Filterwhen 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 ofANDcan be pushed down by the file format.