Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return NULL when zero date with convertToNull in Mysql #16028

Merged
merged 1 commit into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the above comment still valid?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ResultSet#getObject() also solves this issue, but since it returns java.sql.Timestamp for both Mysql datetime and timestamp I prefer to set the exact type used by the StandardColumnMappings#timestampReadFunction and not to fail with any other corner case values.

I think that if we keep the override of isNull() than the comment is not relevant anyway.

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");
}
}
ebyhr marked this conversation as resolved.
Show resolved Hide resolved

@Test
public void testJson()
{
Expand Down