|
| 1 | +package io.github.dfa1.vortex.reader.decode; |
| 2 | + |
| 3 | +import io.github.dfa1.vortex.core.model.DType; |
| 4 | +import io.github.dfa1.vortex.core.model.EncodingId; |
| 5 | +import io.github.dfa1.vortex.core.proto.ProtoALPRDMetadata; |
| 6 | +import io.github.dfa1.vortex.core.proto.ProtoPType; |
| 7 | +import io.github.dfa1.vortex.reader.ReadRegistry; |
| 8 | +import io.github.dfa1.vortex.reader.array.Array; |
| 9 | +import io.github.dfa1.vortex.reader.array.DoubleArray; |
| 10 | +import io.github.dfa1.vortex.reader.array.MaskedArray; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +import java.lang.foreign.Arena; |
| 14 | +import java.lang.foreign.MemorySegment; |
| 15 | +import java.nio.ByteBuffer; |
| 16 | +import java.nio.ByteOrder; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.assertj.core.api.Assertions.within; |
| 21 | + |
| 22 | +class AlpRdEncodingDecoderTest { |
| 23 | + |
| 24 | + private static final AlpRdEncodingDecoder SUT = new AlpRdEncodingDecoder(); |
| 25 | + private static final ReadRegistry REGISTRY = TestRegistry.ofDecoders( |
| 26 | + SUT, new PrimitiveEncodingDecoder(), new BoolEncodingDecoder()); |
| 27 | + |
| 28 | + // ALP-RD reconstruction is `longBitsToDouble((dict[left] << rightBitWidth) | right)`. |
| 29 | + // With rightBitWidth=48, the dict codes are the top 16 bits of an f64 bit pattern: |
| 30 | + // 1.0 = 0x3FF0_0000_0000_0000 -> 0x3FF0; 2.0 = 0x4000_0000_0000_0000 -> 0x4000. |
| 31 | + private static final int RIGHT_BIT_WIDTH = 48; |
| 32 | + private static final int DICT_ONE = 0x3FF0; |
| 33 | + private static final int DICT_TWO = 0x4000; |
| 34 | + |
| 35 | + @Test |
| 36 | + void decode_nullLeftParts_yieldsNullRows() { |
| 37 | + // Given — left codes [0,1,0] with row 1 null via the left_parts validity bitmap; |
| 38 | + // the decoder must carry that mask onto the reconstructed doubles (#234). |
| 39 | + DecodeContext ctx = maskedContext(new short[]{0, 1, 0}, new long[]{0, 0, 0}, |
| 40 | + boolBitmap(true, false, true)); |
| 41 | + |
| 42 | + // When |
| 43 | + Array result = SUT.decode(ctx); |
| 44 | + |
| 45 | + // Then — the null row survives and valid rows decode to their dictionary doubles |
| 46 | + MaskedArray masked = (MaskedArray) result; |
| 47 | + assertThat(masked.isValid(0)).isTrue(); |
| 48 | + assertThat(masked.isValid(1)).isFalse(); |
| 49 | + assertThat(masked.isValid(2)).isTrue(); |
| 50 | + DoubleArray inner = (DoubleArray) masked.inner(); |
| 51 | + assertThat(inner.getDouble(0)).isCloseTo(1.0, within(1e-12)); |
| 52 | + assertThat(inner.getDouble(2)).isCloseTo(1.0, within(1e-12)); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void decode_allValidLeftParts_yieldsPlainArray() { |
| 57 | + // Given — a non-nullable left_parts child (no validity): no-regression path must NOT mask |
| 58 | + DecodeContext ctx = plainContext(new short[]{0, 1, 0}, new long[]{0, 0, 0}); |
| 59 | + |
| 60 | + // When |
| 61 | + Array result = SUT.decode(ctx); |
| 62 | + |
| 63 | + // Then — a bare DoubleArray, and every row decodes through the dictionary |
| 64 | + assertThat(result).isInstanceOf(DoubleArray.class).isNotInstanceOf(MaskedArray.class); |
| 65 | + DoubleArray inner = (DoubleArray) result; |
| 66 | + assertThat(inner.getDouble(0)).isCloseTo(1.0, within(1e-12)); |
| 67 | + assertThat(inner.getDouble(1)).isCloseTo(2.0, within(1e-12)); |
| 68 | + assertThat(inner.getDouble(2)).isCloseTo(1.0, within(1e-12)); |
| 69 | + } |
| 70 | + |
| 71 | + private static DecodeContext maskedContext(short[] leftCodes, long[] rightBits, MemorySegment validity) { |
| 72 | + // buffers: 0 = left u16 codes, 1 = right u64 payloads, 2 = validity bitmap |
| 73 | + ArrayNode validityNode = new ArrayNode(EncodingId.VORTEX_BOOL, null, new ArrayNode[0], new int[]{2}); |
| 74 | + ArrayNode leftNode = new ArrayNode(EncodingId.VORTEX_PRIMITIVE, null, |
| 75 | + new ArrayNode[]{validityNode}, new int[]{0}); |
| 76 | + ArrayNode rightNode = new ArrayNode(EncodingId.VORTEX_PRIMITIVE, null, new ArrayNode[0], new int[]{1}); |
| 77 | + ArrayNode node = new ArrayNode(EncodingId.VORTEX_ALPRD, metaSegment(), |
| 78 | + new ArrayNode[]{leftNode, rightNode}, new int[0]); |
| 79 | + MemorySegment[] segs = {leShorts(leftCodes), leLongs(rightBits), validity}; |
| 80 | + return new DecodeContext(node, DType.F64, leftCodes.length, segs, REGISTRY, Arena.ofAuto()); |
| 81 | + } |
| 82 | + |
| 83 | + private static DecodeContext plainContext(short[] leftCodes, long[] rightBits) { |
| 84 | + // buffers: 0 = left u16 codes, 1 = right u64 payloads (no validity child) |
| 85 | + ArrayNode leftNode = new ArrayNode(EncodingId.VORTEX_PRIMITIVE, null, new ArrayNode[0], new int[]{0}); |
| 86 | + ArrayNode rightNode = new ArrayNode(EncodingId.VORTEX_PRIMITIVE, null, new ArrayNode[0], new int[]{1}); |
| 87 | + ArrayNode node = new ArrayNode(EncodingId.VORTEX_ALPRD, metaSegment(), |
| 88 | + new ArrayNode[]{leftNode, rightNode}, new int[0]); |
| 89 | + MemorySegment[] segs = {leShorts(leftCodes), leLongs(rightBits)}; |
| 90 | + return new DecodeContext(node, DType.F64, leftCodes.length, segs, REGISTRY, Arena.ofAuto()); |
| 91 | + } |
| 92 | + |
| 93 | + private static MemorySegment metaSegment() { |
| 94 | + byte[] meta = new ProtoALPRDMetadata(RIGHT_BIT_WIDTH, 2, |
| 95 | + List.of(DICT_ONE, DICT_TWO), ProtoPType.U16, null).encode(); |
| 96 | + return MemorySegment.ofArray(meta); |
| 97 | + } |
| 98 | + |
| 99 | + private static MemorySegment leShorts(short... vs) { |
| 100 | + byte[] b = new byte[vs.length * 2]; |
| 101 | + ByteBuffer bb = ByteBuffer.wrap(b).order(ByteOrder.LITTLE_ENDIAN); |
| 102 | + for (short v : vs) { |
| 103 | + bb.putShort(v); |
| 104 | + } |
| 105 | + return MemorySegment.ofArray(b); |
| 106 | + } |
| 107 | + |
| 108 | + private static MemorySegment leLongs(long... vs) { |
| 109 | + byte[] b = new byte[vs.length * 8]; |
| 110 | + ByteBuffer bb = ByteBuffer.wrap(b).order(ByteOrder.LITTLE_ENDIAN); |
| 111 | + for (long v : vs) { |
| 112 | + bb.putLong(v); |
| 113 | + } |
| 114 | + return MemorySegment.ofArray(b); |
| 115 | + } |
| 116 | + |
| 117 | + private static MemorySegment boolBitmap(boolean... valid) { |
| 118 | + byte[] bytes = new byte[(valid.length + 7) / 8]; |
| 119 | + for (int i = 0; i < valid.length; i++) { |
| 120 | + if (valid[i]) { |
| 121 | + bytes[i >>> 3] |= (byte) (1 << (i & 7)); |
| 122 | + } |
| 123 | + } |
| 124 | + return MemorySegment.ofArray(bytes); |
| 125 | + } |
| 126 | +} |
0 commit comments