|
1 | 1 | package io.github.dfa1.vortex.encoding; |
2 | 2 |
|
3 | 3 | import com.google.protobuf.InvalidProtocolBufferException; |
| 4 | +import com.google.protobuf.NullValue; |
4 | 5 | import io.github.dfa1.vortex.proto.DTypeProtos; |
5 | 6 | import io.github.dfa1.vortex.proto.EncodingProtos; |
6 | 7 | import io.github.dfa1.vortex.proto.ScalarProtos; |
7 | 8 | import io.github.dfa1.vortex.core.array.Array; |
| 9 | +import io.github.dfa1.vortex.core.array.BoolArray; |
| 10 | +import io.github.dfa1.vortex.core.array.VarBinArray; |
8 | 11 | import io.github.dfa1.vortex.core.ArrayStats; |
9 | 12 | import io.github.dfa1.vortex.core.DType; |
10 | 13 | import io.github.dfa1.vortex.core.PType; |
|
18 | 21 | import java.lang.foreign.ValueLayout; |
19 | 22 | import java.nio.ByteBuffer; |
20 | 23 | import java.nio.ByteOrder; |
| 24 | +import java.nio.charset.StandardCharsets; |
21 | 25 | import java.util.List; |
22 | 26 |
|
23 | 27 | import static org.assertj.core.api.Assertions.assertThat; |
@@ -212,6 +216,150 @@ void decode_offsetSubtracted() { |
212 | 216 | assertThat(result.buffer(0).get(layout, 16L)).isEqualTo(777L); |
213 | 217 | } |
214 | 218 |
|
| 219 | + // regression: NULL_VALUE fill caused "unexpected scalar kind NULL_VALUE" on nullable cols |
| 220 | + @Test |
| 221 | + void decode_nullValueFill_treatedAsZero() { |
| 222 | + // Given — fill encoded as ScalarValue.NULL_VALUE (as Rust writes for nullable cols) |
| 223 | + byte[] nullFill = ScalarProtos.ScalarValue.newBuilder() |
| 224 | + .setNullValue(NullValue.NULL_VALUE).build().toByteArray(); |
| 225 | + byte[] meta = buildSparseMetaBytes(0, 0L, PType.U32); |
| 226 | + DecodeContext ctx = buildCtx(I64_DTYPE, 4, nullFill, meta, new byte[0], new byte[0], |
| 227 | + new DType.Primitive(PType.U32, false)); |
| 228 | + SparseEncoding sut = new SparseEncoding(); |
| 229 | + |
| 230 | + // When |
| 231 | + Array result = sut.decode(ctx); |
| 232 | + |
| 233 | + // Then |
| 234 | + var layout = ValueLayout.JAVA_LONG_UNALIGNED.withOrder(ByteOrder.LITTLE_ENDIAN); |
| 235 | + for (int i = 0; i < 4; i++) { |
| 236 | + assertThat(result.buffer(0).get(layout, (long) i * 8)).as("index %d", i).isZero(); |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + // regression: Utf8 dtype caused "expected primitive dtype, got Utf8[nullable=true]" |
| 241 | + @Test |
| 242 | + void decode_utf8_noPatches_allEmpty() { |
| 243 | + // Given — Utf8 sparse, no patches → all positions empty (null fill) |
| 244 | + DType utf8 = new DType.Utf8(true); |
| 245 | + byte[] nullFill = ScalarProtos.ScalarValue.newBuilder() |
| 246 | + .setNullValue(NullValue.NULL_VALUE).build().toByteArray(); |
| 247 | + byte[] meta = buildSparseMetaBytes(0, 0L, PType.U32); |
| 248 | + DecodeContext ctx = buildCtx(utf8, 3, nullFill, meta, new byte[0], new byte[0], |
| 249 | + new DType.Primitive(PType.U32, false)); |
| 250 | + SparseEncoding sut = new SparseEncoding(); |
| 251 | + |
| 252 | + // When |
| 253 | + Array result = sut.decode(ctx); |
| 254 | + |
| 255 | + // Then |
| 256 | + assertThat(result.length()).isEqualTo(3L); |
| 257 | + VarBinArray varBin = (VarBinArray) result; |
| 258 | + for (int i = 0; i < 3; i++) { |
| 259 | + assertThat(varBin.getByteLength(i)).as("index %d", i).isZero(); |
| 260 | + } |
| 261 | + } |
| 262 | + |
| 263 | + // regression: Utf8 dtype caused "expected primitive dtype, got Utf8[nullable=true]" |
| 264 | + @Test |
| 265 | + void decode_utf8_withPatches_writesStringsAtIndices() { |
| 266 | + // Given — 5 Utf8 elements, patches at [1]="hi" and [3]="bye" |
| 267 | + DType utf8 = new DType.Utf8(true); |
| 268 | + byte[] nullFill = ScalarProtos.ScalarValue.newBuilder() |
| 269 | + .setNullValue(NullValue.NULL_VALUE).build().toByteArray(); |
| 270 | + byte[] meta = buildSparseMetaBytes(2, 0L, PType.U32); |
| 271 | + |
| 272 | + byte[] idxBuf = toLEBytes(new long[]{1L, 3L}, PType.U32); |
| 273 | + byte[] strBytes = "hibye".getBytes(StandardCharsets.UTF_8); |
| 274 | + byte[] offsets = intLEBytes(new int[]{0, 2, 5}); |
| 275 | + byte[] varBinMeta = EncodingProtos.VarBinMetadata.newBuilder() |
| 276 | + .setOffsetsPtype(DTypeProtos.PType.forNumber(PType.I32.ordinal())) |
| 277 | + .build().toByteArray(); |
| 278 | + |
| 279 | + ArrayNode offsetsNode = new ArrayNode(EncodingId.VORTEX_PRIMITIVE, null, |
| 280 | + new ArrayNode[0], new int[]{3}, ArrayStats.empty()); |
| 281 | + ArrayNode valNode = new ArrayNode(EncodingId.VORTEX_VARBIN, |
| 282 | + ByteBuffer.wrap(varBinMeta), |
| 283 | + new ArrayNode[]{offsetsNode}, new int[]{2}, ArrayStats.empty()); |
| 284 | + ArrayNode idxNode = new ArrayNode(EncodingId.VORTEX_PRIMITIVE, null, |
| 285 | + new ArrayNode[0], new int[]{1}, ArrayStats.empty()); |
| 286 | + ArrayNode sparseNode = new ArrayNode(EncodingId.VORTEX_SPARSE, |
| 287 | + ByteBuffer.wrap(meta), |
| 288 | + new ArrayNode[]{idxNode, valNode}, new int[]{0}, ArrayStats.empty()); |
| 289 | + |
| 290 | + EncodingRegistry registry = EncodingRegistry.empty(); |
| 291 | + registry.register(new SparseEncoding()); |
| 292 | + registry.register(new PrimitiveEncoding()); |
| 293 | + registry.register(new VarBinEncoding()); |
| 294 | + |
| 295 | + MemorySegment[] segments = { |
| 296 | + MemorySegment.ofArray(nullFill), |
| 297 | + MemorySegment.ofArray(idxBuf), |
| 298 | + MemorySegment.ofArray(strBytes), |
| 299 | + MemorySegment.ofArray(offsets), |
| 300 | + }; |
| 301 | + DecodeContext ctx = new DecodeContext(sparseNode, utf8, 5, segments, registry, Arena.global()); |
| 302 | + SparseEncoding sut = new SparseEncoding(); |
| 303 | + |
| 304 | + // When |
| 305 | + Array result = sut.decode(ctx); |
| 306 | + |
| 307 | + // Then |
| 308 | + VarBinArray varBin = (VarBinArray) result; |
| 309 | + assertThat(varBin.length()).isEqualTo(5L); |
| 310 | + assertThat(varBin.getByteLength(0)).isZero(); |
| 311 | + assertThat(new String(varBin.getBytes(1))).isEqualTo("hi"); |
| 312 | + assertThat(varBin.getByteLength(2)).isZero(); |
| 313 | + assertThat(new String(varBin.getBytes(3))).isEqualTo("bye"); |
| 314 | + assertThat(varBin.getByteLength(4)).isZero(); |
| 315 | + } |
| 316 | + |
| 317 | + // regression: Bool dtype caused "expected primitive dtype, got Bool[nullable=true]" |
| 318 | + @Test |
| 319 | + void decode_bool_withPatches_setsBitsAtIndices() { |
| 320 | + // Given — 6 Bool elements, patches at [2]=true and [5]=true |
| 321 | + DType bool = new DType.Bool(true); |
| 322 | + byte[] nullFill = ScalarProtos.ScalarValue.newBuilder() |
| 323 | + .setNullValue(NullValue.NULL_VALUE).build().toByteArray(); |
| 324 | + byte[] meta = buildSparseMetaBytes(2, 0L, PType.U32); |
| 325 | + byte[] idxBuf = toLEBytes(new long[]{2L, 5L}, PType.U32); |
| 326 | + byte[] boolBits = new byte[]{0b00000011}; |
| 327 | + |
| 328 | + ArrayNode valNode = new ArrayNode(EncodingId.VORTEX_BOOL, null, |
| 329 | + new ArrayNode[0], new int[]{2}, ArrayStats.empty()); |
| 330 | + ArrayNode idxNode = new ArrayNode(EncodingId.VORTEX_PRIMITIVE, null, |
| 331 | + new ArrayNode[0], new int[]{1}, ArrayStats.empty()); |
| 332 | + ArrayNode sparseNode = new ArrayNode(EncodingId.VORTEX_SPARSE, |
| 333 | + ByteBuffer.wrap(meta), |
| 334 | + new ArrayNode[]{idxNode, valNode}, new int[]{0}, ArrayStats.empty()); |
| 335 | + |
| 336 | + EncodingRegistry registry = EncodingRegistry.empty(); |
| 337 | + registry.register(new SparseEncoding()); |
| 338 | + registry.register(new PrimitiveEncoding()); |
| 339 | + registry.register(new BoolEncoding()); |
| 340 | + |
| 341 | + MemorySegment[] segments = { |
| 342 | + MemorySegment.ofArray(nullFill), |
| 343 | + MemorySegment.ofArray(idxBuf), |
| 344 | + MemorySegment.ofArray(boolBits), |
| 345 | + }; |
| 346 | + DecodeContext ctx = new DecodeContext(sparseNode, bool, 6, segments, registry, Arena.global()); |
| 347 | + SparseEncoding sut = new SparseEncoding(); |
| 348 | + |
| 349 | + // When |
| 350 | + Array result = sut.decode(ctx); |
| 351 | + |
| 352 | + // Then |
| 353 | + BoolArray boolArr = (BoolArray) result; |
| 354 | + assertThat(boolArr.length()).isEqualTo(6L); |
| 355 | + assertThat(boolArr.getBoolean(0)).isFalse(); |
| 356 | + assertThat(boolArr.getBoolean(1)).isFalse(); |
| 357 | + assertThat(boolArr.getBoolean(2)).isTrue(); |
| 358 | + assertThat(boolArr.getBoolean(3)).isFalse(); |
| 359 | + assertThat(boolArr.getBoolean(4)).isFalse(); |
| 360 | + assertThat(boolArr.getBoolean(5)).isTrue(); |
| 361 | + } |
| 362 | + |
215 | 363 | private static DecodeContext buildSparseCtx( |
216 | 364 | DType dtype, long rowCount, long fillLong, PType idxPtype, |
217 | 365 | long[] patchIndices, long[] patchValues |
@@ -315,5 +463,14 @@ private static byte[] f64LEBytes(double[] values) { |
315 | 463 | } |
316 | 464 | return buf; |
317 | 465 | } |
| 466 | + |
| 467 | + private static byte[] intLEBytes(int[] values) { |
| 468 | + byte[] buf = new byte[values.length * 4]; |
| 469 | + ByteBuffer bb = ByteBuffer.wrap(buf).order(ByteOrder.LITTLE_ENDIAN); |
| 470 | + for (int v : values) { |
| 471 | + bb.putInt(v); |
| 472 | + } |
| 473 | + return buf; |
| 474 | + } |
318 | 475 | } |
319 | 476 | } |
0 commit comments