Skip to content

Commit

Permalink
Fixed(PS-7745) - (Stored procedure execution fails if contains a func…
Browse files Browse the repository at this point in the history
…tional index)

https://jira.percona.com/browse/PS-7745

**Problem:**

A Stored Procedure execution fails after some executions when a functional index is present in the SP definition. The error behaves differently depending on the versions I tested:

8.0.23 (PS, Community): It only fails when using a temporary table. After the third execution, all the subsequent executions fail.

8.0.25 (Community): It also fails on normal tables.

How to Repeat

```
DROP PROCEDURE IF EXISTS `sproc`;
DELIMITER $$
CREATE PROCEDURE `sproc`()
BEGIN
DROP TEMPORARY TABLE IF EXISTS `T1`;
CREATE TEMPORARY TABLE `T1`(
`C1` INT,
KEY ( ( `C1` > 0 ) )
);

SELECT 1 FROM ( SELECT 1 `C2` ) `T2`;
END$$
DELIMITER ;
Now if you invoke that procedure 3 times in a row:

mysql> CALL `sproc`(); CALL `sproc`(); CALL `sproc`();
+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

ERROR 3107 (HY000): Generated column can refer only to generated columns defined prior to it.
On 8.0.25 (Community) The error is

ERROR 3754 (HY000): Functional index 'functional_index' cannot refer to an auto-increment column.
```

**Cause:**

The issue happens when checks are performed again during a query
in the function `pre_validate_value_generator_expr()`
and column index of the functional index is not available.

**Solution:**

This solution disables some checks if a column index is not available
in the function `pre_validate_value_generator_expr()` during the second
and later queries.
It is safe because these checks are done in the function
`validate_value_generator_expr()` during the first query.
The column index is available there.
  • Loading branch information
Daniel R. Fiala committed Feb 8, 2023
1 parent ab38037 commit 4e6484d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
20 changes: 20 additions & 0 deletions mysql-test/suite/rocksdb/r/issue7745.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
CREATE PROCEDURE `sproc`()
BEGIN
DROP TEMPORARY TABLE IF EXISTS `t1`;
CREATE TEMPORARY TABLE `t1`(
`c1` INT,
KEY ( ( `c1` > 0 ) )
);
SELECT 1 FROM ( SELECT 1 `c2` ) `t2`;
END|
CALL `sproc`();
1
1
CALL `sproc`();
1
1
CALL `sproc`();
1
1
DROP PROCEDURE `sproc`;
DROP TEMPORARY TABLE `t1`;
18 changes: 18 additions & 0 deletions mysql-test/suite/rocksdb/t/issue7745.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--source include/have_rocksdb.inc

DELIMITER |;
CREATE PROCEDURE `sproc`()
BEGIN
DROP TEMPORARY TABLE IF EXISTS `t1`;
CREATE TEMPORARY TABLE `t1`(
`c1` INT,
KEY ( ( `c1` > 0 ) )
);
SELECT 1 FROM ( SELECT 1 `c2` ) `t2`;
END|
DELIMITER ;|

CALL `sproc`(); CALL `sproc`(); CALL `sproc`();

DROP PROCEDURE `sproc`;
DROP TEMPORARY TABLE `t1`;
3 changes: 1 addition & 2 deletions sql/item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,6 @@ bool Item_field::check_function_as_value_generator(uchar *checker_args) {
}

int fld_idx = func_args->col_index;
assert(fld_idx > -1);

/*
Don't allow the GC (or default expression) to refer itself or another GC
Expand All @@ -1046,7 +1045,7 @@ bool Item_field::check_function_as_value_generator(uchar *checker_args) {
if ((func_args->source != VGS_CHECK_CONSTRAINT) &&
(field->is_gcol() ||
field->has_insert_default_general_value_expression()) &&
field->field_index() >= fld_idx) {
(fld_idx >= 0 && field->field_index() >= fld_idx)) {
func_args->err_code = (func_args->source == VGS_GENERATED_COLUMN)
? ER_GENERATED_COLUMN_NON_PRIOR
: ER_DEFAULT_VAL_GENERATED_NON_PRIOR;
Expand Down

0 comments on commit 4e6484d

Please sign in to comment.