|
1 | 1 | package io.github.dfa1.vortex.integration; |
2 | 2 |
|
| 3 | +import io.github.dfa1.vortex.core.compute.FastLanes; |
3 | 4 | import io.github.dfa1.vortex.core.model.ColumnName; |
4 | 5 | import io.github.dfa1.vortex.core.model.DType; |
5 | 6 | import io.github.dfa1.vortex.core.model.Editions; |
| 7 | +import io.github.dfa1.vortex.core.model.PType; |
| 8 | +import io.github.dfa1.vortex.core.model.EncodingId; |
| 9 | +import io.github.dfa1.vortex.core.proto.ProtoDeltaMetadata; |
| 10 | +import io.github.dfa1.vortex.inspect.InspectorTree; |
6 | 11 | import io.github.dfa1.vortex.reader.ReadRegistry; |
7 | 12 | import io.github.dfa1.vortex.reader.ScanOptions; |
8 | 13 | import io.github.dfa1.vortex.reader.VortexReader; |
| 14 | +import io.github.dfa1.vortex.reader.array.Array; |
| 15 | +import io.github.dfa1.vortex.reader.array.ByteArray; |
9 | 16 | import io.github.dfa1.vortex.reader.array.IntArray; |
| 17 | +import io.github.dfa1.vortex.reader.array.LongArray; |
| 18 | +import io.github.dfa1.vortex.reader.array.ShortArray; |
| 19 | +import io.github.dfa1.vortex.reader.decode.ArrayNode; |
| 20 | +import io.github.dfa1.vortex.reader.decode.DecodeContext; |
| 21 | +import io.github.dfa1.vortex.reader.decode.DeltaEncodingDecoder; |
10 | 22 | import io.github.dfa1.vortex.writer.VortexWriter; |
11 | 23 | import io.github.dfa1.vortex.writer.WriteOptions; |
| 24 | +import io.github.dfa1.vortex.writer.WriteRegistry; |
| 25 | +import io.github.dfa1.vortex.writer.encode.EncodeContext; |
| 26 | +import io.github.dfa1.vortex.writer.encode.EncodeResult; |
| 27 | +import io.github.dfa1.vortex.writer.encode.DeltaEncodingEncoder; |
12 | 28 | import io.github.dfa1.vortex.writer.encode.PatchedEncodingEncoder; |
13 | 29 | import org.junit.jupiter.api.Test; |
14 | 30 | import org.junit.jupiter.api.io.TempDir; |
| 31 | +import org.junit.jupiter.params.ParameterizedTest; |
| 32 | +import org.junit.jupiter.params.provider.EnumSource; |
15 | 33 |
|
16 | 34 | import java.io.IOException; |
| 35 | +import java.lang.foreign.Arena; |
| 36 | +import java.lang.foreign.MemorySegment; |
17 | 37 | import java.nio.channels.FileChannel; |
18 | 38 | import java.nio.file.Path; |
19 | 39 | import java.nio.file.StandardOpenOption; |
| 40 | +import java.util.Arrays; |
20 | 41 | import java.util.ArrayList; |
21 | 42 | import java.util.List; |
22 | 43 | import java.util.Map; |
| 44 | +import java.util.Random; |
23 | 45 |
|
24 | 46 | import static org.assertj.core.api.Assertions.assertThat; |
25 | 47 |
|
26 | | -/// Java writer → Java reader round-trips for encodings the bundled `vortex-jni` build cannot read |
27 | | -/// back, so they have no Java→Rust coverage. This is still a real cross-module integration test: it |
28 | | -/// drives the writer's encode, the on-disk file format, and the reader's decode end to end. |
| 48 | +/// Java writer → Java reader round-trips for encodings whose Java *decode* has no other end-to-end |
| 49 | +/// cover. This is a real cross-module integration test either way: it drives the writer's encode, |
| 50 | +/// the on-disk file format, and the reader's decode end to end. |
29 | 51 | /// |
30 | | -/// `vortex.patched` is the case here — the JNI reader rejects a standalone patched array with |
31 | | -/// "Unknown encoding: vortex.patched", so the round-trip is asserted on the Java side instead. |
| 52 | +/// Two reasons land a case here: |
| 53 | +/// - the bundled `vortex-jni` build cannot read the encoding back, so there is no Java→Rust test. |
| 54 | +/// `vortex.patched` is this case — the JNI reader rejects a standalone patched array with |
| 55 | +/// "Unknown encoding: vortex.patched". |
| 56 | +/// - Java→Rust cover exists but only exercises the *encoder*. `fastlanes.delta` is this case: |
| 57 | +/// `JavaWritesRustReadsIntegrationTest#javaWriter_rustReader_delta_i64` proves what Java writes |
| 58 | +/// is readable, and says nothing about `DeltaEncodingDecoder`. |
32 | 59 | class JavaRoundTripIntegrationTest { |
33 | 60 |
|
34 | 61 | private static final DType.Struct I32_SCHEMA = new DType.Struct( |
@@ -62,6 +89,152 @@ void patched_i32_javaWriteJavaRead(@TempDir Path tmp) throws IOException { |
62 | 89 | assertThat(decoded).containsExactly(data); |
63 | 90 | } |
64 | 91 |
|
| 92 | + /// `fastlanes.delta` decode across every width it accepts, over three FastLanes chunks. |
| 93 | + /// |
| 94 | + /// The unit tests reach the decoder only with I64 and single-element (constant) children, so |
| 95 | + /// nothing covered the per-width read and write paths, and nothing covered more than one |
| 96 | + /// chunk — which is where the chunk-window arithmetic lives. Values are full-width random |
| 97 | + /// bit patterns, not a monotonic ramp: the high bit is exactly where a read that |
| 98 | + /// sign-extends and one that zero-extends diverge, and delta round-trips any values at all |
| 99 | + /// since encode and decode both wrap modulo the type width. |
| 100 | + @ParameterizedTest |
| 101 | + @EnumSource(value = PType.class, names = {"I8", "I16", "I32", "I64", "U8", "U16", "U32", "U64"}) |
| 102 | + void delta_javaWriteJavaRead(PType ptype, @TempDir Path tmp) throws IOException { |
| 103 | + // Given — 2500 rows is three 1024-element chunks, the last one padded. |
| 104 | + long mask = FastLanes.lowMask(ptype.bits()); |
| 105 | + Random rng = new Random(338); |
| 106 | + long[] expected = new long[2500]; |
| 107 | + for (int i = 0; i < expected.length; i++) { |
| 108 | + expected[i] = rng.nextLong() & mask; |
| 109 | + } |
| 110 | + DType.Struct schema = new DType.Struct(List.of(ColumnName.of("v")), |
| 111 | + List.of(new DType.Primitive(ptype, false)), false); |
| 112 | + Path file = tmp.resolve("java_delta_" + ptype + ".vtx"); |
| 113 | + |
| 114 | + // When |
| 115 | + try (var ch = FileChannel.open(file, StandardOpenOption.CREATE, StandardOpenOption.WRITE); |
| 116 | + var sut = VortexWriter.create(ch, schema, |
| 117 | + WriteOptions.defaults().withEdition(Editions.UNSTABLE_2025_05_0), |
| 118 | + List.of(new DeltaEncodingEncoder()))) { |
| 119 | + sut.writeChunk(Map.of(ColumnName.of("v"), narrow(expected, ptype))); |
| 120 | + } |
| 121 | + |
| 122 | + // Then — the encoding is asserted too, so a writer that quietly stopped choosing delta |
| 123 | + // would fail here rather than leave the decoder untested |
| 124 | + try (var reader = VortexReader.open(file, ReadRegistry.loadAll())) { |
| 125 | + assertThat(InspectorTree.build(reader).usedEncodings()).contains("fastlanes.delta"); |
| 126 | + } |
| 127 | + // compared as stored bit patterns, so signed and unsigned widths assert alike |
| 128 | + assertThat(readColumnBits(file, "v", mask)).containsExactly(expected); |
| 129 | + } |
| 130 | + |
| 131 | + /// `fastlanes.delta`'s `offset` metadata — which makes a decode start partway into the |
| 132 | + /// reconstructed elements — has no round-trip cover, because the Java writer always emits 0; |
| 133 | + /// a non-zero offset only ever arrives on a Rust-written sliced array. So this drives the |
| 134 | + /// decoder directly over encoder-produced children instead of through a file, and asserts |
| 135 | + /// the window is exactly the corresponding slice of the full decode. The window arithmetic |
| 136 | + /// (which chunks to reconstruct, and where each lands in the output) is the part of decode |
| 137 | + /// that only a non-zero offset reaches. |
| 138 | + @Test |
| 139 | + void delta_offsetWindowIsTheSliceOfTheFullDecode() { |
| 140 | + // Given — 2500 rows, so the encoder pads to three chunks |
| 141 | + DType dtype = new DType.Primitive(PType.I64, false); |
| 142 | + Random rng = new Random(3381); |
| 143 | + long[] data = new long[2500]; |
| 144 | + for (int i = 0; i < data.length; i++) { |
| 145 | + data[i] = rng.nextLong(); |
| 146 | + } |
| 147 | + try (Arena arena = Arena.ofConfined()) { |
| 148 | + EncodeResult encoded = new DeltaEncodingEncoder().encode(dtype, data, |
| 149 | + EncodeContext.of(arena, WriteRegistry.builder().registerDefaults().build())); |
| 150 | + long padded = 3L * FastLanes.CHUNK; |
| 151 | + long[] full = decodeDelta(encoded, dtype, padded, 0, padded, arena); |
| 152 | + |
| 153 | + // When — a window opening inside chunk 0 and closing inside chunk 2 |
| 154 | + long[] result = decodeDelta(encoded, dtype, padded, 700, 1500, arena); |
| 155 | + |
| 156 | + // Then |
| 157 | + assertThat(result).containsExactly(Arrays.copyOfRange(full, 700, 2200)); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + /// Decodes `encoded` as a `fastlanes.delta` array over the given window, bypassing the file |
| 162 | + /// format so the `offset` the writer never emits can be set. |
| 163 | + /// |
| 164 | + /// @param encoded the encoder's output (bases buffer, deltas buffer) |
| 165 | + /// @param dtype logical element type |
| 166 | + /// @param deltasLen number of reconstructed elements the chunks cover |
| 167 | + /// @param offset absolute index the first returned row maps to |
| 168 | + /// @param rowCount number of rows to decode |
| 169 | + /// @param arena allocator for the decoded segment |
| 170 | + /// @return the decoded values |
| 171 | + private static long[] decodeDelta(EncodeResult encoded, DType dtype, long deltasLen, |
| 172 | + int offset, long rowCount, Arena arena) { |
| 173 | + MemorySegment meta = MemorySegment.ofArray(new ProtoDeltaMetadata(deltasLen, offset).encode()); |
| 174 | + ArrayNode bases = new ArrayNode(EncodingId.VORTEX_PRIMITIVE, null, new ArrayNode[0], new int[]{0}); |
| 175 | + ArrayNode deltas = new ArrayNode(EncodingId.VORTEX_PRIMITIVE, null, new ArrayNode[0], new int[]{1}); |
| 176 | + ArrayNode node = new ArrayNode(EncodingId.FASTLANES_DELTA, meta, |
| 177 | + new ArrayNode[]{bases, deltas}, new int[0]); |
| 178 | + DecodeContext ctx = new DecodeContext(node, dtype, rowCount, |
| 179 | + encoded.buffers().toArray(new MemorySegment[0]), ReadRegistry.loadAll(), arena); |
| 180 | + LongArray decoded = (LongArray) new DeltaEncodingDecoder().decode(ctx); |
| 181 | + long[] out = new long[(int) decoded.length()]; |
| 182 | + for (int i = 0; i < out.length; i++) { |
| 183 | + out[i] = decoded.getLong(i); |
| 184 | + } |
| 185 | + return out; |
| 186 | + } |
| 187 | + |
| 188 | + /// Narrows logical values to the Java array type the writer expects for `ptype`. |
| 189 | + private static Object narrow(long[] values, PType ptype) { |
| 190 | + return switch (ptype) { |
| 191 | + case I8, U8 -> { |
| 192 | + byte[] out = new byte[values.length]; |
| 193 | + for (int i = 0; i < values.length; i++) { |
| 194 | + out[i] = (byte) values[i]; |
| 195 | + } |
| 196 | + yield out; |
| 197 | + } |
| 198 | + case I16, U16 -> { |
| 199 | + short[] out = new short[values.length]; |
| 200 | + for (int i = 0; i < values.length; i++) { |
| 201 | + out[i] = (short) values[i]; |
| 202 | + } |
| 203 | + yield out; |
| 204 | + } |
| 205 | + case I32, U32 -> { |
| 206 | + int[] out = new int[values.length]; |
| 207 | + for (int i = 0; i < values.length; i++) { |
| 208 | + out[i] = (int) values[i]; |
| 209 | + } |
| 210 | + yield out; |
| 211 | + } |
| 212 | + default -> values.clone(); |
| 213 | + }; |
| 214 | + } |
| 215 | + |
| 216 | + /// Reads a primitive column back as raw bit patterns, masked to the type's width so a |
| 217 | + /// sign-extending accessor and a zero-extending one compare equal. |
| 218 | + private static long[] readColumnBits(Path file, String column, long mask) throws IOException { |
| 219 | + var out = new ArrayList<Long>(); |
| 220 | + try (var vf = VortexReader.open(file, ReadRegistry.loadAll()); |
| 221 | + var iter = vf.scan(ScanOptions.columns(column))) { |
| 222 | + iter.forEachRemaining(c -> { |
| 223 | + Array arr = c.column(column); |
| 224 | + for (long i = 0; i < arr.length(); i++) { |
| 225 | + out.add(switch (arr) { |
| 226 | + case ByteArray a -> a.getByte(i) & mask; |
| 227 | + case ShortArray a -> a.getShort(i) & mask; |
| 228 | + case IntArray a -> a.getInt(i) & mask; |
| 229 | + case LongArray a -> a.getLong(i) & mask; |
| 230 | + default -> throw new IllegalStateException("unexpected array " + arr.getClass()); |
| 231 | + }); |
| 232 | + } |
| 233 | + }); |
| 234 | + } |
| 235 | + return out.stream().mapToLong(Long::longValue).toArray(); |
| 236 | + } |
| 237 | + |
65 | 238 | @SuppressWarnings("SameParameterValue") |
66 | 239 | private static int[] readIntColumn(Path file, String column) throws IOException { |
67 | 240 | try (var vf = VortexReader.open(file, ReadRegistry.loadAll()); |
|
0 commit comments