From 4789620d30a60be09b194f8317a0443ab172e5af Mon Sep 17 00:00:00 2001 From: Cheng Pan <379377944@qq.com> Date: Sat, 12 Dec 2020 00:45:09 +0800 Subject: [PATCH] refactor IDataType --- .../housepower/jdbc/data/IDataType.java | 57 +++++++++++++------ .../jdbc/data/type/DataTypeDate.java | 6 +- .../jdbc/data/type/DataTypeFloat32.java | 8 +-- .../jdbc/data/type/DataTypeFloat64.java | 8 +-- .../jdbc/data/type/DataTypeIPv4.java | 8 +-- .../jdbc/data/type/DataTypeInt16.java | 8 +-- .../jdbc/data/type/DataTypeInt32.java | 8 +-- .../jdbc/data/type/DataTypeInt64.java | 8 +-- .../jdbc/data/type/DataTypeInt8.java | 8 +-- .../jdbc/data/type/DataTypeUUID.java | 8 +-- .../jdbc/data/type/complex/DataTypeArray.java | 19 +++---- .../data/type/complex/DataTypeDateTime.java | 8 +-- .../data/type/complex/DataTypeDateTime64.java | 8 +-- .../data/type/complex/DataTypeDecimal.java | 8 +-- .../data/type/complex/DataTypeEnum16.java | 8 +-- .../jdbc/data/type/complex/DataTypeEnum8.java | 8 +-- .../type/complex/DataTypeFixedString.java | 8 +-- .../data/type/complex/DataTypeNullable.java | 18 +++--- .../data/type/complex/DataTypeString.java | 6 +- .../jdbc/data/type/complex/DataTypeTuple.java | 16 +++--- 20 files changed, 128 insertions(+), 106 deletions(-) diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/IDataType.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/IDataType.java index b8407e3a..843395d3 100644 --- a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/IDataType.java +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/IDataType.java @@ -21,18 +21,27 @@ import java.io.IOException; import java.sql.SQLException; -// It would be nice if we introduce a Generic Type, `IDataType`, then we can avoid using `Object` and type cast. -// Unfortunately Java not support unsigned number, UInt8(u_byte) must be represented by Int16(short), which will -// break the Generic Type constriction and cause compile failed. -public interface IDataType { +/** + * IDataType is responsible for: + * 1. represent data type attributions + * 2. serialize and deserialize + * 3. parse from sql text + * 4. cast between inner type and other types + * 5. format to string + * @param inner data type, 1:1 map to ClickHouse data type + */ +public interface IDataType { + // 1. represent data type attributions String name(); - int sqlTypeId(); + default String[] getAliases() { + return new String[0]; + } - Object defaultValue(); + int sqlTypeId(); - Class javaTypeClass(); + Class javaTypeClass(); boolean nullable(); @@ -40,21 +49,35 @@ public interface IDataType { int getScale(); - Object deserializeTextQuoted(SQLLexer lexer) throws SQLException; + T defaultValue(); - Object deserializeBinary(BinaryDeserializer deserializer) throws SQLException, IOException; + // 2. serialize and deserialize + void serializeBinary(T data, BinarySerializer serializer) throws SQLException, IOException; - void serializeBinary(Object data, BinarySerializer serializer) throws SQLException, IOException; - - default void serializeBinaryBulk(Object[] data, BinarySerializer serializer) throws SQLException, IOException { - for (Object d : data) { - serializeBinary(d, serializer); + default void serializeBinaryBulk(T[] values, BinarySerializer serializer) throws SQLException, IOException { + for (T value : values) { + serializeBinary(value, serializer); } } - Object[] deserializeBinaryBulk(int rows, BinaryDeserializer deserializer) throws SQLException, IOException; + T deserializeBinary(BinaryDeserializer deserializer) throws SQLException, IOException; - default String[] getAliases() { - return new String[0]; + T[] deserializeBinaryBulk(int rowCnt, BinaryDeserializer deserializer) throws SQLException, IOException; + + // 3. parse from sql text + T deserializeTextQuoted(SQLLexer lexer) throws SQLException; + + // 4. cast between inner type and other types + default T castFrom(O other) { + return javaTypeClass().cast(other); + } + + default O castTo(T inner, Class castToClazz) { + return castToClazz.cast(inner); + } + + // 5. format to string + default String format(O value) { + return castTo(castFrom(value), String.class); } } diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeDate.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeDate.java index a920444c..a8039b51 100644 --- a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeDate.java +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeDate.java @@ -82,9 +82,9 @@ public Object deserializeBinary(BinaryDeserializer deserializer) throws IOExcept } @Override - public Object[] deserializeBinaryBulk(int rows, BinaryDeserializer deserializer) throws IOException { - Date[] data = new Date[rows]; - for (int row = 0; row < rows; row++) { + public Object[] deserializeBinaryBulk(int rowCnt, BinaryDeserializer deserializer) throws IOException { + Date[] data = new Date[rowCnt]; + for (int row = 0; row < rowCnt; row++) { short daysSinceEpoch = deserializer.readShort(); data[row] = Date.valueOf(LocalDate.ofEpochDay(daysSinceEpoch)); } diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeFloat32.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeFloat32.java index dfaf374d..35ae002f 100644 --- a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeFloat32.java +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeFloat32.java @@ -73,10 +73,10 @@ public Object deserializeBinary(BinaryDeserializer deserializer) throws IOExcept } @Override - public Object[] deserializeBinaryBulk(int rows, BinaryDeserializer deserializer) throws IOException { - Float[] data = new Float[rows]; - for (int row = 0; row < rows; row++) { - data[row] = deserializer.readFloat(); + public Object[] deserializeBinaryBulk(int rowCnt, BinaryDeserializer deserializer) throws IOException { + Float[] data = new Float[rowCnt]; + for (int i = 0; i < rowCnt; i++) { + data[i] = deserializer.readFloat(); } return data; } diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeFloat64.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeFloat64.java index 2de20ff5..931f52c8 100644 --- a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeFloat64.java +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeFloat64.java @@ -73,10 +73,10 @@ public Double deserializeBinary(BinaryDeserializer deserializer) throws SQLExcep } @Override - public Double[] deserializeBinaryBulk(int rows, BinaryDeserializer deserializer) throws IOException { - Double[] data = new Double[rows]; - for (int row = 0; row < rows; row++) { - data[row] = deserializer.readDouble(); + public Double[] deserializeBinaryBulk(int rowCnt, BinaryDeserializer deserializer) throws IOException { + Double[] data = new Double[rowCnt]; + for (int i = 0; i < rowCnt; i++) { + data[i] = deserializer.readDouble(); } return data; } diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeIPv4.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeIPv4.java index a71bcc05..a0070ea5 100644 --- a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeIPv4.java +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeIPv4.java @@ -73,10 +73,10 @@ public Object deserializeBinary(BinaryDeserializer deserializer) throws SQLExcep } @Override - public Object[] deserializeBinaryBulk(int rows, BinaryDeserializer deserializer) throws SQLException, IOException { - Object[] data = new Object[rows]; - for (int row = 0; row < rows; row++) { - data[row] = this.deserializeBinary(deserializer); + public Object[] deserializeBinaryBulk(int rowCnt, BinaryDeserializer deserializer) throws SQLException, IOException { + Object[] data = new Object[rowCnt]; + for (int i = 0; i < rowCnt; i++) { + data[i] = this.deserializeBinary(deserializer); } return data; } diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeInt16.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeInt16.java index 7e0eafe2..36aec2c8 100644 --- a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeInt16.java +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeInt16.java @@ -84,10 +84,10 @@ public Object deserializeBinary(BinaryDeserializer deserializer) throws SQLExcep } @Override - public Object[] deserializeBinaryBulk(int rows, BinaryDeserializer deserializer) throws SQLException, IOException { - Object[] data = new Object[rows]; - for (int row = 0; row < rows; row++) { - data[row] = this.deserializeBinary(deserializer); + public Object[] deserializeBinaryBulk(int rowCnt, BinaryDeserializer deserializer) throws SQLException, IOException { + Object[] data = new Object[rowCnt]; + for (int i = 0; i < rowCnt; i++) { + data[i] = this.deserializeBinary(deserializer); } return data; } diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeInt32.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeInt32.java index fd4e401c..1c437e92 100644 --- a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeInt32.java +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeInt32.java @@ -84,11 +84,11 @@ public Object deserializeBinary(BinaryDeserializer deserializer) throws SQLExcep } @Override - public Object[] deserializeBinaryBulk(int rows, BinaryDeserializer deserializer) + public Object[] deserializeBinaryBulk(int rowCnt, BinaryDeserializer deserializer) throws SQLException, IOException { - Object[] data = new Object[rows]; - for (int row = 0; row < rows; row++) { - data[row] = this.deserializeBinary(deserializer); + Object[] data = new Object[rowCnt]; + for (int i = 0; i < rowCnt; i++) { + data[i] = this.deserializeBinary(deserializer); } return data; } diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeInt64.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeInt64.java index 42a4edbe..2b1d5990 100644 --- a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeInt64.java +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeInt64.java @@ -96,11 +96,11 @@ public BigInteger parseBigIntegerPositive(String num, int bitlen) { } @Override - public Object[] deserializeBinaryBulk(int rows, BinaryDeserializer deserializer) + public Object[] deserializeBinaryBulk(int rowCnt, BinaryDeserializer deserializer) throws SQLException, IOException { - Object[] data = new Object[rows]; - for (int row = 0; row < rows; row++) { - data[row] = this.deserializeBinary(deserializer); + Object[] data = new Object[rowCnt]; + for (int i = 0; i < rowCnt; i++) { + data[i] = this.deserializeBinary(deserializer); } return data; } diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeInt8.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeInt8.java index 6f786ff0..2f620003 100644 --- a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeInt8.java +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeInt8.java @@ -84,10 +84,10 @@ public Number deserializeBinary(BinaryDeserializer deserializer) throws IOExcept } @Override - public Number[] deserializeBinaryBulk(int rows, BinaryDeserializer deserializer) throws IOException { - Number[] data = new Number[rows]; - for (int row = 0; row < rows; row++) { - data[row] = deserializeBinary(deserializer); + public Number[] deserializeBinaryBulk(int rowCnt, BinaryDeserializer deserializer) throws IOException { + Number[] data = new Number[rowCnt]; + for (int i = 0; i < rowCnt; i++) { + data[i] = deserializeBinary(deserializer); } return data; } diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeUUID.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeUUID.java index 8ffb24bc..0416dd9c 100644 --- a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeUUID.java +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/DataTypeUUID.java @@ -79,10 +79,10 @@ public Object deserializeBinary(BinaryDeserializer deserializer) throws SQLExcep } @Override - public Object[] deserializeBinaryBulk(int rows, BinaryDeserializer deserializer) throws SQLException, IOException { - String[] data = new String[rows]; - for (int row = 0; row < rows; row++) { - data[row] = (String) deserializeBinary(deserializer); + public Object[] deserializeBinaryBulk(int rowCnt, BinaryDeserializer deserializer) throws SQLException, IOException { + String[] data = new String[rowCnt]; + for (int i = 0; i < rowCnt; i++) { + data[i] = (String) deserializeBinary(deserializer); } return data; } diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeArray.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeArray.java index 2584f84b..efca8c18 100644 --- a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeArray.java +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeArray.java @@ -117,11 +117,10 @@ public void serializeBinary(Object data, BinarySerializer serializer) throws SQL } } - @Override - public void serializeBinaryBulk(Object[] data, BinarySerializer serializer) throws SQLException, IOException { - offsetIDataType.serializeBinary(data.length, serializer); - getElemDataType().serializeBinaryBulk(data, serializer); + public void serializeBinaryBulk(Object[] values, BinarySerializer serializer) throws SQLException, IOException { + offsetIDataType.serializeBinary(values.length, serializer); + getElemDataType().serializeBinaryBulk(values, serializer); } @Override @@ -131,17 +130,17 @@ public Object deserializeBinary(BinaryDeserializer deserializer) throws SQLExcep } @Override - public Object[] deserializeBinaryBulk(int rows, BinaryDeserializer deserializer) throws IOException, SQLException { - ClickHouseArray[] data = new ClickHouseArray[rows]; - if (rows == 0) { + public Object[] deserializeBinaryBulk(int rowCnt, BinaryDeserializer deserializer) throws IOException, SQLException { + ClickHouseArray[] data = new ClickHouseArray[rowCnt]; + if (rowCnt == 0) { return data; } - Object[] offsets = offsetIDataType.deserializeBinaryBulk(rows, deserializer); + Object[] offsets = offsetIDataType.deserializeBinaryBulk(rowCnt, deserializer); ClickHouseArray res = new ClickHouseArray(elemDataType, - elemDataType.deserializeBinaryBulk(((BigInteger) offsets[rows - 1]).intValue(), deserializer)); + elemDataType.deserializeBinaryBulk(((BigInteger) offsets[rowCnt - 1]).intValue(), deserializer)); - for (int row = 0, lastOffset = 0; row < rows; row++) { + for (int row = 0, lastOffset = 0; row < rowCnt; row++) { BigInteger offset = (BigInteger) offsets[row]; data[row] = res.slice(lastOffset, offset.intValue() - lastOffset); lastOffset = offset.intValue(); diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeDateTime.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeDateTime.java index 136dfdca..0b29c42c 100644 --- a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeDateTime.java +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeDateTime.java @@ -119,10 +119,10 @@ public Object deserializeBinary(BinaryDeserializer deserializer) throws SQLExcep } @Override - public Object[] deserializeBinaryBulk(int rows, BinaryDeserializer deserializer) throws SQLException, IOException { - Timestamp[] data = new Timestamp[rows]; - for (int row = 0; row < rows; row++) { - data[row] = new Timestamp(deserializer.readInt() * 1000L); + public Object[] deserializeBinaryBulk(int rowCnt, BinaryDeserializer deserializer) throws SQLException, IOException { + Timestamp[] data = new Timestamp[rowCnt]; + for (int i = 0; i < rowCnt; i++) { + data[i] = new Timestamp(deserializer.readInt() * 1000L); } return data; } diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeDateTime64.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeDateTime64.java index b86125a7..fdb303cd 100644 --- a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeDateTime64.java +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeDateTime64.java @@ -156,10 +156,10 @@ public void serializeBinary(Object data, BinarySerializer serializer) throws IOE } @Override - public Object[] deserializeBinaryBulk(int rows, BinaryDeserializer deserializer) throws IOException { - Timestamp[] data = new Timestamp[rows]; - for (int row = 0; row < rows; row++) { - data[row] = (Timestamp) deserializeBinary(deserializer); + public Object[] deserializeBinaryBulk(int rowCnt, BinaryDeserializer deserializer) throws IOException { + Timestamp[] data = new Timestamp[rowCnt]; + for (int i = 0; i < rowCnt; i++) { + data[i] = (Timestamp) deserializeBinary(deserializer); } return data; } diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeDecimal.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeDecimal.java index 37703638..c57b9d3d 100644 --- a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeDecimal.java +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeDecimal.java @@ -204,10 +204,10 @@ public Object deserializeBinary(BinaryDeserializer deserializer) throws SQLExcep } @Override - public Object[] deserializeBinaryBulk(int rows, BinaryDeserializer deserializer) throws SQLException, IOException { - BigDecimal[] data = new BigDecimal[rows]; - for (int row = 0; row < rows; row++) { - data[row] = (BigDecimal) this.deserializeBinary(deserializer); + public Object[] deserializeBinaryBulk(int rowCnt, BinaryDeserializer deserializer) throws SQLException, IOException { + BigDecimal[] data = new BigDecimal[rowCnt]; + for (int i = 0; i < rowCnt; i++) { + data[i] = (BigDecimal) this.deserializeBinary(deserializer); } return data; } diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeEnum16.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeEnum16.java index 0cc5aa40..e1175a14 100644 --- a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeEnum16.java +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeEnum16.java @@ -139,10 +139,10 @@ public Object deserializeBinary(BinaryDeserializer deserializer) throws SQLExcep } @Override - public Object[] deserializeBinaryBulk(int rows, BinaryDeserializer deserializer) throws SQLException, IOException { - String[] data = new String[rows]; - for (int row = 0; row < rows; row++) { - data[row] = (String) deserializeBinary(deserializer); + public Object[] deserializeBinaryBulk(int rowCnt, BinaryDeserializer deserializer) throws SQLException, IOException { + String[] data = new String[rowCnt]; + for (int i = 0; i < rowCnt; i++) { + data[i] = (String) deserializeBinary(deserializer); } return data; } diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeEnum8.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeEnum8.java index fb53f372..044f0d8a 100644 --- a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeEnum8.java +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeEnum8.java @@ -139,10 +139,10 @@ public Object deserializeBinary(BinaryDeserializer deserializer) throws SQLExcep } @Override - public Object[] deserializeBinaryBulk(int rows, BinaryDeserializer deserializer) throws SQLException, IOException { - String[] data = new String[rows]; - for (int row = 0; row < rows; row++) { - data[row] = (String) deserializeBinary(deserializer); + public Object[] deserializeBinaryBulk(int rowCnt, BinaryDeserializer deserializer) throws SQLException, IOException { + String[] data = new String[rowCnt]; + for (int i = 0; i < rowCnt; i++) { + data[i] = (String) deserializeBinary(deserializer); } return data; } diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeFixedString.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeFixedString.java index 719e4170..adf6770d 100644 --- a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeFixedString.java +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeFixedString.java @@ -123,10 +123,10 @@ public Object deserializeBinary(BinaryDeserializer deserializer) throws SQLExcep } @Override - public Object[] deserializeBinaryBulk(int rows, BinaryDeserializer deserializer) throws SQLException, IOException { - String[] data = new String[rows]; - for (int row = 0; row < rows; row++) { - data[row] = new String(deserializer.readBytes(n), charset); + public Object[] deserializeBinaryBulk(int rowCnt, BinaryDeserializer deserializer) throws SQLException, IOException { + String[] data = new String[rowCnt]; + for (int i = 0; i < rowCnt; i++) { + data[i] = new String(deserializer.readBytes(n), charset); } return data; } diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeNullable.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeNullable.java index 2910a2d7..1aaea030 100644 --- a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeNullable.java +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeNullable.java @@ -114,21 +114,21 @@ public Object deserializeBinary(BinaryDeserializer deserializer) throws SQLExcep } @Override - public void serializeBinaryBulk(Object[] data, BinarySerializer serializer) throws SQLException, IOException { - Short[] isNull = new Short[data.length]; - for (int i = 0; i < data.length; i++) { - isNull[i] = (data[i] == null ? IS_NULL : NON_NULL); - data[i] = data[i] == null ? nestedDataType.defaultValue() : data[i]; + public void serializeBinaryBulk(Object[] values, BinarySerializer serializer) throws SQLException, IOException { + Short[] isNull = new Short[values.length]; + for (int i = 0; i < values.length; i++) { + isNull[i] = (values[i] == null ? IS_NULL : NON_NULL); + values[i] = values[i] == null ? nestedDataType.defaultValue() : values[i]; } nullMapDataType.serializeBinaryBulk(isNull, serializer); - nestedDataType.serializeBinaryBulk(data, serializer); + nestedDataType.serializeBinaryBulk(values, serializer); } @Override - public Object[] deserializeBinaryBulk(int rows, BinaryDeserializer deserializer) throws SQLException, IOException { - Object[] nullMap = nullMapDataType.deserializeBinaryBulk(rows, deserializer); + public Object[] deserializeBinaryBulk(int rowCnt, BinaryDeserializer deserializer) throws SQLException, IOException { + Object[] nullMap = nullMapDataType.deserializeBinaryBulk(rowCnt, deserializer); - Object[] data = nestedDataType.deserializeBinaryBulk(rows, deserializer); + Object[] data = nestedDataType.deserializeBinaryBulk(rowCnt, deserializer); for (int i = 0; i < nullMap.length; i++) { if (IS_NULL.equals(nullMap[i])) { data[i] = null; diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeString.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeString.java index 63f051ec..bfedc413 100644 --- a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeString.java +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeString.java @@ -94,9 +94,9 @@ public String deserializeBinary(BinaryDeserializer deserializer) throws SQLExcep } @Override - public Object[] deserializeBinaryBulk(int rows, BinaryDeserializer deserializer) throws SQLException, IOException { - String[] data = new String[rows]; - for (int row = 0; row < rows; row++) { + public Object[] deserializeBinaryBulk(int rowCnt, BinaryDeserializer deserializer) throws SQLException, IOException { + String[] data = new String[rowCnt]; + for (int row = 0; row < rowCnt; row++) { byte[] bs = deserializer.readBytesBinary(); data[row] = new String(bs, charset); } diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeTuple.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeTuple.java index a86c3f28..acc7abc0 100644 --- a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeTuple.java +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeTuple.java @@ -119,22 +119,22 @@ public Object deserializeBinary(BinaryDeserializer deserializer) throws SQLExcep } @Override - public void serializeBinaryBulk(Object[] data, BinarySerializer serializer) throws SQLException, IOException { + public void serializeBinaryBulk(Object[] values, BinarySerializer serializer) throws SQLException, IOException { for (int i = 0; i < nestedTypes.length; i++) { - Object[] elemsData = new Object[data.length]; - for (int row = 0; row < data.length; row++) { - elemsData[row] = ((Struct) data[row]).getAttributes()[i]; + Object[] elemsData = new Object[values.length]; + for (int row = 0; row < values.length; row++) { + elemsData[row] = ((Struct) values[row]).getAttributes()[i]; } nestedTypes[i].serializeBinaryBulk(elemsData, serializer); } } @Override - public Object[] deserializeBinaryBulk(int rows, BinaryDeserializer deserializer) throws SQLException, IOException { - Object[][] rowsWithElems = getRowsWithElems(rows, deserializer); + public Object[] deserializeBinaryBulk(int rowCnt, BinaryDeserializer deserializer) throws SQLException, IOException { + Object[][] rowsWithElems = getRowsWithElems(rowCnt, deserializer); - Struct[] rowsData = new Struct[rows]; - for (int row = 0; row < rows; row++) { + Struct[] rowsData = new Struct[rowCnt]; + for (int row = 0; row < rowCnt; row++) { Object[] elemsData = new Object[nestedTypes.length]; for (int elemIndex = 0; elemIndex < nestedTypes.length; elemIndex++) {