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
861 stars 139 forks source link

bug: mysqld crash, when COUNT (DISTINCT col) #1478

Open davidshiz opened 1 year ago

davidshiz commented 1 year ago

Have you read the Contributing Guidelines on issues?

Please confirm if bug report does NOT exists already ?

Describe the problem

only debug version crash

Thread 152 "mysqld" received signal SIGABRT, Aborted.
[Switching to Thread 0x7f633d69f700 (LWP 80418)]
__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  0x00007f6343f47859 in __GI_abort () at abort.c:79
#2  0x00007f6343f47729 in __assert_fail_base (fmt=0x7f63440dd588 "%s%s%s:%u: %s%sAssertion `%s' failed.\n%n", assertion=0x5568a27e9a31 "distinct_size > 0",
    file=0x5568a27e9960 "/stonedb/storage/tianmu/core/group_table.cpp", line=383, function=<optimized out>) at assert.c:92
#3  0x00007f6343f58fd6 in __GI___assert_fail (assertion=0x5568a27e9a31 "distinct_size > 0", file=0x5568a27e9960 "/stonedb/storage/tianmu/core/group_table.cpp",
    line=383, function=0x5568a27e99f8 "void Tianmu::core::GroupTable::Initialize(int64_t, bool)") at assert.c:101
#4  0x00005568a1f8efcd in Tianmu::core::GroupTable::Initialize (this=0x7f633d69aaf8, max_no_groups=1, parallel_allowed=true)
    at /stonedb/storage/tianmu/core/group_table.cpp:383
#5  0x00005568a1f9c4b5 in Tianmu::core::GroupByWrapper::Initialize (this=0x7f633d69aa30, upper_approx_of_groups=1, parallel_allowed=true)
    at /stonedb/storage/tianmu/core/groupby_wrapper.cpp:259
#6  0x00005568a1f24acf in Tianmu::core::AggregationAlgorithm::Aggregate (this=0x7f633d69ad90, just_distinct=false, limit=@0x7f633d69ad68: -1,
    offset=@0x7f633d69ad60: 0, sender=0x7f528c02da90) at /stonedb/storage/tianmu/core/aggregation_algorithm.cpp:143
#7  0x00005568a1c3e37c in Tianmu::core::TempTable::Materialize (this=0x7f528c02a980, in_subq=false, sender=0x7f528c02da90, lazy=false)
    at /stonedb/storage/tianmu/core/temp_table.cpp:2084
#8  0x00005568a1bcfe11 in Tianmu::core::Engine::Execute (this=0x5568a5e57640, thd=0x7f528c000e10, lex=0x7f528c003138, result_output=0x7f528c007af0,
    unit_for_union=0x0) at /stonedb/storage/tianmu/core/engine_execute.cpp:489
#9  0x00005568a1bce9f1 in Tianmu::core::Engine::HandleSelect (this=0x5568a5e57640, thd=0x7f528c000e10, lex=0x7f528c003138, result=@0x7f633d69b498: 0x7f528c007af0,
    setup_tables_done_option=0, res=@0x7f633d69b48c: 32594, is_optimize_after_tianmu=@0x7f633d69b494: 1, tianmu_free_join=@0x7f633d69b490: 1, with_insert=0)
    at /stonedb/storage/tianmu/core/engine_execute.cpp:238
#10 0x00005568a1cf9178 in Tianmu::DBHandler::ha_my_tianmu_query (thd=0x7f528c000e10, lex=0x7f528c003138, result_output=@0x7f633d69b498: 0x7f528c007af0,
    setup_tables_done_option=0, res=@0x7f633d69b48c: 32594, is_optimize_after_tianmu=@0x7f633d69b494: 1, tianmu_free_join=@0x7f633d69b490: 1, with_insert=0)
    at /stonedb/storage/tianmu/handler/ha_my_tianmu.cpp:89
