|
| 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 |
| 19 | + |
| 20 | +import org.apache.spark.sql.catalyst.SQLConfHelper |
| 21 | +import org.apache.spark.sql.catalyst.expressions.{Expression, LambdaFunction, NamedLambdaVariable} |
| 22 | +import org.apache.spark.sql.catalyst.util.TypeUtils.{toSQLConf, toSQLId} |
| 23 | +import org.apache.spark.sql.internal.SQLConf |
| 24 | +import org.apache.spark.sql.types.DataType |
| 25 | + |
| 26 | +/** |
| 27 | + * Object used to bind lambda function arguments to their types and validate lambda argument |
| 28 | + * constraints. |
| 29 | + * |
| 30 | + * This object creates a bound [[LambdaFunction]] by binding the arguments to the given type |
| 31 | + * information (dataType and nullability). The argument names come from the lambda function |
| 32 | + * itself. It handles three cases: |
| 33 | + * |
| 34 | + * 1. Already bound lambda functions: Returns the function as-is, assuming it has been |
| 35 | + * correctly bound to its arguments. |
| 36 | + * |
| 37 | + * 2. Unbound lambda functions: Validates and binds the function by: |
| 38 | + * - Checking that the number of arguments matches the expected count |
| 39 | + * - Checking for duplicate argument names (respecting case sensitivity configuration) |
| 40 | + * - Creating [[NamedLambdaVariable]] instances with the provided types |
| 41 | + * |
| 42 | + * 3. Non-lambda expressions: Wraps the expression in a lambda function with hidden arguments |
| 43 | + * (named `col0`, `col1`, etc.). This is used when an expression does not consume lambda |
| 44 | + * arguments but needs to be passed to a higher-order function. The arguments are hidden to |
| 45 | + * prevent accidental naming collisions. |
| 46 | + */ |
| 47 | +object LambdaBinder extends SQLConfHelper { |
| 48 | + |
| 49 | + /** |
| 50 | + * Binds lambda function arguments to their types and validates lambda argument constraints. |
| 51 | + */ |
| 52 | + def apply(expression: Expression, argumentsInfo: Seq[(DataType, Boolean)]): LambdaFunction = |
| 53 | + expression match { |
| 54 | + case f: LambdaFunction if f.bound => f |
| 55 | + |
| 56 | + case LambdaFunction(function, names, _) => |
| 57 | + if (names.size != argumentsInfo.size) { |
| 58 | + expression.failAnalysis( |
| 59 | + errorClass = "INVALID_LAMBDA_FUNCTION_CALL.NUM_ARGS_MISMATCH", |
| 60 | + messageParameters = Map( |
| 61 | + "expectedNumArgs" -> names.size.toString, |
| 62 | + "actualNumArgs" -> argumentsInfo.size.toString |
| 63 | + ) |
| 64 | + ) |
| 65 | + } |
| 66 | + |
| 67 | + if (names.map(a => conf.canonicalize(a.name)).distinct.size < names.size) { |
| 68 | + expression.failAnalysis( |
| 69 | + errorClass = "INVALID_LAMBDA_FUNCTION_CALL.DUPLICATE_ARG_NAMES", |
| 70 | + messageParameters = Map( |
| 71 | + "args" -> names.map(a => conf.canonicalize(a.name)).map(toSQLId(_)).mkString(", "), |
| 72 | + "caseSensitiveConfig" -> toSQLConf(SQLConf.CASE_SENSITIVE.key) |
| 73 | + ) |
| 74 | + ) |
| 75 | + } |
| 76 | + |
| 77 | + val arguments = argumentsInfo.zip(names).map { |
| 78 | + case ((dataType, nullable), ne) => |
| 79 | + NamedLambdaVariable(ne.name, dataType, nullable) |
| 80 | + } |
| 81 | + LambdaFunction(function, arguments) |
| 82 | + |
| 83 | + case _ => |
| 84 | + val arguments = argumentsInfo.zipWithIndex.map { |
| 85 | + case ((dataType, nullable), i) => |
| 86 | + NamedLambdaVariable(s"col$i", dataType, nullable) |
| 87 | + } |
| 88 | + LambdaFunction(expression, arguments, hidden = true) |
| 89 | + } |
| 90 | +} |
0 commit comments