-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-51259][SQL] Refactor natural and using join keys computation #50009
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
mihailotim-db
wants to merge
1
commit into
apache:master
from
mihailotim-db:mihailotim-db/join_refactor
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
166 changes: 166 additions & 0 deletions
166
...src/main/scala/org/apache/spark/sql/catalyst/analysis/NaturalAndUsingJoinResolution.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,166 @@ | ||
/* | ||
* 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.catalyst.analysis | ||
|
||
import org.apache.spark.sql.catalyst.SQLConfHelper | ||
import org.apache.spark.sql.catalyst.expressions.{ | ||
Alias, | ||
And, | ||
Attribute, | ||
Coalesce, | ||
EqualTo, | ||
Expression, | ||
NamedExpression | ||
} | ||
import org.apache.spark.sql.catalyst.plans.{ | ||
FullOuter, | ||
InnerLike, | ||
JoinType, | ||
LeftExistence, | ||
LeftOuter, | ||
RightOuter | ||
} | ||
import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan | ||
import org.apache.spark.sql.errors.{ | ||
DataTypeErrorsBase, | ||
QueryCompilationErrors, | ||
QueryExecutionErrors | ||
} | ||
|
||
object NaturalAndUsingJoinResolution extends DataTypeErrorsBase with SQLConfHelper { | ||
|
||
/** | ||
* For a given [[Join]], computes output, hidden output and new condition, if such exists. | ||
*/ | ||
def computeJoinOutputsAndNewCondition( | ||
left: LogicalPlan, | ||
leftOutput: Seq[Attribute], | ||
right: LogicalPlan, | ||
rightOutput: Seq[Attribute], | ||
joinType: JoinType, | ||
joinNames: Seq[String], | ||
condition: Option[Expression], | ||
resolveName: (String, String) => Boolean) | ||
: (Seq[NamedExpression], Seq[Attribute], Option[Expression]) = { | ||
val (leftKeys, rightKeys) = resolveKeysForNaturalAndUsingJoin( | ||
left, | ||
leftOutput, | ||
right, | ||
rightOutput, | ||
joinNames, | ||
resolveName | ||
) | ||
val joinPairs = leftKeys.zip(rightKeys) | ||
|
||
val newCondition = (condition ++ joinPairs.map(EqualTo.tupled)).reduceOption(And) | ||
|
||
// the output list looks like: join keys, columns from left, columns from right | ||
val (output, hiddenOutput) = computeOutputAndHiddenOutput( | ||
leftOutput, | ||
leftKeys, | ||
rightOutput, | ||
rightKeys, | ||
joinPairs, | ||
joinType | ||
) | ||
(output, hiddenOutput, newCondition) | ||
} | ||
|
||
/** | ||
* Returns resolved keys for joining based on the output of [[Join]]'s children or throws and | ||
* error if a key name doesn't exist. | ||
*/ | ||
private def resolveKeysForNaturalAndUsingJoin( | ||
left: LogicalPlan, | ||
leftOutput: Seq[Attribute], | ||
right: LogicalPlan, | ||
rightOutput: Seq[Attribute], | ||
joinNames: Seq[String], | ||
resolveName: (String, String) => Boolean): (Seq[Attribute], Seq[Attribute]) = { | ||
val leftKeys = joinNames.map { keyName => | ||
leftOutput.find(attribute => resolveName(attribute.name, keyName)).getOrElse { | ||
throw QueryCompilationErrors.unresolvedUsingColForJoinError( | ||
keyName, | ||
left.schema.fieldNames.sorted.map(toSQLId).mkString(", "), | ||
"left" | ||
) | ||
} | ||
} | ||
val rightKeys = joinNames.map { keyName => | ||
rightOutput.find(attribute => resolveName(attribute.name, keyName)).getOrElse { | ||
throw QueryCompilationErrors.unresolvedUsingColForJoinError( | ||
keyName, | ||
right.schema.fieldNames.sorted.map(toSQLId).mkString(", "), | ||
"right" | ||
) | ||
} | ||
} | ||
(leftKeys, rightKeys) | ||
} | ||
|
||
/** | ||
* Computes the output and hidden output for a given [[Join]], based on the output of its | ||
* children. | ||
*/ | ||
private def computeOutputAndHiddenOutput( | ||
leftOutput: Seq[Attribute], | ||
leftKeys: Seq[Attribute], | ||
rightOutput: Seq[Attribute], | ||
rightKeys: Seq[Attribute], | ||
joinPairs: Seq[(Attribute, Attribute)], | ||
joinType: JoinType): (Seq[NamedExpression], Seq[Attribute]) = { | ||
// columns not in joinPairs | ||
val lUniqueOutput = leftOutput.filterNot(att => leftKeys.contains(att)) | ||
val rUniqueOutput = rightOutput.filterNot(att => rightKeys.contains(att)) | ||
joinType match { | ||
case LeftOuter => | ||
( | ||
leftKeys ++ lUniqueOutput ++ rUniqueOutput.map(_.withNullability(true)), | ||
rightKeys.map(_.withNullability(true)) | ||
) | ||
case LeftExistence(_) => | ||
(leftKeys ++ lUniqueOutput, Seq.empty) | ||
case RightOuter => | ||
( | ||
rightKeys ++ lUniqueOutput.map(_.withNullability(true)) ++ rUniqueOutput, | ||
leftKeys.map(_.withNullability(true)) | ||
) | ||
case FullOuter => | ||
// In full outer join, we should return non-null values for the join columns | ||
// if either side has non-null values for those columns. Therefore, for each | ||
// join column pair, add a coalesce to return the non-null value, if it exists. | ||
val joinedCols = joinPairs.map { | ||
case (l, r) => | ||
// Since this is a full outer join, either side could be null, so we explicitly | ||
// set the nullability to true for both sides. | ||
Alias(Coalesce(Seq(l.withNullability(true), r.withNullability(true))), l.name)() | ||
} | ||
( | ||
joinedCols ++ | ||
lUniqueOutput.map(_.withNullability(true)) ++ | ||
rUniqueOutput.map(_.withNullability(true)), | ||
leftKeys.map(_.withNullability(true)) ++ | ||
rightKeys.map(_.withNullability(true)) | ||
) | ||
case _: InnerLike => | ||
(leftKeys ++ lUniqueOutput ++ rUniqueOutput, rightKeys) | ||
case _ => | ||
throw QueryExecutionErrors.unsupportedNaturalJoinTypeError(joinType) | ||
} | ||
} | ||
} |
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.
I changed the return type of
computeKeysForNaturalOrUsingJoin
fromSeq
toAttributeSet
to avoid quadratic lookups here. I think it makes sense to make this change in this PR, but it can be moved to a followup. Wdyt @cloud-fan @vladimirg-db ?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.
Moved to a separate PR: #50010