-
Notifications
You must be signed in to change notification settings - Fork 28.5k
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-32755][SQL] Maintain the order of expressions in AttributeSet and ExpressionSet #29598
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5afdedb
Maintain the order of expressions in AttributeSet and ExpressionSet
dbaliafroozeh f235b53
Regenerate the files
dbaliafroozeh f3a79a3
Regenerate files
dbaliafroozeh b0478f3
Regenerate more files
dbaliafroozeh 9590555
Use ExpressionSet in CostBasedJoinReorder
dbaliafroozeh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -27,6 +27,10 @@ object ExpressionSet { | |
expressions.foreach(set.add) | ||
set | ||
} | ||
|
||
def apply(): ExpressionSet = { | ||
new ExpressionSet() | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -53,46 +57,102 @@ object ExpressionSet { | |
* This is consistent with how we define `semanticEquals` between two expressions. | ||
*/ | ||
class ExpressionSet protected( | ||
protected val baseSet: mutable.Set[Expression] = new mutable.HashSet, | ||
protected val originals: mutable.Buffer[Expression] = new ArrayBuffer) | ||
extends Set[Expression] { | ||
private val baseSet: mutable.Set[Expression] = new mutable.HashSet, | ||
private val originals: mutable.Buffer[Expression] = new ArrayBuffer) | ||
extends Iterable[Expression] { | ||
|
||
// Note: this class supports Scala 2.12. A parallel source tree has a 2.13 implementation. | ||
|
||
protected def add(e: Expression): Unit = { | ||
if (!e.deterministic) { | ||
originals += e | ||
} else if (!baseSet.contains(e.canonicalized) ) { | ||
} else if (!baseSet.contains(e.canonicalized)) { | ||
baseSet.add(e.canonicalized) | ||
originals += e | ||
} | ||
} | ||
|
||
override def contains(elem: Expression): Boolean = baseSet.contains(elem.canonicalized) | ||
protected def remove(e: Expression): Unit = { | ||
if (e.deterministic) { | ||
baseSet --= baseSet.filter(_ == e.canonicalized) | ||
originals --= originals.filter(_.canonicalized == e.canonicalized) | ||
} | ||
} | ||
|
||
def contains(elem: Expression): Boolean = baseSet.contains(elem.canonicalized) | ||
|
||
override def filter(p: Expression => Boolean): ExpressionSet = { | ||
val newBaseSet = baseSet.filter(e => p(e.canonicalized)) | ||
val newOriginals = originals.filter(e => p(e.canonicalized)) | ||
new ExpressionSet(newBaseSet, newOriginals) | ||
} | ||
|
||
override def filterNot(p: Expression => Boolean): ExpressionSet = { | ||
val newBaseSet = baseSet.filterNot(e => p(e.canonicalized)) | ||
val newOriginals = originals.filterNot(e => p(e.canonicalized)) | ||
new ExpressionSet(newBaseSet, newOriginals) | ||
} | ||
|
||
override def +(elem: Expression): ExpressionSet = { | ||
val newSet = new ExpressionSet(baseSet.clone(), originals.clone()) | ||
def +(elem: Expression): ExpressionSet = { | ||
val newSet = clone() | ||
newSet.add(elem) | ||
newSet | ||
} | ||
|
||
override def ++(elems: GenTraversableOnce[Expression]): ExpressionSet = { | ||
val newSet = new ExpressionSet(baseSet.clone(), originals.clone()) | ||
def ++(elems: GenTraversableOnce[Expression]): ExpressionSet = { | ||
val newSet = clone() | ||
elems.foreach(newSet.add) | ||
newSet | ||
} | ||
|
||
override def -(elem: Expression): ExpressionSet = { | ||
if (elem.deterministic) { | ||
val newBaseSet = baseSet.clone().filterNot(_ == elem.canonicalized) | ||
val newOriginals = originals.clone().filterNot(_.canonicalized == elem.canonicalized) | ||
new ExpressionSet(newBaseSet, newOriginals) | ||
} else { | ||
new ExpressionSet(baseSet.clone(), originals.clone()) | ||
} | ||
def -(elem: Expression): ExpressionSet = { | ||
val newSet = clone() | ||
newSet.remove(elem) | ||
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. Isn't this more efficient?: ExpressionSet(baseSet.filter(_ != e. canonicalized), originals.filter(_.canonicalized != e.canonicalized)) |
||
newSet | ||
} | ||
|
||
def --(elems: GenTraversableOnce[Expression]): ExpressionSet = { | ||
val newSet = clone() | ||
elems.foreach(newSet.remove) | ||
newSet | ||
} | ||
|
||
override def iterator: Iterator[Expression] = originals.iterator | ||
def map(f: Expression => Expression): ExpressionSet = { | ||
val newSet = new ExpressionSet() | ||
this.iterator.foreach(elem => newSet.add(f(elem))) | ||
newSet | ||
} | ||
|
||
def flatMap(f: Expression => Iterable[Expression]): ExpressionSet = { | ||
val newSet = new ExpressionSet() | ||
this.iterator.foreach(f(_).foreach(newSet.add)) | ||
newSet | ||
} | ||
|
||
def iterator: Iterator[Expression] = originals.iterator | ||
|
||
def union(that: ExpressionSet): ExpressionSet = { | ||
val newSet = clone() | ||
that.iterator.foreach(newSet.add) | ||
newSet | ||
} | ||
|
||
def subsetOf(that: ExpressionSet): Boolean = this.iterator.forall(that.contains) | ||
|
||
def intersect(that: ExpressionSet): ExpressionSet = this.filter(that.contains) | ||
|
||
def diff(that: ExpressionSet): ExpressionSet = this -- that | ||
|
||
def apply(elem: Expression): Boolean = this.contains(elem) | ||
|
||
override def equals(obj: Any): Boolean = obj match { | ||
case other: ExpressionSet => this.baseSet == other.baseSet | ||
case _ => false | ||
} | ||
|
||
override def hashCode(): Int = baseSet.hashCode() | ||
|
||
override def clone(): ExpressionSet = new ExpressionSet(baseSet.clone(), originals.clone()) | ||
|
||
/** | ||
* Returns a string containing both the post [[Canonicalize]] expressions and the original | ||
|
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
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.
We should apply the same change in
ExpressionSet
under thescala-2.13
source tree. @dbaliafroozeh can you open a followup PR?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.
@cloud-fan good catch, I thought I already deleted the ExpressionSet in 2.13. Note that we don't want it anymore as ExpressionSet doesn't extend Set anymore. I'll open a followup PR for that.