You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "cloud-fan (via GitHub)" <gi...@apache.org> on 2025/12/27 13:34:28 UTC
[PR] [SPARK-54856][SQL] Refactor scalar function lookup and resolution [spark]
cloud-fan opened a new pull request, #53627:
URL: https://github.com/apache/spark/pull/53627
<!--
Thanks for sending a pull request! Here are some tips for you:
1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
4. Be sure to keep the PR description updated to reflect all changes.
5. Please write your PR title to summarize what this PR proposes.
6. If possible, provide a concise example to reproduce the issue for a faster review.
7. If you want to add a new configuration, please read the guideline first for naming configurations in
'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
8. If you want to add or modify an error type or message, please read the guideline first in
'common/utils/src/main/resources/error/README.md'.
-->
### What changes were proposed in this pull request?
<!--
Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue.
If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
2. If you fix some SQL features, you can provide some references of other DBMSes.
3. If there is design documentation, please add the link.
4. If there is a discussion in the mailing list, please add the link.
-->
This PR adds `loadPersistentScalarFunction` method to `SessionCatalog`, to unify the scalar function lookup and resolution between v1 and v2 catalogs. The key difference between v1 and v2 catalogs about function is:
- v1 catalog provides different APIs for describing function and invoking function to do function lookup: `lookupPersistentFunction` and `resolvePersistentFunction`.
- v2 catalog only has one single lookup API: `loadFunction`. This API returns `UnboundFunction`, for invocation, it needs to enter the second phase and call `UnboundFunction.bind`.
The newly added `loadPersistentScalarFunction` method follows the v2 catalog style and handles function invocation (load resources and create function builder) in a second phase.
### Why are the changes needed?
<!--
Please clarify why the changes are needed. For instance,
1. If you propose a new API, clarify the use case for a new API.
2. If you fix a bug, you can clarify why it is a bug.
-->
Code cleanup, also also avoid redundant function lookup from catalog introduced by https://github.com/apache/spark/pull/53531
### Does this PR introduce _any_ user-facing change?
<!--
Note that it means *any* user-facing change including all aspects such as new features, bug fixes, or other behavior changes. Documentation-only updates are not considered user-facing changes.
If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
If no, write 'No'.
-->
No
### How was this patch tested?
<!--
If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
If tests were not added, please describe why they were not added and/or why it was difficult to add.
If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
-->
existing tests
### Was this patch authored or co-authored using generative AI tooling?
<!--
If generative AI tooling has been used in the process of authoring this patch, please include the
phrase: 'Generated-by: ' followed by the name of the tool and its version.
If no, write 'No'.
Please refer to the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
-->
cursor 2.2.44
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-54856][SQL] Refactor scalar function lookup and resolution [spark]
Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on PR #53627:
URL: https://github.com/apache/spark/pull/53627#issuecomment-3695596313
merged to master
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-54856][SQL] Refactor scalar function lookup and resolution [spark]
Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #53627:
URL: https://github.com/apache/spark/pull/53627#discussion_r2649220146
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/connector/V1Function.scala:
##########
@@ -17,15 +17,79 @@
package org.apache.spark.sql.internal.connector
-import org.apache.spark.SparkUnsupportedOperationException
-import org.apache.spark.sql.catalyst.expressions.ExpressionInfo
+import org.apache.spark.SparkException
+import org.apache.spark.sql.catalyst.analysis.FunctionRegistry.FunctionBuilder
+import org.apache.spark.sql.catalyst.expressions.{Expression, ExpressionInfo}
import org.apache.spark.sql.connector.catalog.functions.{BoundFunction, UnboundFunction}
import org.apache.spark.sql.types.StructType
-case class V1Function(info: ExpressionInfo) extends UnboundFunction {
+/**
+ * A wrapper for V1 scalar functions that mirrors the V2 UnboundFunction pattern.
+ *
+ * V1Function has two responsibilities:
+ * 1. Provide ExpressionInfo for DESCRIBE FUNCTION (accessed via `info`)
+ * 2. Provide a function builder for invocation (accessed via `invoke()`)
+ *
+ * The key design is two-phase lazy loading:
+ * - `info` is computed eagerly or from cache (no resource loading)
+ * - `functionBuilder` is computed lazily on first `invoke()` (triggers resource loading)
+ *
+ * This matches V1 behavior where DESCRIBE doesn't load resources but invocation does.
+ */
+class V1Function private (
+ val info: ExpressionInfo,
+ builderFactory: () => FunctionBuilder) extends UnboundFunction {
+
+ /**
+ * Lazy function builder - only computed on first invoke().
+ * For persistent functions, this triggers resource loading.
+ */
+ private lazy val functionBuilder: FunctionBuilder = builderFactory()
+
+ /**
+ * Invoke the function with the given arguments.
+ * This is the V1 equivalent of V2's bind() pattern.
+ * For persistent functions, first invocation triggers resource loading.
+ */
+ def invoke(arguments: Seq[Expression]): Expression = functionBuilder(arguments)
+
+ // UnboundFunction interface
override def bind(inputType: StructType): BoundFunction = {
- throw new SparkUnsupportedOperationException("_LEGACY_ERROR_TEMP_3110")
+ // V1 functions don't use the V2 bind() pattern - they use invoke() instead
+ throw SparkException.internalError("V1Function.bind() should not be called")
}
+
override def name(): String = info.getName
+
override def description(): String = info.getUsage
}
+
+object V1Function {
+ /** A placeholder builder for metadata-only V1Functions that throws on invocation. */
+ private val metadataOnlyBuilder: FunctionBuilder = _ =>
+ throw SparkException.internalError(
+ "Metadata-only V1Function should not be invoked")
+
+ /**
+ * Create a V1Function with eager info and lazy builder.
+ * The builderFactory is called on first invoke().
+ */
+ def apply(info: ExpressionInfo, builderFactory: () => FunctionBuilder): V1Function = {
+ new V1Function(info, builderFactory)
+ }
+
+ /**
+ * Create a V1Function with eager info and builder (for built-in/temp/cached functions).
+ */
+ def apply(info: ExpressionInfo, builder: FunctionBuilder): V1Function = {
+ new V1Function(info, () => builder)
+ }
+
+ /**
+ * Create a metadata-only V1Function (for DESCRIBE FUNCTION).
+ * If invoke() is called, it will throw an error.
+ */
+ def apply(info: ExpressionInfo): V1Function = {
Review Comment:
after this change, calling `V1Function(info)` would go from the constructor to here, maybe it should use a different name to avoid abuse (especially by external plugins), or just remove it and require the caller to use `metadataOnlyBuilder` explicitly
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-54856][SQL] Refactor scalar function lookup and resolution [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #53627:
URL: https://github.com/apache/spark/pull/53627#discussion_r2649403502
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalog.scala:
##########
@@ -2153,122 +2153,122 @@ class SessionCatalog(
}
/**
- * Look up the `ExpressionInfo` of the given function by name if it's a persistent function.
- * This supports both scalar and table functions.
+ * Look up a persistent function's ExpressionInfo by name (for DESCRIBE FUNCTION).
+ * This only fetches metadata without loading resources or creating builders.
*/
def lookupPersistentFunction(name: FunctionIdentifier): ExpressionInfo = {
val qualifiedIdent = qualifyIdentifier(name)
- val db = qualifiedIdent.database.get
- val funcName = qualifiedIdent.funcName
+ // Check if already cached in either registry
functionRegistry.lookupFunction(qualifiedIdent)
.orElse(tableFunctionRegistry.lookupFunction(qualifiedIdent))
.getOrElse {
- requireDbExists(db)
- if (externalCatalog.functionExists(db, funcName)) {
- val metadata = externalCatalog.getFunction(db, funcName)
- if (metadata.isUserDefinedFunction) {
- UserDefinedFunction.fromCatalogFunction(metadata, parser).toExpressionInfo
- } else {
- makeExprInfoForHiveFunction(metadata.copy(identifier = qualifiedIdent))
- }
+ val funcMetadata = fetchCatalogFunction(qualifiedIdent)
+ if (funcMetadata.isUserDefinedFunction) {
+ UserDefinedFunction.fromCatalogFunction(funcMetadata, parser).toExpressionInfo
} else {
- failFunctionLookup(name)
+ makeExprInfoForHiveFunction(funcMetadata)
}
}
}
/**
- * Look up a persistent scalar function by name and resolves it to an Expression.
+ * Load a persistent scalar function by name.
+ * Returns V1Function with:
+ * - Eager info (from cache or catalog fetch, no resource loading)
+ * - Lazy builder (resource loading only on first invoke)
+ *
+ * This matches V1 behavior where DESCRIBE doesn't load resources.
*/
- def resolvePersistentFunction(
- name: FunctionIdentifier, arguments: Seq[Expression]): Expression = {
- resolvePersistentFunctionInternal[Expression](
- name,
- arguments,
- functionRegistry,
- registerHiveFunc = func =>
- registerFunction(
- func,
- overrideIfExists = false,
- registry = functionRegistry,
- functionBuilder = makeFunctionBuilder(func)
- ),
- registerUserDefinedFunc = function => {
- val builder = makeUserDefinedScalarFuncBuilder(function)
- registerUserDefinedFunction[Expression](
- function = function,
- overrideIfExists = false,
- registry = functionRegistry,
- functionBuilder = builder)
- }
- )
+ def loadPersistentScalarFunction(name: FunctionIdentifier): V1Function = {
+ val qualifiedIdent = qualifyIdentifier(name)
+
+ // Check cache first (no synchronization needed for reads)
+ functionRegistry.lookupFunctionEntry(qualifiedIdent) match {
+ case Some((cachedInfo, cachedBuilder)) =>
+ // Already cached - return with eager builder
+ V1Function(cachedInfo, cachedBuilder)
+
+ case None =>
+ // Fetch metadata eagerly (no resource loading yet)
+ val funcMetadata = fetchCatalogFunction(qualifiedIdent)
+ val info = if (funcMetadata.isUserDefinedFunction) {
+ UserDefinedFunction.fromCatalogFunction(funcMetadata, parser).toExpressionInfo
+ } else {
+ makeExprInfoForHiveFunction(funcMetadata)
+ }
+
+ // Builder factory - loads resources only on first invoke()
+ val builderFactory: () => FunctionBuilder = () => synchronized {
+ // Re-check cache (another thread may have loaded it)
+ functionRegistry.lookupFunctionBuilder(qualifiedIdent).getOrElse {
+ loadFunctionResources(funcMetadata.resources)
+ if (funcMetadata.isUserDefinedFunction) {
+ val udf = UserDefinedFunction.fromCatalogFunction(funcMetadata, parser)
+ registerUserDefinedFunction[Expression](
+ udf,
+ overrideIfExists = false,
+ functionRegistry,
+ makeUserDefinedScalarFuncBuilder(udf))
+ } else {
+ registerFunction(
+ funcMetadata,
+ overrideIfExists = false,
+ functionRegistry,
+ makeFunctionBuilder(funcMetadata))
+ }
+ functionRegistry.lookupFunctionBuilder(qualifiedIdent).get
+ }
+ }
+
+ V1Function(info, builderFactory)
+ }
}
/**
* Look up a persistent table function by name and resolves it to a LogicalPlan.
*/
def resolvePersistentTableFunction(
name: FunctionIdentifier,
- arguments: Seq[Expression]): LogicalPlan = {
- resolvePersistentFunctionInternal[LogicalPlan](
- name,
- arguments,
- tableFunctionRegistry,
- // We don't support persistent Hive table functions yet.
- registerHiveFunc = (func: CatalogFunction) => failFunctionLookup(name),
- registerUserDefinedFunc = function => {
- val builder = makeUserDefinedTableFuncBuilder(function)
- registerUserDefinedFunction[LogicalPlan](
- function = function,
- overrideIfExists = false,
- registry = tableFunctionRegistry,
- functionBuilder = builder)
+ arguments: Seq[Expression]): LogicalPlan = synchronized {
+ val qualifiedIdent = qualifyIdentifier(name)
+ if (tableFunctionRegistry.functionExists(qualifiedIdent)) {
+ // Already cached
+ tableFunctionRegistry.lookupFunction(qualifiedIdent, arguments)
+ } else {
+ // Load from catalog
+ val funcMetadata = fetchCatalogFunction(qualifiedIdent)
+ if (!funcMetadata.isUserDefinedFunction) {
+ // Hive table functions are not supported
+ failFunctionLookup(qualifiedIdent)
}
- )
+ loadFunctionResources(funcMetadata.resources)
+ val udf = UserDefinedFunction.fromCatalogFunction(funcMetadata, parser)
+ registerUserDefinedFunction[LogicalPlan](
+ udf,
+ overrideIfExists = false,
+ tableFunctionRegistry,
+ makeUserDefinedTableFuncBuilder(udf))
+ tableFunctionRegistry.lookupFunction(qualifiedIdent, arguments)
+ }
}
- private def resolvePersistentFunctionInternal[T](
- name: FunctionIdentifier,
- arguments: Seq[Expression],
- registry: FunctionRegistryBase[T],
- registerHiveFunc: CatalogFunction => Unit,
- registerUserDefinedFunc: UserDefinedFunction => Unit): T = {
- // `synchronized` is used to prevent multiple threads from concurrently resolving the
- // same function that has not yet been loaded into the function registry. This is needed
- // because calling `registerFunction` twice with `overrideIfExists = false` can lead to
- // a FunctionAlreadyExistsException.
- synchronized {
- val qualifiedIdent = qualifyIdentifier(name)
- val db = qualifiedIdent.database.get
- val funcName = qualifiedIdent.funcName
- if (registry.functionExists(qualifiedIdent)) {
- // This function has been already loaded into the function registry.
- registry.lookupFunction(qualifiedIdent, arguments)
Review Comment:
v2 function does not support table function yet, so scalar and table functions has different code paths:
- scalar function looks up the cache at https://github.com/apache/spark/pull/53627/files#diff-9dd0899e5406230aeff96654432da54f35255f6dc60eecb87264a5c508a8c826R2186
- table function looks up the cache at https://github.com/apache/spark/pull/53627/files#diff-9dd0899e5406230aeff96654432da54f35255f6dc60eecb87264a5c508a8c826R2236
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-54856][SQL] Refactor scalar function lookup and resolution [spark]
Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #53627:
URL: https://github.com/apache/spark/pull/53627#discussion_r2649215979
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalog.scala:
##########
@@ -2153,122 +2153,122 @@ class SessionCatalog(
}
/**
- * Look up the `ExpressionInfo` of the given function by name if it's a persistent function.
- * This supports both scalar and table functions.
+ * Look up a persistent function's ExpressionInfo by name (for DESCRIBE FUNCTION).
+ * This only fetches metadata without loading resources or creating builders.
*/
def lookupPersistentFunction(name: FunctionIdentifier): ExpressionInfo = {
val qualifiedIdent = qualifyIdentifier(name)
- val db = qualifiedIdent.database.get
- val funcName = qualifiedIdent.funcName
+ // Check if already cached in either registry
functionRegistry.lookupFunction(qualifiedIdent)
.orElse(tableFunctionRegistry.lookupFunction(qualifiedIdent))
.getOrElse {
- requireDbExists(db)
- if (externalCatalog.functionExists(db, funcName)) {
- val metadata = externalCatalog.getFunction(db, funcName)
- if (metadata.isUserDefinedFunction) {
- UserDefinedFunction.fromCatalogFunction(metadata, parser).toExpressionInfo
- } else {
- makeExprInfoForHiveFunction(metadata.copy(identifier = qualifiedIdent))
- }
+ val funcMetadata = fetchCatalogFunction(qualifiedIdent)
+ if (funcMetadata.isUserDefinedFunction) {
+ UserDefinedFunction.fromCatalogFunction(funcMetadata, parser).toExpressionInfo
} else {
- failFunctionLookup(name)
+ makeExprInfoForHiveFunction(funcMetadata)
}
}
}
/**
- * Look up a persistent scalar function by name and resolves it to an Expression.
+ * Load a persistent scalar function by name.
+ * Returns V1Function with:
+ * - Eager info (from cache or catalog fetch, no resource loading)
+ * - Lazy builder (resource loading only on first invoke)
+ *
+ * This matches V1 behavior where DESCRIBE doesn't load resources.
*/
- def resolvePersistentFunction(
- name: FunctionIdentifier, arguments: Seq[Expression]): Expression = {
- resolvePersistentFunctionInternal[Expression](
- name,
- arguments,
- functionRegistry,
- registerHiveFunc = func =>
- registerFunction(
- func,
- overrideIfExists = false,
- registry = functionRegistry,
- functionBuilder = makeFunctionBuilder(func)
- ),
- registerUserDefinedFunc = function => {
- val builder = makeUserDefinedScalarFuncBuilder(function)
- registerUserDefinedFunction[Expression](
- function = function,
- overrideIfExists = false,
- registry = functionRegistry,
- functionBuilder = builder)
- }
- )
+ def loadPersistentScalarFunction(name: FunctionIdentifier): V1Function = {
+ val qualifiedIdent = qualifyIdentifier(name)
+
+ // Check cache first (no synchronization needed for reads)
+ functionRegistry.lookupFunctionEntry(qualifiedIdent) match {
+ case Some((cachedInfo, cachedBuilder)) =>
+ // Already cached - return with eager builder
+ V1Function(cachedInfo, cachedBuilder)
+
+ case None =>
+ // Fetch metadata eagerly (no resource loading yet)
+ val funcMetadata = fetchCatalogFunction(qualifiedIdent)
+ val info = if (funcMetadata.isUserDefinedFunction) {
+ UserDefinedFunction.fromCatalogFunction(funcMetadata, parser).toExpressionInfo
+ } else {
+ makeExprInfoForHiveFunction(funcMetadata)
+ }
+
+ // Builder factory - loads resources only on first invoke()
+ val builderFactory: () => FunctionBuilder = () => synchronized {
+ // Re-check cache (another thread may have loaded it)
+ functionRegistry.lookupFunctionBuilder(qualifiedIdent).getOrElse {
+ loadFunctionResources(funcMetadata.resources)
+ if (funcMetadata.isUserDefinedFunction) {
+ val udf = UserDefinedFunction.fromCatalogFunction(funcMetadata, parser)
+ registerUserDefinedFunction[Expression](
+ udf,
+ overrideIfExists = false,
+ functionRegistry,
+ makeUserDefinedScalarFuncBuilder(udf))
+ } else {
+ registerFunction(
+ funcMetadata,
+ overrideIfExists = false,
+ functionRegistry,
+ makeFunctionBuilder(funcMetadata))
+ }
+ functionRegistry.lookupFunctionBuilder(qualifiedIdent).get
+ }
+ }
+
+ V1Function(info, builderFactory)
+ }
}
/**
* Look up a persistent table function by name and resolves it to a LogicalPlan.
*/
def resolvePersistentTableFunction(
name: FunctionIdentifier,
- arguments: Seq[Expression]): LogicalPlan = {
- resolvePersistentFunctionInternal[LogicalPlan](
- name,
- arguments,
- tableFunctionRegistry,
- // We don't support persistent Hive table functions yet.
- registerHiveFunc = (func: CatalogFunction) => failFunctionLookup(name),
- registerUserDefinedFunc = function => {
- val builder = makeUserDefinedTableFuncBuilder(function)
- registerUserDefinedFunction[LogicalPlan](
- function = function,
- overrideIfExists = false,
- registry = tableFunctionRegistry,
- functionBuilder = builder)
+ arguments: Seq[Expression]): LogicalPlan = synchronized {
+ val qualifiedIdent = qualifyIdentifier(name)
+ if (tableFunctionRegistry.functionExists(qualifiedIdent)) {
+ // Already cached
+ tableFunctionRegistry.lookupFunction(qualifiedIdent, arguments)
+ } else {
+ // Load from catalog
+ val funcMetadata = fetchCatalogFunction(qualifiedIdent)
+ if (!funcMetadata.isUserDefinedFunction) {
+ // Hive table functions are not supported
+ failFunctionLookup(qualifiedIdent)
}
- )
+ loadFunctionResources(funcMetadata.resources)
+ val udf = UserDefinedFunction.fromCatalogFunction(funcMetadata, parser)
+ registerUserDefinedFunction[LogicalPlan](
+ udf,
+ overrideIfExists = false,
+ tableFunctionRegistry,
+ makeUserDefinedTableFuncBuilder(udf))
+ tableFunctionRegistry.lookupFunction(qualifiedIdent, arguments)
+ }
}
- private def resolvePersistentFunctionInternal[T](
- name: FunctionIdentifier,
- arguments: Seq[Expression],
- registry: FunctionRegistryBase[T],
- registerHiveFunc: CatalogFunction => Unit,
- registerUserDefinedFunc: UserDefinedFunction => Unit): T = {
- // `synchronized` is used to prevent multiple threads from concurrently resolving the
- // same function that has not yet been loaded into the function registry. This is needed
- // because calling `registerFunction` twice with `overrideIfExists = false` can lead to
- // a FunctionAlreadyExistsException.
- synchronized {
- val qualifiedIdent = qualifyIdentifier(name)
- val db = qualifiedIdent.database.get
- val funcName = qualifiedIdent.funcName
- if (registry.functionExists(qualifiedIdent)) {
- // This function has been already loaded into the function registry.
- registry.lookupFunction(qualifiedIdent, arguments)
Review Comment:
I previously assumed the second round would go here, seems not?
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-54856][SQL] Refactor scalar function lookup and resolution [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #53627:
URL: https://github.com/apache/spark/pull/53627#issuecomment-3693981611
cc @pan3793
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-54856][SQL] Refactor scalar function lookup and resolution [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #53627:
URL: https://github.com/apache/spark/pull/53627#discussion_r2649403619
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/connector/V1Function.scala:
##########
@@ -17,15 +17,79 @@
package org.apache.spark.sql.internal.connector
-import org.apache.spark.SparkUnsupportedOperationException
-import org.apache.spark.sql.catalyst.expressions.ExpressionInfo
+import org.apache.spark.SparkException
+import org.apache.spark.sql.catalyst.analysis.FunctionRegistry.FunctionBuilder
+import org.apache.spark.sql.catalyst.expressions.{Expression, ExpressionInfo}
import org.apache.spark.sql.connector.catalog.functions.{BoundFunction, UnboundFunction}
import org.apache.spark.sql.types.StructType
-case class V1Function(info: ExpressionInfo) extends UnboundFunction {
+/**
+ * A wrapper for V1 scalar functions that mirrors the V2 UnboundFunction pattern.
+ *
+ * V1Function has two responsibilities:
+ * 1. Provide ExpressionInfo for DESCRIBE FUNCTION (accessed via `info`)
+ * 2. Provide a function builder for invocation (accessed via `invoke()`)
+ *
+ * The key design is two-phase lazy loading:
+ * - `info` is computed eagerly or from cache (no resource loading)
+ * - `functionBuilder` is computed lazily on first `invoke()` (triggers resource loading)
+ *
+ * This matches V1 behavior where DESCRIBE doesn't load resources but invocation does.
+ */
+class V1Function private (
+ val info: ExpressionInfo,
+ builderFactory: () => FunctionBuilder) extends UnboundFunction {
+
+ /**
+ * Lazy function builder - only computed on first invoke().
+ * For persistent functions, this triggers resource loading.
+ */
+ private lazy val functionBuilder: FunctionBuilder = builderFactory()
+
+ /**
+ * Invoke the function with the given arguments.
+ * This is the V1 equivalent of V2's bind() pattern.
+ * For persistent functions, first invocation triggers resource loading.
+ */
+ def invoke(arguments: Seq[Expression]): Expression = functionBuilder(arguments)
+
+ // UnboundFunction interface
override def bind(inputType: StructType): BoundFunction = {
- throw new SparkUnsupportedOperationException("_LEGACY_ERROR_TEMP_3110")
+ // V1 functions don't use the V2 bind() pattern - they use invoke() instead
+ throw SparkException.internalError("V1Function.bind() should not be called")
}
+
override def name(): String = info.getName
+
override def description(): String = info.getUsage
}
+
+object V1Function {
+ /** A placeholder builder for metadata-only V1Functions that throws on invocation. */
+ private val metadataOnlyBuilder: FunctionBuilder = _ =>
+ throw SparkException.internalError(
+ "Metadata-only V1Function should not be invoked")
+
+ /**
+ * Create a V1Function with eager info and lazy builder.
+ * The builderFactory is called on first invoke().
+ */
+ def apply(info: ExpressionInfo, builderFactory: () => FunctionBuilder): V1Function = {
+ new V1Function(info, builderFactory)
+ }
+
+ /**
+ * Create a V1Function with eager info and builder (for built-in/temp/cached functions).
+ */
+ def apply(info: ExpressionInfo, builder: FunctionBuilder): V1Function = {
+ new V1Function(info, () => builder)
+ }
+
+ /**
+ * Create a metadata-only V1Function (for DESCRIBE FUNCTION).
+ * If invoke() is called, it will throw an error.
+ */
+ def apply(info: ExpressionInfo): V1Function = {
Review Comment:
good point!
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-54856][SQL] Refactor scalar function lookup and resolution [spark]
Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng closed pull request #53627: [SPARK-54856][SQL] Refactor scalar function lookup and resolution
URL: https://github.com/apache/spark/pull/53627
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org