srielau opened a new pull request, #60515:
URL: https://github.com/apache/spark/pull/60515

   ### What changes were proposed in this pull request?
   
   This PR applies SQL PATH resolution to partially qualified object names in 
addition to unqualified names.
   
   The change introduces shared identifier PATH expansion where schema entries 
qualify one-part names and catalog entries qualify two-part names. Routines, 
relations, variables, and procedures use this shared ordering, while names 
beginning with a registered catalog remain directly qualified and bypass PATH.
   
   The previous special-case mini-path for `builtin.name` and `session.name` is 
removed in favor of normal PATH ordering. The built-in `DEFAULT_PATH` includes 
catalog entries for `system` and `CURRENT_CATALOG`. The existing 
`spark.sql.legacy.persistentCatalogFirst` configuration controls their order: 
`false` places `system` first, while `true` places the current catalog first. 
Explicit `SET PATH` and `spark.sql.defaultPath` values remain authoritative.
   
   #### Backward compatibility for older views and SQL functions
   
   Older frozen PATH payloads could contain catalog-only entries that did not 
participate in two-part name resolution; two-part names used a separate 
mini-path of `system` plus the creation-time current catalog. There is no 
reliable telltale in existing metadata that distinguishes those legacy paths 
from paths written with the new semantics (V1 views have a creation version, 
but SQL functions and V2 views do not, and the stored PATH shape is ambiguous).
   
   This PR therefore adds a general integer metadata sequence:
   
   - `view.metadataVersion`
   - `function.metadataVersion`
   
   Semantics:
   
   - **Absent / `0` (legacy):** preserve schema entries from the stored PATH, 
discard catalog-only entries that did not participate in old two-part 
resolution, and append the creation catalog and `system` in the order selected 
by `spark.sql.legacy.persistentCatalogFirst`. The creation catalog property is 
still written on every create/replace so this reconstruction remains possible, 
and so PATH-off / older readers keep working.
   - **`1` (current writer):** trust the stored frozen PATH as-is, including 
catalog entries used for two-part names.
   - **Negative or greater than the current writer understands:** fail analysis 
with an unsupported-metadata-version error. Validation runs before PATH JSON 
parse and also when no PATH is stored, so future payloads fail closed with a 
version error rather than a misleading invalid-path error. This applies to 
every view/SQL-function analysis, including PATH-off objects.
   
   New objects always write `metadataVersion = 1`. `ALTER VIEW ... WITH SCHEMA` 
(`captureNewPath = false`) preserves the existing frozen PATH, metadata 
version, and creation catalog/namespace. Legacy (version 0) PATH reconstruction 
derives the creation catalog from those properties, so rewriting them from the 
ALTER-time session would change two-part resolution. Create/replace still 
rewrites catalog/namespace from the current session and drops any leftover 
pre-Spark-3.0 `VIEW_DEFAULT_DATABASE` key, which is superseded by 
`view.catalogAndNamespace.*`. The upgrade is in memory; existing catalog 
metadata is not rewritten.
   
   View and function ceilings are separate constants 
(`CURRENT_VIEW_METADATA_VERSION` / `CURRENT_FUNCTION_METADATA_VERSION`) and are 
passed explicitly into PATH deserialization so they can diverge later.
   
   #### Forward compatibility (newer object on older Spark) is out of scope
   
   SQL PATH shipped in Spark 4.2.0 behind `spark.sql.path.enabled`, which 
defaults to `false`. Objects that depend on the new frozen-PATH catalog-entry 
semantics are only produced when PATH is enabled. Supporting those newer 
view/function payloads on older Sparks that do not understand `metadataVersion` 
/ enriched PATH is therefore out of scope for this PR. Older Spark continues to 
ignore unknown properties and falls back to the creation catalog for its own 
resolution rules; that is sufficient for PATH-off objects and is not a 
compatibility contract for PATH-on objects.
   
   The PR also:
   
   - applies view-effective SQL configuration before legacy PATH enrichment in 
both the fixed-point analyzer and Analyzer++,
   - carries the frozen SQL PATH through Analyzer++ bridge metadata and 
relation identity,
   - distinguishes PATH-resolved relation keys from explicitly qualified 
relation keys,
   - skips missing catalog entries while building PATH candidates,
   - resolves function metadata commands such as `DESCRIBE FUNCTION` through 
the same ordered PATH,
   - preserves procedure catalog failures while allowing missing candidates to 
