|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.sql.catalyst.analysis.resolver |
| 19 | + |
| 20 | +import org.apache.spark.sql.catalyst.analysis.{ |
| 21 | + AnalysisErrorAt, |
| 22 | + AnsiTypeCoercion, |
| 23 | + CollationTypeCoercion, |
| 24 | + TypeCoercion |
| 25 | +} |
| 26 | +import org.apache.spark.sql.catalyst.expressions.Expression |
| 27 | +import org.apache.spark.sql.catalyst.expressions.aggregate.AggregateExpression |
| 28 | +import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, Filter, Project} |
| 29 | + |
| 30 | +/** |
| 31 | + * A resolver for [[AggregateExpression]]s which are introduced while resolving an |
| 32 | + * [[UnresolvedFunction]]. It is responsible for the following: |
| 33 | + * - Handling of the exceptions related to [[AggregateExpressions]]. |
| 34 | + * - Updating the [[ExpressionResolver.expressionResolutionContextStack]]. |
| 35 | + * - Applying type coercion rules to the [[AggregateExpressions]]s children. This is the only |
| 36 | + * resolution that we apply here as we already resolved the children of [[AggregateExpression]] |
| 37 | + * in the [[FunctionResolver]]. |
| 38 | + */ |
| 39 | +class AggregateExpressionResolver( |
| 40 | + expressionResolver: ExpressionResolver, |
| 41 | + timezoneAwareExpressionResolver: TimezoneAwareExpressionResolver) |
| 42 | + extends TreeNodeResolver[AggregateExpression, Expression] |
| 43 | + with ResolvesExpressionChildren { |
| 44 | + private val typeCoercionTransformations: Seq[Expression => Expression] = |
| 45 | + if (conf.ansiEnabled) { |
| 46 | + AggregateExpressionResolver.ANSI_TYPE_COERCION_TRANSFORMATIONS |
| 47 | + } else { |
| 48 | + AggregateExpressionResolver.TYPE_COERCION_TRANSFORMATIONS |
| 49 | + } |
| 50 | + |
| 51 | + private val typeCoercionResolver: TypeCoercionResolver = |
| 52 | + new TypeCoercionResolver(timezoneAwareExpressionResolver, typeCoercionTransformations) |
| 53 | + |
| 54 | + private val expressionResolutionContextStack = |
| 55 | + expressionResolver.getExpressionResolutionContextStack |
| 56 | + |
| 57 | + /** |
| 58 | + * Resolves the given [[AggregateExpression]] by applying: |
| 59 | + * - Type coercion rules |
| 60 | + * - Validity checks. Those include: |
| 61 | + * - Whether the [[AggregateExpression]] is under a valid operator. |
| 62 | + * - Whether there is a nested [[AggregateExpression]]. |
| 63 | + * - Whether there is a nondeterministic child. |
| 64 | + * - Updates to the [[ExpressionResolver.expressionResolutionContextStack]] |
| 65 | + */ |
| 66 | + override def resolve(aggregateExpression: AggregateExpression): Expression = { |
| 67 | + val aggregateExpressionWithTypeCoercion = |
| 68 | + withResolvedChildren(aggregateExpression, typeCoercionResolver.resolve) |
| 69 | + |
| 70 | + throwIfNotUnderValidOperator(aggregateExpression) |
| 71 | + throwIfNestedAggregateExists(aggregateExpressionWithTypeCoercion) |
| 72 | + throwIfHasNondeterministicChildren(aggregateExpressionWithTypeCoercion) |
| 73 | + |
| 74 | + expressionResolutionContextStack |
| 75 | + .peek() |
| 76 | + .hasAggregateExpressionsInASubtree = true |
| 77 | + |
| 78 | + // There are two different cases that we handle regarding the value of the flag: |
| 79 | + // |
| 80 | + // - We have an attribute under an `AggregateExpression`: |
| 81 | + // {{{ SELECT COUNT(col1) FROM VALUES (1); }}} |
| 82 | + // In this case, value of the `hasAttributeInASubtree` flag should be `false` as it |
| 83 | + // indicates whether there is an attribute in the subtree that's not `AggregateExpression` |
| 84 | + // so we can throw the `MISSING_GROUP_BY` exception appropriately. |
| 85 | + // |
| 86 | + // - In the following example: |
| 87 | + // {{{ SELECT COUNT(*), col1 + 1 FROM VALUES (1); }}} |
| 88 | + // It would be `true` as described above. |
| 89 | + expressionResolutionContextStack.peek().hasAttributeInASubtree = false |
| 90 | + |
| 91 | + aggregateExpressionWithTypeCoercion |
| 92 | + } |
| 93 | + |
| 94 | + private def throwIfNotUnderValidOperator(aggregateExpression: AggregateExpression): Unit = { |
| 95 | + expressionResolver.getParentOperator.get match { |
| 96 | + case _: Aggregate | _: Project => |
| 97 | + case filter: Filter => |
| 98 | + filter.failAnalysis( |
| 99 | + errorClass = "INVALID_WHERE_CONDITION", |
| 100 | + messageParameters = Map( |
| 101 | + "condition" -> toSQLExpr(filter.condition), |
| 102 | + "expressionList" -> Seq(aggregateExpression).mkString(", ") |
| 103 | + ) |
| 104 | + ) |
| 105 | + case other => |
| 106 | + other.failAnalysis( |
| 107 | + errorClass = "UNSUPPORTED_EXPR_FOR_OPERATOR", |
| 108 | + messageParameters = Map( |
| 109 | + "invalidExprSqls" -> Seq(aggregateExpression).mkString(", ") |
| 110 | + ) |
| 111 | + ) |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private def throwIfNestedAggregateExists(aggregateExpression: AggregateExpression): Unit = { |
| 116 | + if (expressionResolutionContextStack |
| 117 | + .peek() |
| 118 | + .hasAggregateExpressionsInASubtree) { |
| 119 | + aggregateExpression.failAnalysis( |
| 120 | + errorClass = "NESTED_AGGREGATE_FUNCTION", |
| 121 | + messageParameters = Map.empty |
| 122 | + ) |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private def throwIfHasNondeterministicChildren(aggregateExpression: AggregateExpression): Unit = { |
| 127 | + aggregateExpression.aggregateFunction.children.foreach(child => { |
| 128 | + if (!child.deterministic) { |
| 129 | + child.failAnalysis( |
| 130 | + errorClass = "AGGREGATE_FUNCTION_WITH_NONDETERMINISTIC_EXPRESSION", |
| 131 | + messageParameters = Map("sqlExpr" -> toSQLExpr(aggregateExpression)) |
| 132 | + ) |
| 133 | + } |
| 134 | + }) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +object AggregateExpressionResolver { |
| 139 | + // Ordering in the list of type coercions should be in sync with the list in [[TypeCoercion]]. |
| 140 | + private val TYPE_COERCION_TRANSFORMATIONS: Seq[Expression => Expression] = Seq( |
| 141 | + CollationTypeCoercion.apply, |
| 142 | + TypeCoercion.InTypeCoercion.apply, |
| 143 | + TypeCoercion.FunctionArgumentTypeCoercion.apply, |
| 144 | + TypeCoercion.IfTypeCoercion.apply, |
| 145 | + TypeCoercion.ImplicitTypeCoercion.apply |
| 146 | + ) |
| 147 | + |
| 148 | + // Ordering in the list of type coercions should be in sync with the list in [[AnsiTypeCoercion]]. |
| 149 | + private val ANSI_TYPE_COERCION_TRANSFORMATIONS: Seq[Expression => Expression] = Seq( |
| 150 | + CollationTypeCoercion.apply, |
| 151 | + AnsiTypeCoercion.InTypeCoercion.apply, |
| 152 | + AnsiTypeCoercion.FunctionArgumentTypeCoercion.apply, |
| 153 | + AnsiTypeCoercion.IfTypeCoercion.apply, |
| 154 | + AnsiTypeCoercion.ImplicitTypeCoercion.apply |
| 155 | + ) |
| 156 | +} |
0 commit comments