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

UNION adds trailing zero bytes to ADDTIME() results under SET NAMES binary

    XMLWordPrintable

Details

    • Unexpected results

    Description

      Under SET NAMES binary, combining two identical ADDTIME() expressions with UNION changes the returned value by appending ten zero bytes.

      SELECT DISTINCT returns the expected 16-byte value. The equivalent UNION query returns a 26-byte value. The additional bytes are visible through HEX() and OCTET_LENGTH(), and cause comparison with the directly computed value to return false.

      Reproducer

      SET NAMES binary;
       
      SELECT
        HEX(col1) AS hex_value,
        OCTET_LENGTH(col1) AS byte_length,
        col1 = ADDTIME('-01:01:01.01', '-23:59:59.1') AS equals_direct
      FROM (
        SELECT DISTINCT
          ADDTIME('-01:01:01.01', '-23:59:59.1') AS col1
      ) AS s;
       
      SELECT
        HEX(col1) AS hex_value,
        OCTET_LENGTH(col1) AS byte_length,
        col1 = ADDTIME('-01:01:01.01', '-23:59:59.1') AS equals_direct
      FROM (
        SELECT ADDTIME('-01:01:01.01', '-23:59:59.1') AS col1
        UNION
        SELECT ADDTIME('-01:01:01.01', '-23:59:59.1') AS col1
      ) AS s;
      

      Actual result

      The first query returns:

      hex_value:     2D32353A30313A30302E313130303030
      byte_length:   16
      equals_direct: 1
      

      The second query returns:

      hex_value:     2D32353A30313A30302E31313030303000000000000000000000
      byte_length:   26
      equals_direct: 0
      

      The second result contains ten trailing 0x00 bytes after the expected string '-25:01:00.110000'.

      Expected result

      Both queries should return:

      hex_value:     2D32353A30313A30302E313130303030
      byte_length:   16
      equals_direct: 1
      

      Both UNION branches evaluate the same ADDTIME() expression. Combining their results should not append bytes or change the value.

      Additional observations

      • The difference is reproducible through server-side HEX(), OCTET_LENGTH(), and equality comparison.
      • The same query pair returns matching results with SET NAMES utf8mb4 or SET NAMES latin1.

      Related report

      MySQL Bug #40217: UNION ALL query results present faulty internal datetime

      MySQL Bug #40217 is closed. The developer comment states that it was fixed in MySQL 5.6.40 and later.

      Attachments

        Issue Links

          Activity

            People

              raghunandan.bhat Raghunandan Bhat
              jxgui jinxingui
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:

                Time Tracking

                  Estimated:
                  Original Estimate - 0d
                  0d
                  Remaining:
                  Remaining Estimate - 2d
                  2d
                  Logged:
                  Time Spent - Not Specified
                  Not Specified

                  Git Integration

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