srielau commented on code in PR #60515:
URL: https://github.com/apache/spark/pull/60515#discussion_r4589640642
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/connector/catalog/CatalogManager.scala:
##########
@@ -588,13 +584,81 @@ private[sql] object CatalogManager extends Logging {
parts(1).equalsIgnoreCase(BUILTIN_NAMESPACE)
/**
- * True when `system.builtin` is the first entry of `pathEntries`. This is
the path-shape
- * condition under which a built-in function found by an unqualified
single-part name cannot be
- * shadowed by any earlier path entry -- the precise property the
function-resolution built-in
- * fast-path relies on. Pure predicate over the path shape; callers decide
how to use it.
+ * True when `system.builtin` is the first schema entry of `pathEntries`.
Catalog-only entries do
+ * not participate in one-part resolution. This is the path-shape condition
under which a
+ * built-in function found by an unqualified single-part name cannot be
shadowed by any earlier
+ * relevant entry -- the precise property the function-resolution built-in
fast-path relies on.
+ * Pure predicate over the path shape; callers decide how to use it.
*/
def isBuiltinFirstOnPath(pathEntries: Seq[Seq[String]]): Boolean =
- pathEntries.headOption.exists(isSystemBuiltinPathEntry)
+ pathEntries.find(_.length > 1).exists(isSystemBuiltinPathEntry)
+
+ /**
+ * True if an identifier is resolved through SQL PATH. A two-part name whose
first part is a
+ * registered catalog is already fully catalog-qualified and bypasses PATH.
+ */
+ def identifierUsesPath(
+ nameParts: Seq[String],
+ isCatalogRegistered: String => Boolean): Boolean = {
+ nameParts.length == 1 ||
+ (nameParts.length == 2 && !isCatalogRegistered(nameParts.head))
+ }
+
+ /**
+ * Select the SQL path entries that participate in resolving an identifier.
+ *
+ * - One-part names use schema entries (catalog + namespace). Catalog-only
entries are included
+ * only when that catalog is not already present as the prefix of a schema
entry -- the case
+ * where CURRENT_SCHEMA collapses to the catalog name because the default
namespace is empty
+ * (common for V2 catalogs). Catalog-only entries that duplicate a schema
prefix are omitted
+ * so session-catalog probes like `spark_catalog.<name>` (invalid empty
namespace) are not
+ * introduced when `spark_catalog.<schema>` is already on the path.
+ * - Two-part partially qualified names use the distinct catalog prefixes of
the path. Explicit
+ * catalog-only entries and catalogs implied by schema entries both
participate, so a PATH that
+ * lists only schemas (e.g. `spark_catalog.db_a, system.builtin`) still
resolves `db.table`.
+ * - Fully catalog-qualified names do not use the path for resolution, but
retain the historical
+ * schema-shaped path for diagnostics.
+ */
+ def identifierPathEntries(
+ pathEntries: Seq[Seq[String]],
+ nameParts: Seq[String],
+ isCatalogRegistered: String => Boolean): Seq[Seq[String]] =
nameParts.length match {
+ case 1 =>
+ val schemaEntries = pathEntries.filter(_.length > 1)
+ val catalogsWithSchema =
schemaEntries.map(_.head.toLowerCase(java.util.Locale.ROOT)).toSet
+ val uncoveredCatalogEntries = pathEntries.filter { e =>
+ e.length == 1 &&
!catalogsWithSchema.contains(e.head.toLowerCase(java.util.Locale.ROOT))
+ }
+ schemaEntries ++ uncoveredCatalogEntries
+ case 2 if identifierUsesPath(nameParts, isCatalogRegistered) =>
+ // Catalog-only entries keep their configured order (e.g.
persistentCatalogFirst). Catalogs
+ // that appear only as the prefix of a schema entry are appended after
them.
+ val catalogOnly = pathEntries.filter(_.length == 1)
+ val schemaCatalogs = pathEntries.filter(_.length > 1).map(_.take(1))
+ (catalogOnly ++ schemaCatalogs).distinct
+ case _ => pathEntries.filter(_.length > 1)
+ }
+
+ /**
+ * Build resolution candidates for an identifier from the SQL path. Schema
path entries
+ * (catalog + namespace) qualify one-part names, while catalog path entries
qualify two-part
+ * names. Names with three or more parts are already fully qualified.
+ */
+ def identifierPathCandidates(
+ pathEntries: Seq[Seq[String]],
+ nameParts: Seq[String],
+ isCatalogRegistered: String => Boolean): Seq[Seq[String]] = {
+ if (identifierUsesPath(nameParts, isCatalogRegistered)) {
Review Comment:
Partially addressed in 67b5064d204.
identifierPathCandidates now memoizes the registration probe within one
call. The callers (RelationResolution, procedure and function resolution) still
evaluate identifierUsesPath separately, so a probe can still repeat across
calls. Not done yet.
##########
docs/sql-ref-syntax-aux-conf-mgmt-set-path.md:
##########
@@ -132,6 +150,8 @@ path_element
* Setting the path takes effect immediately.
* Identifier case is preserved in storage and in `current_path()` output.
+* Catalog entries are ignored for one-part names, and schema entries are
ignored for two-part
Review Comment:
Addressed in 67b5064d204.
The SET PATH page now states the arity rules the code applies.
Name-resolution and DESCRIBE FUNCTION pages are updated too. The scaladoc and
code comments you listed (resolutionCandidates, RelationResolution,
VariableResolution) have not all been rechecked.
##########
docs/sql-migration-guide.md:
##########
@@ -24,6 +24,8 @@ license: |
## Upgrading from Spark SQL 4.4 to 5.0
+- Since Spark 5.0, two-part object names use catalog entries in the SQL Path.
By default, the built-in default path tries the virtual `system` catalog before
the current catalog, so `builtin.func()` and `session.func()` prefer
`system.builtin` and `system.session`. Setting
`spark.sql.legacy.persistentCatalogFirst` to `true` reverses those two catalog
entries in the built-in default path. An explicit `SET PATH` or
`spark.sql.defaultPath` value controls the order directly: each catalog-only
entry and each catalog implied by a schema entry (for example `spark_catalog`
from `spark_catalog.default`) participates, in first-seen order. Schema-only
paths that worked in Spark 4.2 and 4.3 therefore keep resolving two-part names
through those implied catalogs. Views and SQL functions without a frozen PATH
(created with PATH off, and V2 views) do not inherit the invoker's `SET PATH`;
they use `spark.sql.defaultPath` when it is set, otherwise the object's home
catalog and namespace with the buil
t-in ordering. Legacy SQL functions that store a version-0 frozen PATH
continue to qualify two-part names with the invoker's current catalog, as
before. See [Reserved system
names](sql-ref-identifier.html#reserved-system-names) and [SET
PATH](sql-ref-syntax-aux-conf-mgmt-set-path.html).
Review Comment:
Addressed in 6b780ff1486 and 67b5064d204.
The note describes catalog-role entries, the invoker's catalog for SQL
functions, and the version-0 rule. It documents the schema-only rule kept by
decision (see the reply on identifierPathEntries).
##########
sql/core/src/test/scala/org/apache/spark/sql/connector/ProcedureSuite.scala:
##########
@@ -266,6 +266,69 @@ class ProcedureSuite extends SharedSparkSession with
BeforeAndAfter {
}
}
+ test("PATH enabled: partially qualified CALL follows catalog entries") {
+ withSQLConf(SQLConf.PATH_ENABLED.key -> "true") {
+ try {
+ catalog.createProcedure(Identifier.of(Array("ns"), "sum"), UnboundSum)
+ catalog("cat2").createProcedure(Identifier.of(Array("ns"), "sum"),
UnboundLongSum)
+
+ sql("SET PATH = cat2, cat")
+ checkAnswer(sql("CALL ns.sum(1, 2)"), Row(3L) :: Nil)
Review Comment:
Addressed in 67b5064d204.
The test asserts the output schema type (LongType vs IntegerType) as well as
the values, so a wrong catalog order fails.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/VariableResolution.scala:
##########
@@ -187,17 +177,15 @@ class VariableResolution(
}
}
- // The temp variables live in `SYSTEM.SESSION`, and the name can be
qualified or not.
- private def maybeTempVariableName(nameParts: Seq[String]): Boolean = {
- nameParts.length == 1 || {
- if (nameParts.length == 2) {
- nameParts.head.equalsIgnoreCase(CatalogManager.SESSION_NAMESPACE)
- } else if (nameParts.length == 3) {
- nameParts(0).equalsIgnoreCase(CatalogManager.SYSTEM_CATALOG_NAME) &&
- nameParts(1).equalsIgnoreCase(CatalogManager.SESSION_NAMESPACE)
- } else {
- false
- }
+ // The temp variables live in `SYSTEM.SESSION`. One- and two-part references
use schema and
+ // catalog entries from the SQL path; the three-part form is fully qualified.
+ private def resolvesToTempVariable(nameParts: Seq[String]): Boolean = {
+ val candidates = CatalogManager.identifierPathCandidates(
Review Comment:
Partially addressed in 67b5064d204.
resolvesToTempVariable returns early for shapes that cannot match: three or
more parts, and two-part names that do not start with session. It still builds
the path per attempt; memoizing the path through AnalysisContext is not done.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/SQLFunction.scala:
##########
@@ -187,6 +187,20 @@ case class SQLFunction(
/** Frozen PATH string persisted when the function was created with SQL PATH
enabled. */
def functionStoredResolutionPath: Option[String] =
properties.get(SQLFunction.FUNCTION_RESOLUTION_PATH)
+
+ /**
+ * Persisted SQL function metadata format version. Absent means version 0.
See
+ * [[SQLFunction.CURRENT_FUNCTION_METADATA_VERSION]].
+ */
+ def functionMetadataVersion: Int =
+ CatalogTable.parseMetadataVersion(
+ properties.get(SQLFunction.FUNCTION_METADATA_VERSION),
+ objectType = "SQL function",
+ objectName = name.unquotedString)
+
+ /** Catalog that was current when the function was created. */
+ def functionCreationCatalog: Option[String] =
Review Comment:
Partially addressed in 67b5064d204.
functionCreationCatalog is removed. The creationCatalog parameter of
deserializePathEntriesForAnalysisOrFail is still documented as the view's
creation catalog. That doc needs updating for SQL functions.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/SQLFunction.scala:
##########
@@ -201,6 +215,20 @@ object SQLFunction {
*/
val FUNCTION_RESOLUTION_PATH: String = "function.resolutionPath"
+ /**
+ * Sequence counter for incompatible changes to persisted SQL function
property interpretation.
+ * Absent = 0. Version history matches
[[CatalogTable.VIEW_METADATA_VERSION]].
+ */
+ val FUNCTION_METADATA_VERSION: String = "function.metadataVersion"
+
+ /**
+ * Current [[FUNCTION_METADATA_VERSION]] written by this Spark build. Bump
when a change cannot
+ * read older function property payloads without a version branch. This may
diverge from
+ * [[CatalogTable.CURRENT_VIEW_METADATA_VERSION]]; callers must pass the
matching ceiling to
+ * `CatalogManager.deserializePathEntriesForAnalysisOrFail`.
+ */
+ val CURRENT_FUNCTION_METADATA_VERSION: Int =
CatalogTable.CURRENT_VIEW_METADATA_VERSION
Review Comment:
Addressed in 67b5064d204.
CURRENT_FUNCTION_METADATA_VERSION is now its own literal (1) and does not
reference the view constant.
--
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]