#11 0x00005568a12679b2 in execute_sqlcom_select (thd=0x7f528c000e10, all_tables=0x7f528c007548) at /stonedb/sql/sql_parse.cc:5204
#12 0x00005568a1260a21 in mysql_execute_command (thd=0x7f528c000e10, first_level=true) at /stonedb/sql/sql_parse.cc:2847
#13 0x00005568a1268a40 in mysql_parse (thd=0x7f528c000e10, parser_state=0x7f633d69c630) at /stonedb/sql/sql_parse.cc:5642
#14 0x00005568a125d5e1 in dispatch_command (thd=0x7f528c000e10, com_data=0x7f633d69cee0, command=COM_QUERY) at /stonedb/sql/sql_parse.cc:1495
#15 0x00005568a125c458 in do_command (thd=0x7f528c000e10) at /stonedb/sql/sql_parse.cc:1034
#16 0x00005568a13a5b1e in handle_connection (arg=0x5568ae70d850) at /stonedb/sql/conn_handler/connection_handler_per_thread.cc:313
#17 0x00005568a1af19cf in pfs_spawn_thread (arg=0x5568ae5f0c70) at /stonedb/storage/perfschema/pfs.cc:2197
#18 0x00007f6344270609 in start_thread (arg=<optimized out>) at pthread_create.c:477
#19 0x00007f6344044133 in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95

Expected behavior

No response

How To Reproduce

CREATE TABLE cs1 (d1 DECIMAL(17), d2 DECIMAL(17,10), d3 DECIMAL(17,16), d4 DECIMAL(17),d5 DECIMAL(17),i1 INT) ;
INSERT INTO cs1 VALUES
    (1234,5678,-0.123456789012345,2,1,1),     
    (1234,-5678,0.12345678901234,2,1,1),
    (1234567890123,123456.90123,0.12345678901234,2,1,1),
    (NULL,NULL,NULL,2,1,1),
    (0,0,0,2,1,1),
    (1234,5678,0.123456789,3,2,2),
    (-12345678923,-5678,0.1234567890121,3,2,2),
    (56789012,123856.90,0.12345678909,3,2,2),
    (NULL,NULL,NULL,3,2,2),
    (0,0,0,3,2,2),
    (1234,5678,-0.123456789012345,1111111111,1,1),
    (1234,5678,-0.123456789012345,1111111111,1,1),
    (1234,5678,-0.123456789012345,1111111112,1,1); 
SELECT "count(distinct 17)_test1", count(distinct d1),count(distinct d2),count(distinct d3) FROM cs1;

Environment

root@ub01:~# /stonedb57/install/bin/mysqld --version
/stonedb57/install/bin/mysqld  Ver 5.7.36-StoneDB-v1.0.3.05db04de4 for Linux on x86_64 (build-)
build information as follow:
        Repository address: https://github.com/stoneatom/stonedb.git:stonedb-5.7-dev
        Branch name: stonedb-5.7-dev
        Last commit ID: 05db04de4
        Last commit time: Date:   Wed Mar 22 21:09:55 2023 +0800
        Build time: Date: Mon Mar 27 14:06:28 CST 2023

Are you interested in submitting a PR to solve the problem?

RingsC commented 1 year ago

assertion=0x5568a27e9a31 "distinct_size > 0" failed in group_table.cpp

  // pre-allocation of distinct memory
  int distinct_width = 0;
  int64_t distinct_size = 0;
  for (int i = no_grouping_attr; i < no_attr; i++) {
    if (distinct[i]) {
      desc = aggregated_desc[i - no_grouping_attr];
      distinct_width = vc[i]->MaxStringSize() + 2;
      distinct_size += desc.max_no_values * distinct_width;
    }
  }
  distinct_size = int64_t(ceil(distinct_size * 1.3));

  // initialize distinct tables
  for (int i = no_grouping_attr; i < no_attr; i++)
    if (distinct[i]) {
      desc = aggregated_desc[i - no_grouping_attr];
      DEBUG_ASSERT(distinct_size > 0);
      gdistinct[i] = std::make_shared<GroupDistinctTable>(p_power);
      gdistinct[i]->InitializeVC(vm_tab->RowNumberScope(), vc[i], desc.max_no_values,
                                 distinct_size / no_columns_with_distinct,
                                 (operation[i] != GT_Aggregation::GT_COUNT_NOT_NULL));  // otherwise must be decodable
      gdistinct[i]->CopyFromValueMatchingTable(vm_tab.get());
      distinct_size -= gdistinct[i]->BytesTaken();
      no_columns_with_distinct--;
    }