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

FIELD(), INTERVAL(), IS_IPV4()/IS_IPV6()/IS_IPV4_COMPAT()/IS_IPV4_MAPPED(), JSON_ARRAY(), JSON_OBJECT(), JSON_MERGE_PATCH(), NVL2(), COLUMN_CREATE() on inner table of LEFT JOIN wrongly convert outer join to inner join

    XMLWordPrintable

Details

    • Bug
    • Status: Open (View Workflow)
    • Major
    • Resolution: Unresolved
    • 13.1.2, 13.0.2
    • None
    • Optimizer
    • Linux x86_64, official linux-systemd-x86_64 binary tarballs, default optimizer_switch

    Description

      These functions return a non-NULL value when their argument is NULL. For example, FIELD(NULL,1)=0, INTERVAL(NULL,1)=-1, IS_IPV4(<NULL int></null>)=0, JSON_ARRAY(NULL)='[null]' and NVL2(NULL,0,1)=1. Even so, a WHERE predicate on such a function over a column of the inner table of a LEFT JOIN is treated as null-rejecting. The LEFT JOIN is then converted to an inner join, and the NULL-complemented rows are lost.

      This is the same class of problem as MDEV-40863 (CHAR), MDEV-40864 (MAKE_SET), MDEV-40875 (SFORMAT) and MDEV-39235 (QUOTE, fixed). The functions listed here are not covered by those reports.

      How to repeat:

      CREATE TABLE t1 (a INT);
      CREATE TABLE t2 (b INT);
      INSERT INTO t1 VALUES (1), (2);
      INSERT INTO t2 VALUES (1);
       
      -- the NULL-complemented row (2, NULL) has FIELD(t2.b,1) = 0:
      SELECT t1.a, t2.b, FIELD(t2.b, 1) FROM t1 LEFT JOIN t2 ON t1.a = t2.b;
      -- returns (1,1,1), (2,NULL,0)
       
      SELECT t1.a, t2.b FROM t1 LEFT JOIN t2 ON t1.a = t2.b WHERE FIELD(t2.b, 1) = 0;
      -- expected: (2, NULL)        actual: empty set
       
      SELECT t1.a, t2.b FROM t1 LEFT JOIN t2 ON t1.a = t2.b WHERE INTERVAL(t2.b, 1) = -1;
      -- expected: (2, NULL)        actual: empty set
      SELECT t1.a, t2.b FROM t1 LEFT JOIN t2 ON t1.a = t2.b WHERE NVL2(t2.b, 0, 1) = 1;
      -- expected: (2, NULL)        actual: empty set
      SELECT t1.a, t2.b FROM t1 LEFT JOIN t2 ON t1.a = t2.b WHERE IS_IPV4(t2.b) = 0;
      -- expected: (1,1), (2,NULL)  actual: (1,1)     (same for IS_IPV6, IS_IPV4_COMPAT, IS_IPV4_MAPPED)
      SELECT t1.a, t2.b FROM t1 LEFT JOIN t2 ON t1.a = t2.b WHERE JSON_ARRAY(t2.b) IS NOT NULL;
      -- expected: (1,1), (2,NULL)  actual: (1,1)     (same for JSON_OBJECT(t2.b,1), JSON_MERGE_PATCH(t2.b,1), COLUMN_CREATE(1,t2.b))
       
      EXPLAIN EXTENDED SELECT t1.a, t2.b FROM t1 LEFT JOIN t2 ON t1.a = t2.b WHERE FIELD(t2.b, 1) = 0;
      SHOW WARNINGS;
      -- Note 1003: select ... from t1 join t2 where t1.a = t2.b and field(t2.b,1) = 0     <- LEFT JOIN became JOIN
      

      Wrapping the function so that it is no longer seen as null-rejecting gives the correct row. For example, WHERE (CASE WHEN 1 THEN FIELD(t2.b,1) END) = 0 returns (2, NULL).

      Analysis:
      Item_func_field, Item_func_interval, Item_func_nvl2, Item_func_json_array (and Item_func_json_object), Item_func_json_merge_patch, Item_func_dyncol_create and the INET6 IS_IPV4/IS_IPV6/IS_IPV4_COMPAT/IS_IPV4_MAPPED items do not override not_null_tables() / eval_not_null_tables(). So Item_func's default, the union of the arguments' not_null_tables(), is used, which assumes the function returns NULL whenever an argument is NULL. Item_func_quote and Item_func_concat_ws already override it with `table_map not_null_tables() const override

      { return 0; }

      `; the same override is needed in the classes above.

      Version: 13.0.2-MariaDB and 13.1.2-MariaDB (official linux-systemd-x86_64 binary tarballs), default optimizer_switch. Linux x86_64.

      Attachments

        Activity

          People

            Unassigned Unassigned
            dingjiacheng Jiacheng Ding
            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.