Skip to content
Snippets Groups Projects
Commit 68ccc6e1 authored by Yijie Shen's avatar Yijie Shen Committed by Yin Huai
Browse files

[SPARK-8930] [SQL] Throw a AnalysisException with meaningful messages if...

[SPARK-8930] [SQL] Throw a AnalysisException with meaningful messages if DataFrame#explode takes a star in expressions

Author: Yijie Shen <henry.yijieshen@gmail.com>

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

eae181d [Yijie Shen] change explaination message
54c9d11 [Yijie Shen] meaning message for * in explode
parent e9c36938
No related branches found
No related tags found
No related merge requests found
......@@ -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)
}
......@@ -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)))
......
......@@ -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}")
}
}
......@@ -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")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment