Bug #111376 Assertion `!thd->lex->is_exec_started() || thd->lex->is_query_tables_locked()' f
Submitted: 13 Jun 2023 2:37 Modified: 13 Jun 2023 7:48
Reporter: Yu Liang Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: DDL Severity:S6 (Debug Builds)
Version:8.0.33 OS:Ubuntu (20.04 LTS)
Assigned to: CPU Architecture:x86 (Intel(R) Core(TM) i7-10700 CPU)

[13 Jun 2023 2:37] Yu Liang
Description:
The latest version of the MySQL Server (version 8.0.33) (git commit hash: ea7087d8850) crashes with Assertion Failure when executing the following query:

Config from "/etc/mysql/conf.d/mysql.cnf":

```
[mysqld]
sql_mode = "NO_ENGINE_SUBSTITUTION"
```

```sql
drop database if exists test123;
create database test123;
use test123;
create table v0(c1 INT);
CREATE TABLE v3 ( c4 NATIONAL CHAR ) PARTITION BY HASH ( c1 ) AS TABLE v0 ;
CREATE TABLE v5 ( INDEX ( ( c4 SOUNDS LIKE TRUE ) ) ) AS TABLE v3 ;
```

The last query returns error: 

```
mysql> CREATE TABLE v5 ( INDEX ( ( c4 SOUNDS LIKE TRUE ) ) ) AS TABLE v3 ;
mysqld: /home/mysql/mysql-server/sql/item.cc:1202: bool Item::may_evaluate_const(const THD *) const: Assertion `!thd->lex->is_exec_started() || thd->lex->is_query_tables_locked()' failed.
2023-06-13T01:25:39Z UTC - mysqld got signal 6 ;
Most likely, you have hit a bug, but this error can also be caused by malfunctioning hardware.
BuildID[sha1]=1d55d4671b7469352fc317efb0121b604c26443d
Thread pointer: 0xfffee8001040
Attempting backtrace. You can use the following information to find out
where mysqld died. If you see no messages after this, something went
terribly wrong...
stack_bottom = ffff7c613538 thread_stack 0x100000
/home/mysql/mysql-server/bld/runtime_output_directory/mysqld(my_print_stacktrace(unsigned char const*, unsigned long)+0x60) [0x4e6bc30]
/home/mysql/mysql-server/bld/runtime_output_directory/mysqld(print_fatal_signal(int)+0x340) [0x1a89320]
/home/mysql/mysql-server/bld/runtime_output_directory/mysqld(handle_fatal_signal+0x16c) [0x1a896dc]
linux-vdso.so.1(__kernel_rt_sigreturn+0) [0xffff86cc27a0]
/lib/aarch64-linux-gnu/libc.so.6(gsignal+0xe0) [0xffff86336d78]
/lib/aarch64-linux-gnu/libc.so.6(abort+0x114) [0xffff86323aac]
/lib/aarch64-linux-gnu/libc.so.6(+0x2d490) [0xffff86330490]
/lib/aarch64-linux-gnu/libc.so.6(+0x2d4f4) [0xffff863304f4]
/home/mysql/mysql-server/bld/runtime_output_directory/mysqld(Item::check_cols(unsigned int)+0) [0x1e868bc]
/home/mysql/mysql-server/bld/runtime_output_directory/mysqld(Item_charset_conversion::Item_charset_conversion(THD*, Item*, CHARSET_INFO const*, bool)+0x138) [0x1ed8a00]
/home/mysql/mysql-server/bld/runtime_output_directory/mysqld(Item::safe_charset_converter(THD*, CHARSET_INFO const*)+0x84) [0x1e870e8]
/home/mysql/mysql-server/bld/runtime_output_directory/mysqld(agg_item_set_converter(DTCollation&, char const*, Item**, unsigned int, unsigned int, int, bool)+0x2c8) [0x1e90ab0]
/home/mysql/mysql-server/bld/runtime_output_directory/mysqld(agg_item_charsets(DTCollation&, char const*, Item**, unsigned int, unsigned int, int, bool)+0x90) [0x1e91298]
/home/mysql/mysql-server/bld/runtime_output_directory/mysqld(Item_bool_func2::resolve_type(THD*)+0x1b8) [0x1ee1430]
/home/mysql/mysql-server/bld/runtime_output_directory/mysqld(Item_func::fix_fields(THD*, Item**)+0x230) [0x1fb9194]
/home/mysql/mysql-server/bld/runtime_output_directory/mysqld(Key_part_spec::resolve_expression(THD*)+0x170) [0x236c8e4]
/home/mysql/mysql-server/bld/runtime_output_directory/mysqld(mysql_prepare_create_table(THD*, char const*, char const*, HA_CREATE_INFO*, Alter_info*, handler*, bool, KEY**, unsigned int*, FOREIGN_KEY**, unsigned int*, FOREIGN_KEY*, unsigned int, dd::Table const*, unsigned int, int, bool)+0x1fac) [0x17be364]
/home/mysql/mysql-server/bld/runtime_output_directory/mysqld() [0x17d6c9c]
/home/mysql/mysql-server/bld/runtime_output_directory/mysqld(mysql_create_table_no_lock(THD*, char const*, char const*, HA_CREATE_INFO*, Alter_info*, unsigned int, bool, bool*, handlerton**)+0x14c0) [0x17d17ec]
/home/mysql/mysql-server/bld/runtime_output_directory/mysqld(Query_result_create::create_table_for_query_block(THD*)+0x11c4) [0x25bfee0]
/home/mysql/mysql-server/bld/runtime_output_directory/mysqld(Sql_cmd_create_table::execute(THD*)+0x1d18) [0x2573704]
/home/mysql/mysql-server/bld/runtime_output_directory/mysqld(mysql_execute_command(THD*, bool)+0x3884) [0x162cd40]
/home/mysql/mysql-server/bld/runtime_output_directory/mysqld(dispatch_sql_command(THD*, Parser_state*)+0x1124) [0x16252c0]
/home/mysql/mysql-server/bld/runtime_output_directory/mysqld(dispatch_command(THD*, COM_DATA const*, enum_server_command)+0x5434) [0x161e224]
/home/mysql/mysql-server/bld/runtime_output_directory/mysqld(do_command(THD*)+0xb8c) [0x16228f4]
/home/mysql/mysql-server/bld/runtime_output_directory/mysqld() [0x1a59a64]
/home/mysql/mysql-server/bld/runtime_output_directory/mysqld() [0x655c1a0]
/lib/aarch64-linux-gnu/libpthread.so.0(+0x7624) [0xffff86c68624]
/lib/aarch64-linux-gnu/libc.so.6(+0xd149c) [0xffff863d449c]

