MDEV-38329 Named parameters in stored procedure CALL#4902
Draft
MooSayed1 wants to merge 1 commit into
Draft
Conversation
146504a to
ec66dad
Compare
Add support for named parameter syntax in stored routine calls, allowing callers to specify arguments by name and in any order. Stored procedures use the => syntax: CALL proc(a => 1, b => 2); CALL proc(c => 3, a => 1, b => 2); CALL proc(1, b => 2, c => 3); Stored functions use the existing AS alias syntax: SELECT func(1 AS a, 2 AS b); SELECT func(3 AS c, 1 AS a, 2 AS b); Parameters with default values can be omitted: CALL proc(a => 1); -- b,c use defaults SELECT func(1 AS a, 3 AS c); -- b uses default Parser: added sp_cparam rule in sql_yacc.yy to accept ident ARROW_SYM expr in CALL argument lists. Named args set IS_EXPLICIT_NAME and store the name in Item::name, reusing the UDF named argument mechanism. Positional args after named args are rejected at parse time. Reordering for procedures: in sp_head::execute_procedure(), named arguments are matched against sp_pcontext formal parameters and reordered to declared positions. Omitted params with defaults are filled from sp_variable::default_value. Reordering for functions: in Item_func_sp::fix_fields(), after resolving the sp_head, named arguments (marked via IS_EXPLICIT_NAME by the AS alias syntax) are matched and reordered the same way. The has_named_parameters() rejection in Create_sp_func::create_with_db() is removed. Error handling: - Unknown parameter name: ER_SP_UNDECLARED_VAR - Duplicate parameter name: ER_SP_DUP_PARAM - Missing required parameter: ER_SP_WRONG_NO_OF_ARGS
ec66dad to
08491be
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The Jira issue number for this PR is: MDEV-38329
Description
Stored procedures currently require all arguments to be passed positionally.
When a routine has many parameters with defaults, there is no way to skip
middle parameters — only trailing ones can be omitted.
This patch adds named parameter invocation using the
=>syntax:This brings MariaDB in line with Oracle, PostgreSQL, SQL Server, and Firebird,
all of which already support named parameter invocation.
Parser changes (
sql/sql_yacc.yy):Added
sp_cparamrule to acceptident ARROW_SYM expralongside plainexprin CALL argument lists. Named arguments set
IS_EXPLICIT_NAMEon the Item andstore the parameter name in
Item::name, reusing the same mechanism that UDFnamed arguments already use. Positional arguments after a named argument are
rejected at parse time.
Reordering (
sql/sp_head.cc):In
sp_head::execute_procedure(), before the binding loop, named arguments arematched against
sp_pcontext's formal parameter list by name and reordered totheir declared positions. Omitted parameters with defaults are filled from
sp_variable::default_value. The following errors are detected:ER_SP_UNDECLARED_VAR)ER_SP_UNDECLARED_VAR)ER_SP_WRONG_NO_OF_ARGS)Current status
Done:
CALL proc(a => 1, b => 2)syntax acceptedCALL proc(1, b => 2)execute_procedure()Still working on:
SELECT func(a => 1))PREPARE stmt FROM 'CALL p(a => ?)')This PR is a work in progress. Reviews and feedback on the current approach are welcome.
Release Notes
Stored procedures now support named parameter invocation using the
=>syntax.Arguments can be passed by name in any order, and parameters with default values
can be skipped:
CALL proc(a => 1, c => 3).How can this PR be tested?
The test covers:
Basing the PR against the correct MariaDB version
This is a new feature. The PR targets
main.PR quality check