|
1 | 1 | package com.clickhouse.client.api.data_formats.internal; |
2 | 2 |
|
3 | 3 | import com.clickhouse.data.ClickHouseColumn; |
| 4 | +import com.clickhouse.data.format.BinaryStreamUtils; |
4 | 5 |
|
5 | 6 | import java.io.ByteArrayInputStream; |
6 | 7 | import java.io.ByteArrayOutputStream; |
@@ -206,24 +207,102 @@ public void testReadNullVariantReturnsNull() throws Exception { |
206 | 207 | } |
207 | 208 |
|
208 | 209 | @Test |
209 | | - public void testReadVarIntReadsMaxInt() throws IOException { |
210 | | - Assert.assertEquals(readVarInt((byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x07), |
211 | | - Integer.MAX_VALUE); |
| 210 | + public void testNullableArrayValueUsesBoxedComponentType() throws Exception { |
| 211 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 212 | + BinaryStreamUtils.writeVarInt(baos, 2); |
| 213 | + BinaryStreamUtils.writeNonNull(baos); |
| 214 | + BinaryStreamUtils.writeFloat64(baos, 1.0); |
| 215 | + BinaryStreamUtils.writeNonNull(baos); |
| 216 | + BinaryStreamUtils.writeFloat64(baos, 2.0); |
| 217 | + |
| 218 | + BinaryStreamReader reader = new BinaryStreamReader( |
| 219 | + new ByteArrayInputStream(baos.toByteArray()), |
| 220 | + TimeZone.getTimeZone("UTC"), |
| 221 | + null, |
| 222 | + new BinaryStreamReader.CachingByteBufferAllocator(), |
| 223 | + false, |
| 224 | + null); |
| 225 | + |
| 226 | + BinaryStreamReader.ArrayValue array = (BinaryStreamReader.ArrayValue) reader.readValue( |
| 227 | + ClickHouseColumn.of("v", "Array(Nullable(Float64))")); |
| 228 | + |
| 229 | + Assert.assertEquals(array.getArray().getClass().getComponentType(), Double.class); |
212 | 230 | } |
213 | 231 |
|
214 | 232 | @Test |
215 | | - public void testReadVarIntRejectsOverflow() { |
216 | | - Assert.assertThrows(IOException.class, |
217 | | - () -> readVarInt((byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x08)); |
| 233 | + public void testNullableUnsignedArrayUsesWidenedType() throws Exception { |
| 234 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 235 | + BinaryStreamUtils.writeVarInt(baos, 2); |
| 236 | + BinaryStreamUtils.writeNonNull(baos); |
| 237 | + BinaryStreamUtils.writeUnsignedInt8(baos, 10); |
| 238 | + BinaryStreamUtils.writeNonNull(baos); |
| 239 | + BinaryStreamUtils.writeUnsignedInt8(baos, 20); |
| 240 | + |
| 241 | + BinaryStreamReader reader = new BinaryStreamReader( |
| 242 | + new ByteArrayInputStream(baos.toByteArray()), |
| 243 | + TimeZone.getTimeZone("UTC"), |
| 244 | + null, |
| 245 | + new BinaryStreamReader.CachingByteBufferAllocator(), |
| 246 | + false, |
| 247 | + null); |
| 248 | + |
| 249 | + BinaryStreamReader.ArrayValue array = (BinaryStreamReader.ArrayValue) reader.readValue( |
| 250 | + ClickHouseColumn.of("v", "Array(Nullable(UInt8))")); |
| 251 | + |
| 252 | + Assert.assertEquals(array.getArray().getClass().getComponentType(), Short.class); |
218 | 253 | } |
219 | 254 |
|
220 | 255 | @Test |
221 | | - public void testReadVarIntRejectsOverlongValue() { |
222 | | - Assert.assertThrows(IOException.class, |
223 | | - () -> readVarInt((byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x01)); |
| 256 | + public void testNullableEnumArrayUsesEnumValueType() throws Exception { |
| 257 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 258 | + BinaryStreamUtils.writeVarInt(baos, 2); |
| 259 | + BinaryStreamUtils.writeNonNull(baos); |
| 260 | + baos.write(1); // enum ordinal for 'a' |
| 261 | + BinaryStreamUtils.writeNonNull(baos); |
| 262 | + baos.write(2); // enum ordinal for 'b' |
| 263 | + |
| 264 | + BinaryStreamReader reader = new BinaryStreamReader( |
| 265 | + new ByteArrayInputStream(baos.toByteArray()), |
| 266 | + TimeZone.getTimeZone("UTC"), |
| 267 | + null, |
| 268 | + new BinaryStreamReader.CachingByteBufferAllocator(), |
| 269 | + false, |
| 270 | + null); |
| 271 | + |
| 272 | + BinaryStreamReader.ArrayValue array = (BinaryStreamReader.ArrayValue) reader.readValue( |
| 273 | + ClickHouseColumn.of("v", "Array(Nullable(Enum8('a'=1,'b'=2)))")); |
| 274 | + |
| 275 | + Assert.assertEquals(array.getArray().getClass().getComponentType(), |
| 276 | + BinaryStreamReader.EnumValue.class); |
| 277 | + } |
| 278 | + |
| 279 | + @Test |
| 280 | + public void testEmptyArrayTypes() throws Exception { |
| 281 | + assertEmptyArrayComponentType("Array(UInt8)", short.class); |
| 282 | + assertEmptyArrayComponentType("Array(Nullable(UInt8))", Short.class); |
| 283 | + assertEmptyArrayComponentType("Array(String)", String.class); |
| 284 | + assertEmptyArrayComponentType("Array(Nullable(String))", String.class); |
| 285 | + assertEmptyArrayComponentType("Array(Enum8('a'=1))", BinaryStreamReader.EnumValue.class); |
| 286 | + assertEmptyArrayComponentType("Array(Nullable(Enum8('a'=1)))", BinaryStreamReader.EnumValue.class); |
| 287 | + assertEmptyArrayComponentType("Array(Variant(Int32, String))", Object.class); |
| 288 | + assertEmptyArrayComponentType("Array(Array(String))", BinaryStreamReader.ArrayValue.class); |
224 | 289 | } |
225 | 290 |
|
226 | | - private static int readVarInt(byte... bytes) throws IOException { |
227 | | - return BinaryStreamReader.readVarInt(new ByteArrayInputStream(bytes)); |
| 291 | + private void assertEmptyArrayComponentType(String columnType, Class<?> expectedComponentType) throws Exception { |
| 292 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 293 | + BinaryStreamUtils.writeVarInt(baos, 0); |
| 294 | + |
| 295 | + BinaryStreamReader reader = new BinaryStreamReader( |
| 296 | + new ByteArrayInputStream(baos.toByteArray()), |
| 297 | + TimeZone.getTimeZone("UTC"), |
| 298 | + null, |
| 299 | + new BinaryStreamReader.CachingByteBufferAllocator(), |
| 300 | + false, |
| 301 | + null); |
| 302 | + |
| 303 | + BinaryStreamReader.ArrayValue array = (BinaryStreamReader.ArrayValue) reader.readValue( |
| 304 | + ClickHouseColumn.of("v", columnType)); |
| 305 | + |
| 306 | + Assert.assertEquals(array.getArray().getClass().getComponentType(), expectedComponentType, "Failed for " + columnType); |
228 | 307 | } |
229 | 308 | } |
0 commit comments