|
| 1 | +package io.github.dfa1.vortex.integration; |
| 2 | + |
| 3 | +import dev.vortex.api.Session; |
| 4 | +import dev.vortex.api.VortexWriter; |
| 5 | +import dev.vortex.arrow.ArrowAllocation; |
| 6 | +import dev.vortex.jni.NativeLoader; |
| 7 | +import io.github.dfa1.vortex.core.model.ColumnName; |
| 8 | +import io.github.dfa1.vortex.core.model.DType; |
| 9 | +import io.github.dfa1.vortex.core.model.PType; |
| 10 | +import io.github.dfa1.vortex.inspect.InspectorTree; |
| 11 | +import io.github.dfa1.vortex.reader.VortexReader; |
| 12 | +import io.github.dfa1.vortex.writer.WriteOptions; |
| 13 | +import io.github.dfa1.vortex.writer.encode.NullableData; |
| 14 | +import org.apache.arrow.c.ArrowArray; |
| 15 | +import org.apache.arrow.c.ArrowSchema; |
| 16 | +import org.apache.arrow.c.Data; |
| 17 | +import org.apache.arrow.memory.BufferAllocator; |
| 18 | +import org.apache.arrow.vector.BigIntVector; |
| 19 | +import org.apache.arrow.vector.FieldVector; |
| 20 | +import org.apache.arrow.vector.Float8Vector; |
| 21 | +import org.apache.arrow.vector.IntVector; |
| 22 | +import org.apache.arrow.vector.VarCharVector; |
| 23 | +import org.apache.arrow.vector.VectorSchemaRoot; |
| 24 | +import org.apache.arrow.vector.types.FloatingPointPrecision; |
| 25 | +import org.apache.arrow.vector.types.pojo.ArrowType; |
| 26 | +import org.apache.arrow.vector.types.pojo.Field; |
| 27 | +import org.apache.arrow.vector.types.pojo.Schema; |
| 28 | +import org.junit.jupiter.api.io.TempDir; |
| 29 | +import org.junit.jupiter.params.ParameterizedTest; |
| 30 | +import org.junit.jupiter.params.provider.Arguments; |
| 31 | +import org.junit.jupiter.params.provider.MethodSource; |
| 32 | + |
| 33 | +import java.io.IOException; |
| 34 | +import java.nio.channels.FileChannel; |
| 35 | +import java.nio.charset.StandardCharsets; |
| 36 | +import java.nio.file.Path; |
| 37 | +import java.nio.file.StandardOpenOption; |
| 38 | +import java.util.Arrays; |
| 39 | +import java.util.List; |
| 40 | +import java.util.Map; |
| 41 | +import java.util.Random; |
| 42 | +import java.util.stream.Stream; |
| 43 | + |
| 44 | +import static org.assertj.core.api.Assertions.assertThat; |
| 45 | + |
| 46 | +/// Rust's dict layout admits a column only when its probe compressor, the full cascade, turns the |
| 47 | +/// column's first 8192-row block into a `vortex.dict` (`DictStrategy`); a cardinality rule of thumb |
| 48 | +/// admits columns Rust leaves to bit-packing. Each column goes through vortex-jni and vortex-java, |
| 49 | +/// and both must agree on whether it gets a dict layout. |
| 50 | +class DictAdmissionInteropIntegrationTest { |
| 51 | + |
| 52 | + private static final Session SESSION = Session.create(); |
| 53 | + private static final BufferAllocator ALLOCATOR = ArrowAllocation.rootAllocator(); |
| 54 | + private static final ColumnName V = ColumnName.of("v"); |
| 55 | + private static final int ROWS = 20_000; |
| 56 | + |
| 57 | + static { |
| 58 | + NativeLoader.loadJni(); |
| 59 | + } |
| 60 | + |
| 61 | + /// The column type. The wide kinds spread their values over the whole range, where a |
| 62 | + /// dictionary beats bit-packing; the others keep them small. |
| 63 | + private enum Kind { I32, I64, WIDE_I64, F64, WIDE_F64, UTF8 } |
| 64 | + |
| 65 | + static Stream<Arguments> columns() { |
| 66 | + return Stream.of( |
| 67 | + // wide values: a dictionary beats the values |
| 68 | + Arguments.of("wide i64, 100 distinct", Kind.WIDE_I64, 100, 0), |
| 69 | + Arguments.of("wide f64, 10 distinct", Kind.WIDE_F64, 10, 0), |
| 70 | + // few small values: bit-packing a few bits a row beats a dictionary |
| 71 | + Arguments.of("i64, 4 distinct", Kind.I64, 4, 0), |
| 72 | + Arguments.of("f64, 5 distinct", Kind.F64, 5, 0), |
| 73 | + Arguments.of("utf8, 5 distinct", Kind.UTF8, 5, 0), |
| 74 | + Arguments.of("nullable i32, 10 distinct", Kind.I32, 10, 7), |
| 75 | + // under half distinct, yet bit-packing 7 bits wins over a 100-value dictionary. |
| 76 | + // A nullable column with 1000 distinct values still gets a dict in Java only: its |
| 77 | + // admission keeps the cardinality check until the nullable cascade charges |
| 78 | + // bit-packing the validity, as Rust's sampled sizes do. |
| 79 | + Arguments.of("i64, 100 distinct", Kind.I64, 100, 0), |
| 80 | + Arguments.of("i32, 1000 distinct", Kind.I32, 1000, 0), |
| 81 | + Arguments.of("utf8, 1000 distinct", Kind.UTF8, 1000, 0), |
| 82 | + Arguments.of("utf8, mostly distinct", Kind.UTF8, 1_000_000, 0)); |
| 83 | + } |
| 84 | + |
| 85 | + @ParameterizedTest(name = "{0}") |
| 86 | + @MethodSource("columns") |
| 87 | + void javaGivesADictLayoutWhereJniDoes(String name, Kind kind, int distinct, int nullEvery, @TempDir Path tmp) |
| 88 | + throws Exception { |
| 89 | + // Given |
| 90 | + long[] draws = new Random(3).longs(ROWS, 0, distinct).toArray(); |
| 91 | + Path jniFile = tmp.resolve("jni.vortex"); |
| 92 | + Path javaFile = tmp.resolve("java.vortex"); |
| 93 | + writeWithJni(jniFile, kind, draws, nullEvery); |
| 94 | + boolean expected = hasDictLayout(jniFile); |
| 95 | + |
| 96 | + // When |
| 97 | + writeWithJava(javaFile, kind, draws, nullEvery); |
| 98 | + |
| 99 | + // Then |
| 100 | + assertThat(hasDictLayout(javaFile)).as("vortex-jni dict layout: %s", expected).isEqualTo(expected); |
| 101 | + } |
| 102 | + |
| 103 | + private static boolean isNull(int row, int nullEvery) { |
| 104 | + return nullEvery > 0 && row % nullEvery == 0; |
| 105 | + } |
| 106 | + |
| 107 | + private static long i64(Kind kind, long draw) { |
| 108 | + return kind == Kind.WIDE_I64 ? draw * 0x9E3779B97F4A7C15L : draw; |
| 109 | + } |
| 110 | + |
| 111 | + private static double f64(Kind kind, long draw) { |
| 112 | + return kind == Kind.WIDE_F64 ? new Random(draw).nextDouble() * 1000 : draw * 0.25; |
| 113 | + } |
| 114 | + |
| 115 | + private static String string(long draw) { |
| 116 | + return "value-" + draw; |
| 117 | + } |
| 118 | + |
| 119 | + private static void writeWithJni(Path path, Kind kind, long[] draws, int nullEvery) throws Exception { |
| 120 | + ArrowType type = switch (kind) { |
| 121 | + case I32 -> new ArrowType.Int(32, true); |
| 122 | + case I64, WIDE_I64 -> new ArrowType.Int(64, true); |
| 123 | + case F64, WIDE_F64 -> new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE); |
| 124 | + case UTF8 -> new ArrowType.Utf8(); |
| 125 | + }; |
| 126 | + Field field = nullEvery > 0 ? Field.nullable("v", type) : Field.notNullable("v", type); |
| 127 | + Schema schema = new Schema(List.of(field)); |
| 128 | + try (VortexWriter writer = VortexWriter.builder(SESSION, path.toAbsolutePath().toUri().toString(), schema, |
| 129 | + ALLOCATOR).build(); |
| 130 | + VectorSchemaRoot root = VectorSchemaRoot.create(schema, ALLOCATOR)) { |
| 131 | + FieldVector vector = root.getVector("v"); |
| 132 | + vector.setInitialCapacity(ROWS); |
| 133 | + vector.allocateNew(); |
| 134 | + for (int i = 0; i < ROWS; i++) { |
| 135 | + if (isNull(i, nullEvery)) { |
| 136 | + vector.setNull(i); |
| 137 | + continue; |
| 138 | + } |
| 139 | + switch (kind) { |
| 140 | + case I32 -> ((IntVector) vector).setSafe(i, (int) draws[i]); |
| 141 | + case I64, WIDE_I64 -> ((BigIntVector) vector).setSafe(i, i64(kind, draws[i])); |
| 142 | + case F64, WIDE_F64 -> ((Float8Vector) vector).setSafe(i, f64(kind, draws[i])); |
| 143 | + case UTF8 -> ((VarCharVector) vector).setSafe(i, string(draws[i]).getBytes(StandardCharsets.UTF_8)); |
| 144 | + } |
| 145 | + } |
| 146 | + vector.setValueCount(ROWS); |
| 147 | + root.setRowCount(ROWS); |
| 148 | + try (ArrowArray array = ArrowArray.allocateNew(ALLOCATOR); |
| 149 | + ArrowSchema arrowSchema = ArrowSchema.allocateNew(ALLOCATOR)) { |
| 150 | + Data.exportVectorSchemaRoot(ALLOCATOR, root, null, array, arrowSchema); |
| 151 | + writer.writeBatch(array.memoryAddress(), arrowSchema.memoryAddress()); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private static void writeWithJava(Path path, Kind kind, long[] draws, int nullEvery) throws IOException { |
| 157 | + boolean nullable = nullEvery > 0; |
| 158 | + DType dtype = switch (kind) { |
| 159 | + case I32 -> new DType.Primitive(PType.I32, nullable); |
| 160 | + case I64, WIDE_I64 -> new DType.Primitive(PType.I64, nullable); |
| 161 | + case F64, WIDE_F64 -> new DType.Primitive(PType.F64, nullable); |
| 162 | + case UTF8 -> new DType.Utf8(nullable); |
| 163 | + }; |
| 164 | + Object values = switch (kind) { |
| 165 | + case I32 -> Arrays.stream(draws).mapToInt(d -> (int) d).toArray(); |
| 166 | + case I64, WIDE_I64 -> Arrays.stream(draws).map(d -> i64(kind, d)).toArray(); |
| 167 | + case F64, WIDE_F64 -> Arrays.stream(draws).mapToDouble(d -> f64(kind, d)).toArray(); |
| 168 | + case UTF8 -> Arrays.stream(draws).mapToObj(DictAdmissionInteropIntegrationTest::string) |
| 169 | + .toArray(String[]::new); |
| 170 | + }; |
| 171 | + Object data = values; |
| 172 | + if (nullable) { |
| 173 | + boolean[] valid = new boolean[ROWS]; |
| 174 | + for (int i = 0; i < ROWS; i++) { |
| 175 | + valid[i] = !isNull(i, nullEvery); |
| 176 | + } |
| 177 | + data = new NullableData(values, valid); |
| 178 | + } |
| 179 | + DType.Struct schema = new DType.Struct(List.of(V), List.of(dtype), false); |
| 180 | + try (var channel = FileChannel.open(path, StandardOpenOption.CREATE, StandardOpenOption.WRITE); |
| 181 | + var writer = io.github.dfa1.vortex.writer.VortexWriter.create(channel, schema, WriteOptions.defaults())) { |
| 182 | + writer.writeChunk(Map.of(V, data)); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + private static boolean hasDictLayout(Path file) throws IOException { |
| 187 | + try (VortexReader reader = VortexReader.open(file)) { |
| 188 | + return hasDictLayout(InspectorTree.build(reader).root()); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + private static boolean hasDictLayout(InspectorTree.Node node) { |
| 193 | + if (node.layout().isDict()) { |
| 194 | + return true; |
| 195 | + } |
| 196 | + return node.children().stream().anyMatch(DictAdmissionInteropIntegrationTest::hasDictLayout); |
| 197 | + } |
| 198 | +} |
0 commit comments