|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package io.trino.tests; |
| 15 | + |
| 16 | +import io.trino.Session; |
| 17 | +import io.trino.plugin.memory.MemoryPlugin; |
| 18 | +import io.trino.testing.AbstractTestQueryFramework; |
| 19 | +import io.trino.testing.QueryRunner; |
| 20 | +import io.trino.tests.tpch.TpchQueryRunner; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import static io.trino.testing.TestingNames.randomNameSuffix; |
| 24 | +import static io.trino.testing.TestingSession.testSessionBuilder; |
| 25 | +import static java.lang.String.format; |
| 26 | + |
| 27 | +public class TestJoinIsNotDistinct |
| 28 | + extends AbstractTestQueryFramework |
| 29 | +{ |
| 30 | + private final Session memorySession = testSessionBuilder() |
| 31 | + .setCatalog("memory") |
| 32 | + .setSchema("default") |
| 33 | + .build(); |
| 34 | + |
| 35 | + @Override |
| 36 | + protected QueryRunner createQueryRunner() |
| 37 | + throws Exception |
| 38 | + { |
| 39 | + QueryRunner queryRunner = TpchQueryRunner.builder().build(); |
| 40 | + |
| 41 | + queryRunner.installPlugin(new MemoryPlugin()); |
| 42 | + queryRunner.createCatalog("memory", "memory"); |
| 43 | + |
| 44 | + return queryRunner; |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testJoinWithIsNotDistinctFromOnNulls() |
| 49 | + { |
| 50 | + String tableName1 = "test_tab_" + randomNameSuffix(); |
| 51 | + getQueryRunner().execute(memorySession, format("CREATE TABLE %s (k1 INT, k2 INT)", tableName1)); |
| 52 | + getQueryRunner().execute(memorySession, format("INSERT INTO %s VALUES (1, NULL)", tableName1)); |
| 53 | + |
| 54 | + String tableName2 = "test_tab_" + randomNameSuffix(); |
| 55 | + getQueryRunner().execute(memorySession, format("CREATE TABLE %s (k1 INT, k2 INT)", tableName2)); |
| 56 | + getQueryRunner().execute(memorySession, format("INSERT INTO %s VALUES (1, NULL)", tableName2)); |
| 57 | + |
| 58 | + assertQuery( |
| 59 | + memorySession, |
| 60 | + format("SELECT *" + |
| 61 | + " FROM %s t" + |
| 62 | + " INNER JOIN %s AS s" + |
| 63 | + " ON s.k1 IS NOT DISTINCT FROM t.k1" + |
| 64 | + " AND s.k2 IS NOT DISTINCT FROM t.k2", tableName1, tableName2), |
| 65 | + "VALUES (1, NULL, 1, NULL)"); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testJoinWithIsNotDistinctFromOnNulls2() |
| 70 | + { |
| 71 | + String tableName1 = "test_tab_" + randomNameSuffix(); |
| 72 | + getQueryRunner().execute(memorySession, format("CREATE TABLE %s (k1 INT, k2 INT)", tableName1)); |
| 73 | + getQueryRunner().execute(memorySession, format("INSERT INTO %s VALUES (1, NULL)", tableName1)); |
| 74 | + getQueryRunner().execute(memorySession, format("INSERT INTO %s VALUES (NULL, 2)", tableName1)); |
| 75 | + |
| 76 | + String tableName2 = "test_tab_" + randomNameSuffix(); |
| 77 | + getQueryRunner().execute(memorySession, format("CREATE TABLE %s (k1 INT, k2 INT)", tableName2)); |
| 78 | + getQueryRunner().execute(memorySession, format("INSERT INTO %s VALUES (1, NULL)", tableName2)); |
| 79 | + getQueryRunner().execute(memorySession, format("INSERT INTO %s VALUES (3, NULL)", tableName2)); |
| 80 | + |
| 81 | + assertQuery( |
| 82 | + memorySession, |
| 83 | + format("SELECT *" + |
| 84 | + " FROM %s t" + |
| 85 | + " INNER JOIN %s AS s" + |
| 86 | + " ON s.k1 IS NOT DISTINCT FROM t.k1" + |
| 87 | + " AND s.k2 IS NOT DISTINCT FROM t.k2", tableName1, tableName2), |
| 88 | + "VALUES (1, NULL, 1, NULL)"); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testJoinWithIsNotDistinctFromOnNulls3() |
| 93 | + { |
| 94 | + String tableName1 = "test_tab_" + randomNameSuffix(); |
| 95 | + getQueryRunner().execute(memorySession, format("CREATE TABLE %s (k1 INT, k2 INT)", tableName1)); |
| 96 | + getQueryRunner().execute(memorySession, format("INSERT INTO %s VALUES (1, NULL)", tableName1)); |
| 97 | + getQueryRunner().execute(memorySession, format("INSERT INTO %s VALUES (NULL, 2)", tableName1)); |
| 98 | + getQueryRunner().execute(memorySession, format("INSERT INTO %s VALUES (NULL, NULL)", tableName1)); |
| 99 | + |
| 100 | + String tableName2 = "test_tab_" + randomNameSuffix(); |
| 101 | + getQueryRunner().execute(memorySession, format("CREATE TABLE %s (k1 INT, k2 INT)", tableName2)); |
| 102 | + getQueryRunner().execute(memorySession, format("INSERT INTO %s VALUES (1, NULL)", tableName2)); |
| 103 | + getQueryRunner().execute(memorySession, format("INSERT INTO %s VALUES (3, NULL)", tableName2)); |
| 104 | + getQueryRunner().execute(memorySession, format("INSERT INTO %s VALUES (NULL, NULL)", tableName2)); |
| 105 | + |
| 106 | + assertQuery( |
| 107 | + memorySession, |
| 108 | + format("SELECT *" + |
| 109 | + " FROM %s t" + |
| 110 | + " INNER JOIN %s AS s" + |
| 111 | + " ON s.k1 IS NOT DISTINCT FROM t.k1" + |
| 112 | + " AND s.k2 IS NOT DISTINCT FROM t.k2", tableName1, tableName2), |
| 113 | + "VALUES (1, NULL, 1, NULL), (NULL, NULL, NULL, NULL)"); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testJoinWithIsNotDistinctFromOnNulls4() |
| 118 | + { |
| 119 | + String tableName1 = "test_tab_" + randomNameSuffix(); |
| 120 | + getQueryRunner().execute(memorySession, format("CREATE TABLE %s (k1 INT, k2 INT)", tableName1)); |
| 121 | + getQueryRunner().execute(memorySession, format("INSERT INTO %s VALUES (1, NULL)", tableName1)); |
| 122 | + getQueryRunner().execute(memorySession, format("INSERT INTO %s VALUES (NULL, 2)", tableName1)); |
| 123 | + getQueryRunner().execute(memorySession, format("INSERT INTO %s VALUES (NULL, NULL)", tableName1)); |
| 124 | + getQueryRunner().execute(memorySession, format("INSERT INTO %s VALUES (2, 2)", tableName1)); |
| 125 | + |
| 126 | + String tableName2 = "test_tab_" + randomNameSuffix(); |
| 127 | + getQueryRunner().execute(memorySession, format("CREATE TABLE %s (k1 INT, k2 INT)", tableName2)); |
| 128 | + getQueryRunner().execute(memorySession, format("INSERT INTO %s VALUES (1, NULL)", tableName2)); |
| 129 | + getQueryRunner().execute(memorySession, format("INSERT INTO %s VALUES (3, NULL)", tableName2)); |
| 130 | + getQueryRunner().execute(memorySession, format("INSERT INTO %s VALUES (NULL, NULL)", tableName2)); |
| 131 | + getQueryRunner().execute(memorySession, format("INSERT INTO %s VALUES (2, 2)", tableName2)); |
| 132 | + |
| 133 | + assertQuery( |
| 134 | + memorySession, |
| 135 | + format("SELECT *" + |
| 136 | + " FROM %s t" + |
| 137 | + " INNER JOIN %s AS s" + |
| 138 | + " ON s.k1 IS NOT DISTINCT FROM t.k1" + |
| 139 | + " AND s.k2 IS NOT DISTINCT FROM t.k2", tableName1, tableName2), |
| 140 | + "VALUES (1, NULL, 1, NULL), (NULL, NULL, NULL, NULL), (2, 2, 2, 2)"); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void testJoinWithIsNotDistinctFromOnNullsInMemory() |
| 145 | + { |
| 146 | + assertQuery( |
| 147 | + memorySession, |
| 148 | + "SELECT *" + |
| 149 | + " FROM (SELECT 1 AS k1, NULL AS k2) t" + |
| 150 | + " INNER JOIN (SELECT 1 AS k1, NULL AS k2) AS s" + |
| 151 | + " ON s.k1 IS NOT DISTINCT FROM t.k1" + |
| 152 | + " AND s.k2 IS NOT DISTINCT FROM t.k2", |
| 153 | + "VALUES (1, NULL, 1, NULL)"); |
| 154 | + } |
| 155 | +} |
0 commit comments