Skip to content

Commit

Permalink
Bump mysql-connector-java to 8.0.22
Browse files Browse the repository at this point in the history
  • Loading branch information
guyco33 authored and findepi committed Jun 21, 2021
1 parent 2602248 commit 01670f3
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package io.trino.plugin.mysql;

import com.google.common.collect.ImmutableSet;
import com.mysql.jdbc.Statement;
import com.mysql.cj.jdbc.JdbcStatement;
import io.trino.plugin.jdbc.BaseJdbcClient;
import io.trino.plugin.jdbc.BaseJdbcConfig;
import io.trino.plugin.jdbc.ColumnMapping;
Expand All @@ -25,6 +25,7 @@
import io.trino.plugin.jdbc.JdbcSortItem;
import io.trino.plugin.jdbc.JdbcTableHandle;
import io.trino.plugin.jdbc.JdbcTypeHandle;
import io.trino.plugin.jdbc.LongReadFunction;
import io.trino.plugin.jdbc.PreparedQuery;
import io.trino.plugin.jdbc.WriteMapping;
import io.trino.plugin.jdbc.expression.AggregateFunctionRewriter;
Expand Down Expand Up @@ -66,6 +67,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Map;
Expand All @@ -75,8 +77,8 @@

import static com.google.common.base.Verify.verify;
import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
import static com.mysql.jdbc.SQLError.SQL_STATE_ER_TABLE_EXISTS_ERROR;
import static com.mysql.jdbc.SQLError.SQL_STATE_SYNTAX_ERROR;
import static com.mysql.cj.exceptions.MysqlErrorNumbers.SQL_STATE_ER_TABLE_EXISTS_ERROR;
import static com.mysql.cj.exceptions.MysqlErrorNumbers.SQL_STATE_SYNTAX_ERROR;
import static io.airlift.slice.Slices.utf8Slice;
import static io.trino.plugin.base.util.JsonTypeUtil.jsonParse;
import static io.trino.plugin.jdbc.DecimalConfig.DecimalMapping.ALLOW_OVERFLOW;
Expand Down Expand Up @@ -104,7 +106,7 @@
import static io.trino.plugin.jdbc.StandardColumnMappings.shortDecimalWriteFunction;
import static io.trino.plugin.jdbc.StandardColumnMappings.smallintColumnMapping;
import static io.trino.plugin.jdbc.StandardColumnMappings.smallintWriteFunction;
import static io.trino.plugin.jdbc.StandardColumnMappings.timestampWriteFunctionUsingSqlTimestamp;
import static io.trino.plugin.jdbc.StandardColumnMappings.timestampWriteFunction;
import static io.trino.plugin.jdbc.StandardColumnMappings.tinyintColumnMapping;
import static io.trino.plugin.jdbc.StandardColumnMappings.tinyintWriteFunction;
import static io.trino.plugin.jdbc.StandardColumnMappings.varbinaryReadFunction;
Expand All @@ -122,11 +124,15 @@
import static io.trino.spi.type.TimeWithTimeZoneType.TIME_WITH_TIME_ZONE;
import static io.trino.spi.type.TimestampType.TIMESTAMP_MILLIS;
import static io.trino.spi.type.TimestampWithTimeZoneType.TIMESTAMP_TZ_MILLIS;
import static io.trino.spi.type.Timestamps.MICROSECONDS_PER_MILLISECOND;
import static io.trino.spi.type.Timestamps.MICROSECONDS_PER_SECOND;
import static io.trino.spi.type.Timestamps.NANOSECONDS_PER_MILLISECOND;
import static io.trino.spi.type.TinyintType.TINYINT;
import static io.trino.spi.type.VarbinaryType.VARBINARY;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.String.format;
import static java.time.ZoneOffset.UTC;
import static java.util.stream.Collectors.joining;

