Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-42525

Partitioning changes the query result for a Spider table: COUNT(*), SUM(), MIN() and MAX() miss rows for an OR condition

    XMLWordPrintable

Details

    Description

      Likely cause: MDEV-22372, commit 9ba56c072c016cf226a796ca64f227f253be70c9, which added the spider_strict_group_by parameter in CS 10.5.4. Since then, Spider asks the remote server for min() of each column that the query does not aggregate, instead of the column itself. CS 10.4.25 gives the right result, and on CS 13.1.2 the table option strict_group_by "0" gives the right result too.

      --source include/have_partition.inc
      --disable_query_log
      --disable_result_log
      --source ../../t/test_init.inc
      --enable_result_log
      --enable_query_log
      SET spider_same_server_link=1;
      SET GLOBAL spider_same_server_link=1;
      evalp CREATE SERVER srv FOREIGN DATA WRAPPER MYSQL OPTIONS (SOCKET "$MASTER_1_MYSOCK", DATABASE 'test', USER 'root');
      CREATE TABLE r0 (id INT, a INT, b INT);
      CREATE TABLE r1 (id INT, a INT, b INT);
      CREATE TABLE t1 (id INT, a INT, b INT) ENGINE=SPIDER COMMENT='wrapper "mysql", srv "srv"' PARTITION BY HASH (id) (PARTITION p0 COMMENT='table "r0"', PARTITION p1 COMMENT='table "r1"');
      INSERT INTO t1 VALUES (1, 1, 0), (2, 1, 0), (3, 0, 1), (4, 0, 1);
      --let $c= query_get_value(SELECT COUNT(*) AS c FROM t1 WHERE a = 1 OR b = 1, c, 1)
      if ($c != 4)
      {
        --die A partitioned Spider table gives COUNT(*) $c, not 4, for the rows that match an OR condition.
      }
      --let $s= query_get_value(SELECT SUM(id) AS s FROM t1 WHERE a = 1 OR b = 1, s, 1)
      if ($s != 10)
      {
        --die A partitioned Spider table gives SUM(id) $s, not 10, for the rows that match an OR condition.
      }
      DROP TABLE t1, r0, r1;
      DROP SERVER srv;
      --disable_query_log
      --disable_result_log
      --source ../../t/test_deinit.inc
      --enable_result_log
      --enable_query_log

      Leads to:

      CS 13.1.2 c8e6ab229f030119c19f3208d40f415decb41dae (Debug, Clang 22.1.8-20260622) Build 19/11/2026

      mysqltest: At line 17: A partitioned Spider table gives COUNT(*) 0, not 4, for the rows that match an OR condition.

      Partitioning changes the query result. Without partitioning, the same query gives the right count. Here COUNT(*) gives 0 instead of 4, and SUM(id), MAX(id) and MIN(id) give NULL instead of 10, 4 and 1. SELECT id FROM t1 WHERE a = 1 OR b = 1 returns the 4 rows. For the COUNT(*), each remote table receives:

      CS 13.1.2 c8e6ab229f030119c19f3208d40f415decb41dae (Debug, Clang 22.1.8-20260622) Build 19/11/2026

      select count(0),min(`a`),min(`b`) from `test`.`r0` where ((`a` = 1) or (`b` = 1))

      WHERE a = 1 OR b = 0, which the minimum values meet, gives the right count. SET spider_direct_aggregate=0 also gives the right result. A Spider table without partitioning gives the same wrong result with spider_disable_group_by_handler=1. Optimized builds give the same result. The testcase runs in the spider/bugfix suite.

      From the code it seems that spider_mbase_handler::append_table_select() asks the remote server for min() of each column that is not in the GROUP BY, and that the server then checks the WHERE against that row of minimum values. When the row does not match, the server drops the aggregate values that the remote server returned.

      Attachments

        Issue Links

          Activity

            People

              ycp Yuchen Pei
              Roel Roel Van de Paar
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - Not Specified
                  Not Specified
                  Logged:
                  Time Spent - 20m
                  20m

                  Git Integration

                    Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.