stoneatom / stonedb

StoneDB is an Open-Source MySQL HTAP and MySQL-Native DataBase for OLTP, Real-Time Analytics, a counterpart of MySQLHeatWave. (https://stonedb.io)
https://stonedb.io/
GNU General Public License v2.0
862 stars 139 forks source link

feature: in certain scenario, UNION clause is not supported #1168

Open davidshiz opened 1 year ago

davidshiz commented 1 year ago

Is your feature request related to a problem? Please describe.

mysql> create table t1 (a int);
Query OK, 0 rows affected (0.02 sec)

mysql> insert into t1 values (120), (0), (111);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select a+0 from t1 union select NULL;
ERROR 6 (HY000): The query includes syntax that is not supported by the storage engine. Either restructure the query with supported syntax, or enable the MySQL core::Query Path in config file to execute the query with reduced performance.
lujiashun commented 1 year ago

ACK

lujiashun commented 1 year ago
(gdb) bt
#0  0x000055bc66ad33b2 in __cxa_throw ()
#1  0x000055bc6657f970 in Tianmu::core::Query::Compile (this=0x7f64883f5ff0, compiled_query=0x7f64883f5f20, selects_list=0x7f5f64005b90,
    last_distinct=0x7f5f640072f0, res_tab=0x0, ignore_limit=false, left_expr_for_subselect=0x0, oper_for_subselect=0x0,
    ignore_minmax=false, for_subq_in_where=false) at /data/codebase/stonedb57/stonedb/storage/tianmu/core/query_compile.cpp:1103
#2  0x000055bc665260cd in Tianmu::core::Engine::Execute (this=0x55bc68786610, thd=0x7f5f64000e10, lex=0x7f5f64003138,
    result_output=0x7f5f64007988, unit_for_union=0x7f5f64005e78)
    at /data/codebase/stonedb57/stonedb/storage/tianmu/core/engine_execute.cpp:436
#3  0x000055bc6652513c in Tianmu::core::Engine::HandleSelect (this=0x55bc68786610, thd=0x7f5f64000e10, lex=0x7f5f64003138,
    result=@0x7f64883f6488: 0x7f5f64007988, setup_tables_done_option=0, res=@0x7f64883f647c: 0, optimize_after_tianmu=@0x7f64883f6484: 1,
    tianmu_free_join=@0x7f64883f6480: 0, with_insert=0) at /data/codebase/stonedb57/stonedb/storage/tianmu/core/engine_execute.cpp:191
#4  0x000055bc6663e528 in Tianmu::handler::ha_my_tianmu_query (thd=0x7f5f64000e10, lex=0x7f5f64003138,
    result_output=@0x7f64883f6488: 0x7f5f64007988, setup_tables_done_option=0, res=@0x7f64883f647c: 0,
    optimize_after_tianmu=@0x7f64883f6484: 1, tianmu_free_join=@0x7f64883f6480: 0, with_insert=0)
    at /data/codebase/stonedb57/stonedb/storage/tianmu/handler/ha_my_tianmu.cpp:88
#5  0x000055bc65bbc860 in execute_sqlcom_select (thd=0x7f5f64000e10, all_tables=0x7f5f64006d50)
    at /data/codebase/stonedb57/stonedb/sql/sql_parse.cc:5208
#6  0x000055bc65bb58cf in mysql_execute_command (thd=0x7f5f64000e10, first_level=true)
    at /data/codebase/stonedb57/stonedb/sql/sql_parse.cc:2851
#7  0x000055bc65bbd8ee in mysql_parse (thd=0x7f5f64000e10, parser_state=0x7f64883f7630)
    at /data/codebase/stonedb57/stonedb/sql/sql_parse.cc:5646
#8  0x000055bc65bb247f in dispatch_command (thd=0x7f5f64000e10, com_data=0x7f64883f7ee0, command=COM_QUERY)
    at /data/codebase/stonedb57/stonedb/sql/sql_parse.cc:1495
#9  0x000055bc65bb12f6 in do_command (thd=0x7f5f64000e10) at /data/codebase/stonedb57/stonedb/sql/sql_parse.cc:1034
#10 0x000055bc65cf9bd6 in handle_connection (arg=0x55bc6d3e3aa0)
    at /data/codebase/stonedb57/stonedb/sql/conn_handler/connection_handler_per_thread.cc:313
#11 0x000055bc66446135 in pfs_spawn_thread (arg=0x55bc6d1636b0) at /data/codebase/stonedb57/stonedb/storage/perfschema/pfs.cc:2197
#12 0x00007f648efcb609 in start_thread (arg=<optimized out>) at pthread_create.c:477
#13 0x00007f648ed9f133 in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95
(gdb) f 1
#1  0x000055bc6657f970 in Tianmu::core::Query::Compile (this=0x7f64883f5ff0, compiled_query=0x7f64883f5f20, selects_list=0x7f5f64005b90,
    last_distinct=0x7f5f640072f0, res_tab=0x0, ignore_limit=false, left_expr_for_subselect=0x0, oper_for_subselect=0x0,
    ignore_minmax=false, for_subq_in_where=false) at /data/codebase/stonedb57/stonedb/storage/tianmu/core/query_compile.cpp:1103
1103            throw CompilationError();
(gdb) list
1098          std::vector<TabID> left_tables, right_tables;
1099          bool first_table = true;
1100          if (QueryRouteTo::kToMySQL == AddJoins(*join_list, tmp_table, left_tables, right_tables,
1101                                                 (res_tab != nullptr && res_tab->n != 0), first_table, for_subq_in_where,
1102                                                 use_tmp_when_no_join))
1103            throw CompilationError();
1104
1105          List<Item> field_list_for_subselect;
1106          if (left_expr_for_subselect && field_for_subselect) {
1107            field_list_for_subselect.push_back(field_for_subselect);
(gdb)
lujiashun commented 1 year ago

(gdb) c
Continuing.

Thread 152 "mysqld" hit Breakpoint 3, Tianmu::core::Query::AddJoins (this=0x7f64883f5f20, join=..., tmp_table=...,
    left_tables=std::vector of length 23566914813490, capacity 35011919420930 = {...},
    right_tables=std::vector of length -2305843006529339395, capacity -2305843006529339395 = {...}, in_subquery=100,
    first_table=@0x55bc6652993b: 144, for_subq_in_where=48, use_tmp_when_no_join=100)
    at /data/codebase/stonedb57/stonedb/storage/tianmu/core/query_compile.cpp:303
303                                  bool for_subq_in_where /*false*/, bool use_tmp_when_no_join /*false*/) {
(gdb) n
304       if (!join.elements) {
(gdb)
307         if (use_tmp_when_no_join) {
(gdb)
319         return QueryRouteTo::kToMySQL;
(gdb)
lujiashun commented 1 year ago

In this scenario, in GDB mode, if set use_tmp_when_no_join true(query_compile.cpp:307), the result will be as below. What should be cared about is to how to enlarge the judgement condition suitablely.

mysql> select a+0 from t1 union select NULL;
+------+
| a+0  |
+------+
|  120 |
|    0 |
|  111 |
| NULL |
+------+