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

   ### What changes were proposed in this pull request?
   
   Before analyzing a SQL function body, `CREATE FUNCTION` runs two checks: one 
for cyclic references and one for references to temporary functions. Both 
looked up every called name as a persistent function in `spark_catalog`, so 
`builtin.` and `session.` were treated as schema names, and names with three 
parts were rejected outright.
   
   This PR makes both checks resolve a called name the way function resolution 
does:
   
   - `builtin.f` / `session.f` use the system namespace when it has `f`. 
Otherwise they fall back to a persistent schema with the same name. With 
`spark.sql.legacy.persistentCatalogFirst=true`, the persistent function is 
tried first.
   - `system.builtin.f` / `system.session.f` always use the system namespace.
   - `spark_catalog.db.f` is looked up directly.
   - Functions in other catalogs are left to analysis.
   
   ### Why are the changes needed?
   
   The same call works in a query but fails in a function body:
   
   ```sql
   SELECT builtin.abs(-1);                                          -- 1
   CREATE FUNCTION f1() RETURNS INT RETURN builtin.abs(-1);         -- 
SCHEMA_NOT_FOUND
   CREATE FUNCTION f2() RETURNS INT RETURN system.builtin.abs(-1);  -- 
IDENTIFIER_TOO_MANY_NAME_PARTS
   CREATE FUNCTION g() RETURNS INT RETURN 5;
   CREATE FUNCTION f3() RETURNS INT RETURN spark_catalog.default.g(); -- 
IDENTIFIER_TOO_MANY_NAME_PARTS
   
   CREATE TEMPORARY FUNCTION temp_fn() RETURNS INT RETURN 1;
   CREATE FUNCTION f4() RETURNS INT RETURN session.temp_fn();       -- 
SCHEMA_NOT_FOUND (wrong error)
   CREATE TEMPORARY FUNCTION t() RETURNS INT RETURN system.session.temp_fn(); 
-- IDENTIFIER_TOO_MANY_NAME_PARTS
   ```
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, it fixes a bug. With this PR:
   
   - `f1`, `f2`, `f3` and `t` are created and return the same results as a 
query would.
   - `f4` fails with `INVALID_TEMP_OBJ_REFERENCE`, because a persistent 
function cannot reference a temporary function.
   - Cycles through qualified names are still detected, for example `CREATE OR 
REPLACE FUNCTION g() RETURNS INT RETURN spark_catalog.default.f3()` fails with 
`CYCLIC_FUNCTION_REFERENCE`.
   
   ### How was this patch tested?
   
   New tests in `SQLFunctionSuite`. They cover builtin, session and 
catalog-qualified calls in scalar and table functions, cycles through those 
names, schemas named `builtin` and `session`, and the legacy 
`persistentCatalogFirst` setting. `SQLFunctionSuite`, 
`FunctionQualificationSuite` and the `sql-udf.sql` golden files pass.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Claude Opus 5.5)
   
   This pull request and its description were written by Isaac.
   


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