-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-25558][SQL] Pushdown predicates for nested fields in DataSource Strategy #22573
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -442,53 +442,65 @@ object DataSourceStrategy { | |
| * @return a `Some[Filter]` if the input [[Expression]] is convertible, otherwise a `None`. | ||
| */ | ||
| protected[sql] def translateFilter(predicate: Expression): Option[Filter] = { | ||
| // Recursively try to find an attribute name from the top level that can be pushed down. | ||
| def attrName(e: Expression): Option[String] = e match { | ||
| // In Spark and many data sources such as parquet, dots are used as a column path delimiter; | ||
| // thus, we don't translate such expressions. | ||
| case a: Attribute if !a.name.contains(".") => | ||
| Some(a.name) | ||
| case s: GetStructField if !s.childSchema(s.ordinal).name.contains(".") => | ||
| attrName(s.child).map(_ + s".${s.childSchema(s.ordinal).name}") | ||
|
Contributor
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.
|
||
| case _ => | ||
| None | ||
| } | ||
|
|
||
| predicate match { | ||
| case expressions.EqualTo(a: Attribute, Literal(v, t)) => | ||
| Some(sources.EqualTo(a.name, convertToScala(v, t))) | ||
| case expressions.EqualTo(Literal(v, t), a: Attribute) => | ||
| Some(sources.EqualTo(a.name, convertToScala(v, t))) | ||
|
|
||
| case expressions.EqualNullSafe(a: Attribute, Literal(v, t)) => | ||
| Some(sources.EqualNullSafe(a.name, convertToScala(v, t))) | ||
| case expressions.EqualNullSafe(Literal(v, t), a: Attribute) => | ||
| Some(sources.EqualNullSafe(a.name, convertToScala(v, t))) | ||
|
|
||
| case expressions.GreaterThan(a: Attribute, Literal(v, t)) => | ||
| Some(sources.GreaterThan(a.name, convertToScala(v, t))) | ||
| case expressions.GreaterThan(Literal(v, t), a: Attribute) => | ||
| Some(sources.LessThan(a.name, convertToScala(v, t))) | ||
|
|
||
| case expressions.LessThan(a: Attribute, Literal(v, t)) => | ||
| Some(sources.LessThan(a.name, convertToScala(v, t))) | ||
| case expressions.LessThan(Literal(v, t), a: Attribute) => | ||
| Some(sources.GreaterThan(a.name, convertToScala(v, t))) | ||
|
|
||
| case expressions.GreaterThanOrEqual(a: Attribute, Literal(v, t)) => | ||
| Some(sources.GreaterThanOrEqual(a.name, convertToScala(v, t))) | ||
| case expressions.GreaterThanOrEqual(Literal(v, t), a: Attribute) => | ||
| Some(sources.LessThanOrEqual(a.name, convertToScala(v, t))) | ||
|
|
||
| case expressions.LessThanOrEqual(a: Attribute, Literal(v, t)) => | ||
| Some(sources.LessThanOrEqual(a.name, convertToScala(v, t))) | ||
| case expressions.LessThanOrEqual(Literal(v, t), a: Attribute) => | ||
| Some(sources.GreaterThanOrEqual(a.name, convertToScala(v, t))) | ||
|
|
||
| case expressions.InSet(a: Attribute, set) => | ||
| val toScala = CatalystTypeConverters.createToScalaConverter(a.dataType) | ||
| Some(sources.In(a.name, set.toArray.map(toScala))) | ||
| case expressions.EqualTo(e: Expression, Literal(v, t)) => | ||
| attrName(e).map(name => sources.EqualTo(name, convertToScala(v, t))) | ||
| case expressions.EqualTo(Literal(v, t), e: Expression) => | ||
| attrName(e).map(name => sources.EqualTo(name, convertToScala(v, t))) | ||
|
|
||
| case expressions.EqualNullSafe(e: Expression, Literal(v, t)) => | ||
| attrName(e).map(name => sources.EqualNullSafe(name, convertToScala(v, t))) | ||
| case expressions.EqualNullSafe(Literal(v, t), e: Expression) => | ||
| attrName(e).map(name => sources.EqualNullSafe(name, convertToScala(v, t))) | ||
|
|
||
| case expressions.GreaterThan(e: Expression, Literal(v, t)) => | ||
| attrName(e).map(name => sources.GreaterThan(name, convertToScala(v, t))) | ||
| case expressions.GreaterThan(Literal(v, t), e: Expression) => | ||
| attrName(e).map(name => sources.LessThan(name, convertToScala(v, t))) | ||
|
|
||
| case expressions.LessThan(e: Expression, Literal(v, t)) => | ||
| attrName(e).map(name => sources.LessThan(name, convertToScala(v, t))) | ||
| case expressions.LessThan(Literal(v, t), e: Expression) => | ||
| attrName(e).map(name => sources.GreaterThan(name, convertToScala(v, t))) | ||
|
|
||
| case expressions.GreaterThanOrEqual(e: Expression, Literal(v, t)) => | ||
| attrName(e).map(name => sources.GreaterThanOrEqual(name, convertToScala(v, t))) | ||
| case expressions.GreaterThanOrEqual(Literal(v, t), e: Expression) => | ||
| attrName(e).map(name => sources.LessThanOrEqual(name, convertToScala(v, t))) | ||
|
|
||
| case expressions.LessThanOrEqual(e: Expression, Literal(v, t)) => | ||
| attrName(e).map(name => sources.LessThanOrEqual(name, convertToScala(v, t))) | ||
| case expressions.LessThanOrEqual(Literal(v, t), e: Expression) => | ||
| attrName(e).map(name => sources.GreaterThanOrEqual(name, convertToScala(v, t))) | ||
|
|
||
| case expressions.InSet(e: Expression, set) => | ||
| val toScala = CatalystTypeConverters.createToScalaConverter(e.dataType) | ||
| attrName(e).map(name => sources.In(name, set.toArray.map(toScala))) | ||
|
|
||
| // Because we only convert In to InSet in Optimizer when there are more than certain | ||
| // items. So it is possible we still get an In expression here that needs to be pushed | ||
| // down. | ||
| case expressions.In(a: Attribute, list) if list.forall(_.isInstanceOf[Literal]) => | ||
| val hSet = list.map(_.eval(EmptyRow)) | ||
| val toScala = CatalystTypeConverters.createToScalaConverter(a.dataType) | ||
| Some(sources.In(a.name, hSet.toArray.map(toScala))) | ||
| case expressions.In(e: Expression, list) if list.forall(_.isInstanceOf[Literal]) => | ||
| val hSet = list.map(e => e.eval(EmptyRow)) | ||
| val toScala = CatalystTypeConverters.createToScalaConverter(e.dataType) | ||
| attrName(e).map(name => sources.In(name, hSet.toArray.map(toScala))) | ||
|
|
||
| case expressions.IsNull(a: Attribute) => | ||
| Some(sources.IsNull(a.name)) | ||
| case expressions.IsNotNull(a: Attribute) => | ||
| Some(sources.IsNotNull(a.name)) | ||
| case expressions.IsNull(e: Expression) => | ||
| attrName(e).map(name => sources.IsNull(name)) | ||
| case expressions.IsNotNull(e: Expression) => | ||
| attrName(e).map(name => sources.IsNotNull(name)) | ||
|
|
||
| case expressions.And(left, right) => | ||
| // See SPARK-12218 for detailed discussion | ||
|
|
@@ -514,14 +526,14 @@ object DataSourceStrategy { | |
| case expressions.Not(child) => | ||
| translateFilter(child).map(sources.Not) | ||
|
|
||
| case expressions.StartsWith(a: Attribute, Literal(v: UTF8String, StringType)) => | ||
| Some(sources.StringStartsWith(a.name, v.toString)) | ||
| case expressions.StartsWith(e: Expression, Literal(v: UTF8String, StringType)) => | ||
| attrName(e).map(name => sources.StringStartsWith(name, v.toString)) | ||
|
|
||
| case expressions.EndsWith(a: Attribute, Literal(v: UTF8String, StringType)) => | ||
| Some(sources.StringEndsWith(a.name, v.toString)) | ||
| case expressions.EndsWith(e: Expression, Literal(v: UTF8String, StringType)) => | ||
| attrName(e).map(name => sources.StringEndsWith(name, v.toString)) | ||
|
|
||
| case expressions.Contains(a: Attribute, Literal(v: UTF8String, StringType)) => | ||
| Some(sources.StringContains(a.name, v.toString)) | ||
| case expressions.Contains(e: Expression, Literal(v: UTF8String, StringType)) => | ||
| attrName(e).map(name => sources.StringContains(name, v.toString)) | ||
|
|
||
| case expressions.Literal(true, BooleanType) => | ||
| Some(sources.AlwaysTrue) | ||
|
|
||
Oops, something went wrong.
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.
Will this cause regression for data source supporting dot in column name?
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 have any data source currently supporting
dotin the column name with pushdown? The worst case will be no pushdown for those data sources.I know ORC doesn't work for now. We can have another followup PR to address this.
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.
JDBC data source seems having no such restrict. So I worry that this change can cause some regressions.
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.
Yes, @dbtsai . This PR has a regression on ORC at least. The following is ORC result in Spark 2.3.2 and it will slowdown at least 5 times like Parquet.
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.
Apache Spark 2.4.0 RC2 has a regression on this case. So, for now, this PR doesn't have regssion on
masterbranch.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.
Probably a dumb question. Is it possible to store a column with
.in its name in parquet?More holistically, I think it would be better to create an abstraction for a multipart identifier in a filter as opposed to encoding it using a '.'.