hang8929201 commented on code in PR #5028:
URL: https://github.com/apache/paimon/pull/5028#discussion_r1967316896
##########
docs/content/append-table/query-performance.md:
##########
@@ -64,6 +64,7 @@ scenario. Using a bitmap may consume more space but can
result in greater accura
`Bitmap`:
* `file-index.bitmap.columns`: specify the columns that need bitmap index.
+* `file-index.bitmap.<column_name>.index-block-size`: to config secondary
index block size, default value is 16kb.
Review Comment:
done
##########
paimon-common/src/main/java/org/apache/paimon/fileindex/bitmap/BitmapFileIndex.java:
##########
@@ -132,16 +144,36 @@ public byte[] serializedBytes() {
offsetRef[0] += bytes.length;
}
});
- BitmapFileIndexMeta bitmapFileIndexMeta =
- new BitmapFileIndexMeta(
- dataType,
- rowNumber,
- id2bitmap.size(),
- !nullBitmap.isEmpty(),
- nullBitmap.getCardinality() == 1
- ? -1 - nullBitmap.iterator().next()
- : 0,
- bitmapOffsets);
+ BitmapFileIndexMeta bitmapFileIndexMeta;
+ if (version == VERSION_1) {
+ bitmapFileIndexMeta =
+ new BitmapFileIndexMeta(
+ dataType,
+ options,
+ rowNumber,
+ id2bitmap.size(),
+ !nullBitmap.isEmpty(),
+ nullBitmap.getCardinality() == 1
+ ? -1 - nullBitmap.iterator().next()
+ : 0,
+ bitmapOffsets);
+ } else if (version == VERSION_2) {
+ bitmapFileIndexMeta =
+ new BitmapFileIndexMetaV2(
+ dataType,
+ options,
+ rowNumber,
+ id2bitmap.size(),
+ !nullBitmap.isEmpty(),
+ nullBitmap.getCardinality() == 1
+ ? -1 - nullBitmap.iterator().next()
+ : 0,
+ nullBitmapBytes.length,
+ bitmapOffsets,
+ offsetRef[0]);
+ } else {
+ throw new RuntimeException("invalid version: " + version);
Review Comment:
When the writer is upgraded to the latest version and the default index
version is v2, if the reader is not upgraded, an error will be reported here.
Is it more appropriate to use the v1 format by default or force users to
upgrade the paimon version by reporting an error? @Tan-JiaLiang @JingsongLi
##########
docs/content/concepts/spec/fileindex.md:
##########
@@ -98,9 +98,71 @@ This class use (64-bits) long hash. Store the num hash
function (one integer) an
Define `'file-index.bitmap.columns'`.
+Bitmap file index format (V2):
Review Comment:
Yes, I have added it to the documentation.
##########
paimon-common/src/main/java/org/apache/paimon/fileindex/bitmap/BitmapFileIndex.java:
##########
@@ -49,39 +49,51 @@
public class BitmapFileIndex implements FileIndexer {
public static final int VERSION_1 = 1;
+ public static final int VERSION_2 = 2;
+
+ public static final String VERSION = "version";
+ public static final String INDEX_BLOCK_SIZE = "index-block-size";
+ public static final String ENABLE_BUFFERED_INPUT = "enable-buffered-input";
+ public static final String ENABLE_NEXT_OFFSET_TO_SIZE =
"enable-next-offset-to-size";
Review Comment:
This parameter is just a switch for the benchmark test.
##########
paimon-common/src/main/java/org/apache/paimon/fileindex/bitmap/BitmapFileIndexMetaV2.java:
##########
@@ -0,0 +1,415 @@
+/*
+ * 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.paimon.fileindex.bitmap;
+
+import org.apache.paimon.data.BinaryString;
+import org.apache.paimon.fs.SeekableInputStream;
+import org.apache.paimon.options.MemorySize;
+import org.apache.paimon.options.Options;
+import org.apache.paimon.types.DataType;
+
+import java.io.BufferedInputStream;
+import java.io.DataInput;
+import java.io.DataInputStream;
+import java.io.DataOutput;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+
+/**
+ * When the bitmap-indexed column cardinality is high, using the first version
of the bitmap index
+ * format will take a lot of time to read the entire dictionary. But in fact
we don't need a full
+ * dictionary when dealing with a small number of predicates, the performance
of predicate hits on
+ * the bitmap can be improved by creating a secondary index on the dictionary.
+ *
+ * <pre>
+ * Bitmap file index format (V2)
+ * +-------------------------------------------------+-----------------
+ * | version (1 byte) = 2 |
+ * +-------------------------------------------------+
+ * | row count (4 bytes int) |
+ * +-------------------------------------------------+
+ * | non-null value bitmap number (4 bytes int) |
+ * +-------------------------------------------------+
+ * | has null value (1 byte) |
+ * +-------------------------------------------------+
+ * | null value offset (4 bytes if has null value) | HEAD
+ * +-------------------------------------------------+
+ * | null bitmap length (4 bytes if has null value) |
+ * +-------------------------------------------------+
+ * | bitmap index block number (4 bytes int) |
+ * +-------------------------------------------------+
+ * | value 1 | offset 1 |
+ * +-------------------------------------------------+
+ * | value 2 | offset 2 |
+ * +-------------------------------------------------+
+ * | ... |
+ * +-------------------------------------------------+
+ * | bitmap blocks offset (4 bytes int) |
+ * +-------------------------------------------------+-----------------
+ * | bitmap index block 1 |
+ * +-------------------------------------------------+
+ * | bitmap index block 2 | INDEX BLOCKS
+ * +-------------------------------------------------+
+ * | ... |
+ * +-------------------------------------------------+-----------------
+ * | serialized bitmap 1 |
+ * +-------------------------------------------------+
+ * | serialized bitmap 2 |
+ * +-------------------------------------------------+ BITMAP BLOCKS
+ * | serialized bitmap 3 |
+ * +-------------------------------------------------+
+ * | ... |
+ * +-------------------------------------------------+-----------------
+ *
+ * index block format:
+ * +-------------------------------------------------+
+ * | entry number (4 bytes int) |
+ * +-------------------------------------------------+
+ * | value 1 | offset 1 | length 1 |
+ * +-------------------------------------------------+
+ * | value 2 | offset 2 | length 2 |
+ * +-------------------------------------------------+
+ * | ... |
+ * +-------------------------------------------------+
+ * </pre>
+ */
+public class BitmapFileIndexMetaV2 extends BitmapFileIndexMeta {
+
+ private long blockSizeLimit;
+
+ private List<BitmapIndexBlock> indexBlocks;
+ private long indexBlockStart;
+ private int nullBitmapLength;
+
+ public BitmapFileIndexMetaV2(DataType dataType, Options options) {
+ super(dataType, options);
+ this.nullBitmapLength = -1;
+ }
+
+ public BitmapFileIndexMetaV2(
+ DataType dataType,
+ Options options,
+ int rowCount,
+ int nonNullBitmapNumber,
+ boolean hasNullValue,
+ int nullValueOffset,
+ int nullBitmapLength,
+ LinkedHashMap<Object, Integer> bitmapOffsets,
+ int finalOffset) {
+ super(
+ dataType,
+ options,
+ rowCount,
+ nonNullBitmapNumber,
+ hasNullValue,
+ nullValueOffset,
+ bitmapOffsets);
+ this.nullBitmapLength = nullBitmapLength;
+ blockSizeLimit =
+
MemorySize.parse(options.getString(BitmapFileIndex.INDEX_BLOCK_SIZE, "16kb"))
+ .getBytes();
+ if (enableNextOffsetToSize) {
+ bitmapLengths = new HashMap<>();
+ Object lastValue = null;
+ int lastOffset = nullValueOffset;
+ for (Map.Entry<Object, Integer> entry : bitmapOffsets.entrySet()) {
+ Object value = entry.getKey();
+ Integer offset = entry.getValue();
+ if (offset >= 0) {
+ if (lastOffset >= 0) {
+ bitmapLengths.put(lastValue, offset - lastOffset);
+ }
+ lastValue = value;
+ lastOffset = offset;
+ }
+ }
+ bitmapLengths.put(lastValue, finalOffset - lastOffset);
+ }
+ }
+
+ public static Comparator<Object> getComparator(DataType dataType) {
+ return dataType.accept(
+ new DataTypeVisitorAdapter<Comparator<Object>>() {
+ @Override
+ public Comparator<Object> visitBinaryString() {
+ return Comparator.comparing(o -> ((BinaryString) o));
+ }
+
+ @Override
+ public Comparator<Object> visitByte() {
+ return Comparator.comparing(o -> ((Byte) o));
+ }
+
+ @Override
+ public Comparator<Object> visitShort() {
+ return Comparator.comparing(o -> ((Short) o));
+ }
+
+ @Override
+ public Comparator<Object> visitInt() {
+ return Comparator.comparing(o -> ((Integer) o));
+ }
+
+ @Override
+ public Comparator<Object> visitLong() {
+ return Comparator.comparing(o -> ((Long) o));
+ }
+
+ @Override
+ public Comparator<Object> visitFloat() {
+ return Comparator.comparing(o -> ((Float) o));
+ }
+
+ @Override
+ public Comparator<Object> visitDouble() {
+ return Comparator.comparing(o -> ((Double) o));
+ }
+
+ @Override
+ public Comparator<Object> visitBoolean() {
+ return Comparator.comparing(o -> ((Boolean) o));
+ }
+ });
+ }
+
+ @Override
+ public Entry findEntry(Object bitmapId) {
+ if (bitmapId == null) {
+ if (hasNullValue) {
+ return new Entry(null, nullValueOffset, nullBitmapLength);
+ }
+ } else {
+ BitmapIndexBlock block = findBlock(bitmapId);
+ if (block != null) {
+ return block.findEntry(bitmapId);
+ }
+ }
+ return null;
+ }
+
+ private BitmapIndexBlock findBlock(Object bitmapId) {
+ Comparator<Object> comparator = getComparator(dataType);
+ int idx =
+ Collections.binarySearch(
+ indexBlocks, null, (b1, ignore) ->
comparator.compare(b1.key, bitmapId));
+ idx = idx < 0 ? -2 - idx : idx;
+ return idx < 0 ? null : indexBlocks.get(idx);
+ }
+
+ @Override
+ public void serialize(DataOutput out) throws Exception {
+
+ ThrowableConsumer valueWriter = getValueWriter(out);
+
+ out.writeInt(rowCount);
+ out.writeInt(nonNullBitmapNumber);
+ out.writeBoolean(hasNullValue);
+ if (hasNullValue) {
+ out.writeInt(nullValueOffset);
+ out.writeInt(nullBitmapLength);
+ }
+
+ LinkedList<BitmapIndexBlock> indexBlocks = new LinkedList<>();
+ this.indexBlocks = indexBlocks;
+ indexBlocks.add(new BitmapIndexBlock(0));
+ Comparator<Object> comparator = getComparator(dataType);
+ bitmapOffsets.entrySet().stream()
+ .map(
+ it ->
+ new Entry(
+ it.getKey(),
+ it.getValue(),
+ bitmapLengths == null
+ ? -1
+ :
bitmapLengths.getOrDefault(it.getKey(), -1)))
+ .sorted((e1, e2) -> comparator.compare(e1.key, e2.key))
+ .forEach(
+ e -> {
+ BitmapIndexBlock last = indexBlocks.peekLast();
+ if (!last.tryAdd(e)) {
+ BitmapIndexBlock next =
+ new BitmapIndexBlock(last.offset +
last.serializedBytes);
+ indexBlocks.add(next);
+ if (!next.tryAdd(e)) {
+ throw new RuntimeException("index fail");
+ }
+ }
+ });
+
+ out.writeInt(indexBlocks.size());
+
+ int bitmapBodyOffset = 0;
+ for (BitmapIndexBlock e : indexBlocks) {
+ // secondary entry
+ valueWriter.accept(e.key);
+ out.writeInt(e.offset);
+ bitmapBodyOffset += e.serializedBytes;
+ }
+
+ // bitmap body offset
+ out.writeInt(bitmapBodyOffset);
+
+ // bitmap index blocks
+ for (BitmapIndexBlock indexBlock : indexBlocks) {
+ out.writeInt(indexBlock.entryList.size());
+ for (Entry e : indexBlock.entryList) {
+ valueWriter.accept(e.key);
+ out.writeInt(e.offset);
+ out.writeInt(e.length);
+ }
+ }
+ }
+
+ @Override
+ public void deserialize(SeekableInputStream seekableInputStream) throws
Exception {
+
+ indexBlockStart = seekableInputStream.getPos();
+
+ InputStream inputStream = seekableInputStream;
+ if (options.getBoolean(BitmapFileIndex.ENABLE_BUFFERED_INPUT, true)) {
+ inputStream = new BufferedInputStream(inputStream);
+ }
+ DataInput in = new DataInputStream(inputStream);
+ ThrowableSupplier valueReader = getValueReader(in);
+ Function<Object, Integer> measure = getSerializeSizeMeasure();
+
+ rowCount = in.readInt();
+ indexBlockStart += Integer.BYTES;
+
+ nonNullBitmapNumber = in.readInt();
+ indexBlockStart += Integer.BYTES;
+
+ hasNullValue = in.readBoolean();
+ indexBlockStart++;
+
+ if (hasNullValue) {
+ nullValueOffset = in.readInt();
+ nullBitmapLength = in.readInt();
+ indexBlockStart += 2 * Integer.BYTES;
+ }
+
+ bitmapOffsets = new LinkedHashMap<>();
+
+ int bitmapBlockNumber = in.readInt();
+ indexBlockStart += Integer.BYTES;
+
+ indexBlocks = new ArrayList<>(bitmapBlockNumber);
+ for (int i = 0; i < bitmapBlockNumber; i++) {
+ Object key = valueReader.get();
+ int offset = in.readInt();
+ indexBlocks.add(
+ new BitmapIndexBlock(dataType, options, key, offset,
seekableInputStream));
+ indexBlockStart += measure.apply(key) + Integer.BYTES;
+ }
+
+ // bitmap body offset
+ int bitmapBodyOffset = in.readInt();
+ indexBlockStart += Integer.BYTES;
+
+ bodyStart = indexBlockStart + bitmapBodyOffset;
+ }
+
+ /** Split of all bitmap entries. */
+ class BitmapIndexBlock {
+
+ Object key;
+ int offset;
+ int serializedBytes = Integer.BYTES;
+ List<Entry> entryList;
+ Function<Object, Integer> keyBytesMapper;
+ DataType dataType;
+ SeekableInputStream seekableInputStream;
+ Options options;
+
+ void tryDeserialize() {
+ if (entryList == null) {
Review Comment:
Sorry, I don't quite understand what you mean. The purpose of designing
secondary indexes is to read only part of the entries.
##########
paimon-common/src/main/java/org/apache/paimon/fileindex/bitmap/BitmapFileIndexMetaV2.java:
##########
@@ -0,0 +1,415 @@
+/*
+ * 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.paimon.fileindex.bitmap;
+
+import org.apache.paimon.data.BinaryString;
+import org.apache.paimon.fs.SeekableInputStream;
+import org.apache.paimon.options.MemorySize;
+import org.apache.paimon.options.Options;
+import org.apache.paimon.types.DataType;
+
+import java.io.BufferedInputStream;
+import java.io.DataInput;
+import java.io.DataInputStream;
+import java.io.DataOutput;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+
+/**
+ * When the bitmap-indexed column cardinality is high, using the first version
of the bitmap index
+ * format will take a lot of time to read the entire dictionary. But in fact
we don't need a full
+ * dictionary when dealing with a small number of predicates, the performance
of predicate hits on
+ * the bitmap can be improved by creating a secondary index on the dictionary.
+ *
+ * <pre>
+ * Bitmap file index format (V2)
+ * +-------------------------------------------------+-----------------
+ * | version (1 byte) = 2 |
+ * +-------------------------------------------------+
+ * | row count (4 bytes int) |
+ * +-------------------------------------------------+
+ * | non-null value bitmap number (4 bytes int) |
+ * +-------------------------------------------------+
+ * | has null value (1 byte) |
+ * +-------------------------------------------------+
+ * | null value offset (4 bytes if has null value) | HEAD
+ * +-------------------------------------------------+
+ * | null bitmap length (4 bytes if has null value) |
+ * +-------------------------------------------------+
+ * | bitmap index block number (4 bytes int) |
+ * +-------------------------------------------------+
+ * | value 1 | offset 1 |
+ * +-------------------------------------------------+
+ * | value 2 | offset 2 |
+ * +-------------------------------------------------+
+ * | ... |
+ * +-------------------------------------------------+
+ * | bitmap blocks offset (4 bytes int) |
+ * +-------------------------------------------------+-----------------
+ * | bitmap index block 1 |
+ * +-------------------------------------------------+
+ * | bitmap index block 2 | INDEX BLOCKS
+ * +-------------------------------------------------+
+ * | ... |
+ * +-------------------------------------------------+-----------------
+ * | serialized bitmap 1 |
+ * +-------------------------------------------------+
+ * | serialized bitmap 2 |
+ * +-------------------------------------------------+ BITMAP BLOCKS
+ * | serialized bitmap 3 |
+ * +-------------------------------------------------+
+ * | ... |
+ * +-------------------------------------------------+-----------------
+ *
+ * index block format:
Review Comment:
I feel that it is usually more appropriate to compress the data block, our
'data block' is the roaring bitmap, which is relatively well compressed. The
index block is relatively small and has high query performance requirements. I
guess there is no need to compress it. Maybe in the future when there is a bad
case in the business, we will design a compressed format.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]