Skip to content

Commit 80b5972

Browse files
committed
implemented writing major data types. fixed tests and date/datetimes
1 parent 7ae8f5d commit 80b5972

File tree

8 files changed

+493
-106
lines changed

8 files changed

+493
-106
lines changed

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

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader;
1111
import com.clickhouse.client.api.data_formats.NativeFormatReader;
1212
import com.clickhouse.client.api.data_formats.RowBinaryFormatReader;
13+
import com.clickhouse.client.api.data_formats.RowBinaryFormatSerializer;
1314
import com.clickhouse.client.api.data_formats.RowBinaryWithNamesAndTypesFormatReader;
1415
import com.clickhouse.client.api.data_formats.RowBinaryWithNamesFormatReader;
1516
import com.clickhouse.client.api.data_formats.internal.BinaryStreamReader;
@@ -43,7 +44,6 @@
4344
import com.clickhouse.client.api.query.Records;
4445
import com.clickhouse.client.config.ClickHouseClientOption;
4546
import com.clickhouse.data.ClickHouseColumn;
46-
import com.clickhouse.data.ClickHouseDataType;
4747
import com.clickhouse.data.ClickHouseFormat;
4848
import org.apache.hc.client5.http.ConnectTimeoutException;
4949
import org.apache.hc.core5.concurrent.DefaultThreadFactory;
@@ -1229,45 +1229,9 @@ public synchronized void register(Class<?> clazz, TableSchema schema) {
12291229
schemaSerializers.put(column.getColumnName(), (obj, stream) -> {
12301230
Object value = getterMethod.invoke(obj);
12311231

1232-
if (defaultsSupport) {
1233-
if (value != null) {//Because we now support defaults, we have to send nonNull
1234-
SerializerUtils.writeNonNull(stream);//Write 0 for no default
1235-
1236-
if (column.isNullable()) {//If the column is nullable
1237-
SerializerUtils.writeNonNull(stream);//Write 0 for not null
1238-
}
1239-
} else {//So if the object is null
1240-
if (column.hasDefault()) {
1241-
SerializerUtils.writeNull(stream);//Send 1 for default
1242-
return;
1243-
} else if (column.isNullable()) {//And the column is nullable
1244-
SerializerUtils.writeNonNull(stream);
1245-
SerializerUtils.writeNull(stream);//Then we send null, write 1
1246-
return;//And we're done
1247-
} else if (column.getDataType() == ClickHouseDataType.Array) {//If the column is an array
1248-
SerializerUtils.writeNonNull(stream);//Then we send nonNull
1249-
} else {
1250-
throw new IllegalArgumentException(String.format("An attempt to write null into not nullable column '%s'", column.getColumnName()));
1251-
}
1252-
}
1253-
} else {
1254-
if (column.isNullable()) {
1255-
if (value == null) {
1256-
SerializerUtils.writeNull(stream);
1257-
return;
1258-
}
1259-
SerializerUtils.writeNonNull(stream);
1260-
} else if (value == null) {
1261-
if (column.getDataType() == ClickHouseDataType.Array) {
1262-
SerializerUtils.writeNonNull(stream);
1263-
} else {
1264-
throw new IllegalArgumentException(String.format("An attempt to write null into not nullable column '%s'", column.getColumnName()));
1265-
}
1266-
}
1232+
if (RowBinaryFormatSerializer.writeValuePreamble(stream, defaultsSupport, column, value)) {
1233+
SerializerUtils.serializeData(stream, value, column);
12671234
}
1268-
1269-
//Handle the different types
1270-
SerializerUtils.serializeData(stream, value, column);
12711235
});
12721236
} else {
12731237
LOG.warn("No getter method found for column: {}", propertyName);

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

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
package com.clickhouse.client.api.data_formats;
2+
3+
import com.clickhouse.client.api.data_formats.internal.SerializerUtils;
4+
import com.clickhouse.data.ClickHouseColumn;
5+
import com.clickhouse.data.ClickHouseDataType;
6+
import com.clickhouse.data.ClickHouseFormat;
7+
import com.clickhouse.data.format.BinaryStreamUtils;
8+
9+
import java.io.IOException;
10+
import java.io.OutputStream;
11+
import java.math.BigDecimal;
12+
import java.math.BigInteger;
13+
import java.net.Inet4Address;
14+
import java.net.Inet6Address;
15+
import java.time.ZonedDateTime;
16+
17+
/**
18+
* This class is intended to be used for very precise data serializations.
19+
* It is an auxiliary class to handle only low level write operations.
20+
*/
21+
public class RowBinaryFormatSerializer {
22+
23+
private OutputStream out;
24+
25+
public RowBinaryFormatSerializer(OutputStream out) {
26+
this.out = out;
27+
}
28+
29+
public void writeNull() throws IOException {
30+
SerializerUtils.writeNull(out);
31+
}
32+
33+
public void writeNotNull() throws IOException {
34+
SerializerUtils.writeNonNull(out);
35+
}
36+
37+
public void writeDefault() throws IOException {
38+
SerializerUtils.writeBoolean(out, true);
39+
}
40+
41+
public void writeInt8(byte value) throws IOException {
42+
BinaryStreamUtils.writeInt8(out, value);
43+
}
44+
45+
public void writeUInt8(short value) throws IOException {
46+
BinaryStreamUtils.writeUnsignedInt8(out, value);
47+
}
48+
49+
public void writeInt16(short value) throws IOException {
50+
BinaryStreamUtils.writeInt16(out, value);
51+
}
52+
53+
public void writeUInt16(int value) throws IOException {
54+
BinaryStreamUtils.writeUnsignedInt16(out, value);
55+
}
56+
57+
public void writeInt32(int value) throws IOException {
58+
BinaryStreamUtils.writeInt32(out, value);
59+
}
60+
61+
public void writeUInt32(long value) throws IOException {
62+
BinaryStreamUtils.writeUnsignedInt32(out, value);
63+
}
64+
65+
public void writeInt64(long value) throws IOException {
66+
BinaryStreamUtils.writeInt64(out, value);
67+
}
68+
69+
public void writeUInt64(BigInteger value) throws IOException {
70+
BinaryStreamUtils.writeUnsignedInt64(out, value);
71+
}
72+
73+
public void writeInt128(BigInteger value) throws IOException {
74+
BinaryStreamUtils.writeInt128(out, value);
75+
}
76+
77+
public void writeUInt128(BigInteger value) throws IOException {
78+
BinaryStreamUtils.writeUnsignedInt128(out, value);
79+
}
80+
81+
public void writeInt256(BigInteger value) throws IOException {
82+
BinaryStreamUtils.writeInt256(out, value);
83+
}
84+
85+
public void writeUInt256(BigInteger value) throws IOException {
86+
BinaryStreamUtils.writeUnsignedInt128(out, value);
87+
}
88+
89+
public void writeBool(boolean value) throws IOException {
90+
BinaryStreamUtils.writeBoolean(out, value);
91+
}
92+
93+
public void writeFloat32(float value) throws IOException {
94+
BinaryStreamUtils.writeFloat32(out, value);
95+
}
96+
97+
public void writeFloat64(double value) throws IOException {
98+
BinaryStreamUtils.writeFloat64(out, value);
99+
}
100+
101+
public void writeDecimal(BigDecimal value, int precision, int scale) throws IOException {
102+
BinaryStreamUtils.writeDecimal(out, value, precision, scale);
103+
}
104+
105+
public void writeDecimal32(BigDecimal value, int scale) throws IOException {
106+
BinaryStreamUtils.writeDecimal32(out, value, scale);
107+
}
108+
109+
public void writeDecimal64(BigDecimal value, int scale) throws IOException {
110+
BinaryStreamUtils.writeDecimal64(out, value, scale);
111+
}
112+
113+
public void writeDecimal128(BigDecimal value, int scale) throws IOException {
114+
BinaryStreamUtils.writeDecimal128(out, value, scale);
115+
}
116+
117+
public void writeDecimal256(BigDecimal value, int scale) throws IOException {
118+
BinaryStreamUtils.writeDecimal256(out, value, scale);
119+
}
120+
121+
public void writeString(String value) throws IOException {
122+
BinaryStreamUtils.writeString(out, value);
123+
}
124+
125+
public void writeFixedString(String value, int len) throws IOException {
126+
BinaryStreamUtils.writeFixedString(out, value, len);
127+
}
128+
129+
public void writeDate(ZonedDateTime value) throws IOException {
130+
131+
}
132+
133+
public void writeDate32(ZonedDateTime value) throws IOException {
134+
135+
}
136+
137+
public void writeDateTime(ZonedDateTime value) throws IOException {
138+
139+
}
140+
141+
public void writeDateTime64(ZonedDateTime value) throws IOException {
142+
143+
}
144+
145+
public void writeEnum8(byte value) throws IOException {
146+
BinaryStreamUtils.writeEnum8(out, value);
147+
}
148+
149+
public void writeEnum16(short value) throws IOException {
150+
BinaryStreamUtils.writeEnum16(out, value);
151+
}
152+
153+
public void writeUUID(long leastSignificantBits, long mostSignificantBits) throws IOException {
154+
BinaryStreamUtils.writeInt64(out, mostSignificantBits);
155+
BinaryStreamUtils.writeInt64(out, leastSignificantBits);
156+
157+
}
158+
159+
public void writeIPV4Address(Inet4Address value) throws IOException {
160+
BinaryStreamUtils.writeInet4Address(out, value);
161+
}
162+
163+
public void writeIPV6Address(Inet6Address value) throws IOException {
164+
BinaryStreamUtils.writeInet6Address(out, value);
165+
}
166+
167+
public void writeArray() throws IOException {
168+
169+
}
170+
171+
public void writeTuple() throws IOException {
172+
173+
}
174+
175+
public void writeMap() throws IOException {
176+
177+
}
178+
179+
public void writeAggregationFunction() throws IOException {
180+
181+
}
182+
183+
public void writeSimpleFunction() throws IOException {
184+
185+
}
186+
187+
public void writeGeoPoint() throws IOException {
188+
189+
}
190+
191+
public void writeGeoRing() throws IOException {
192+
193+
}
194+
195+
public void writeGeoLineString() throws IOException {
196+
197+
}
198+
199+
public void writeGeoMultiLineString() throws IOException {
200+
201+
}
202+
203+
public void writeGeoPolygon() throws IOException {
204+
205+
}
206+
207+
public void writeGeoMultiPolygon() throws IOException {
208+
209+
}
210+
211+
public void writeNested() throws IOException {
212+
213+
}
214+
215+
public static boolean writeValuePreamble(OutputStream out, boolean defaultsSupport, ClickHouseColumn column, Object value) throws IOException {
216+
return writeValuePreamble(out, defaultsSupport, value, column.isNullable(), column.getDataType(), column.hasDefault(), column.getColumnName());
217+
}
218+
219+
public static boolean writeValuePreamble(OutputStream out, boolean defaultsSupport, Object value, boolean isNullable, ClickHouseDataType dataType, boolean hasDefault, String column) throws IOException {
220+
if (defaultsSupport) {
221+
if (value != null) {//Because we now support defaults, we have to send nonNull
222+
SerializerUtils.writeNonNull(out);//Write 0 for no default
223+
224+
if (isNullable) {//If the column is nullable
225+
SerializerUtils.writeNonNull(out);//Write 0 for not null
226+
}
227+
} else {//So if the object is null
228+
if (hasDefault) {
229+
SerializerUtils.writeNull(out);//Send 1 for default
230+
return false;
231+
} else if (isNullable) {//And the column is nullable
232+
SerializerUtils.writeNonNull(out);
233+
SerializerUtils.writeNull(out);//Then we send null, write 1
234+
return false;//And we're done
235+
} else if (dataType == ClickHouseDataType.Array) {//If the column is an array
236+
SerializerUtils.writeNonNull(out);//Then we send nonNull
237+
} else {
238+
throw new IllegalArgumentException(String.format("An attempt to write null into not nullable column '%s'", column));
239+
}
240+
}
241+
} else {
242+
if (isNullable) {
243+
if (value == null) {
244+
SerializerUtils.writeNull(out);
245+
return false;
246+
}
247+
SerializerUtils.writeNonNull(out);
248+
} else if (value == null) {
249+
if (dataType == ClickHouseDataType.Array) {
250+
SerializerUtils.writeNonNull(out);
251+
} else {
252+
throw new IllegalArgumentException(String.format("An attempt to write null into not nullable column '%s'", column));
253+
}
254+
}
255+
}
256+
257+
return true;
258+
}
259+
}

0 commit comments

Comments
 (0)