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

`BIT_XOR` result changes from binary string to integer after materialization

    XMLWordPrintable

Details

    Description

      MariaDB returns a binary value for `BIT_XOR(t1.c1)` when the argument is a binary/bit column. When the same expression is evaluated through a materialized derived table (or a temporary table created with `CREATE TEMPORARY TABLE ... AS SELECT`), the result is returned as an integer instead.

      The numeric value is unchanged, but the SQL result type and client-visible representation are different. For example, the direct query returns `b'
      x8b'`, while the materialized query returns `139`.

      This violates the expectation that materializing an equivalent relational subquery preserves the expression's result type. The issue is therefore a MariaDB type-inference/materialization bug candidate, rather than a query syntax error or a value-computation error.

      Expected result (binary representation):

      ```text
      b'\\x8b'
      b'\\x81'
      b'\\x05'
      ```
      
      

      Actual result after materialization (integer representation):

       
      ```text
      139
      129
      5
      ```
      

      The values are numerically equivalent (`0x8b = 139`, `0x81 = 129`, `0x05 = 5`), but their SQL types are not equivalent.

      How to repeat

       
      ```sql
      DROP DATABASE IF EXISTS bit_xor_type_test;
      CREATE DATABASE bit_xor_type_test;
      USE bit_xor_type_test;
       
      CREATE TABLE t (
          id INT PRIMARY KEY,
          b BIT(8),
          v INT
      );
       
      INSERT INTO t VALUES
          (1, b'10001011', 7),
          (2, b'10000001', 3),
          (3, b'00000101', 12);
      ```
      
      

      Run the grouped aggregate directly:

       
      ```sql
      SELECT b, BIT_XOR(v) AS x
      FROM t
      GROUP BY b;
      ```
      

      A binary-capable client receives:

       
      ```text
      b'\x05'  12
      b'\x81'  3
      b'\x8b'  7
      ```
      
      

      The first result column has MariaDB protocol type `BIT`.

      Now move the source projection into a mergeable CTE:

       
      ```sql
      WITH cut AS (
          SELECT b, v
          FROM t
      )
      SELECT b, BIT_XOR(v) AS x
      FROM cut
      GROUP BY b;
      ```
      
      

      The equivalent query returns:

       
      ```text
      5    12
      129  3
      139  7
      ```
      
      

      The first result column now has protocol type `LONG`.

      The derived-table form produces the same changed representation:

      ```sql
      SELECT b, BIT_XOR(v) AS x
      FROM (
          SELECT b, v
          FROM t
      ) AS cut
      GROUP BY b;
      ```
      

      Attachments

        Issue Links

          Activity

            People

              psergei Sergei Petrunia
              chen7897 cl hl
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:

                Git Integration

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