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

TIME(expr) in a generated column or CHECK constraint loses fractional seconds (stored as cast(expr as time)), a valid row is rejected

    XMLWordPrintable

Details

    • Bug
    • Status: Open (View Workflow)
    • Major
    • Resolution: Unresolved
    • 13.2
    • None
    • Virtual Columns
    • None

    Description

      Problem

      TIME(expr) without an explicit precision keeps the fractional seconds of its argument in a normal query and in a view, but when it is used in a generated column or a CHECK constraint the fraction is lost, because the stored definition is cast(expr as time), i.e. precision 0.

      How to repeat

      SELECT TIME(CAST('2020-01-01 12:34:56.789012' AS DATETIME(6))) = '12:34:56.789012' AS plain;
       
      CREATE TABLE tq (dt DATETIME(6), t TIME(6) AS (TIME(dt)) VIRTUAL, t2 VARCHAR(30) AS (TIME(dt)) VIRTUAL);
      INSERT INTO tq (dt) VALUES ('2020-01-01 12:34:56.789012');
      SELECT TIME(dt) AS direct, t, t2 FROM tq;
      SELECT COUNT(*) FROM tq WHERE t = TIME(dt);
      SHOW CREATE TABLE tq;
       
      CREATE TABLE tq2 (dt DATETIME(6), CHECK (TIME(dt) = '12:34:56.789012'));
      SHOW CREATE TABLE tq2;
      INSERT INTO tq2 VALUES ('2020-01-01 12:34:56.789012');
       
      CREATE TABLE tq3 (dt DATETIME(6), CHECK (CAST(dt AS TIME(6)) = '12:34:56.789012'));
      INSERT INTO tq3 VALUES ('2020-01-01 12:34:56.789012');

      Actual result

      • the plain comparison is 1
      • direct = 12:34:56.789012, but t = 12:34:56.000000 and t2 = 12:34:56; COUNT ... WHERE t = TIME(dt) is 0 (expected 1)
      • SHOW CREATE TABLE tq: GENERATED ALWAYS AS (cast(`dt` as time))
      • SHOW CREATE TABLE tq2: CHECK (cast(`dt` as time) = '12:34:56.789012'), and the INSERT fails with ERROR 4025: CONSTRAINT `CONSTRAINT_1` failed although the row satisfies the constraint as written
      • with the explicit CAST(dt AS TIME(6)) (table tq3) the row is accepted

      The same happens with a string argument: a generated column AS (TIME('12:34:56.789')) returns 12:34:56, while AS (CAST('12:34:56.789' AS TIME(3))) returns 12:34:56.789. In a VIEW the same expression is printed with its precision (cast('12:34:56.789' as time(3))) and works.

      Side observation: a STORED generated column AS (TIME(dt)) is rejected with ERROR 1901: Function or expression 'cast(`dt` as time)' cannot be used in the GENERATED ALWAYS AS clause, naming an expression the user did not write. I did not check whether the rejection itself is intended.

      Analysis

      Item_time_typecast (sql/item_timefunc.h) stores the precision in decimals. The parser creates TIME(expr) as Item_time_typecast(thd, $3, AUTO_SEC_PART_DIGITS) (sql/sql_yacc.yy; AUTO_SEC_PART_DIGITS is DECIMAL_NOT_SPECIFIED), and Type_handler::Item_time_typecast_fix_length_and_dec() (sql/sql_type.cc) replaces it by the argument's time precision only in fix_length_and_dec(), i.e. after fix_fields(). Item_func::print_cast_temporal() (sql/item_timefunc.cc) prints the precision only when decimals is neither 0 nor NOT_FIXED_DEC. The text of a generated column or a CHECK constraint is printed before fix_fields() (precision still not fixed), so it contains no precision, and parsing CAST(x AS TIME) again gives precision 0. A view is printed after fix_fields(), so the precision is there.

      Possible fix: print an unfixed Item_time_typecast as time(<arg>) (the form the user wrote), or derive the precision before printing. 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 generated columns with the plain SELECT.

      Attachments

        Activity

          People

            Unassigned Unassigned
            hacker_zheng Wang Zheng
            Votes:
            0 Vote for this issue
            Watchers:
            1 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.