diff --git a/plugin/trino-mysql/src/main/java/io/trino/plugin/mysql/MySqlClient.java b/plugin/trino-mysql/src/main/java/io/trino/plugin/mysql/MySqlClient.java index 2f14125eed2f..7423209b93b6 100644 --- a/plugin/trino-mysql/src/main/java/io/trino/plugin/mysql/MySqlClient.java +++ b/plugin/trino-mysql/src/main/java/io/trino/plugin/mysql/MySqlClient.java @@ -89,6 +89,7 @@ import java.sql.SQLSyntaxErrorException; import java.sql.Types; import java.time.LocalDate; +import java.time.LocalDateTime; import java.util.AbstractMap.SimpleEntry; import java.util.Collection; import java.util.List; @@ -496,7 +497,7 @@ public boolean isNull(ResultSet resultSet, int columnIndex) throws SQLException { // super calls ResultSet#getObject(), which for TIMESTAMP type returns java.sql.Timestamp, for which the conversion can fail if the value isn't a valid instant in server's time zone. - resultSet.getObject(columnIndex, String.class); + resultSet.getObject(columnIndex, LocalDateTime.class); return resultSet.wasNull(); } diff --git a/plugin/trino-mysql/src/test/java/io/trino/plugin/mysql/TestMySqlTypeMapping.java b/plugin/trino-mysql/src/test/java/io/trino/plugin/mysql/TestMySqlTypeMapping.java index 00a41b0efbac..0f1b41b2b2f5 100644 --- a/plugin/trino-mysql/src/test/java/io/trino/plugin/mysql/TestMySqlTypeMapping.java +++ b/plugin/trino-mysql/src/test/java/io/trino/plugin/mysql/TestMySqlTypeMapping.java @@ -19,6 +19,7 @@ import io.trino.plugin.jdbc.UnsupportedTypeHandling; import io.trino.spi.type.TimeZoneKey; import io.trino.testing.AbstractTestQueryFramework; +import io.trino.testing.DistributedQueryRunner; import io.trino.testing.QueryRunner; import io.trino.testing.TestingSession; import io.trino.testing.datatype.CreateAndInsertDataSetup; @@ -34,8 +35,12 @@ import org.testng.annotations.Test; import java.math.RoundingMode; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.Statement; import java.time.LocalDate; import java.time.ZoneId; +import java.util.Map; import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Verify.verify; @@ -68,6 +73,7 @@ import static java.math.RoundingMode.UNNECESSARY; import static java.time.ZoneOffset.UTC; import static java.util.Arrays.asList; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; public class TestMySqlTypeMapping @@ -930,6 +936,34 @@ public Object[][] sessionZonesDataProvider() }; } + @Test + public void testZeroTimestamp() + throws Exception + { + String connectionUrl = mySqlServer.getJdbcUrl() + "&zeroDateTimeBehavior=convertToNull"; + + DistributedQueryRunner queryRunner = DistributedQueryRunner.builder(getSession()).build(); + queryRunner.installPlugin(new MySqlPlugin()); + Map properties = ImmutableMap.builder() + .put("connection-url", connectionUrl) + .put("connection-user", mySqlServer.getUsername()) + .put("connection-password", mySqlServer.getPassword()) + .buildOrThrow(); + queryRunner.createCatalog("mysql", "mysql", properties); + + try (Connection connection = DriverManager.getConnection(connectionUrl, mySqlServer.getUsername(), mySqlServer.getPassword()); + Statement statement = connection.createStatement()) { + statement.execute("CREATE TABLE tpch.test_zero_ts(col_dt datetime, col_ts timestamp)"); + statement.execute("SET sql_mode=''"); + statement.execute("INSERT INTO tpch.test_zero_ts(col_dt, col_ts) VALUES ('0000-00-00 00:00:00', '0000-00-00 00:00:00')"); + + assertThat(queryRunner.execute("SELECT col_dt FROM test_zero_ts").getOnlyValue()).isNull(); + assertThat(queryRunner.execute("SELECT col_ts FROM test_zero_ts").getOnlyValue()).isNull(); + + statement.execute("DROP TABLE tpch.test_zero_ts"); + } + } + @Test public void testJson() {