Skip to content

Commit

Permalink
Return NULL when zero date with convertToNull in Mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
guyco33 authored and ebyhr committed Feb 10, 2023
1 parent e589152 commit b4d3931
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<String, String> properties = ImmutableMap.<String, String>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()
{
Expand Down

0 comments on commit b4d3931

Please sign in to comment.