From 5b5a67b2a9aecdc41b35b5cb12ebbf960384f7bd Mon Sep 17 00:00:00 2001 From: Vladislav Vaintroub Date: Thu, 7 Oct 2021 19:52:18 +0200 Subject: [PATCH] MDEV-26786 Inserting NULL into base column breaks NATURAL_SORT_KEY column When returning non-null value from natural_sort_key, make sure Item::null_value is false. --- mysql-test/main/natural_sort_key.result | 13 +++++++++++++ mysql-test/main/natural_sort_key.test | 7 +++++++ sql/item_strfunc.cc | 2 +- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/mysql-test/main/natural_sort_key.result b/mysql-test/main/natural_sort_key.result index a99fa7a71b8..10adc7acef3 100644 --- a/mysql-test/main/natural_sort_key.result +++ b/mysql-test/main/natural_sort_key.result @@ -190,3 +190,16 @@ pic120 pic2120 pic121 pic2121 pic02000 pic32000 tom tom +create table t (a varchar(8), b varchar(8) as (natural_sort_key(a))); +insert into t (a) values ('a2'),(NULL),('a11'); +select * from t order by b; +a b +NULL NULL +a2 a02 +a11 a111 +select a, b from t order by b; +a b +NULL NULL +a2 a02 +a11 a111 +drop table t; diff --git a/mysql-test/main/natural_sort_key.test b/mysql-test/main/natural_sort_key.test index dda25c9e1f8..4f4e2b0e05e 100644 --- a/mysql-test/main/natural_sort_key.test +++ b/mysql-test/main/natural_sort_key.test @@ -86,3 +86,10 @@ SELECT RPAD(val,20,' ') value, NATURAL_SORT_KEY(val) FROM ('a01b3') )AS data ORDER BY 2,1; +# MDEV-27686 (null value indicator not always reset) +create table t (a varchar(8), b varchar(8) as (natural_sort_key(a))); +insert into t (a) values ('a2'),(NULL),('a11'); +select * from t order by b; +select a, b from t order by b; +drop table t; + diff --git a/sql/item_strfunc.cc b/sql/item_strfunc.cc index 60e2d297459..f43115fef54 100644 --- a/sql/item_strfunc.cc +++ b/sql/item_strfunc.cc @@ -5682,7 +5682,7 @@ String *Item_func_natural_sort_key::val_str(String *out) if (tmp.copy(out, cs, &errs) || out->copy(tmp)) goto error_exit; } - + null_value= false; return out; error_exit: