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

Nested query does not give same result as subquery and an outer query

Details

    • Bug
    • Status: Closed (View Workflow)
    • Major
    • Resolution: Fixed
    • 10.2.6, 10.2.7, 10.2.8, 10.3.1
    • N/A
    • Optimizer
    • Windows 64

    Description

      A nested query does not return correct results in at least one case.

      Scenario:

      • A nested query query (Select col... where col in (subquery)) returns empty.
      • Subquery correctly returns the rows (x,y,z).
      • OuterQuery takes (z,y,z) in an IN statement and correctly selects the relevant results (Select col... where col in (x,y,z))

      This is not happening in 10.1 but only on 102 onwards.

      I could not reproduce in a simpler scenario. Perhaps this is due to using fulltext search in the subquery, or any other reason.

      Details:
      https://stackoverflow.com/questions/45964454/subqueries-do-not-work-in-windows-but-works-on-linux

      Tables are innodb. Attached the cfg settings I added to my.ini.

      Attachments

        Issue Links

          Activity

            costateixeira Jose Costa Teixeira created issue -
            costateixeira Jose Costa Teixeira added a comment - - edited

            Update: After checking the result of EXPLAIN, it seems that this is related to the use of temporary tables (please forgive my language, I am not very familiar with SQL)
            The version where it does not work has "Using filesort" while the one that works has a "Using temporary; Using filesort"
            After finding that the temporary tables could be due to ORDER BY on a same column, I made a workaround :
            added a "ORDER BY table1.entityKey DESC" in the subquery.
            This seems to resolve the issue. Perhaps it points to a solution.

            The query that works is (in bold underlined the addition to make it work) is:

            Select table1.entityKey
            from table1
            where table1.Deleted = 0
            and table1.MasterKey is null
            and table1.entityTypeKey = 8
            and table1.entityKey in
            (select table2.entityKey
            from table2
            where table2.Flag <> 2
            and (table2.IndexKey = 4 and MATCH (table2.xhtmltext) AGAINST ('gold')) ORDER BY entityKey DESC)
            order by table1.entityKey DESC

            costateixeira Jose Costa Teixeira added a comment - - edited Update: After checking the result of EXPLAIN, it seems that this is related to the use of temporary tables (please forgive my language, I am not very familiar with SQL) The version where it does not work has "Using filesort" while the one that works has a "Using temporary; Using filesort" After finding that the temporary tables could be due to ORDER BY on a same column, I made a workaround : added a "ORDER BY table1.entityKey DESC" in the subquery. This seems to resolve the issue. Perhaps it points to a solution. The query that works is (in bold underlined the addition to make it work) is: Select table1.entityKey from table1 where table1.Deleted = 0 and table1.MasterKey is null and table1.entityTypeKey = 8 and table1.entityKey in (select table2.entityKey from table2 where table2.Flag <> 2 and (table2.IndexKey = 4 and MATCH (table2.xhtmltext) AGAINST ('gold')) ORDER BY entityKey DESC ) order by table1.entityKey DESC
            elenst Elena Stepanova made changes -
            Field Original Value New Value
            Assignee Elena Stepanova [ elenst ]
            elenst Elena Stepanova made changes -
            elenst Elena Stepanova made changes -

            Thanks for the report. As a workaround, you can set optimizer_switch=orderby_uses_equalities=off in the config file, it should help.

            Test case

            --source include/have_innodb.inc
             
            CREATE TABLE t1 (entityKey INT PRIMARY KEY) ENGINE=InnoDB;
            INSERT INTO t1 VALUES (1),(2),(3);
             
            CREATE TABLE t2 (entityKey INT, xhtmltext TEXT, FULLTEXT(xhtmltext)) ENGINE=InnoDB;
            INSERT INTO t2 VALUES (1,"silver"),(2,"gold"),(3,"gold"),(4,"gold");
             
            SELECT t1.entityKey FROM t1 
            WHERE t1.entityKey IN (
              SELECT t2.entityKey FROM t2 
              WHERE MATCH (t2.xhtmltext) AGAINST ('gold')
            ) 
            ORDER BY t1.entityKey DESC;
             
            # Cleanup
            DROP TABLE t1, t2;
            

            Actual result on 10.2 with default optimizer_switch

            MariaDB [test]> SELECT t1.entityKey FROM t1 
                -> WHERE t1.entityKey IN (
                ->   SELECT t2.entityKey FROM t2 
                ->   WHERE MATCH (t2.xhtmltext) AGAINST ('gold')
                -> ) 
                -> ORDER BY t1.entityKey DESC;
            Empty set (0.06 sec)
            

            Expected result

            MariaDB [test]> SELECT t1.entityKey FROM t1 
                -> WHERE t1.entityKey IN (
                ->   SELECT t2.entityKey FROM t2 
                ->   WHERE MATCH (t2.xhtmltext) AGAINST ('gold')
                -> ) 
                -> ORDER BY t1.entityKey DESC;
            +-----------+
            | entityKey |
            +-----------+
            |         3 |
            |         2 |
            +-----------+
            2 rows in set (0.08 sec)
            

            After fixing, please also check this test case. It differs from the first one only in the highlighted line, one extra value is inserted:

            Another test case

            --source include/have_innodb.inc
             
            CREATE TABLE t1 (entityKey INT PRIMARY KEY) ENGINE=InnoDB;
            INSERT INTO t1 VALUES (1),(2),(3),(4);
             
            CREATE TABLE t2 (entityKey INT, xhtmltext TEXT, FULLTEXT(xhtmltext)) ENGINE=InnoDB;
            INSERT INTO t2 VALUES (1,"silver"),(2,"gold"),(3,"gold"),(4,"gold");
             
            SELECT t1.entityKey FROM t1 
            WHERE t1.entityKey IN (
              SELECT t2.entityKey FROM t2 
              WHERE MATCH (t2.xhtmltext) AGAINST ('gold')
            ) 
            ORDER BY t1.entityKey DESC;
             
            # Cleanup
            DROP TABLE t1, t2;
            

            Actual result from the 2nd test case

            MariaDB [test]> SELECT t1.entityKey FROM t1 
                -> WHERE t1.entityKey IN (
                ->   SELECT t2.entityKey FROM t2 
                ->   WHERE MATCH (t2.xhtmltext) AGAINST ('gold')
                -> ) 
                -> ORDER BY t1.entityKey DESC;
            +-----------+
            | entityKey |
            +-----------+
            |         4 |
            |         4 |
            |         4 |
            +-----------+
            3 rows in set (0.00 sec)
            

            With optimizer_switch=orderby_uses_equalities=off both test cases return expected results.

            elenst Elena Stepanova added a comment - Thanks for the report. As a workaround, you can set optimizer_switch=orderby_uses_equalities=off in the config file, it should help. Test case --source include/have_innodb.inc   CREATE TABLE t1 (entityKey INT PRIMARY KEY ) ENGINE=InnoDB; INSERT INTO t1 VALUES (1),(2),(3);   CREATE TABLE t2 (entityKey INT , xhtmltext TEXT, FULLTEXT(xhtmltext)) ENGINE=InnoDB; INSERT INTO t2 VALUES (1, "silver" ),(2, "gold" ),(3, "gold" ),(4, "gold" );   SELECT t1.entityKey FROM t1 WHERE t1.entityKey IN ( SELECT t2.entityKey FROM t2 WHERE MATCH (t2.xhtmltext) AGAINST ( 'gold' ) ) ORDER BY t1.entityKey DESC ;   # Cleanup DROP TABLE t1, t2; Actual result on 10.2 with default optimizer_switch MariaDB [test]> SELECT t1.entityKey FROM t1 -> WHERE t1.entityKey IN ( -> SELECT t2.entityKey FROM t2 -> WHERE MATCH (t2.xhtmltext) AGAINST ( 'gold' ) -> ) -> ORDER BY t1.entityKey DESC ; Empty set (0.06 sec) Expected result MariaDB [test]> SELECT t1.entityKey FROM t1 -> WHERE t1.entityKey IN ( -> SELECT t2.entityKey FROM t2 -> WHERE MATCH (t2.xhtmltext) AGAINST ( 'gold' ) -> ) -> ORDER BY t1.entityKey DESC ; + -----------+ | entityKey | + -----------+ | 3 | | 2 | + -----------+ 2 rows in set (0.08 sec) After fixing, please also check this test case. It differs from the first one only in the highlighted line, one extra value is inserted: Another test case --source include/have_innodb.inc   CREATE TABLE t1 (entityKey INT PRIMARY KEY ) ENGINE=InnoDB; INSERT INTO t1 VALUES (1),(2),(3),(4);   CREATE TABLE t2 (entityKey INT , xhtmltext TEXT, FULLTEXT(xhtmltext)) ENGINE=InnoDB; INSERT INTO t2 VALUES (1, "silver" ),(2, "gold" ),(3, "gold" ),(4, "gold" );   SELECT t1.entityKey FROM t1 WHERE t1.entityKey IN ( SELECT t2.entityKey FROM t2 WHERE MATCH (t2.xhtmltext) AGAINST ( 'gold' ) ) ORDER BY t1.entityKey DESC ;   # Cleanup DROP TABLE t1, t2; Actual result from the 2nd test case MariaDB [test]> SELECT t1.entityKey FROM t1 -> WHERE t1.entityKey IN ( -> SELECT t2.entityKey FROM t2 -> WHERE MATCH (t2.xhtmltext) AGAINST ( 'gold' ) -> ) -> ORDER BY t1.entityKey DESC ; + -----------+ | entityKey | + -----------+ | 4 | | 4 | | 4 | + -----------+ 3 rows in set (0.00 sec) With optimizer_switch=orderby_uses_equalities=off both test cases return expected results.
            elenst Elena Stepanova made changes -
            Component/s Optimizer [ 10200 ]
            Fix Version/s 10.2 [ 14601 ]
            Assignee Elena Stepanova [ elenst ] Varun Gupta [ varun ]
            elenst Elena Stepanova made changes -
            Status Open [ 1 ] Confirmed [ 10101 ]

            Hi. The workaround does not work in my specific case. I will have to wait for a fix.

            Thanks!

            costateixeira Jose Costa Teixeira added a comment - Hi. The workaround does not work in my specific case. I will have to wait for a fix. Thanks!
            varun Varun Gupta (Inactive) made changes -
            Fix Version/s 10.3 [ 22126 ]
            Fix Version/s 10.2 [ 14601 ]

            It is possibly a duplicate of MDEV-13694 which is in review

            varun Varun Gupta (Inactive) added a comment - It is possibly a duplicate of MDEV-13694 which is in review
            serg Sergei Golubchik made changes -
            Fix Version/s 10.4 [ 22408 ]
            varun Varun Gupta (Inactive) made changes -
            varun Varun Gupta (Inactive) made changes -
            Fix Version/s N/A [ 14700 ]
            Fix Version/s 10.3 [ 22126 ]
            Fix Version/s 10.4 [ 22408 ]
            Resolution Fixed [ 1 ]
            Status Confirmed [ 10101 ] Closed [ 6 ]
            serg Sergei Golubchik made changes -
            Workflow MariaDB v3 [ 82358 ] MariaDB v4 [ 152745 ]

            People

              varun Varun Gupta (Inactive)
              costateixeira Jose Costa Teixeira
              Votes:
              0 Vote for this issue
              Watchers:
              5 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.