Skip to content

Commit

Permalink
refactor IDataType
Browse files Browse the repository at this point in the history
  • Loading branch information
pan3793 committed Dec 15, 2020
1 parent b0e2dcb commit 4789620
Show file tree
Hide file tree
Showing 20 changed files with 128 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,63 @@
import java.io.IOException;
import java.sql.SQLException;

// It would be nice if we introduce a Generic Type, `IDataType<T>`, 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 <T> inner data type, 1:1 map to ClickHouse data type
*/
public interface IDataType<T> {

// 1. represent data type attributions
String name();

int sqlTypeId();
default String[] getAliases() {
return new String[0];
}

Object defaultValue();
int sqlTypeId();

Class javaTypeClass();
Class<T> javaTypeClass();

boolean nullable();

int getPrecision();

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 <O> T castFrom(O other) {
return javaTypeClass().cast(other);
}

default <O> O castTo(T inner, Class<O> castToClazz) {
return castToClazz.cast(inner);
}

// 5. format to string
default <O> String format(O value) {
return castTo(castFrom(value), String.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Loading

0 comments on commit 4789620

Please sign in to comment.