fall through,
   - includes `spark.sql.legacy.persistentCatalogFirst` in the cached 
`DEFAULT_PATH` expansion key,
   - adds `CURRENT_CATALOG` as a dynamic PATH element, and
   - updates SQL PATH documentation and generated SQL test results.
   
   ### Why are the changes needed?
   
   Before this change, SQL PATH was used for unqualified routines, relations, 
and variables, but partially qualified names used separate mini-path logic 
limited to the system and current catalogs. For example, `session.t` did not 
follow arbitrary catalog entries in the configured PATH.
   
   That behavior made resolution inconsistent and duplicated ordering logic 
across object types. Using the normal PATH for two-part names makes resolution 
order explicit and consistent across routines, relations, variables, and 
procedures. Carrying the frozen PATH in Analyzer++ metadata also prevents 
relations with identical unresolved syntax but different resolution contexts 
from sharing the wrong metadata.
   
   The metadata version is required so that an older view or SQL function 
containing a two-part name keeps its previous resolution behavior after 
upgrade, instead of suddenly honoring catalog-only entries that were persisted 
but ignored when the object was created. A general `metadataVersion` sequence 
is used rather than a PATH-specific version so later incompatible 
property-format changes can reuse the same marker.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes.
   
   Partially qualified names now follow catalog entries in SQL PATH. For 
example:
   
   ```sql
   SET PATH = cat2, cat1, system;
   SELECT * FROM ns.t;
   ```
   
   The query searches `cat2.ns.t` before `cat1.ns.t`. The same ordering applies 
to partially qualified routines, variables, and procedures as appropriate.
   
   For the built-in default path, `session.t` and `builtin.f()` search the 
`system` catalog before the current catalog by default. Setting 
`spark.sql.legacy.persistentCatalogFirst=true` reverses those two catalog 
entries. Fully catalog-qualified names continue to bypass PATH.
   
   Views and SQL functions created before this change (no `metadataVersion`, 
treated as `0`) keep their previous two-part resolution behavior when queried 
after upgrade. New objects write `metadataVersion = 1` and trust the stored 
PATH.
   
   Reading a PATH-enabled object created by this Spark on an older Spark is out 
of scope: PATH is opt-in (`spark.sql.path.enabled` defaults to `false`), so 
those payloads are not the default migration path.
   
   Unsupported or negative `metadataVersion` values now fail view/SQL-function 
analysis even when PATH is disabled or absent.
   
   ### How was this patch tested?
   
   Added focused positive, negative, ordering, error-propagation, 
compatibility, and Analyzer++ identity tests for routines, relations, 
variables, procedures, resolver guards, the session catalog, persisted views 
and SQL functions, and V2 catalogs.
   
   The following focused checks passed:
   
   ```text
   build/sbt "catalyst/testOnly 
org.apache.spark.sql.connector.catalog.CatalogManagerSuite"
   build/sbt "sql/testOnly org.apache.spark.sql.AlwaysPersistedConfigsSuite"
   build/sbt "sql/testOnly org.apache.spark.sql.SetPathSuite"
   build/sbt "sql/testOnly org.apache.spark.sql.RelationQualificationSuite"
   build/sbt "sql/testOnly org.apache.spark.sql.FunctionQualificationSuite"
   build/sbt "sql/testOnly 
org.apache.spark.sql.analysis.resolver.ResolverGuardSuite"
   build/sbt "sql/testOnly 
org.apache.spark.sql.analysis.resolver.MetadataResolverSuite"
   build/sbt "sql/testOnly org.apache.spark.sql.connector.ProcedureSuite"
   build/sbt "sql/testOnly org.apache.spark.sql.connector.SqlPathV2CatalogSuite"
   build/sbt "sql/testOnly org.apache.spark.sql.JsonArraySuite"
   build/sbt "sql/testOnly org.apache.spark.sql.JsonValueSuite"
   build/sbt "sql/testOnly org.apache.spark.sql.SQLQueryTestSuite -- -z 
sql-path.sql"
   build/sbt ";sql/scalastyle;sql/Test/scalastyle"
   build/sbt "sql/testOnly 
org.apache.spark.sql.execution.command.v1.AlterViewSchemaBindingSuite"
   ```
   
   Metadata-version and ALTER catalog-preserve suites also passed, including a 
case that ALTERs under a second registered current catalog.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: OpenAI Codex CLI 0.167.1
   Generated-by: Cursor (Grok 4.5)
   


-- 
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]

Reply via email to