From 30355b9d7edb9c65112b89184fa0a2b80ac5b3bd Mon Sep 17 00:00:00 2001 From: Yuya Ebihara Date: Tue, 8 Feb 2022 14:30:55 +0900 Subject: [PATCH] Add test cases for negative dates to BaseConnectorTest --- .../accumulo/TestAccumuloConnectorTest.java | 8 ++++++ .../TestClickHouseConnectorTest.java | 22 ++++++++++++++++ .../plugin/druid/BaseDruidConnectorTest.java | 8 ++++++ .../memsql/TestMemSqlConnectorTest.java | 9 +++++++ .../plugin/mysql/BaseMySqlConnectorTest.java | 1 + .../sqlserver/BaseSqlServerConnectorTest.java | 10 ++------ .../io/trino/testing/BaseConnectorTest.java | 25 +++++++++++++++++++ .../testing/TestingConnectorBehavior.java | 1 + 8 files changed, 76 insertions(+), 8 deletions(-) diff --git a/plugin/trino-accumulo/src/test/java/io/trino/plugin/accumulo/TestAccumuloConnectorTest.java b/plugin/trino-accumulo/src/test/java/io/trino/plugin/accumulo/TestAccumuloConnectorTest.java index cb84cf9332c9..6abf509e0961 100644 --- a/plugin/trino-accumulo/src/test/java/io/trino/plugin/accumulo/TestAccumuloConnectorTest.java +++ b/plugin/trino-accumulo/src/test/java/io/trino/plugin/accumulo/TestAccumuloConnectorTest.java @@ -193,6 +193,14 @@ public void testInsertDuplicateRows() } } + @Test + @Override + public void testInsertNegativeDate() + { + // TODO (https://github.com/trinodb/trino/issues/10208) Fix negative date handling. Expected '-2016-12-07', but got '-2016-11-21' + assertThatThrownBy(super::testInsertNegativeDate); + } + @Override public void testShowColumns() { diff --git a/plugin/trino-clickhouse/src/test/java/io/trino/plugin/clickhouse/TestClickHouseConnectorTest.java b/plugin/trino-clickhouse/src/test/java/io/trino/plugin/clickhouse/TestClickHouseConnectorTest.java index d0df3778b8b6..845578755690 100644 --- a/plugin/trino-clickhouse/src/test/java/io/trino/plugin/clickhouse/TestClickHouseConnectorTest.java +++ b/plugin/trino-clickhouse/src/test/java/io/trino/plugin/clickhouse/TestClickHouseConnectorTest.java @@ -82,6 +82,7 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior) return false; case SUPPORTS_ARRAY: + case SUPPORTS_NEGATIVE_DATE: return false; case SUPPORTS_DELETE: @@ -451,6 +452,27 @@ public void testInsertIntoNotNullColumn() } } + @Test + @Override + public void testInsertNegativeDate() + { + // Override because the connector throws a different error message in the super class + try (TestTable table = new TestTable(getQueryRunner()::execute, "insert_date", "(dt DATE)")) { + assertQueryFails(format("INSERT INTO %s VALUES (DATE '-2016-12-07')", table.getName()), "Date must be between 1970-01-01 and 2106-02-07 in ClickHouse: -2016-12-07"); + } + } + + @Test + @Override + public void testDateYearOfEraPredicate() + { + // Override because the connector throws an exception instead of an empty result when the value is out of supported range + assertQuery("SELECT orderdate FROM orders WHERE orderdate = DATE '1997-09-14'", "VALUES DATE '1997-09-14'"); + assertQueryFails( + "SELECT * FROM orders WHERE orderdate = DATE '-1996-09-14'", + "Date must be between 1970-01-01 and 2106-02-07 in ClickHouse: -1996-09-14"); + } + @Override protected SqlExecutor onRemoteDatabase() { diff --git a/plugin/trino-druid/src/test/java/io/trino/plugin/druid/BaseDruidConnectorTest.java b/plugin/trino-druid/src/test/java/io/trino/plugin/druid/BaseDruidConnectorTest.java index 8264a2cdd976..7e8113a1b853 100644 --- a/plugin/trino-druid/src/test/java/io/trino/plugin/druid/BaseDruidConnectorTest.java +++ b/plugin/trino-druid/src/test/java/io/trino/plugin/druid/BaseDruidConnectorTest.java @@ -27,6 +27,7 @@ import io.trino.testing.assertions.Assert; import io.trino.testing.sql.SqlExecutor; import org.intellij.lang.annotations.Language; +import org.testng.SkipException; import org.testng.annotations.AfterClass; import org.testng.annotations.Test; @@ -275,4 +276,11 @@ public void testLimitPushDown() "LIMIT 30")) .isNotFullyPushedDown(joinOverTableScans); } + + @Test + @Override + public void testDateYearOfEraPredicate() + { + throw new SkipException("Druid connector does not map 'orderdate' column to date type"); + } } diff --git a/plugin/trino-memsql/src/test/java/io/trino/plugin/memsql/TestMemSqlConnectorTest.java b/plugin/trino-memsql/src/test/java/io/trino/plugin/memsql/TestMemSqlConnectorTest.java index 21198c4a469b..118cbed83763 100644 --- a/plugin/trino-memsql/src/test/java/io/trino/plugin/memsql/TestMemSqlConnectorTest.java +++ b/plugin/trino-memsql/src/test/java/io/trino/plugin/memsql/TestMemSqlConnectorTest.java @@ -38,6 +38,7 @@ import static java.util.stream.Collectors.joining; import static java.util.stream.IntStream.range; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertTrue; @@ -298,6 +299,14 @@ public void testPredicatePushdown() .isNotFullyPushedDown(AggregationNode.class); } + @Test + @Override + public void testInsertNegativeDate() + { + // TODO (https://github.com/trinodb/trino/issues/10208) MemSQL stores '0000-00-00' when inserted negative dates and it throws an exception during reading the row + assertThatThrownBy(super::testInsertNegativeDate); + } + /** * This test helps to tune TupleDomain simplification threshold. */ diff --git a/plugin/trino-mysql/src/test/java/io/trino/plugin/mysql/BaseMySqlConnectorTest.java b/plugin/trino-mysql/src/test/java/io/trino/plugin/mysql/BaseMySqlConnectorTest.java index 9e2f24617fbd..0ff5a974bcfb 100644 --- a/plugin/trino-mysql/src/test/java/io/trino/plugin/mysql/BaseMySqlConnectorTest.java +++ b/plugin/trino-mysql/src/test/java/io/trino/plugin/mysql/BaseMySqlConnectorTest.java @@ -62,6 +62,7 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior) return false; case SUPPORTS_ARRAY: + case SUPPORTS_NEGATIVE_DATE: return false; case SUPPORTS_RENAME_SCHEMA: diff --git a/plugin/trino-sqlserver/src/test/java/io/trino/plugin/sqlserver/BaseSqlServerConnectorTest.java b/plugin/trino-sqlserver/src/test/java/io/trino/plugin/sqlserver/BaseSqlServerConnectorTest.java index 53a419d59436..2e38ce48f7bd 100644 --- a/plugin/trino-sqlserver/src/test/java/io/trino/plugin/sqlserver/BaseSqlServerConnectorTest.java +++ b/plugin/trino-sqlserver/src/test/java/io/trino/plugin/sqlserver/BaseSqlServerConnectorTest.java @@ -73,6 +73,7 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior) return false; case SUPPORTS_ARRAY: + case SUPPORTS_NEGATIVE_DATE: return false; case SUPPORTS_RENAME_TABLE_ACROSS_SCHEMAS: @@ -460,6 +461,7 @@ public void testShowCreateForUniqueConstraintCompressedTable() } @Test + @Override public void testDateYearOfEraPredicate() { // SQL Server throws an exception instead of an empty result when the value is out of range @@ -469,14 +471,6 @@ public void testDateYearOfEraPredicate() "Conversion failed when converting date and/or time from character string\\."); } - @Test - public void testInsertNegativeDate() - { - try (TestTable table = new TestTable(getQueryRunner()::execute, "insert_date", "(dt DATE)")) { - assertQueryFails(format("INSERT INTO %s VALUES (DATE '-2016-12-07')", table.getName()), "(?s).*Failed to insert data.*"); - } - } - @Override protected String errorMessageForInsertIntoNotNullColumn(String columnName) { 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 c0365a082481..1154bf716ff4 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 @@ -57,6 +57,7 @@ import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_DROP_COLUMN; import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_INSERT; import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_MULTI_STATEMENT_WRITES; +import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_NEGATIVE_DATE; import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_NOT_NULL_CONSTRAINT; import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_RENAME_COLUMN; import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_RENAME_MATERIALIZED_VIEW; @@ -389,6 +390,14 @@ public void testRangePredicate() "WHERE orderkey BETWEEN 10 AND 50"); } + @Test + public void testDateYearOfEraPredicate() + { + // Verify the predicate of '-1996-09-14' doesn't match '1997-09-14'. Both values return same formatted string when we use 'yyyy-MM-dd' in DateTimeFormatter + assertQuery("SELECT orderdate FROM orders WHERE orderdate = DATE '1997-09-14'", "VALUES DATE '1997-09-14'"); + assertQueryReturnsEmptyResult("SELECT * FROM orders WHERE orderdate = DATE '-1996-09-14'"); + } + @Test public void testPredicateReflectedInExplain() { @@ -1184,6 +1193,22 @@ public void testRenameColumn() super.testRenameColumn(); } + @Test + public void testInsertNegativeDate() + { + skipTestUnless(hasBehavior(SUPPORTS_CREATE_TABLE) && hasBehavior(SUPPORTS_INSERT)); + + try (TestTable table = new TestTable(getQueryRunner()::execute, "insert_date", "(dt DATE)")) { + if (hasBehavior(SUPPORTS_NEGATIVE_DATE)) { + assertUpdate(format("INSERT INTO %s VALUES (DATE '-2016-12-07')", table.getName()), 1); + assertQuery("SELECT * FROM " + table.getName(), "VALUES DATE '-2016-12-07'"); + } + else { + assertQueryFails(format("INSERT INTO %s VALUES (DATE '-2016-12-07')", table.getName()), "(?s).*Failed to insert data.*"); + } + } + } + @Test public void testInsertIntoNotNullColumn() { diff --git a/testing/trino-testing/src/main/java/io/trino/testing/TestingConnectorBehavior.java b/testing/trino-testing/src/main/java/io/trino/testing/TestingConnectorBehavior.java index 49c4eebba4f4..4faa1f516d03 100644 --- a/testing/trino-testing/src/main/java/io/trino/testing/TestingConnectorBehavior.java +++ b/testing/trino-testing/src/main/java/io/trino/testing/TestingConnectorBehavior.java @@ -78,6 +78,7 @@ public enum TestingConnectorBehavior SUPPORTS_TRUNCATE(false), SUPPORTS_ARRAY, + SUPPORTS_NEGATIVE_DATE, SUPPORTS_CANCELLATION(false),