Skip to content

Commit

Permalink
[SPARK-8930] [SQL] Throw a AnalysisException with meaningful messages…
Browse files Browse the repository at this point in the history
… if DataFrame#explode takes a star in expressions

Author: Yijie Shen <[email protected]>

Closes apache#8057 from yjshen/explode_star and squashes the following commits:

eae181d [Yijie Shen] change explaination message
54c9d11 [Yijie Shen] meaning message for * in explode

(cherry picked from commit 68ccc6e)
Signed-off-by: Yin Huai <[email protected]>
  • Loading branch information
yjshen authored and yhuai committed Aug 9, 2015
1 parent b12f073 commit 1ce5061
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ class Analyzer(
/**
* Returns true if `exprs` contains a [[Star]].
*/
protected def containsStar(exprs: Seq[Expression]): Boolean =
def containsStar(exprs: Seq[Expression]): Boolean =
exprs.exists(_.collect { case _: Star => true }.nonEmpty)
}

Expand Down Expand Up @@ -602,6 +602,8 @@ class Analyzer(
*/
object ResolveGenerate extends Rule[LogicalPlan] {
def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
case g: Generate if ResolveReferences.containsStar(g.generator.children) =>
failAnalysis("Cannot explode *, explode can only be applied on a specific column.")
case p: Generate if !p.child.resolved || !p.generator.resolved => p
case g: Generate if !g.resolved =>
g.copy(generatorOutput = makeGeneratorOutput(g.generator, g.generatorOutput.map(_.name)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ trait AnalysisTest extends PlanTest {
val e = intercept[Exception] {
analyzer.checkAnalysis(analyzer.execute(inputPlan))
}
expectedErrors.forall(e.getMessage.contains)
assert(expectedErrors.map(_.toLowerCase).forall(e.getMessage.toLowerCase.contains),
s"Expected to throw Exception contains: ${expectedErrors.mkString(", ")}, " +
s"actually we get ${e.getMessage}")
}
}
15 changes: 15 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/DataFrameSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,21 @@ class DataFrameSuite extends QueryTest with SQLTestUtils {
)
}

test("SPARK-8930: explode should fail with a meaningful message if it takes a star") {
val df = Seq(("1", "1,2"), ("2", "4"), ("3", "7,8,9")).toDF("prefix", "csv")
val e = intercept[AnalysisException] {
df.explode($"*") { case Row(prefix: String, csv: String) =>
csv.split(",").map(v => Tuple1(prefix + ":" + v)).toSeq
}.queryExecution.assertAnalyzed()
}
assert(e.getMessage.contains(
"Cannot explode *, explode can only be applied on a specific column."))

df.explode('prefix, 'csv) { case Row(prefix: String, csv: String) =>
csv.split(",").map(v => Tuple1(prefix + ":" + v)).toSeq
}.queryExecution.assertAnalyzed()
}

test("explode alias and star") {
val df = Seq((Array("a"), 1)).toDF("a", "b")

Expand Down

0 comments on commit 1ce5061

Please sign in to comment.