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

Incorrect Operator Precedence of BETWEEN and Comparators in MariaDB 11.6.2

    XMLWordPrintable

Details

    Description

      Hi,

      According to the official MariaDB documentation, comparison operators have higher precedence than the BETWEEN operator. Here's the relevant part of the documentation:
      https://mariadb.com/kb/en/operator-precedence/

      (highest)
      ...
      = (comparison), <=>, >=, >, <=, <, <>, !=, IS, LIKE, REGEXP, IN, MEMBER OF
      BETWEEN, CASE, WHEN, THEN, ELSE
      ...
      (lowest)
      

      However, we've observed that MariaDB evaluates the BETWEEN operator first, which seems to be in contrast to what's stated in the documentation.

      Minimized Query to Reproduce:

      SELECT 5 BETWEEN 0 AND 10 = 1;
      

      Expected Result:

      +--------------------------+
      | 5 BETWEEN 0 AND (10 = 1) |
      +--------------------------+
      |                        0 |
      +--------------------------+
      

      This is the expected result when the comparator = is evaluated first, as indicated in the documentation. (Note the parentheses around 10 = 1, which are required for correct behavior.)

      Actual result (MariaDB, version 11.6.2):

      SELECT 5 BETWEEN 0 AND 10 = 1;
      +------------------------+
      | 5 BETWEEN 0 AND 10 = 1 |
      +------------------------+
      |                      1 |
      +------------------------+
       
      SELECT (5 BETWEEN 0 AND 10) = 1;
      +--------------------------+
      | (5 BETWEEN 0 AND 10) = 1 |
      +--------------------------+
      |                        1 |
      +--------------------------+
      

      In MariaDB 11.6.2, the actual result is the same as when BETWEEN is explicitly forced to be evaluated first using parentheses.

      We believe this behavior is a bug because it differs from the precedence described in the official documentation.

      Developers often rely on the official documentation, assuming it's accurate, only to find that it doesn't reflect the actual behavior in this case.
      The fact that each DBMS defines the precedence of BETWEEN and comparison operators autonomously makes this situation even more problematic.

      We suggest that MariaDB be updated to evaluate comparison operators first, as specified in the documentation, or that the documentation be updated to align with the current behavior.

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

      Best regards,

      Attachments

        Activity

          People

            serg Sergei Golubchik
            QueryHouse QueryHouse
            Votes:
            0 Vote for this issue
            Watchers:
            3 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.