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

Equivalent query on InnoDB temp table not recognized as empty, DISTINCT+filesort triggers ERROR 1038 Out of sort memory on zero rows, while original MyISAM query returns empty set.

    XMLWordPrintable

Details

    • Bug
    • Status: Closed (View Workflow)
    • Minor
    • Resolution: Not a Bug
    • 10.11, 11.4, 11.8, 12.3, 13.1, 12.3.2
    • N/A
    • Optimizer
    • Operating system:
      - Ubuntu 20.04.6 LTS (Focal Fossa)
      - Linux kernel: 5.4.0-204-generic
      - Architecture: x86_64

      Hardware:
      - CPU: 2 × Intel Xeon Gold 5218R @ 2.10 GHz
      - 40 physical cores / 80 logical CPUs
    • Not for Release Notes

    Description

          1. Problem Description
            Under the same data and session parameters, the original single-table query (Q1) returns an empty set directly because the optimizer recognizes the empty table; the query rewritten to use an InnoDB temporary table (Q2) does not recognize the temporary table as empty, chooses a DISTINCT + filesort plan, and triggers `ERROR 1038 (HY001): Out of sort memory` on zero-row input. After adjusting `max_sort_length`, Q2 can return an empty set, indicating that the logical result is consistent, but the query fails under the original parameters. This is not a "wrong result set" issue, but a "legal query fails under legal parameters and behaves inconsistently with an equivalent query" issue.
          1. How to Reproduce
            Execute the following SQL:

      ```sql
      DROP DATABASE IF EXISTS repro_mariadb813_db0_vp599_min;
      CREATE DATABASE repro_mariadb813_db0_vp599_min;
      USE repro_mariadb813_db0_vp599_min;

      SET SESSION optimizer_switch='derived_merge=on';
      SET SESSION sort_buffer_size=2097152;
      SET SESSION max_sort_length=6259757;

      CREATE TABLE s(id INT, c1 BOOLEAN) ENGINE=MyISAM;
      CREATE TABLE l(id INT) ENGINE=MyISAM;
      CREATE TABLE r(id INT, c1 BOOLEAN) ENGINE=MyISAM;

      DROP TEMPORARY TABLE IF EXISTS tmp;
      CREATE TEMPORARY TABLE tmp ENGINE=InnoDB AS
      SELECT l.id, r.c1
      FROM l JOIN r ON l.id=r.id;

      – Q1: original single-table query
      SELECT *
      FROM (
      SELECT DISTINCT REGEXP_REPLACE('1', c1, 'a') AS v
      FROM s
      WHERE c1
      ) q
      ORDER BY 1;

      – Q2: query after table reconstruction
      SELECT *
      FROM (
      SELECT DISTINCT REGEXP_REPLACE('1', c1, 'a') AS v
      FROM tmp
      WHERE c1
      ) q
      ORDER BY 1;
      ```

          1. Expected Behavior
            Q1 and Q2 should return the same result, i.e., both should return `Empty set`.
          1. Actual Behavior
      • Q1: returns `Empty set`.
      • Q2: fails with
        ```
        ERROR 1038 (HY001):
        Out of sort memory, consider increasing server sort buffer size
        ```
          1. Error Information and/or Stack Trace
            ```
            ERROR 1038 (HY001):
            Out of sort memory, consider increasing server sort buffer size
            ```

      Attachments

        Activity

          People

            Unassigned Unassigned
            Annie Annie liu
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Git Integration

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