Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

public class ClientSettings {
public static final Integer DEFAULT_QUERY_TIMEOUT = 300;
public static final Integer DEFAULT_CONNECTION_TIMEOUT = 0; // seconds
// seconds
public static final Integer DEFAULT_CONNECTION_TIMEOUT = 0;
public static final Integer DEFAULT_SOCKET_TIMEOUT = 0;
public static final int DEFAULT_RETRY_ATTEMPTS = 5;
public static final String X_Databend_Query_ID = "X-DATABEND-QUERY-ID";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ private static DiscoveryResponseCodec.DiscoveryResponse getDiscoveryResponse(OkH
}

try {
MILLISECONDS.sleep(attempts * 100); // Exponential backoff
// Exponential backoff
MILLISECONDS.sleep(attempts * 100L);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Interrupted while fetching discovery nodes", e);
Expand All @@ -211,7 +212,8 @@ private static DiscoveryResponseCodec.DiscoveryResponse getDiscoveryResponse(OkH
if (response.getStatusCode() == HTTP_OK && response.hasValue()) {
DiscoveryResponseCodec.DiscoveryResponse discoveryResponse = response.getValue();
if (discoveryResponse.getError() == null) {
return discoveryResponse; // Successful response
// Successful response
return discoveryResponse;
}
if (discoveryResponse.getError().notFound()) {
throw new UnsupportedOperationException("Discovery request feature not supported: " + discoveryResponse.getError());
Expand Down Expand Up @@ -240,20 +242,21 @@ private boolean executeInternal(Request request, OptionalLong materializedJsonSi

long start = System.nanoTime();
int attempts = 0;
Exception cause = null;
// Exception cause = null;

while (true) {
if (attempts > 0) {
Duration sinceStart = Duration.ofNanos(System.nanoTime() - start);
if (sinceStart.compareTo(Duration.ofSeconds(requestTimeoutSecs)) > 0) {
throw new RuntimeException(format("Error fetching next (attempts: %s, duration: %s)",
attempts, sinceStart.getSeconds()), cause);
attempts, sinceStart.getSeconds()), null);
}

try {
logger.log(Level.FINE, "Executing query attempt #" + attempts);
// Apply exponential backoff with a cap
long sleepTime = Math.min(100 * (1 << Math.min(attempts - 1, 10)), 5000); // Max 5 seconds
// Max 5 seconds
long sleepTime = Math.min(100 * (1 << Math.min(attempts - 1, 10)), 5000);
MILLISECONDS.sleep(sleepTime);
} catch (InterruptedException e) {
try {
Expand All @@ -274,7 +277,8 @@ private boolean executeInternal(Request request, OptionalLong materializedJsonSi
if (e.getCause() instanceof ConnectException) {
// Log the connection exception but rethrow it to match original behavior
logger.log(Level.WARNING, "Connection exception on attempt " + attempts + ": " + e.getMessage());
throw e; // This will be caught by the caller's retry mechanism
// This will be caught by the caller's retry mechanism
throw e;
}
throw new RuntimeException("Query failed: " + e.getMessage(), e);
}
Expand Down Expand Up @@ -344,7 +348,7 @@ private void processResponse(Headers headers, QueryResults results) {
if (serverVersionString != null) {
try {
serverVersion = serverVersionString;
} catch (Exception e) {
} catch (Exception ignored) {
}
}
String route_hint = headers.get(ClientSettings.X_DATABEND_ROUTE_HINT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public static List<List<Object>> parseRawData(List<QueryRowField> schema, List<L
newRow.add(parsed);
column++;
}
rows.add(unmodifiableList(newRow)); // allow nulls in list
// allow nulls in list
rows.add(unmodifiableList(newRow));
}
return rows.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ abstract class AbstractDatabendResultSet implements ResultSet {
protected final Iterator<List<Object>> results;
private final Optional<Statement> statement;
private final AtomicReference<List<Object>> row = new AtomicReference<>();
private final AtomicLong currentRowNumber = new AtomicLong(); // Index into 'rows' of our current row (1-based)
// Index into 'rows' of our current row (1-based)
private final AtomicLong currentRowNumber = new AtomicLong();
private final AtomicBoolean wasNull = new AtomicBoolean();
private final Map<String, Integer> fieldMap;
private final List<DatabendColumnInfo> databendColumnInfoList;
Expand Down Expand Up @@ -125,11 +126,15 @@ private static List<DatabendColumnInfo> getColumnInfo(List<QueryRowField> column
ImmutableList.Builder<DatabendColumnInfo> list = ImmutableList.builderWithExpectedSize(columns.size());
for (QueryRowField column : columns) {
DatabendColumnInfo.Builder builder = new DatabendColumnInfo.Builder()
.setCatalogName("") // TODO
.setSchemaName("") // TODO
.setTableName("") // TODO
// TODO
.setCatalogName("")
// TODO
.setSchemaName("")
// TODO
.setTableName("")
.setColumnLabel(column.getName())
.setColumnName(column.getName()) // TODO
// TODO
.setColumnName(column.getName())
.setColumnTypeSignature(column.getDataType())
.setCurrency(false);
setTypeInfo(builder, column.getDataType());
Expand Down Expand Up @@ -230,8 +235,8 @@ private static Time parseTime(String value, ZoneId localTimeZone) {
precision = fraction.length();
fractionValue = Long.parseLong(fraction);
}

long picosOfSecond = rescale(fractionValue, precision, 12); // maximum precision
// maximum precision
long picosOfSecond = rescale(fractionValue, precision, 12);
// We eventually truncate to millis, so truncate picos to nanos for consistency TODO (https://github.com/trinodb/trino/issues/6205) reconsider
int nanosOfSecond = toIntExact(picosOfSecond / PICOSECONDS_PER_NANOSECOND);
long epochMilli = ZonedDateTime.of(1970, 1, 1, hour, minute, second, nanosOfSecond, localTimeZone)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ private static int getQueryHash(String query_id) {
if (query_id.isEmpty()) {
return 0;
}
int hash = 202011; // Using the seed value
// Using the seed value
int hash = 202011;
for (char c : query_id.toCharArray()) {
hash = hash * 31 + c;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

public class DatabendColumnInfo {
private static final int VARBINARY_MAX = 1024 * 1024 * 1024;
private static final int TIME_ZONE_MAX = 40; // current longest time zone is 32
// current longest time zone is 32
private static final int TIME_ZONE_MAX = 40;
private static final int TIME_MAX = "HH:mm:ss.SSS".length();
private static final int TIME_WITH_TIME_ZONE_MAX = TIME_MAX + TIME_ZONE_MAX;
private static final int TIMESTAMP_MAX = "yyyy-MM-dd HH:mm:ss.SSS".length();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ private static String stringColumnEquals(String columnName, String value) {
protected static String stringColumnLike(String columnName, String pattern) {
if (pattern == null || pattern.isEmpty()) {
return null;
} if (Pattern.matches("^[^%]*$", pattern)) { // Checks if the string does not contain the %
}

// Checks if the string does not contain the %
if (Pattern.matches("^[^%]*$", pattern)) {
return stringColumnEquals(columnName, pattern);
}
StringBuilder filter = new StringBuilder();
Expand Down Expand Up @@ -997,7 +1000,8 @@ public ResultSet getTableTypes()
}};

private static StringBuilder columnMetaSqlTemplate() {
StringBuilder sql = new StringBuilder("SELECT table_catalog as TABLE_CAT" + // 1
// 1
StringBuilder sql = new StringBuilder("SELECT table_catalog as TABLE_CAT" +
", table_schema as TABLE_SCHEM" + // 2
", table_name as TABLE_NAME" + // 3
", column_name as COLUMN_NAME" + // 4
Expand All @@ -1022,38 +1026,57 @@ private ResultSet getColumnsMetaDataBySQL(String sql) throws SQLException {
try (ResultSet rs = select(sql)) {
while (rs.next()) {
List<Object> result = new ArrayList<>();
result.add(rs.getString(1));// TABLE_CAT
result.add(rs.getString(2));// TABLE_SCHEM
result.add(rs.getString(3));// TABLE_NAME
result.add(rs.getString(4));// COLUMN_NAME
// TABLE_CAT
result.add(rs.getString(1));
// TABLE_SCHEM
result.add(rs.getString(2));
// TABLE_NAME
result.add(rs.getString(3));
// COLUMN_NAME
result.add(rs.getString(4));
String originType = rs.getString(5);
DatabendRawType rowType = new DatabendRawType(originType);
DatabendDataType dataType = rowType.getDataType();
result.add(dataType.getSqlType());// DATA_TYPE
result.add(rowType.getType());// TYPE_NAME
result.add(rowType.getColumnSize());// COLUMN_SIZE
result.add(0);// BUFFER_LENGTH
result.add(rowType.getDecimalDigits());// DECIMAL_DIGITS
result.add(0);// NUM_PREC_RADIX
result.add(rs.getString(6));// COLUMN_NAME
result.add(rs.getObject(7));// REMARKS
result.add(rs.getString(8));// COLUMN_DEF
result.add(0);// SQL_DATA_TYPE
result.add(0);// SQL_DATETIME_SUB
// DATA_TYPE
result.add(dataType.getSqlType());
// TYPE_NAME
result.add(rowType.getType());
// COLUMN_SIZE
result.add(rowType.getColumnSize());
// BUFFER_LENGTH
result.add(0);
// DECIMAL_DIGITS
result.add(rowType.getDecimalDigits());
// NUM_PREC_RADIX
result.add(0);
// COLUMN_NAME
result.add(rs.getString(6));
// REMARKS
result.add(rs.getObject(7));
// COLUMN_DEF
result.add(rs.getString(8));
// SQL_DATA_TYPE
result.add(0);
// SQL_DATETIME_SUB
result.add(0);
// CHAR_OCTET_LENGTH (for char types the maximum number of bytes in the column)
if (dataType == DatabendDataType.STRING) {
result.add(dataType.getLength());
} else {
result.add(null);
}
result.add(rs.getString(9));// ORDINAL_POSITION
result.add(rs.getString(10));// IS_NULLABLE
// ORDINAL_POSITION
result.add(rs.getString(9));
// IS_NULLABLE
result.add(rs.getString(10));
result.add(null);
result.add(null);
result.add(null);
result.add(null);
result.add("NO");// IS_AUTOINCREMENT
result.add("NO");// IS_GENERATEDCOLUMN
// IS_AUTOINCREMENT
result.add("NO");
// IS_GENERATEDCOLUMN
result.add("NO");
results.add(result);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,8 @@ private static Map.Entry<DatabendNodes, Map<String, String>> parse(String url)
uris.clear();
uris.addAll(uriSet);
// Create DatabendNodes object
DatabendClientLoadBalancingPolicy policy = DatabendClientLoadBalancingPolicy.create(DatabendClientLoadBalancingPolicy.DISABLED); // You might want to make this configurable
// You might want to make this configurable
DatabendClientLoadBalancingPolicy policy = DatabendClientLoadBalancingPolicy.create(DatabendClientLoadBalancingPolicy.DISABLED);
DatabendNodes databendNodes = new DatabendNodes(uris, policy, uriPath, uriQuery, uriFragment, 5 * 60 * 1000);
return new AbstractMap.SimpleImmutableEntry<>(databendNodes, uriProperties);
} catch (URISyntaxException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@

import static com.databend.jdbc.ObjectCasts.*;
import static com.databend.jdbc.StatementUtil.replaceParameterMarksWithValues;
import static com.databend.jdbc.constant.DatabendConstant.BASE64_STR;
import static com.databend.jdbc.constant.DatabendConstant.*;
import static java.lang.String.format;
import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE;
import static java.time.format.DateTimeFormatter.ISO_LOCAL_TIME;
Expand Down Expand Up @@ -428,7 +428,7 @@ protected boolean execute(List<StatementInfoWrapper> statements) throws SQLExcep
try {
for (int i = 0; i < statements.size(); i++) {
String sql = statements.get(i).getSql();
if (sql.toLowerCase().contains("insert into") && !sql.toLowerCase().contains("select")) {
if (isBatchInsert(sql)) {
handleBatchInsert();
} else {
execute(sql);
Expand All @@ -442,6 +442,9 @@ protected boolean execute(List<StatementInfoWrapper> statements) throws SQLExcep
return true;
}

private boolean isBatchInsert(String sql) {
return sql.toLowerCase().contains(DATABEND_KEYWORDS_INSERT_INTO) && !sql.toLowerCase().contains(DATABEND_KEYWORDS_SELECT);
}
protected void handleBatchInsert() throws SQLException {
try {
addBatch();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static DatabendResultSet create(Statement statement, DatabendClient client, long
if (client.getServerVersion() != null) {
try {
serverVersion = Version.valueOf(client.getServerVersion());
} catch (Exception _e) {
} catch (Exception ignored) {

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ final boolean internalExecute(String sql, StageAttachment attachment) throws SQL
resultSet = DatabendResultSet.create(this, client, maxRows.get());
currentResult.set(resultSet);
if (isQueryStatement(sql)) {
currentUpdateCount = -1;// Always -1 when returning a ResultSet with query statement
// Always -1 when returning a ResultSet with query statement
currentUpdateCount = -1;
} else {
QueryResults results = client.getResults();
if (sql.toLowerCase().startsWith("update") || sql.toLowerCase().startsWith("delete")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
@AllArgsConstructor
@Value
public class ParamMarker {
int id; // Id / index of the param marker in the SQL statement
int position; // Position in the SQL subStatement
// Id / index of the param marker in the SQL statement
int id;
// Position in the SQL subStatement
int position;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.databend.jdbc;

public enum StatementType {
PARAM_SETTING, // SET
QUERY, // eg: SELECT, SHOW
NON_QUERY // eg: INSERT
// SET
PARAM_SETTING,
// eg: SELECT, SHOW
QUERY,
// eg: INSERT
NON_QUERY
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ public Pair<Optional<String>, Optional<String>> extractDbNameAndTableNamePairFro
} else if (StringUtils.startsWithIgnoreCase(withoutQuotes, "DESCRIBE")) {
from = Optional.of("tables");
} else if (StringUtils.startsWithIgnoreCase(withoutQuotes, "SHOW")) {
from = Optional.empty(); // Depends on the information requested
// Depends on the information requested
from = Optional.empty();
} else {
log.debug("Could not find table name for query {}. This may happen when there is no table.", cleanSql);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private static void parseParam(Map.Entry<String, String> s, StringBuilder sb) {
try {
DatabendParams p = DatabendParams.valueOf(s.getKey().toUpperCase(Locale.US));
needQuote = p.needQuote();
} catch (IllegalArgumentException e) {
} catch (IllegalArgumentException ignored) {
}
if (needQuote) {
sb.append(s.getKey()).append(" = ").append("'").append(s.getValue()).append("'").append(" ");
Expand Down Expand Up @@ -124,7 +124,8 @@ public enum DatabendParams {
ROW_TAG("ROW_TAG", String.class),
COMPRESSION("COMPRESSION", String.class),
SIZE_LIMIT("SIZE_LIMIT", Integer.class),
PURGE("PURGE", Boolean.class),// default false
// default false
PURGE("PURGE", Boolean.class),
FORCE("FORCE", Boolean.class),
// on error only support continue/abort without quote
ON_ERROR("ON_ERROR", null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,7 @@ private ResponseBody executeInternal(Request request, boolean shouldClose)
MILLISECONDS.sleep(attempts * 100);
}
catch (InterruptedException e) {
try {
}
finally {
Thread.currentThread().interrupt();
}
Thread.currentThread().interrupt();
throw new RuntimeException("StatementClient thread was interrupted");
}
}
Expand Down Expand Up @@ -326,7 +322,8 @@ public MediaType contentType()
@Override
public long contentLength()
{
return fileSize; // return the actual file size
// return the actual file size
return fileSize;
// return inputStream.available() == 0 ? -1 : inputStream.available();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
public class DatabendConstant {
public static final String ENABLE_STR = "enable";
public static final String BASE64_STR = "base64";
public static final String DATABEND_KEYWORDS_INSERT_INTO = "insert into";
public static final String DATABEND_KEYWORDS_SELECT = "select";
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ public DatabendConnectionPool(DatabendConnectionFactory factory, GenericObjectPo

public void testDemo() throws Exception {
GenericObjectPoolConfig<DatabendConnection> config = new GenericObjectPoolConfig<>();
config.setMaxTotal(10); // set max total connection
config.setMinIdle(2); // set min idle connection
// set max total connection
config.setMaxTotal(10);
// set min idle connection
config.setMinIdle(2);

Properties props = new Properties();
props.setProperty("database", "db3");
Expand Down
Loading