srielau commented on code in PR #60515:
URL: https://github.com/apache/spark/pull/60515#discussion_r4589636684
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala:
##########
@@ -292,13 +307,46 @@ object AnalysisContext {
try f finally { set(originContext) }
}
- def withAnalysisContext[A](function: SQLFunction)(f: => A): A = {
+ /** Test helper: analyze a SQL function body with an empty invoker
catalog/namespace. */
+ def withAnalysisContext[A](function: SQLFunction)(f: => A): A =
+ withAnalysisContext(function, Nil)(f)
+
+ /**
+ * Analyze a SQL function body.
+ *
+ * @param invokerCatalogAndNamespace the caller's current catalog and
namespace. Version-0
+ * frozen PATH reconstruction appends this
catalog (not the
+ * creation catalog) for two-part names,
matching the base
+ * branch. Pathless bodies also use it as
their home so they
+ * never inherit the caller's SET PATH.
+ */
+ def withAnalysisContext[A](
+ function: SQLFunction,
+ invokerCatalogAndNamespace: Seq[String])(f: => A): A = {
val originContext = value.get()
- // Function body analysis should not inherit any caller-pinned path; use
only function metadata.
+ // Validate even when PATH is absent so future metadata versions still
fail closed.
+ CatalogTable.requireSupportedMetadataVersion(
+ function.functionMetadataVersion,
+ SQLFunction.CURRENT_FUNCTION_METADATA_VERSION,
+ "SQL function",
+ function.name.unquotedString)
+ val hasFrozenPath = function.functionStoredResolutionPath.isDefined
+ // Function body analysis should not inherit any caller-pinned path. A
frozen PATH is used
+ // as stored (with version-0 enrichment against the invoker catalog). A
pathless body pins
+ // the invoker's home catalog/namespace so DEFAULT_PATH / the built-in
default apply, and
+ // the invoker's SET PATH does not.
val context = originContext.copy(
+ catalogAndNamespace =
+ if (hasFrozenPath) Nil else invokerCatalogAndNamespace,
Review Comment:
Addressed in 70b72ead07e.
Pathless SQL function bodies keep the caller's catalogAndNamespace again, so
temp-object gates see the caller's context. With PATH enabled and no frozen
PATH, the body's entries are pinned to spark.sql.defaultPath, or to the
built-in default around the function's home, so the invoker's SET PATH does not
apply. With PATH off, body resolution is the same as before this PR. Not yet
verified with the temp-view, temp-function and session-variable tests listed in
the review.
##########
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
Review Comment:
Decision: keep the current design.
Two-part names use catalog entries only, so a path that lists only schemas
gives two-part names no catalog to resolve against. The migration note and the
SetPathSuite test 'schema-only SET PATH does not invent catalogs' document
this. Restoring the base-pair fallback was considered and not taken.
##########
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)
Review Comment:
Addressed in 6b780ff1486.
A collapsed CURRENT_SCHEMA is now a schema-role entry, so it no longer
counts as a catalog entry. Two-part names use catalog-role entries only, and
the built-in default keeps system ahead of the current catalog unless
persistentCatalogFirst is true. Not yet covered by a regression test for the
table-only V2 catalog case.
##########
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
Review Comment:
Addressed in 6b780ff1486.
One-part names now use schema-role entries and uncovered catalog-role
entries in declared order. isBuiltinFirstOnPath uses the same list, so a
catalog-only entry no longer jumps ahead of a schema entry that precedes it.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/connector/catalog/CatalogManager.scala:
##########
@@ -735,4 +803,43 @@ private[sql] object CatalogManager extends Logging {
context = Array.empty)
}
}
+
+ /**
+ * Parse a frozen path for analysis, using the object's metadata version to
decide whether the
+ * stored PATH already encodes catalog entries for two-part names. Version 0
(absent) payloads
+ * could contain catalog-only entries that did not participate in
resolution; two-part names used
+ * a separate mini-path containing `system` and the creation-time current
catalog. Preserve the
+ * schema entries and append those catalog entries in the historical order
so old views and SQL
+ * functions keep their behavior. Version
`METADATA_VERSION_PATH_WITH_CATALOG_ENTRIES` and above
+ * trusts the stored PATH as-is.
+ */
+ def deserializePathEntriesForAnalysisOrFail(
+ storedPathStr: String,
+ metadataVersion: Int,
+ creationCatalog: Option[String],
+ persistentCatalogFirst: Boolean,
+ objectType: String,
+ objectName: String,
+ currentMetadataVersion: Int): Seq[Seq[String]] = {
+ import org.apache.spark.sql.catalyst.catalog.CatalogTable.{
+ METADATA_VERSION_PATH_WITH_CATALOG_ENTRIES,
+ requireSupportedMetadataVersion
+ }
+ // Check the version before parsing PATH so a future-format payload
reports an
+ // unsupported-version error instead of a misleading invalid-path error.
+ requireSupportedMetadataVersion(
+ metadataVersion, currentMetadataVersion, objectType, objectName)
+ val entries = deserializePathEntriesOrFail(storedPathStr, objectType,
objectName)
+ if (metadataVersion >= METADATA_VERSION_PATH_WITH_CATALOG_ENTRIES) {
+ entries
+ } else {
+ val persistentCatalog = creationCatalog.map(Seq(_)).toSeq
+ val catalogEntries = if (persistentCatalogFirst) {
+ persistentCatalog :+ Seq(SYSTEM_CATALOG_NAME)
+ } else {
+ Seq(Seq(SYSTEM_CATALOG_NAME)) ++ persistentCatalog
+ }
+ entries.filter(_.length > 1) ++ catalogEntries
Review Comment:
Addressed in 6b780ff1486.
Version-0 frozen paths keep each stored entry as a schema-role entry, so a
collapsed current schema stays where it was declared. The creation catalog and
system are appended as catalog-role entries, so they take part only in two-part
resolution.
--
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]