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

DEFAULT(col) evaluates to a bad value in WHERE clause

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open (View Workflow)
    • Priority: Critical
    • Resolution: Unresolved
    • Affects Version/s: 10.2, 10.3, 10.4, 10.5
    • Fix Version/s: 10.2
    • Component/s: OTHER
    • Labels:
      None

      Description

      I run this script:

      DROP TABLE IF EXISTS t1;
      CREATE TABLE t1 (a BIGINT NOT NULL DEFAULT (IF(false,UNIX_TIMESTAMP(),10)));
      INSERT INTO t1 VALUES (10000);
      SELECT
        a,
        DEFAULT(a),
        CASE WHEN a THEN DEFAULT(a) END AS c,
        CASE WHEN a THEN DEFAULT(a) END = 10 AS ce
      FROM t1;
      

      It returns the following correct result:

      +-------+------------+------+------+
      | a     | DEFAULT(a) | c    | ce   |
      +-------+------------+------+------+
      | 10000 |         10 |   10 |    1 |
      +-------+------------+------+------+
      

      Looks good so far. The value of the CASE expression (result column c) is correctly evaluates to 10, and the comparison expression (result column ce) correctly evaluates to true.

      Now I put the comparison expression into the WHERE clause:

      SELECT a FROM t1 WHERE CASE WHEN a THEN DEFAULT(a) END=10;
      

      Empty set (0.001 sec)
      

      Looks wrong.

      Now I put a breakpoint in Field_longlong::val_int() and rerun the last SQL query with the WHERE condition.
      val_int() is called two times:

      • I skip the first call which is evaluation of WHEN a
      • I print the following values during the second call, which is evaluation of THEN DEFAULT(a):

      (gdb) print ptr
      $6 = (uchar *) 0x62b000001a99 "\245\245\245\245\245\245\245\245", '\217' <repeats 15 times>
      (gdb) fin
      Run till exit from #0  Field_longlong::val_int (this=0x62b0000019c8)
          at /home/bar/maria-git/server.10.2.asan/sql/field.cc:4514
      Item_field::val_int_result (this=0x62b000000d88)
          at /home/bar/maria-git/server.10.2.asan/sql/item.cc:2898
      2898	  return result_field->val_int();
      Value returned is $7 = -6510615555426900571
      

      Notice:

      • Field_longlong::ptr points to a non-initialized buffer
      • val_int() returns a non-relevant value instead of 10

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              sanja Oleksandr Byelkin
              Reporter:
              bar Alexander Barkov
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: