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

Wrong result of DAYNAME()=xxx in combination with condition_pushdown_for_derived=on

Details

    • Bug
    • Status: Open (View Workflow)
    • Major
    • Resolution: Unresolved
    • 10.2(EOL), 10.3(EOL), 10.4(EOL), 10.5
    • 10.5
    • Optimizer, Views
    • None

    Description

      I create two tables and a view as follows:

      DROP TABLE IF EXISTS t1, t2;
      DROP VIEW IF EXISTS v1;
       
      CREATE TABLE t1 (a INT, b DATE, c INT);
      INSERT INTO t1 VALUES
        (1,'2001-01-21',345),
        (6,'2001-01-20',315),
        (6,'2001-01-20',214);
       
      CREATE TABLE t2 (a INT, b INT);
      INSERT INTO t2 VALUES (2,19), (7,20);
      CREATE VIEW v1 AS SELECT a, b, max(c) AS max_c FROM t1
        GROUP BY a,b HAVING max_c < 707;
      

      and run this query:

      SELECT *, dayname(v1.b) FROM v1,t2 WHERE (v1.max_c>214) AND (t2.a>v1.a);
      

      +------+------------+-------+------+------+---------------+
      | a    | b          | max_c | a    | b    | dayname(v1.b) |
      +------+------------+-------+------+------+---------------+
      |    1 | 2001-01-21 |   345 |    2 |   19 | Sunday        |
      |    1 | 2001-01-21 |   345 |    7 |   20 | Sunday        |
      |    6 | 2001-01-20 |   315 |    7 |   20 | Saturday      |
      +------+------------+-------+------+------+---------------+
      

      So far so good.

      Now I add and extra condition to select only Sunday records:

      SET optimizer_switch='condition_pushdown_for_derived=off';
      SELECT *, dayname(v1.b) FROM v1,t2 WHERE (v1.max_c>214) AND (t2.a>v1.a) AND dayname(v1.b)='Sunday';
      

      +------+------------+-------+------+------+---------------+
      | a    | b          | max_c | a    | b    | dayname(v1.b) |
      +------+------------+-------+------+------+---------------+
      |    1 | 2001-01-21 |   345 |    2 |   19 | Sunday        |
      |    1 | 2001-01-21 |   345 |    7 |   20 | Sunday        |
      +------+------------+-------+------+------+---------------+
      

      Everything is still fine.

      Now I change the optimizer switch and run the same query:

      SET optimizer_switch='condition_pushdown_for_derived=on';
      SELECT *, dayname(v1.b) FROM v1,t2 WHERE (v1.max_c>214) AND (t2.a>v1.a) AND dayname(v1.b)='Sunday';
      

      It retuns empty set with the following warning:

      +---------+------+--------------------------------------------+
      | Level   | Code | Message                                    |
      +---------+------+--------------------------------------------+
      | Warning | 1292 | Truncated incorrect DOUBLE value: 'Sunday' |
      +---------+------+--------------------------------------------+
      

      The result of the last query looks wrong. It should return two records, like the previous query does.

      Attachments

        Issue Links

          Activity

            bar Alexander Barkov created issue -
            bar Alexander Barkov made changes -
            Field Original Value New Value
            bar Alexander Barkov made changes -
            Description I create two tables and a view as follows:
            {code:sql}
            DROP TABLE IF EXISTS t1, t2;
            DROP VIEW IF EXISTS v1;

            CREATE TABLE t1 (a INT, b DATE, c INT);
            INSERT INTO t1 VALUES
              (1,'2001-01-21',345),
              (6,'2001-01-20',315),
              (6,'2001-01-20',214);

            CREATE TABLE t2 (a INT, b INT);
            INSERT INTO t2 VALUES (2,19), (7,20);
            CREATE VIEW v1 AS SELECT a, b, max(c) AS max_c FROM t1
              GROUP BY a,b HAVING max_c < 707;
            {noformat}

            and run this query:

            {code:sql}
            SELECT *, dayname(v1.b) FROM v1,t2 WHERE (v1.max_c>214) AND (t2.a>v1.a);
            {code}
            {noformat}
            +------+------------+-------+------+------+---------------+
            | a | b | max_c | a | b | dayname(v1.b) |
            +------+------------+-------+------+------+---------------+
            | 1 | 2001-01-21 | 345 | 2 | 19 | Sunday |
            | 1 | 2001-01-21 | 345 | 7 | 20 | Sunday |
            | 6 | 2001-01-20 | 315 | 7 | 20 | Saturday |
            +------+------------+-------+------+------+---------------+
            {noformat}

            So far so good.

            Now I add and extra condition to select only Sunday records:

            {code:sql}
            SET optimizer_switch='condition_pushdown_for_derived=off';
            SELECT *, dayname(v1.b) FROM v1,t2 WHERE (v1.max_c>214) AND (t2.a>v1.a) AND dayname(v1.b)='Sunday';
            {code}
            {noformat}
            +------+------------+-------+------+------+---------------+
            | a | b | max_c | a | b | dayname(v1.b) |
            +------+------------+-------+------+------+---------------+
            | 1 | 2001-01-21 | 345 | 2 | 19 | Sunday |
            | 1 | 2001-01-21 | 345 | 7 | 20 | Sunday |
            +------+------------+-------+------+------+---------------+
            {noformat}
            Everything is still fine.


            Now I change the optimizer switch and run the same query:
            {code:sql}
            SET optimizer_switch='condition_pushdown_for_derived=on';
            SELECT *, dayname(v1.b) FROM v1,t2 WHERE (v1.max_c>214) AND (t2.a>v1.a) AND dayname(v1.b)='Sunday';
            {code}
            It retuns empty set with the following warning:
            {noformat}
            +---------+------+--------------------------------------------+
            | Level | Code | Message |
            +---------+------+--------------------------------------------+
            | Warning | 1292 | Truncated incorrect DOUBLE value: 'Sunday' |
            +---------+------+--------------------------------------------+
            {noformat}

            The result of the last query looks wrong. It should return two records, like the previous query does.
            I create two tables and a view as follows:
            {code:sql}
            DROP TABLE IF EXISTS t1, t2;
            DROP VIEW IF EXISTS v1;

            CREATE TABLE t1 (a INT, b DATE, c INT);
            INSERT INTO t1 VALUES
              (1,'2001-01-21',345),
              (6,'2001-01-20',315),
              (6,'2001-01-20',214);

            CREATE TABLE t2 (a INT, b INT);
            INSERT INTO t2 VALUES (2,19), (7,20);
            CREATE VIEW v1 AS SELECT a, b, max(c) AS max_c FROM t1
              GROUP BY a,b HAVING max_c < 707;
            {code}

            and run this query:

            {code:sql}
            SELECT *, dayname(v1.b) FROM v1,t2 WHERE (v1.max_c>214) AND (t2.a>v1.a);
            {code}
            {noformat}
            +------+------------+-------+------+------+---------------+
            | a | b | max_c | a | b | dayname(v1.b) |
            +------+------------+-------+------+------+---------------+
            | 1 | 2001-01-21 | 345 | 2 | 19 | Sunday |
            | 1 | 2001-01-21 | 345 | 7 | 20 | Sunday |
            | 6 | 2001-01-20 | 315 | 7 | 20 | Saturday |
            +------+------------+-------+------+------+---------------+
            {noformat}

            So far so good.

            Now I add and extra condition to select only Sunday records:

            {code:sql}
            SET optimizer_switch='condition_pushdown_for_derived=off';
            SELECT *, dayname(v1.b) FROM v1,t2 WHERE (v1.max_c>214) AND (t2.a>v1.a) AND dayname(v1.b)='Sunday';
            {code}
            {noformat}
            +------+------------+-------+------+------+---------------+
            | a | b | max_c | a | b | dayname(v1.b) |
            +------+------------+-------+------+------+---------------+
            | 1 | 2001-01-21 | 345 | 2 | 19 | Sunday |
            | 1 | 2001-01-21 | 345 | 7 | 20 | Sunday |
            +------+------------+-------+------+------+---------------+
            {noformat}
            Everything is still fine.


            Now I change the optimizer switch and run the same query:
            {code:sql}
            SET optimizer_switch='condition_pushdown_for_derived=on';
            SELECT *, dayname(v1.b) FROM v1,t2 WHERE (v1.max_c>214) AND (t2.a>v1.a) AND dayname(v1.b)='Sunday';
            {code}
            It retuns empty set with the following warning:
            {noformat}
            +---------+------+--------------------------------------------+
            | Level | Code | Message |
            +---------+------+--------------------------------------------+
            | Warning | 1292 | Truncated incorrect DOUBLE value: 'Sunday' |
            +---------+------+--------------------------------------------+
            {noformat}

            The result of the last query looks wrong. It should return two records, like the previous query does.
            serg Sergei Golubchik made changes -
            Fix Version/s 10.3 [ 22126 ]
            Fix Version/s 10.2.31 [ 24017 ]
            serg Sergei Golubchik made changes -
            Workflow MariaDB v3 [ 102653 ] MariaDB v4 [ 141711 ]
            ralf.gebhardt Ralf Gebhardt made changes -
            Fix Version/s 10.2 [ 14601 ]
            julien.fritsch Julien Fritsch made changes -
            Fix Version/s 10.3 [ 22126 ]
            julien.fritsch Julien Fritsch made changes -
            Fix Version/s 10.4 [ 22408 ]

            People

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