Trying to get some variables.
Some pointers may be invalid and cause the dump to abort.
Query (fffee81f0080): CREATE TABLE v5 ( INDEX ( ( c4 SOUNDS LIKE TRUE ) ) ) AS TABLE v3
Connection ID (thread ID): 8
Status: NOT_KILLED
```

How to repeat:
Steps to repeat the Assertion Failure:
1. Download the MySQL Server source code from the official github repo: `https://github.com/mysql/mysql-server`
2. Checkout to the latest mysql released version: 8.0.33 (hash: `ea7087d8850`)
3. Compile MySQL using the command: 

```
mkdir -p bld
cd bld
cmake .. -DDOWNLOAD_BOOST=1 -DWITH_BOOST=../boost -DWITH_UNIT_TESTS=OFF -DUSE_LD_GOLD=1 -DWITH_DEBUG=1
make
```

4. Run the MySQL Server with command: 

```
./bin/mysqld --basedir=$(pwd) --datadir=$(pwd)/data_all/ori_data --port=7000  --socket=/tmp/mysql_0.sock --mysqlx=OFF --performance_schema=OFF
```

5. Setup the MySQL Server config in the path: "/etc/mysql/conf.d/mysql.cnf"

```
[mysqld]
sql_mode = "NO_ENGINE_SUBSTITUTION"
```

6. Run the MySQL Client with the PoC:

```
./bin/mysql --port=7000 --user=root --socket=/tmp/mysql_0.sock < poc_0.sql
```

