Skip to content

Commit

Permalink
Merge branch 'main' into v2_jwt_auth
Browse files Browse the repository at this point in the history
  • Loading branch information
chernser committed Dec 18, 2024
2 parents cb4c32d + 19172f6 commit da03749
Show file tree
Hide file tree
Showing 20 changed files with 865 additions and 318 deletions.
1 change: 1 addition & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

## Checklist
Delete items not relevant to your PR:
- [ ] Closes issue <!-- Link to an issue to close on merge. -->
- [ ] Unit and integration tests covering the common scenarios were added
- [ ] A human-readable description of the changes was provided to include in CHANGELOG
- [ ] For significant changes, documentation in https://github.com/ClickHouse/clickhouse-docs was updated with further explanations or tutorials
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
### New Features
- Added basic auth support for proxies. Now you can specify username/password when connecting via a proxy that requires it with HttpURLConnection and Apache HttpClient.

### Bug Fixes
- Fix for retrying on `ConnectTimeoutException`

## 0.7.1-patch1

### Bug Fixes
Expand Down
7 changes: 4 additions & 3 deletions client-v2/src/main/java/com/clickhouse/client/api/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.ConnectException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
Expand Down Expand Up @@ -1426,7 +1427,7 @@ public CompletableFuture<InsertResponse> insert(String tableName, List<?> data,
metrics.operationComplete();
metrics.setQueryId(queryId);
return new InsertResponse(metrics);
} catch ( NoHttpResponseException | ConnectionRequestTimeoutException | ConnectTimeoutException e) {
} catch (NoHttpResponseException | ConnectionRequestTimeoutException | ConnectTimeoutException | ConnectException e) {
lastException = httpClientHelper.wrapException("Insert request initiation failed", e);
if (httpClientHelper.shouldRetry(e, finalSettings.getAllSettings())) {
LOG.warn("Retrying", e);
Expand Down Expand Up @@ -1554,7 +1555,7 @@ public CompletableFuture<InsertResponse> insert(String tableName,
metrics.operationComplete();
metrics.setQueryId(queryId);
return new InsertResponse(metrics);
} catch ( NoHttpResponseException | ConnectionRequestTimeoutException | ConnectTimeoutException e) {
} catch (NoHttpResponseException | ConnectionRequestTimeoutException | ConnectTimeoutException | ConnectException e) {
lastException = httpClientHelper.wrapException("Insert request initiation failed", e);
if (httpClientHelper.shouldRetry(e, finalSettings.getAllSettings())) {
LOG.warn("Retrying", e);
Expand Down Expand Up @@ -1720,7 +1721,7 @@ public CompletableFuture<QueryResponse> query(String sqlQuery, Map<String, Objec

return new QueryResponse(httpResponse, finalSettings.getFormat(), finalSettings, metrics);

} catch ( NoHttpResponseException | ConnectionRequestTimeoutException | ConnectTimeoutException e) {
} catch (NoHttpResponseException | ConnectionRequestTimeoutException | ConnectTimeoutException | ConnectException e) {
lastException = httpClientHelper.wrapException("Query request initiation failed", e);
if (httpClientHelper.shouldRetry(e, finalSettings.getAllSettings())) {
LOG.warn("Retrying.", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private boolean readBlock() throws IOException {

@Override
public <T> T readValue(int colIndex) {
return (T) currentRecord.get(getSchema().indexToName(colIndex));
return (T) currentRecord.get(getSchema().columnIndexToName(colIndex));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ public <T> T readValue(int colIndex) {
if (colIndex < 1 || colIndex > getSchema().getColumns().size()) {
throw new ClientException("Column index out of bounds: " + colIndex);
}
colIndex = colIndex - 1;
return (T) currentRecord.get(getSchema().indexToName(colIndex));
return (T) currentRecord.get(getSchema().columnIndexToName(colIndex));
}

@Override
Expand Down Expand Up @@ -514,7 +513,7 @@ public boolean[] getBooleanArray(String colName) {

@Override
public boolean hasValue(int colIndex) {
return currentRecord.containsKey(getSchema().indexToName(colIndex - 1));
return currentRecord.containsKey(getSchema().columnIndexToName(colIndex));
}

@Override
Expand All @@ -525,47 +524,47 @@ public boolean hasValue(String colName) {

@Override
public byte getByte(int index) {
return getByte(schema.indexToName(index - 1 ));
return getByte(schema.columnIndexToName(index));
}

@Override
public short getShort(int index) {
return getShort(schema.indexToName(index - 1));
return getShort(schema.columnIndexToName(index));
}

@Override
public int getInteger(int index) {
return getInteger(schema.indexToName(index - 1));
return getInteger(schema.columnIndexToName(index));
}

@Override
public long getLong(int index) {
return getLong(schema.indexToName(index - 1));
return getLong(schema.columnIndexToName(index));
}

@Override
public float getFloat(int index) {
return getFloat(schema.indexToName(index - 1));
return getFloat(schema.columnIndexToName(index));
}

@Override
public double getDouble(int index) {
return getDouble(schema.indexToName(index - 1));
return getDouble(schema.columnIndexToName(index));
}

@Override
public boolean getBoolean(int index) {
return getBoolean(schema.indexToName(index - 1));
return getBoolean(schema.columnIndexToName(index));
}

@Override
public BigInteger getBigInteger(int index) {
return getBigInteger(schema.indexToName(index - 1));
return getBigInteger(schema.columnIndexToName(index));
}

@Override
public BigDecimal getBigDecimal(int index) {
return getBigDecimal(schema.indexToName(index - 1));
return getBigDecimal(schema.columnIndexToName(index));
}

@Override
Expand Down Expand Up @@ -620,37 +619,37 @@ public ClickHouseGeoMultiPolygonValue getGeoMultiPolygon(int index) {

@Override
public <T> List<T> getList(int index) {
return getList(schema.indexToName(index));
return getList(schema.columnIndexToName(index));
}

@Override
public byte[] getByteArray(int index) {
return getPrimitiveArray(schema.indexToName(index));
return getPrimitiveArray(schema.columnIndexToName(index));
}

@Override
public int[] getIntArray(int index) {
return getPrimitiveArray(schema.indexToName(index));
return getPrimitiveArray(schema.columnIndexToName(index));
}

@Override
public long[] getLongArray(int index) {
return getPrimitiveArray(schema.indexToName(index));
return getPrimitiveArray(schema.columnIndexToName(index));
}

@Override
public float[] getFloatArray(int index) {
return getPrimitiveArray(schema.indexToName(index));
return getPrimitiveArray(schema.columnIndexToName(index));
}

@Override
public double[] getDoubleArray(int index) {
return getPrimitiveArray(schema.indexToName(index));
return getPrimitiveArray(schema.columnIndexToName(index));
}

@Override
public boolean[] getBooleanArray(int index) {
return getPrimitiveArray(schema.indexToName(index));
return getPrimitiveArray(schema.columnIndexToName(index));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ public <T> T readValue(ClickHouseColumn column, Class<?> typeHint) throws IOExce
return (T) readTuple(column);
case Nothing:
return null;
// case SimpleAggregateFunction:
case SimpleAggregateFunction:
return (T) readValue(column.getNestedColumns().get(0));
case AggregateFunction:
return (T) readBitmap( column);
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public <T> T readValue(int colIndex) {
if (colIndex < 1 || colIndex > schema.getColumns().size()) {
throw new ClientException("Column index out of bounds: " + colIndex);
}
colIndex = colIndex - 1;
return (T) record.get(schema.indexToName(colIndex));

return (T) record.get(schema.columnIndexToName(colIndex));
}

public <T> T readValue(String colName) {
Expand Down Expand Up @@ -133,8 +133,7 @@ public BigDecimal getBigDecimal(String colName) {

@Override
public Instant getInstant(String colName) {
int colIndex = schema.nameToIndex(colName);
ClickHouseColumn column = schema.getColumns().get(colIndex);
ClickHouseColumn column = schema.getColumnByName(colName);
switch (column.getDataType()) {
case Date:
case Date32:
Expand All @@ -144,15 +143,13 @@ public Instant getInstant(String colName) {
case DateTime64:
LocalDateTime dateTime = readValue(colName);
return dateTime.toInstant(column.getTimeZone().toZoneId().getRules().getOffset(dateTime));

}
throw new ClientException("Column of type " + column.getDataType() + " cannot be converted to Instant");
}

@Override
public ZonedDateTime getZonedDateTime(String colName) {
int colIndex = schema.nameToIndex(colName);
ClickHouseColumn column = schema.getColumns().get(colIndex);
ClickHouseColumn column = schema.getColumnByName(colName);
switch (column.getDataType()) {
case DateTime:
case DateTime64:
Expand All @@ -166,8 +163,7 @@ public ZonedDateTime getZonedDateTime(String colName) {

@Override
public Duration getDuration(String colName) {
int colIndex = schema.nameToIndex(colName);
ClickHouseColumn column = schema.getColumns().get(colIndex);
ClickHouseColumn column = schema.getColumnByName(colName);
BigInteger value = readValue(colName);
try {
switch (column.getDataType()) {
Expand Down Expand Up @@ -288,7 +284,7 @@ public boolean[] getBooleanArray(String colName) {

@Override
public boolean hasValue(int colIndex) {
return record.containsKey(schema.indexToName(colIndex));
return record.containsKey(schema.columnIndexToName(colIndex));
}

@Override
Expand All @@ -298,37 +294,37 @@ public boolean hasValue(String colName) {

@Override
public byte getByte(int index) {
return getByte(schema.indexToName(index));
return getByte(schema.columnIndexToName(index));
}

@Override
public short getShort(int index) {
return getShort(schema.indexToName(index));
return getShort(schema.columnIndexToName(index));
}

@Override
public int getInteger(int index) {
return getInteger(schema.indexToName(index));
return getInteger(schema.columnIndexToName(index));
}

@Override
public long getLong(int index) {
return getLong(schema.indexToName(index));
return getLong(schema.columnIndexToName(index));
}

@Override
public float getFloat(int index) {
return getFloat(schema.indexToName(index));
return getFloat(schema.columnIndexToName(index));
}

@Override
public double getDouble(int index) {
return getDouble(schema.indexToName(index));
return getDouble(schema.columnIndexToName(index));
}

@Override
public boolean getBoolean(int index) {
return getBoolean(schema.indexToName(index));
return getBoolean(schema.columnIndexToName(index));
}

@Override
Expand Down Expand Up @@ -393,37 +389,37 @@ public ClickHouseGeoMultiPolygonValue getGeoMultiPolygon(int index) {

@Override
public <T> List<T> getList(int index) {
return getList(schema.indexToName(index));
return getList(schema.columnIndexToName(index));
}

@Override
public byte[] getByteArray(int index) {
return getPrimitiveArray(schema.indexToName(index));
return getPrimitiveArray(schema.columnIndexToName(index));
}

@Override
public int[] getIntArray(int index) {
return getPrimitiveArray(schema.indexToName(index));
return getPrimitiveArray(schema.columnIndexToName(index));
}

@Override
public long[] getLongArray(int index) {
return getPrimitiveArray(schema.indexToName(index));
return getPrimitiveArray(schema.columnIndexToName(index));
}

@Override
public float[] getFloatArray(int index) {
return getPrimitiveArray(schema.indexToName(index));
return getPrimitiveArray(schema.columnIndexToName(index));
}

@Override
public double[] getDoubleArray(int index) {
return getPrimitiveArray(schema.indexToName(index));
return getPrimitiveArray(schema.columnIndexToName(index));
}

@Override
public boolean[] getBooleanArray(int index) {
return getPrimitiveArray(schema.indexToName(index));
return getPrimitiveArray(schema.columnIndexToName(index));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ public boolean shouldRetry(Exception ex, Map<String, Object> requestSettings) {
return retryCauses.contains(ClientFaultCause.NoHttpResponse);
}

if (ex instanceof ConnectException) {
if (ex instanceof ConnectException || ex instanceof ConnectTimeoutException) {
return retryCauses.contains(ClientFaultCause.ConnectTimeout);
}

Expand All @@ -602,6 +602,7 @@ public boolean shouldRetry(Exception ex, Map<String, Object> requestSettings) {
// ClientException will be also wrapped
public ClientException wrapException(String message, Exception cause) {
if (cause instanceof ConnectionRequestTimeoutException ||
cause instanceof NoHttpResponseException ||
cause instanceof ConnectTimeoutException ||
cause instanceof ConnectException) {
return new ConnectionInitiationException(message, cause);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ public ClickHouseColumn getColumnByName(String name) {
return columns.get(nameToIndex(name));
}

/**
* Takes absolute index (starting from 0) and returns corresponding column.
*
* @param index - column index starting from 0
* @return - column name
*/
public String indexToName(int index) {
try {
return columns.get(index).getColumnName();
Expand All @@ -99,6 +105,17 @@ public String indexToName(int index) {
}
}

/**
* Takes absolute index (starting from 1) and return corresponding column.
* Equals to {@code indexToName(index - 1}.
*
* @param index - column index starting from 1
* @return - column name.
*/
public String columnIndexToName(int index) {
return indexToName(index - 1);
}

public int nameToIndex(String name) {
Integer index = colIndex.get(name);
if (index == null) {
Expand Down
Loading

0 comments on commit da03749

Please sign in to comment.