|
1 | 1 | package io.github.dfa1.vortex.encoding; |
2 | 2 |
|
3 | 3 | import com.google.protobuf.InvalidProtocolBufferException; |
| 4 | +import io.airlift.compress.zstd.ZstdCompressor; |
4 | 5 | import io.airlift.compress.zstd.ZstdDecompressor; |
5 | 6 | import io.github.dfa1.vortex.core.ArrayStats; |
6 | 7 | import io.github.dfa1.vortex.core.DType; |
|
17 | 18 | import io.github.dfa1.vortex.core.array.VarBinArray; |
18 | 19 | import io.github.dfa1.vortex.proto.EncodingProtos; |
19 | 20 |
|
| 21 | +import java.lang.foreign.Arena; |
20 | 22 | import java.lang.foreign.MemorySegment; |
21 | 23 | import java.lang.foreign.ValueLayout; |
22 | 24 | import java.nio.ByteBuffer; |
| 25 | +import java.nio.charset.StandardCharsets; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.List; |
23 | 28 |
|
24 | | -/// Decoder for {@code vortex.zstd} — Zstandard-compressed columnar array. |
| 29 | +/// Encoder/decoder for {@code vortex.zstd} — Zstandard-compressed columnar array. |
25 | 30 | /// |
26 | 31 | /// <p>Wire format: |
27 | 32 | /// <ul> |
|
30 | 35 | /// <li>Child 0: validity bitmap (optional; this decoder rejects nullable arrays)</li> |
31 | 36 | /// </ul> |
32 | 37 | /// |
33 | | -/// <p>Primitive dtype: decompressed bytes are raw LE values → returned as typed primitive array. |
34 | | -/// <p>Utf8/Binary dtype: decompressed bytes interleave {@code [u32-LE length][data]} per valid string. |
| 38 | +/// <p>Primitive dtype: raw LE values compressed into a single frame. |
| 39 | +/// <p>Utf8/Binary dtype: {@code [u32-LE length][data]} per string, compressed into a single frame. |
35 | 40 | /// |
36 | | -/// <p>Scope: decode only, no dictionary, non-nullable. |
| 41 | +/// <p>No dictionary, no nullable (validity child not supported). |
37 | 42 | public final class ZstdEncoding implements Encoding { |
38 | 43 |
|
39 | 44 | @Override |
40 | 45 | public EncodingId encodingId() { |
41 | 46 | return EncodingId.VORTEX_ZSTD; |
42 | 47 | } |
43 | 48 |
|
| 49 | + @Override |
| 50 | + public boolean accepts(DType dtype) { |
| 51 | + return dtype instanceof DType.Primitive || dtype instanceof DType.Utf8 || dtype instanceof DType.Binary; |
| 52 | + } |
| 53 | + |
44 | 54 | @Override |
45 | 55 | public EncodeResult encode(DType dtype, Object data) { |
46 | | - throw new UnsupportedOperationException("encode not supported by " + encodingId()); |
| 56 | + return Encoder.encode(dtype, data); |
47 | 57 | } |
48 | 58 |
|
49 | 59 | @Override |
50 | 60 | public Array decode(DecodeContext ctx) { |
51 | 61 | return Decoder.decode(ctx); |
52 | 62 | } |
53 | 63 |
|
| 64 | + private static final class Encoder { |
| 65 | + |
| 66 | + private static EncodeResult encode(DType dtype, Object data) { |
| 67 | + if (dtype instanceof DType.Primitive dt) { |
| 68 | + return encodePrimitive(dt, data); |
| 69 | + } |
| 70 | + if (dtype instanceof DType.Utf8 || dtype instanceof DType.Binary) { |
| 71 | + return encodeVarBin(dtype, (String[]) data); |
| 72 | + } |
| 73 | + throw new VortexException(EncodingId.VORTEX_ZSTD, "unsupported dtype: " + dtype); |
| 74 | + } |
| 75 | + |
| 76 | + private static EncodeResult encodePrimitive(DType.Primitive dt, Object data) { |
| 77 | + MemorySegment raw = primitiveToLeBytes(dt.ptype(), data); |
| 78 | + long n = primitiveLength(dt.ptype(), data); |
| 79 | + byte[] rawBytes = raw.toArray(ValueLayout.JAVA_BYTE); |
| 80 | + return buildResult(rawBytes, n); |
| 81 | + } |
| 82 | + |
| 83 | + private static EncodeResult encodeVarBin(DType dtype, String[] strings) { |
| 84 | + byte[] raw = buildLengthPrefixed(strings); |
| 85 | + return buildResult(raw, strings.length); |
| 86 | + } |
| 87 | + |
| 88 | + private static EncodeResult buildResult(byte[] raw, long n) { |
| 89 | + byte[] compressed = compress(raw); |
| 90 | + byte[] meta = EncodingProtos.ZstdMetadata.newBuilder() |
| 91 | + .setDictionarySize(0) |
| 92 | + .addFrames(EncodingProtos.ZstdFrameMetadata.newBuilder() |
| 93 | + .setUncompressedSize(raw.length) |
| 94 | + .setNValues(n)) |
| 95 | + .build().toByteArray(); |
| 96 | + EncodeNode root = new EncodeNode(EncodingId.VORTEX_ZSTD, ByteBuffer.wrap(meta), |
| 97 | + new EncodeNode[0], new int[]{0}); |
| 98 | + return new EncodeResult(root, List.of(MemorySegment.ofArray(compressed)), null, null); |
| 99 | + } |
| 100 | + |
| 101 | + private static byte[] compress(byte[] input) { |
| 102 | + ZstdCompressor compressor = new ZstdCompressor(); |
| 103 | + byte[] out = new byte[compressor.maxCompressedLength(input.length)]; |
| 104 | + int len = compressor.compress(input, 0, input.length, out, 0, out.length); |
| 105 | + return Arrays.copyOf(out, len); |
| 106 | + } |
| 107 | + |
| 108 | + private static MemorySegment primitiveToLeBytes(PType ptype, Object data) { |
| 109 | + return switch (ptype) { |
| 110 | + case I8, U8 -> MemorySegment.ofArray((byte[]) data); |
| 111 | + case I16, U16, F16 -> { |
| 112 | + short[] arr = (short[]) data; |
| 113 | + MemorySegment seg = Arena.ofAuto().allocate((long) arr.length * 2, 2); |
| 114 | + for (int i = 0; i < arr.length; i++) { |
| 115 | + seg.setAtIndex(PTypeIO.LE_SHORT, i, arr[i]); |
| 116 | + } |
| 117 | + yield seg; |
| 118 | + } |
| 119 | + case I32, U32 -> { |
| 120 | + int[] arr = (int[]) data; |
| 121 | + MemorySegment seg = Arena.ofAuto().allocate((long) arr.length * 4, 4); |
| 122 | + for (int i = 0; i < arr.length; i++) { |
| 123 | + seg.setAtIndex(PTypeIO.LE_INT, i, arr[i]); |
| 124 | + } |
| 125 | + yield seg; |
| 126 | + } |
| 127 | + case I64, U64 -> { |
| 128 | + long[] arr = (long[]) data; |
| 129 | + MemorySegment seg = Arena.ofAuto().allocate((long) arr.length * 8, 8); |
| 130 | + for (int i = 0; i < arr.length; i++) { |
| 131 | + seg.setAtIndex(PTypeIO.LE_LONG, i, arr[i]); |
| 132 | + } |
| 133 | + yield seg; |
| 134 | + } |
| 135 | + case F32 -> { |
| 136 | + float[] arr = (float[]) data; |
| 137 | + MemorySegment seg = Arena.ofAuto().allocate((long) arr.length * 4, 4); |
| 138 | + for (int i = 0; i < arr.length; i++) { |
| 139 | + seg.setAtIndex(PTypeIO.LE_FLOAT, i, arr[i]); |
| 140 | + } |
| 141 | + yield seg; |
| 142 | + } |
| 143 | + case F64 -> { |
| 144 | + double[] arr = (double[]) data; |
| 145 | + MemorySegment seg = Arena.ofAuto().allocate((long) arr.length * 8, 8); |
| 146 | + for (int i = 0; i < arr.length; i++) { |
| 147 | + seg.setAtIndex(PTypeIO.LE_DOUBLE, i, arr[i]); |
| 148 | + } |
| 149 | + yield seg; |
| 150 | + } |
| 151 | + }; |
| 152 | + } |
| 153 | + |
| 154 | + private static long primitiveLength(PType ptype, Object data) { |
| 155 | + return switch (ptype) { |
| 156 | + case I8, U8 -> ((byte[]) data).length; |
| 157 | + case I16, U16, F16 -> ((short[]) data).length; |
| 158 | + case I32, U32 -> ((int[]) data).length; |
| 159 | + case F32 -> ((float[]) data).length; |
| 160 | + case I64, U64 -> ((long[]) data).length; |
| 161 | + case F64 -> ((double[]) data).length; |
| 162 | + }; |
| 163 | + } |
| 164 | + |
| 165 | + private static byte[] buildLengthPrefixed(String[] strings) { |
| 166 | + int total = 0; |
| 167 | + byte[][] encoded = new byte[strings.length][]; |
| 168 | + for (int i = 0; i < strings.length; i++) { |
| 169 | + encoded[i] = strings[i].getBytes(StandardCharsets.UTF_8); |
| 170 | + total += 4 + encoded[i].length; |
| 171 | + } |
| 172 | + MemorySegment seg = Arena.ofAuto().allocate(total > 0 ? total : 1); |
| 173 | + long pos = 0; |
| 174 | + for (byte[] bytes : encoded) { |
| 175 | + seg.set(PTypeIO.LE_INT, pos, bytes.length); |
| 176 | + pos += 4; |
| 177 | + MemorySegment.copy(MemorySegment.ofArray(bytes), 0, seg, pos, bytes.length); |
| 178 | + pos += bytes.length; |
| 179 | + } |
| 180 | + return seg.asSlice(0, total).toArray(ValueLayout.JAVA_BYTE); |
| 181 | + } |
| 182 | + } |
| 183 | + |
54 | 184 | private static final class Decoder { |
55 | 185 |
|
56 | 186 | private static Array decode(DecodeContext ctx) { |
|
0 commit comments