[SPARK-54807][SQL] Allow qualified names for built-in and session functions#53570
[SPARK-54807][SQL] Allow qualified names for built-in and session functions#53570srielau wants to merge 126 commits into
Conversation
- Persistent functions now cached with unqualified keys for compatibility - Temporary functions use composite keys (session.funcName) - Both can coexist in the same registry - Views correctly exclude temp functions from resolution Issue: View test still failing - needs investigation into function builder resolution
- Persistent functions now stored with qualified keys (catalog.db.func) - Prevents conflicts when multiple databases have same function name - Temporary functions still use composite keys (session.func) Known issues: - View function resolution test still failing - Possible function listing regressions to investigate
Added extensive debug logging to understand why views capture wrong function class. Ready for detailed tracing.
**THE BUG:** In resolveBuiltinOrTempFunctionInternal, the 'isBuiltin' parameter was incorrectly checking if the temp/builtin identifier existed in the session registry, instead of checking the static FunctionRegistry.builtin. This caused lookupTempFuncWithViewContext to treat temp functions as builtins, bypassing view context checks and allowing temp functions created AFTER a view to incorrectly shadow the persistent function that the view should use. **THE FIX:** Changed the isBuiltin check to use FunctionRegistry.builtin.functionExists and TableFunctionRegistry.builtin.functionExists directly, matching master's behavior. **TEST RESULTS:** ✅ All 62 tests pass (PersistedViewTestSuite + FunctionQualificationSuite) ✅ SPARK-33692 view test now passes ✅ View correctly uses MyDoubleAvg and ignores temp MyDoubleSum
Removed leftover test scripts that were causing compilation errors: - test_simple_function.scala - test_view_function.scala All code now compiles cleanly.
Analysis covers: - Complete API surface (read/write operations) - Current architecture and memory usage - Three proposed optimization approaches - Detailed feasibility assessment KEY DISCOVERY: Internal functions already use separate static registry! - FunctionRegistry.internal contains ~20 ML/Pandas/Connect functions - Resolved directly, bypassing SessionCatalog - Proves composite registry pattern works in production - Validates proposed optimization approach Memory savings potential: 98% reduction for high-session deployments Implementation effort: 2-3 days coding + testing Risk: Low (pattern already proven with internal functions)
Comprehensive comparison covering: - Registry architecture (cloned vs static) - User-facing vs implementation details - Resolution paths and shadowing behavior - Examples and use cases - Historical context (Spark 4 separation) KEY FINDINGS: - Builtin: ~500 user-facing SQL functions, cloned per session - Internal: ~20 implementation functions for Connect/ML/Pandas, single global registry - Internal functions already use separate static registry pattern - Proves composite registry approach is production-ready This validates our proposed optimization approach for builtins.
Updated test to expect INVALID_TEMP_OBJ_QUALIFIER (AnalysisException) instead of INVALID_SQL_SYNTAX.CREATE_TEMP_FUNC_WITH_DATABASE (ParseException) for invalid temporary function qualifications. This aligns with the user's request to treat invalid temp function qualifications as semantic errors (42602 SQLSTATE) rather than syntax errors. Test cases updated: - CREATE TEMPORARY FUNCTION a.b() - now expects INVALID_TEMP_OBJ_QUALIFIER - CREATE TEMPORARY FUNCTION a.b.c() - now expects INVALID_TEMP_OBJ_QUALIFIER All tests pass.
These files were working notes created during development and should not be committed to the repository: - BUILTIN_VS_INTERNAL_FUNCTIONS.md - CURSOR_IMPLEMENTATION_SUMMARY.md - CURSOR_TEST_RESULTS.md - FUNCTION_QUALIFICATION_ANALYSIS.md - FUNCTION_QUALIFICATION_COMPLETE.md - FUNCTION_QUALIFICATION_SUMMARY.md - FUNCTION_REGISTRY_API_ANALYSIS.md - IMPLEMENTATION_COMPLETE.md - TABLE_FUNCTION_REGISTRY_ANALYSIS.md - UNIFIED_FUNCTION_NAMESPACE.md Only production code and tests should be in the repository.
These were working test files created during development: - test_both.sql - test_namespace.sql - test_range.sql They should not be committed to the repository.
…apsulation Refactoring #1: Extract Scalar/Table Function Duplication - Added handleViewContext() helper to centralize view resolution logic - Added lookupFunctionWithShadowing() generic helper for both scalar and table functions - Eliminated ~50 lines of duplicated shadowing and view context logic - Simplified lookupBuiltinOrTempFunction() and lookupBuiltinOrTempTableFunction() Refactoring #2: Unified Qualification Checker - Added isQualifiedWithNamespace() helper to check namespace qualifications - Refactored maybeBuiltinFunctionName() and maybeTempFunctionName() to use common helper - Eliminated ~15 lines of duplication - Prepares for future PATH implementation Documentation Improvements: - Added comprehensive scaladoc to TEMP_FUNCTION_DB explaining composite key pattern - Added scaladoc to tempFunctionIdentifier() and isTempFunctionIdentifier() - Added detailed comments to new helper methods Benefits: - Single source of truth for shadowing logic - Easier to maintain and test - Better encapsulation of view resolution context - More consistent code structure All tests pass: - FunctionQualificationSuite: 24/24 tests passed - SessionCatalogSuite: 100/100 tests passed
Naming Improvements: - Renamed 'useTempIdentifier' → 'lookupAsTemporary' for clarity - More semantic boolean parameter names Code Quality: - Made handleViewContext() functional (removed imperative 'return') - Removed unused 'tempIdentifier' and 'builtinIdentifier' variables - Extracted resolveFunctionWithFallback() to eliminate duplication - Used Option.filter for more idiomatic Scala Benefits: - More functional programming style - Clearer intent with better parameter names - No unused variables cluttering the code - Extracted common pattern reduces duplication by ~30 lines - More readable and maintainable All 24 tests in FunctionQualificationSuite pass.
Removed sql-function-qualifiers.sql and its golden files. All test coverage is comprehensively provided by FunctionQualificationSuite.scala. Rationale: - Eliminates duplication between SQL and Scala tests - Scala tests provide better error validation with checkError() - Scala tests are easier to maintain and debug - Scala tests have better test isolation - No loss of coverage: Scala suite has 24 tests covering all scenarios FunctionQualificationSuite.scala provides complete coverage: - 8 tests: Reference qualification (SELECT statements) - 9 tests: DDL (CREATE/DROP TEMPORARY FUNCTION) - 4 tests: Type mismatch errors - 3 tests: Integration scenarios All 24 tests pass.
|
I don't think we need A few concrete issues with the current
Suggestion: remove This comment was generated with GitHub MCP. |
|
This review was generated by Cursor with Claude Opus 4.6. Additional implementation review comments beyond what's already been raised: 1.
|
Followup cleanup for function qualification PR
|
I will merge this one once the CI passed. Thanks for the works @srielau |
|
Hi @srielau @cloud-fan @gengliangwang @vladimirg-db, a heads-up on a performance regression introduced by this change (SPARK-54807), tracked as SPARK-57758. Before this PR, an unqualified built-in function ( The impact is amplified under Spark Connect, which re-analyzes the whole (growing) plan on every I opened #56869 to fix it: memoize the resolution path per analysis pass and add a built-in-only fast path for single-part names (gated so it only short-circuits when Would appreciate your review. Thanks! |
design_sketch.md
What changes were proposed in this pull request?
Allow reference of built in functions with qualifiers builtin or system.builtin and temporary functions as session or system.session. Functions registered as extensionz can be qualified with system.extension or extension.
Cleaned up APIs to resolve functions to prep for configurable path
Register builtin extension, and session functions with qualified names, so they can co-exist
Fix a bug that allowed session functions with the same name to co-exist as table and scalar functions
design_sketch.md
Why are the changes needed?
This portion of work allows users to excplicitly pick a builtin or temporary function, the same way they would pick a persisted function by fully qualifying it.
This increases security.
WIth this we now have a fixed order:
extension -> builtin -> session -> current schema for function resolution.
In follow on work we plan to allow the priority of function resolution to be configurable, for example to push temporary functions after built-ins or even after persisted functions.
Ultimately we aim for proper SQL Standard PATH support where a user can add "libraries" of functions to the path.
Does this PR introduce any user-facing change?
You can now reference builtin functions such as concat with builtin.concat or system.builtin.concat. Teh same for temporary functions which can be qualified as session, or system.session.
How was this patch tested?
A new suite: functionQualificationSuite.scala has been added
Was this patch authored or co-authored using generative AI tooling?
Yes: Claude Sonnet