Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,8 @@ class Analyzer(
// attributes that its child might have or could have.
val missing = missingAttrs -- g.child.outputSet
g.copy(join = true, child = addMissingAttr(g.child, missing))
case d: Distinct =>
throw new AnalysisException(s"Can't add $missingAttrs to $d")
case u: UnaryNode =>
u.withNewChildren(addMissingAttr(u.child, missingAttrs) :: Nil)
case other =>
Expand Down
24 changes: 24 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,30 @@ class SQLQuerySuite extends QueryTest with SharedSQLContext {
)
}

test("SPARK-17863: SELECT distinct does not work correctly if order by missing attribute") {
checkAnswer(
sql("""select distinct struct.a, struct.b
|from (
| select named_struct('a', 1, 'b', 2, 'c', 3) as struct
| union all
| select named_struct('a', 1, 'b', 2, 'c', 4) as struct) tmp
|order by a, b
|""".stripMargin),
Row(1, 2) :: Nil)

val error = intercept[AnalysisException] {
sql("""select distinct struct.a, struct.b
|from (
| select named_struct('a', 1, 'b', 2, 'c', 3) as struct
| union all
| select named_struct('a', 1, 'b', 2, 'c', 4) as struct) tmp
|order by struct.a, struct.b
|""".stripMargin)
}
assert(error.message contains "cannot resolve '`struct.a`' given input columns: [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.

Is it possible to suggest the workaround in the error message?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What's the suggestion looks like?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, i see. Seems this error is thrown by check analysis, which does not have info about what is the query. So, it is hard to figure out what messages (for the workaround) to put at here.


}

test("cast boolean to string") {
// TODO Ensure true/false string letter casing is consistent with Hive in all cases.
checkAnswer(
Expand Down