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

CHR(NULL) returns an empty string instead of NULL in a view or a generated column, because CHR() is printed as CHAR(... USING ...)

    XMLWordPrintable

Details

    • Bug
    • Status: In Review (View Workflow)
    • Critical
    • Resolution: Unresolved
    • 10.11, 11.4, 13.2
    • 11.4
    • Server

    Description

      Problem

      CHR(NULL) is NULL, but the same expression stored in a view or in a generated column returns an empty string, because CHR is printed (and stored) as char(x using utf8mb4) and CHAR(NULL USING ...) returns an empty string.

      How to repeat

      SELECT CHR(NULL) AS v, CHR(NULL) IS NULL AS is_null;
      CREATE VIEW vchr AS SELECT CHR(NULL) AS c;
      SELECT c, c IS NULL AS is_null FROM vchr;
      SHOW CREATE VIEW vchr;
       
      CREATE TABLE tg (a INT DEFAULT 1, c VARCHAR(30) AS (CHR(NULL)) VIRTUAL);
      INSERT INTO tg (a) VALUES (1);
      SELECT c, c IS NULL AS is_null FROM tg;
      SHOW CREATE TABLE tg;

      Actual result

      • SELECT CHR(NULL): NULL, IS NULL = 1
      • the view: c is the empty string, c IS NULL = 0; SHOW CREATE VIEW shows select char(NULL using utf8mb4) AS `c`
      • the generated column: the empty string, IS NULL = 0; SHOW CREATE TABLE shows GENERATED ALWAYS AS (char(NULL using utf8mb4)) VIRTUAL. With an INT column the value is 0, with DATETIME it is 0000-00-00 00:00:00.

      Expected: NULL in all cases. Non-NULL arguments are not affected (CHR(65), CHR(256), CHR(300) return the same bytes through a view).

      Analysis

      Item_func_chr derives from Item_func_char and does not override print(). Item_func_char::print() (sql/item_strfunc.cc) writes Item_func_char::func_name_cstring(), a qualified call that does not dispatch virtually, so an Item_func_chr is printed as char(... using cs). The two functions differ for NULL: Item_func_chr::val_str() returns NULL when its argument is NULL, Item_func_char::val_str() skips NULL arguments and returns an empty string. A view, a generated column, a CHECK constraint or a DEFAULT is stored as its printed text and parsed again, so the stored expression is not equivalent.

      Possible fix: override print() in Item_func_chr so that it prints chr(<arg>). I did not build or test a patch.

      The code is unchanged on current main (checked against 52391ebe4feb56c8ccbbf33a29ea3acbb7b7cea7). Found by a fuzzer that compares values read back from views and generated columns with the plain SELECT.

      Attachments

        Activity

          People

            sanja Oleksandr Byelkin
            hacker_zheng Wang Zheng
            External_Contributor External_Contributor
            Oleksandr Byelkin Oleksandr Byelkin
            Votes:
            1 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.