diff --git a/mysql-test/main/vector2.result b/mysql-test/main/vector2.result index 1d3ccbea13d..1273a214173 100644 --- a/mysql-test/main/vector2.result +++ b/mysql-test/main/vector2.result @@ -436,3 +436,21 @@ id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE t ALL NULL NULL NULL NULL 2 Using filesort drop table t; # End of 11.7 tests +# +# MDEV-35309 - ALTER performs vector truncation without WARN_DATA_TRUNCATED or similar warnings/errors +# +create table t (v vector(2)); +insert into t values (0x3131313132323232); +select * from t; +v +11112222 +alter table t modify v vector(1); +ERROR 01000: Data truncated for column 'v' at row 1 +set statement sql_mode='' for alter table t modify v vector(1); +Warnings: +Warning 1265 Data truncated for column 'v' at row 1 +select * from t; +v +1111 +drop table t; +# End of 11.8 tests diff --git a/mysql-test/main/vector2.test b/mysql-test/main/vector2.test index ca50dd5427e..087d9a8916e 100644 --- a/mysql-test/main/vector2.test +++ b/mysql-test/main/vector2.test @@ -318,3 +318,17 @@ insert into t values (0x00000000),(0x00000000); explain select vec_totext(a) from t order by vec_distance_euclidean(a,0x00000000) limit 1; drop table t; --echo # End of 11.7 tests + +--echo # +--echo # MDEV-35309 - ALTER performs vector truncation without WARN_DATA_TRUNCATED or similar warnings/errors +--echo # +create table t (v vector(2)); +insert into t values (0x3131313132323232); +select * from t; +--error WARN_DATA_TRUNCATED +alter table t modify v vector(1); +set statement sql_mode='' for alter table t modify v vector(1); +select * from t; +drop table t; + +--echo # End of 11.8 tests diff --git a/sql/sql_type_vector.cc b/sql/sql_type_vector.cc index b8d7f385c5d..8cb87ccbf9e 100644 --- a/sql/sql_type_vector.cc +++ b/sql/sql_type_vector.cc @@ -256,7 +256,22 @@ static void do_copy_vec(const Copy_field *copy) int2store(copy->to_ptr, to_length); if (from_length > to_length) + { memcpy(to, from, to_length); + if (copy->from_field->table->in_use->count_cuted_fields > + CHECK_FIELD_EXPRESSION) + { + for (uint i= to_length; i < from_length; i++) + { + if (from[i] != 0) + { + copy->to_field->set_warning(Sql_condition::WARN_LEVEL_WARN, + WARN_DATA_TRUNCATED, 1); + break; + } + } + } + } else { memcpy(to, from, from_length);