YannisSismanis commented on code in PR #41763:
URL: https://github.com/apache/spark/pull/41763#discussion_r1304830952
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/LogicalPlan.scala:
##########
@@ -324,6 +325,124 @@ object LogicalPlanIntegrity {
LogicalPlanIntegrity.hasUniqueExprIdsForOutput(plan))
}
+ /**
+ * This method validates there are no dangling attribute references.
+ * Returns an error message if the check does not pass, or None if it does
pass.
+ */
+ def validateNoDanglingReferences(plan: LogicalPlan): Option[String] = {
+ plan.collectFirst {
+ // DML commands and multi instance relations (like InMemoryRelation
caches)
+ // have different output semantics than typical queries.
+ case _: Command => None
+ case _: MultiInstanceRelation => None
+ case n if canGetOutputAttrs(n) =>
+ val inputExprIds = (n.children.flatMap(c => c.output ++
c.metadataOutput) ++ n.output)
+ .map(_.exprId).toSet
+ val danglingReferences = n.references.filter {
+ a => a.resolved && !inputExprIds.contains(a.exprId)
+ }.map(_.qualifiedName)
+ if (danglingReferences.nonEmpty) {
+ Some(s"Aliases ${danglingReferences.mkString(", ")} are dangling " +
+ s"in the references for plan:\n ${n.treeString}")
+ } else {
+ None
+ }
+ }.flatten
+ }
+
+ /**
+ * Validate that the grouping key types in Aggregate plans are valid.
+ * Returns an error message if the check fails, or None if it succeeds.
+ */
+ def validateGroupByTypes(plan: LogicalPlan): Option[String] = {
+ plan.collectFirst {
+ case a @ Aggregate(groupingExprs, _, _) =>
+ val badExprs =
groupingExprs.filter(_.dataType.isInstanceOf[MapType]).map(_.toString)
+ if (badExprs.nonEmpty) {
+ Some(s"Grouping expressions ${badExprs.mkString(", ")} cannot be of
type Map " +
+ s"for plan:\n ${a.treeString}")
+ } else {
+ None
+ }
+ }.flatten
+ }
+
+ /**
+ * Validate that the aggregation expressions in Aggregate plans are valid.
+ * Returns an error message if the check fails, or None if it succeeds.
+ */
+ def validateAggregateExpressions(plan: LogicalPlan): Option[String] = {
+ /**
+ * Returns true if all the exprIds, referenced in an Expression <subExpr>
+ * (used in the aggregate expressions of an Aggregate),
+ * are either used as measures (under an AggregateFunction) or are
explicitly grouped,
+ * given the <groupingExprIds> of all grouping expressions
+ * (used in the grouping expressions of the same Aggregate)
+
+ * I.e:
+ * <groupingExprIds> can be used "freely" in an AggregateExpressions, all
other exprIds can only
+ * be used under an AggregateFunction (i.e. aggregated).
+ */
+ def isAggregateSubExpressionValid(
+ groupingExpressions: Seq[Expression],
+ groupingExprIds: Set[ExprId],
+ subExpr: Expression): Boolean = {
+ // First collect all non-grouping ExprIds in <subExpr>.
+ val restrictedExprIds = subExpr.collect {
+ case g: Attribute if !groupingExprIds.contains(g.exprId) => g.exprId
+ }.groupBy(identity).mapValues(_.size)
+
+ // Second collect all non-grouping ExprIds under an AggregateFunction in
<subExpr>.
+ val restrictedExprIdsUnderAggregateFunction = subExpr.flatMap {
+ case a: AggregateExpression => a.collect {
+ case g: Attribute if !groupingExprIds.contains(g.exprId) => g.exprId
+ }
+ case _ => Seq.empty
+ }.groupBy(identity).mapValues(_.size)
+
+ // Validate a) that all non-grouping exprIds are used under some
aggregate function,
+ // i.e. all such exprIds are aggregated (measures) OR
+ // b) a grouping expression can be used in sub-expressions
+ // for aggregate expressions of an Aggregate.
+ restrictedExprIds == restrictedExprIdsUnderAggregateFunction ||
+ groupingExpressions.exists(_.semanticEquals(subExpr))
+ }
+
+ def isAggregateExpressionValid(groupingExpressions: Seq[Expression],
+ groupingExprIds: Set[ExprId],
+ expr: Expression): Boolean = {
+ isAggregateSubExpressionValid(groupingExpressions, groupingExprIds,
expr) ||
+ (expr.children.nonEmpty &&
+ expr.children.forall(isAggregateExpressionValid(groupingExpressions,
groupingExprIds, _)))
+ }
+
+ plan.collectFirst {
+ case a @ Aggregate(groupingExprs, aggrExprs, _) =>
+ // Collect all exprIds used in grouping keys that can be used in
aggregate expressions
+ // outside an aggregate function (like sum).
Review Comment:
Grouping expressions can be referenced in aggregate expressions either by
exprid (if the grouping expression corresponds to a named expression) or by
semantic equivalence. Grouping expressions can be used everywhere in aggregate
expressions (they do not need to be below an aggregation function), unlike
non-grouping expressions that can only be used under an aggregation function
(like sum).
In your example, `a`, `c` can be referenced everywhere in aggregate
expressions through exprid and `a+b`, `a*b` can be used everywhere through
semantic equivalence. I.e. something like `select c, (a*b), a+b, sum(c*m) from
... group by a, a+b, a*b as c` is valid. The check for the semantic equivalence
for expressions like (`a+b` or `a*b`) for each of the aggregate expressions
that appear in the select clause happens in line 408 above.
All other expressions (i.e. non-grouping expressions) can only be used under
an aggregation function in aggregate expressions.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]