|
| 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 com.google.common.collect.ImmutableMap; |
| 17 | +import io.trino.plugin.memory.MemoryPlugin; |
| 18 | +import io.trino.sql.query.QueryAssertions; |
| 19 | +import io.trino.testing.QueryRunner; |
| 20 | +import io.trino.testing.StandaloneQueryRunner; |
| 21 | +import org.junit.jupiter.api.AfterAll; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.api.TestInstance; |
| 24 | +import org.junit.jupiter.api.parallel.Execution; |
| 25 | +import org.junit.jupiter.api.parallel.ExecutionMode; |
| 26 | + |
| 27 | +import static io.trino.testing.TestingNames.randomNameSuffix; |
| 28 | +import static io.trino.testing.TestingSession.testSessionBuilder; |
| 29 | +import static java.lang.String.format; |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | +import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; |
| 32 | + |
| 33 | +@TestInstance(PER_CLASS) |
| 34 | +@Execution(ExecutionMode.SAME_THREAD) |
| 35 | +public class TestJoinIsNotDistinct |
| 36 | +{ |
| 37 | + private static final String LOCAL_CATALOG = "local"; |
| 38 | + private static final String DEFAULT_SCHEMA = "default"; |
| 39 | + |
| 40 | + private final QueryRunner queryRunner; |
| 41 | + |
| 42 | + private final QueryAssertions assertions; |
| 43 | + |
| 44 | + public TestJoinIsNotDistinct() |
| 45 | + { |
| 46 | + queryRunner = new StandaloneQueryRunner(testSessionBuilder() |
| 47 | + .setCatalog(LOCAL_CATALOG) |
| 48 | + .setSchema(DEFAULT_SCHEMA) |
| 49 | + .build()); |
| 50 | + queryRunner.installPlugin(new MemoryPlugin()); |
| 51 | + queryRunner.createCatalog(LOCAL_CATALOG, "memory", ImmutableMap.of()); |
| 52 | + |
| 53 | + assertions = new QueryAssertions(queryRunner); |
| 54 | + } |
| 55 | + |
| 56 | + @AfterAll |
| 57 | + public void teardown() |
| 58 | + { |
| 59 | + assertions.close(); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testJoinWithIsNotDistinctFromOnNulls() |
| 64 | + { |
| 65 | + String tableName1 = "test_tab_" + randomNameSuffix(); |
| 66 | + String tableName2 = "test_tab_" + randomNameSuffix(); |
| 67 | + queryRunner.execute(format("CREATE TABLE %s (k1 INT, k2 INT)", tableName1)); |
| 68 | + queryRunner.execute(format("CREATE TABLE %s (k1 INT, k2 INT)", tableName2)); |
| 69 | + |
| 70 | + queryRunner.execute(format("INSERT INTO %s VALUES (1, NULL)", tableName1)); |
| 71 | + queryRunner.execute(format("INSERT INTO %s VALUES (1, NULL)", tableName2)); |
| 72 | + assertThat(assertions.query(format("SELECT *" + |
| 73 | + " FROM %s t" + |
| 74 | + " INNER JOIN %s AS s" + |
| 75 | + " ON s.k1 IS NOT DISTINCT FROM t.k1" + |
| 76 | + " AND s.k2 IS NOT DISTINCT FROM t.k2", tableName1, tableName2))) |
| 77 | + .matches("VALUES (1, CAST(NULL AS INTEGER), 1, CAST(NULL AS INTEGER))"); |
| 78 | + |
| 79 | + queryRunner.execute(format("INSERT INTO %s VALUES (NULL, NULL)", tableName1)); |
| 80 | + queryRunner.execute(format("INSERT INTO %s VALUES (NULL, NULL)", tableName2)); |
| 81 | + assertThat(assertions.query(format("SELECT *" + |
| 82 | + " FROM %s t" + |
| 83 | + " INNER JOIN %s AS s" + |
| 84 | + " ON s.k1 IS NOT DISTINCT FROM t.k1" + |
| 85 | + " AND s.k2 IS NOT DISTINCT FROM t.k2", tableName1, tableName2))) |
| 86 | + .matches("VALUES (1, CAST(NULL AS INTEGER), 1, CAST(NULL AS INTEGER))," + |
| 87 | + " (CAST(NULL AS INTEGER), CAST(NULL AS INTEGER), CAST(NULL AS INTEGER), CAST(NULL AS INTEGER))"); |
| 88 | + |
| 89 | + queryRunner.execute(format("INSERT INTO %s VALUES (NULL, 2)", tableName1)); |
| 90 | + queryRunner.execute(format("INSERT INTO %s VALUES (3, NULL)", tableName2)); |
| 91 | + assertThat(assertions.query(format("SELECT *" + |
| 92 | + " FROM %s t" + |
| 93 | + " INNER JOIN %s AS s" + |
| 94 | + " ON s.k1 IS NOT DISTINCT FROM t.k1" + |
| 95 | + " AND s.k2 IS NOT DISTINCT FROM t.k2", tableName1, tableName2))) |
| 96 | + .matches("VALUES (1, CAST(NULL AS INTEGER), 1, CAST(NULL AS INTEGER))," + |
| 97 | + " (CAST(NULL AS INTEGER), CAST(NULL AS INTEGER), CAST(NULL AS INTEGER), CAST(NULL AS INTEGER))"); |
| 98 | + |
| 99 | + queryRunner.execute(format("INSERT INTO %s VALUES (2, 2)", tableName1)); |
| 100 | + queryRunner.execute(format("INSERT INTO %s VALUES (2, 2)", tableName2)); |
| 101 | + assertThat(assertions.query(format("SELECT *" + |
| 102 | + " FROM %s t" + |
| 103 | + " INNER JOIN %s AS s" + |
| 104 | + " ON s.k1 IS NOT DISTINCT FROM t.k1" + |
| 105 | + " AND s.k2 IS NOT DISTINCT FROM t.k2", tableName1, tableName2))) |
| 106 | + .matches("VALUES (1, CAST(NULL AS INTEGER), 1, CAST(NULL AS INTEGER))," + |
| 107 | + " (CAST(NULL AS INTEGER), CAST(NULL AS INTEGER), CAST(NULL AS INTEGER), CAST(NULL AS INTEGER))," + |
| 108 | + " (2, 2, 2, 2)"); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testJoinWithIsNotDistinctFromOnNullsOnDerivedTables() |
| 113 | + { |
| 114 | + assertThat(assertions.query("SELECT *" + |
| 115 | + " FROM (SELECT 1 AS k1, CAST(NULL AS INTEGER) AS k2) t" + |
| 116 | + " INNER JOIN (SELECT 1 AS k1, CAST(NULL AS INTEGER) AS k2) AS s" + |
| 117 | + " ON s.k1 IS NOT DISTINCT FROM t.k1" + |
| 118 | + " AND s.k2 IS NOT DISTINCT FROM t.k2")) |
| 119 | + .matches("VALUES (1, CAST(NULL AS INTEGER), 1, CAST(NULL AS INTEGER))"); |
| 120 | + |
| 121 | + assertThat(assertions.query("SELECT *" + |
| 122 | + " FROM (SELECT CAST(NULL AS INTEGER) AS k1, CAST(NULL AS INTEGER) AS k2) t" + |
| 123 | + " INNER JOIN (SELECT CAST(NULL AS INTEGER) AS k1, CAST(NULL AS INTEGER) AS k2) AS s" + |
| 124 | + " ON s.k1 IS NOT DISTINCT FROM t.k1" + |
| 125 | + " AND s.k2 IS NOT DISTINCT FROM t.k2")) |
| 126 | + .matches("VALUES (CAST(NULL AS INTEGER), CAST(NULL AS INTEGER), CAST(NULL AS INTEGER), CAST(NULL AS INTEGER))"); |
| 127 | + |
| 128 | + assertThat(assertions.query("SELECT *" + |
| 129 | + " FROM (SELECT CAST(NULL AS INTEGER) AS k1, 2 AS k2) t" + |
| 130 | + " INNER JOIN (SELECT 3 AS k1, CAST(NULL AS INTEGER) AS k2) AS s" + |
| 131 | + " ON s.k1 IS NOT DISTINCT FROM t.k1" + |
| 132 | + " AND s.k2 IS NOT DISTINCT FROM t.k2")) |
| 133 | + .returnsEmptyResult(); |
| 134 | + } |
| 135 | +} |
0 commit comments