Bug #116454 For mysql8.0.32 DEBUG version,it crash when do sql about ANALYZE TABLE USING DAT
Submitted: 23 Oct 2024 2:29 Modified: 23 Oct 2024 5:40
Reporter: wu sunny Email Updates:
Status: Can't repeat Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S6 (Debug Builds)
Version:8.0.32 OS:Any
Assigned to: CPU Architecture:Any
Tags: ANALYZE TABLE USING DATA, crash, debug

[23 Oct 2024 2:29] wu sunny
Description:
For mysql8.0.32 DEBUG version,it crash when do sql about ANALYZE TABLE USING DATA,and it's OK in release version.
The reason is that not do bitmap_set_bit before Field->store when do histograms::check_value_aux.

How to repeat:
For mysql8.0.32 DEBUG version:
CREATE TABLE t2 (cc1 INT PRIMARY KEY, cc2 INT);
INSERT INTO t2 VALUES (1,3),(2,1),(3,2),(4,3),(5,15);
ANALYZE TABLE t2 UPDATE HISTOGRAM ON cc2 USING DATA '{"buckets": [[1, 0.25], [2, 0.5], [3, 0.625], [15, 0.75]], "data-type": "int", "null-values": 0.25, "collation-id": 8, "sampling-rate": 1.0, "histogram-type": "singleton", "number-of-buckets-specified": 4}'; -->It made crash

print the stack:
Thread 56 "connection" received signal SIGABRT, Aborted.
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
50	../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
#1  0x00007ffff750b859 in __GI_abort () at abort.c:79
#2  0x00007ffff750b729 in __assert_fail_base (
    fmt=0x7ffff76a1588 "%s%s%s:%u: %s%sAssertion `%s' failed.\n%n", 
    assertion=0x55555c9d7c60 "!table || (!table->write_set || bitmap_is_set(table->write_set, field_index()))", file=0x55555c9d75a0 "sql/field.cc", 
    line=4356, function=<optimized out>) at assert.c:92
#3  0x00007ffff751cfd6 in __GI___assert_fail (
    assertion=0x55555c9d7c60 "!table || (!table->write_set || bitmap_is_set(table->write_set, field_index()))", file=0x55555c9d75a0 "sql/field.cc", 
    line=4356, 
    function=0x55555c9d8ca0 "virtual type_conversion_status Field_long::store(longlong, bool)")
    at assert.c:101
#4  0x0000555559b052bc in Field_long::store (this=0x7fff14ac25a0, nr=1, unsigned_val=false)
    at sql/field.cc:4356
#5  0x0000555559b4ad7b in histograms::check_value_aux<long long> (field=0x7fff14ac25a0, 
    nr=0x7fffbc5f2998)
    at sql/histograms/histogram.cc:311
#6  0x0000555559b54cba in histograms::Error_context::check_value<long long> (this=0x7fffbc5f2d60, 
    v=0x7fffbc5f2998) at sql/histograms/histogram.cc:334
#7  0x0000555559b665d2 in histograms::Singleton<long long>::json_to_histogram (
    this=0x7fff14011150, json_object=..., context=0x7fffbc5f2d60)
    at sql/histograms/singleton.cc:338
#8  0x0000555559b4c49b in histograms::Histogram::json_to_histogram (mem_root=0x7fffbc5f2ca0, 
    schema_name="db1", table_name="t2", column_name="cc2", json_object=..., context=0x7fffbc5f2d60)
    at sql/histograms/histogram.cc:649
#9  0x0000555559b4fd07 in histograms::update_histogram (thd=0x7fff14001050, table=0x7fff14ac1e58, 
    Python Exception <class 'AttributeError'> 'NoneType' object has no attribute 'pointer': 
columns=std::set with 1 element, num_buckets=0, data=..., Python Exception <class 'AttributeError'> 'NoneType' object has no attribute 'pointer': 
results=std::map with 0 elements)
    at sql/histograms/histogram.cc:1414
#10 0x000055555949e85f in Sql_cmd_analyze_table::update_histogram (this=0x7fff14ac24f0, 
    thd=0x7fff14001050, table=0x7fff14ac1e58, Python Exception <class 'AttributeError'> 'NoneType' object has no attribute 'pointer': 
results=std::map with 0 elements)
    at sql/sql_admin.cc:628
#11 0x00005555594a21bf in Sql_cmd_analyze_table::handle_histogram_command (this=0x7fff14ac24f0, 
    thd=0x7fff14001050, table=0x7fff14ac1e58)
    at sql/sql_admin.cc:1675
#12 0x00005555594a2571 in Sql_cmd_analyze_table::execute (this=0x7fff14ac24f0, thd=0x7fff14001050)
    at sql/sql_admin.cc:1737

