Skip to content

Commit 029f9bd

Browse files
committed
Checks for missing attributes and unresolved operator for all types of operator
1 parent 9f3273b commit 029f9bd

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckAnalysis.scala

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class CheckAnalysis {
6363
s"filter expression '${f.condition.prettyString}' " +
6464
s"of type ${f.condition.dataType.simpleString} is not a boolean.")
6565

66-
case aggregatePlan@Aggregate(groupingExprs, aggregateExprs, child) =>
66+
case Aggregate(groupingExprs, aggregateExprs, child) =>
6767
def checkValidAggregateExpression(expr: Expression): Unit = expr match {
6868
case _: AggregateExpression => // OK
6969
case e: Attribute if !groupingExprs.contains(e) =>
@@ -85,13 +85,19 @@ class CheckAnalysis {
8585

8686
cleaned.foreach(checkValidAggregateExpression)
8787

88-
case o if o.children.nonEmpty && o.missingInput.nonEmpty =>
89-
val missingAttributes = o.missingInput.map(_.prettyString).mkString(",")
90-
val input = o.inputSet.map(_.prettyString).mkString(",")
88+
case _ => // Fallbacks to the following checks
89+
}
90+
91+
operator match {
92+
case o if o.children.nonEmpty &&
93+
!o.references.filter(_.name != "grouping__id").subsetOf(o.inputSet) =>
94+
val missingAttributes = (o.references -- o.inputSet).mkString(",")
95+
val input = o.inputSet.mkString(",")
9196

92-
failAnalysis(s"resolved attributes $missingAttributes missing from $input")
97+
failAnalysis(
98+
s"resolved attribute(s) $missingAttributes missing from $input " +
99+
s"in operator ${operator.simpleString}")
93100

94-
// Catch all
95101
case o if !o.resolved =>
96102
failAnalysis(
97103
s"unresolved operator ${operator.simpleString}")

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/AnalysisSuite.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,21 @@ class AnalysisSuite extends FunSuite with BeforeAndAfter {
199199
assert(pl(3).dataType == DecimalType.Unlimited)
200200
assert(pl(4).dataType == DoubleType)
201201
}
202+
203+
test("SPARK-6452: CheckAnalysis should throw when Aggregate contains missing attributes") {
204+
val plan =
205+
Aggregate(
206+
Nil,
207+
Alias(Sum(AttributeReference("a", StringType)(exprId = ExprId(1))), "b")() :: Nil,
208+
LocalRelation(
209+
AttributeReference("a", StringType)(exprId = ExprId(2))))
210+
211+
assert(plan.resolved)
212+
213+
val message = intercept[AnalysisException] {
214+
caseSensitiveAnalyze(plan)
215+
}.getMessage
216+
217+
assert(message.contains("resolved attribute(s) a#1 missing from a#2"))
218+
}
202219
}

0 commit comments

Comments
 (0)