|
| 1 | +package io.github.dfa1.vortex.encoding; |
| 2 | + |
| 3 | +import io.github.dfa1.vortex.core.DType; |
| 4 | +import io.github.dfa1.vortex.core.PType; |
| 5 | +import io.github.dfa1.vortex.core.VortexException; |
| 6 | +import io.github.dfa1.vortex.core.array.Array; |
| 7 | +import io.github.dfa1.vortex.core.array.MaskedArray; |
| 8 | +import org.junit.jupiter.api.Nested; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +import java.lang.foreign.Arena; |
| 12 | +import java.lang.foreign.MemorySegment; |
| 13 | +import java.lang.foreign.ValueLayout; |
| 14 | +import java.nio.ByteOrder; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 20 | + |
| 21 | +class MaskedEncodingTest { |
| 22 | + |
| 23 | + private static final ValueLayout.OfInt LE_INT = |
| 24 | + ValueLayout.JAVA_INT_UNALIGNED.withOrder(ByteOrder.LITTLE_ENDIAN); |
| 25 | + |
| 26 | + @Nested |
| 27 | + class Decode { |
| 28 | + |
| 29 | + @Test |
| 30 | + void oneChild_noValidity_allPositionsValid() { |
| 31 | + // Given |
| 32 | + var sut = new MaskedEncoding(); |
| 33 | + EncodingRegistry registry = buildRegistry(); |
| 34 | + DType i32Nullable = new DType.Primitive(PType.I32, true); |
| 35 | + EncodeResult ctx = maskedResult(new int[]{10, 20, 30}, null); |
| 36 | + |
| 37 | + // When |
| 38 | + Array result = sut.decode(EncodeTestHelper.toDecodeContext(ctx, 3L, i32Nullable, registry)); |
| 39 | + |
| 40 | + // Then |
| 41 | + assertThat(result).isInstanceOf(MaskedArray.class); |
| 42 | + MaskedArray masked = (MaskedArray) result; |
| 43 | + assertThat(masked.length()).isEqualTo(3); |
| 44 | + assertThat(masked.isValid(0)).isTrue(); |
| 45 | + assertThat(masked.isValid(1)).isTrue(); |
| 46 | + assertThat(masked.isValid(2)).isTrue(); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void twoChildren_withValidity_masksNulls() { |
| 51 | + // Given |
| 52 | + var sut = new MaskedEncoding(); |
| 53 | + EncodingRegistry registry = buildRegistry(); |
| 54 | + DType i32Nullable = new DType.Primitive(PType.I32, true); |
| 55 | + EncodeResult ctx = maskedResult(new int[]{1, 2, 3, 4, 5}, |
| 56 | + new boolean[]{true, false, true, false, true}); |
| 57 | + |
| 58 | + // When |
| 59 | + Array result = sut.decode(EncodeTestHelper.toDecodeContext(ctx, 5L, i32Nullable, registry)); |
| 60 | + |
| 61 | + // Then |
| 62 | + assertThat(result).isInstanceOf(MaskedArray.class); |
| 63 | + MaskedArray masked = (MaskedArray) result; |
| 64 | + assertThat(masked.length()).isEqualTo(5); |
| 65 | + assertThat(masked.isValid(0)).isTrue(); |
| 66 | + assertThat(masked.isValid(1)).isFalse(); |
| 67 | + assertThat(masked.isValid(2)).isTrue(); |
| 68 | + assertThat(masked.isValid(3)).isFalse(); |
| 69 | + assertThat(masked.isValid(4)).isTrue(); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void dtype_isNullable() { |
| 74 | + // Given |
| 75 | + var sut = new MaskedEncoding(); |
| 76 | + EncodingRegistry registry = buildRegistry(); |
| 77 | + DType i32Nullable = new DType.Primitive(PType.I32, true); |
| 78 | + EncodeResult ctx = maskedResult(new int[]{1, 2, 3}, null); |
| 79 | + |
| 80 | + // When |
| 81 | + Array result = sut.decode(EncodeTestHelper.toDecodeContext(ctx, 3L, i32Nullable, registry)); |
| 82 | + |
| 83 | + // Then |
| 84 | + assertThat(result.dtype().nullable()).isTrue(); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void buffer_delegatesToChild() { |
| 89 | + // Given |
| 90 | + var sut = new MaskedEncoding(); |
| 91 | + EncodingRegistry registry = buildRegistry(); |
| 92 | + DType i32Nullable = new DType.Primitive(PType.I32, true); |
| 93 | + EncodeResult ctx = maskedResult(new int[]{7, 8, 9}, null); |
| 94 | + |
| 95 | + // When |
| 96 | + Array result = sut.decode(EncodeTestHelper.toDecodeContext(ctx, 3L, i32Nullable, registry)); |
| 97 | + |
| 98 | + // Then — buffer(0) works and contains child values |
| 99 | + assertThat(result.buffer(0).get(LE_INT, 0L)).isEqualTo(7); |
| 100 | + assertThat(result.buffer(0).get(LE_INT, 4L)).isEqualTo(8); |
| 101 | + assertThat(result.buffer(0).get(LE_INT, 8L)).isEqualTo(9); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void buffersPresentThrows() { |
| 106 | + // Given |
| 107 | + var sut = new MaskedEncoding(); |
| 108 | + EncodingRegistry registry = buildRegistry(); |
| 109 | + DType i32Nullable = new DType.Primitive(PType.I32, true); |
| 110 | + |
| 111 | + // Build a node with an unexpected buffer index |
| 112 | + EncodeNode childNode = EncodeNode.leaf(EncodingId.VORTEX_PRIMITIVE, 0); |
| 113 | + EncodeNode maskedNode = new EncodeNode( |
| 114 | + EncodingId.VORTEX_MASKED, null, |
| 115 | + new EncodeNode[]{childNode}, new int[]{1}); |
| 116 | + MemorySegment dummyBuf = Arena.ofAuto().allocate(4); |
| 117 | + EncodeResult result = new EncodeResult(maskedNode, List.of(dummyBuf, dummyBuf), null, null); |
| 118 | + |
| 119 | + // When / Then |
| 120 | + assertThatThrownBy(() -> |
| 121 | + sut.decode(EncodeTestHelper.toDecodeContext(result, 1L, i32Nullable, registry))) |
| 122 | + .isInstanceOf(VortexException.class) |
| 123 | + .hasMessageContaining("expected 0 buffers"); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + void zeroChildrenThrows() { |
| 128 | + // Given |
| 129 | + var sut = new MaskedEncoding(); |
| 130 | + EncodingRegistry registry = buildRegistry(); |
| 131 | + DType i32Nullable = new DType.Primitive(PType.I32, true); |
| 132 | + |
| 133 | + EncodeNode maskedNode = new EncodeNode( |
| 134 | + EncodingId.VORTEX_MASKED, null, new EncodeNode[]{}, new int[]{}); |
| 135 | + EncodeResult result = new EncodeResult(maskedNode, List.of(), null, null); |
| 136 | + |
| 137 | + // When / Then |
| 138 | + assertThatThrownBy(() -> |
| 139 | + sut.decode(EncodeTestHelper.toDecodeContext(result, 0L, i32Nullable, registry))) |
| 140 | + .isInstanceOf(VortexException.class) |
| 141 | + .hasMessageContaining("expected 1 or 2 children"); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + void threeChildrenThrows() { |
| 146 | + // Given |
| 147 | + var sut = new MaskedEncoding(); |
| 148 | + EncodingRegistry registry = buildRegistry(); |
| 149 | + DType i32Nullable = new DType.Primitive(PType.I32, true); |
| 150 | + |
| 151 | + PrimitiveEncoding primitiveEncoding = new PrimitiveEncoding(); |
| 152 | + DType i32 = new DType.Primitive(PType.I32, false); |
| 153 | + EncodeResult childResult = primitiveEncoding.encode(i32, new int[]{1}); |
| 154 | + EncodeNode childNode = childResult.rootNode(); |
| 155 | + EncodeNode maskedNode = new EncodeNode( |
| 156 | + EncodingId.VORTEX_MASKED, null, |
| 157 | + new EncodeNode[]{childNode, childNode, childNode}, new int[]{}); |
| 158 | + List<MemorySegment> bufs = new ArrayList<>(childResult.buffers()); |
| 159 | + EncodeResult result = new EncodeResult(maskedNode, bufs, null, null); |
| 160 | + |
| 161 | + // When / Then |
| 162 | + assertThatThrownBy(() -> |
| 163 | + sut.decode(EncodeTestHelper.toDecodeContext(result, 1L, i32Nullable, registry))) |
| 164 | + .isInstanceOf(VortexException.class) |
| 165 | + .hasMessageContaining("expected 1 or 2 children"); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + // ── helpers ─────────────────────────────────────────────────────────────── |
| 170 | + |
| 171 | + private static EncodingRegistry buildRegistry() { |
| 172 | + EncodingRegistry registry = EncodingRegistry.empty(); |
| 173 | + registry.register(new MaskedEncoding()); |
| 174 | + registry.register(new PrimitiveEncoding()); |
| 175 | + registry.register(new BoolEncoding()); |
| 176 | + return registry; |
| 177 | + } |
| 178 | + |
| 179 | + /// Build a synthetic masked EncodeResult wrapping an I32 child and optional validity. |
| 180 | + private static EncodeResult maskedResult(int[] values, boolean[] validity) { |
| 181 | + PrimitiveEncoding primitiveEncoding = new PrimitiveEncoding(); |
| 182 | + DType i32 = new DType.Primitive(PType.I32, false); |
| 183 | + EncodeResult childResult = primitiveEncoding.encode(i32, values); |
| 184 | + |
| 185 | + List<MemorySegment> allBuffers = new ArrayList<>(childResult.buffers()); |
| 186 | + EncodeNode[] children; |
| 187 | + |
| 188 | + if (validity == null) { |
| 189 | + children = new EncodeNode[]{childResult.rootNode()}; |
| 190 | + } else { |
| 191 | + BoolEncoding boolEncoding = new BoolEncoding(); |
| 192 | + DType boolDtype = new DType.Bool(false); |
| 193 | + EncodeResult validityResult = boolEncoding.encode(boolDtype, validity); |
| 194 | + EncodeNode remapped = EncodeNode.remapBufferIndices( |
| 195 | + validityResult.rootNode(), childResult.buffers().size()); |
| 196 | + allBuffers.addAll(validityResult.buffers()); |
| 197 | + children = new EncodeNode[]{childResult.rootNode(), remapped}; |
| 198 | + } |
| 199 | + |
| 200 | + EncodeNode maskedNode = new EncodeNode( |
| 201 | + EncodingId.VORTEX_MASKED, null, children, new int[]{}); |
| 202 | + return new EncodeResult(maskedNode, allBuffers, null, null); |
| 203 | + } |
| 204 | +} |
0 commit comments