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

COLUMN_EXISTS() with a NULL column name is accepted by WHERE / IF / CASE although its value is NULL

    XMLWordPrintable

Details

    Description

      Problem

      COLUMN_EXISTS(blob, NULL) evaluates to NULL when selected, but a WHERE condition, IF() or CASE WHEN treats it as TRUE. A row for which the condition is NULL is therefore returned both by WHERE cond and by WHERE cond IS NULL, which breaks SQL three-valued logic.

      How to repeat

      No table is needed:

      SELECT COLUMN_EXISTS(NULL, NULL) AS v, COLUMN_EXISTS(NULL, NULL) IS NULL AS v_is_null;
      SELECT 'row' WHERE COLUMN_EXISTS(NULL, NULL);
      SELECT 'row' WHERE NOT COLUMN_EXISTS(NULL, NULL);
      SELECT 'row' WHERE COLUMN_EXISTS(NULL, NULL) IS NULL;
      SELECT IF(COLUMN_EXISTS(NULL, NULL), 'T', 'F');
      SELECT COLUMN_EXISTS(COLUMN_CREATE('a',1), NULL) IS NULL AS null_valued,
             (SELECT COUNT(*) FROM (SELECT 1 AS x) d WHERE COLUMN_EXISTS(COLUMN_CREATE('a',1), NULL)) AS where_rows;

      Actual result

      statement actual expected
      projection NULL, IS NULL is 1 same
      WHERE COLUMN_EXISTS(NULL,NULL) returns the row no row
      WHERE NOT COLUMN_EXISTS(NULL,NULL) no row no row
      WHERE COLUMN_EXISTS(NULL,NULL) IS NULL returns the row returns the row
      IF(COLUMN_EXISTS(NULL,NULL),'T','F') T F
      NULL name with a real dynamic column blob null-valued 1, WHERE rows 1 WHERE rows 0

      Only a NULL column name (second argument) triggers it: COLUMN_EXISTS(NULL, 'a') is filtered correctly.

      Analysis

      Item_func_dyncol_exists::val_bool() in sql/item_cmpfunc.cc sets null_value and returns 1 when the name argument is NULL:

      String *nm= args[1]->val_str(&nmbuf);
      if (!nm || args[1]->null_value)
      {
        null_value= 1;
        return 1;
      }

      Condition evaluation (WHERE, IF, CASE) uses the returned value of val_bool() without looking at null_value, so the payload 1 is read as TRUE, while the select-list path checks null_value and shows NULL. Suggested fix: return 0 here. I did not build or test a patch.

      The code is unchanged on current main (checked against 52391ebe4feb56c8ccbbf33a29ea3acbb7b7cea7). Found by a sweep that calls every server function with NULL arguments and compares expr IS NULL with WHERE expr.

      Attachments

        Activity

          People

            sanja Oleksandr Byelkin
            hacker_zheng Wang Zheng
            Khaled Amr Khaled Amr
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:

              Time Tracking

                Estimated:
                Original Estimate - 0d
                0d
                Remaining:
                Remaining Estimate - 1h
                1h
                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.