Suggested fix:
diff --git a/sql/histograms/histogram.cc b/sql/histograms/histogram.cc
index 4da9f514e96..ad7388e86d6 100644
--- a/sql/histograms/histogram.cc
+++ b/sql/histograms/histogram.cc
@@ -298,31 +298,37 @@ type_conversion_status check_value_aux(Field *f, T *v);
 
 template <>
 type_conversion_status check_value_aux(Field *field, double *nr) {
+  bitmap_set_bit(field->table->write_set, field->field_index());
   return field->store(*nr);
 }
 
 template <>
 type_conversion_status check_value_aux(Field *field, String *str) {
+  bitmap_set_bit(field->table->write_set, field->field_index());
   return field->store(str->ptr(), str->length(), str->charset());
 }
 
 template <>
 type_conversion_status check_value_aux(Field *field, longlong *nr) {
+  bitmap_set_bit(field->table->write_set, field->field_index());
   return field->store(*nr, false);
 }
 
 template <>
 type_conversion_status check_value_aux(Field *field, ulonglong *nr) {
+  bitmap_set_bit(field->table->write_set, field->field_index());
   return field->store(*nr, true);
 }
 
 template <>
 type_conversion_status check_value_aux(Field *field, MYSQL_TIME *ltime) {
+  bitmap_set_bit(field->table->write_set, field->field_index());
   return field->store_time(ltime);
 }
 
 template <>
 type_conversion_status check_value_aux(Field *field, my_decimal *mdec) {
+  bitmap_set_bit(field->table->write_set, field->field_index());
   return field->store_decimal(mdec);
 }
[23 Oct 2024 3:21] wu sunny
It can also be resolved by next patch:
diff --git a/sql/histograms/histogram.cc b/sql/histograms/histogram.cc
index 4da9f514e96..691dad17284 100644
--- a/sql/histograms/histogram.cc
+++ b/sql/histograms/histogram.cc
@@ -221,6 +221,7 @@ Error_context::Error_context(THD *thd, Field *field, TABLE *table,
   m_field = make_field(create_field, table->s, m_buffer + 1, m_buffer,
                        0 /* null bit*/);
   m_field->table = table;
+  bitmap_set_bit(table->write_set, m_field->field_index());
 }
 
 void Error_context::report_global(Message err_code) {
[23 Oct 2024 5:40] MySQL Verification Team
Hello wu sunny,

Thank you for the report and test case.
I can confirm that 8.0.32-8.0.35 debug builds are affected but this issue is no longer seen since 8.0.36+. 
Looking at the change log, this is most likely fixed after Bug #35710383. Please see https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-36.html

UPDATE HISTOGRAM did not behave as expected in all cases.
UPDATE HISTOGRAM did not behave as expected in all cases. (Bug #35710404)

-- 8.0.32(lowest checked)-8.0.35

bin/mysql -uroot -S/tmp/mysql.sock
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 8.0.35-debug MySQL Community Server - GPL - Debug

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database test;
Query OK, 1 row affected (0.01 sec)

mysql> use test
Database changed
mysql> CREATE TABLE t2 (cc1 INT PRIMARY KEY, cc2 INT);
Query OK, 0 rows affected (0.05 sec)

mysql> INSERT INTO t2 VALUES (1,3),(2,1),(3,2),(4,3),(5,15);
Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> ANALYZE TABLE t2 UPDATE HISTOGRAM ON cc2 USING DATA '{"buckets": [[1, 0.25], [2, 0.5], [3, 0.625], [15, 0.75]], "data-type": "int", "null-values": 0.25, "collation-id": 8, "sampling-rate": 1.0, "histogram-type": "singleton", "number-of-buckets-specified": 4}';
ERROR 2013 (HY000): Lost connection to MySQL server during query

-- 8.0.36+ no issue seen

bin/mysql -uroot -S/tmp/mysql.sock
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 8.0.36-debug MySQL Community Server - GPL - Debug

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database test;
Query OK, 1 row affected (0.01 sec)

mysql> use test
Database changed
mysql> CREATE TABLE t2 (cc1 INT PRIMARY KEY, cc2 INT);
Query OK, 0 rows affected (0.05 sec)

mysql> INSERT INTO t2 VALUES (1,3),(2,1),(3,2),(4,3),(5,15);
Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> ANALYZE TABLE t2 UPDATE HISTOGRAM ON cc2 USING DATA '{"buckets": [[1, 0.25], [2, 0.5], [3, 0.625], [15, 0.75]], "data-type": "int", "null-values": 0.25, "collation-id": 8, "sampling-rate": 1.0, "histogram-type": "singleton", "number-of-buckets-specified": 4}';
+---------+-----------+----------+------------------------------------------------+
| Table   | Op        | Msg_type | Msg_text                                       |
+---------+-----------+----------+------------------------------------------------+
| test.t2 | histogram | status   | Histogram statistics created for column 'cc2'. |
+---------+-----------+----------+------------------------------------------------+
1 row in set (0.00 sec)

regards,
Umesh