Skip to content

MDEV-38329 Named parameters in stored procedure CALL#4902

Open
MooSayed1 wants to merge 1 commit intoMariaDB:mainfrom
MooSayed1:MDEV-38329-named-params
Open

MDEV-38329 Named parameters in stored procedure CALL#4902
MooSayed1 wants to merge 1 commit intoMariaDB:mainfrom
MooSayed1:MDEV-38329-named-params

Conversation

@MooSayed1
Copy link
Copy Markdown
Contributor

@MooSayed1 MooSayed1 commented Apr 4, 2026

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:

CALL proc(a => 1, b => 2, c => 3);       -- all named
CALL proc(c => 3, a => 1, b => 2);       -- any order
CALL proc(1, b => 2, c => 3);            -- mixed positional + named
CALL proc(a => 1, c => 3);               -- skip middle param with default

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_cparam rule to accept ident ARROW_SYM expr alongside plain expr
in CALL argument lists. Named arguments set IS_EXPLICIT_NAME on the Item and
store the parameter name in Item::name, reusing the same mechanism that UDF
named 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 are
matched against sp_pcontext's formal parameter list by name and reordered to
their declared positions. Omitted parameters with defaults are filled from
sp_variable::default_value. The following errors are detected:

  • Unknown parameter name (ER_SP_UNDECLARED_VAR)
  • Duplicate parameter name (ER_SP_UNDECLARED_VAR)
  • Missing required parameter without default (ER_SP_WRONG_NO_OF_ARGS)

Current status

Done:

  • Parser: CALL proc(a => 1, b => 2) syntax accepted
  • Mixed positional + named: CALL proc(1, b => 2)
  • Parse-time rejection of positional after named
  • Argument reordering in execute_procedure()
  • Default value filling for omitted parameters
  • Error detection: unknown name, duplicate name, missing required param
  • MTR test coverage

Still working on:

  • Named params for stored functions in expressions (SELECT func(a => 1))
  • Prepared statement support (PREPARE stmt FROM 'CALL p(a => ?)')
  • OUT/INOUT parameter support with named args
  • Extended test coverage (edge cases, nested calls, reserved words as param names)

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?

./mtr main.sp_named_params

The test covers:

  • All positional (no regression)
  • All named in declared order and different order
  • Mixed positional + named
  • Positional after named (syntax error)
  • Unknown parameter name, duplicate name
  • Default value: skip middle param, only required param, override all defaults
  • Missing required parameter without default

Basing the PR against the correct MariaDB version

This is a new feature. The PR targets main.

PR quality check

  • I checked the CODING_STANDARDS.md file and my PR conforms to this where appropriate.
  • For any trivial modifications to the PR, I am ok with the reviewer making the changes themselves.

Add support for named parameter syntax using => in
CALL statements:

  CALL proc(a => 1, b => 2);
  CALL proc(1, b => 2, c => 3);
  CALL proc(a => 1, c => 3);

Parser: added sp_cparam rule in sql_yacc.yy to accept
ident ARROW_SYM expr. 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: in sp_head::execute_procedure(), named args
are matched against sp_pcontext formal parameters and
reordered to declared positions. Omitted params with
defaults are filled from sp_variable::default_value.
Unknown names, duplicates, and missing required params
are detected at execution time.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant