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

An index or a hash join changes the query result when a DATE, DATETIME or TIME column is compared with a YEAR column

    XMLWordPrintable

Details

    Description

      Likely cause: old code, not a recent change. CS 10.4.25 gives the same wrong result for the first two queries, and MySQL 5.5 to 9.1 for the first one. For the third query, CS 10.4.25 scans the whole index and finds the 3 rows, while CS 10.5.14 uses a range on the key and finds 1 row.

      CREATE TABLE t1 (y YEAR);
      INSERT INTO t1 VALUES (1901),(2020);
      CREATE TABLE t2 (d DATE, KEY (d));
      INSERT INTO t2 VALUES ('1901-00-00'),('2020-00-00'),('2020-01-01');
      --let $c= query_get_value(SELECT COUNT(*) AS c FROM t1 STRAIGHT_JOIN t2 FORCE INDEX (d) ON t2.d = t1.y, c, 1)
      if ($c != 2)
      {
        --die With ref access on the DATE key, t2.d = t1.y finds $c rows and not 2
      }
      DROP TABLE t2;
      CREATE TABLE t2 (t TIME);
      INSERT INTO t2 VALUES ('00:00:00'),('00:20:20');
      SET join_cache_level=3;
      --let $c= query_get_value(SELECT COUNT(*) AS c FROM t1 STRAIGHT_JOIN t2 ON t2.t = t1.y, c, 1)
      if ($c != 2)
      {
        --die With a hash join, t2.t = t1.y finds $c rows and not 2
      }
      SET join_cache_level=DEFAULT;
      DROP TABLE t1, t2;
      CREATE TABLE t1 (a INT PRIMARY KEY, y YEAR);
      INSERT INTO t1 VALUES (1,2020);
      CREATE TABLE t2 (t TIME, KEY (t));
      INSERT INTO t2 VALUES ('00:00:00'),('00:10:00'),('00:20:20'),('00:30:00');
      --let $c= query_get_value(SELECT COUNT(*) AS c FROM t1 JOIN t2 FORCE INDEX (t) ON t2.t > t1.y WHERE t1.a = 1, c, 1)
      if ($c != 3)
      {
        --die With a range on the TIME key, t2.t > t1.y finds $c rows and not 3
      }
      DROP TABLE t1, t2;

      Leads to:

      CS 13.1.2 87e62ca17270e163a6d41b3fc8d6f4d440f1bb88 (Debug, Clang 22.1.8-20260622) Build 07/11/2026

      mysqltest: At line 8: With ref access on the DATE key, t2.d = t1.y finds 0 rows and not 2

      An index or a hash join changes the query result. With IGNORE INDEX (d), t2.d = t1.y finds 2 rows: the comparison takes the YEAR as a date with the year only, as CAST(y AS DATE) does, so 1901 matches 1901-00-00 and 2020 matches 2020-00-00. With ref access on the key it finds no row.

      Run alone, the second and the third query fail too. With a hash join (join_cache_level=3), t2.t = t1.y finds no row, where the default join_cache_level finds 2: both years match 00:00:00, as CAST(y AS TIME) gives. With a range on the TIME key, t2.t > t1.y finds 1 row, where IGNORE INDEX (t) finds 3. A DATETIME key, COALESCE(t1.y) and t2.t IN (t1.y, '05:00:00') give a wrong result as well. Optimized builds give the same results.

      MySQL 5.5 to 9.1 have the same bug for the first query, and give other values for the other two:

      MySQL

      Query                            5.5.62  5.6.51  5.7.44  8.0.36  9.1.0
      t2.d = t1.y with the key on d    0       0       0       0       0
      t2.d = t1.y without the key      2       2       2       2       2
      t2.t = t1.y, no key on t         1       1       1       1       1
      t2.t > t1.y with the key on t    1       1       1       1       1
      t2.t > t1.y without the key      1       1       1       1       1

      From the code it seems that the key value is made from the YEAR in another way than the value that the comparison uses. The comparison converts the YEAR with Type_handler_year::Item_get_date(), which gives 2020-00-00. For ref access and the hash join, the key value is copied with do_field_string(), which Field_temporal::get_copy_func() in sql/field_conv.cc picks for a YEAR, with the note "TODO: MDEV-9343". For the range, Field_temporal::get_mm_leaf() stores the YEAR into the key field. Both take 2020 as the number 2020: 0000-00-00 for a DATE and 00:20:20 for a TIME. INSERT ... SELECT and ALTER TABLE store the same values, as MDEV-9343 shows for a DATETIME column. Field_temporal::can_optimize_keypart_ref() and Field_temporal::can_optimize_range() allow the key for every value.

      Attachments

        Issue Links

          Activity

            People

              bar Alexander Barkov
              Roel Roel Van de Paar
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - Not Specified
                  Not Specified
                  Logged:
                  Time Spent - 20m
                  20m

                  Git Integration

                    Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.