From ebf03b6b93af1fa62284096358c1152efdf8fa5e Mon Sep 17 00:00:00 2001 From: Yongzao <532741407@qq.com> Date: Wed, 25 Nov 2026 14:26:27 +0800 Subject: [PATCH 1/2] feat(java): add read-only ALP v1 decoding Generated-by: OpenAI Codex --- cpp/tools/skills/tsfile-cli/SKILL.md | 2 +- .../apache/tsfile/i18n/messages.properties | 3 + .../apache/tsfile/i18n/messages_zh.properties | 3 + .../tsfile/encoding/decoder/AlpDecoder.java | 278 ++++++++++++++++++ .../file/metadata/enums/TSEncoding.java | 9 +- .../tsfile/read/common/type/DoubleType.java | 2 + .../tsfile/read/common/type/FloatType.java | 2 + .../apache/tsfile/read/AlpCppInteropTest.java | 159 ++++++++++ java/tsfile/src/test/resources/alp/README.md | 50 ++++ .../test/resources/alp/alp-empty-float.bin | Bin 0 -> 16 bytes .../resources/alp/alp-vector-pages.tsfile | Bin 0 -> 6320 bytes .../tsfile/src/test/resources/alp/generate.cc | 162 ++++++++++ python/tsfile/constants.py | 4 +- skills/tsfile-cli/SKILL.md | 2 +- skills/tsfile/references/compatibility.md | 6 +- .../tsfile/references/encoding-compression.md | 8 +- 16 files changed, 676 insertions(+), 14 deletions(-) create mode 100644 java/tsfile/src/main/java/org/apache/tsfile/encoding/decoder/AlpDecoder.java create mode 100644 java/tsfile/src/test/java/org/apache/tsfile/read/AlpCppInteropTest.java create mode 100644 java/tsfile/src/test/resources/alp/README.md create mode 100644 java/tsfile/src/test/resources/alp/alp-empty-float.bin create mode 100644 java/tsfile/src/test/resources/alp/alp-vector-pages.tsfile create mode 100644 java/tsfile/src/test/resources/alp/generate.cc diff --git a/cpp/tools/skills/tsfile-cli/SKILL.md b/cpp/tools/skills/tsfile-cli/SKILL.md index 0bebbe565e..936405ab85 100644 --- a/cpp/tools/skills/tsfile-cli/SKILL.md +++ b/cpp/tools/skills/tsfile-cli/SKILL.md @@ -116,7 +116,7 @@ COMP ∈ UNCOMPRESSED, SNAPPY, GZIP, LZO, LZ4, ZSTD, LZMA2 and GORILLA_V1 for FLOAT/DOUBLE, and RLE for BOOLEAN/FLOAT/DOUBLE. GORILLA_V1 rejects NaN (reserved end marker), but accepts NULL and infinities. FLOAT/DOUBLE RLE uses the SDK's two-decimal scaling and may be lossy. - ALP is C++/Python-only, not Java-compatible. LZMA2 requires an enabled build. + Java builds with ALP v1 decoding can read these files. LZMA2 requires an enabled build. - CSV null and backslash rules: see `references/commands.md` before preparing input. - `DATE` cells are `YYYY-MM-DD`; `TIMESTAMP` and row `time` use strict decimal int64. - **timestamps must be strictly increasing per device** (device = tag-column values); diff --git a/java/common/src/main/resources/org/apache/tsfile/i18n/messages.properties b/java/common/src/main/resources/org/apache/tsfile/i18n/messages.properties index 3700a46f61..239ccf86ff 100644 --- a/java/common/src/main/resources/org/apache/tsfile/i18n/messages.properties +++ b/java/common/src/main/resources/org/apache/tsfile/i18n/messages.properties @@ -688,6 +688,9 @@ error.encoding.float_decoder_method_not_supported = Method %1$s is not supported error.encoding.plain_decoder_bigdecimal_unsupported = Method readBigDecimal is not supported by PlainDecoder # Decoder — method not supported (8 sites, 1 key) +error.encoding.alp_malformed = Malformed ALP v1 page +error.encoding.alp_no_more_values = No more ALP values in this page +error.encoding.alp_type_not_supported = ALP decoder does not support %1$s error.encoding.decoder_method_not_supported = Method %1$s is not supported by Decoder # Decoder.getDecoderByType — encoding+type combination not found (many sites, 1 key using ERROR_MSG) diff --git a/java/common/src/main/resources/org/apache/tsfile/i18n/messages_zh.properties b/java/common/src/main/resources/org/apache/tsfile/i18n/messages_zh.properties index e86263ab0f..8c74fa4676 100644 --- a/java/common/src/main/resources/org/apache/tsfile/i18n/messages_zh.properties +++ b/java/common/src/main/resources/org/apache/tsfile/i18n/messages_zh.properties @@ -688,6 +688,9 @@ error.encoding.float_decoder_method_not_supported = FloatDecoder 不支持方法 error.encoding.plain_decoder_bigdecimal_unsupported = PlainDecoder 不支持 readBigDecimal 方法 # Decoder — method not supported (8 sites, 1 key) +error.encoding.alp_malformed = ALP v1 Page 格式无效 +error.encoding.alp_no_more_values = 当前 Page 中没有更多 ALP 值 +error.encoding.alp_type_not_supported = ALP Decoder 不支持 %1$s error.encoding.decoder_method_not_supported = Decoder 不支持方法 %1$s # Decoder.getDecoderByType — encoding+type combination not found (many sites, 1 key using ERROR_MSG) diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/decoder/AlpDecoder.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/decoder/AlpDecoder.java new file mode 100644 index 0000000000..b182226f8f --- /dev/null +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/decoder/AlpDecoder.java @@ -0,0 +1,278 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tsfile.encoding.decoder; + +import org.apache.tsfile.enums.TSDataType; +import org.apache.tsfile.exception.encoding.TsFileDecodingException; +import org.apache.tsfile.file.metadata.enums.TSEncoding; +import org.apache.tsfile.i18n.Messages; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; + +/** Reads the C++ TsFile ALP v1 value stream, one block at a time. */ +public final class AlpDecoder extends Decoder { + + private static final int VERSION = 1; + private static final int PAGE_HEADER_BYTES = 16; + private static final int BLOCK_HEADER_BYTES = 28; + private static final int MAX_BLOCK_VALUES = 1024; + private static final int BODY_ALIGNMENT = 32; + private static final int BODY_PADDING = 16; + private static final int SCHEME_ALP = 0; + private static final int SCHEME_PLAIN = 1; + + private static final long[] FACTORS = { + 1L, + 10L, + 100L, + 1000L, + 10000L, + 100000L, + 1000000L, + 10000000L, + 100000000L, + 1000000000L, + 10000000000L, + 100000000000L, + 1000000000000L, + 10000000000000L, + 100000000000000L, + 1000000000000000L, + 10000000000000000L, + 100000000000000000L, + 1000000000000000000L + }; + + // Match the C++ constants and calculate in the column's own precision. + private static final float[] FLOAT_FRACTIONS = { + 1f, 1e-1f, 1e-2f, 1e-3f, 1e-4f, 1e-5f, 1e-6f, 1e-7f, 1e-8f, 1e-9f, 1e-10f + }; + + private static final double[] DOUBLE_FRACTIONS = { + 1d, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6, 1e-7, 1e-8, 1e-9, 1e-10, 1e-11, 1e-12, 1e-13, 1e-14, + 1e-15, 1e-16, 1e-17, 1e-18 + }; + + private final boolean floatType; + private final int valueBytes; + private final int maxExponent; + private ByteBuffer page; + private int blocksRemaining; + private long valuesRemaining; + private int blockValues; + private int index; + private int scheme; + private int factor; + private int exponent; + private long base; + private int width; + private int bitmapOffset; + private int exceptionOffset; + private int bodyOffset; + + public AlpDecoder(TSDataType dataType) { + super(TSEncoding.ALP); + if (dataType != TSDataType.FLOAT && dataType != TSDataType.DOUBLE) { + throw new TsFileDecodingException( + Messages.format("error.encoding.alp_type_not_supported", dataType)); + } + floatType = dataType == TSDataType.FLOAT; + valueBytes = floatType ? Float.BYTES : Double.BYTES; + maxExponent = (floatType ? FLOAT_FRACTIONS.length : DOUBLE_FRACTIONS.length) - 1; + } + + @Override + public boolean hasNext(ByteBuffer buffer) { + if (index < blockValues) { + return true; + } + if (page == null) { + if (!buffer.hasRemaining()) { + return false; + } + readPageHeader(buffer); + } + if (blocksRemaining == 0) { + return false; + } + readBlock(buffer); + return true; + } + + @Override + public float readFloat(ByteBuffer buffer) { + return floatType ? Float.intBitsToFloat((int) readValueBits(buffer)) : super.readFloat(buffer); + } + + @Override + public double readDouble(ByteBuffer buffer) { + return floatType ? super.readDouble(buffer) : Double.longBitsToDouble(readValueBits(buffer)); + } + + @Override + public void reset() { + page = null; + index = 0; + blockValues = 0; + } + + private void readPageHeader(ByteBuffer buffer) { + // Do not change the caller's byte order; TsFile's outer headers use their own order. + ByteBuffer input = buffer.duplicate().order(ByteOrder.LITTLE_ENDIAN); + if (input.remaining() < PAGE_HEADER_BYTES + || Byte.toUnsignedInt(input.get()) != VERSION + || Byte.toUnsignedInt(input.get()) != (floatType ? 0 : 1) + || input.getShort() != 0) { + throw malformed(); + } + valuesRemaining = Integer.toUnsignedLong(input.getInt()); + blocksRemaining = input.getInt(); + if (input.getInt() != 0 + || blocksRemaining < 0 + || blocksRemaining > input.remaining() / BLOCK_HEADER_BYTES + || valuesRemaining < blocksRemaining + || valuesRemaining > (long) blocksRemaining * MAX_BLOCK_VALUES + || (blocksRemaining == 0 && input.hasRemaining())) { + throw malformed(); + } + page = input; + buffer.position(input.position()); + } + + private void readBlock(ByteBuffer buffer) { + if (page.remaining() < BLOCK_HEADER_BYTES) { + throw malformed(); + } + scheme = Byte.toUnsignedInt(page.get()); + factor = Byte.toUnsignedInt(page.get()); + exponent = Byte.toUnsignedInt(page.get()); + page.get(); // Flags are advisory; the exception count determines the bitmap layout. + int count = page.getInt(); + int exceptions = page.getInt(); + int bodyBytes = page.getInt(); + base = page.getLong(); + width = Byte.toUnsignedInt(page.get()); + page.position(page.position() + 3); // Block reserved bytes. + if (count <= 0 + || count > MAX_BLOCK_VALUES + || exceptions < 0 + || exceptions > count + || width > valueBytes * Byte.SIZE + || factor > exponent + || exponent > maxExponent) { + throw malformed(); + } + + bitmapOffset = page.position(); + int bitmapBytes = 0; + int exceptionBytes = 0; + int expectedBodyBytes; + if (scheme == SCHEME_PLAIN) { + expectedBodyBytes = count * valueBytes; + } else if (scheme == SCHEME_ALP) { + bitmapBytes = exceptions == 0 ? 0 : (count + 7) / 8; + exceptionBytes = exceptions * valueBytes; + int packedBytes = (count * width + 7) / 8; + expectedBodyBytes = + width == 0 ? 0 : (packedBytes + BODY_PADDING + BODY_ALIGNMENT - 1) & -BODY_ALIGNMENT; + } else { + throw malformed(); // Includes the reserved ALP_RD scheme. + } + + if (bodyBytes != expectedBodyBytes + || bitmapBytes + exceptionBytes + bodyBytes > page.remaining()) { + throw malformed(); + } + if (bitmapBytes > 0) { + int marked = 0; + for (int i = 0; i < bitmapBytes; i++) { + int validBits = Math.min(8, count - i * 8); + marked += Integer.bitCount(page.get(bitmapOffset + i) & ((1 << validBits) - 1)); + } + if (marked != exceptions) { + throw malformed(); + } + } + + exceptionOffset = bitmapOffset + bitmapBytes; + bodyOffset = exceptionOffset + exceptionBytes; + page.position(bodyOffset + bodyBytes); + valuesRemaining -= count; + blocksRemaining--; + if (valuesRemaining < blocksRemaining + || valuesRemaining > (long) blocksRemaining * MAX_BLOCK_VALUES + || (blocksRemaining == 0 && page.hasRemaining())) { + throw malformed(); + } + buffer.position(page.position()); + index = 0; + blockValues = count; + } + + private long readValueBits(ByteBuffer buffer) { + if (!hasNext(buffer)) { + throw new TsFileDecodingException(Messages.get("error.encoding.alp_no_more_values")); + } + int i = index++; + if (scheme == SCHEME_PLAIN) { + return rawBits(bodyOffset + i * valueBytes); + } + if (exceptionOffset > bitmapOffset + && (page.get(bitmapOffset + (i >>> 3)) & (1 << (i & 7))) != 0) { + long bits = rawBits(exceptionOffset); + exceptionOffset += valueBytes; + return bits; + } + + // Addition wraps modulo 2^32 (FLOAT) or 2^64 (DOUBLE), then the bits are read as signed. + long encoded = base + unpackDelta(i); + return floatType + ? Float.floatToRawIntBits( + (float) (int) encoded * (float) FACTORS[factor] * FLOAT_FRACTIONS[exponent]) + : Double.doubleToRawLongBits( + (double) encoded * (double) FACTORS[factor] * DOUBLE_FRACTIONS[exponent]); + } + + private long rawBits(int offset) { + return floatType ? page.getInt(offset) : page.getLong(offset); + } + + private long unpackDelta(int i) { + if (width == 0) { + return 0; + } + int bit = i * width; + int offset = bodyOffset + (bit >>> 3); + if (width == Long.SIZE) { + return page.getLong(offset); + } + int shift = bit & 7; + long value = page.getLong(offset) >>> shift; + if (width + shift > Long.SIZE) { + value |= (long) Byte.toUnsignedInt(page.get(offset + Long.BYTES)) << (Long.SIZE - shift); + } + return value & ((1L << width) - 1); + } + + private static TsFileDecodingException malformed() { + return new TsFileDecodingException(Messages.get("error.encoding.alp_malformed")); + } +} diff --git a/java/tsfile/src/main/java/org/apache/tsfile/file/metadata/enums/TSEncoding.java b/java/tsfile/src/main/java/org/apache/tsfile/file/metadata/enums/TSEncoding.java index e3f522c613..9d7adccb71 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/file/metadata/enums/TSEncoding.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/file/metadata/enums/TSEncoding.java @@ -43,7 +43,9 @@ public enum TSEncoding { CHIMP((byte) 11), SPRINTZ((byte) 12), RLBE((byte) 13), - CAMEL((byte) 14); + CAMEL((byte) 14), + // Readable for FLOAT and DOUBLE; deliberately absent from the Java writer support map. + ALP((byte) 15); private final byte type; @SuppressWarnings("java:S2386") // used by other projects @@ -145,10 +147,9 @@ private static TSEncoding getTsEncoding(byte encoding) { return TSEncoding.RLBE; case 14: return TSEncoding.CAMEL; + case 15: + return TSEncoding.ALP; default: - // Encoding ids above CAMEL (15 = ALP) are written by the C++/Python - // implementations only; see compat/compatibility-matrix.csv. A file - // containing such a chunk opens, but reading that series fails here. throw new IllegalArgumentException(Messages.format("error.common.invalid_input", encoding)); } } diff --git a/java/tsfile/src/main/java/org/apache/tsfile/read/common/type/DoubleType.java b/java/tsfile/src/main/java/org/apache/tsfile/read/common/type/DoubleType.java index 14c6160be6..793bd1f78e 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/read/common/type/DoubleType.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/read/common/type/DoubleType.java @@ -23,6 +23,7 @@ import org.apache.tsfile.block.column.ColumnBuilder; import org.apache.tsfile.block.column.ColumnBuilderStatus; import org.apache.tsfile.common.conf.TSFileConfig; +import org.apache.tsfile.encoding.decoder.AlpDecoder; import org.apache.tsfile.encoding.decoder.CamelDecoder; import org.apache.tsfile.encoding.decoder.Decoder; import org.apache.tsfile.encoding.decoder.DoublePrecisionChimpDecoder; @@ -156,6 +157,7 @@ public Decoder getDecoder(TSEncoding encoding) { case SPRINTZ -> new DoubleSprintzDecoder(); case RLBE -> new DoubleRLBEDecoder(); case CAMEL -> new CamelDecoder(); + case ALP -> new AlpDecoder(TSDataType.DOUBLE); default -> throw decoderNotFound(encoding); }; } diff --git a/java/tsfile/src/main/java/org/apache/tsfile/read/common/type/FloatType.java b/java/tsfile/src/main/java/org/apache/tsfile/read/common/type/FloatType.java index 0300b16292..4740183e8e 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/read/common/type/FloatType.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/read/common/type/FloatType.java @@ -23,6 +23,7 @@ import org.apache.tsfile.block.column.ColumnBuilder; import org.apache.tsfile.block.column.ColumnBuilderStatus; import org.apache.tsfile.common.conf.TSFileConfig; +import org.apache.tsfile.encoding.decoder.AlpDecoder; import org.apache.tsfile.encoding.decoder.Decoder; import org.apache.tsfile.encoding.decoder.FloatDecoder; import org.apache.tsfile.encoding.decoder.FloatRLBEDecoder; @@ -154,6 +155,7 @@ public Decoder getDecoder(TSEncoding encoding) { case CHIMP -> new SinglePrecisionChimpDecoder(); case SPRINTZ -> new FloatSprintzDecoder(); case RLBE -> new FloatRLBEDecoder(); + case ALP -> new AlpDecoder(TSDataType.FLOAT); default -> throw decoderNotFound(encoding); }; } diff --git a/java/tsfile/src/test/java/org/apache/tsfile/read/AlpCppInteropTest.java b/java/tsfile/src/test/java/org/apache/tsfile/read/AlpCppInteropTest.java new file mode 100644 index 0000000000..5909462f1b --- /dev/null +++ b/java/tsfile/src/test/java/org/apache/tsfile/read/AlpCppInteropTest.java @@ -0,0 +1,159 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.tsfile.read; + +import org.apache.tsfile.encoding.decoder.Decoder; +import org.apache.tsfile.enums.TSDataType; +import org.apache.tsfile.file.metadata.TableSchema; +import org.apache.tsfile.file.metadata.enums.TSEncoding; +import org.apache.tsfile.read.query.dataset.ResultSet; +import org.apache.tsfile.read.v4.DeviceTableModelReader; + +import org.junit.Assert; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; +import java.util.Arrays; +import java.util.Map; + +public class AlpCppInteropTest { + + private static final int PAGE_ROWS = 1103; + private static final int ROWS = 3 * PAGE_ROWS + 32; + + private static float expectedFloat(int row) { + if (row >= 3 * PAGE_ROWS) { + row = row % 4 == 0 ? 7 : row % 4 == 1 ? 8 : 1023; + } else if (row >= 2 * PAGE_ROWS) { + return -7.25f; + } + switch (row) { + case 7: + return Float.intBitsToFloat(0x7fc12345); + case 8: + return Float.POSITIVE_INFINITY; + case 1023: + return Float.NEGATIVE_INFINITY; + case 1024: + return -0.0f; + case 1099: + return Float.MIN_VALUE; + case 600: + return 0.1234567f; + default: + return ((float) (row % 61 - 30)) * 0.01f; + } + } + + private static double expectedDouble(int row) { + if (row >= 3 * PAGE_ROWS) { + row = row % 4 == 0 ? 7 : row % 4 == 1 ? 8 : 1023; + } else if (row >= 2 * PAGE_ROWS) { + return 42.125; + } + switch (row) { + case 7: + return Double.longBitsToDouble(0xfff8123456789abcL); + case 8: + return Double.POSITIVE_INFINITY; + case 1023: + return Double.NEGATIVE_INFINITY; + case 1024: + return -0.0; + case 1099: + return Double.MIN_VALUE; + case 600: + return 0.123456789012345; + default: + return ((double) (row % 127 - 63)) * 0.0001; + } + } + + @Test + public void readsCppAlpPagesAndMatchesEveryValueBitForBit() throws Exception { + File tsFile = copyFixture(); + try { + try (TsFileSequenceReader sequenceReader = new TsFileSequenceReader(tsFile.getPath())) { + Map schemas = sequenceReader.getTableSchemaMap(); + Assert.assertTrue("C++ table schema was not read", schemas.containsKey("alp_table")); + Assert.assertEquals( + TSDataType.FLOAT, schemas.get("alp_table").getColumnSchemas().get(2).getType()); + Assert.assertEquals( + TSDataType.DOUBLE, schemas.get("alp_table").getColumnSchemas().get(3).getType()); + } + + try (DeviceTableModelReader reader = new DeviceTableModelReader(tsFile); + ResultSet rows = reader.query("alp_table", Arrays.asList("keep", "f", "d"), 0, ROWS)) { + int rowCount = 0; + while (rows.next()) { + int row = (int) rows.getLong(1); + Assert.assertEquals("timestamp and keep column differ", row, rows.getInt(2)); + Assert.assertTrue("row ordering changed", row == rowCount); + boolean expectNull = row >= PAGE_ROWS && row < 2 * PAGE_ROWS; + Assert.assertEquals("FLOAT null bitmap at row " + row, expectNull, rows.isNull(3)); + Assert.assertEquals("DOUBLE null bitmap at row " + row, expectNull, rows.isNull(4)); + if (!expectNull) { + Assert.assertEquals( + "FLOAT bits at row " + row, + Float.floatToRawIntBits(expectedFloat(row)), + Float.floatToRawIntBits(rows.getFloat(3))); + Assert.assertEquals( + "DOUBLE bits at row " + row, + Double.doubleToRawLongBits(expectedDouble(row)), + Double.doubleToRawLongBits(rows.getDouble(4))); + } + rowCount++; + } + Assert.assertEquals(ROWS, rowCount); + } + } finally { + Files.deleteIfExists(tsFile.toPath()); + } + } + + @Test + public void decoderFactoryAcceptsCppEmptyPage() throws Exception { + byte[] bytes; + try (InputStream input = + AlpCppInteropTest.class.getResourceAsStream("/alp/alp-empty-float.bin")) { + Assert.assertNotNull("Missing C++ empty ALP page", input); + bytes = input.readAllBytes(); + } + Assert.assertEquals(16, bytes.length); + ByteBuffer page = ByteBuffer.wrap(bytes); + Decoder decoder = Decoder.getDecoderByType(TSEncoding.ALP, TSDataType.FLOAT); + Assert.assertFalse(decoder.hasNext(page)); + Assert.assertFalse(page.hasRemaining()); + } + + private static File copyFixture() throws IOException { + File file = Files.createTempFile("tsfile-cpp-alp-", ".tsfile").toFile(); + try (InputStream input = + AlpCppInteropTest.class.getResourceAsStream("/alp/alp-vector-pages.tsfile")) { + Assert.assertNotNull("Missing checked-in C++ fixture", input); + Files.copy(input, file.toPath(), StandardCopyOption.REPLACE_EXISTING); + } + return file; + } +} diff --git a/java/tsfile/src/test/resources/alp/README.md b/java/tsfile/src/test/resources/alp/README.md new file mode 100644 index 0000000000..ccbf42c032 --- /dev/null +++ b/java/tsfile/src/test/resources/alp/README.md @@ -0,0 +1,50 @@ + + +# C++ ALP interop fixture + +alp-vector-pages.tsfile is generated by generate.cc with the C++ TsFile writer +in this source revision. The uncompressed fixture is about 6 KiB. It has FLOAT +and DOUBLE fields split into 1,103-row pages, so pages cross the 1,024 value +ALP block boundary. It includes a fully null page, constant-value page, +fixed-decimal values, occasional ALP exceptions, NaN and infinities, negative +zero, and subnormal values. The tail page contains special values. +The companion alp-empty-float.bin is the C++ ALP v1 encoding of an empty FLOAT +page (16-byte header, zero values, zero blocks). + +The checked-in binary is the Java test input; the C++ source documents how the +file was constructed. To regenerate on macOS from the repository root: + +```bash +cmake -S cpp -B /private/tmp/tsfile-alp-fixture \ + -DCMAKE_BUILD_TYPE=Release -DBUILD_TEST=OFF -DBUILD_TOOLS=OFF \ + -DENABLE_ANTLR4=OFF -DENABLE_SNAPPY=OFF -DENABLE_LZ4=OFF \ + -DENABLE_LZOKAY=OFF -DENABLE_ZLIB=OFF -DENABLE_ZSTD=OFF \ + -DENABLE_LZMA2=OFF -DENABLE_SIMD=OFF +cmake --build /private/tmp/tsfile-alp-fixture --target tsfile --parallel 5 +c++ -std=c++11 -Icpp/src java/tsfile/src/test/resources/alp/generate.cc \ + -L/private/tmp/tsfile-alp-fixture/lib -ltsfile \ + -Wl,-rpath,/private/tmp/tsfile-alp-fixture/lib \ + -o /private/tmp/tsfile-alp-generate +/private/tmp/tsfile-alp-generate \ + java/tsfile/src/test/resources/alp/alp-vector-pages.tsfile \ + java/tsfile/src/test/resources/alp/alp-empty-float.bin +``` diff --git a/java/tsfile/src/test/resources/alp/alp-empty-float.bin b/java/tsfile/src/test/resources/alp/alp-empty-float.bin new file mode 100644 index 0000000000000000000000000000000000000000..49689ea221e10e5adafdbd5a54242f10b3ea92ae GIT binary patch literal 16 KcmZQ%Km`B*AOHdY literal 0 HcmV?d00001 diff --git a/java/tsfile/src/test/resources/alp/alp-vector-pages.tsfile b/java/tsfile/src/test/resources/alp/alp-vector-pages.tsfile new file mode 100644 index 0000000000000000000000000000000000000000..50f22e0f1232a37028aeb5adbf0c7f39b696cac8 GIT binary patch literal 6320 zcmeHM4>(lm8b4>|Ff(RElTay>RJQD7%gUB&vbfe-$qvbt(99$|Y0~84ibLqH(*G!m zl2t1yD*Yk3a^=cO$a>_;m9^H&x{{UaerL`(=8TOdYrDU0p9~~CvcHOj2~fAXN#=C)yy=&QL-xFWNl2Z4*ym? zuC-PJ{!~4#{iO!{nR=YzsR3^QxSH8A*HLH7Ob4|9aef3h6e|%#%vPaCNf2-XXPJVs zI{dtPTxYfhJX1Z+o}&T3s2=Cc1-P2oaxNJg^MDEvrw8Y{9Kf;RlSE93)3cf;dZv#+ zddci$_OD<5(bsr`7=o8XJYj6y_LR3OuyKd+J9p=pw`7ONn$d|u|AN8n)*ujs?Kzwf z7y>f*LFefZR7#AL0c4c01X1y~u=nW+y_Xv9WzBs%ia+>u4F8SO(w5YcRfIf>qW%I4 zV+f58EDf@qmAqGEpX1FxeNa-?6~6L(a*)G<6t}OB2bTvI`CU6Lb-L0Na{FvZMfXtj zgYz4~pR^om>>oi_w;({d$u+d}Wm%8|rLfcrdXY-se2; z`2LQ#${?GwhdSvx`l{F=Pu&-XAuy4Ubg^ecY^js;(z|MXR2% z!K8hT?UPf4{FE;`r%XTLmgCty^-O(8(S}|l6T`!m;{IuAUU^Mh2WQMWRnxU|c;>mr z-l1Kqcsyf$okY4>hPT!9Lkx4X6!!%u+4C+m$(E(-S(r~h;&N%qf-IkWpQ}ce^Ul-; z-<-kEZtjaLHnp-a;U0K2XK{}2;xkX?TQ5lSxYDrD=1NQG-4}~&EoU98dbMO(uHTL3 zpO)J3&((FmS-vu_wSPETAh6;Ya2O8u`To`>dd?1uGlZ5#u1+`Fw?|J`lCa-`&>SFzF8>k0Oyx+L|EYvMjIMbyBap9bxx3%DnC7 zovPj}_qi{*)v_(zcV%{C&!3|FN;(Heh>iE39RdPl(Gl}C{@{8{@hxKxfjm?woy~8Z z4ELJIW~_Vg*zwc=H;kQSl<`jBsIRD~k-y4&hxat0-MJnY_vOT-ld$+m63`Kc83jVs z70?Q3JiMMVE{>0IWTYb_8_7XC9=5btY{5DqjSq9V3R%%F2`U8g_BsYHy(WG9=y;$1 z|Egb2lN_8xhxFdE-m=u3El>!G0atNKN=k9UtlLUUPpFpyNMQe|dkOo>0d*rQlsdV_r18TLJi}fX8tNfCQPAuI+a2=3fZK8@w-V_3hl{ zZ*fSpB|Yf!RcUvz%(6zlwKe=|ubn=VzSfv=)Ph}St7q`p6wmcWC%4RKj4(CcKWBaF z{M5?}o9`?#samq3d1-q0ay{gsz~E7=`QwMyPw0+M9XuNn#NR#I`GS%7()r}8eV2ZC zeDf!E#m}ydZ<0EHc|wo1(ofjV+jre6sb6r!dhLmw0Vy}Prwztt*dEP_$jrHOJ$LwF zzFl46uJ)qhe#Nj&oPQs-Nfq?Fwcr0GQ6B#_Z9cGXOwFkQStXZ-${!d@dQ>6<-*Dn}Q1?tcxB5FDvgLu4yxq zrYixgq{#4L1tW+iLYT#qv9gkMIpaTc$W$KbOi7Nnp-+Km61uu>Q01w8oG%6~+1nt6 ze}grSj~pk#J +#include +#include +#include +#include +#include +#include + +#include "common/schema.h" +#include "common/tablet.h" +#include "encoding/alp/alp_scalar.h" +#include "file/write_file.h" +#include "writer/tsfile_table_writer.h" + +namespace { +constexpr uint32_t kPageRows = 1103; +constexpr uint32_t kRows = 3 * kPageRows + 32; + +void check(int code) { + if (code != common::E_OK) { + std::cerr << "TsFile error: " << code << '\n'; + std::exit(EXIT_FAILURE); + } +} + +template +T from_bits(U bits) { + static_assert(sizeof(T) == sizeof(U), "Mismatched bit width"); + T value; + std::memcpy(&value, &bits, sizeof(value)); + return value; +} + +float float_value(uint32_t row) { + if (row >= 3 * kPageRows) { + row = (row % 4 == 0) ? 7 : (row % 4 == 1) ? 8 : 1023; + } else if (row >= 2 * kPageRows) { + return -7.25f; + } + switch (row) { + case 7: + return from_bits(uint32_t{0x7fc12345}); + case 8: + return std::numeric_limits::infinity(); + case 1023: + return -std::numeric_limits::infinity(); + case 1024: + return -0.0f; + case 1099: + return std::numeric_limits::denorm_min(); + case 600: + return 0.1234567f; + default: + return static_cast(static_cast(row % 61) - 30) * + 0.01f; + } +} + +double double_value(uint32_t row) { + if (row >= 3 * kPageRows) { + row = (row % 4 == 0) ? 7 : (row % 4 == 1) ? 8 : 1023; + } else if (row >= 2 * kPageRows) { + return 42.125; + } + switch (row) { + case 7: + return from_bits(uint64_t{0xfff8123456789abc}); + case 8: + return std::numeric_limits::infinity(); + case 1023: + return -std::numeric_limits::infinity(); + case 1024: + return -0.0; + case 1099: + return std::numeric_limits::denorm_min(); + case 600: + return 0.123456789012345; + default: + return static_cast(static_cast(row % 127) - 63) * + 0.0001; + } +} +} // namespace + +int main(int argc, char** argv) { + if (argc != 3) { + std::cerr << "Usage: generate \n"; + return EXIT_FAILURE; + } + + check(storage::libtsfile_init()); + std::vector empty_float_page; + if (storage::alp::AlpEncodePage(NULL, 0, empty_float_page) != + storage::alp::ALP_OK) { + return EXIT_FAILURE; + } + std::ofstream empty_page_file(argv[2], std::ios::binary); + empty_page_file.write(reinterpret_cast(empty_float_page.data()), + empty_float_page.size()); + if (!empty_page_file) { + return EXIT_FAILURE; + } + empty_page_file.close(); + check(storage::set_page_max_point_count(kPageRows)); + common::g_config_value_.time_compress_type_ = common::UNCOMPRESSED; + { + storage::WriteFile file; + check(file.create(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644)); + std::vector columns = { + new storage::MeasurementSchema("tag", common::STRING, common::PLAIN, + common::UNCOMPRESSED), + new storage::MeasurementSchema("keep", common::INT32, + common::TS_2DIFF, + common::UNCOMPRESSED), + new storage::MeasurementSchema("f", common::FLOAT, common::ALP, + common::UNCOMPRESSED), + new storage::MeasurementSchema("d", common::DOUBLE, common::ALP, + common::UNCOMPRESSED)}; + std::vector categories = { + common::ColumnCategory::TAG, common::ColumnCategory::FIELD, + common::ColumnCategory::FIELD, common::ColumnCategory::FIELD}; + storage::TableSchema schema("alp_table", columns, categories); + storage::TsFileTableWriter writer(&file, &schema); + check(writer.get_error()); + + storage::Tablet tablet( + schema.get_table_name(), schema.get_measurement_names(), + schema.get_data_types(), schema.get_column_categories(), kRows); + for (uint32_t row = 0; row < kRows; ++row) { + check(tablet.add_timestamp(row, row)); + check(tablet.add_value(row, "tag", common::String("dev"))); + // Keep rows visible when the entire FLOAT/DOUBLE page is null. + check(tablet.add_value(row, "keep", static_cast(row))); + if (row < kPageRows || row >= 2 * kPageRows) { + check(tablet.add_value(row, "f", float_value(row))); + check(tablet.add_value(row, "d", double_value(row))); + } + } + check(writer.write_table(tablet)); + check(writer.close()); + } + storage::libtsfile_destroy(); +} diff --git a/python/tsfile/constants.py b/python/tsfile/constants.py index aa22a08008..45179063bf 100644 --- a/python/tsfile/constants.py +++ b/python/tsfile/constants.py @@ -200,9 +200,7 @@ class TSEncoding(IntEnum): SPRINTZ = 12 RLBE = 13 CAMEL = 14 - # C++/Python-only for now: the Java implementation has no ALP decoder. - # A file containing an ALP column opens in Java, but querying that - # series fails. Do not use ALP for files shared with Java. + # ALP v1 is writable by C++/Python and readable by Java builds with ALP decoding. ALP = 15 diff --git a/skills/tsfile-cli/SKILL.md b/skills/tsfile-cli/SKILL.md index 7f73e8ce99..0f070a6a15 100644 --- a/skills/tsfile-cli/SKILL.md +++ b/skills/tsfile-cli/SKILL.md @@ -132,7 +132,7 @@ TYPE ∈ { BOOLEAN, INT32, INT64, FLOAT, DOUBLE, STRING, TEXT, TIMESTAMP, DATE, and GORILLA_V1 for FLOAT/DOUBLE, and RLE for BOOLEAN/FLOAT/DOUBLE. GORILLA_V1 rejects NaN (reserved end marker), but accepts NULL and infinities. FLOAT/DOUBLE RLE uses the SDK's two-decimal scaling and may be lossy. - ALP is C++/Python-only, not Java-compatible. LZMA2 requires an enabled build. + Java builds with ALP v1 decoding can read these files. LZMA2 requires an enabled build. - `--stdin` or `-i ` is required; TSV, `--columns`, `--no-header`, and `--header-match` are not supported. - Unquoted `\N` denotes NULL. Empty STRING/TEXT cells are empty strings; diff --git a/skills/tsfile/references/compatibility.md b/skills/tsfile/references/compatibility.md index 72165f4362..30fb5e4870 100644 --- a/skills/tsfile/references/compatibility.md +++ b/skills/tsfile/references/compatibility.md @@ -68,8 +68,10 @@ Repository checks: null, time, text, date, and binary values. 6. Close writers, readers, and result sets to finalize file structures and release native resources. -7. Java cannot query ALP series. Duplicate timestamps are opt-in; check the - file's timestamp-policy property and the consumer's ordering requirements. +7. Java can read ALP v1 `FLOAT` and `DOUBLE` series. Use C++ or Python to write + ALP files. +8. Duplicate timestamps are opt-in; check the file's timestamp-policy + property and the consumer's ordering requirements. ## Troubleshooting Order diff --git a/skills/tsfile/references/encoding-compression.md b/skills/tsfile/references/encoding-compression.md index 08edfa76ef..9bba47da69 100644 --- a/skills/tsfile/references/encoding-compression.md +++ b/skills/tsfile/references/encoding-compression.md @@ -29,7 +29,7 @@ Validate with `TSEncoding.isSupported(dataType, encoding)`, create encoders with `TSEncodingBuilder.getEncodingBuilder(encoding).getEncoder(dataType)`, and create decoders with `Decoder.getDecoderByType(encoding, dataType)`. -| data type | current supported encodings | +| data type | current writable encodings | |---|---| | `BOOLEAN` | `PLAIN`, `RLE` | | `INT32`, `INT64`, `DATE`, `TIMESTAMP` | `PLAIN`, `RLE`, `TS_2DIFF`, `GORILLA`, `ZIGZAG`, `CHIMP`, `SPRINTZ`, `RLBE` | @@ -38,6 +38,9 @@ create decoders with `Decoder.getDecoderByType(encoding, dataType)`. | `TEXT`, `STRING` | `PLAIN`, `DICTIONARY` | | `BLOB`, `OBJECT` | `PLAIN` | +Java can also read ALP v1 (encoding ID 15) for `FLOAT` and `DOUBLE`. +Use a C++ or Python writer for this encoding. + Create compressors with `ICompressor.getCompressor(CompressionType)` and decompressors with `IUnCompressor.getUnCompressor(CompressionType)`. The current Java factory set is `UNCOMPRESSED`, `SNAPPY`, `GZIP`, `LZ4`, `ZSTD`, @@ -71,8 +74,7 @@ allocations with `CompressorFactory::free`. Python exposes schema encoding/compression enums through the native binding but does not expose these Java/C++ low-level factories as an equivalent Python API. -ALP is C++/Python-only; Java cannot query ALP series. CLI encoding overrides -follow the C++ encoder/decoder factory support matrix. +CLI encoding overrides follow the C++ encoder/decoder factory support matrix. GORILLA_V1 reserves NaN as its end marker; the CLI rejects NaN input for this codec. FLOAT/DOUBLE RLE uses two-decimal scaling by default and may be lossy. From 9aeee6704be6fbf1ba70657e63969967b88fe0e1 Mon Sep 17 00:00:00 2001 From: Yongzao <532741407@qq.com> Date: Wed, 25 Nov 2026 16:36:33 +0800 Subject: [PATCH 2/2] test(compat): declare read-only ALP rows in the compatibility matrix Declare C++ write support and Java and C++ read support for ALP FLOAT and DOUBLE across the table and tree models. Extend the C++ coverage scan to include ALP so the matrix exercises Java's decoder without implying Java encoding support. Group the ALP messages and document how to regenerate the fixtures using a portable path. Verified 1,401 Java tests, 92 C++ fixtures read by Java, and 44 Java fixtures read by C++. The full C++ and AddressSanitizer suites passed (1,859 and 1,886 tests), as did Spotless and Apache RAT. Generated-by: OpenAI Codex --- compat/README.md | 4 ++++ compat/compatibility-matrix.csv | 17 ++++++++++++---- cpp/docs/alp-codec-design.md | 15 +++++--------- cpp/src/common/db_common.h | 5 ++--- cpp/src/cwrapper/tsfile_cwrapper.h | 8 +++----- ...encoding_compression_compatibility_test.cc | 12 +++++------ cpp/tools/README.md | 7 ++++--- .../apache/tsfile/i18n/messages.properties | 4 +++- .../apache/tsfile/i18n/messages_zh.properties | 4 +++- java/tsfile/src/test/resources/alp/README.md | 20 +++++++++---------- 10 files changed, 52 insertions(+), 44 deletions(-) diff --git a/compat/README.md b/compat/README.md index 1a453fba87..1497cf0b77 100644 --- a/compat/README.md +++ b/compat/README.md @@ -60,6 +60,10 @@ cartesian product of every data type and encoding the local writer supports and fails when the spec does not cover it. That is what keeps the matrix complete as new encodings are added. +ALP is currently readable by Java for FLOAT and DOUBLE files written by C++. +Java does not encode ALP, so its matrix rows declare `javaWrite=no` while +retaining `javaRead=yes`. + ## Data scale and page-size tiers Every (type, encoding, compression) combination is generated at four data diff --git a/compat/compatibility-matrix.csv b/compat/compatibility-matrix.csv index ed2a211de5..99e9d1c0c5 100644 --- a/compat/compatibility-matrix.csv +++ b/compat/compatibility-matrix.csv @@ -52,10 +52,9 @@ # when one block spans INT_MIN and INT_MAX, so SPRINTZ rows use the # 'sprintz' profile (values within +/-1e9). Streaming numeric extremes are # covered under PLAIN, RLE, TS_2DIFF, GORILLA, CHIMP and RLBE. -# - ALP (encoding id 15): C++/Python-only for now. The Java implementation -# has no ALP decoder, so this matrix declares no ALP rows and the C++ -# completeness sweep stops at CAMEL. Add ALP rows and extend that bound -# only together with the Java decoder. +# - ALP (encoding id 15): Java can decode C++-written ALP files for FLOAT +# and DOUBLE, but Java does not provide an ALP encoder. ALP rows therefore +# declare javaWrite=no while keeping Java reads enabled. id,model,dataType,encoding,compressions,rowCount,pageRows,chunks,devices,valueProfile,nullPatterns,javaWrite,cppWrite,javaRead,cppRead,gapReason,timestampProfile table_int32_plain_r10,table,INT32,PLAIN,ALL,10,4,1,1,default,NONE,yes,yes,yes,yes, @@ -210,6 +209,10 @@ table_float_rlbe_r10,table,FLOAT,RLBE,ALL,10,4,1,1,default,NONE,yes,yes,yes,yes, table_float_rlbe_r1000,table,FLOAT,RLBE,ALL,1000,100,1,1,default,NONE,yes,yes,yes,yes, table_float_rlbe_r100000,table,FLOAT,RLBE,ALL,100000,10000,1,1,default,NONE,yes,yes,yes,yes, table_float_rlbe_r1000000,table,FLOAT,RLBE,SNAPPY+GZIP+LZ4+ZSTD+LZMA2,1000000,10000,1,1,default,NONE,yes,yes,yes,yes, +table_float_alp_r10,table,FLOAT,ALP,ALL,10,4,1,1,default,NONE,no,yes,yes,yes,Java supports ALP decoding only; it has no ALP encoder. +table_float_alp_r1000,table,FLOAT,ALP,ALL,1000,100,1,1,default,NONE,no,yes,yes,yes,Java supports ALP decoding only; it has no ALP encoder. +table_float_alp_r100000,table,FLOAT,ALP,ALL,100000,10000,1,1,default,NONE,no,yes,yes,yes,Java supports ALP decoding only; it has no ALP encoder. +table_float_alp_r1000000,table,FLOAT,ALP,SNAPPY+GZIP+LZ4+ZSTD+LZMA2,1000000,10000,1,1,default,NONE,no,yes,yes,yes,Java supports ALP decoding only; it has no ALP encoder. table_float_rle_r10,table,FLOAT,RLE,ALL,10,4,1,1,runs,NONE,yes,yes,yes,yes, table_float_rle_r1000,table,FLOAT,RLE,ALL,1000,100,1,1,runs,NONE,yes,yes,yes,yes, table_float_rle_r100000,table,FLOAT,RLE,ALL,100000,10000,1,1,runs,NONE,yes,yes,yes,yes, @@ -251,6 +254,10 @@ table_double_camel_r10,table,DOUBLE,CAMEL,ALL,10,4,1,1,default,NONE,yes,yes,yes, table_double_camel_r1000,table,DOUBLE,CAMEL,ALL,1000,100,1,1,default,NONE,yes,yes,yes,yes, table_double_camel_r100000,table,DOUBLE,CAMEL,ALL,100000,10000,1,1,default,NONE,yes,yes,yes,yes, table_double_camel_r1000000,table,DOUBLE,CAMEL,SNAPPY+GZIP+LZ4+ZSTD+LZMA2,1000000,10000,1,1,default,NONE,yes,yes,yes,yes, +table_double_alp_r10,table,DOUBLE,ALP,ALL,10,4,1,1,default,NONE,no,yes,yes,yes,Java supports ALP decoding only; it has no ALP encoder. +table_double_alp_r1000,table,DOUBLE,ALP,ALL,1000,100,1,1,default,NONE,no,yes,yes,yes,Java supports ALP decoding only; it has no ALP encoder. +table_double_alp_r100000,table,DOUBLE,ALP,ALL,100000,10000,1,1,default,NONE,no,yes,yes,yes,Java supports ALP decoding only; it has no ALP encoder. +table_double_alp_r1000000,table,DOUBLE,ALP,SNAPPY+GZIP+LZ4+ZSTD+LZMA2,1000000,10000,1,1,default,NONE,no,yes,yes,yes,Java supports ALP decoding only; it has no ALP encoder. table_double_rle_r10,table,DOUBLE,RLE,ALL,10,4,1,1,runs,NONE,yes,yes,yes,yes, table_double_rle_r1000,table,DOUBLE,RLE,ALL,1000,100,1,1,runs,NONE,yes,yes,yes,yes, table_double_rle_r100000,table,DOUBLE,RLE,ALL,100000,10000,1,1,runs,NONE,yes,yes,yes,yes, @@ -348,3 +355,5 @@ table_multi_chunk_group,table,INT32,RLE,ALL,50,0,1,3,runs,NONE,yes,yes,yes,yes, table_long_runs,table,INT64,RLE,ALL,1024,0,1,1,runs,NONE,yes,yes,yes,yes, table_int32_plain_dups,table,INT32,PLAIN,UNCOMPRESSED,10,4,1,1,default,NONE,yes,yes,yes,yes,,duplicates tree_int32_plain_dups,tree,INT32,PLAIN,UNCOMPRESSED,10,4,1,1,default,NONE,yes,yes,yes,yes,,duplicates +tree_float_alp_dups,tree,FLOAT,ALP,UNCOMPRESSED,10,4,1,1,default,NONE,no,yes,yes,yes,Java supports ALP decoding only; it has no ALP encoder.,duplicates +tree_double_alp_dups,tree,DOUBLE,ALP,UNCOMPRESSED,10,4,1,1,default,NONE,no,yes,yes,yes,Java supports ALP decoding only; it has no ALP encoder.,duplicates diff --git a/cpp/docs/alp-codec-design.md b/cpp/docs/alp-codec-design.md index 3026a1b6f4..7ecec1c361 100644 --- a/cpp/docs/alp-codec-design.md +++ b/cpp/docs/alp-codec-design.md @@ -30,16 +30,11 @@ Status: draft for the C++-first implementation on `colin/alp-codec`. - The existing Gorilla default is preserved. A column selects ALP by asking for `TSEncoding::ALP`; there is no build flag. Readers that predate the encoding reject the chunk instead of misreading it. -- **Java compatibility warning.** ALP is C++/Python-only for now. The Java - implementation has no `ALP` enum value, so a Java reader opens a file that - contains an ALP chunk but fails when it parses that chunk header (an - `IllegalArgumentException` naming the unknown encoding byte). Do not enable - ALP for files that are read by Java, and do not mix ALP and non-ALP writers - in a deployment that requires cross-language reads. The compatibility matrix - (`compat/compatibility-matrix.csv`) intentionally has no ALP rows until a - Java decoder exists. -- The format is language-neutral so a Java decoder can be added later without - changing encoded bytes. +- Java readers support ALP v1 decoding for FLOAT and DOUBLE; Java writers do + not encode ALP. The compatibility matrix (`compat/compatibility-matrix.csv`) + declares C++-written ALP cases and validates them with Java. +- The format is language-neutral, so the read-only Java decoder consumes the + same encoded bytes produced by C++ and Python. - Python reuses the C++ implementation through the C wrapper. ## Relationship to TS_2DIFF diff --git a/cpp/src/common/db_common.h b/cpp/src/common/db_common.h index c2b394e00f..6d500b9352 100644 --- a/cpp/src/common/db_common.h +++ b/cpp/src/common/db_common.h @@ -77,9 +77,8 @@ enum TSEncoding : uint8_t { SPRINTZ = 12, RLBE = 13, CAMEL = 14, - // C++/Python-only for now: the Java implementation has no ALP decoder. - // Java can open a file containing an ALP chunk, but any query that reads - // that series fails. Do not enable ALP for files shared with Java. + // C++/Python writers and C++/Java readers support ALP. Java currently + // provides decoding only; Java writers do not encode ALP. ALP = 15, INVALID_ENCODING = 255 }; diff --git a/cpp/src/cwrapper/tsfile_cwrapper.h b/cpp/src/cwrapper/tsfile_cwrapper.h index ae84338a32..d95599c2b2 100644 --- a/cpp/src/cwrapper/tsfile_cwrapper.h +++ b/cpp/src/cwrapper/tsfile_cwrapper.h @@ -60,9 +60,7 @@ typedef enum { TS_ENCODING_SPRINTZ = 12, TS_ENCODING_RLBE = 13, TS_ENCODING_CAMEL = 14, - /* C++/Python-only for now: the Java implementation has no ALP decoder. - * A file containing an ALP chunk opens in Java, but reading that series - * fails. Do not write ALP into files consumed by Java. */ + /* Java supports read-only ALP decoding for FLOAT and DOUBLE. */ TS_ENCODING_ALP = 15, TS_ENCODING_INVALID = 255 } TSEncoding; @@ -473,8 +471,8 @@ int set_global_time_compression(uint8_t compression); * - FLOAT: PLAIN, TS_2DIFF, GORILLA, SPRINTZ, CHIMP, RLBE, ALP * - DOUBLE: PLAIN, TS_2DIFF, GORILLA, SPRINTZ, CHIMP, RLBE, CAMEL, ALP * - STRING: PLAIN, DICTIONARY - * @warning ALP is C++/Python-only for now. A file written with ALP cannot be - * read by the Java implementation. + * @note Java can read ALP-encoded FLOAT and DOUBLE data but does not encode + * ALP. */ int set_datatype_encoding(uint8_t data_type, uint8_t encoding); diff --git a/cpp/test/reader/table_view/encoding_compression_compatibility_test.cc b/cpp/test/reader/table_view/encoding_compression_compatibility_test.cc index a6c3e39402..a7acc6bac2 100644 --- a/cpp/test/reader/table_view/encoding_compression_compatibility_test.cc +++ b/cpp/test/reader/table_view/encoding_compression_compatibility_test.cc @@ -656,7 +656,7 @@ std::vector Split(const std::string& line, char delimiter) { } bool ParseEncodingName(const std::string& value, TSEncoding& out) { - for (int encoding = PLAIN; encoding <= CAMEL; ++encoding) { + for (int encoding = PLAIN; encoding <= ALP; ++encoding) { TSEncoding candidate = static_cast(encoding); if (value == get_encoding_name(candidate)) { out = candidate; @@ -1926,14 +1926,12 @@ TEST_F(EncodingCompressionCompatibilityTest, SpecMatchesCppCapabilities) { ASSERT_FALSE(available_compressions.empty()); int missing = 0; - // Only the table model is exercised by the cross-language matrix. + // Only the table model has a complete capability matrix. for (Model model : {Model::TABLE}) { for (TSDataType data_type : kMatrixDataTypes) { - // The upper bound stops at CAMEL on purpose: ALP (15) is - // C++/Python-only and has no matrix rows until the Java - // implementation gains an ALP decoder. Extend this bound together - // with the Java decoder and the corresponding matrix rows. - for (int e = PLAIN; e <= CAMEL; e++) { + // Keep this sweep in sync with the Java-readable and C++-writable + // encodings declared in compatibility-matrix.csv. + for (int e = PLAIN; e <= ALP; e++) { TSEncoding encoding = static_cast(e); const bool can_write = EncoderSupported(encoding, data_type); const bool can_read = DecoderSupported(encoding, data_type); diff --git a/cpp/tools/README.md b/cpp/tools/README.md index abb0b2a9c5..52c16f4fd5 100644 --- a/cpp/tools/README.md +++ b/cpp/tools/README.md @@ -173,9 +173,10 @@ including ALP and GORILLA_V1 for FLOAT/DOUBLE, and RLE for BOOLEAN/FLOAT/DOUBLE. GORILLA_V1 reserves NaN as its end marker: CSV NaN values are rejected with the column and line number, without committing output; NULL and infinities remain valid. FLOAT/DOUBLE RLE follows the SDK's two-decimal scaling and may be lossy. -`schema` and verbose write summaries report ALP by name. ALP is C++/Python-only; -Java cannot query ALP series. Native build options control compression -availability; LZMA2 is opt-in. +`schema` and verbose write summaries report ALP by name. Java can query +ALP-encoded FLOAT and DOUBLE series written by C++, but Java writers do not +encode ALP. Native build options control compression availability; LZMA2 is +opt-in. | Option | Meaning | |---|---| diff --git a/java/common/src/main/resources/org/apache/tsfile/i18n/messages.properties b/java/common/src/main/resources/org/apache/tsfile/i18n/messages.properties index 239ccf86ff..f45c38eb71 100644 --- a/java/common/src/main/resources/org/apache/tsfile/i18n/messages.properties +++ b/java/common/src/main/resources/org/apache/tsfile/i18n/messages.properties @@ -687,10 +687,12 @@ error.encoding.float_decoder_method_not_supported = Method %1$s is not supported # PlainDecoder — BigDecimal not supported error.encoding.plain_decoder_bigdecimal_unsupported = Method readBigDecimal is not supported by PlainDecoder -# Decoder — method not supported (8 sites, 1 key) +# ALP decoder error.encoding.alp_malformed = Malformed ALP v1 page error.encoding.alp_no_more_values = No more ALP values in this page error.encoding.alp_type_not_supported = ALP decoder does not support %1$s + +# Decoder — method not supported (8 sites, 1 key) error.encoding.decoder_method_not_supported = Method %1$s is not supported by Decoder # Decoder.getDecoderByType — encoding+type combination not found (many sites, 1 key using ERROR_MSG) diff --git a/java/common/src/main/resources/org/apache/tsfile/i18n/messages_zh.properties b/java/common/src/main/resources/org/apache/tsfile/i18n/messages_zh.properties index 8c74fa4676..241f5ada51 100644 --- a/java/common/src/main/resources/org/apache/tsfile/i18n/messages_zh.properties +++ b/java/common/src/main/resources/org/apache/tsfile/i18n/messages_zh.properties @@ -687,10 +687,12 @@ error.encoding.float_decoder_method_not_supported = FloatDecoder 不支持方法 # PlainDecoder — BigDecimal not supported error.encoding.plain_decoder_bigdecimal_unsupported = PlainDecoder 不支持 readBigDecimal 方法 -# Decoder — method not supported (8 sites, 1 key) +# ALP decoder error.encoding.alp_malformed = ALP v1 Page 格式无效 error.encoding.alp_no_more_values = 当前 Page 中没有更多 ALP 值 error.encoding.alp_type_not_supported = ALP Decoder 不支持 %1$s + +# Decoder — method not supported (8 sites, 1 key) error.encoding.decoder_method_not_supported = Decoder 不支持方法 %1$s # Decoder.getDecoderByType — encoding+type combination not found (many sites, 1 key using ERROR_MSG) diff --git a/java/tsfile/src/test/resources/alp/README.md b/java/tsfile/src/test/resources/alp/README.md index ccbf42c032..b0d5981a9e 100644 --- a/java/tsfile/src/test/resources/alp/README.md +++ b/java/tsfile/src/test/resources/alp/README.md @@ -31,20 +31,20 @@ The companion alp-empty-float.bin is the C++ ALP v1 encoding of an empty FLOAT page (16-byte header, zero values, zero blocks). The checked-in binary is the Java test input; the C++ source documents how the -file was constructed. To regenerate on macOS from the repository root: +file was constructed. To regenerate from the repository root: ```bash -cmake -S cpp -B /private/tmp/tsfile-alp-fixture \ +build_dir="$PWD/build/alp-fixture" +fixture_dir=java/tsfile/src/test/resources/alp +cmake -S cpp -B "$build_dir" \ -DCMAKE_BUILD_TYPE=Release -DBUILD_TEST=OFF -DBUILD_TOOLS=OFF \ -DENABLE_ANTLR4=OFF -DENABLE_SNAPPY=OFF -DENABLE_LZ4=OFF \ -DENABLE_LZOKAY=OFF -DENABLE_ZLIB=OFF -DENABLE_ZSTD=OFF \ -DENABLE_LZMA2=OFF -DENABLE_SIMD=OFF -cmake --build /private/tmp/tsfile-alp-fixture --target tsfile --parallel 5 -c++ -std=c++11 -Icpp/src java/tsfile/src/test/resources/alp/generate.cc \ - -L/private/tmp/tsfile-alp-fixture/lib -ltsfile \ - -Wl,-rpath,/private/tmp/tsfile-alp-fixture/lib \ - -o /private/tmp/tsfile-alp-generate -/private/tmp/tsfile-alp-generate \ - java/tsfile/src/test/resources/alp/alp-vector-pages.tsfile \ - java/tsfile/src/test/resources/alp/alp-empty-float.bin +cmake --build "$build_dir" --target tsfile --parallel 5 +c++ -std=c++11 -Icpp/src "$fixture_dir/generate.cc" \ + -L"$build_dir/lib" -ltsfile -Wl,-rpath,"$build_dir/lib" \ + -o "$build_dir/alp-fixture-generate" +"$build_dir/alp-fixture-generate" \ + "$fixture_dir/alp-vector-pages.tsfile" "$fixture_dir/alp-empty-float.bin" ```