-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-6145][SQL] fix ORDER BY on nested fields #4904
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 2 commits
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 |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
|
|
||
| package org.apache.spark.sql.catalyst.plans.logical | ||
|
|
||
| import org.apache.spark.sql.catalyst.analysis.Resolver | ||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.plans._ | ||
| import org.apache.spark.sql.types._ | ||
|
|
@@ -152,6 +153,18 @@ case class Sort( | |
| global: Boolean, | ||
| child: LogicalPlan) extends UnaryNode { | ||
| override def output = child.output | ||
|
|
||
| override def resolveChildren(name: String, resolver: Resolver) = { | ||
| val input = child match { | ||
| case Project(list, c) => list.filter { | ||
| case Alias(g: GetField, _) => false | ||
| case Alias(g: GetItem, _) => false | ||
|
||
| case _ => true | ||
| }.map(_.toAttribute) | ||
| case _ => child.flatMap(_.output) | ||
| } | ||
| resolve(name, input, resolver) | ||
| } | ||
| } | ||
|
|
||
| case class Aggregate( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1053,10 +1053,19 @@ class SQLQuerySuite extends QueryTest with BeforeAndAfterAll { | |
| test("SPARK-6145: ORDER BY test for nested fields") { | ||
| jsonRDD(sparkContext.makeRDD( | ||
| """{"a": {"b": 1, "a": {"a": 1}}, "c": [{"d": 1}]}""" :: Nil)).registerTempTable("nestedOrder") | ||
| // These should be successfully analyzed | ||
| sql("SELECT 1 FROM nestedOrder ORDER BY a.b").queryExecution.analyzed | ||
| sql("SELECT a.b FROM nestedOrder ORDER BY a.b").queryExecution.analyzed | ||
| sql("SELECT 1 FROM nestedOrder ORDER BY a.a.a").queryExecution.analyzed | ||
| sql("SELECT 1 FROM nestedOrder ORDER BY c[0].d").queryExecution.analyzed | ||
|
|
||
| checkAnswer(sql("SELECT 1 FROM nestedOrder ORDER BY a.b"), Row(1)) | ||
| checkAnswer(sql("SELECT a.b FROM nestedOrder ORDER BY a.b"), Row(1)) | ||
| checkAnswer(sql("SELECT 1 FROM nestedOrder ORDER BY a.a.a"), Row(1)) | ||
| checkAnswer(sql("SELECT a.a.a FROM nestedOrder ORDER BY a.a.a"), Row(1)) | ||
| checkAnswer(sql("SELECT 1 FROM nestedOrder ORDER BY c[0].d"), Row(1)) | ||
| checkAnswer(sql("SELECT c[0].d FROM nestedOrder ORDER BY c[0].d"), Row(1)) | ||
|
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 you try
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. Please ignore the above comment |
||
| } | ||
|
|
||
| test("SPARK-6145: special cases") { | ||
| jsonRDD(sparkContext.makeRDD( | ||
| """{"a": {"b": [1]}, "b": [{"a": 1}], "c0": {"a": 1}}""" :: Nil)).registerTempTable("t") | ||
| checkAnswer(sql("SELECT a.b[0] FROM t ORDER BY c0.a"), Row(1)) | ||
| checkAnswer(sql("SELECT b[0].a FROM t ORDER BY c0.a"), Row(1)) | ||
| } | ||
| } | ||
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.
This feels kind of hacky to me as its mixing the particulars of analysis for SQL into the logical plan. Could we instead just make resolve not do partial resolution, where we can resolve the base attribute but not the GetFields that are on top. I think this change is the root cause of the regression.