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

Indexed virtual column with STR_TO_DATE() is reported corrupted when sql_mode differs from the one used at INSERT

    XMLWordPrintable

Details

    Description

      The value of an indexed virtual column is stored in the index when the row is written, but recomputed from the current sql_mode when the row is read. If STR_TO_DATE() (or any function whose result depends on NO_ZERO_DATE / NO_ZERO_IN_DATE) returns a zero date under one sql_mode and NULL under another, the index entry and the recomputed value differ. The index is then reported as corrupted.

      --source include/have_innodb.inc
       
      CREATE TABLE t1 (id INT PRIMARY KEY, a VARCHAR(30),
                       c DATE AS (STR_TO_DATE(a,'%Y-%m-%d')) VIRTUAL, KEY(c)) ENGINE=InnoDB;
      SET sql_mode='';
      INSERT INTO t1(id,a) VALUES (1,'2026-00-00');
      SET sql_mode='NO_ZERO_IN_DATE,NO_ZERO_DATE';
      --echo # BUG if the index has no entry for the row
      SELECT * FROM t1 FORCE INDEX(c) WHERE c IS NULL;
      CHECK TABLE t1 EXTENDED;
      DROP TABLE t1;

      Actual output:

      SELECT * FROM t1 FORCE INDEX(c) WHERE c IS NULL;
      id	a	c
      CHECK TABLE t1 EXTENDED;
      Table	Op	Msg_type	Msg_text
      test.t1	check	Warning	Incorrect datetime value: '2026-00-00' for function str_to_date
      test.t1	check	Warning	InnoDB: Index 'c' contains 0 entries, should be 1.
      test.t1	check	error	Corrupt

      Expected: FORCE INDEX(c) WHERE c IS NULL returns the same rows as a full scan, and CHECK TABLE succeeds.

      Attachments

        Issue Links

          Activity

            People

              bar Alexander Barkov
              alice Alice Sherepa
              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.