diff --git a/timeplus-native-jdbc/src/main/java/com/timeplus/data/ColumnLowCardinality.java b/timeplus-native-jdbc/src/main/java/com/timeplus/data/ColumnLowCardinality.java index 5411115..46b4923 100644 --- a/timeplus-native-jdbc/src/main/java/com/timeplus/data/ColumnLowCardinality.java +++ b/timeplus-native-jdbc/src/main/java/com/timeplus/data/ColumnLowCardinality.java @@ -37,20 +37,13 @@ public ColumnLowCardinality(String name, DataTypeLowCardinality type, Object[] v @Override public void write(Object object) throws IOException, SQLException { - boolean found = false; - for (Object obj : dict) { - if (obj != null && obj.equals(object)) { - found = true; - break; - } - } - if (found) { - long value = dict.indexOf(object); + long value = dict.indexOf(object); + if (value != -1) { indexes.add(value); } else { dict.add(object); - long value = dict.indexOf(object); + value = dict.indexOf(object); indexes.add(value); } } @@ -62,15 +55,15 @@ public void flushToSerializer(BinarySerializer serializer, boolean immediate) th serializer.writeUTF8StringBinary(type.name()); } if (immediate) { - serializer.writeLong(version); // add version - serializer.writeLong(IndexType.UInt64.getValue() | IndexType.HasAdditionalKeysBit.getValue()); // need to judge the type as additional key - serializer.writeLong(dict.size()); // give dictionary type size + /// The data layout: [version][index_type][dictionary][indexes] + serializer.writeLong(version); + serializer.writeLong(IndexType.UInt64.getValue() | IndexType.HasAdditionalKeysBit.getValue()); + serializer.writeLong(dict.size()); for (int i = 0; i < dict.size(); i++) { data.write(dict.get(i)); } data.flushToSerializer(serializer, true); - - serializer.writeLong(indexes.size()); // give index type size + serializer.writeLong(indexes.size()); for (int i = 0; i < indexes.size(); i++) { serializer.writeLong(indexes.get(i)); } diff --git a/timeplus-native-jdbc/src/main/java/com/timeplus/data/type/complex/DataTypeLowCardinality.java b/timeplus-native-jdbc/src/main/java/com/timeplus/data/type/complex/DataTypeLowCardinality.java index 727eb7a..1848acb 100644 --- a/timeplus-native-jdbc/src/main/java/com/timeplus/data/type/complex/DataTypeLowCardinality.java +++ b/timeplus-native-jdbc/src/main/java/com/timeplus/data/type/complex/DataTypeLowCardinality.java @@ -38,6 +38,7 @@ public class DataTypeLowCardinality implements IDataType { private final String name; private final IDataType nestedDataType; private final Long version = 1L; + private final Long IndexTypeMask = 0b11111111L; public DataTypeLowCardinality(String name, IDataType nestedDataType) { this.name = name; @@ -101,13 +102,13 @@ public void serializeBinaryBulk(Object[] data, BinarySerializer serializer) thro @Override public Object deserializeBinary(BinaryDeserializer deserializer) throws SQLException, IOException { - return this.nestedDataType.deserializeBinary(deserializer); + return this.getNestedTypes().deserializeBinary(deserializer); } @Override public Object[] deserializeBinaryBulk(int rows, BinaryDeserializer deserializer) throws SQLException, IOException { if (rows==0) { - Object[] data = nestedDataType.deserializeBinaryBulk(rows, deserializer); + Object[] data = getNestedTypes().deserializeBinaryBulk(rows, deserializer); return data; } else { @@ -115,9 +116,9 @@ public Object[] deserializeBinaryBulk(int rows, BinaryDeserializer deserializer) if (version != this.version) { throw new SQLException("version error in type low_cardinality"); } - Long index_type = deserializer.readLong() & 0b11111111; // decide what type it is + Long index_type = deserializer.readLong() & IndexTypeMask; Long key_nums = deserializer.readLong(); - Object[] dictionary = nestedDataType.deserializeBinaryBulk(key_nums.intValue(), deserializer); + Object[] dictionary = getNestedTypes().deserializeBinaryBulk(key_nums.intValue(), deserializer); Long row_nums = deserializer.readLong(); Object[] data = new Object[rows]; for (int i = 0; i < row_nums; i++) {