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

Window Function Returns NULL When ROWS PRECEDING Exceeds Signed BIGINT Maximum

    XMLWordPrintable

Details

    • Bug
    • Status: Closed (View Workflow)
    • Major
    • Resolution: Duplicate
    • 11.6.2, 12.1.2, 12.2.2
    • N/A
    • None
    • Docker image on Ubuntu 22.04 LTS
    • Not for Release Notes

    Description

      Hello,

      We have identified a logical issue related to window frame definitions in MariaDB.


      When a window function is used with a frame specification of the form:

      ROWS BETWEEN n PRECEDING AND CURRENT ROW
      

      and n is an unsigned integer value greater than or equal to 2^63, the query returns NULL for all rows. Instead of computing the expected aggregate, the window frame behaves as if it is empty.

      This issue affects all window functions documented under window frame specifications, with the exception of LAG and LEAD.

      Steps to Reproduce

      CREATE TABLE t0 (c0 BIGINT);
      INSERT INTO t0 (c0) VALUES (3), (2), (5), (15), (20);
      SELECT MAX(c0) OVER (ORDER BY c0 ROWS BETWEEN 9223372036854775808 PRECEDING AND CURRENT ROW) AS res FROM t0;
      

      Actual Result

      +------+
      | res  |
      +------+
      | NULL |
      | NULL |
      | NULL |
      | NULL |
      | NULL |
      +------+
      

      Expected Result

      The value 9223372036854775808 (2^63) greatly exceeds the number of rows in any practical dataset and should therefore behave equivalently to UNBOUNDED PRECEDING.

      The expected output is a cumulative maximum:

      +------+
      | res  |
      +------+
      |    2 |
      |    3 |
      |    5 |
      |   15 |
      |   20 |
      +------+
      

      This matches the behavior observed when using:

      ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
      

      Analysis

      The value 9223372036854775808 equals 2^63, which exceeds the maximum value of a signed 64-bit integer (2^63 - 1).

      The issue appears to stem from one of the following:

      1. Integer overflow when the frame boundary is interpreted as a signed 64-bit integer.
      2. Silent failure during frame boundary parsing, resulting in an invalid or negative frame size.

      In both cases, the query produces NULL results instead of either:

      • Raising a validation error, or
      • Correctly interpreting the frame as effectively unbounded.

      Suggested Fix

      • Validate frame boundary values and return an explicit error when they exceed supported numeric limits.
      • Ensure consistent and well-defined handling of large boundary values, regardless of whether they are interpreted as signed or unsigned integers.
      • Consider aligning behavior with UNBOUNDED PRECEDING when the numeric value exceeds practical row counts.

      Thank you for your time and attention to this matter.
      We look forward to your response.

      Best regards,

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              QueryHouse QueryHouse
              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.