Skip to content

Commit 2539301

Browse files
committed
Merge remote-tracking branch 'origin/main' into bitmap-serialize
2 parents 13e8b89 + f9b0f7a commit 2539301

35 files changed

+2557
-316
lines changed

.github/workflows/analysis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,6 @@ jobs:
9595
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
9696
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
9797
run: |
98-
mvn --batch-mode -DclickhouseVersion=$PREFERRED_LTS_VERSION \
98+
mvn --batch-mode -DclickhouseVersion=$PREFERRED_LTS_VERSION -Dclient.tests.useNewImplementation=true \
9999
-Panalysis verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
100100
continue-on-error: true

client-v2/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@
8585
<scope>provided</scope>
8686
</dependency>
8787

88+
<!-- https://mvnrepository.com/artifact/org.ow2.asm/asm -->
89+
<dependency>
90+
<groupId>org.ow2.asm</groupId>
91+
<artifactId>asm</artifactId>
92+
<version>9.7</version>
93+
</dependency>
94+
95+
8896
<!-- Test dependencies -->
8997
<dependency>
9098
<groupId>${project.parent.groupId}</groupId>

client-v2/src/main/java/com/clickhouse/client/api/Client.java

Lines changed: 191 additions & 63 deletions
Large diffs are not rendered by default.

client-v2/src/main/java/com/clickhouse/client/api/data_formats/ClickHouseBinaryFormatReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.util.Map;
2020
import java.util.UUID;
2121

