vladimirg-db commented on code in PR #49029: URL: https://github.com/apache/spark/pull/49029#discussion_r1878032435
########## sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/resolver/LimitExpressionResolver.scala: ########## @@ -0,0 +1,114 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.spark.sql.catalyst.analysis.resolver + +import org.apache.spark.sql.AnalysisException +import org.apache.spark.sql.catalyst.expressions.Expression +import org.apache.spark.sql.errors.QueryErrorsBase +import org.apache.spark.sql.types.IntegerType + +/** + * The [[LimitExpressionResolver]] is a resolver that resolves a [[LocalLimit]] or [[GlobalLimit]] + * expression and performs all the necessary validation. + */ +class LimitExpressionResolver(expressionResolver: ExpressionResolverBase[Expression]) + extends ExpressionResolverBase[Expression] + with QueryErrorsBase { + + /** + * Resolve a limit expression of [[GlobalLimit]] or [[LocalLimit]] and perform validation. + */ + override def resolve(unresolvedLimitExpression: Expression): Expression = { + val resolvedLimitExpression = expressionResolver.resolve(unresolvedLimitExpression) + validateLimitExpression(resolvedLimitExpression, expressionName = "limit") + resolvedLimitExpression + } + + /** + * Validate a resolved limit expression of [[GlobalLimit]] or [[LocalLimit]]: + * - The expression has to be foldable + * - The result data type has to be [[IntegerType]] + * - The evaluated expression has to be non-null + * - The evaluated expression has to be positive + * + * The `foldable` check is implemented in some expressions + * as a recursive expression tree traversal. + * It is not an ideal approach for the single-pass [[ExpressionResolver]], + * but __is__ practical, since: + * - We have to call `eval` here anyway, and it's recursive + * - In practice `LIMIT` expression trees are very small + */ + private def validateLimitExpression(expression: Expression, expressionName: String): Unit = { + if (!expression.foldable) { + throwInvalidLimitLikeExpressionIsUnfoldable(expressionName, expression) + } + if (expression.dataType != IntegerType) { + throwInvalidLimitLikeExpressionDataType(expressionName, expression) + } + expression.eval() match { + case null => + throwInvalidLimitLikeExpressionIsNull(expressionName, expression) + case value: Int if value < 0 => + throwInvalidLimitLikeExpressionIsNegative(expressionName, expression, value) + case _ => + } + } + + private def throwInvalidLimitLikeExpressionIsUnfoldable( + name: String, + expression: Expression): Nothing = + throw new AnalysisException( + errorClass = "INVALID_LIMIT_LIKE_EXPRESSION.IS_UNFOLDABLE", + messageParameters = Map( + "name" -> name, Review Comment: That's not how it is in the legacy Analyzer [1]. I'm maintaining a compatibility here. [1] https://github.com/apache/spark/blob/master/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckAnalysis.scala#L111 -- 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]
