Skip to content

MDEV-38329 Named parameters in stored procedure CALL#4902

Draft
MooSayed1 wants to merge 1 commit into
MariaDB:mainfrom
MooSayed1:MDEV-38329-named-params
Draft

MDEV-38329 Named parameters in stored procedure CALL#4902
MooSayed1 wants to merge 1 commit into
MariaDB: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.

@MooSayed1 MooSayed1 marked this pull request as draft April 5, 2026 03:21
@gkodinov gkodinov added the External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements. label Apr 6, 2026
@MooSayed1 MooSayed1 force-pushed the MDEV-38329-named-params branch from 146504a to ec66dad Compare May 18, 2026 22:20
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
@MooSayed1 MooSayed1 force-pushed the MDEV-38329-named-params branch from ec66dad to 08491be Compare May 18, 2026 22:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements.

Development

Successfully merging this pull request may close these issues.

2 participants