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

Fractional JSON numbers evaluate as false in IF and WHERE

    XMLWordPrintable

Details

    Description

      MariaDB 12.3.3 evaluates nonzero fractional JSON numbers as false when they are used directly as boolean expressions. The same values are nonzero numerically, and ordinary fractional numeric literals evaluate as true.

      This occurs when a fractional JSON number is evaluated directly as a boolean expression, including in IF or WHERE.

      Steps to reproduce

      1. Run the following statements in one MariaDB session. They create no persistent state.

      SELECT 'fraction' AS case_name,
       IF(JSON_EXTRACT('0.5','$'),1,0) AS actual,
       IF(0.5,1,0) AS expected,
       JSON_EXTRACT('0.5','$') <> 0 AS numeric_nonzero;
      SELECT 'negative_fraction' AS case_name,
       IF(JSON_EXTRACT('-0.5','$'),1,0) AS actual,
       IF(-0.5,1,0) AS expected;
      SELECT 'zero' AS case_name,
       IF(JSON_EXTRACT('0','$'),1,0) AS actual,
       0 AS expected;
      SELECT 'json_true' AS case_name,
       IF(JSON_EXTRACT('true','$'),1,0) AS actual,
       1 AS expected;
      SELECT 'json_false' AS case_name,
       IF(JSON_EXTRACT('false','$'),1,0) AS actual,
       0 AS expected;
      SELECT 'predicate' AS case_name,
       COUNT(*) AS actual,
       1 AS expected
      FROM (SELECT 1 AS n) AS row_source
      WHERE JSON_EXTRACT('0.5','$');

      Actual result

      case_name          actual  expected  numeric_nonzero
      fraction           0       1         1
       
      case_name          actual  expected
      negative_fraction  0       1
      zero               0       0
      json_true          1       1
      json_false         0       0
      predicate          0       1

      Both 0.5 and -0.5 extracted from JSON evaluate as false. The WHERE predicate also discards its row. The numeric comparison reports that the extracted 0.5 is nonzero, while the zero and JSON-boolean controls return their expected values.

      Expected result

      A nonzero numeric scalar should evaluate as true regardless of whether it originated as a JSON number. The positive and negative fraction queries should therefore return actual = 1, and the WHERE query should count one row.

      The source-level hypothesis from the reproducer is that JSON boolean evaluation uses an integer conversion that loses the fractional part before testing the value.

      Attachments

        Issue Links

          Activity

            People

              bar Alexander Barkov
              Milo liu
              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.