22-
public interface ClickHouseBinaryFormatReader {
22+
public interface ClickHouseBinaryFormatReader extends AutoCloseable {
2323

2424
/**
2525
* Reads a single value from the stream.

client-v2/src/main/java/com/clickhouse/client/api/data_formats/NativeFormatReader.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,9 @@ public class NativeFormatReader extends AbstractBinaryFormatReader {
2323

2424
private int blockRowIndex;
2525

26-
public NativeFormatReader(InputStream inputStream) {
27-
this(inputStream, null);
28-
}
29-
30-
public NativeFormatReader(InputStream inputStream, QuerySettings settings) {
31-
super(inputStream, settings, null);
26+
public NativeFormatReader(InputStream inputStream, QuerySettings settings,
27+
BinaryStreamReader.ByteBufferAllocator byteBufferAllocator) {
28+
super(inputStream, settings, null, byteBufferAllocator);
3229
readNextRecord();
3330
}
3431

client-v2/src/main/java/com/clickhouse/client/api/data_formats/RowBinaryFormatReader.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.clickhouse.client.api.data_formats;
22

33
import com.clickhouse.client.api.data_formats.internal.AbstractBinaryFormatReader;
4+
import com.clickhouse.client.api.data_formats.internal.BinaryStreamReader;
45
import com.clickhouse.client.api.metadata.TableSchema;
56
import com.clickhouse.client.api.query.QuerySettings;
67
import com.clickhouse.data.ClickHouseColumn;
@@ -12,12 +13,9 @@
1213

1314
public class RowBinaryFormatReader extends AbstractBinaryFormatReader {
1415

15-
public RowBinaryFormatReader(InputStream inputStream, TableSchema schema) {
16-
this(inputStream, null, schema);
17-
}
18-
19-
public RowBinaryFormatReader(InputStream inputStream, QuerySettings querySettings, TableSchema schema) {
20-
super(inputStream, querySettings, schema);
16+
public RowBinaryFormatReader(InputStream inputStream, QuerySettings querySettings, TableSchema schema,
17+
BinaryStreamReader.ByteBufferAllocator byteBufferAllocator) {
18+
super(inputStream, querySettings, schema, byteBufferAllocator);
2119
readNextRecord();
2220
}
2321

client-v2/src/main/java/com/clickhouse/client/api/data_formats/RowBinaryWithNamesAndTypesFormatReader.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.clickhouse.client.api.data_formats.internal.BinaryStreamReader;
66
import com.clickhouse.client.api.metadata.TableSchema;
77
import com.clickhouse.client.api.query.QuerySettings;
8-
import com.clickhouse.data.ClickHouseColumn;
98

109
import java.io.EOFException;
1110
import java.io.IOException;
@@ -17,8 +16,9 @@
1716

1817
public class RowBinaryWithNamesAndTypesFormatReader extends AbstractBinaryFormatReader implements Iterator<Map<String, Object>> {
1918

20-
public RowBinaryWithNamesAndTypesFormatReader(InputStream inputStream, QuerySettings querySettings) {
21-
super(inputStream, querySettings, null);
19+
public RowBinaryWithNamesAndTypesFormatReader(InputStream inputStream, QuerySettings querySettings,
20+
BinaryStreamReader.ByteBufferAllocator byteBufferAllocator) {
21+
super(inputStream, querySettings, null, byteBufferAllocator);
2222
readSchema();
2323
}
2424

client-v2/src/main/java/com/clickhouse/client/api/data_formats/RowBinaryWithNamesFormatReader.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,21 @@
44
import com.clickhouse.client.api.data_formats.internal.BinaryStreamReader;
55
import com.clickhouse.client.api.metadata.TableSchema;
66
import com.clickhouse.client.api.query.QuerySettings;
7-
import com.clickhouse.data.ClickHouseColumn;
87

98
import java.io.EOFException;
109
import java.io.IOException;
1110
import java.io.InputStream;
1211
import java.util.ArrayList;
1312
import java.util.Collections;
1413
import java.util.List;
15-
import java.util.Map;
1614

1715
public class RowBinaryWithNamesFormatReader extends AbstractBinaryFormatReader {
1816

1917
private List<String> columns = null;
2018

21-
public RowBinaryWithNamesFormatReader(InputStream inputStream, TableSchema schema) {
22-
this(inputStream, null, schema);
23-
readNextRecord();
24-
}
25-
26-
public RowBinaryWithNamesFormatReader(InputStream inputStream, QuerySettings querySettings, TableSchema schema) {
27-
super(inputStream, querySettings, schema);
19+
public RowBinaryWithNamesFormatReader(InputStream inputStream, QuerySettings querySettings, TableSchema schema,
20+
BinaryStreamReader.ByteBufferAllocator byteBufferAllocator) {
21+
super(inputStream, querySettings, schema, byteBufferAllocator);
2822
int nCol = 0;
2923
try {
3024
nCol = BinaryStreamReader.readVarInt(input);

client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/AbstractBinaryFormatReader.java

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
import com.clickhouse.client.api.ClientException;
44
import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader;
5-
import com.clickhouse.client.api.internal.MapUtils;
65
import com.clickhouse.client.api.metadata.TableSchema;
76
import com.clickhouse.client.api.query.NullValueException;
7+
import com.clickhouse.client.api.query.POJOSetter;
88
import com.clickhouse.client.api.query.QuerySettings;
99
import com.clickhouse.client.config.ClickHouseClientOption;
1010
import com.clickhouse.data.ClickHouseColumn;
11-
import com.clickhouse.data.value.ClickHouseArrayValue;
1211
import com.clickhouse.data.value.ClickHouseGeoMultiPolygonValue;
1312
import com.clickhouse.data.value.ClickHouseGeoPointValue;
1413
import com.clickhouse.data.value.ClickHouseGeoPolygonValue;
@@ -30,17 +29,14 @@
3029
import java.time.ZoneOffset;
3130
import java.time.ZonedDateTime;
3231
import java.time.temporal.ChronoUnit;
33-
import java.util.ArrayList;
3432
import java.util.Collections;
3533
import java.util.HashMap;
3634
import java.util.List;
3735
import java.util.Map;
38-
import java.util.NoSuchElementException;
3936
import java.util.TimeZone;
4037
import java.util.UUID;
4138
import java.util.concurrent.ConcurrentHashMap;
4239
import java.util.concurrent.atomic.AtomicBoolean;
43-
import java.util.function.Function;
4440

4541
public abstract class AbstractBinaryFormatReader implements ClickHouseBinaryFormatReader {
4642

@@ -58,7 +54,8 @@ public abstract class AbstractBinaryFormatReader implements ClickHouseBinaryForm
5854

5955
private volatile boolean hasNext = true;
6056

61-
protected AbstractBinaryFormatReader(InputStream inputStream, QuerySettings querySettings, TableSchema schema) {
57+
protected AbstractBinaryFormatReader(InputStream inputStream, QuerySettings querySettings, TableSchema schema,
58+
BinaryStreamReader.ByteBufferAllocator byteBufferAllocator) {
6259
this.input = inputStream;
6360
this.settings = querySettings == null ? Collections.emptyMap() : new HashMap<>(querySettings.getAllSettings());
6461
boolean useServerTimeZone = (boolean) this.settings.get(ClickHouseClientOption.USE_SERVER_TIME_ZONE.getKey());
@@ -67,7 +64,7 @@ protected AbstractBinaryFormatReader(InputStream inputStream, QuerySettings quer
6764
if (timeZone == null) {
6865
throw new ClientException("Time zone is not set. (useServerTimezone:" + useServerTimeZone + ")");
6966
}
70-
this.binaryStreamReader = new BinaryStreamReader(inputStream, timeZone, LOG);
67+
this.binaryStreamReader = new BinaryStreamReader(inputStream, timeZone, LOG, byteBufferAllocator);
7168
setSchema(schema);
7269
}
7370

@@ -76,6 +73,32 @@ protected AbstractBinaryFormatReader(InputStream inputStream, QuerySettings quer
7673

7774
protected AtomicBoolean nextRecordEmpty = new AtomicBoolean(true);
7875

76+
public boolean readToPOJO(Map<String, POJOSetter> deserializers, Object obj ) throws IOException {
77+
boolean firstColumn = true;
78+
79+
for (ClickHouseColumn column : columns) {
80+
try {
81+
Object val = binaryStreamReader.readValue(column);
82+
if (val != null) {
83+
POJOSetter deserializer = deserializers.get(column.getColumnName());
84+
if (deserializer != null) {
85+
deserializer.setValue(obj, val);
86+
}
87+
}
88+
firstColumn = false;
89+
} catch (EOFException e) {
90+
if (firstColumn) {
91+
endReached();
92+
return false;
93+
}
94+
throw e;
95+
} catch (Exception e) {
96+
throw new ClientException("Failed to put value of '" + column.getColumnName() + "' into POJO", e);
97+
}
98+
}
99+
return true;
100+
}
101+
79102
/**
80103
* It is still internal method and should be used with care.
81104
* Usually this method is called to read next record into internal object and affects hasNext() method.
@@ -133,12 +156,12 @@ protected void readNextRecord() {
133156
try {
134157
nextRecordEmpty.set(true);
135158
if (!readRecord(nextRecord)) {
136-
hasNext = false;
159+
endReached();
137160
} else {
138161
nextRecordEmpty.compareAndSet(true, false);
139162
}
140163
} catch (IOException e) {
141-
hasNext = false;
164+
endReached();
142165
throw new ClientException("Failed to read next row", e);
143166
}
144167
}
@@ -165,7 +188,7 @@ public Map<String, Object> next() {
165188
return null;
166189
}
167190
} catch (IOException e) {
168-
hasNext = false;
191+
endReached();
169192
throw new ClientException("Failed to read row", e);
170193
}
171194
}
@@ -621,4 +644,9 @@ public LocalDateTime getLocalDateTime(int index) {
621644
}
622645
return (LocalDateTime) value;
623646
}
647+
648+
@Override
649+
public void close() throws Exception {
650+
input.close();
651+
}
624652
}

0 commit comments

Comments
 (0)