-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-7067][SQL] fix bug when use complex nested fields in ORDER BY #5659
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 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Next
Next commit
fix SPARK-7067
- Loading branch information
commit 1fc41a2eead35ac03f207c166cb29b52e29c4a6c
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 |
|---|---|---|
|
|
@@ -336,9 +336,37 @@ class Analyzer( | |
| } | ||
| j.copy(right = newRight) | ||
|
|
||
| // When resolve `SortOrder`s in Sort based on child, don't report errors as | ||
| // we still have chance to resolve it based on grandchild | ||
| case s @ Sort(ordering, global, child) => | ||
| var changed = false | ||
| val newOrdering = ordering.map { order => | ||
| // Resolve SortOrder in one round, or fail and return the origin one. | ||
| try { | ||
| val newOrder = order transformUp { | ||
| case u @ UnresolvedAttribute(nameParts) => | ||
| child.resolve(nameParts, resolver).getOrElse(u) | ||
| case UnresolvedExtractValue(child, fieldName) if child.resolved => | ||
| ExtractValue(child, fieldName, resolver) | ||
| } | ||
| if (!newOrder.fastEquals(order)) { | ||
| changed = true | ||
| } | ||
| newOrder.asInstanceOf[SortOrder] | ||
| } catch { | ||
| case a: AnalysisException => order | ||
| } | ||
| } | ||
|
|
||
| if (changed) { | ||
| Sort(newOrdering, global, child) | ||
| } else { | ||
| s | ||
| } | ||
|
|
||
| case q: LogicalPlan => | ||
| logTrace(s"Attempting to resolve ${q.simpleString}") | ||
| q transformExpressionsUp { | ||
| q transformExpressionsUp { | ||
| case u @ UnresolvedAttribute(nameParts) if nameParts.length == 1 && | ||
| resolver(nameParts(0), VirtualColumn.groupingIdName) && | ||
| q.isInstanceOf[GroupingAnalytics] => | ||
|
|
@@ -424,6 +452,7 @@ class Analyzer( | |
| Sort(withAggsRemoved, global, | ||
| Aggregate(grouping, aggs ++ missing, child))) | ||
| } else { | ||
| logDebug(s"Failed to find $missing in ${a.output.mkString(", ")}") | ||
| s // Nothing we can do here. Return original plan. | ||
| } | ||
| } | ||
|
|
@@ -437,31 +466,31 @@ class Analyzer( | |
| ordering: Seq[SortOrder], | ||
| child: LogicalPlan, | ||
| grandchild: LogicalPlan): (Seq[SortOrder], Seq[Attribute]) = { | ||
| // Find any attributes that remain unresolved in the sort. | ||
| val unresolved: Seq[Seq[String]] = | ||
| ordering.flatMap(_.collect { case UnresolvedAttribute(nameParts) => nameParts }) | ||
|
|
||
| // Create a map from name, to resolved attributes, when the desired name can be found | ||
| // prior to the projection. | ||
| val resolved: Map[Seq[String], NamedExpression] = | ||
| unresolved.flatMap(u => grandchild.resolve(u, resolver).map(a => u -> a)).toMap | ||
| // Store `SortOrder`s we resolved based on grandchild | ||
| val resolved = scala.collection.mutable.ListBuffer.empty[Expression] | ||
|
|
||
| val resolvedOrdering = ordering.map { order => | ||
|
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. can this just be a map without the mutable list buffer? again, i'm not really worried about performance here. |
||
| val newOrder = order transformUp { | ||
| case u @ UnresolvedAttribute(nameParts) => | ||
| grandchild.resolve(nameParts, resolver).getOrElse(u) | ||
| case UnresolvedExtractValue(child, fieldName) if child.resolved => | ||
| ExtractValue(child, fieldName, resolver) | ||
| } | ||
| if (!newOrder.fastEquals(order) && newOrder.resolved) { | ||
| resolved += newOrder | ||
| } | ||
| newOrder.asInstanceOf[SortOrder] | ||
| } | ||
|
|
||
| // Construct a set that contains all of the attributes that we need to evaluate the | ||
| // ordering. | ||
| val requiredAttributes = AttributeSet(resolved.values) | ||
| val requiredAttributes = AttributeSet(resolved) | ||
|
|
||
| // Figure out which ones are missing from the projection, so that we can add them and | ||
| // remove them after the sort. | ||
| val missingInProject = requiredAttributes -- child.output | ||
|
|
||
| // Now that we have all the attributes we need, reconstruct a resolved ordering. | ||
| // It is important to do it here, instead of waiting for the standard resolved as adding | ||
| // attributes to the project below can actually introduce ambiquity that was not present | ||
| // before. | ||
| val resolvedOrdering = ordering.map(_ transform { | ||
| case u @ UnresolvedAttribute(name) => resolved.getOrElse(name, u) | ||
| }).asInstanceOf[Seq[SortOrder]] | ||
|
|
||
| (resolvedOrdering, missingInProject.toSeq) | ||
| } | ||
| } | ||
|
|
||
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
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
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
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.
Two suggestions here:
changedoptimization. The rule executor and transform already do checks to avoid churn when the plan does not change. Either way, I think its better to keep rules simple even if there is a small performance penalty.