Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

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 the bug of nested data type resolving in sort by
  • Loading branch information
chenghao-intel committed Mar 4, 2015
commit b82c8c5c8a743860b718f3a4bddeb08fd08bdcb8
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ class SqlParser extends AbstractSparkSQLParser {

protected lazy val dotExpressionHeader: Parser[Expression] =
(ident <~ ".") ~ ident ~ rep("." ~> ident) ^^ {
case i1 ~ i2 ~ rest => UnresolvedAttribute(i1 + "." + i2 + rest.mkString(".", ".", ""))
case i1 ~ i2 ~ rest => UnresolvedAttribute((Seq(i1, i2) ++ rest).mkString("."))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

val i1="a"
val i2="b"
val rest:Seq[String]=Nil
println(i1 + "." + i2 + rest.mkString(".", ".", ""))

outputs a.b., what we expect is a.b

Copy link
Contributor

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 mkString method on Nil

Copy link
Contributor

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("."))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

val i1="a"
val i2="b"
val rest:Seq[String]="c" :: Nil
println(i1 + "." + i2 + rest.mkString("."))

Outputs a.bc

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That make sense, thanks!

}

protected lazy val dataType: Parser[DataType] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,12 @@ class Analyzer(catalog: Catalog,
case s @ Sort(ordering, global, p @ Project(projectList, child))
if !s.resolved && p.resolved =>
val unresolved = ordering.flatMap(_.collect { case UnresolvedAttribute(name) => name })
// TODO child.resolve maybe not resolve all of the nested data at once
val resolved = unresolved.flatMap(child.resolve(_, resolver))
val requiredAttributes = AttributeSet(resolved.collect { case a: Attribute => a })
val requiredAttributes = AttributeSet(resolved.collect {
case a: Attribute => a
case a: NamedExpression if a.resolved == false => a
})

val missingInProject = requiredAttributes -- p.output
if (missingInProject.nonEmpty) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ class SQLQuerySuite extends QueryTest with BeforeAndAfterAll {
)
}

test("SPARK-6145 order by the nested data") {
sqlCtx.jsonRDD(sqlCtx.sparkContext.parallelize(
"""{"a": {"b": {"d": 1}}, "c": 1}""" :: Nil)).registerTempTable("nestedOrder")

checkAnswer(sqlCtx.sql("SELECT 1 FROM nestedOrder ORDER BY c"), Row(1))
checkAnswer(sqlCtx.sql("SELECT 1 FROM nestedOrder ORDER BY a.b.d"), Row(1))
checkAnswer(sqlCtx.sql("SELECT a.b.d FROM nestedOrder ORDER BY a.b.d"), Row(1))
}

test("grouping on nested fields") {
jsonRDD(sparkContext.parallelize("""{"nested": {"attribute": 1}, "value": 2}""" :: Nil))
.registerTempTable("rows")
Expand Down