YannisSismanis commented on code in PR #41763:
URL: https://github.com/apache/spark/pull/41763#discussion_r1305279784
##########
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 ||
Review Comment:
the validation needs to check the exprids; we've seen quite a few cases
where rewrites mess those up. But if I understand your example correctly about
performance, in the case where all aggregate expressions and grouping
expressions are just attribute references (i.e. there are no "new" expressions,
like `select a+1 ...` or `group by b+1`) one can further speedup the test.
Let me think a bit about that:
- If subExpr is just a single attribute reference, then the
restrictedExprIdsUnderAggregateFunction does not match anything (there is no
nested aggregation function in it), so there is no groupby really.
- The restrictedExprIds is at most a single groupby (the corresponding
exprid if it doesn't match the grouping exprid)
Both of this can probably be optimized using pattern matching indeed!
##########
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 ||
Review Comment:
the validation needs to check the exprids; we've seen quite a few cases
where rewrites mess those up. But if I understand your example correctly about
performance, in the case where all aggregate expressions and grouping
expressions are just attribute references (i.e. there are no "new" expressions,
like `select a+1 ...` or `group by b+1`) one can further speedup the test.
Let me think a bit about that:
- If subExpr is just a single attribute reference, then the
restrictedExprIdsUnderAggregateFunction does not match anything (there is no
nested aggregation function in it), so there is no groupby really.
- The restrictedExprIds is at most a single groupby (the corresponding
exprid if it doesn't match the grouping exprid)
Both of these can probably be optimized using pattern matching indeed!
--
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]