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

`BIT` grouping column is returned as integer after a mergeable CTE or derived table

    XMLWordPrintable

Details

    • Bug
    • Status: Closed (View Workflow)
    • Major
    • Resolution: Duplicate
    • 12.3.2
    • N/A
    • Optimizer
    • None
    • Not for Release Notes

    Description

      MariaDB changes the result type of a `BIT` column when the column passes through a
      mergeable CTE or derived table and is then used as an outer `GROUP BY` key.

      The direct query returns the grouping column with the MariaDB protocol type `BIT`
      and binary values such as `b'\xf5'`. The equivalent CTE or derived-table query
      returns the same column with protocol type `LONG` and integer values such as
      `245`.

      The numeric bit patterns are unchanged, but the SQL result metadata and the
      client-visible values are different:

       
      ```text
      Direct query:        b'\x09', b'\xf0', b'\xf5'  (BIT)
      CTE/derived query:   9,       240,      245      (LONG)
      ```
      
      

      This case does not use `BIT_XOR` or another bit aggregate. It affects an ordinary
      `BIT(8)` column selected as a grouping key, so it is distinct from the
      `BIT_XOR` materialization type-change issue.

      1. How to repeat

      Create a self-contained test table:

       
      ```sql
      DROP DATABASE IF EXISTS bit_group_type_test;
      CREATE DATABASE bit_group_type_test;
      USE bit_group_type_test;
       
      CREATE TABLE t (
          id INT PRIMARY KEY,
          b BIT(8),
          payload VARBINARY(8)
      );
       
      INSERT INTO t VALUES
          (1, b'11110101', X'F5'),
          (2, b'00001001', X'09'),
          (3, b'11110000', X'F0');
      ```
      
      

      Run the direct query:

       
      ```sql
      SELECT b, MAX(payload) AS m
      FROM t
      GROUP BY b;
      ```
      
      

      The result column `b` is exposed as `BIT` and a binary-capable client receives:

       
      ```text
      b'\x09'
      b'\xf0'
      b'\xf5'
      ```
      
      

      Now put the source columns in a mergeable CTE:

       
      ```sql
      WITH x AS (
          SELECT b, payload
          FROM t
      )
      SELECT b, MAX(payload) AS m
      FROM x
      GROUP BY b;
      ```
      
      

      The result column `b` is exposed as `LONG` and the same client receives:

       
      ```text
      9
      240
      245
      ```
      
      

      The equivalent derived-table form has the same problem:

       
      ```sql
      SELECT b, MAX(payload) AS m
      FROM (
          SELECT b, payload
          FROM t
      ) AS x
      GROUP BY b;
      ```
      
      

      1. Expected result

      All three equivalent queries should expose the grouping column as `BIT(8)` and
      return the same binary representation. Introducing a relational CTE or derived
      table should not change the selected column's result type.

      1. Actual result

      The mergeable CTE and derived-table forms expose the grouping column as `LONG`.
      Applications therefore receive Python integers instead of byte strings, despite
      the source column remaining `BIT(8)`.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              chen7897 cl hl
              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.