Skip to content

Commit

Permalink
MDEV-34391 SET PATH statement
Browse files Browse the repository at this point in the history
Definition
==========
Implement the SET PATH statement. The grammar differs from the Jira
ticket where the equal sign is required:
```
SET PATH= 'schema1, schema2, schema3';
```

Function
--------
In MariaDB, the SQL-path will also be used to lookup packages in
package routine invocations, so the following script:

  SET PATH='sys';
  SELECT UTL_ENCODE.BASE64_DECODE('data');

invokes SYS.UTL_ENCODE.BASE64_DECODE('data'), i.e. the function
BASE64_DECODE() in the packge UTL_ENCODE in the database SYS
(if such package and package routine exist).

MariaDB implementation details
------------------------------
The ticket's requirement stated the grammar should support delimited
identifiers with backticks or double quotes within the string quote,
ie: the single tick. A simple parser was implemented to handle this
requirement in Sql_path::from_text.

Only 16 schemas are supported per session - we do not want too many
schemas in the path anyway since it will impact performance. This also
means that the path cache is a simple static array for easier memory
management.

Internal schemas (mariadb_schema, oracle_schema) are supported in paths.
Only the first internal schema in the path will be used to resolve native
routines to their respective versions.
  • Loading branch information
wong-github1 authored and iqbal-hasprime committed Jan 28, 2025
1 parent 9f5adf0 commit 8f87435
Show file tree
Hide file tree
Showing 50 changed files with 12,224 additions and 3,492 deletions.
1 change: 1 addition & 0 deletions libmysqld/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ SET(SQL_EMBEDDED_SOURCES emb_qcache.cc libmysqld.c lib_sql.cc
../sql/json_table.cc
../sql/opt_histogram_json.cc
../sql/sp_instr.cc
../sql/sql_path.cc
${GEN_SOURCES}
${MYSYS_LIBWRAP_SOURCE}
)
Expand Down
24 changes: 18 additions & 6 deletions mysql-test/include/commit.inc
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ flush status;

--echo #
--echo # Count of reading of p_verify_status_increment() from mysql.proc
call p_verify_status_increment(2, 0, 2, 0);
call p_verify_status_increment(4, 0, 2, 0);

--echo #
--echo # 1. Read-only statement: SELECT
Expand Down Expand Up @@ -581,7 +581,7 @@ call p_verify_status_increment(4, 0, 4, 0);
--echo # the binary log.
--echo #
select f1();
call p_verify_status_increment(3, 0, 3, 0);
call p_verify_status_increment(5, 0, 3, 0);
commit;
call p_verify_status_increment(1, 0, 1, 0);

Expand Down Expand Up @@ -620,14 +620,20 @@ create temporary table t2 (a int);
# One commit for the create temporary table, and two for committing the
# read of the stored procedure from Aria table (creating temporary table
# clears the sp cache).
call p_verify_status_increment(3, 0, 2, 0);
# After the sp cache cleard, another two commits increased due to the read
# of the stored procedure from mysql.proc table during the invocation of
# p_verify_status_increment() with using new sql path resolution mechanism.
call p_verify_status_increment(5, 0, 2, 0);
set sql_mode=default;
--echo # 19. A function changes temp-trans-table.
--echo #
select f1();
--echo # Two commits because a binary log record is written, and another two
--echo # as the function f1() is reloaded after creating temporary table.
call p_verify_status_increment(4, 0, 3, 0);
--echo # After the function f1() is reloaded, another two commits increased due
--echo # to the read of the stored procedure from mysql.proc table during the
--echo # invocation of f1() with using new sql path resolution mechanism.
call p_verify_status_increment(6, 0, 3, 0);
commit;
call p_verify_status_increment(2, 0, 1, 0);

Expand Down Expand Up @@ -682,7 +688,10 @@ call p_verify_status_increment(2, 0, 1, 0);
drop temporary table t2;
# Dropping temporary table clears SP caches, so get another two commit
# increments from loading the p_verify_status_increment procedure.
call p_verify_status_increment(3, 0, 2, 0);
# After the sp cache cleard, another two commits increased due to the read
# of the stored procedure from mysql.proc table during the invocation of
# p_verify_status_increment() with using new sql path resolution mechanism.
call p_verify_status_increment(5, 0, 2, 0);
commit;
call p_verify_status_increment(1, 0, 0, 0);

Expand Down Expand Up @@ -733,7 +742,10 @@ call p_verify_status_increment(0, 0, 0, 0);
do (select f1() from t1 where a=2);
# Again extra 2 commit increments from re-loading function f1 after
# dropping temporary table.
call p_verify_status_increment(4, 2, 4, 2);
# After the function f1() is reloaded, another two commits increased due
# to the read of the stored procedure from mysql.proc table during the
# invocation of f1() with using new sql path resolution mechanism.
call p_verify_status_increment(6, 2, 4, 2);
commit;
call p_verify_status_increment(2, 2, 2, 2);

Expand Down
4 changes: 4 additions & 0 deletions mysql-test/include/deselect_db.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Drop the current db. This de-selects any db.
CREATE DATABASE deselect_db;
USE deselect_db;
DROP DATABASE deselect_db;
15 changes: 9 additions & 6 deletions mysql-test/main/commit_1innodb.result
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ end|
flush status;
#
# Count of reading of p_verify_status_increment() from mysql.proc
call p_verify_status_increment(2, 0, 2, 0);
call p_verify_status_increment(4, 0, 2, 0);
SUCCESS