where `poc_0.sql` is:

```sql
drop database if exists test123;
create database test123;
use test123;
create table v0(c1 INT);
CREATE TABLE v3 ( c4 NATIONAL CHAR ) PARTITION BY HASH ( c1 ) AS TABLE v0 ;
CREATE TABLE v5 ( INDEX ( ( c4 SOUNDS LIKE TRUE ) ) ) AS TABLE v3 ;
```

Suggested fix:
The server should continue running instead of crashing by Assertion Failure.
[13 Jun 2023 7:48] MySQL Verification Team
Hello Yu Liang,

Thank you for the report and test case.
Observed that 8.0.33 only debug build is affected.

regards,
Umesh
[13 Jun 2023 7:49] MySQL Verification Team
-- release build
 ./mtr bug111376 --nocheck-testcases
Logging: ./mtr  bug111376 --nocheck-testcases
MySQL Version 8.0.33
Checking supported features
Using 'all' suites
Collecting tests
Checking leftover processes
Removing old var directory
Creating var directory '/export/home/tmp/ushastry/binaries/mysql-8.0.33/mysql-test/var'
Installing system database
Using parallel: 1

==============================================================================
                  TEST NAME                       RESULT  TIME (ms) COMMENT
------------------------------------------------------------------------------
drop database if exists test123;
Warnings:
Note    1008    Can't drop database 'test123'; database doesn't exist
create database test123;
use test123;
create table v0(c1 INT);
CREATE TABLE v3 ( c4 NATIONAL CHAR ) PARTITION BY HASH ( c1 ) AS TABLE v0 ;
Warnings:
Warning 3720    NATIONAL/NCHAR/NVARCHAR implies the character set UTF8MB3, which will be replaced by UTF8MB4 in a future release. Please consider using CHAR(x) CHARACTER SET UTF8MB4 in order to be unambiguous.
CREATE TABLE v5 ( INDEX ( ( c4 SOUNDS LIKE TRUE ) ) ) AS TABLE v3 ;
[ 50%] main.bug111376                            [ pass ]    128
[100%] shutdown_report                           [ pass ]

-- debug build - affected

 ./mtr bug111376 --nocheck-testcases --debug-server
Logging: ./mtr  bug111376 --nocheck-testcases --debug-server
MySQL Version 8.0.33
Checking supported features
 - Binaries are debug compiled
Using 'all' suites
Collecting tests
Checking leftover processes
Removing old var directory
Creating var directory '/export/home/tmp/ushastry/binaries/mysql-8.0.33/mysql-test/var'
Installing system database
Using parallel: 1

==============================================================================
                  TEST NAME                       RESULT  TIME (ms) COMMENT
------------------------------------------------------------------------------
drop database if exists test123;
Warnings:
Note    1008    Can't drop database 'test123'; database doesn't exist
create database test123;
use test123;
create table v0(c1 INT);
CREATE TABLE v3 ( c4 NATIONAL CHAR ) PARTITION BY HASH ( c1 ) AS TABLE v0 ;
Warnings:
Warning 3720    NATIONAL/NCHAR/NVARCHAR implies the character set UTF8MB3, which will be replaced by UTF8MB4 in a future release. Please consider using CHAR(x) CHARACTER SET UTF8MB4 in order to be unambiguous.
CREATE TABLE v5 ( INDEX ( ( c4 SOUNDS LIKE TRUE ) ) ) AS TABLE v3 ;
[ 50%] main.bug111376                            [ fail ]
        Test ended at 2023-06-13 09:48:01

CURRENT_TEST: main.bug111376
mysqltest: At line 6: Query 'CREATE TABLE v5 ( INDEX ( ( c4 SOUNDS LIKE TRUE ) ) ) AS TABLE v3 ' failed.
ERROR 2013 (HY000): Lost connection to MySQL server during query

-bt

