-
Notifications
You must be signed in to change notification settings - Fork 29.1k
[SPARK-14781] [SQL] support nested predicate subquery #12820
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
Changes from all commits
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 |
|---|---|---|
|
|
@@ -99,16 +99,18 @@ abstract class Optimizer(sessionCatalog: SessionCatalog, conf: CatalystConf) | |
| EliminateSorts, | ||
| SimplifyCasts, | ||
| SimplifyCaseConversionExpressions, | ||
| EliminateSerialization, | ||
| RewritePredicateSubquery) :: | ||
| EliminateSerialization) :: | ||
| Batch("Decimal Optimizations", fixedPoint, | ||
| DecimalAggregates) :: | ||
| Batch("Typed Filter Optimization", fixedPoint, | ||
| EmbedSerializerInFilter) :: | ||
| Batch("LocalRelation", fixedPoint, | ||
| ConvertToLocalRelation) :: | ||
| Batch("OptimizeCodegen", Once, | ||
| OptimizeCodegen(conf)) :: Nil | ||
| OptimizeCodegen(conf)) :: | ||
| Batch("RewriteSubquery", Once, | ||
| RewritePredicateSubquery, | ||
| CollapseProject) :: Nil | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -1077,7 +1079,14 @@ object ReorderJoin extends Rule[LogicalPlan] with PredicateHelper { | |
| def createOrderedJoin(input: Seq[LogicalPlan], conditions: Seq[Expression]): LogicalPlan = { | ||
| assert(input.size >= 2) | ||
| if (input.size == 2) { | ||
| Join(input(0), input(1), Inner, conditions.reduceLeftOption(And)) | ||
| val (joinConditions, others) = conditions.partition( | ||
| e => !PredicateSubquery.hasPredicateSubquery(e)) | ||
| val join = Join(input(0), input(1), Inner, joinConditions.reduceLeftOption(And)) | ||
| if (others.nonEmpty) { | ||
| Filter(others.reduceLeft(And), join) | ||
| } else { | ||
| join | ||
| } | ||
| } else { | ||
| val left :: rest = input.toList | ||
| // find out the first join that have at least one join condition | ||
|
|
@@ -1090,7 +1099,8 @@ object ReorderJoin extends Rule[LogicalPlan] with PredicateHelper { | |
| val right = conditionalJoin.getOrElse(rest.head) | ||
|
|
||
| val joinedRefs = left.outputSet ++ right.outputSet | ||
| val (joinConditions, others) = conditions.partition(_.references.subsetOf(joinedRefs)) | ||
| val (joinConditions, others) = conditions.partition( | ||
| e => e.references.subsetOf(joinedRefs) && !PredicateSubquery.hasPredicateSubquery(e)) | ||
| val joined = Join(left, right, Inner, joinConditions.reduceLeftOption(And)) | ||
|
|
||
| // should not have reference to same logical plan | ||
|
|
@@ -1200,9 +1210,16 @@ object PushPredicateThroughJoin extends Rule[LogicalPlan] with PredicateHelper { | |
| reduceLeftOption(And).map(Filter(_, left)).getOrElse(left) | ||
| val newRight = rightFilterConditions. | ||
| reduceLeftOption(And).map(Filter(_, right)).getOrElse(right) | ||
| val newJoinCond = (commonFilterCondition ++ joinCondition).reduceLeftOption(And) | ||
| val (newJoinConditions, others) = | ||
| commonFilterCondition.partition(e => !PredicateSubquery.hasPredicateSubquery(e)) | ||
| val newJoinCond = (newJoinConditions ++ joinCondition).reduceLeftOption(And) | ||
|
|
||
| Join(newLeft, newRight, Inner, newJoinCond) | ||
| val join = Join(newLeft, newRight, Inner, newJoinCond) | ||
| if (others.nonEmpty) { | ||
|
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. 2nd time you need this. Almost warrants an inner method.
Contributor
Author
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. Repeat it only twice, I think it's OK.
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. nvm - two different rules. |
||
| Filter(others.reduceLeft(And), join) | ||
| } else { | ||
| join | ||
| } | ||
| case RightOuter => | ||
| // push down the right side only `where` condition | ||
| val newLeft = left | ||
|
|
@@ -1530,6 +1547,16 @@ object RewritePredicateSubquery extends Rule[LogicalPlan] with PredicateHelper { | |
| // Note that will almost certainly be planned as a Broadcast Nested Loop join. Use EXISTS | ||
| // if performance matters to you. | ||
| Join(p, sub, LeftAnti, Option(Or(anyNull, condition))) | ||
| case (p, predicate) => | ||
|
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. This won't plan these joins outside of filters right? So this is not working yet: select a.*, a.value in (select value from b) as in_b from a
Contributor
Author
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. yes. If that's needed, we could support that as follow-up PR.
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. Follow-up PR works. |
||
| var joined = p | ||
| val replaced = predicate transformUp { | ||
| case PredicateSubquery(sub, conditions, nullAware, _) => | ||
| // TODO: support null-aware join | ||
| val exists = AttributeReference("exists", BooleanType, false)() | ||
| joined = Join(joined, sub, ExistenceJoin(exists), conditions.reduceLeftOption(And)) | ||
| exists | ||
| } | ||
| Project(p.output, Filter(replaced, joined)) | ||
| } | ||
| } | ||
| } | ||
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.
NIT: It might be easier to flip the names and call
PredicateSubquery.hasPredicateSubquerydirectlyThere 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.
It's weird to see
otherscome beforejoinConditions, so I make it this way.