#
Expand Down Expand Up @@ -585,7 +585,7 @@ SUCCESS
select f1();
f1()
2
call p_verify_status_increment(3, 0, 3, 0);
call p_verify_status_increment(5, 0, 3, 0);
SUCCESS

commit;
Expand Down Expand Up @@ -634,7 +634,7 @@ SUCCESS
drop table t2;
set sql_mode=no_engine_substitution;
create temporary table t2 (a int);
call p_verify_status_increment(3, 0, 2, 0);
call p_verify_status_increment(5, 0, 2, 0);
SUCCESS

set sql_mode=default;
Expand All @@ -645,7 +645,10 @@ f1()
2
# Two commits because a binary log record is written, and another two
# as the function f1() is reloaded after creating temporary table.
call p_verify_status_increment(4, 0, 3, 0);
# After the function f1() is reloaded, another two commits increased due
# to the read of the stored procedure from mysql.proc table during the
# invocation of f1() with using new sql path resolution mechanism.
call p_verify_status_increment(6, 0, 3, 0);
SUCCESS

commit;
Expand Down Expand Up @@ -716,7 +719,7 @@ SUCCESS
# 25. DDL: DROP TEMPORARY TABLE, does not start a transaction
#
drop temporary table t2;
call p_verify_status_increment(3, 0, 2, 0);
call p_verify_status_increment(5, 0, 2, 0);
SUCCESS

commit;
Expand Down Expand Up @@ -802,7 +805,7 @@ call p_verify_status_increment(0, 0, 0, 0);
SUCCESS

do (select f1() from t1 where a=2);
call p_verify_status_increment(4, 2, 4, 2);
call p_verify_status_increment(6, 2, 4, 2);
SUCCESS

commit;
Expand Down
6 changes: 5 additions & 1 deletion mysql-test/main/lock_sync.result
Original file line number Diff line number Diff line change
Expand Up @@ -743,13 +743,17 @@ CREATE VIEW v1 AS SELECT * FROM t1 WHERE f1() = 1;
DROP FUNCTION f1;
connect con2, localhost, root;
connect con1, localhost, root;
SET DEBUG_SYNC= 'open_tables_after_open_and_process_table SIGNAL opened WAIT_FOR dropped EXECUTE 2';
SET DEBUG_SYNC= 'open_tables_after_open_and_process_table SIGNAL opened WAIT_FOR dropped EXECUTE 4';
# Sending:
SHOW CREATE VIEW v1;
connection con2;
SET DEBUG_SYNC= 'now WAIT_FOR opened';
SET DEBUG_SYNC= 'now SIGNAL dropped';
SET DEBUG_SYNC= 'now WAIT_FOR opened';
SET DEBUG_SYNC= 'now SIGNAL dropped';
SET DEBUG_SYNC= 'now WAIT_FOR opened';
SET DEBUG_SYNC= 'now SIGNAL dropped';
SET DEBUG_SYNC= 'now WAIT_FOR opened';
# Sending:
FLUSH TABLES t1;
connection default;
Expand Down
8 changes: 7 additions & 1 deletion mysql-test/main/lock_sync.test
Original file line number Diff line number Diff line change
Expand Up @@ -947,14 +947,20 @@ connect(con2, localhost, root);
connect (con1, localhost, root);
# Need to trigger this sync point at least twice in order to
# get valgrind test failures without the patch
SET DEBUG_SYNC= 'open_tables_after_open_and_process_table SIGNAL opened WAIT_FOR dropped EXECUTE 2';
SET DEBUG_SYNC= 'open_tables_after_open_and_process_table SIGNAL opened WAIT_FOR dropped EXECUTE 4';
--echo # Sending:
--send SHOW CREATE VIEW v1

connection con2;
SET DEBUG_SYNC= 'now WAIT_FOR opened';
SET DEBUG_SYNC= 'now SIGNAL dropped';
SET DEBUG_SYNC= 'now WAIT_FOR opened';
# Increase the count of sync point to 4 from 2 due to the new sql path
# need to perform the db + routine/package lookup
SET DEBUG_SYNC= 'now SIGNAL dropped';
SET DEBUG_SYNC= 'now WAIT_FOR opened';
SET DEBUG_SYNC= 'now SIGNAL dropped';
SET DEBUG_SYNC= 'now WAIT_FOR opened';
--echo # Sending:
--send FLUSH TABLES t1

Expand Down
2 changes: 2 additions & 0 deletions mysql-test/main/mysqld--help.result
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,7 @@ The following specify which files/extra groups are read (specified before remain
Cost of checking the row against the WHERE clause.
Increasing this will have the optimizer to prefer plans
with less row combinations
--path=name SET PATH statement
--performance-schema
Enable the performance schema
--performance-schema-accounts-size=#
Expand Down Expand Up @@ -1885,6 +1886,7 @@ optimizer-trace
optimizer-trace-max-mem-size 1048576
optimizer-use-condition-selectivity 4
optimizer-where-cost 0.032
path (No default value)
performance-schema FALSE
performance-schema-accounts-size -1
performance-schema-consumer-events-stages-current FALSE
Expand Down
Loading

0 comments on commit 8f87435

Please sign in to comment.