#0  0x00007fa735e43aa1 in pthread_kill () from /lib64/libpthread.so.0
#1  0x0000000004005426 in my_write_core(int) ()
#2  0x00000000032b0166 in handle_fatal_signal ()
#3  <signal handler called>
#4  0x00007fa734304387 in raise () from /lib64/libc.so.6
#5  0x00007fa734305a78 in abort () from /lib64/libc.so.6
#6  0x00007fa7342fd1a6 in __assert_fail_base () from /lib64/libc.so.6
#7  0x00007fa7342fd252 in __assert_fail () from /lib64/libc.so.6
#8  0x00000000033f5684 in Item::may_evaluate_const(THD const*) const ()
#9  0x000000000340b445 in Item_charset_conversion::Item_charset_conversion(THD*, Item*, CHARSET_INFO const*, bool) ()
#10 0x000000000340b54d in Item_func_conv_charset::Item_func_conv_charset(THD*, Item*, CHARSET_INFO const*, bool) ()
#11 0x00000000033fb15d in Item::safe_charset_converter(THD*, CHARSET_INFO const*) ()
#12 0x00000000033fb366 in agg_item_set_converter(DTCollation&, char const*, Item**, unsigned int, unsigned int, int, bool) ()
#13 0x00000000033fb436 in agg_item_charsets(DTCollation&, char const*, Item**, unsigned int, unsigned int, int, bool) ()
#14 0x00000000034221b5 in agg_item_charsets_for_comparison(DTCollation&, char const*, Item**, unsigned int, int) ()
#15 0x00000000034224c2 in Item_func::agg_arg_charsets_for_comparison(DTCollation&, Item**, unsigned int, int) ()
#16 0x0000000003419829 in Item_bool_func2::resolve_type(THD*) ()
#17 0x00000000034585ca in Item_func::fix_fields(THD*, Item**) ()
#18 0x000000000354a683 in Key_part_spec::resolve_expression(THD*) ()
#19 0x00000000031d8649 in add_functional_index_to_create_list(THD*, Key_spec*, Alter_info*, Key_part_spec*, unsigned int, HA_CREATE_INFO*) ()
#20 0x00000000031da8ba in mysql_prepare_create_table(THD*, char const*, char const*, HA_CREATE_INFO*, Alter_info*, handler*, bool, KEY**, unsigned int*, FOREIGN_KEY**, unsigned int*, FOREIGN_KEY*, unsigned int, dd::Table const*, unsigned int, int, bool) ()
#21 0x00000000031dc05c in create_table_impl(THD*, dd::Schema const&, char const*, char const*, char const*, char const*, HA_CREATE_INFO*, Alter_info*, bool, unsigned int, bool, bool, bool, bool*, KEY**, unsigned int*, Alter_info::enum_enable_or_disable, FOREIGN_KEY**, unsigned int*, FOREIGN_KEY*, unsigned int, dd::Table const*, unsigned int, std::unique_ptr<dd::Table, std::default_delete<dd::Table> >*, handlerton**) ()
#22 0x00000000031dcaa5 in mysql_create_table_no_lock(THD*, char const*, char const*, HA_CREATE_INFO*, Alter_info*, unsigned int, bool, bool*, handlerton**) ()
#23 0x0000000003602485 in create_table_from_items(THD*, HA_CREATE_INFO*, Table_ref*, Alter_info*, mem_root_deque<Item*> const&, handlerton**) ()
#24 0x0000000003602668 in Query_result_create::create_table_for_query_block(THD*) ()
#25 0x00000000035ede58 in populate_table(THD*, LEX*) ()
#26 0x00000000035eeb2c in Sql_cmd_create_table::execute(THD*) ()
#27 0x000000000313d2da in mysql_execute_command(THD*, bool) ()
#28 0x0000000003140bc8 in dispatch_sql_command(THD*, Parser_state*) ()
#29 0x00000000031421fb in dispatch_command(THD*, COM_DATA const*, enum_server_command) ()
#30 0x0000000003143e97 in do_command(THD*) ()
#31 0x00000000032a219e in handle_connection ()
#32 0x00000000047b1601 in pfs_spawn_thread ()
#33 0x00007fa735e3eea5 in start_thread () from /lib64/libpthread.so.0
#34 0x00007fa7343ccb2d in clone () from /lib64/libc.so.6