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

DECODE_ORACLE with two TIMESTAMP arguments hits Assertion `0' in cmp_item_timestamp::cmp_not_null() on a debug build, and returns the first branch for different timestamps on a build without assertions

    XMLWordPrintable

Details

    • Bug
    • Status: Open (View Workflow)
    • Major
    • Resolution: Unresolved
    • 13.3
    • None
    • Server
    • None

    Description

      Problem

      DECODE_ORACLE (and DECODE with sql_mode=ORACLE) compares its first argument with the following arguments through the NULL-equal comparison path. When both sides are of type TIMESTAMP, that path calls cmp_item_timestamp::cmp_not_null(), which is a stub: it contains only DBUG_ASSERT(0) and return 0. A debug build aborts; a build without assertions returns 0 (equal), so the first branch is taken for any two non-NULL timestamps.

      How to repeat

      Default sql_mode, fresh server:

      SELECT DECODE_ORACLE(NOW(), NOW(), 1, 2);

      With a table:

      CREATE TABLE t (c TIMESTAMP);
      INSERT INTO t VALUES ('2020-02-29 12:34:56');
      SELECT DECODE_ORACLE(c, c, c) FROM t;

      In Oracle mode:

      SET sql_mode=ORACLE;
      SELECT DECODE(NOW(), NOW(), 1, 2);

      I started a fresh server on an empty data directory three times for each statement (debug build of 13.3.0 main, --no-defaults) and ran it once: the statement hit the assertion in 3 of 3 runs, for all three forms. NOW(6) instead of NOW() crashes as well.

      Actual result

      Debug build, SIGABRT, the client loses the connection:

      sql/item_cmpfunc.cc:4521: virtual int cmp_item_timestamp::cmp_not_null(const Value *): Assertion `0' failed.
        cmp_item_timestamp::cmp_not_null(Value const*)
        Predicant_to_list_comparator::cmp_args_nulls_equal(THD*, Item_args*, unsigned int)
        Predicant_to_list_comparator::cmp_nulls_equal(THD*, Item_args*, unsigned int*)
        Item_func_decode_oracle::find_item()
        Item_func_case::int_op()
        Item_func_hybrid_field_type::val_int_from_int_op()
        Type_handler_int_result::Item_func_hybrid_field_type_val_int(Item_func_hybrid_field_type*)
        Item_func_hybrid_field_type::val_int()
        Type_handler::Item_send_long(Item*, Protocol*, st_value*)

      A build of the same commit without assertions (the coverage build; it carries a small local hook in sql/sql_parse.cc that does not touch this code) returns the wrong branch for two different timestamps. CREATE TABLE t (c TIMESTAMP, d TIMESTAMP); INSERT INTO t VALUES ('2020-01-01 00:00:00', '2021-01-01 00:00:00');

      SELECT DECODE_ORACLE(c, d, 'same', 'diff') FROM t;     -> same   (expected: diff)
      SELECT DECODE_ORACLE(c, c, 'same', 'diff') FROM t;     -> same
      SELECT CASE c WHEN d THEN 'same' ELSE 'diff' END FROM t;  -> diff   (correct)

      2 of 2 fresh servers. The ASAN/UBSAN build of the same commit hits the assertion too (2 of 2).

      Notes

      The comment above cmp_item_timestamp::cmp_not_null() says it will be implemented together with a TIMESTAMP WITH LOCAL TIME ZONE syntax, and that for now a TIMESTAMP is compared with a non-TIMESTAMP using DATETIME. The NULL-equal path of DECODE_ORACLE (Predicant_to_list_comparator::cmp_args_nulls_equal()) reaches the stub when the predicant and an argument are both TIMESTAMP.

      Not affected (fresh-server runs on a debug build of the same commit: DECODE_ORACLE(<TIMESTAMP column>, <DATETIME column>, 1, 2) 3 of 3 on the unpatched build, the others once each on the build that carries the local hook): CASE NOW() WHEN NOW() THEN 1 ELSE 2 END, DECODE_ORACLE(UTC_TIMESTAMP, UTC_TIMESTAMP, 1, 2), DECODE_ORACLE(<TIMESTAMP column>, '2020-02-29 12:34:56', 1, 2), DECODE_ORACLE(<TIMESTAMP column>, <DATETIME column>, 1, 2), and DECODE_ORACLE(c, c, 1, 2) when the value of c is NULL. DATETIME and DATE literals and CAST(... AS DATETIME) as both arguments do not crash either.

      Attachments

        Activity

          People

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