Skip to content

Commit

Permalink
MDEV-34025 Virtual columns do not check assignment cast validity
Browse files Browse the repository at this point in the history
It was possible to create virtual columns with incompatible
GENERATED ALWAYS expression data types:

  CREATE TABLE t1 (a INT, b POINT GENERATED ALWAYS AS (a));
  CREATE TABLE t1 (a POINT, b INT GENERATED ALWAYS AS (a));

These data type combinations are not allowed in other cases,
e.g. INSERT, UPDATE, SP variable assignment.

Fix:

Disallowing bad combinations of the column data type and its
GENERATED ALWAYS expression data type.
  • Loading branch information
abarkov committed Apr 29, 2024
1 parent f582ea4 commit f151c5f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
7 changes: 7 additions & 0 deletions mysql-test/main/gis.result
Expand Up @@ -5501,5 +5501,12 @@ DROP TABLE t2,t3,t4,t5,t6,t7,t8,t9;
DROP VIEW v2,v3,v4,v5,v6,v7,v8,v9;
DROP TABLE t1;
#
# MDEV-34025 Virtual columns do not check assignment cast validity
#
CREATE TABLE t1 (a INT, b POINT GENERATED ALWAYS AS (a));
ERROR HY000: Cannot cast 'int' as 'point' in assignment of `test`.`t1`.`b`
CREATE TABLE t1 (a POINT, b INT GENERATED ALWAYS AS (a));
ERROR HY000: Cannot cast 'point' as 'int' in assignment of `test`.`t1`.`b`
#
# End of 11.5 tests
#
12 changes: 12 additions & 0 deletions mysql-test/main/gis.test
Expand Up @@ -3499,6 +3499,18 @@ DROP TABLE t2,t3,t4,t5,t6,t7,t8,t9;
DROP VIEW v2,v3,v4,v5,v6,v7,v8,v9;
DROP TABLE t1;


--echo #
--echo # MDEV-34025 Virtual columns do not check assignment cast validity
--echo #

--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
CREATE TABLE t1 (a INT, b POINT GENERATED ALWAYS AS (a));

--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
CREATE TABLE t1 (a POINT, b INT GENERATED ALWAYS AS (a));


--echo #
--echo # End of 11.5 tests
--echo #
4 changes: 3 additions & 1 deletion sql/table.cc
Expand Up @@ -1237,7 +1237,9 @@ bool parse_vcol_defs(THD *thd, MEM_ROOT *mem_root, TABLE *table,
Set table->map to non-zero temporarily.
*/
table->map= 1;
if (vcol && field_ptr[0]->check_vcol_sql_mode_dependency(thd, mode))
if (vcol &&
(field_ptr[0]->check_vcol_sql_mode_dependency(thd, mode) ||
vcol->expr->check_assignability_to(field_ptr[0], false)))
{
DBUG_ASSERT(thd->is_error());
*error_reported= true;
Expand Down

0 comments on commit f151c5f

Please sign in to comment.