From fd42247abf188af0ccc78301f2933fbb5326cd71 Mon Sep 17 00:00:00 2001 From: zhihanz Date: Thu, 5 Sep 2024 20:49:59 +0800 Subject: [PATCH 1/2] feat: support passive auto discovery --- databend-client/pom.xml | 4 +- .../com/databend/client/DatabendClientV1.java | 2 +- .../com/databend/client/DiscoveryNode.java | 3 + .../com/databend/client/TestClientIT.java | 4 +- databend-jdbc/pom.xml | 6 +- .../jdbc/AbstractDatabendResultSet.java | 28 +-- .../databend/jdbc/ConnectionProperties.java | 12 +- .../com/databend/jdbc/ConnectionProperty.java | 76 +++---- .../DatabendClientLoadBalancingPolicy.java | 5 +- .../com/databend/jdbc/DatabendConnection.java | 63 +++--- .../jdbc/DatabendDatabaseMetaData.java | 16 +- .../com/databend/jdbc/DatabendDriver.java | 6 +- .../com/databend/jdbc/DatabendDriverUri.java | 16 +- .../com/databend/jdbc/DatabendNodeRouter.java | 10 +- .../java/com/databend/jdbc/DatabendNodes.java | 66 ++++-- .../jdbc/DatabendParameterMetaData.java | 1 - .../jdbc/DatabendPreparedStatement.java | 34 +-- .../com/databend/jdbc/DatabendResultSet.java | 8 +- .../jdbc/DatabendResultSetMetaData.java | 10 +- .../com/databend/jdbc/DatabendStatement.java | 12 +- .../jdbc/DatabendUnboundQueryResultSet.java | 1 - .../java/com/databend/jdbc/DriverInfo.java | 12 +- .../com/databend/jdbc/JdbcTypeMapping.java | 1 - .../java/com/databend/jdbc/LoggerUtil.java | 9 +- .../databend/jdbc/NonQueryRawStatement.java | 5 +- .../jdbc/NonRegisteringDatabendDriver.java | 12 +- .../java/com/databend/jdbc/ObjectCasts.java | 66 ++---- .../java/com/databend/jdbc/ParamMarker.java | 1 + .../com/databend/jdbc/PresignContext.java | 2 +- .../com/databend/jdbc/QueryRawStatement.java | 10 +- .../java/com/databend/jdbc/RawStatement.java | 10 +- .../databend/jdbc/RawStatementWrapper.java | 8 +- .../databend/jdbc/SetParamRawStatement.java | 10 +- .../databend/jdbc/StatementInfoWrapper.java | 14 +- .../java/com/databend/jdbc/StatementType.java | 1 + .../java/com/databend/jdbc/StatementUtil.java | 15 +- .../jdbc/cloud/DatabendCopyParams.java | 1 - .../jdbc/cloud/DatabendPresignClientV1.java | 13 +- .../databend/jdbc/cloud/DatabendStage.java | 41 ++-- .../jdbc/cloud/ExternalLocationS3.java | 55 ++--- .../examples/DatabendConnectionFactory.java | 8 +- .../jdbc/examples/DatabendConnectionPool.java | 4 +- .../com/databend/jdbc/examples/Examples.java | 11 +- .../com/databend/jdbc/log/DatabendLogger.java | 30 +-- .../java/com/databend/jdbc/log/JDKLogger.java | 208 +++++++++--------- .../com/databend/jdbc/log/SLF4JLogger.java | 162 +++++++------- .../jdbc/parser/BatchInsertUtils.java | 12 +- .../com/databend/jdbc/StatementUtilTest.java | 5 +- .../com/databend/jdbc/TestBasicDriver.java | 2 +- .../java/com/databend/jdbc/TestCopyInto.java | 6 +- .../jdbc/TestDatabendDatabaseMetaData.java | 15 +- .../databend/jdbc/TestDatabendDriverUri.java | 4 +- .../jdbc/TestDatabendParameterMetaData.java | 1 - .../com/databend/jdbc/TestFileTransfer.java | 15 +- .../java/com/databend/jdbc/TestMultiHost.java | 110 +++++++-- .../databend/jdbc/TestPrepareStatement.java | 9 - .../com/databend/jdbc/TestPresignContext.java | 13 +- .../databend/jdbc/TestStageAttachment.java | 9 +- .../jdbc/cloud/TestDatabendCopyParams.java | 4 +- .../jdbc/cloud/TestDatabendStage.java | 9 +- pom.xml | 2 +- 61 files changed, 603 insertions(+), 705 deletions(-) diff --git a/databend-client/pom.xml b/databend-client/pom.xml index 6119dfff..bd17f322 100644 --- a/databend-client/pom.xml +++ b/databend-client/pom.xml @@ -6,12 +6,12 @@ com.databend databend-base - 0.3.0 + 0.3.1 ../pom.xml com.databend databend-client - 0.3.0 + 0.3.1 diff --git a/databend-client/src/main/java/com/databend/client/DatabendClientV1.java b/databend-client/src/main/java/com/databend/client/DatabendClientV1.java index 6f623b50..ee151767 100644 --- a/databend-client/src/main/java/com/databend/client/DatabendClientV1.java +++ b/databend-client/src/main/java/com/databend/client/DatabendClientV1.java @@ -94,7 +94,7 @@ public DatabendClientV1(OkHttpClient httpClient, String sql, ClientSettings sett } } - public static List dicoverNodes(OkHttpClient httpClient, ClientSettings settings) { + public static List discoverNodes(OkHttpClient httpClient, ClientSettings settings) { requireNonNull(httpClient, "httpClient is null"); requireNonNull(settings, "settings is null"); requireNonNull(settings.getHost(), "settings.host is null"); diff --git a/databend-client/src/main/java/com/databend/client/DiscoveryNode.java b/databend-client/src/main/java/com/databend/client/DiscoveryNode.java index 7a08c524..6689e137 100644 --- a/databend-client/src/main/java/com/databend/client/DiscoveryNode.java +++ b/databend-client/src/main/java/com/databend/client/DiscoveryNode.java @@ -30,6 +30,9 @@ public DiscoveryNode( this.address = address; } + public static DiscoveryNode create(String address) { + return new DiscoveryNode(address); + } // add builder @JsonProperty diff --git a/databend-client/src/test/java/com/databend/client/TestClientIT.java b/databend-client/src/test/java/com/databend/client/TestClientIT.java index 5f356871..25b6df5c 100644 --- a/databend-client/src/test/java/com/databend/client/TestClientIT.java +++ b/databend-client/src/test/java/com/databend/client/TestClientIT.java @@ -102,7 +102,7 @@ public void testDiscoverNodes() { Map additionalHeaders = new HashMap<>(); additionalHeaders.put(X_Databend_Query_ID, expectedUUID); ClientSettings settings = new ClientSettings(DATABEND_HOST, DatabendSession.createDefault(), DEFAULT_QUERY_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT, DEFAULT_SOCKET_TIMEOUT, PaginationOptions.defaultPaginationOptions(), additionalHeaders, null, DEFAULT_RETRY_ATTEMPTS); - List nodes = DatabendClientV1.dicoverNodes(client, settings); + List nodes = DatabendClientV1.discoverNodes(client, settings); Assert.assertFalse(nodes.isEmpty()); for (DiscoveryNode node : nodes) { System.out.println(node.getAddress()); @@ -119,7 +119,7 @@ public void testDiscoverNodesUnSupported() { additionalHeaders.put("~mock.unsupported.discovery", "true"); ClientSettings settings = new ClientSettings(DATABEND_HOST, DatabendSession.createDefault(), DEFAULT_QUERY_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT, DEFAULT_SOCKET_TIMEOUT, PaginationOptions.defaultPaginationOptions(), additionalHeaders, null, DEFAULT_RETRY_ATTEMPTS); try { - DatabendClientV1.dicoverNodes(client, settings); + DatabendClientV1.discoverNodes(client, settings); Assert.fail("Expected exception was not thrown"); } catch (Exception e) { System.out.println(e.getMessage()); diff --git a/databend-jdbc/pom.xml b/databend-jdbc/pom.xml index 17d8b8bd..997f98b9 100644 --- a/databend-jdbc/pom.xml +++ b/databend-jdbc/pom.xml @@ -6,12 +6,12 @@ com.databend databend-base - 0.3.0 + 0.3.1 ../pom.xml com.databend databend-jdbc - 0.3.0 + 0.3.1 @@ -24,7 +24,7 @@ com.databend databend-client - 0.3.0 + 0.3.1 com.squareup.okhttp3 diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/AbstractDatabendResultSet.java b/databend-jdbc/src/main/java/com/databend/jdbc/AbstractDatabendResultSet.java index b44272d8..3d53e4fd 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/AbstractDatabendResultSet.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/AbstractDatabendResultSet.java @@ -1,6 +1,5 @@ package com.databend.jdbc; -import com.databend.client.DatabendClient; import com.databend.client.QueryResults; import com.databend.client.QueryRowField; import com.databend.client.data.ColumnTypeHandler; @@ -24,33 +23,12 @@ import java.math.BigDecimal; import java.net.URL; import java.nio.charset.StandardCharsets; -import java.sql.Array; -import java.sql.Blob; -import java.sql.Clob; import java.sql.Date; -import java.sql.NClob; -import java.sql.Ref; -import java.sql.ResultSet; -import java.sql.ResultSetMetaData; -import java.sql.RowId; -import java.sql.SQLException; -import java.sql.SQLFeatureNotSupportedException; -import java.sql.SQLType; -import java.sql.SQLWarning; -import java.sql.SQLXML; -import java.sql.Statement; -import java.sql.Time; -import java.sql.Timestamp; +import java.sql.*; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; -import java.util.Calendar; -import java.util.GregorianCalendar; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.TimeZone; +import java.util.*; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; @@ -365,7 +343,7 @@ private Object column(int index) if (value == null || value.toString().equals("NULL")) { wasNull.set(true); return null; - }else { + } else { wasNull.set(false); } diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/ConnectionProperties.java b/databend-jdbc/src/main/java/com/databend/jdbc/ConnectionProperties.java index a5994a76..4be05383 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/ConnectionProperties.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/ConnectionProperties.java @@ -3,9 +3,7 @@ import com.databend.client.PaginationOptions; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; -import org.checkerframework.checker.units.qual.C; -import java.sql.Connection; import java.util.Map; import java.util.Optional; import java.util.Set; @@ -26,7 +24,7 @@ public final class ConnectionProperties { public static final ConnectionProperty MAX_FAILOVER_RETRY = new MaxFailoverRetry(); public static final ConnectionProperty LOAD_BALANCING_POLICY = new LoadBalancingPolicy(); public static final ConnectionProperty AUTO_DISCOVERY = new AutoDiscovery(); - + public static final ConnectionProperty ENABLE_MOCK = new EnableMock(); public static final ConnectionProperty DATABASE = new Database(); public static final ConnectionProperty ACCESS_TOKEN = new AccessToken(); @@ -158,12 +156,18 @@ public LoadBalancingPolicy() { } } - private static class AutoDiscovery extends AbstractConnectionProperty { + private static class AutoDiscovery extends AbstractConnectionProperty { public AutoDiscovery() { super("auto_discovery", Optional.of("false"), NOT_REQUIRED, ALLOWED, BOOLEAN_CONVERTER); } } + private static class EnableMock extends AbstractConnectionProperty { + public EnableMock() { + super("enable_mock", Optional.of("false"), NOT_REQUIRED, ALLOWED, BOOLEAN_CONVERTER); + } + } + private static class AccessToken extends AbstractConnectionProperty { public AccessToken() { diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/ConnectionProperty.java b/databend-jdbc/src/main/java/com/databend/jdbc/ConnectionProperty.java index 1a45893f..3c2db693 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/ConnectionProperty.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/ConnectionProperty.java @@ -15,8 +15,7 @@ import static java.util.Locale.ENGLISH; import static java.util.Objects.requireNonNull; -interface ConnectionProperty -{ +interface ConnectionProperty { String getKey(); Optional getDefault(); @@ -31,8 +30,7 @@ Optional getValue(Properties properties) throws SQLException; default T getRequiredValue(Properties properties) - throws SQLException - { + throws SQLException { return getValue(properties).orElseThrow(() -> new SQLException(format("Connection property '%s' is required", getKey()))); } @@ -42,8 +40,7 @@ void validate(Properties properties) } abstract class AbstractConnectionProperty - implements ConnectionProperty -{ + implements ConnectionProperty { protected static final Predicate NOT_REQUIRED = properties -> false; protected static final Predicate ALLOWED = properties -> true; protected static final Converter STRING_CONVERTER = value -> value; @@ -70,17 +67,16 @@ abstract class AbstractConnectionProperty private final String[] choices; protected AbstractConnectionProperty(String key, Optional defaultValue, - Predicate isRequired, Predicate isAllowed, Converter converter, String[] choices, String[] aliases) - { + Predicate isRequired, Predicate isAllowed, Converter converter, String[] choices, String[] aliases) { this.key = requireNonNull(key, "key is null"); this.defaultValue = requireNonNull(defaultValue, "defaultValue is null"); this.isRequired = requireNonNull(isRequired, "isRequired is null"); this.isAllowed = requireNonNull(isAllowed, "isAllowed is null"); this.converter = requireNonNull(converter, "converter is null"); if (choices == null || choices.length == 0) { - this.choices = inferChoices(converter);; - } - else { + this.choices = inferChoices(converter); + ; + } else { this.choices = choices; } } @@ -90,8 +86,7 @@ protected AbstractConnectionProperty( Optional defaultValue, Predicate isRequired, Predicate isAllowed, - Converter converter) - { + Converter converter) { this.key = requireNonNull(key, "key is null"); this.defaultValue = requireNonNull(defaultValue, "defaultValue is null"); this.isRequired = requireNonNull(isRequired, "isRequired is null"); @@ -105,8 +100,7 @@ protected AbstractConnectionProperty( String key, Predicate required, Predicate allowed, - Converter converter) - { + Converter converter) { this(key, Optional.empty(), required, allowed, converter); } @@ -115,18 +109,15 @@ protected AbstractConnectionProperty( Predicate required, Predicate allowed, Converter converter, - String[] aliases) - { + String[] aliases) { this(key, Optional.empty(), required, allowed, converter, null, aliases); } - protected static Predicate checkedPredicate(CheckedPredicate predicate) - { + protected static Predicate checkedPredicate(CheckedPredicate predicate) { return t -> { try { return predicate.test(t); - } - catch (SQLException e) { + } catch (SQLException e) { return false; } }; @@ -134,49 +125,43 @@ protected static Predicate checkedPredicate(CheckedPredicate predicate private String[] inferChoices(Converter converter) { String[] choices = null; - Class type = new TypeToken(getClass()) {}.getRawType(); + Class type = new TypeToken(getClass()) { + }.getRawType(); if (type == Boolean.class) { - choices = new String[] {"true", "false"}; - } - else if (Enum.class.isAssignableFrom(type)) { + choices = new String[]{"true", "false"}; + } else if (Enum.class.isAssignableFrom(type)) { choices = Stream.of(type.getEnumConstants()) .map(Object::toString) .toArray(String[]::new); - } - else { + } else { choices = null; } - return choices; + return choices; } @Override - public String getKey() - { + public String getKey() { return key; } @Override - public Optional getDefault() - { + public Optional getDefault() { return defaultValue; } @Override - public boolean isRequired(Properties properties) - { + public boolean isRequired(Properties properties) { return isRequired.test(properties); } @Override - public boolean isAllowed(Properties properties) - { + public boolean isAllowed(Properties properties) { return isAllowed.test(properties); } @Override public Optional getValue(Properties properties) - throws SQLException - { + throws SQLException { String value = properties.getProperty(key); if (value == null) { if (isRequired(properties)) { @@ -187,8 +172,7 @@ public Optional getValue(Properties properties) try { return Optional.of(converter.convert(value)); - } - catch (RuntimeException e) { + } catch (RuntimeException e) { if (value.isEmpty()) { throw new SQLException(format("Connection property '%s' value is empty", key), e); } @@ -197,8 +181,7 @@ public Optional getValue(Properties properties) } @Override - public DriverPropertyInfo getDriverPropertyInfo(Properties mergedProperties) - { + public DriverPropertyInfo getDriverPropertyInfo(Properties mergedProperties) { String currentValue = mergedProperties.getProperty(key); DriverPropertyInfo result = new DriverPropertyInfo(key, currentValue); result.required = isRequired.test(mergedProperties); @@ -208,8 +191,7 @@ public DriverPropertyInfo getDriverPropertyInfo(Properties mergedProperties) @Override public void validate(Properties properties) - throws SQLException - { + throws SQLException { if (properties.containsKey(key) && !isAllowed(properties)) { throw new SQLException(format("Connection property '%s' is not allowed", key)); } @@ -217,13 +199,11 @@ public void validate(Properties properties) getValue(properties); } - interface Converter - { + interface Converter { T convert(String value); } - protected interface CheckedPredicate - { + protected interface CheckedPredicate { boolean test(T t) throws SQLException; } diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendClientLoadBalancingPolicy.java b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendClientLoadBalancingPolicy.java index 1f51b6f6..72feb8a6 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendClientLoadBalancingPolicy.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendClientLoadBalancingPolicy.java @@ -2,7 +2,6 @@ import java.net.URI; import java.util.List; -import java.util.Random; public class DatabendClientLoadBalancingPolicy { static class DisabledPolicy extends DatabendClientLoadBalancingPolicy { @@ -12,6 +11,7 @@ public String toString() { } // do nothing } + static class RandomPolicy extends DatabendClientLoadBalancingPolicy { @Override protected URI pickUri(String query_id, DatabendNodes nodes) { @@ -56,6 +56,7 @@ public String toString() { return "RoundRobin"; } } + /** * Policy that disable load balance and always use the first node. */ @@ -88,6 +89,7 @@ static DatabendClientLoadBalancingPolicy create(String name) { /** * Policy to pick a node based on the least loaded algorithm. + * * @param nodes the list of URIs to choose from * @return the URI to use */ @@ -101,6 +103,7 @@ protected URI pickUri(String query_id, DatabendNodes nodes) { /** * Get int hash value of given query id + * * @param query_id the query id used for choosing load balancing node * @return hash value of the query id */ diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendConnection.java b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendConnection.java index 48469483..7a801fe0 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendConnection.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendConnection.java @@ -12,33 +12,17 @@ import java.io.*; import java.net.ConnectException; import java.net.URI; -import java.net.URISyntaxException; -import java.sql.Array; -import java.sql.Blob; -import java.sql.CallableStatement; -import java.sql.Clob; -import java.sql.Connection; -import java.sql.DatabaseMetaData; -import java.sql.NClob; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLClientInfoException; -import java.sql.SQLException; -import java.sql.SQLFeatureNotSupportedException; -import java.sql.SQLWarning; -import java.sql.SQLXML; -import java.sql.Savepoint; -import java.sql.Statement; -import java.sql.Struct; +import java.sql.*; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Executor; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; -import java.util.logging.*; import java.util.function.Consumer; +import java.util.logging.FileHandler; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.logging.SimpleFormatter; import java.util.zip.GZIPOutputStream; import static com.databend.client.ClientSettings.*; @@ -145,7 +129,6 @@ public static URI parseRouteHint(String routeHint) { } - private static void checkResultSet(int resultSetType, int resultSetConcurrency) throws SQLFeatureNotSupportedException { if (resultSetType != ResultSet.TYPE_FORWARD_ONLY) { @@ -587,6 +570,10 @@ public boolean copyPurge() { return this.driverUri.copyPurge(); } + public boolean isAutoDiscovery() { + return this.autoDiscovery; + } + public String warehouse() { return this.driverUri.getWarehouse(); } @@ -660,6 +647,7 @@ public void accept(DatabendSession session) { /** * Retry executing a query in case of connection errors. fail over mechanism is used to retry the query when connect error occur * It will find next target host based on configured Load balancing Policy. + * * @param sql The SQL statement to execute. * @param attach The stage attachment to use for the query. * @return A DatabendClient instance representing the successful query execution. @@ -670,7 +658,7 @@ DatabendClient startQueryWithFailover(String sql, StageAttachment attach) throws Exception e = null; int times = getMaxFailoverRetries() + 1; - for( int i = 1; i <= times; i++) { + for (int i = 1; i <= times; i++) { if (e != null && !(e.getCause() instanceof ConnectException)) { throw new SQLException("Error start query: " + "SQL: " + sql + " " + e.getMessage() + " cause: " + e.getCause(), e); } @@ -702,9 +690,9 @@ DatabendClient startQueryWithFailover(String sql, StageAttachment attach) throws ClientSettings s = sb.build(); logger.log(Level.FINE, "retry " + i + " times to execute query: " + sql + " on " + s.getHost()); // discover new hosts in need. -// if (this.autoDiscovery) { -// -// } + if (this.autoDiscovery) { + tryAutoDiscovery(httpClient, s); + } return new DatabendClientV1(httpClient, sql, s, this); } catch (RuntimeException e1) { e = e1; @@ -712,7 +700,32 @@ DatabendClient startQueryWithFailover(String sql, StageAttachment attach) throws throw new SQLException("Error executing query: " + "SQL: " + sql + " " + e1.getMessage() + " cause: " + e1.getCause(), e1); } } - throw new SQLException("Failover Retry Error executing query after" + getMaxFailoverRetries() + "failover retry: " + "SQL: " + sql + " " + e.getMessage() + " cause: " + e.getCause(), e); + throw new SQLException("Failover Retry Error executing query after " + getMaxFailoverRetries() + " failover retry: " + "SQL: " + sql + " " + e.getMessage() + " cause: " + e.getCause(), e); + } + + /** + * Try to auto discovery the databend nodes it will log exceptions when auto discovery failed and not affect real query execution + * + * @param client the http client to query on + * @param settings the client settings to use + */ + void tryAutoDiscovery(OkHttpClient client, ClientSettings settings) { + if (this.autoDiscovery) { + if (this.driverUri.enableMock()) { + settings.getAdditionalHeaders().put("~mock.unsupported.discovery", "true"); + } + DatabendNodes nodes = this.driverUri.getNodes(); + if (nodes != null && nodes.needDiscovery()) { + try { + nodes.discoverUris(client, settings); + } catch (UnsupportedOperationException e) { + logger.log(Level.WARNING, "Current Query Node do not support auto discovery, close the functionality: " + e.getMessage()); + this.autoDiscovery = false; + } catch (Exception e) { + logger.log(Level.FINE, "Error auto discovery: " + " cause: " + e.getCause() + " message: " + e.getMessage()); + } + } + } } DatabendClient startQuery(String sql) throws SQLException { diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendDatabaseMetaData.java b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendDatabaseMetaData.java index 06618951..165f2f67 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendDatabaseMetaData.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendDatabaseMetaData.java @@ -3,16 +3,9 @@ import com.databend.client.QueryRowField; import com.databend.client.data.DatabendDataType; import com.databend.client.data.DatabendRawType; -import com.databend.client.data.DatabendTypes; import com.google.common.base.Joiner; -import java.sql.Connection; -import java.sql.DatabaseMetaData; -import java.sql.ResultSet; -import java.sql.RowIdLifetime; -import java.sql.SQLException; -import java.sql.SQLFeatureNotSupportedException; -import java.sql.Statement; +import java.sql.*; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -20,10 +13,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -import static com.databend.jdbc.DriverInfo.DRIVER_NAME; -import static com.databend.jdbc.DriverInfo.DRIVER_VERSION; -import static com.databend.jdbc.DriverInfo.DRIVER_VERSION_MAJOR; -import static com.databend.jdbc.DriverInfo.DRIVER_VERSION_MINOR; +import static com.databend.jdbc.DriverInfo.*; import static java.util.Objects.requireNonNull; public class DatabendDatabaseMetaData implements DatabaseMetaData { @@ -1040,7 +1030,7 @@ public ResultSet getColumns(String catalog, String schemaPattern, String tableNa emptyStringLikeFilter(filters, "table_schema", schemaPattern); if (tableNamePattern != null) { optionalStringLikeFilter(filters, "table_name", tableNamePattern.replace("\\", "")); - }else { + } else { optionalStringLikeFilter(filters, "table_name", null); } optionalStringLikeFilter(filters, "column_name", columnNamePattern); diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendDriver.java b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendDriver.java index 3551a5e8..00030e1d 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendDriver.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendDriver.java @@ -5,13 +5,11 @@ import java.util.logging.Level; import java.util.logging.Logger; -public class DatabendDriver extends NonRegisteringDatabendDriver -{ +public class DatabendDriver extends NonRegisteringDatabendDriver { static { try { DriverManager.registerDriver(new DatabendDriver()); - } - catch (SQLException e) { + } catch (SQLException e) { Logger.getLogger(DatabendDriver.class.getPackage().getName()) .log(Level.SEVERE, "Failed to register driver", e); throw new RuntimeException("Failed to register DatabendDriver", e); diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendDriverUri.java b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendDriverUri.java index 8dc79017..c7dec009 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendDriverUri.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendDriverUri.java @@ -12,9 +12,7 @@ import java.util.concurrent.TimeUnit; import java.util.logging.Logger; -import static com.databend.client.OkHttpUtils.basicAuthInterceptor; -import static com.databend.client.OkHttpUtils.setupInsecureSsl; -import static com.databend.client.OkHttpUtils.tokenAuth; +import static com.databend.client.OkHttpUtils.*; import static com.databend.jdbc.ConnectionProperties.*; import static com.google.common.base.MoreObjects.firstNonNull; import static com.google.common.base.Strings.isNullOrEmpty; @@ -49,6 +47,7 @@ public final class DatabendDriverUri { private final Integer connectionTimeout; private final Integer maxFailoverRetry; private final boolean autoDiscovery; + private final boolean enableMock; private final Integer queryTimeout; private final Integer socketTimeout; private final Integer waitTimeSecs; @@ -65,6 +64,7 @@ private DatabendDriverUri(String url, Properties driverProperties) this.useSecureConnection = SSL.getValue(properties).orElse(false); this.useVerify = USE_VERIFY.getValue(properties).orElse(false); this.debug = DEBUG.getValue(properties).orElse(false); + this.enableMock = ENABLE_MOCK.getValue(properties).orElse(false); this.strNullAsNull = STRNULL_AS_NULL.getValue(properties).orElse(true); this.warehouse = WAREHOUSE.getValue(properties).orElse(""); this.sslmode = SSL_MODE.getValue(properties).orElse("disable"); @@ -109,7 +109,7 @@ private static void initDatabase(URI uri, Map uriProperties) thr uriProperties.put(DATABASE.getKey(), db); } - private static List canonicalizeUris(List uris, boolean isSSLSecured, String sslmode) throws SQLException { + public static List canonicalizeUris(List uris, boolean isSSLSecured, String sslmode) throws SQLException { List finalUris = new ArrayList<>(); for (URI uri : uris) { finalUris.add(canonicalizeUri(uri, isSSLSecured, sslmode)); @@ -224,7 +224,7 @@ private static Map.Entry> parse(String url) try { for (String raw_host : hosts) { String fullUri = (raw_host.startsWith("http://") || raw_host.startsWith("https://")) ? - raw_host : + raw_host : "http://" + raw_host; URI uri = new URI(fullUri); @@ -306,6 +306,7 @@ public DatabendNodes getNodes() { public URI getUri() { return nodes.getUris().get(0); } + public URI getUri(String query_id) { return nodes.pickUri(query_id); } @@ -313,6 +314,7 @@ public URI getUri(String query_id) { public Boolean autoDiscovery() { return autoDiscovery; } + public String getDatabase() { return database; } @@ -341,6 +343,10 @@ public boolean getDebug() { return debug; } + public boolean enableMock() { + return enableMock; + } + public String getSslmode() { return sslmode; } diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendNodeRouter.java b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendNodeRouter.java index 386ac524..dfa31bda 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendNodeRouter.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendNodeRouter.java @@ -7,7 +7,7 @@ import java.util.List; /** - * Node manager manage a list of hosts + * Node manager manage a list of hosts */ public interface DatabendNodeRouter { /** @@ -24,10 +24,8 @@ public interface DatabendNodeRouter { /** * Discover all possible query uris through databend discovery api and update candidate node router list in need - * @return true if update operation executed, false otherwise - * Ref PR: - * https://github.com/datafuselabs/databend-jdbc/pull/264 - * https://github.com/datafuselabs/databend/pull/16353 */ - boolean discoverUris(OkHttpClient client, ClientSettings settings); + void discoverUris(OkHttpClient client, ClientSettings settings) throws UnsupportedOperationException; + + boolean needDiscovery(); } diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendNodes.java b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendNodes.java index 39a7cf07..aba3a498 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendNodes.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendNodes.java @@ -3,19 +3,18 @@ import com.databend.client.ClientSettings; import com.databend.client.DatabendClientV1; import com.databend.client.DiscoveryNode; +import lombok.Setter; import okhttp3.OkHttpClient; import java.net.URI; import java.net.URISyntaxException; +import java.security.InvalidParameterException; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; -import java.util.stream.Collectors; - -import static com.databend.jdbc.ConnectionProperties.SSL; -import static com.databend.jdbc.ConnectionProperties.SSL_MODE; +import java.util.logging.Logger; public class DatabendNodes implements DatabendNodeRouter { @@ -23,6 +22,9 @@ public class DatabendNodes implements DatabendNodeRouter { protected final AtomicInteger index; // keep track of latest discovery scheduled time protected final AtomicReference lastDiscoveryTime = new AtomicReference<>(0L); + private static final Logger logger = Logger.getLogger(DatabendNodes.class.getPackage().getName()); + @Setter + private boolean debug = false; // minimum time between discovery protected long discoveryInterval = 1000 * 60 * 5; protected DatabendClientLoadBalancingPolicy policy; @@ -67,35 +69,49 @@ public DatabendClientLoadBalancingPolicy getPolicy() { } @Override - public boolean discoverUris(OkHttpClient client, ClientSettings settings) { + public void discoverUris(OkHttpClient client, ClientSettings settings) throws UnsupportedOperationException { // do nothing if discovery interval is not reached Long lastDiscoveryTime = this.lastDiscoveryTime.get(); if (System.currentTimeMillis() - lastDiscoveryTime < discoveryInterval) { - return false; + return; } - List current_nodes = query_nodes_uris.get(); + List current_uris = query_nodes_uris.get(); if (!this.lastDiscoveryTime.compareAndSet(lastDiscoveryTime, System.currentTimeMillis())) { - return false; + return; } - - List new_nodes = DatabendClientV1.dicoverNodes(client, settings); - if (!new_nodes.isEmpty()) { - // convert new nodes using lambda - List new_uris = new_nodes.stream().map(node -> URI.create("http://" + node.getAddress())).collect(Collectors.toList()); - updateNodes(new_uris); - return true; + try { + List new_nodes = DatabendClientV1.discoverNodes(client, settings); + if (!new_nodes.isEmpty()) { + // convert new nodes using lambda + List new_uris = this.parseURI(new_nodes); + if (this.query_nodes_uris.compareAndSet(current_uris, new_uris)) { + java.util.logging.Level level = debug ? java.util.logging.Level.INFO : java.util.logging.Level.FINE; + // the log would only show that when truly updated the nodes + logger.log(level, "Automatic Discovery updated nodes: " + new_uris); + } + } + } catch (UnsupportedOperationException e) { + throw e; + } catch (RuntimeException e) { + logger.log(java.util.logging.Level.WARNING, "Error updating nodes: " + e.getMessage()); } - return false; + + } + + @Override + public boolean needDiscovery() { + Long lastDiscoveryTime = this.lastDiscoveryTime.get(); + return System.currentTimeMillis() - lastDiscoveryTime >= discoveryInterval; } - private List parseURI(List nodes) throws SQLException { + public List parseURI(List nodes) throws RuntimeException { String host = null; List uris = new ArrayList<>(); try { for (DiscoveryNode node : nodes) { String raw_host = node.getAddress(); String fullUri = (raw_host.startsWith("http://") || raw_host.startsWith("https://")) ? - raw_host : + raw_host : "http://" + raw_host; URI uri = new URI(fullUri); @@ -106,24 +122,26 @@ private List parseURI(List nodes) throws SQLException { } else if (hostAndPort.length == 1) { host = hostAndPort[0]; } else { - throw new SQLException("Invalid host and port, url: " + uri); + throw new InvalidParameterException("Invalid host and port, url: " + uri); } if (host == null || host.isEmpty()) { - throw new SQLException("Invalid host " + host); + throw new InvalidParameterException("Invalid host " + host); } uris.add(new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uriPath, uriQuery, uriFragment)); } - + return DatabendDriverUri.canonicalizeUris(uris, this.useSecureConnection, this.sslmode); } catch (URISyntaxException e) { - throw new SQLException("Invalid URI", e.getMessage()); + throw new InvalidParameterException("Invalid URI " + e.getMessage()); + } catch (SQLException e) { + throw new RuntimeException("Error parsing URI " + e.getMessage()); } - - return uris; } + public URI pickUri(String query_id) { return policy.pickUri(query_id, this); } + @Override public String toString() { return "DatabendNodes{" + diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendParameterMetaData.java b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendParameterMetaData.java index c1d23199..f7622a62 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendParameterMetaData.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendParameterMetaData.java @@ -5,7 +5,6 @@ import java.sql.Types; import java.util.List; import java.util.Locale; -import java.util.Map; import static com.databend.jdbc.DatabendResultSetMetaData.getTypeClassName; import static java.util.Objects.requireNonNull; diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendPreparedStatement.java b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendPreparedStatement.java index a21d02a4..1dc3cb3f 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendPreparedStatement.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendPreparedStatement.java @@ -15,44 +15,16 @@ import java.math.BigDecimal; import java.net.URL; import java.nio.charset.StandardCharsets; -import java.sql.Array; -import java.sql.Blob; -import java.sql.Clob; import java.sql.Date; -import java.sql.NClob; -import java.sql.ParameterMetaData; -import java.sql.PreparedStatement; -import java.sql.Ref; -import java.sql.ResultSet; -import java.sql.ResultSetMetaData; -import java.sql.RowId; -import java.sql.SQLException; -import java.sql.SQLFeatureNotSupportedException; -import java.sql.SQLXML; -import java.sql.Time; -import java.sql.Timestamp; -import java.sql.Types; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.LocalTime; -import java.time.OffsetTime; -import java.time.OffsetDateTime; +import java.sql.*; +import java.time.*; import java.time.format.DateTimeFormatterBuilder; import java.util.*; -import java.util.concurrent.ConcurrentHashMap; import java.util.function.Consumer; import java.util.logging.Logger; import java.util.stream.Collectors; -import static com.databend.jdbc.ObjectCasts.castToBigDecimal; -import static com.databend.jdbc.ObjectCasts.castToBinary; -import static com.databend.jdbc.ObjectCasts.castToBoolean; -import static com.databend.jdbc.ObjectCasts.castToByte; -import static com.databend.jdbc.ObjectCasts.castToDouble; -import static com.databend.jdbc.ObjectCasts.castToFloat; -import static com.databend.jdbc.ObjectCasts.castToInt; -import static com.databend.jdbc.ObjectCasts.castToLong; -import static com.databend.jdbc.ObjectCasts.castToShort; +import static com.databend.jdbc.ObjectCasts.*; import static com.databend.jdbc.StatementUtil.replaceParameterMarksWithValues; import static java.lang.String.format; import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE; diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendResultSet.java b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendResultSet.java index 523703fc..7f8ad811 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendResultSet.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendResultSet.java @@ -9,18 +9,12 @@ import com.google.common.util.concurrent.ThreadFactoryBuilder; import javax.annotation.concurrent.GuardedBy; - import java.sql.SQLException; import java.sql.Statement; import java.util.Iterator; import java.util.List; import java.util.Optional; -import java.util.concurrent.ArrayBlockingQueue; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Future; -import java.util.concurrent.Semaphore; +import java.util.concurrent.*; import java.util.stream.Stream; import static com.google.common.base.Throwables.throwIfUnchecked; diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendResultSetMetaData.java b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendResultSetMetaData.java index 9dfe335e..aac793c9 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendResultSetMetaData.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendResultSetMetaData.java @@ -1,15 +1,7 @@ package com.databend.jdbc; import java.math.BigDecimal; -import java.sql.Array; -import java.sql.Blob; -import java.sql.Clob; -import java.sql.Date; -import java.sql.ResultSetMetaData; -import java.sql.SQLException; -import java.sql.Time; -import java.sql.Timestamp; -import java.sql.Types; +import java.sql.*; import java.util.List; public class DatabendResultSetMetaData implements ResultSetMetaData { diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendStatement.java b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendStatement.java index 129d8c37..76aeef5f 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendStatement.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendStatement.java @@ -1,14 +1,12 @@ package com.databend.jdbc; -import com.databend.client.*; +import com.databend.client.DatabendClient; +import com.databend.client.DatabendSession; +import com.databend.client.QueryResults; +import com.databend.client.StageAttachment; import com.databend.jdbc.annotation.NotImplemented; -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.SQLFeatureNotSupportedException; -import java.sql.SQLWarning; -import java.sql.Statement; +import java.sql.*; import java.util.List; import java.util.Optional; import java.util.concurrent.atomic.AtomicBoolean; diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendUnboundQueryResultSet.java b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendUnboundQueryResultSet.java index 3eb1a942..306a3dc1 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendUnboundQueryResultSet.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendUnboundQueryResultSet.java @@ -1,6 +1,5 @@ package com.databend.jdbc; -import com.databend.client.DatabendClient; import com.databend.client.QueryRowField; import java.sql.SQLException; diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/DriverInfo.java b/databend-jdbc/src/main/java/com/databend/jdbc/DriverInfo.java index 9bcd26f8..15b1d86b 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/DriverInfo.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/DriverInfo.java @@ -16,14 +16,14 @@ import static com.google.common.io.Resources.getResource; import static java.lang.Integer.parseInt; -final class DriverInfo -{ +final class DriverInfo { static final String DRIVER_NAME; static final String DRIVER_VERSION; static final int DRIVER_VERSION_MAJOR; static final int DRIVER_VERSION_MINOR; - private DriverInfo() {} + private DriverInfo() { + } static { try { @@ -31,8 +31,7 @@ private DriverInfo() {} URL url = getResource(DriverInfo.class, "driver.properties"); try (InputStream in = url.openStream()) { properties.load(in); - } - catch (IOException e) { + } catch (IOException e) { throw new UncheckedIOException(e); } @@ -47,8 +46,7 @@ private DriverInfo() {} DRIVER_VERSION_MAJOR = parseInt(matcher.group(1)); DRIVER_VERSION_MINOR = parseInt(firstNonNull(matcher.group(3), "0")); - } - catch (RuntimeException e) { + } catch (RuntimeException e) { // log message since DriverManager hides initialization exceptions Logger.getLogger(DatabendDriver.class.getPackage().getName()) .log(Level.SEVERE, "Failed to load driver info", e); diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/JdbcTypeMapping.java b/databend-jdbc/src/main/java/com/databend/jdbc/JdbcTypeMapping.java index 09f0f052..82aa5430 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/JdbcTypeMapping.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/JdbcTypeMapping.java @@ -3,7 +3,6 @@ import com.databend.client.data.DatabendDataType; import java.sql.Types; -import java.util.Map; public class JdbcTypeMapping { /** diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/LoggerUtil.java b/databend-jdbc/src/main/java/com/databend/jdbc/LoggerUtil.java index 12725b0c..e67f5e2f 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/LoggerUtil.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/LoggerUtil.java @@ -1,16 +1,15 @@ package com.databend.jdbc; -import java.io.*; -import java.nio.charset.StandardCharsets; -import java.util.stream.Collectors; - import com.databend.jdbc.log.DatabendLogger; import com.databend.jdbc.log.JDKLogger; import com.databend.jdbc.log.SLF4JLogger; - import lombok.CustomLog; import lombok.experimental.UtilityClass; +import java.io.*; +import java.nio.charset.StandardCharsets; +import java.util.stream.Collectors; + @UtilityClass @CustomLog public class LoggerUtil { diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/NonQueryRawStatement.java b/databend-jdbc/src/main/java/com/databend/jdbc/NonQueryRawStatement.java index 41d77e64..e6236740 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/NonQueryRawStatement.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/NonQueryRawStatement.java @@ -1,11 +1,10 @@ package com.databend.jdbc; -import static com.databend.jdbc.StatementType.NON_QUERY; +import lombok.EqualsAndHashCode; import java.util.List; - -import lombok.EqualsAndHashCode; +import static com.databend.jdbc.StatementType.NON_QUERY; /** * A non query statement is a statement that does not return data (such as diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/NonRegisteringDatabendDriver.java b/databend-jdbc/src/main/java/com/databend/jdbc/NonRegisteringDatabendDriver.java index 6fc900df..b74de401 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/NonRegisteringDatabendDriver.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/NonRegisteringDatabendDriver.java @@ -3,20 +3,12 @@ import okhttp3.OkHttpClient; import java.io.Closeable; -import java.io.IOException; -import java.sql.Connection; -import java.sql.Driver; -import java.sql.DriverPropertyInfo; -import java.sql.SQLException; -import java.sql.SQLFeatureNotSupportedException; +import java.sql.*; import java.util.Properties; import java.util.logging.Logger; import static com.databend.client.OkHttpUtils.userAgentInterceptor; -import static com.databend.jdbc.DriverInfo.DRIVER_NAME; -import static com.databend.jdbc.DriverInfo.DRIVER_VERSION; -import static com.databend.jdbc.DriverInfo.DRIVER_VERSION_MAJOR; -import static com.databend.jdbc.DriverInfo.DRIVER_VERSION_MINOR; +import static com.databend.jdbc.DriverInfo.*; public class NonRegisteringDatabendDriver implements Driver, Closeable { private final OkHttpClient httpClient = newHttpClient(); diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/ObjectCasts.java b/databend-jdbc/src/main/java/com/databend/jdbc/ObjectCasts.java index 8069fed2..52ff9f24 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/ObjectCasts.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/ObjectCasts.java @@ -14,13 +14,12 @@ import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.Locale.ENGLISH; -final class ObjectCasts -{ - private ObjectCasts() {} +final class ObjectCasts { + private ObjectCasts() { + } public static boolean castToBoolean(Object x, int targetSqlType) - throws SQLException - { + throws SQLException { if (x instanceof Boolean) { return (Boolean) x; } @@ -39,16 +38,14 @@ public static boolean castToBoolean(Object x, int targetSqlType) } throw new IllegalArgumentException("Invalid boolean value: " + x); } - } - catch (RuntimeException e) { + } catch (RuntimeException e) { throw invalidConversion(x, targetSqlType, e); } throw invalidConversion(x, targetSqlType); } public static byte castToByte(Object x, int targetSqlType) - throws SQLException - { + throws SQLException { if (x instanceof Boolean) { return (byte) (((Boolean) x) ? 1 : 0); } @@ -59,16 +56,14 @@ public static byte castToByte(Object x, int targetSqlType) if (x instanceof String) { return parseByte((String) x); } - } - catch (RuntimeException e) { + } catch (RuntimeException e) { throw invalidConversion(x, targetSqlType, e); } throw invalidConversion(x, targetSqlType); } public static short castToShort(Object x, int targetSqlType) - throws SQLException - { + throws SQLException { if (x instanceof Boolean) { return (short) (((Boolean) x) ? 1 : 0); } @@ -79,16 +74,14 @@ public static short castToShort(Object x, int targetSqlType) if (x instanceof String) { return parseShort((String) x); } - } - catch (RuntimeException e) { + } catch (RuntimeException e) { throw invalidConversion(x, targetSqlType, e); } throw invalidConversion(x, targetSqlType); } public static int castToInt(Object x, int targetSqlType) - throws SQLException - { + throws SQLException { if (x instanceof Boolean) { return (((Boolean) x) ? 1 : 0); } @@ -99,16 +92,14 @@ public static int castToInt(Object x, int targetSqlType) if (x instanceof String) { return parseInt((String) x); } - } - catch (RuntimeException e) { + } catch (RuntimeException e) { throw invalidConversion(x, targetSqlType, e); } throw invalidConversion(x, targetSqlType); } public static long castToLong(Object x, int targetSqlType) - throws SQLException - { + throws SQLException { if (x instanceof Boolean) { return (((Boolean) x) ? 1 : 0); } @@ -119,16 +110,14 @@ public static long castToLong(Object x, int targetSqlType) if (x instanceof String) { return parseLong((String) x); } - } - catch (RuntimeException e) { + } catch (RuntimeException e) { throw invalidConversion(x, targetSqlType, e); } throw invalidConversion(x, targetSqlType); } public static float castToFloat(Object x, int targetSqlType) - throws SQLException - { + throws SQLException { if (x instanceof Boolean) { return (((Boolean) x) ? 1 : 0); } @@ -139,16 +128,14 @@ public static float castToFloat(Object x, int targetSqlType) if (x instanceof String) { return parseFloat((String) x); } - } - catch (RuntimeException e) { + } catch (RuntimeException e) { throw invalidConversion(x, targetSqlType, e); } throw invalidConversion(x, targetSqlType); } public static double castToDouble(Object x, int targetSqlType) - throws SQLException - { + throws SQLException { if (x instanceof Boolean) { return (((Boolean) x) ? 1 : 0); } @@ -159,16 +146,14 @@ public static double castToDouble(Object x, int targetSqlType) if (x instanceof String) { return parseDouble((String) x); } - } - catch (RuntimeException e) { + } catch (RuntimeException e) { throw invalidConversion(x, targetSqlType, e); } throw invalidConversion(x, targetSqlType); } public static BigDecimal castToBigDecimal(Object x, int targetSqlType) - throws SQLException - { + throws SQLException { if (x instanceof Boolean) { return BigDecimal.valueOf(((Boolean) x) ? 1 : 0); } @@ -188,16 +173,14 @@ public static BigDecimal castToBigDecimal(Object x, int targetSqlType) if (x instanceof String) { return new BigDecimal((String) x); } - } - catch (RuntimeException e) { + } catch (RuntimeException e) { throw invalidConversion(x, targetSqlType, e); } throw invalidConversion(x, targetSqlType); } public static byte[] castToBinary(Object x, int targetSqlType) - throws SQLException - { + throws SQLException { if (x instanceof byte[]) { return (byte[]) x; } @@ -207,18 +190,15 @@ public static byte[] castToBinary(Object x, int targetSqlType) throw invalidConversion(x, targetSqlType); } - private static SQLException invalidConversion(Object x, int sqlType) - { + private static SQLException invalidConversion(Object x, int sqlType) { return invalidConversion(x, sqlType, null); } - private static SQLException invalidConversion(Object x, int sqlType, Exception e) - { + private static SQLException invalidConversion(Object x, int sqlType, Exception e) { return new SQLException(format("Cannot convert instance of %s to SQL type %s", x.getClass().getName(), sqlType), e); } - static SQLException invalidConversion(Object x, String toType) - { + static SQLException invalidConversion(Object x, String toType) { return new SQLException(format("Cannot convert instance of %s to %s", x.getClass().getName(), toType)); } } diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/ParamMarker.java b/databend-jdbc/src/main/java/com/databend/jdbc/ParamMarker.java index b1266669..92e707ec 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/ParamMarker.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/ParamMarker.java @@ -1,4 +1,5 @@ package com.databend.jdbc; + import lombok.AllArgsConstructor; import lombok.Value; diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/PresignContext.java b/databend-jdbc/src/main/java/com/databend/jdbc/PresignContext.java index 2a69ff07..406b8c96 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/PresignContext.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/PresignContext.java @@ -40,7 +40,7 @@ public static void dropStageIfExists(DatabendConnection connection, String stage } public static PresignContext getPresignContext(DatabendConnection connection, PresignMethod method, String stageName, String fileName) - throws SQLException { + throws SQLException { requireNonNull(connection, "connection is null"); requireNonNull(method, "method is null"); Statement statement = connection.createStatement(); diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/QueryRawStatement.java b/databend-jdbc/src/main/java/com/databend/jdbc/QueryRawStatement.java index a6b22c4d..aefab2e6 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/QueryRawStatement.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/QueryRawStatement.java @@ -1,15 +1,13 @@ package com.databend.jdbc; -import static com.databend.jdbc.StatementType.QUERY; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import org.apache.commons.lang3.tuple.Pair; import java.util.List; import java.util.Optional; -import org.apache.commons.lang3.tuple.Pair; - - -import lombok.EqualsAndHashCode; -import lombok.Getter; +import static com.databend.jdbc.StatementType.QUERY; /** * A query statement is a statement that returns data (Typically starts with diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/RawStatement.java b/databend-jdbc/src/main/java/com/databend/jdbc/RawStatement.java index 68a68b88..15719c1e 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/RawStatement.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/RawStatement.java @@ -1,15 +1,11 @@ package com.databend.jdbc; -import java.util.List; -import java.util.Optional; - +import lombok.Data; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.Pair; -import com.databend.jdbc.ParamMarker; -import com.databend.jdbc.StatementType; - -import lombok.Data; +import java.util.List; +import java.util.Optional; @Data public abstract class RawStatement { diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/RawStatementWrapper.java b/databend-jdbc/src/main/java/com/databend/jdbc/RawStatementWrapper.java index fdca4c4a..d74d8908 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/RawStatementWrapper.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/RawStatementWrapper.java @@ -1,11 +1,11 @@ package com.databend.jdbc; -import java.util.Collection; -import java.util.List; - -import org.apache.commons.lang3.StringUtils; import lombok.CustomLog; import lombok.Value; +import org.apache.commons.lang3.StringUtils; + +import java.util.Collection; +import java.util.List; @CustomLog @Value diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/SetParamRawStatement.java b/databend-jdbc/src/main/java/com/databend/jdbc/SetParamRawStatement.java index 2e230319..4b72e45a 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/SetParamRawStatement.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/SetParamRawStatement.java @@ -1,12 +1,12 @@ package com.databend.jdbc; -import static com.databend.jdbc.StatementType.PARAM_SETTING; - -import java.util.List; - -import org.apache.commons.lang3.tuple.Pair; import lombok.EqualsAndHashCode; import lombok.Getter; +import org.apache.commons.lang3.tuple.Pair; + +import java.util.List; + +import static com.databend.jdbc.StatementType.PARAM_SETTING; /** * A Set param statement is a special statement that sets a parameter internally diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/StatementInfoWrapper.java b/databend-jdbc/src/main/java/com/databend/jdbc/StatementInfoWrapper.java index 7f88beb4..5814b119 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/StatementInfoWrapper.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/StatementInfoWrapper.java @@ -1,16 +1,14 @@ package com.databend.jdbc; -import static com.databend.jdbc.StatementType.PARAM_SETTING; - -import java.util.UUID; - -import org.apache.commons.lang3.tuple.Pair; - - import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; +import org.apache.commons.lang3.tuple.Pair; + +import java.util.UUID; + +import static com.databend.jdbc.StatementType.PARAM_SETTING; /** * This represents a statement that is ready to be sent to Databend or executed @@ -39,7 +37,7 @@ public static StatementInfoWrapper of(@NonNull RawStatement rawStatement) { * Creates a StatementInfoWrapper from the {@link RawStatement}. * * @param rawStatement the raw statement - * @param id the id of the statement to execute + * @param id the id of the statement to execute * @return the statement that will be sent to the server */ public static StatementInfoWrapper of(@NonNull RawStatement rawStatement, String id) { diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/StatementType.java b/databend-jdbc/src/main/java/com/databend/jdbc/StatementType.java index 3eff1f3f..cde09627 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/StatementType.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/StatementType.java @@ -1,4 +1,5 @@ package com.databend.jdbc; + public enum StatementType { PARAM_SETTING, // SET QUERY, // eg: SELECT, SHOW diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/StatementUtil.java b/databend-jdbc/src/main/java/com/databend/jdbc/StatementUtil.java index a4680ba7..ed651e88 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/StatementUtil.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/StatementUtil.java @@ -1,18 +1,17 @@ package com.databend.jdbc; -import java.util.*; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import java.util.stream.Collectors; - +import lombok.CustomLog; +import lombok.NonNull; +import lombok.experimental.UtilityClass; import org.apache.commons.lang3.RegExUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.commons.lang3.tuple.Pair; -import lombok.CustomLog; -import lombok.NonNull; -import lombok.experimental.UtilityClass; +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; @UtilityClass @CustomLog diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/cloud/DatabendCopyParams.java b/databend-jdbc/src/main/java/com/databend/jdbc/cloud/DatabendCopyParams.java index cde8e097..04ca87e1 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/cloud/DatabendCopyParams.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/cloud/DatabendCopyParams.java @@ -1,6 +1,5 @@ package com.databend.jdbc.cloud; -import javax.xml.crypto.Data; import java.util.List; import java.util.Locale; import java.util.Map; diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/cloud/DatabendPresignClientV1.java b/databend-jdbc/src/main/java/com/databend/jdbc/cloud/DatabendPresignClientV1.java index 7e65c9de..cfa7a04d 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/cloud/DatabendPresignClientV1.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/cloud/DatabendPresignClientV1.java @@ -1,22 +1,12 @@ package com.databend.jdbc.cloud; -import com.databend.client.DatabendClientV1; -import okhttp3.Headers; -import okhttp3.HttpUrl; -import okhttp3.MediaType; -import okhttp3.MultipartBody; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.RequestBody; -import okhttp3.Response; -import okhttp3.ResponseBody; +import okhttp3.*; import okio.BufferedSink; import okio.Okio; import okio.Source; import org.checkerframework.checker.nullness.qual.NonNull; import javax.annotation.Nullable; - import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -27,7 +17,6 @@ import java.util.logging.Level; import java.util.logging.Logger; -import static com.databend.jdbc.ConnectionProperties.SOCKET_TIMEOUT; import static java.lang.String.format; import static java.util.Objects.requireNonNull; import static java.util.concurrent.TimeUnit.MILLISECONDS; diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/cloud/DatabendStage.java b/databend-jdbc/src/main/java/com/databend/jdbc/cloud/DatabendStage.java index 966dcc45..876950a1 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/cloud/DatabendStage.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/cloud/DatabendStage.java @@ -1,46 +1,40 @@ package com.databend.jdbc.cloud; -public class DatabendStage -{ +public class DatabendStage { private static final String defaultStageName = "~"; private final String stageName; private final String path; private final ExternalLocationS3 externalLocationS3; + // TODO(zhihanz) support more external location such as Azure/GCS - private DatabendStage(String stageName, String path, ExternalLocationS3 externalLocationS3) - { + private DatabendStage(String stageName, String path, ExternalLocationS3 externalLocationS3) { if (stageName == null || stageName.isEmpty()) { - this.stageName = defaultStageName; + this.stageName = defaultStageName; } else { - this.stageName = stageName; + this.stageName = stageName; } this.path = path; this.externalLocationS3 = externalLocationS3; } - public static DatabendStage.Builder builder() - { + public static DatabendStage.Builder builder() { return new DatabendStage.Builder(); } - public String getStageName() - { + public String getStageName() { return stageName; } - public String getPath() - { + public String getPath() { return path; } - public ExternalLocationS3 getExternalLocationS3() - { + public ExternalLocationS3 getExternalLocationS3() { return externalLocationS3; } @Override - public String toString() - { + public String toString() { if (this.externalLocationS3 != null) { return this.externalLocationS3.toString(); } @@ -51,32 +45,27 @@ public String toString() } - public static class Builder - { + public static class Builder { private String stageName; private String path; private ExternalLocationS3 externalLocationS3; - public Builder stageName(String stageName) - { + public Builder stageName(String stageName) { this.stageName = stageName; return this; } - public Builder path(String path) - { + public Builder path(String path) { this.path = path; return this; } - public Builder externalLocationS3(ExternalLocationS3 externalLocationS3) - { + public Builder externalLocationS3(ExternalLocationS3 externalLocationS3) { this.externalLocationS3 = externalLocationS3; return this; } - public DatabendStage build() - { + public DatabendStage build() { return new DatabendStage(stageName, path, externalLocationS3); } } diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/cloud/ExternalLocationS3.java b/databend-jdbc/src/main/java/com/databend/jdbc/cloud/ExternalLocationS3.java index c7fba93d..8b41696f 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/cloud/ExternalLocationS3.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/cloud/ExternalLocationS3.java @@ -1,8 +1,7 @@ package com.databend.jdbc.cloud; // https://databend.rs/doc/sql-commands/dml/dml-copy-into-table#externallocation -public class ExternalLocationS3 -{ +public class ExternalLocationS3 { // for example: s3://bucket_name/path private final String location; private final String endpointUrl; @@ -11,8 +10,8 @@ public class ExternalLocationS3 private final String sessionToken; private final String region; private final boolean enableVirtualHostStyle; - private ExternalLocationS3(String location, String endpointUrl, String accessKeyId, String secretAccessKey, String sessionToken, String region, boolean enableVirtualHostStyle) - { + + private ExternalLocationS3(String location, String endpointUrl, String accessKeyId, String secretAccessKey, String sessionToken, String region, boolean enableVirtualHostStyle) { this.location = location; this.endpointUrl = endpointUrl; this.accessKeyId = accessKeyId; @@ -26,38 +25,31 @@ public static ExternalLocationS3.Builder builder() { return new ExternalLocationS3.Builder(); } - public String getLocation() - { + public String getLocation() { return location; } - public String getEndpointUrl() - { + public String getEndpointUrl() { return endpointUrl; } - public String getAccessKeyId() - { + public String getAccessKeyId() { return accessKeyId; } - public String getSecretAccessKey() - { + public String getSecretAccessKey() { return secretAccessKey; } - public String getSessionToken() - { + public String getSessionToken() { return sessionToken; } - public String getRegion() - { + public String getRegion() { return region; } - public boolean isEnableVirtualHostStyle() - { + public boolean isEnableVirtualHostStyle() { return enableVirtualHostStyle; } @@ -112,8 +104,7 @@ public String toString() { } // builder pattern - public static class Builder - { + public static class Builder { private String location; private String endpointUrl; private String accessKeyId; @@ -122,50 +113,42 @@ public static class Builder private String region; private boolean enableVirtualHostStyle; - public Builder setLocation(String location) - { + public Builder setLocation(String location) { this.location = location; return this; } - public Builder setEndpointUrl(String endpointUrl) - { + public Builder setEndpointUrl(String endpointUrl) { this.endpointUrl = endpointUrl; return this; } - public Builder setAccessKeyId(String accessKeyId) - { + public Builder setAccessKeyId(String accessKeyId) { this.accessKeyId = accessKeyId; return this; } - public Builder setSecretAccessKey(String secretAccessKey) - { + public Builder setSecretAccessKey(String secretAccessKey) { this.secretAccessKey = secretAccessKey; return this; } - public Builder setSessionToken(String sessionToken) - { + public Builder setSessionToken(String sessionToken) { this.sessionToken = sessionToken; return this; } - public Builder setRegion(String region) - { + public Builder setRegion(String region) { this.region = region; return this; } - public Builder setEnableVirtualHostStyle(boolean enableVirtualHostStyle) - { + public Builder setEnableVirtualHostStyle(boolean enableVirtualHostStyle) { this.enableVirtualHostStyle = enableVirtualHostStyle; return this; } - public ExternalLocationS3 build() - { + public ExternalLocationS3 build() { return new ExternalLocationS3(location, endpointUrl, accessKeyId, secretAccessKey, sessionToken, region, enableVirtualHostStyle); } } diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/examples/DatabendConnectionFactory.java b/databend-jdbc/src/main/java/com/databend/jdbc/examples/DatabendConnectionFactory.java index 466f315b..2bfd1ffd 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/examples/DatabendConnectionFactory.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/examples/DatabendConnectionFactory.java @@ -1,10 +1,9 @@ package com.databend.jdbc.examples; + import com.databend.jdbc.DatabendConnection; -import com.fasterxml.jackson.databind.annotation.JsonAppend; -import org.apache.commons.pool2.PooledObjectFactory; import org.apache.commons.pool2.PooledObject; +import org.apache.commons.pool2.PooledObjectFactory; import org.apache.commons.pool2.impl.DefaultPooledObject; -import org.apache.commons.pool2.impl.GenericObjectPool; import java.sql.Connection; import java.sql.DriverManager; @@ -13,13 +12,14 @@ public class DatabendConnectionFactory implements PooledObjectFactory { - private String url; + private String url; private Properties properties; public DatabendConnectionFactory(String url, Properties properties) { this.url = url; this.properties = properties; } + private Connection createConnection(String url, Properties p) throws SQLException { return DriverManager.getConnection(url, p); } diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/examples/DatabendConnectionPool.java b/databend-jdbc/src/main/java/com/databend/jdbc/examples/DatabendConnectionPool.java index 77954622..9ec68768 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/examples/DatabendConnectionPool.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/examples/DatabendConnectionPool.java @@ -8,7 +8,7 @@ public class DatabendConnectionPool extends GenericObjectPool { public DatabendConnectionPool(DatabendConnectionFactory factory, GenericObjectPoolConfig config) { - super(factory,config); + super(factory, config); } public void testDemo() throws Exception { @@ -21,7 +21,7 @@ public void testDemo() throws Exception { props.setProperty("SSL", "false"); // Create a Databend connection pool DatabendConnectionFactory factory = new DatabendConnectionFactory("jdbc:databend://localhost:8000", props); - DatabendConnectionPool pool = new DatabendConnectionPool(factory,config); + DatabendConnectionPool pool = new DatabendConnectionPool(factory, config); // Get a connection from the pool DatabendConnection connection = pool.borrowObject(); diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/examples/Examples.java b/databend-jdbc/src/main/java/com/databend/jdbc/examples/Examples.java index f23c87f8..0443e38d 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/examples/Examples.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/examples/Examples.java @@ -1,15 +1,6 @@ package com.databend.jdbc.examples; -import java.sql.Connection; -import java.sql.Date; -import java.sql.DriverManager; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.sql.Timestamp; -import java.util.ArrayList; -import java.util.List; +import java.sql.*; class Examples { private static Connection createConnection() diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/log/DatabendLogger.java b/databend-jdbc/src/main/java/com/databend/jdbc/log/DatabendLogger.java index 1ce7a2a4..688a6ef5 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/log/DatabendLogger.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/log/DatabendLogger.java @@ -2,34 +2,34 @@ public interface DatabendLogger { - void trace(String message); + void trace(String message); - void trace(String message, Object... arguments); + void trace(String message, Object... arguments); - void trace(String message, Throwable t); + void trace(String message, Throwable t); - void debug(String message); + void debug(String message); - void debug(String message, Object... arguments); + void debug(String message, Object... arguments); - void debug(String message, Throwable t); + void debug(String message, Throwable t); - void info(String message); + void info(String message); - void info(String message, Object... arguments); + void info(String message, Object... arguments); - void info(String message, Throwable t); + void info(String message, Throwable t); - void warn(String message); + void warn(String message); - void warn(String message, Object... arguments); + void warn(String message, Object... arguments); - void warn(String message, Throwable t); + void warn(String message, Throwable t); - void error(String message); + void error(String message); - void error(String message, Object... arguments); + void error(String message, Object... arguments); - void error(String message, Throwable t); + void error(String message, Throwable t); } diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/log/JDKLogger.java b/databend-jdbc/src/main/java/com/databend/jdbc/log/JDKLogger.java index 4ae6000e..6c3fe47e 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/log/JDKLogger.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/log/JDKLogger.java @@ -4,108 +4,108 @@ public class JDKLogger implements DatabendLogger { - private final java.util.logging.Logger logger; - - public JDKLogger(String name) { - this.logger = java.util.logging.Logger.getLogger(name); - } - - @Override - public void trace(String message) { - logger.log(Level.FINEST, message); - } - - @Override - public void trace(String message, Object... arguments) { - logger.log(Level.FINEST, addMissingArgumentsIndexes(message), arguments); - } - - @Override - public void trace(String message, Throwable t) { - logger.log(Level.FINEST, message, t); - } - - @Override - public void debug(String message) { - logger.log(Level.FINE, message); - } - - @Override - public void debug(String message, Object... arguments) { - logger.log(Level.FINE, addMissingArgumentsIndexes(message), arguments); - } - - @Override - public void debug(String message, Throwable t) { - logger.log(Level.FINE, message, t); - } - - @Override - public void info(String message) { - logger.log(Level.INFO, message); - } - - @Override - public void info(String message, Object... arguments) { - logger.log(Level.INFO, addMissingArgumentsIndexes(message), arguments); - } - - @Override - public void info(String message, Throwable t) { - logger.log(Level.INFO, message, t); - } - - @Override - public void warn(String message) { - logger.log(Level.WARNING, message); - } - - @Override - public void warn(String message, Object... arguments) { - logger.log(Level.WARNING, addMissingArgumentsIndexes(message), arguments); - } - - @Override - public void warn(String message, Throwable t) { - logger.log(Level.WARNING, message, t); - - } - - @Override - public void error(String message) { - logger.log(Level.SEVERE, message); - } - - @Override - public void error(String message, Object... arguments) { - logger.log(Level.SEVERE, addMissingArgumentsIndexes(message), arguments); - } - - @Override - public void error(String message, Throwable t) { - logger.log(Level.SEVERE, message, t); - } - - /** - * SLF4J and java.util.logging use a different log format. With SLF4J it is not - * required to have argument indexes in the logs (eg: "log.info("hello {}", - * "world");), but it is required for java.util.logging (eg: "log.info("hello - * {1}", "world");) In this project we use the SLF4J way of logging, which is - * why we need to add the missing indexes. - */ - private String addMissingArgumentsIndexes(String message) { - StringBuilder result = new StringBuilder(); - int argumentIndex = 0; - int i = 0; - while (i < message.length()) { - if (message.charAt(i) == '{' && i < message.length() - 1 && message.charAt(i + 1) == '}') { - result.append(String.format("{%d}", argumentIndex++)); - i++; - } else { - result.append(message.charAt(i)); - } - i++; - } - return result.toString(); - } + private final java.util.logging.Logger logger; + + public JDKLogger(String name) { + this.logger = java.util.logging.Logger.getLogger(name); + } + + @Override + public void trace(String message) { + logger.log(Level.FINEST, message); + } + + @Override + public void trace(String message, Object... arguments) { + logger.log(Level.FINEST, addMissingArgumentsIndexes(message), arguments); + } + + @Override + public void trace(String message, Throwable t) { + logger.log(Level.FINEST, message, t); + } + + @Override + public void debug(String message) { + logger.log(Level.FINE, message); + } + + @Override + public void debug(String message, Object... arguments) { + logger.log(Level.FINE, addMissingArgumentsIndexes(message), arguments); + } + + @Override + public void debug(String message, Throwable t) { + logger.log(Level.FINE, message, t); + } + + @Override + public void info(String message) { + logger.log(Level.INFO, message); + } + + @Override + public void info(String message, Object... arguments) { + logger.log(Level.INFO, addMissingArgumentsIndexes(message), arguments); + } + + @Override + public void info(String message, Throwable t) { + logger.log(Level.INFO, message, t); + } + + @Override + public void warn(String message) { + logger.log(Level.WARNING, message); + } + + @Override + public void warn(String message, Object... arguments) { + logger.log(Level.WARNING, addMissingArgumentsIndexes(message), arguments); + } + + @Override + public void warn(String message, Throwable t) { + logger.log(Level.WARNING, message, t); + + } + + @Override + public void error(String message) { + logger.log(Level.SEVERE, message); + } + + @Override + public void error(String message, Object... arguments) { + logger.log(Level.SEVERE, addMissingArgumentsIndexes(message), arguments); + } + + @Override + public void error(String message, Throwable t) { + logger.log(Level.SEVERE, message, t); + } + + /** + * SLF4J and java.util.logging use a different log format. With SLF4J it is not + * required to have argument indexes in the logs (eg: "log.info("hello {}", + * "world");), but it is required for java.util.logging (eg: "log.info("hello + * {1}", "world");) In this project we use the SLF4J way of logging, which is + * why we need to add the missing indexes. + */ + private String addMissingArgumentsIndexes(String message) { + StringBuilder result = new StringBuilder(); + int argumentIndex = 0; + int i = 0; + while (i < message.length()) { + if (message.charAt(i) == '{' && i < message.length() - 1 && message.charAt(i + 1) == '}') { + result.append(String.format("{%d}", argumentIndex++)); + i++; + } else { + result.append(message.charAt(i)); + } + i++; + } + return result.toString(); + } } diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/log/SLF4JLogger.java b/databend-jdbc/src/main/java/com/databend/jdbc/log/SLF4JLogger.java index 9876f869..7abe8bcc 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/log/SLF4JLogger.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/log/SLF4JLogger.java @@ -5,85 +5,85 @@ public class SLF4JLogger implements DatabendLogger { - private final Logger logger; - - public SLF4JLogger(String name) { - logger = LoggerFactory.getLogger(name); - } - - @Override - public void trace(String message) { - logger.trace(message); - } - - @Override - public void trace(String message, Object... arguments) { - logger.trace(message, arguments); - } - - @Override - public void trace(String message, Throwable t) { - logger.trace(message, t); - } - - @Override - public void debug(String message) { - logger.debug(message); - } - - @Override - public void debug(String message, Object... arguments) { - logger.debug(message, arguments); - - } - - @Override - public void debug(String message, Throwable t) { - logger.debug(message, t); - } - - @Override - public void info(String message) { - logger.info(message); - } - - @Override - public void info(String message, Object... arguments) { - logger.info(message, arguments); - } - - @Override - public void info(String message, Throwable t) { - logger.info(message, t); - } - - @Override - public void warn(String message) { - logger.warn(message); - } - - @Override - public void warn(String message, Object... arguments) { - logger.warn(message, arguments); - } - - @Override - public void warn(String message, Throwable t) { - logger.warn(message, t); - } - - @Override - public void error(String message) { - logger.error(message); - } - - @Override - public void error(String message, Object... arguments) { - logger.error(message, arguments); - } - - @Override - public void error(String message, Throwable t) { - logger.error(message, t); - } + private final Logger logger; + + public SLF4JLogger(String name) { + logger = LoggerFactory.getLogger(name); + } + + @Override + public void trace(String message) { + logger.trace(message); + } + + @Override + public void trace(String message, Object... arguments) { + logger.trace(message, arguments); + } + + @Override + public void trace(String message, Throwable t) { + logger.trace(message, t); + } + + @Override + public void debug(String message) { + logger.debug(message); + } + + @Override + public void debug(String message, Object... arguments) { + logger.debug(message, arguments); + + } + + @Override + public void debug(String message, Throwable t) { + logger.debug(message, t); + } + + @Override + public void info(String message) { + logger.info(message); + } + + @Override + public void info(String message, Object... arguments) { + logger.info(message, arguments); + } + + @Override + public void info(String message, Throwable t) { + logger.info(message, t); + } + + @Override + public void warn(String message) { + logger.warn(message); + } + + @Override + public void warn(String message, Object... arguments) { + logger.warn(message, arguments); + } + + @Override + public void warn(String message, Throwable t) { + logger.warn(message, t); + } + + @Override + public void error(String message) { + logger.error(message); + } + + @Override + public void error(String message, Object... arguments) { + logger.error(message, arguments); + } + + @Override + public void error(String message, Throwable t) { + logger.error(message, t); + } } diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/parser/BatchInsertUtils.java b/databend-jdbc/src/main/java/com/databend/jdbc/parser/BatchInsertUtils.java index fe7f2dd6..7383596c 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/parser/BatchInsertUtils.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/parser/BatchInsertUtils.java @@ -2,22 +2,16 @@ import de.siegmar.fastcsv.writer.CsvWriter; import de.siegmar.fastcsv.writer.LineDelimiter; -import de.siegmar.fastcsv.writer.QuoteStrategy; import java.io.File; import java.io.FileWriter; import java.io.IOException; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.TreeMap; -import java.util.UUID; -import java.util.logging.Level; +import java.util.*; import java.util.logging.Logger; -import java.util.stream.Collectors; -import java.util.stream.Stream; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; +import java.util.stream.Stream; public class BatchInsertUtils { private static final Logger logger = Logger.getLogger(BatchInsertUtils.class.getPackage().getName()); diff --git a/databend-jdbc/src/test/java/com/databend/jdbc/StatementUtilTest.java b/databend-jdbc/src/test/java/com/databend/jdbc/StatementUtilTest.java index dc7af752..9a91ae52 100644 --- a/databend-jdbc/src/test/java/com/databend/jdbc/StatementUtilTest.java +++ b/databend-jdbc/src/test/java/com/databend/jdbc/StatementUtilTest.java @@ -1,13 +1,13 @@ package com.databend.jdbc; import com.google.common.collect.ImmutableMap; -import org.apache.commons.lang3.tuple.ImmutablePair; import org.junit.jupiter.api.Test; import java.util.Map; import static com.databend.jdbc.StatementUtil.replaceParameterMarksWithValues; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; + public class StatementUtilTest { @Test void shouldGetAllQueryParamsFromIn() { @@ -16,6 +16,7 @@ void shouldGetAllQueryParamsFromIn() { System.out.println(StatementUtil.parseToRawStatementWrapper(sql).getSubStatements()); assertEquals(1, StatementUtil.parseToRawStatementWrapper(sql).getSubStatements().size()); } + @Test void shouldGetAllQueryParams() { String sql = "SElECT * FROM EMPLOYEES WHERE id = ?"; diff --git a/databend-jdbc/src/test/java/com/databend/jdbc/TestBasicDriver.java b/databend-jdbc/src/test/java/com/databend/jdbc/TestBasicDriver.java index c8758e6d..bf79fa5c 100644 --- a/databend-jdbc/src/test/java/com/databend/jdbc/TestBasicDriver.java +++ b/databend-jdbc/src/test/java/com/databend/jdbc/TestBasicDriver.java @@ -8,7 +8,7 @@ import org.testng.Assert; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; -import org.junit.Ignore; + import java.sql.*; import java.util.Properties; diff --git a/databend-jdbc/src/test/java/com/databend/jdbc/TestCopyInto.java b/databend-jdbc/src/test/java/com/databend/jdbc/TestCopyInto.java index dba09e4e..6fa004d8 100644 --- a/databend-jdbc/src/test/java/com/databend/jdbc/TestCopyInto.java +++ b/databend-jdbc/src/test/java/com/databend/jdbc/TestCopyInto.java @@ -9,11 +9,9 @@ import static org.testng.Assert.assertEquals; -public class TestCopyInto -{ +public class TestCopyInto { @Test(groups = {"Unit"}) - public void TestParseSql() - { + public void TestParseSql() { DatabendStage s = DatabendStage.builder().stageName("~").path("a/b/c").build(); List files = new ArrayList<>(); files.add("file.csv"); diff --git a/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendDatabaseMetaData.java b/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendDatabaseMetaData.java index 53ac3cc9..33cbeea7 100644 --- a/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendDatabaseMetaData.java +++ b/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendDatabaseMetaData.java @@ -5,19 +5,14 @@ import org.testng.annotations.Test; import java.math.BigDecimal; -import java.sql.Connection; -import java.sql.DatabaseMetaData; -import java.sql.DriverManager; -import java.sql.ResultSet; -import java.sql.ResultSetMetaData; -import java.sql.SQLException; -import java.sql.Types; +import java.sql.*; import java.util.ArrayList; import java.util.List; import java.util.Locale; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; + @Test(timeOut = 10000) public class TestDatabendDatabaseMetaData { private static void assertTableMetadata(ResultSet rs) @@ -99,7 +94,7 @@ public void testGetDatabaseProductVersion() throws Exception { try (Connection connection = createConnection()) { DatabaseMetaData metaData = connection.getMetaData(); - float majorVersion = (float)metaData.getDatabaseMajorVersion() / 10; + float majorVersion = (float) metaData.getDatabaseMajorVersion() / 10; int minorVersion = metaData.getDatabaseMinorVersion(); String checkVersion = String.format("v%.1f.%d", majorVersion, minorVersion); Assert.assertTrue(metaData.getDatabaseProductVersion().contains(checkVersion)); @@ -178,7 +173,7 @@ public void testColumnsMeta() throws Exception { String columnName = rs.getString("COLUMN_NAME"); int dataType = rs.getInt("data_type"); String columnType = rs.getString("type_name"); - System.out.println(tableCat + " "+tableSchem + " " + tableName + " " + columnName + " " + dataType + " " + columnType); + System.out.println(tableCat + " " + tableSchem + " " + tableName + " " + columnName + " " + dataType + " " + columnType); } } System.out.println("===================================="); @@ -229,7 +224,7 @@ public void testGetObjectWithDecimal() throws Exception { try (Connection connection = createConnection()) { connection.createStatement().execute("insert into decimal_test values(1.2)"); ResultSet rs = connection.createStatement().executeQuery("select * from decimal_test"); - while (rs.next()){ + while (rs.next()) { assertTrue(rs.getObject(1) instanceof BigDecimal); } } diff --git a/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendDriverUri.java b/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendDriverUri.java index 926a9ca7..4477dc1c 100644 --- a/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendDriverUri.java +++ b/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendDriverUri.java @@ -9,6 +9,7 @@ import java.util.Properties; import static org.assertj.core.api.Assertions.assertThatThrownBy; + @Test(timeOut = 10000) public class TestDatabendDriverUri { private static DatabendDriverUri createDriverUri(String url) @@ -232,6 +233,7 @@ public void testFull() throws SQLException { Assert.assertEquals("null", uri.nullDisplay().toString()); Assert.assertEquals(false, uri.getStrNullAsNull()); } + @Test public void TestSetSchema() throws SQLException { String url = "jdbc:databend://databend:databend@localhost:8000/"; @@ -240,7 +242,7 @@ public void TestSetSchema() throws SQLException { try { connection.createStatement().execute("create or replace database test2"); connection.createStatement().execute("create or replace table test2.test2(id int)"); - }catch (SQLException e){ + } catch (SQLException e) { throw new RuntimeException(e); } connection.setSchema("test2"); diff --git a/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendParameterMetaData.java b/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendParameterMetaData.java index e241c062..86d06919 100644 --- a/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendParameterMetaData.java +++ b/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendParameterMetaData.java @@ -6,7 +6,6 @@ import org.testng.annotations.Test; import java.sql.*; -import java.util.Properties; public class TestDatabendParameterMetaData { diff --git a/databend-jdbc/src/test/java/com/databend/jdbc/TestFileTransfer.java b/databend-jdbc/src/test/java/com/databend/jdbc/TestFileTransfer.java index a29adba6..29a99d5d 100644 --- a/databend-jdbc/src/test/java/com/databend/jdbc/TestFileTransfer.java +++ b/databend-jdbc/src/test/java/com/databend/jdbc/TestFileTransfer.java @@ -9,20 +9,9 @@ import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; +import java.io.*; import java.nio.file.Files; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.HashMap; -import java.util.Map; +import java.sql.*; import java.util.logging.Level; import java.util.logging.Logger; diff --git a/databend-jdbc/src/test/java/com/databend/jdbc/TestMultiHost.java b/databend-jdbc/src/test/java/com/databend/jdbc/TestMultiHost.java index 06f2d5f1..e1150774 100644 --- a/databend-jdbc/src/test/java/com/databend/jdbc/TestMultiHost.java +++ b/databend-jdbc/src/test/java/com/databend/jdbc/TestMultiHost.java @@ -1,19 +1,25 @@ package com.databend.jdbc; -import com.databend.client.PaginationOptions; +import com.databend.client.DiscoveryNode; import org.testng.Assert; import org.testng.annotations.Test; +import java.net.URI; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; public class TestMultiHost { - private final String DEFAULT_JDBC_URL = "jdbc:databend://localhost:8000,localhost:8002,localhost:8003/default"; - private final String RANDOM_JDBC_URL = "jdbc:databend://localhost:8000,localhost:8002,localhost:8003/default?load_balancing_policy=random"; - private final String ROUND_ROBIN_JDBC_URL = "jdbc:databend://localhost:8000,localhost:8002,localhost:8003/default?load_balancing_policy=round_robin"; - private final String FAIL_OVER_JDBC_URL = "jdbc:databend://localhost:7222,localhost:7223,localhost:7224,localhost:8000/default?load_balancing_policy=round_robin&max_failover_retry=4"; + private final String DEFAULT_JDBC_URL = "jdbc:databend://localhost:8000,localhost:8002,localhost:8003/default"; + private final String RANDOM_JDBC_URL = "jdbc:databend://localhost:8000,localhost:8002,localhost:8003/default?load_balancing_policy=random"; + private final String ROUND_ROBIN_JDBC_URL = "jdbc:databend://localhost:8000,localhost:8002,localhost:8003/default?load_balancing_policy=round_robin"; + private final String FAIL_OVER_JDBC_URL = "jdbc:databend://localhost:7222,localhost:7223,localhost:7224,localhost:8000/default?load_balancing_policy=round_robin&max_failover_retry=4"; + private final String AUTO_DISCOVERY_JDBC_URL = "jdbc:databend://localhost:8000/default?load_balancing_policy=round_robin&auto_discovery=true"; + private final String UNSUPPORT_AUTO_DISCOVERY_JDBC_URL = "jdbc:databend://localhost:8000/default?load_balancing_policy=round_robin&auto_discovery=true&enable_mock=true"; + private Connection createConnection(String url) throws SQLException { return DriverManager.getConnection(url, "databend", "databend"); @@ -33,15 +39,15 @@ public void testDefaultLoadBalancing() statement.execute("select value from system.configs where name = 'http_handler_port';"); ResultSet r = statement.getResultSet(); r.next(); - if (r.getInt(1) == 8000) { - node8000++; - } else if (r.getInt(1) == 8002) { - node8002++; - } else if (r.getInt(1) == 8003) { - node8003++; - } else { - unknown++; - } + if (r.getInt(1) == 8000) { + node8000++; + } else if (r.getInt(1) == 8002) { + node8002++; + } else if (r.getInt(1) == 8003) { + node8003++; + } else { + unknown++; + } } } Assert.assertEquals(node8000, 100); @@ -185,4 +191,80 @@ public void testFailOver() Assert.assertEquals(unknown, 0); Assert.assertEquals(node8000 + node8002 + node8003, 90); } + + @Test(groups = {"IT", "cluster"}) + public void testAutoDiscovery() + throws SQLException { + // try connect with three nodes 1000 times and count for each node + int node8000 = 0; + int node8002 = 0; + int node8003 = 0; + int unknown = 0; + try (Connection connection = createConnection(AUTO_DISCOVERY_JDBC_URL)) { + for (int i = 0; i < 30; i++) { + DatabendStatement statement = (DatabendStatement) connection.createStatement(); + // remove the effect setup commands + for (int j = 0; j < 3; j++) { + statement.execute("select value from system.configs where name = 'http_handler_port';"); + ResultSet r = statement.getResultSet(); + r.next(); + if (r.getInt(1) == 8000) { + node8000++; + } else if (r.getInt(1) == 8002) { + node8002++; + } else if (r.getInt(1) == 8003) { + node8003++; + } else { + unknown++; + } + } + } + } + System.out.println("node8000: " + node8000 + ", node8002: " + node8002 + ", node8003: " + node8003 + ", unknown: " + unknown); + + Assert.assertEquals(node8000, 31); + Assert.assertEquals(node8002, 30); + Assert.assertEquals(node8003, 29); + Assert.assertEquals(unknown, 0); + Assert.assertEquals(node8000 + node8002 + node8003, 90); + } + + @Test(groups = {"IT", "cluster"}) + public void testUnSupportedAutoDiscovery() + throws SQLException { + try (Connection connection = createConnection(UNSUPPORT_AUTO_DISCOVERY_JDBC_URL)) { + + DatabendStatement statement = (DatabendStatement) connection.createStatement(); + statement.execute("select value from system.configs where name = 'http_handler_port';"); + ResultSet r = statement.getResultSet(); + r.next(); + DatabendConnection dbc = (DatabendConnection) connection; + // automatically + Assert.assertFalse(dbc.isAutoDiscovery()); + } catch (SQLException e) { + // there should be no exception + Assert.fail("Should not throw exception"); + } + } + + @Test(groups = {"unit"}) + public void testAutoDiscoveryUriParsing() throws SQLException { + DatabendDriverUri uri = DatabendDriverUri.create("jdbc:databend://localhost:8000?ssl=true", null); + DatabendDriverUri uri2 = DatabendDriverUri.create("jdbc:databend://127.0.0.1:8000,127.0.0.1:8002,127.0.0.1:8003?ssl=true", null); + List uris2 = uri2.getNodes().getUris(); + + DatabendNodes nodes = uri.getNodes(); + List discoveryNodes = new ArrayList<>(); + discoveryNodes.add(DiscoveryNode.create("127.0.0.1:8000")); + discoveryNodes.add(DiscoveryNode.create("127.0.0.1:8002")); + discoveryNodes.add(DiscoveryNode.create("127.0.0.1:8003")); + List uris = nodes.parseURI(discoveryNodes); + for (URI u : uris) { + System.out.println(u); + } + Assert.assertEquals(uris.size(), 3); + Assert.assertEquals(uris2.size(), 3); + Assert.assertEquals(uris2, uris); + + } } diff --git a/databend-jdbc/src/test/java/com/databend/jdbc/TestPrepareStatement.java b/databend-jdbc/src/test/java/com/databend/jdbc/TestPrepareStatement.java index b62c94ec..b315f2a6 100644 --- a/databend-jdbc/src/test/java/com/databend/jdbc/TestPrepareStatement.java +++ b/databend-jdbc/src/test/java/com/databend/jdbc/TestPrepareStatement.java @@ -6,16 +6,7 @@ import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; -import java.io.ByteArrayInputStream; -import java.io.InputStream; -import java.sql.Connection; -import java.sql.Date; -import java.sql.DriverManager; import java.sql.*; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.sql.Timestamp; import java.util.ArrayList; import java.util.List; diff --git a/databend-jdbc/src/test/java/com/databend/jdbc/TestPresignContext.java b/databend-jdbc/src/test/java/com/databend/jdbc/TestPresignContext.java index 4366dd90..f1f653c3 100644 --- a/databend-jdbc/src/test/java/com/databend/jdbc/TestPresignContext.java +++ b/databend-jdbc/src/test/java/com/databend/jdbc/TestPresignContext.java @@ -7,14 +7,13 @@ import java.sql.DriverManager; import java.sql.SQLException; -public class TestPresignContext -{ +public class TestPresignContext { private Connection createConnection() - throws SQLException - { + throws SQLException { String url = "jdbc:databend://localhost:8000"; return DriverManager.getConnection(url, "databend", "databend"); } + @Test(groups = {"Unit"}) public void TestPreisgnUrlBuild() { String presignSql = PresignContext.buildRequestSQL(PresignContext.PresignMethod.UPLOAD, "test_bucket", "test.csv"); @@ -33,8 +32,7 @@ public void TestGetPresignUrl() { Assert.assertNotNull(ctx); Assert.assertNotNull(ctx.getUrl()); Assert.assertNotNull(ctx.getHeaders()); - } - catch (Exception e) { + } catch (Exception e) { throw new RuntimeException(e); } } @@ -49,8 +47,7 @@ public void TestGetPresignUrlCase2() { Assert.assertNotNull(ctx); Assert.assertNotNull(ctx.getUrl()); Assert.assertNotNull(ctx.getHeaders()); - } - catch (Exception e) { + } catch (Exception e) { throw new RuntimeException(e); } } diff --git a/databend-jdbc/src/test/java/com/databend/jdbc/TestStageAttachment.java b/databend-jdbc/src/test/java/com/databend/jdbc/TestStageAttachment.java index f18c4b1a..43a9f2bb 100644 --- a/databend-jdbc/src/test/java/com/databend/jdbc/TestStageAttachment.java +++ b/databend-jdbc/src/test/java/com/databend/jdbc/TestStageAttachment.java @@ -3,21 +3,18 @@ import com.databend.client.StageAttachment; import org.testng.annotations.Test; -import java.time.LocalDateTime; -import java.util.UUID; - import static org.testng.Assert.assertEquals; public class TestStageAttachment { @Test(groups = {"Unit"}) - public void TestStageAttachment(){ + public void TestStageAttachment() { String uuid = "uuid/"; - String stagePrefix = "prefix/"+uuid; + String stagePrefix = "prefix/" + uuid; String fileName = "test"; String stagePath = "@~/" + stagePrefix + fileName; StageAttachment attachment = new StageAttachment.Builder().setLocation(stagePath) .build(); - assertEquals("StageAttachment{location=@~/prefix/uuid/test, file_format_options={type=CSV}, copy_options=null}",attachment.toString()); + assertEquals("StageAttachment{location=@~/prefix/uuid/test, file_format_options={type=CSV}, copy_options=null}", attachment.toString()); } } diff --git a/databend-jdbc/src/test/java/com/databend/jdbc/cloud/TestDatabendCopyParams.java b/databend-jdbc/src/test/java/com/databend/jdbc/cloud/TestDatabendCopyParams.java index 08c45400..2fbc6106 100644 --- a/databend-jdbc/src/test/java/com/databend/jdbc/cloud/TestDatabendCopyParams.java +++ b/databend-jdbc/src/test/java/com/databend/jdbc/cloud/TestDatabendCopyParams.java @@ -42,8 +42,8 @@ public void testDatabendStage() { DatabendStage s = DatabendStage.builder().stageName("~").path("jdbc/c2/").build(); prms = DatabendCopyParams.builder().setFiles(files).setDatabendStage(s).setPattern("a.txt").setType("parquet") .setCopyOptions(copyOptions).setFileOptions(fileOptions).build(); - assertEquals(prms.getDatabendStage().getStageName(),"~"); - assertEquals(prms.getDatabendStage().getPath(),"jdbc/c2/"); + assertEquals(prms.getDatabendStage().getStageName(), "~"); + assertEquals(prms.getDatabendStage().getPath(), "jdbc/c2/"); assertEquals(prms.getPattern(), "a.txt"); assertEquals(prms.getType(), "parquet"); assertEquals(prms.getFiles().size(), 2); diff --git a/databend-jdbc/src/test/java/com/databend/jdbc/cloud/TestDatabendStage.java b/databend-jdbc/src/test/java/com/databend/jdbc/cloud/TestDatabendStage.java index b9b642f0..d83e75d2 100644 --- a/databend-jdbc/src/test/java/com/databend/jdbc/cloud/TestDatabendStage.java +++ b/databend-jdbc/src/test/java/com/databend/jdbc/cloud/TestDatabendStage.java @@ -4,11 +4,9 @@ import static org.testng.Assert.assertEquals; -public class TestDatabendStage -{ +public class TestDatabendStage { @Test(groups = {"Unit"}) - public void testDatabendStage() - { + public void testDatabendStage() { DatabendStage stage = DatabendStage.builder() .stageName("stage_name") .path("path") @@ -19,8 +17,7 @@ public void testDatabendStage() } @Test(groups = {"Unit"}) - public void testDatabendStageWithExternalLocation() - { + public void testDatabendStageWithExternalLocation() { DatabendStage stage = DatabendStage.builder() .externalLocationS3(ExternalLocationS3.builder() .setLocation("s3://mybucket/path/a/b/c") diff --git a/pom.xml b/pom.xml index 5ce8bf19..cbe4b5f7 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ com.databend databend-base - 0.3.0 + 0.3.1 databend-base Databend pom From b8145fb76997fae6b0b72506c28dd06d653ed20e Mon Sep 17 00:00:00 2001 From: zhihanz Date: Fri, 6 Sep 2024 10:24:11 +0800 Subject: [PATCH 2/2] chore: fix linter --- .../jdbc/AbstractDatabendResultSet.java | 25 ++++++++++- .../com/databend/jdbc/DatabendConnection.java | 40 +++++++++++++++-- .../jdbc/DatabendDatabaseMetaData.java | 8 +++- .../com/databend/jdbc/DatabendDriverUri.java | 12 +++++- .../jdbc/DatabendPreparedStatement.java | 43 +++++++++++++++++-- .../com/databend/jdbc/DatabendResultSet.java | 7 ++- .../jdbc/DatabendResultSetMetaData.java | 10 ++++- .../com/databend/jdbc/DatabendStatement.java | 7 ++- .../java/com/databend/jdbc/LoggerUtil.java | 6 ++- .../jdbc/NonRegisteringDatabendDriver.java | 6 ++- .../java/com/databend/jdbc/StatementUtil.java | 8 +++- .../jdbc/cloud/DatabendPresignClientV1.java | 10 ++++- .../com/databend/jdbc/examples/Examples.java | 6 ++- .../jdbc/parser/BatchInsertUtils.java | 6 ++- 14 files changed, 173 insertions(+), 21 deletions(-) diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/AbstractDatabendResultSet.java b/databend-jdbc/src/main/java/com/databend/jdbc/AbstractDatabendResultSet.java index 3d53e4fd..2eec7742 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/AbstractDatabendResultSet.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/AbstractDatabendResultSet.java @@ -23,12 +23,33 @@ import java.math.BigDecimal; import java.net.URL; import java.nio.charset.StandardCharsets; +import java.sql.Array; +import java.sql.Blob; +import java.sql.Clob; import java.sql.Date; -import java.sql.*; +import java.sql.NClob; +import java.sql.Ref; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.RowId; +import java.sql.SQLException; +import java.sql.SQLFeatureNotSupportedException; +import java.sql.SQLType; +import java.sql.SQLWarning; +import java.sql.SQLXML; +import java.sql.Statement; +import java.sql.Time; +import java.sql.Timestamp; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; -import java.util.*; +import java.util.Calendar; +import java.util.GregorianCalendar; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.TimeZone; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendConnection.java b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendConnection.java index 7a801fe0..d96341f7 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendConnection.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendConnection.java @@ -1,6 +1,12 @@ package com.databend.jdbc; -import com.databend.client.*; +import com.databend.client.ClientSettings; +import com.databend.client.DatabendClient; +import com.databend.client.DatabendClientV1; +import com.databend.client.DatabendSession; +import com.databend.client.PaginationOptions; +import com.databend.client.QueryRequest; +import com.databend.client.StageAttachment; import com.databend.jdbc.annotation.NotImplemented; import com.databend.jdbc.cloud.DatabendCopyParams; import com.databend.jdbc.cloud.DatabendPresignClient; @@ -9,11 +15,37 @@ import okhttp3.Headers; import okhttp3.OkHttpClient; -import java.io.*; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; import java.net.ConnectException; import java.net.URI; -import java.sql.*; -import java.util.*; +import java.sql.Array; +import java.sql.Blob; +import java.sql.CallableStatement; +import java.sql.Clob; +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.NClob; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLClientInfoException; +import java.sql.SQLException; +import java.sql.SQLFeatureNotSupportedException; +import java.sql.SQLWarning; +import java.sql.SQLXML; +import java.sql.Savepoint; +import java.sql.Statement; +import java.sql.Struct; +import java.util.Base64; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; +import java.util.Random; +import java.util.Set; +import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Executor; import java.util.concurrent.atomic.AtomicBoolean; diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendDatabaseMetaData.java b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendDatabaseMetaData.java index 165f2f67..12ef87c7 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendDatabaseMetaData.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendDatabaseMetaData.java @@ -5,7 +5,13 @@ import com.databend.client.data.DatabendRawType; import com.google.common.base.Joiner; -import java.sql.*; +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.ResultSet; +import java.sql.RowIdLifetime; +import java.sql.SQLException; +import java.sql.SQLFeatureNotSupportedException; +import java.sql.Statement; import java.util.ArrayList; import java.util.Collections; import java.util.List; diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendDriverUri.java b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendDriverUri.java index c7dec009..8e740262 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendDriverUri.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendDriverUri.java @@ -8,7 +8,17 @@ import java.net.URI; import java.net.URISyntaxException; import java.sql.SQLException; -import java.util.*; +import java.util.AbstractMap; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Properties; +import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.logging.Logger; diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendPreparedStatement.java b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendPreparedStatement.java index 1dc3cb3f..13db41d8 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendPreparedStatement.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendPreparedStatement.java @@ -11,15 +11,50 @@ import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.ISODateTimeFormat; -import java.io.*; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; import java.math.BigDecimal; import java.net.URL; import java.nio.charset.StandardCharsets; +import java.sql.Array; +import java.sql.Blob; +import java.sql.Clob; import java.sql.Date; -import java.sql.*; -import java.time.*; +import java.sql.NClob; +import java.sql.ParameterMetaData; +import java.sql.PreparedStatement; +import java.sql.Ref; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.RowId; +import java.sql.SQLException; +import java.sql.SQLFeatureNotSupportedException; +import java.sql.SQLXML; +import java.sql.Time; +import java.sql.Timestamp; +import java.sql.Types; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.OffsetDateTime; +import java.time.OffsetTime; import java.time.format.DateTimeFormatterBuilder; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Base64; +import java.util.Calendar; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.UUID; import java.util.function.Consumer; import java.util.logging.Logger; import java.util.stream.Collectors; diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendResultSet.java b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendResultSet.java index 7f8ad811..fe235179 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendResultSet.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendResultSet.java @@ -14,7 +14,12 @@ import java.util.Iterator; import java.util.List; import java.util.Optional; -import java.util.concurrent.*; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import java.util.concurrent.Semaphore; import java.util.stream.Stream; import static com.google.common.base.Throwables.throwIfUnchecked; diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendResultSetMetaData.java b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendResultSetMetaData.java index aac793c9..9dfe335e 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendResultSetMetaData.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendResultSetMetaData.java @@ -1,7 +1,15 @@ package com.databend.jdbc; import java.math.BigDecimal; -import java.sql.*; +import java.sql.Array; +import java.sql.Blob; +import java.sql.Clob; +import java.sql.Date; +import java.sql.ResultSetMetaData; +import java.sql.SQLException; +import java.sql.Time; +import java.sql.Timestamp; +import java.sql.Types; import java.util.List; public class DatabendResultSetMetaData implements ResultSetMetaData { diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendStatement.java b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendStatement.java index 76aeef5f..0ee3bbe3 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendStatement.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendStatement.java @@ -6,7 +6,12 @@ import com.databend.client.StageAttachment; import com.databend.jdbc.annotation.NotImplemented; -import java.sql.*; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.SQLFeatureNotSupportedException; +import java.sql.SQLWarning; +import java.sql.Statement; import java.util.List; import java.util.Optional; import java.util.concurrent.atomic.AtomicBoolean; diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/LoggerUtil.java b/databend-jdbc/src/main/java/com/databend/jdbc/LoggerUtil.java index e67f5e2f..9c25f3e5 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/LoggerUtil.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/LoggerUtil.java @@ -6,7 +6,11 @@ import lombok.CustomLog; import lombok.experimental.UtilityClass; -import java.io.*; +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.util.stream.Collectors; diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/NonRegisteringDatabendDriver.java b/databend-jdbc/src/main/java/com/databend/jdbc/NonRegisteringDatabendDriver.java index b74de401..1e92be34 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/NonRegisteringDatabendDriver.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/NonRegisteringDatabendDriver.java @@ -3,7 +3,11 @@ import okhttp3.OkHttpClient; import java.io.Closeable; -import java.sql.*; +import java.sql.Connection; +import java.sql.Driver; +import java.sql.DriverPropertyInfo; +import java.sql.SQLException; +import java.sql.SQLFeatureNotSupportedException; import java.util.Properties; import java.util.logging.Logger; diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/StatementUtil.java b/databend-jdbc/src/main/java/com/databend/jdbc/StatementUtil.java index ed651e88..8f80f9a9 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/StatementUtil.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/StatementUtil.java @@ -8,7 +8,13 @@ import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.commons.lang3.tuple.Pair; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/cloud/DatabendPresignClientV1.java b/databend-jdbc/src/main/java/com/databend/jdbc/cloud/DatabendPresignClientV1.java index cfa7a04d..81204b62 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/cloud/DatabendPresignClientV1.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/cloud/DatabendPresignClientV1.java @@ -1,6 +1,14 @@ package com.databend.jdbc.cloud; -import okhttp3.*; +import okhttp3.Headers; +import okhttp3.HttpUrl; +import okhttp3.MediaType; +import okhttp3.MultipartBody; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import okhttp3.ResponseBody; import okio.BufferedSink; import okio.Okio; import okio.Source; diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/examples/Examples.java b/databend-jdbc/src/main/java/com/databend/jdbc/examples/Examples.java index 0443e38d..40014ee4 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/examples/Examples.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/examples/Examples.java @@ -1,6 +1,10 @@ package com.databend.jdbc.examples; -import java.sql.*; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; class Examples { private static Connection createConnection() diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/parser/BatchInsertUtils.java b/databend-jdbc/src/main/java/com/databend/jdbc/parser/BatchInsertUtils.java index 7383596c..835027e7 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/parser/BatchInsertUtils.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/parser/BatchInsertUtils.java @@ -6,7 +6,11 @@ import java.io.File; import java.io.FileWriter; import java.io.IOException; -import java.util.*; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.TreeMap; +import java.util.UUID; import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern;