-
Notifications
You must be signed in to change notification settings - Fork 28.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-50734][SQL] Add catalog API for creating and registering SQL UDFs
### What changes were proposed in this pull request? This PR adds catalog APIs to support the creation and registration of SQL UDFs. It uses Hive Metastore to persist a SQL UDF by deserializing the function information into a FunctionResource and storing it in Hive (toCatalogFunction). During resolution, it retrieves the catalog function and deserializes it into a SQLFunction. This PR only adds the catalog API, and a subsequent PR will add the analyzer logic to resolve SQL UDFs. ### Why are the changes needed? To support SQL UDFs in Spark. ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? Existing tests. End to end tests will be added in the next PR once we support SQL UDF resolution. ### Was this patch authored or co-authored using generative AI tooling? No Closes #49389 from allisonwang-db/spark-50734-sql-udf-catalog-api. Authored-by: Allison Wang <[email protected]> Signed-off-by: Wenchen Fan <[email protected]>
- Loading branch information
1 parent
204c672
commit bba8cf4
Showing
11 changed files
with
624 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...atalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/SQLFunctionExpression.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* 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 | ||
|
||
import org.apache.spark.sql.catalyst.catalog.SQLFunction | ||
import org.apache.spark.sql.catalyst.expressions.{Expression, Unevaluable} | ||
import org.apache.spark.sql.catalyst.trees.TreePattern.{SQL_FUNCTION_EXPRESSION, TreePattern} | ||
import org.apache.spark.sql.types.DataType | ||
|
||
/** | ||
* Represent a SQL function expression resolved from the catalog SQL function builder. | ||
*/ | ||
case class SQLFunctionExpression( | ||
name: String, | ||
function: SQLFunction, | ||
inputs: Seq[Expression], | ||
returnType: Option[DataType]) extends Expression with Unevaluable { | ||
override def children: Seq[Expression] = inputs | ||
override def dataType: DataType = returnType.get | ||
override def nullable: Boolean = true | ||
override def prettyName: String = name | ||
override def toString: String = s"$name(${children.mkString(", ")})" | ||
override protected def withNewChildrenInternal( | ||
newChildren: IndexedSeq[Expression]): SQLFunctionExpression = copy(inputs = newChildren) | ||
final override val nodePatterns: Seq[TreePattern] = Seq(SQL_FUNCTION_EXPRESSION) | ||
} |
64 changes: 64 additions & 0 deletions
64
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/SQLFunctionNode.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* 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 | ||
|
||
import org.apache.spark.sql.catalyst.catalog.SQLFunction | ||
import org.apache.spark.sql.catalyst.expressions.{Attribute, Expression} | ||
import org.apache.spark.sql.catalyst.plans.logical.{LeafNode, LogicalPlan, UnaryNode} | ||
import org.apache.spark.sql.catalyst.trees.TreePattern.{FUNCTION_TABLE_RELATION_ARGUMENT_EXPRESSION, SQL_TABLE_FUNCTION, TreePattern} | ||
import org.apache.spark.sql.errors.DataTypeErrors.toSQLId | ||
import org.apache.spark.sql.errors.QueryCompilationErrors | ||
|
||
/** | ||
* A container for holding a SQL function query plan and its function identifier. | ||
* | ||
* @param function: the SQL function that this node represents. | ||
* @param child: the SQL function body. | ||
*/ | ||
case class SQLFunctionNode( | ||
function: SQLFunction, | ||
child: LogicalPlan) extends UnaryNode { | ||
override def output: Seq[Attribute] = child.output | ||
override def stringArgs: Iterator[Any] = Iterator(function.name, child) | ||
override protected def withNewChildInternal(newChild: LogicalPlan): SQLFunctionNode = | ||
copy(child = newChild) | ||
|
||
// Throw a reasonable error message when trying to call a SQL UDF with TABLE argument(s). | ||
if (child.containsPattern(FUNCTION_TABLE_RELATION_ARGUMENT_EXPRESSION)) { | ||
throw QueryCompilationErrors | ||
.tableValuedArgumentsNotYetImplementedForSqlFunctions("call", toSQLId(function.name.funcName)) | ||
} | ||
} | ||
|
||
/** | ||
* Represent a SQL table function plan resolved from the catalog SQL table function builder. | ||
*/ | ||
case class SQLTableFunction( | ||
name: String, | ||
function: SQLFunction, | ||
inputs: Seq[Expression], | ||
override val output: Seq[Attribute]) extends LeafNode { | ||
final override val nodePatterns: Seq[TreePattern] = Seq(SQL_TABLE_FUNCTION) | ||
|
||
// Throw a reasonable error message when trying to call a SQL UDF with TABLE argument(s) because | ||
// this functionality is not implemented yet. | ||
if (inputs.exists(_.containsPattern(FUNCTION_TABLE_RELATION_ARGUMENT_EXPRESSION))) { | ||
throw QueryCompilationErrors | ||
.tableValuedArgumentsNotYetImplementedForSqlFunctions("call", toSQLId(name)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.