diff --git a/core/trino-main/src/test/java/io/trino/sql/query/QueryAssertions.java b/core/trino-main/src/test/java/io/trino/sql/query/QueryAssertions.java index 1d84ac176faf..df402d455f47 100644 --- a/core/trino-main/src/test/java/io/trino/sql/query/QueryAssertions.java +++ b/core/trino-main/src/test/java/io/trino/sql/query/QueryAssertions.java @@ -444,7 +444,7 @@ public QueryAssert isFullyPushedDown() if (!skipResultsCorrectnessCheckForPushdown) { // Compare the results with pushdown disabled, so that explicit matches() call is not needed - verifyResultsWithPushdownDisabled(); + hasCorrectResultsRegardlessOfPushdown(); } return this; } @@ -511,17 +511,19 @@ private QueryAssert hasPlan(PlanMatchPattern expectedPlan, Consumer additi if (!skipResultsCorrectnessCheckForPushdown) { // Compare the results with pushdown disabled, so that explicit matches() call is not needed - verifyResultsWithPushdownDisabled(); + hasCorrectResultsRegardlessOfPushdown(); } return this; } - private void verifyResultsWithPushdownDisabled() + @CanIgnoreReturnValue + public QueryAssert hasCorrectResultsRegardlessOfPushdown() { Session withoutPushdown = Session.builder(session) .setSystemProperty("allow_pushdown_into_connectors", "false") .build(); matches(runner.execute(withoutPushdown, query)); + return this; } } diff --git a/plugin/trino-kudu/src/test/java/io/trino/plugin/kudu/TestKuduConnectorTest.java b/plugin/trino-kudu/src/test/java/io/trino/plugin/kudu/TestKuduConnectorTest.java index a92fad6a8b5e..2d73b6e46c4a 100644 --- a/plugin/trino-kudu/src/test/java/io/trino/plugin/kudu/TestKuduConnectorTest.java +++ b/plugin/trino-kudu/src/test/java/io/trino/plugin/kudu/TestKuduConnectorTest.java @@ -620,6 +620,15 @@ public void testDateYearOfEraPredicate() .hasStackTraceContaining("Cannot apply operator: varchar = date"); } + @Override + public void testVarcharCastToDateInPredicate() + { + assertThatThrownBy(super::testVarcharCastToDateInPredicate) + .hasStackTraceContaining("Table partitioning must be specified using setRangePartitionColumns or addHashPartitions"); + + throw new SkipException("TODO: implement the test for Kudu"); + } + @Test @Override public void testCharVarcharComparison() diff --git a/testing/trino-testing/src/main/java/io/trino/testing/BaseConnectorTest.java b/testing/trino-testing/src/main/java/io/trino/testing/BaseConnectorTest.java index 3bb01b32cfd1..a21dad966315 100644 --- a/testing/trino-testing/src/main/java/io/trino/testing/BaseConnectorTest.java +++ b/testing/trino-testing/src/main/java/io/trino/testing/BaseConnectorTest.java @@ -503,6 +503,49 @@ public void testSortItemsReflectedInExplain() expectedPattern); } + // CAST(a_varchar AS date) = DATE '...' is an interesting condition which may want to be optimized by the engine or a connector + @Test + public void testVarcharCastToDateInPredicate() + { + skipTestUnless(hasBehavior(SUPPORTS_CREATE_TABLE_WITH_DATA)); + + try (TestTable table = new TestTable( + getQueryRunner()::execute, + "varchar_as_date_pred", + "(a varchar)", + List.of( + "'999-09-09'", + "'1005-09-09'", + "'2005-06-06'", "'2005-06-6'", "'2005-6-06'", "'2005-6-6'", "' 2005-06-06'", "'2005-06-06 '", "' +2005-06-06'", "'02005-06-06'", + "'2005-09-06'", "'2005-09-6'", "'2005-9-06'", "'2005-9-6'", "' 2005-09-06'", "'2005-09-06 '", "' +2005-09-06'", "'02005-09-06'", + "'2005-09-09'", "'2005-09-9'", "'2005-9-09'", "'2005-9-9'", "' 2005-09-09'", "'2005-09-09 '", "' +2005-09-09'", "'02005-09-09'", + "'2005-09-10'", "'2005-9-10'", "' 2005-09-10'", "'2005-09-10 '", "' +2005-09-10'", "'02005-09-10'", + "'9999-09-09'", + "'99999-09-09'"))) { + for (String date : List.of("2005-09-06", "2005-09-09", "2005-09-10")) { + for (String operator : List.of("=", "<=", "<", ">", ">=", "!=", "IS DISTINCT FROM", "IS NOT DISTINCT FROM")) { + assertThat(query("SELECT a FROM %s WHERE CAST(a AS date) %s DATE '%s'".formatted(table.getName(), operator, date))) + .hasCorrectResultsRegardlessOfPushdown(); + } + } + } + + try (TestTable table = new TestTable( + getQueryRunner()::execute, + "varchar_as_date_pred", + "(a varchar)", + List.of("'2005-06-bad-date'", "'2005-09-10'"))) { + assertThatThrownBy(() -> query("SELECT a FROM %s WHERE CAST(a AS date) < DATE '2005-09-10'".formatted(table.getName()))) + .hasMessage("Value cannot be cast to date: 2005-06-bad-date"); + // This failure isn't guaranteed. TODO make test more flexible when need arises. + assertThatThrownBy(() -> query("SELECT a FROM %s WHERE CAST(a AS date) = DATE '2005-09-10'".formatted(table.getName()))) + .hasMessage("Value cannot be cast to date: 2005-06-bad-date"); + // This failure isn't guaranteed. TODO make test more flexible when need arises. + assertThatThrownBy(() -> query("SELECT a FROM %s WHERE CAST(a AS date) > DATE '2022-08-10'".formatted(table.getName()))) + .hasMessage("Value cannot be cast to date: 2005-06-bad-date"); + } + } + @Test public void testConcurrentScans() {