-
Notifications
You must be signed in to change notification settings - Fork 614
Added getShortArray & getStringArray implementation #2604
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| return values; | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| throw new ClientException("Not ArrayValue type."); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+642
to
+656
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||||||||||||||
| public boolean hasValue(int colIndex) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return currentRecord[colIndex - 1] != null; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -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); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}); | ||
|
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
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.