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

Wrong result for SELECT..WHERE varchar_column=DATE'2001-01-01' AND varchar_column='2001-01-01'

Details

    Description

      DROP TABLE IF EXISTS t1;
      CREATE TABLE t1 (a VARCHAR(40));
      INSERT INTO t1 VALUES ('2001-01-01'),('2001-01-01x');
      SELECT * FROM t1 WHERE a=DATE'2001-01-01' AND a='2001-01-01';

      returns:

      +-------------+
      | a           |
      +-------------+
      | 2001-01-01  |
      | 2001-01-01x |
      +-------------+

      The second row is obviously wrong.

      If I write WHERE the other way round:

      SELECT * FROM t1 WHERE a='2001-01-01' AND a=DATE'2001-01-01';

      it works fine and correctly returns one row:

      +------------+
      | a          |
      +------------+
      | 2001-01-01 |
      +------------+

      The same problem is repeatable with ENUM type:

      DROP TABLE IF EXISTS t1;
      CREATE TABLE t1 (a ENUM('2001-01-01','2001-01-01x'));
      INSERT INTO t1 VALUES ('2001-01-01'),('2001-01-01x');
      SELECT * FROM t1 WHERE a=DATE'2001-01-01' AND a='2001-01-01';
      SELECT * FROM t1 WHERE a='2001-01-01' AND a=DATE'2001-01-01';

      Another example using ROW syntax:

      DROP TABLE IF EXISTS t1;
      CREATE TABLE t1 (a VARCHAR(20));
      INSERT INTO t1 VALUES ('2001-01-01x'),('2001-01-01');
      SELECT * FROM t1 WHERE (a,a)=('2001-01-01x',DATE'2001-01-01');
      SELECT * FROM t1 WHERE (a,a)=(DATE'2001-01-01','2001-01-01x')

      The first query correctly returns one row, the second query erroneously returns two rows.

      Another example:

      DROP TABLE IF EXISTS t1;
      CREATE TABLE t1 (a VARCHAR(40),b VARCHAR(40));
      INSERT INTO t1 VALUES ('2001-01-01','2001-01-01x');
      SELECT * FROM t1 WHERE a=b AND a=DATE'2001-01-01';

      returns:

      +------------+-------------+
      | a          | b           |
      +------------+-------------+
      | 2001-01-01 | 2001-01-01x |
      +------------+-------------+

      This is wrong. The expected result is empty set.

      The same problem happens with ENUM:

      DROP TABLE IF EXISTS t1;
      CREATE TABLE t1 (a ENUM('2001-01-01','2001-01-01x'),b ENUM('2001-01-01','2001-01-01x'));
      INSERT INTO t1 VALUES ('2001-01-01','2001-01-01x');
      SELECT * FROM t1 WHERE a=b AND a=DATE'2001-01-01';

      It erroneously returns one row. The expected result it to return no rows.

      Attachments

        Issue Links

          Activity

            bar Alexander Barkov created issue -
            bar Alexander Barkov made changes -
            Field Original Value New Value
            Description {code}
            DROP TABLE IF EXISTS t1;
            CREATE TABLE t1 (a VARCHAR(40));
            INSERT INTO t1 VALUES ('2001-01-01'),('2001-01-01x');
            SELECT * FROM t1 WHERE a=DATE'2001-01-01' AND a='2001-01-01';
            {code}
            returns:
            {noformat}
            +-------------+
            | a |
            +-------------+
            | 2001-01-01 |
            | 2001-01-01x |
            +-------------+
            {noformat}
            The second row is obviously wrong.
            {code}
            DROP TABLE IF EXISTS t1;
            CREATE TABLE t1 (a VARCHAR(40));
            INSERT INTO t1 VALUES ('2001-01-01'),('2001-01-01x');
            SELECT * FROM t1 WHERE a=DATE'2001-01-01' AND a='2001-01-01';
            {code}
            returns:
            {noformat}
            +-------------+
            | a |
            +-------------+
            | 2001-01-01 |
            | 2001-01-01x |
            +-------------+
            {noformat}
            The second row is obviously wrong.

            Another example:
            {code}
            DROP TABLE IF EXISTS t1;
            CREATE TABLE t1 (a VARCHAR(40),b VARCHAR(40));
            INSERT INTO t1 VALUES ('2001-01-01','2001-01-01x');
            SELECT * FROM t1 WHERE a=b AND a=DATE'2001-01-01';
            {code}
            returns:
            {noformat}
            +------------+-------------+
            | a | b |
            +------------+-------------+
            | 2001-01-01 | 2001-01-01x |
            +------------+-------------+
            {noformat}
            This is wrong. The expected result is empty set.
            bar Alexander Barkov made changes -
            Description {code}
            DROP TABLE IF EXISTS t1;
            CREATE TABLE t1 (a VARCHAR(40));
            INSERT INTO t1 VALUES ('2001-01-01'),('2001-01-01x');
            SELECT * FROM t1 WHERE a=DATE'2001-01-01' AND a='2001-01-01';
            {code}
            returns:
            {noformat}
            +-------------+
            | a |
            +-------------+
            | 2001-01-01 |
            | 2001-01-01x |
            +-------------+
            {noformat}
            The second row is obviously wrong.

            Another example:
            {code}
            DROP TABLE IF EXISTS t1;
            CREATE TABLE t1 (a VARCHAR(40),b VARCHAR(40));
            INSERT INTO t1 VALUES ('2001-01-01','2001-01-01x');
            SELECT * FROM t1 WHERE a=b AND a=DATE'2001-01-01';
            {code}
            returns:
            {noformat}
            +------------+-------------+
            | a | b |
            +------------+-------------+
            | 2001-01-01 | 2001-01-01x |
            +------------+-------------+
            {noformat}
            This is wrong. The expected result is empty set.
            {code}
            DROP TABLE IF EXISTS t1;
            CREATE TABLE t1 (a VARCHAR(40));
            INSERT INTO t1 VALUES ('2001-01-01'),('2001-01-01x');
            SELECT * FROM t1 WHERE a=DATE'2001-01-01' AND a='2001-01-01';
            {code}
            returns:
            {noformat}
            +-------------+
            | a |
            +-------------+
            | 2001-01-01 |
            | 2001-01-01x |
            +-------------+
            {noformat}
            The second row is obviously wrong.

            If I write WHERE the other way round:
            {code}
            SELECT * FROM t1 WHERE a='2001-01-01' AND a=DATE'2001-01-01';
            {code}
            it works fine and correctly returns one row:
            {noformat}
            +------------+
            | a |
            +------------+
            | 2001-01-01 |
            +------------+
            {noformat}

            Another example:
            {code}
            DROP TABLE IF EXISTS t1;
            CREATE TABLE t1 (a VARCHAR(40),b VARCHAR(40));
            INSERT INTO t1 VALUES ('2001-01-01','2001-01-01x');
            SELECT * FROM t1 WHERE a=b AND a=DATE'2001-01-01';
            {code}
            returns:
            {noformat}
            +------------+-------------+
            | a | b |
            +------------+-------------+
            | 2001-01-01 | 2001-01-01x |
            +------------+-------------+
            {noformat}
            This is wrong. The expected result is empty set.
            bar Alexander Barkov made changes -
            Description {code}
            DROP TABLE IF EXISTS t1;
            CREATE TABLE t1 (a VARCHAR(40));
            INSERT INTO t1 VALUES ('2001-01-01'),('2001-01-01x');
            SELECT * FROM t1 WHERE a=DATE'2001-01-01' AND a='2001-01-01';
            {code}
            returns:
            {noformat}
            +-------------+
            | a |
            +-------------+
            | 2001-01-01 |
            | 2001-01-01x |
            +-------------+
            {noformat}
            The second row is obviously wrong.

            If I write WHERE the other way round:
            {code}
            SELECT * FROM t1 WHERE a='2001-01-01' AND a=DATE'2001-01-01';
            {code}
            it works fine and correctly returns one row:
            {noformat}
            +------------+
            | a |
            +------------+
            | 2001-01-01 |
            +------------+
            {noformat}

            Another example:
            {code}
            DROP TABLE IF EXISTS t1;
            CREATE TABLE t1 (a VARCHAR(40),b VARCHAR(40));
            INSERT INTO t1 VALUES ('2001-01-01','2001-01-01x');
            SELECT * FROM t1 WHERE a=b AND a=DATE'2001-01-01';
            {code}
            returns:
            {noformat}
            +------------+-------------+
            | a | b |
            +------------+-------------+
            | 2001-01-01 | 2001-01-01x |
            +------------+-------------+
            {noformat}
            This is wrong. The expected result is empty set.
            {code}
            DROP TABLE IF EXISTS t1;
            CREATE TABLE t1 (a VARCHAR(40));
            INSERT INTO t1 VALUES ('2001-01-01'),('2001-01-01x');
            SELECT * FROM t1 WHERE a=DATE'2001-01-01' AND a='2001-01-01';
            {code}
            returns:
            {noformat}
            +-------------+
            | a |
            +-------------+
            | 2001-01-01 |
            | 2001-01-01x |
            +-------------+
            {noformat}
            The second row is obviously wrong.

            If I write WHERE the other way round:
            {code}
            SELECT * FROM t1 WHERE a='2001-01-01' AND a=DATE'2001-01-01';
            {code}
            it works fine and correctly returns one row:
            {noformat}
            +------------+
            | a |
            +------------+
            | 2001-01-01 |
            +------------+
            {noformat}

            Another example using ROW syntax:
            {code}
            DROP TABLE IF EXISTS t1;
            CREATE TABLE t1 (a VARCHAR(20));
            INSERT INTO t1 VALUES ('2001-01-01x'),('2001-01-01');
            SELECT * FROM t1 WHERE (a,a)=('2001-01-01x',DATE'2001-01-01');
            SELECT * FROM t1 WHERE (a,a)=(DATE'2001-01-01','2001-01-01x')
            {code}
            The first query correctly returns one row, the second query erroneously returns two rows.


            Another example:
            {code}
            DROP TABLE IF EXISTS t1;
            CREATE TABLE t1 (a VARCHAR(40),b VARCHAR(40));
            INSERT INTO t1 VALUES ('2001-01-01','2001-01-01x');
            SELECT * FROM t1 WHERE a=b AND a=DATE'2001-01-01';
            {code}
            returns:
            {noformat}
            +------------+-------------+
            | a | b |
            +------------+-------------+
            | 2001-01-01 | 2001-01-01x |
            +------------+-------------+
            {noformat}
            This is wrong. The expected result is empty set.
            bar Alexander Barkov made changes -
            Description {code}
            DROP TABLE IF EXISTS t1;
            CREATE TABLE t1 (a VARCHAR(40));
            INSERT INTO t1 VALUES ('2001-01-01'),('2001-01-01x');
            SELECT * FROM t1 WHERE a=DATE'2001-01-01' AND a='2001-01-01';
            {code}
            returns:
            {noformat}
            +-------------+
            | a |
            +-------------+
            | 2001-01-01 |
            | 2001-01-01x |
            +-------------+
            {noformat}
            The second row is obviously wrong.

            If I write WHERE the other way round:
            {code}
            SELECT * FROM t1 WHERE a='2001-01-01' AND a=DATE'2001-01-01';
            {code}
            it works fine and correctly returns one row:
            {noformat}
            +------------+
            | a |
            +------------+
            | 2001-01-01 |
            +------------+
            {noformat}

            Another example using ROW syntax:
            {code}
            DROP TABLE IF EXISTS t1;
            CREATE TABLE t1 (a VARCHAR(20));
            INSERT INTO t1 VALUES ('2001-01-01x'),('2001-01-01');
            SELECT * FROM t1 WHERE (a,a)=('2001-01-01x',DATE'2001-01-01');
            SELECT * FROM t1 WHERE (a,a)=(DATE'2001-01-01','2001-01-01x')
            {code}
            The first query correctly returns one row, the second query erroneously returns two rows.


            Another example:
            {code}
            DROP TABLE IF EXISTS t1;
            CREATE TABLE t1 (a VARCHAR(40),b VARCHAR(40));
            INSERT INTO t1 VALUES ('2001-01-01','2001-01-01x');
            SELECT * FROM t1 WHERE a=b AND a=DATE'2001-01-01';
            {code}
            returns:
            {noformat}
            +------------+-------------+
            | a | b |
            +------------+-------------+
            | 2001-01-01 | 2001-01-01x |
            +------------+-------------+
            {noformat}
            This is wrong. The expected result is empty set.
            {code}
            DROP TABLE IF EXISTS t1;
            CREATE TABLE t1 (a VARCHAR(40));
            INSERT INTO t1 VALUES ('2001-01-01'),('2001-01-01x');
            SELECT * FROM t1 WHERE a=DATE'2001-01-01' AND a='2001-01-01';
            {code}
            returns:
            {noformat}
            +-------------+
            | a |
            +-------------+
            | 2001-01-01 |
            | 2001-01-01x |
            +-------------+
            {noformat}
            The second row is obviously wrong.

            If I write WHERE the other way round:
            {code}
            SELECT * FROM t1 WHERE a='2001-01-01' AND a=DATE'2001-01-01';
            {code}
            it works fine and correctly returns one row:
            {noformat}
            +------------+
            | a |
            +------------+
            | 2001-01-01 |
            +------------+
            {noformat}

            Another example using ROW syntax:
            {code}
            DROP TABLE IF EXISTS t1;
            CREATE TABLE t1 (a VARCHAR(20));
            INSERT INTO t1 VALUES ('2001-01-01x'),('2001-01-01');
            SELECT * FROM t1 WHERE (a,a)=('2001-01-01x',DATE'2001-01-01');
            SELECT * FROM t1 WHERE (a,a)=(DATE'2001-01-01','2001-01-01x')
            {code}
            The first query correctly returns one row, the second query erroneously returns two rows.


            Another example:
            {code}
            DROP TABLE IF EXISTS t1;
            CREATE TABLE t1 (a VARCHAR(40),b VARCHAR(40));
            INSERT INTO t1 VALUES ('2001-01-01','2001-01-01x');
            SELECT * FROM t1 WHERE a=b AND a=DATE'2001-01-01';
            {code}
            returns:
            {noformat}
            +------------+-------------+
            | a | b |
            +------------+-------------+
            | 2001-01-01 | 2001-01-01x |
            +------------+-------------+
            {noformat}
            This is wrong. The expected result is empty set.

            The same problem happens with ENUM:
            {code}
            DROP TABLE IF EXISTS t1;
            CREATE TABLE t1 (a ENUM('2001-01-01','2001-01-01x'),b ENUM('2001-01-01','2001-01-01x'));
            INSERT INTO t1 VALUES ('2001-01-01','2001-01-01x');
            SELECT * FROM t1 WHERE a=b AND a=DATE'2001-01-01';
            {code}
            It erroneously returns one row. The expected result it to return no rows.
            bar Alexander Barkov made changes -
            Description {code}
            DROP TABLE IF EXISTS t1;
            CREATE TABLE t1 (a VARCHAR(40));
            INSERT INTO t1 VALUES ('2001-01-01'),('2001-01-01x');
            SELECT * FROM t1 WHERE a=DATE'2001-01-01' AND a='2001-01-01';
            {code}
            returns:
            {noformat}
            +-------------+
            | a |
            +-------------+
            | 2001-01-01 |
            | 2001-01-01x |
            +-------------+
            {noformat}
            The second row is obviously wrong.

            If I write WHERE the other way round:
            {code}
            SELECT * FROM t1 WHERE a='2001-01-01' AND a=DATE'2001-01-01';
            {code}
            it works fine and correctly returns one row:
            {noformat}
            +------------+
            | a |
            +------------+
            | 2001-01-01 |
            +------------+
            {noformat}

            Another example using ROW syntax:
            {code}
            DROP TABLE IF EXISTS t1;
            CREATE TABLE t1 (a VARCHAR(20));
            INSERT INTO t1 VALUES ('2001-01-01x'),('2001-01-01');
            SELECT * FROM t1 WHERE (a,a)=('2001-01-01x',DATE'2001-01-01');
            SELECT * FROM t1 WHERE (a,a)=(DATE'2001-01-01','2001-01-01x')
            {code}
            The first query correctly returns one row, the second query erroneously returns two rows.


            Another example:
            {code}
            DROP TABLE IF EXISTS t1;
            CREATE TABLE t1 (a VARCHAR(40),b VARCHAR(40));
            INSERT INTO t1 VALUES ('2001-01-01','2001-01-01x');
            SELECT * FROM t1 WHERE a=b AND a=DATE'2001-01-01';
            {code}
            returns:
            {noformat}
            +------------+-------------+
            | a | b |
            +------------+-------------+
            | 2001-01-01 | 2001-01-01x |
            +------------+-------------+
            {noformat}
            This is wrong. The expected result is empty set.

            The same problem happens with ENUM:
            {code}
            DROP TABLE IF EXISTS t1;
            CREATE TABLE t1 (a ENUM('2001-01-01','2001-01-01x'),b ENUM('2001-01-01','2001-01-01x'));
            INSERT INTO t1 VALUES ('2001-01-01','2001-01-01x');
            SELECT * FROM t1 WHERE a=b AND a=DATE'2001-01-01';
            {code}
            It erroneously returns one row. The expected result it to return no rows.
            {code}
            DROP TABLE IF EXISTS t1;
            CREATE TABLE t1 (a VARCHAR(40));
            INSERT INTO t1 VALUES ('2001-01-01'),('2001-01-01x');
            SELECT * FROM t1 WHERE a=DATE'2001-01-01' AND a='2001-01-01';
            {code}
            returns:
            {noformat}
            +-------------+
            | a |
            +-------------+
            | 2001-01-01 |
            | 2001-01-01x |
            +-------------+
            {noformat}
            The second row is obviously wrong.

            If I write WHERE the other way round:
            {code}
            SELECT * FROM t1 WHERE a='2001-01-01' AND a=DATE'2001-01-01';
            {code}
            it works fine and correctly returns one row:
            {noformat}
            +------------+
            | a |
            +------------+
            | 2001-01-01 |
            +------------+
            {noformat}

            The same problem is repeatable with ENUM type:
            {code}
            DROP TABLE IF EXISTS t1;
            CREATE TABLE t1 (a ENUM('2001-01-01','2001-01-01x'));
            INSERT INTO t1 VALUES ('2001-01-01'),('2001-01-01x');
            SELECT * FROM t1 WHERE a=DATE'2001-01-01' AND a='2001-01-01';
            SELECT * FROM t1 WHERE a='2001-01-01' AND a=DATE'2001-01-01';
            {code}

            Another example using ROW syntax:
            {code}
            DROP TABLE IF EXISTS t1;
            CREATE TABLE t1 (a VARCHAR(20));
            INSERT INTO t1 VALUES ('2001-01-01x'),('2001-01-01');
            SELECT * FROM t1 WHERE (a,a)=('2001-01-01x',DATE'2001-01-01');
            SELECT * FROM t1 WHERE (a,a)=(DATE'2001-01-01','2001-01-01x')
            {code}
            The first query correctly returns one row, the second query erroneously returns two rows.


            Another example:
            {code}
            DROP TABLE IF EXISTS t1;
            CREATE TABLE t1 (a VARCHAR(40),b VARCHAR(40));
            INSERT INTO t1 VALUES ('2001-01-01','2001-01-01x');
            SELECT * FROM t1 WHERE a=b AND a=DATE'2001-01-01';
            {code}
            returns:
            {noformat}
            +------------+-------------+
            | a | b |
            +------------+-------------+
            | 2001-01-01 | 2001-01-01x |
            +------------+-------------+
            {noformat}
            This is wrong. The expected result is empty set.

            The same problem happens with ENUM:
            {code}
            DROP TABLE IF EXISTS t1;
            CREATE TABLE t1 (a ENUM('2001-01-01','2001-01-01x'),b ENUM('2001-01-01','2001-01-01x'));
            INSERT INTO t1 VALUES ('2001-01-01','2001-01-01x');
            SELECT * FROM t1 WHERE a=b AND a=DATE'2001-01-01';
            {code}
            It erroneously returns one row. The expected result it to return no rows.
            bar Alexander Barkov made changes -
            Fix Version/s 10.1.7 [ 19604 ]
            Fix Version/s 10.1 [ 16100 ]
            Resolution Fixed [ 1 ]
            Status Open [ 1 ] Closed [ 6 ]
            bar Alexander Barkov made changes -
            bar Alexander Barkov made changes -
            Labels upstream
            bar Alexander Barkov made changes -
            Labels upstream propagation upstream
            serg Sergei Golubchik made changes -
            Workflow MariaDB v3 [ 71253 ] MariaDB v4 [ 149540 ]

            People

              bar Alexander Barkov
              bar Alexander Barkov
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Git Integration

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