-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-6145][SQL] fix ORDER BY on nested fields #5189
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
Show all changes
3 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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ import org.apache.spark.sql.catalyst.expressions._ | |
| import org.apache.spark.sql.catalyst.plans.QueryPlan | ||
| import org.apache.spark.sql.catalyst.trees.TreeNode | ||
| import org.apache.spark.sql.catalyst.trees | ||
| import org.apache.spark.sql.types.{ArrayType, StructType, StructField} | ||
|
|
||
|
|
||
| abstract class LogicalPlan extends QueryPlan[LogicalPlan] with Logging { | ||
|
|
@@ -109,16 +110,22 @@ abstract class LogicalPlan extends QueryPlan[LogicalPlan] with Logging { | |
| * nodes of this LogicalPlan. The attribute is expressed as | ||
| * as string in the following form: `[scope].AttributeName.[nested].[fields]...`. | ||
| */ | ||
| def resolveChildren(name: String, resolver: Resolver): Option[NamedExpression] = | ||
| resolve(name, children.flatMap(_.output), resolver) | ||
| def resolveChildren( | ||
| name: String, | ||
| resolver: Resolver, | ||
| throwErrors: Boolean = false): Option[NamedExpression] = | ||
| resolve(name, children.flatMap(_.output), resolver, throwErrors) | ||
|
|
||
| /** | ||
| * Optionally resolves the given string to a [[NamedExpression]] based on the output of this | ||
| * LogicalPlan. The attribute is expressed as string in the following form: | ||
| * `[scope].AttributeName.[nested].[fields]...`. | ||
| */ | ||
| def resolve(name: String, resolver: Resolver): Option[NamedExpression] = | ||
| resolve(name, output, resolver) | ||
| def resolve( | ||
| name: String, | ||
| resolver: Resolver, | ||
| throwErrors: Boolean = false): Option[NamedExpression] = | ||
| resolve(name, output, resolver, throwErrors) | ||
|
|
||
| /** | ||
| * Resolve the given `name` string against the given attribute, returning either 0 or 1 match. | ||
|
|
@@ -162,7 +169,8 @@ abstract class LogicalPlan extends QueryPlan[LogicalPlan] with Logging { | |
| protected def resolve( | ||
| name: String, | ||
| input: Seq[Attribute], | ||
| resolver: Resolver): Option[NamedExpression] = { | ||
| resolver: Resolver, | ||
| throwErrors: Boolean): Option[NamedExpression] = { | ||
|
|
||
| val parts = name.split("\\.") | ||
|
|
||
|
|
@@ -196,14 +204,19 @@ abstract class LogicalPlan extends QueryPlan[LogicalPlan] with Logging { | |
|
|
||
| // One match, but we also need to extract the requested nested field. | ||
| case Seq((a, nestedFields)) => | ||
| // The foldLeft adds UnresolvedGetField for every remaining parts of the name, | ||
| // and aliased it with the last part of the name. | ||
| // For example, consider name "a.b.c", where "a" is resolved to an existing attribute. | ||
| // Then this will add UnresolvedGetField("b") and UnresolvedGetField("c"), and alias | ||
| // the final expression as "c". | ||
| val fieldExprs = nestedFields.foldLeft(a: Expression)(UnresolvedGetField) | ||
| val aliasName = nestedFields.last | ||
| Some(Alias(fieldExprs, aliasName)()) | ||
| try { | ||
|
|
||
| // The foldLeft adds UnresolvedGetField for every remaining parts of the name, | ||
| // and aliased it with the last part of the name. | ||
| // For example, consider name "a.b.c", where "a" is resolved to an existing attribute. | ||
| // Then this will add UnresolvedGetField("b") and UnresolvedGetField("c"), and alias | ||
| // the final expression as "c". | ||
|
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. Update comment? Seems we will not add |
||
| val fieldExprs = nestedFields.foldLeft(a: Expression)(resolveGetField(_, _, resolver)) | ||
| val aliasName = nestedFields.last | ||
| Some(Alias(fieldExprs, aliasName)()) | ||
| } catch { | ||
| case a: AnalysisException if !throwErrors => None | ||
| } | ||
|
|
||
| // No matches. | ||
| case Seq() => | ||
|
|
@@ -212,11 +225,46 @@ abstract class LogicalPlan extends QueryPlan[LogicalPlan] with Logging { | |
|
|
||
| // More than one match. | ||
| case ambiguousReferences => | ||
| val referenceNames = ambiguousReferences.map(_._1.qualifiedName).mkString(", ") | ||
| val referenceNames = ambiguousReferences.map(_._1).mkString(", ") | ||
| throw new AnalysisException( | ||
| s"Reference '$name' is ambiguous, could be: $referenceNames.") | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns the resolved `GetField`, and report error if no desired field or over one | ||
| * desired fields are found. | ||
| * | ||
| * TODO: this code is duplicated from Analyzer and should be refactored to avoid this. | ||
| */ | ||
| protected def resolveGetField( | ||
| expr: Expression, | ||
| fieldName: String, | ||
| resolver: Resolver): Expression = { | ||
| def findField(fields: Array[StructField]): Int = { | ||
| val checkField = (f: StructField) => resolver(f.name, fieldName) | ||
| val ordinal = fields.indexWhere(checkField) | ||
| if (ordinal == -1) { | ||
| throw new AnalysisException( | ||
| s"No such struct field $fieldName in ${fields.map(_.name).mkString(", ")}") | ||
| } else if (fields.indexWhere(checkField, ordinal + 1) != -1) { | ||
| throw new AnalysisException( | ||
| s"Ambiguous reference to fields ${fields.filter(checkField).mkString(", ")}") | ||
| } else { | ||
| ordinal | ||
| } | ||
| } | ||
| expr.dataType match { | ||
| case StructType(fields) => | ||
| val ordinal = findField(fields) | ||
| StructGetField(expr, fields(ordinal), ordinal) | ||
| case ArrayType(StructType(fields), containsNull) => | ||
| val ordinal = findField(fields) | ||
| ArrayGetField(expr, fields(ordinal), ordinal, containsNull) | ||
| case otherType => | ||
| throw new AnalysisException(s"GetField is not valid on fields of type $otherType") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
||
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
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.
Even there is no missing in project, we still need to build new
Sortwith resolvedSortOrder.