Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -293,6 +293,20 @@ public interface ClickHouseBinaryFormatReader extends AutoCloseable {
*/
boolean[] getBooleanArray(String colName);

/**
*
* @param colName
* @return
*/
short[] getShortArray(String colName);

/**
*
* @param colName
* @return
*/
String[] getStringArray(String colName);
Comment on lines +296 to +308
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JavaDoc comments for the new methods are incomplete. Consider adding descriptions of what these methods do, similar to other array getter methods in the interface.

Suggested change
/**
*
* @param colName
* @return
*/
short[] getShortArray(String colName);
/**
*
* @param colName
* @return
*/
String[] getStringArray(String colName);
/**
* Reads column with name `colName` as a short array.
*
* @param colName
* @return
*/
short[] getShortArray(String colName);
/**
* Reads column with name `colName` as a string array.
*
* @param colName
* @return
*/
String[] getStringArray(String colName);


/**
* Reads column with name `colName` as a string.
*
Expand Down Expand Up @@ -517,6 +531,10 @@ public interface ClickHouseBinaryFormatReader extends AutoCloseable {

boolean[] getBooleanArray(int index);

short [] getShortArray(int index);

String[] getStringArray(int index);

Object[] getTuple(int index);

Object[] getTuple(String colName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,32 @@ public boolean[] getBooleanArray(String colName) {
}
}

@Override
public short[] getShortArray(String colName) {
try {
return getPrimitiveArray(colName, short.class);
} catch (ClassCastException | IllegalArgumentException e) {
throw new ClientException("Value cannot be converted to an array of primitives", e);
}
}

@Override
public String[] getStringArray(String colName) {
Object value = readValue(colName);
if (value instanceof BinaryStreamReader.ArrayValue) {
BinaryStreamReader.ArrayValue array = (BinaryStreamReader.ArrayValue) value;
int length = array.length;
if (!array.itemType.equals(String.class))
throw new ClientException("Not A String type.");
String [] values = new String[length];
for (int i = 0; i < length; i++) {
values[i] = (String)((BinaryStreamReader.ArrayValue) value).get(i);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary cast in the loop when accessing array elements. The value is already cast to BinaryStreamReader.ArrayValue earlier.

Suggested change
values[i] = (String)((BinaryStreamReader.ArrayValue) value).get(i);
values[i] = (String)array.get(i);

}
return values;
}
throw new ClientException("Not ArrayValue type.");
}
Comment on lines +642 to +656
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The getStringArray implementation should follow the same pattern as other array getters by using the getPrimitiveArray method. This would make the code more consistent and reduce duplication.

Suggested change
public String[] getStringArray(String colName) {
Object value = readValue(colName);
if (value instanceof BinaryStreamReader.ArrayValue) {
BinaryStreamReader.ArrayValue array = (BinaryStreamReader.ArrayValue) value;
int length = array.length;
if (!array.itemType.equals(String.class))
throw new ClientException("Not A String type.");
String [] values = new String[length];
for (int i = 0; i < length; i++) {
values[i] = (String)((BinaryStreamReader.ArrayValue) value).get(i);
}
return values;
}
throw new ClientException("Not ArrayValue type.");
}
@Override
public String[] getStringArray(String colName) {
try {
return getPrimitiveArray(colName, String.class);
} catch (ClassCastException | IllegalArgumentException e) {
throw new ClientException("Value cannot be converted to an array of strings", e);
}
}


@Override
public boolean hasValue(int colIndex) {
return currentRecord[colIndex - 1] != null;
Expand Down Expand Up @@ -774,6 +800,16 @@ public boolean[] getBooleanArray(int index) {
return getBooleanArray(schema.columnIndexToName(index));
}

@Override
public short[] getShortArray(int index) {
return getShortArray(schema.columnIndexToName(index));
}

@Override
public String[] getStringArray(int index) {
return getStringArray(schema.columnIndexToName(index));
}

@Override
public Object[] getTuple(int index) {
return readValue(index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ public boolean[] getBooleanArray(String colName) {
return reader.getBooleanArray(colName);
}

@Override
public short[] getShortArray(String colName) {
return reader.getShortArray(colName);
}

@Override
public String[] getStringArray(String colName) {
return reader.getStringArray(colName);
}

@Override
public String getString(int index) {
return reader.getString(index);
Expand Down Expand Up @@ -315,6 +325,16 @@ public boolean[] getBooleanArray(int index) {
return reader.getBooleanArray(index);
}

@Override
public short[] getShortArray(int index) {
return reader.getShortArray(index);
}

@Override
public String[] getStringArray(int index) {
return reader.getStringArray(index);
}

@Override
public Object[] getTuple(int index) {
return reader.getTuple(index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,16 @@ public boolean[] getBooleanArray(String colName) {
return getPrimitiveArray(colName);
}

@Override
public short[] getShortArray(String colName) {
return getPrimitiveArray(colName);
}

@Override
public String[] getStringArray(String colName) {
return getPrimitiveArray(colName);
}

@Override
public boolean hasValue(int colIndex) {
return hasValue(schema.columnIndexToName(colIndex));
Expand Down Expand Up @@ -407,6 +417,16 @@ public boolean[] getBooleanArray(int index) {
return getPrimitiveArray(schema.columnIndexToName(index));
}

@Override
public short[] getShortArray(int index) {
return getPrimitiveArray(schema.columnIndexToName(index));
}

@Override
public String[] getStringArray(int index) {
return getPrimitiveArray(schema.columnIndexToName(index));
}

@Override
public Object[] getTuple(int index) {
return readValue(index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ public interface GenericRecord {

boolean[] getBooleanArray(String colName);

short[] getShortArray(String colName);

String[] getStringArray(String colName);

/**
* Reads column with name `colName` as a string.
*
Expand Down Expand Up @@ -482,6 +486,10 @@ public interface GenericRecord {

boolean[] getBooleanArray(int index);

short[] getShortArray(int index);

String[] getStringArray(int index);

Object[] getTuple(int index);

Object[] getTuple(String colName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,55 @@ public void testReadingAsBoolean() throws Exception {
Assert.assertEquals(reader.getString("a"), "true");
Assert.assertEquals(reader.getString("b"), "false");
}

@Test
public void testReadingArrays() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
String[] names = new String[]{ "a1", "a2", "a3", "a4", "a5"};
String[] types = new String[]{"Array(Int8)", "Array(String)", "Array(Int16)", "Array(Int32)", "Array(Int64)"};

BinaryStreamUtils.writeVarInt(out, names.length);
for (String name : names) {
BinaryStreamUtils.writeString(out, name);
}
for (String type : types) {
BinaryStreamUtils.writeString(out, type);
}

// write data
BinaryStreamUtils.writeVarInt(out, 2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like we have array of data and doing it in the loop - it keep test code compact.

BinaryStreamUtils.writeInt8(out, (byte) 1);
BinaryStreamUtils.writeInt8(out, (byte) 2);

BinaryStreamUtils.writeVarInt(out, 2);
BinaryStreamUtils.writeString(out, "a");
BinaryStreamUtils.writeString(out, "b");

BinaryStreamUtils.writeVarInt(out, 2);
BinaryStreamUtils.writeInt16(out, (short) 1);
BinaryStreamUtils.writeInt16(out, (short) 2);

BinaryStreamUtils.writeVarInt(out, 2);
BinaryStreamUtils.writeInt32(out, (int) 1);
BinaryStreamUtils.writeInt32(out, (int) 2);

BinaryStreamUtils.writeVarInt(out, 2);
BinaryStreamUtils.writeInt64(out, (long) 1);
BinaryStreamUtils.writeInt64(out, (long) 2);


InputStream in = new ByteArrayInputStream(out.toByteArray());
QuerySettings querySettings = new QuerySettings().setUseTimeZone(TimeZone.getTimeZone("UTC").toZoneId().getId());
RowBinaryWithNamesAndTypesFormatReader reader =
new RowBinaryWithNamesAndTypesFormatReader(in, querySettings, new BinaryStreamReader.CachingByteBufferAllocator());

reader.next();

Assert.assertEquals(reader.getByteArray("a1"), new byte[] {(byte) 1, (byte) 2});
Assert.assertEquals(reader.getStringArray("a2"), new String[] {"a", "b"});
Assert.assertEquals(reader.getShortArray("a3"), new short[] {(short) 1, (short) 2});
Assert.assertEquals(reader.getIntArray("a4"), new int[] {1, 2});
Assert.assertEquals(reader.getLongArray("a5"), new long[] {1L, 2L});

}
}
Loading