Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the bug existed in low_cardinality(nullable) type #43

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.timeplus.data;

import com.timeplus.data.type.complex.DataTypeLowCardinality;
import com.timeplus.data.type.complex.DataTypeNullable;
import com.timeplus.serde.BinarySerializer;
import java.io.IOException;
import java.sql.SQLException;
Expand All @@ -27,23 +28,39 @@ public class ColumnLowCardinality extends AbstractColumn {
private final List<Long> indexes;
private final List<Object> dict;
private final Long version = 1L;
private boolean nested_is_nullable;

public ColumnLowCardinality(String name, DataTypeLowCardinality type, Object[] values) {
super(name, type, values);
indexes = new ArrayList<>();
dict = new ArrayList<>();
data = ColumnFactory.createColumn(null, type.getNestedTypes(), null);
nested_is_nullable = type.getNestedTypes().nullable();
if (nested_is_nullable) {
dict.add(type.getNestedTypes().defaultValue());
dict.add(type.getNestedTypes().defaultValue());
yl-lisen marked this conversation as resolved.
Show resolved Hide resolved
}
}

@Override
public void write(Object object) throws IOException, SQLException {
long value = dict.indexOf(object);
if (value != -1) {
indexes.add(value);
if (object == null) {
if (nested_is_nullable) {
indexes.add(0l);
yl-lisen marked this conversation as resolved.
Show resolved Hide resolved
}
else {
throw new SQLException("null object appeared without nullable field");
}
}
else {
indexes.add((long) dict.size());
dict.add(object);
long value = dict.lastIndexOf(object);
if (value != -1) {
indexes.add(value);
}
else {
indexes.add((long) dict.size());
dict.add(object);
}
}
}

Expand All @@ -53,16 +70,37 @@ public void flushToSerializer(BinarySerializer serializer, boolean immediate) th
serializer.writeUTF8StringBinary(name);
serializer.writeUTF8StringBinary(type.name());
}

if (immediate) {
/// 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));

if (data.type().nullable()) {
yl-lisen marked this conversation as resolved.
Show resolved Hide resolved

ColumnNullable type = (ColumnNullable) data;
DataTypeNullable inside_column_type = (DataTypeNullable) type.type();
IDataType inside_type = inside_column_type.getNestedDataType();
yl-lisen marked this conversation as resolved.
Show resolved Hide resolved

for (int i = 0; i < dict.size(); i++) {
Object temp = dict.get(i);
if (temp == null) {
inside_type.serializeBinary(inside_type.defaultValue(), serializer);
}
else {
inside_type.serializeBinary(temp, serializer);
yl-lisen marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
data.flushToSerializer(serializer, true);
serializer.writeLong(indexes.size());
else {
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
for (int i = 0; i < indexes.size(); i++) {
serializer.writeLong(indexes.get(i));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,33 @@ public Object[] deserializeBinaryBulk(int rows, BinaryDeserializer deserializer)
return data;
}
else {
Long version = deserializer.readLong();
Long version = deserializer.readLong();

if (version != this.version) {
throw new SQLException("version error in type low_cardinality");
}

Long index_type = deserializer.readLong() & IndexTypeMask;
Long key_nums = deserializer.readLong();
Object[] dictionary = getNestedTypes().deserializeBinaryBulk(key_nums.intValue(), deserializer);
Object[] dictionary = new Object[key_nums.intValue()];

if (getNestedTypes().nullable()) {
yl-lisen marked this conversation as resolved.
Show resolved Hide resolved
DataTypeNullable type = (DataTypeNullable) getNestedTypes();
IDataType inside_type = type.getNestedDataType();
dictionary = inside_type.deserializeBinaryBulk(key_nums.intValue(), deserializer);
}
else {
dictionary = getNestedTypes().deserializeBinaryBulk(key_nums.intValue(), deserializer);
}

Long row_nums = deserializer.readLong();

if (row_nums != rows) {
throw new SQLException("read unexpected rows in low_cardinality, expected:" + rows + ", actual:" + row_nums);
}

IDataType type;

if (index_type == IndexType.UInt8.getValue()) {
type = new DataTypeUInt8();
}
Expand All @@ -148,6 +161,7 @@ else if (index_type == IndexType.UInt32.getValue()) {

Object[] index_data = type.deserializeBinaryBulk(rows, deserializer);
Object[] data = new Object[rows];

if (type instanceof DataTypeUInt8) {
for (int i = 0; i < rows; i++) {
data[i] = dictionary[(short) index_data[i]];
Expand All @@ -157,7 +171,7 @@ else if (index_type == IndexType.UInt32.getValue()) {
for (int i = 0; i < rows; i++) {
data[i] = dictionary[(Integer) index_data[i]];
}
}
}
return data;
}
}
Expand Down