-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-6145] [SQL] Fix the bug of nested data type resolving in ORDER BY #4892
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
b82c8c5
44ac17d
47db754
5de3b9e
73eb346
9209ac1
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 |
|---|---|---|
|
|
@@ -19,11 +19,12 @@ package org.apache.spark.sql.catalyst.plans.logical | |
|
|
||
| import org.apache.spark.Logging | ||
| import org.apache.spark.sql.AnalysisException | ||
| import org.apache.spark.sql.catalyst.analysis.{UnresolvedGetField, Resolver} | ||
| import org.apache.spark.sql.catalyst.analysis.Resolver | ||
| 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 { | ||
|
|
@@ -192,14 +193,17 @@ 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)()) | ||
| // The foldLeft will resolve all of the nested data type, to get its attributes. | ||
| val fieldExprs = nestedFields.foldLeft(a: Expression) { case (e, fieldName) => | ||
| resolveGetField(e, fieldName, resolver) | ||
| } | ||
|
|
||
| // TODO the alias name is quite tricky to me, set it to _col1, _col2.. ? | ||
| // Set it as original attribute name like "a.b.c" seems still confusing, | ||
| // and we may never reference this column by its name (with "."), except | ||
| // people write SQL like: SELECT a.b.c as newCol FROM nestedTable, which | ||
| // explicitly specifying the alias name for the output column | ||
| Some(Alias(fieldExprs, name)()) | ||
|
Member
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. The python test failure is caused by replacing
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. Thank you @viirya I've updated the python code.
Member
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. I meant should it be that? In Hive it should be
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. I am not so sure how Hive handle that, but it can not be e.g.
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. I don't think we can change the default alias when extracting nested fields. I believe we match hive behaviors now, and this would break existing queries.
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, I agree we shouldn't break the existed logic, but I believe this is a bug of Hive. I am wondering if we can break the naming rule of Hive for nested data type references, which always causes ambiguous.
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. How is this a bug? These are pretty contrived examples. How often do you actually have nested structures where the outside name is the same as the inside name?
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. Yea, I shouldn't say "always", but "possible", it maybe quite often while with |
||
|
|
||
| // No matches. | ||
| case Seq() => | ||
|
|
@@ -212,6 +216,36 @@ abstract class LogicalPlan extends QueryPlan[LogicalPlan] with Logging { | |
| s"Ambiguous references to $name: ${ambiguousReferences.mkString(",")}") | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns the resolved `GetField`, and report error if no desired field or over one | ||
| * desired fields are found. | ||
| */ | ||
| 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") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
||
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.
outputs
a.b., what we expect isa.bThere 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 was my mistake...didn't test the
mkStringmethod onNilThere 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.
how about
UnresolvedAttribute(i1 + "." + i2 + rest.mkString("."))
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.
Outputs
a.bcThere 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.
That make sense, thanks!