public class MySqlClient
Expand Down Expand Up @@ -215,8 +221,8 @@ public PreparedStatement getPreparedStatement(Connection connection, String sql)
throws SQLException
{
PreparedStatement statement = connection.prepareStatement(sql);
if (statement.isWrapperFor(Statement.class)) {
statement.unwrap(Statement.class).enableStreamingResults();
if (statement.isWrapperFor(JdbcStatement.class)) {
statement.unwrap(JdbcStatement.class).enableStreamingResults();
}
return statement;
}
Expand Down Expand Up @@ -314,13 +320,30 @@ public Optional<ColumnMapping> toColumnMapping(ConnectorSession session, Connect

case Types.TIMESTAMP:
// TODO support higher precisions (https://github.com/trinodb/trino/issues/6910)
break; // currently handled by the default mappings
return Optional.of(ColumnMapping.longMapping(
TIMESTAMP_MILLIS,
timestampMillisReadFunction(),
timestampWriteFunction(TIMESTAMP_MILLIS),
DISABLE_PUSHDOWN));
}

// TODO add explicit mappings
return legacyColumnMapping(session, connection, typeHandle);
}

// TODO support higher precisions (https://github.com/trinodb/trino/issues/6910)
private static LongReadFunction timestampMillisReadFunction()
{
return (resultSet, columnIndex) -> toTrinoTimestampMillis(resultSet.getObject(columnIndex, LocalDateTime.class));
}

private static long toTrinoTimestampMillis(LocalDateTime localDateTime)
{
return localDateTime.toEpochSecond(UTC) * MICROSECONDS_PER_SECOND
// rounding to millis, TODO support higher precisions (https://github.com/trinodb/trino/issues/6910)
+ Math.round(localDateTime.getNano() * 1.0 / NANOSECONDS_PER_MILLISECOND) * MICROSECONDS_PER_MILLISECOND;
}

@Override
public WriteMapping toWriteMapping(ConnectorSession session, Type type)
{
Expand Down Expand Up @@ -361,7 +384,7 @@ public WriteMapping toWriteMapping(ConnectorSession session, Type type)
}
if (TIMESTAMP_MILLIS.equals(type)) {
// TODO use `timestampWriteFunction` (https://github.com/trinodb/trino/issues/6910)
return WriteMapping.longMapping("datetime(3)", timestampWriteFunctionUsingSqlTimestamp(TIMESTAMP_MILLIS));
return WriteMapping.longMapping("datetime(3)", timestampWriteFunction(TIMESTAMP_MILLIS));
}
if (VARBINARY.equals(type)) {
return WriteMapping.sliceMapping("mediumblob", varbinaryWriteFunction());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public static ConnectionFactory createConnectionFactory(BaseJdbcConfig config, C
{
Properties connectionProperties = new Properties();
connectionProperties.setProperty("useInformationSchema", Boolean.toString(mySqlConfig.isDriverUseInformationSchema()));
connectionProperties.setProperty("nullCatalogMeansCurrent", "false");
connectionProperties.setProperty("useUnicode", "true");
connectionProperties.setProperty("characterEncoding", "utf8");
connectionProperties.setProperty("tinyInt1isBit", "false");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
*/
package io.trino.plugin.mysql;

import com.mysql.cj.conf.ConnectionUrlParser;
import com.mysql.cj.exceptions.CJException;
import com.mysql.jdbc.Driver;
import io.trino.plugin.jdbc.BaseJdbcConfig;

import javax.validation.constraints.AssertTrue;

import java.sql.SQLException;
import java.util.Properties;

import static com.google.common.base.Strings.isNullOrEmpty;
import static com.mysql.cj.conf.ConnectionUrlParser.parseConnectionString;

public class MySqlJdbcConfig
extends BaseJdbcConfig
Expand All @@ -29,8 +33,7 @@ public boolean isUrlValid()
{
try {
Driver driver = new Driver();
Properties properties = driver.parseURL(getConnectionUrl(), null);
return properties != null;
return driver.acceptsURL(getConnectionUrl());
}
catch (SQLException e) {
throw new RuntimeException(e);
Expand All @@ -41,12 +44,11 @@ public boolean isUrlValid()
public boolean isUrlWithoutDatabase()
{
try {
Driver driver = new Driver();
Properties properties = driver.parseURL(getConnectionUrl(), null);
return (properties == null) || (driver.database(properties) == null);
ConnectionUrlParser parser = parseConnectionString(getConnectionUrl());
return isNullOrEmpty(parser.getPath());
}
catch (SQLException e) {
throw new RuntimeException(e);
catch (CJException ignore) {
return false;
}
}
}
Loading

0 comments on commit 01670f3

Please sign in to comment.