You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iceberg.apache.org by pv...@apache.org on 2026/09/21 16:03:11 UTC
(iceberg) branch main updated: Core, Spark 4.2: Add PositionDeleteIndex.forEachInRange and use it in vectorized delete checks (#18027)
This is an automated email from the ASF dual-hosted git repository.
pvary pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg.git
The following commit(s) were added to refs/heads/main by this push:
new 8a6830af69 Core, Spark 4.2: Add PositionDeleteIndex.forEachInRange and use it in vectorized delete checks (#18027)
8a6830af69 is described below
commit 8a6830af6930f132b2dd8f14965f93ca195913b8
Author: JeonDaehong <90...@users.noreply.github.com>
AuthorDate: Tue Sep 22 01:03:05 2026 +0900
Core, Spark 4.2: Add PositionDeleteIndex.forEachInRange and use it in vectorized delete checks (#18027)
---
.../deletes/PositionDeleteIndexBenchmark.java | 126 ++++++++++++++
.../iceberg/deletes/BitmapPositionDeleteIndex.java | 5 +
.../iceberg/deletes/EmptyPositionDeleteIndex.java | 12 ++
.../iceberg/deletes/PositionDeleteIndex.java | 24 +++
.../iceberg/deletes/RoaringPositionBitmap.java | 79 +++++++++
.../deletes/TestBitmapPositionDeleteIndex.java | 190 +++++++++++++++++++++
.../iceberg/deletes/TestRoaringPositionBitmap.java | 132 ++++++++++++++
.../spark/data/vectorized/ColumnarBatchUtil.java | 107 ++++++++++++
.../data/vectorized/TestColumnarBatchUtil.java | 166 ++++++++++++++----
9 files changed, 804 insertions(+), 37 deletions(-)
diff --git a/core/src/jmh/java/org/apache/iceberg/deletes/PositionDeleteIndexBenchmark.java b/core/src/jmh/java/org/apache/iceberg/deletes/PositionDeleteIndexBenchmark.java
new file mode 100644
index 0000000000..dcae5a8cd0
--- /dev/null
+++ b/core/src/jmh/java/org/apache/iceberg/deletes/PositionDeleteIndexBenchmark.java
@@ -0,0 +1,126 @@
+/*
+ * 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.iceberg.deletes;
+
+import java.util.Random;
+import java.util.concurrent.TimeUnit;
+import java.util.function.LongConsumer;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.Measurement;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Param;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.Setup;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.Threads;
+import org.openjdk.jmh.annotations.Timeout;
+import org.openjdk.jmh.annotations.Warmup;
+import org.openjdk.jmh.infra.Blackhole;
+
+/**
+ * A benchmark that compares probing {@link PositionDeleteIndex} once per position against
+ * traversing a range once, which is how vectorized readers consume the index.
+ *
+ * <p>Readers process positions in contiguous ascending batches, so every probe in a batch resolves
+ * the same underlying bitmap and container. The density determines which Roaring container is
+ * chosen: up to 4096 deleted positions per 65536-position chunk are stored in a sorted array that
+ * is binary searched, beyond that in a fixed bitmap.
+ *
+ * <p>To run this benchmark: <code>
+ * ./gradlew :iceberg-core:jmh
+ * -PjmhIncludeRegex=PositionDeleteIndexBenchmark
+ * -PjmhOutputPath=benchmark/position-delete-index-benchmark.txt
+ * </code>
+ */
+@Fork(1)
+@State(Scope.Benchmark)
+@Warmup(iterations = 3)
+@Measurement(iterations = 5)
+@BenchmarkMode(Mode.SingleShotTime)
+@OutputTimeUnit(TimeUnit.MILLISECONDS)
+@Timeout(time = 5, timeUnit = TimeUnit.MINUTES)
+public class PositionDeleteIndexBenchmark {
+
+ private static final long TOTAL_POSITIONS = 5_000_000L;
+ private static final int BATCH_SIZE = 5000;
+ private static final long SEED = 1234L;
+
+ /** Deleted positions per 65536-position chunk, as a percentage. */
+ @Param({"0.5", "6.1", "12.0"})
+ private double density;
+
+ private PositionDeleteIndex index;
+
+ @Setup
+ public void setupBenchmark() {
+ Random random = new Random(SEED);
+ PositionDeleteIndex bitmapIndex = new BitmapPositionDeleteIndex();
+ for (long pos = 0; pos < TOTAL_POSITIONS; pos++) {
+ if (random.nextDouble() * 100 < density) {
+ bitmapIndex.delete(pos);
+ }
+ }
+
+ this.index = bitmapIndex;
+ }
+
+ @Benchmark
+ @Threads(1)
+ public void probePerPosition(Blackhole blackhole) {
+ long deletedCount = 0;
+
+ for (long batchStart = 0; batchStart < TOTAL_POSITIONS; batchStart += BATCH_SIZE) {
+ for (int rowId = 0; rowId < BATCH_SIZE; rowId++) {
+ if (index.isDeleted(batchStart + rowId)) {
+ deletedCount++;
+ }
+ }
+ }
+
+ blackhole.consume(deletedCount);
+ }
+
+ @Benchmark
+ @Threads(1)
+ public void traverseRange(Blackhole blackhole) {
+ PositionCounter counter = new PositionCounter();
+
+ for (long batchStart = 0; batchStart < TOTAL_POSITIONS; batchStart += BATCH_SIZE) {
+ index.forEachInRange(batchStart, batchStart + BATCH_SIZE, counter);
+ }
+
+ blackhole.consume(counter.count());
+ }
+
+ private static class PositionCounter implements LongConsumer {
+ private long count = 0;
+
+ @Override
+ public void accept(long pos) {
+ this.count++;
+ }
+
+ long count() {
+ return count;
+ }
+ }
+}
diff --git a/core/src/main/java/org/apache/iceberg/deletes/BitmapPositionDeleteIndex.java b/core/src/main/java/org/apache/iceberg/deletes/BitmapPositionDeleteIndex.java
index 29cd368616..3a495c5957 100644
--- a/core/src/main/java/org/apache/iceberg/deletes/BitmapPositionDeleteIndex.java
+++ b/core/src/main/java/org/apache/iceberg/deletes/BitmapPositionDeleteIndex.java
@@ -98,6 +98,11 @@ class BitmapPositionDeleteIndex implements PositionDeleteIndex {
bitmap.forEach(consumer);
}
+ @Override
+ public void forEachInRange(long posStartInclusive, long posEndExclusive, LongConsumer consumer) {
+ bitmap.forEachInRange(posStartInclusive, posEndExclusive, consumer);
+ }
+
@Override
public Collection<DeleteFile> deleteFiles() {
return deleteFiles;
diff --git a/core/src/main/java/org/apache/iceberg/deletes/EmptyPositionDeleteIndex.java b/core/src/main/java/org/apache/iceberg/deletes/EmptyPositionDeleteIndex.java
index 660e01038c..565348261b 100644
--- a/core/src/main/java/org/apache/iceberg/deletes/EmptyPositionDeleteIndex.java
+++ b/core/src/main/java/org/apache/iceberg/deletes/EmptyPositionDeleteIndex.java
@@ -18,6 +18,9 @@
*/
package org.apache.iceberg.deletes;
+import java.util.function.LongConsumer;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+
class EmptyPositionDeleteIndex implements PositionDeleteIndex {
private static final EmptyPositionDeleteIndex INSTANCE = new EmptyPositionDeleteIndex();
@@ -48,6 +51,15 @@ class EmptyPositionDeleteIndex implements PositionDeleteIndex {
return true;
}
+ @Override
+ public void forEachInRange(long posStartInclusive, long posEndExclusive, LongConsumer consumer) {
+ Preconditions.checkArgument(
+ posStartInclusive <= posEndExclusive,
+ "Start position must not exceed end position: [%s, %s)",
+ posStartInclusive,
+ posEndExclusive);
+ }
+
@Override
public String toString() {
return "PositionDeleteIndex{}";
diff --git a/core/src/main/java/org/apache/iceberg/deletes/PositionDeleteIndex.java b/core/src/main/java/org/apache/iceberg/deletes/PositionDeleteIndex.java
index 6f97b3a6ac..db75dc331f 100644
--- a/core/src/main/java/org/apache/iceberg/deletes/PositionDeleteIndex.java
+++ b/core/src/main/java/org/apache/iceberg/deletes/PositionDeleteIndex.java
@@ -22,6 +22,7 @@ import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.function.LongConsumer;
import org.apache.iceberg.DeleteFile;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
public interface PositionDeleteIndex {
@@ -79,6 +80,29 @@ public interface PositionDeleteIndex {
}
}
+ /**
+ * Traverses the deleted positions within the given range in ascending order, applying the
+ * provided consumer.
+ *
+ * @param posStartInclusive inclusive beginning of position range
+ * @param posEndExclusive exclusive ending of position range
+ * @param consumer a consumer for the deleted positions in the range
+ * @throws IllegalArgumentException if posStartInclusive > posEndExclusive
+ */
+ default void forEachInRange(long posStartInclusive, long posEndExclusive, LongConsumer consumer) {
+ Preconditions.checkArgument(
+ posStartInclusive <= posEndExclusive,
+ "Start position must not exceed end position: [%s, %s)",
+ posStartInclusive,
+ posEndExclusive);
+
+ for (long pos = posStartInclusive; pos < posEndExclusive; pos++) {
+ if (isDeleted(pos)) {
+ consumer.accept(pos);
+ }
+ }
+ }
+
/**
* Returns delete files that this index was created from or an empty collection if unknown.
*
diff --git a/core/src/main/java/org/apache/iceberg/deletes/RoaringPositionBitmap.java b/core/src/main/java/org/apache/iceberg/deletes/RoaringPositionBitmap.java
index 037a38b114..b23073bc0f 100644
--- a/core/src/main/java/org/apache/iceberg/deletes/RoaringPositionBitmap.java
+++ b/core/src/main/java/org/apache/iceberg/deletes/RoaringPositionBitmap.java
@@ -27,6 +27,7 @@ import java.util.function.LongConsumer;
import org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting;
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.roaringbitmap.RelativeRangeConsumer;
import org.roaringbitmap.RoaringBitmap;
/**
@@ -192,6 +193,42 @@ class RoaringPositionBitmap {
}
}
+ /**
+ * Iterates over the positions set within the given range, in ascending order.
+ *
+ * <p>Each underlying 32-bit bitmap that the range covers is traversed once, instead of resolving
+ * the containing bitmap for every position as {@link #contains(long)} does.
+ *
+ * @param posStartInclusive inclusive beginning of position range
+ * @param posEndExclusive exclusive ending of position range
+ * @param consumer a consumer for the positions that are set within the range
+ * @throws IllegalArgumentException if posStartInclusive > posEndExclusive
+ */
+ public void forEachInRange(long posStartInclusive, long posEndExclusive, LongConsumer consumer) {
+ Preconditions.checkArgument(
+ posStartInclusive <= posEndExclusive,
+ "Start position must not exceed end position: [%s, %s)",
+ posStartInclusive,
+ posEndExclusive);
+
+ if (posStartInclusive == posEndExclusive) {
+ return;
+ }
+
+ validatePosition(posStartInclusive);
+ validatePosition(posEndExclusive - 1);
+
+ int startKey = key(posStartInclusive);
+ int endKey = key(posEndExclusive - 1);
+
+ for (int key = startKey; key <= endKey && key < bitmaps.length; key++) {
+ long lowStart = key == startKey ? Integer.toUnsignedLong(pos32Bits(posStartInclusive)) : 0L;
+ long lowEnd =
+ key == endKey ? Integer.toUnsignedLong(pos32Bits(posEndExclusive - 1)) + 1 : 1L << 32;
+ forEachInRange(key, bitmaps[key], lowStart, lowEnd, consumer);
+ }
+ }
+
@VisibleForTesting
int allocatedBitmapCount() {
return bitmaps.length;
@@ -339,6 +376,48 @@ class RoaringPositionBitmap {
bitmap.forEach((int pos32Bits) -> consumer.accept(toPosition(key, pos32Bits)));
}
+ // iterates over a range of 32-bit positions within one bitmap, reconstructing 64-bit positions
+ // the underlying range API takes an int length, so wider ranges are traversed in chunks
+ private static void forEachInRange(
+ int key, RoaringBitmap bitmap, long lowStart, long lowEnd, LongConsumer consumer) {
+ for (long low = lowStart; low < lowEnd; ) {
+ int length = (int) Math.min(lowEnd - low, Integer.MAX_VALUE);
+ bitmap.forAllInRange((int) low, length, new RangeConsumer(key, low, consumer));
+ low += length;
+ }
+ }
+
+ // reports the set positions within a range, which are given relative to the start of the range
+ private static class RangeConsumer implements RelativeRangeConsumer {
+ private final int key;
+ private final long lowStart;
+ private final LongConsumer consumer;
+
+ RangeConsumer(int key, long lowStart, LongConsumer consumer) {
+ this.key = key;
+ this.lowStart = lowStart;
+ this.consumer = consumer;
+ }
+
+ @Override
+ public void acceptPresent(int relativePos) {
+ consumer.accept(toPosition(key, (int) (lowStart + relativePos)));
+ }
+
+ @Override
+ public void acceptAbsent(int relativePos) {}
+
+ @Override
+ public void acceptAllPresent(int relativeFrom, int relativeTo) {
+ for (int relativePos = relativeFrom; relativePos < relativeTo; relativePos++) {
+ consumer.accept(toPosition(key, (int) (lowStart + relativePos)));
+ }
+ }
+
+ @Override
+ public void acceptAllAbsent(int relativeFrom, int relativeTo) {}
+ }
+
private static void validatePosition(long pos) {
Preconditions.checkArgument(
pos >= 0 && pos <= MAX_POSITION,
diff --git a/core/src/test/java/org/apache/iceberg/deletes/TestBitmapPositionDeleteIndex.java b/core/src/test/java/org/apache/iceberg/deletes/TestBitmapPositionDeleteIndex.java
index 022a3102be..2e5e154364 100644
--- a/core/src/test/java/org/apache/iceberg/deletes/TestBitmapPositionDeleteIndex.java
+++ b/core/src/test/java/org/apache/iceberg/deletes/TestBitmapPositionDeleteIndex.java
@@ -25,8 +25,11 @@ import java.io.IOException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.util.List;
+import java.util.Random;
+import java.util.Set;
import org.apache.iceberg.DeleteFile;
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.relocated.com.google.common.collect.Sets;
import org.apache.iceberg.relocated.com.google.common.io.Resources;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
@@ -35,6 +38,7 @@ public class TestBitmapPositionDeleteIndex {
private static final long BITMAP_OFFSET = 0xFFFFFFFFL + 1L;
private static final long CONTAINER_OFFSET = Character.MAX_VALUE + 1L;
+ private static final long SEED = 20260818L;
@Test
public void testForEach() {
@@ -74,6 +78,135 @@ public class TestBitmapPositionDeleteIndex {
assertThat(positions).isEmpty();
}
+ @Test
+ public void testForEachInRange() {
+ PositionDeleteIndex index = indexOf(10L, 11L, 12L, 13L);
+
+ // the beginning is inclusive and the end is exclusive
+ assertThat(positionsInRange(index, 11L, 13L)).containsExactly(11L, 12L);
+ assertThat(positionsInRange(index, 11L, 12L)).containsExactly(11L);
+
+ // a range that ends where the deletes start
+ assertThat(positionsInRange(index, 0L, 10L)).isEmpty();
+
+ // a range that starts after the deletes end
+ assertThat(positionsInRange(index, 14L, 24L)).isEmpty();
+ }
+
+ @Test
+ public void testForEachInRangeAscendingOrder() {
+ PositionDeleteIndex index = indexOf(9L, 3L, 7L, 1L, 5L);
+ assertThat(positionsInRange(index, 0L, 20L)).containsExactly(1L, 3L, 5L, 7L, 9L);
+ }
+
+ @Test
+ public void testForEachInRangeEmptyRange() {
+ PositionDeleteIndex index = indexOf(0L, 1L, 2L);
+ assertThat(positionsInRange(index, 0L, 0L)).isEmpty();
+ }
+
+ @Test
+ public void testForEachInRangeEmptyBitmapIndex() {
+ assertThat(positionsInRange(new BitmapPositionDeleteIndex(), 0L, 1000L)).isEmpty();
+ }
+
+ @Test
+ public void testForEachInRangeEmptyIndex() {
+ assertThat(positionsInRange(PositionDeleteIndex.empty(), 0L, 1000L)).isEmpty();
+ assertThat(positionsInRange(PositionDeleteIndex.empty(), 12345L, 13345L)).isEmpty();
+ }
+
+ @Test
+ public void testForEachInRangeInvalidRange() {
+ // each implementation validates the range on its own, so check all of them
+ assertThatThrownBy(() -> positionsInRange(indexOf(1L, 2L, 3L), 5L, 3L))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("Start position must not exceed end position");
+
+ assertThatThrownBy(() -> positionsInRange(PositionDeleteIndex.empty(), 5L, 3L))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("Start position must not exceed end position");
+
+ assertThatThrownBy(
+ () ->
+ positionsInRange(
+ new SetBackedPositionDeleteIndex(Sets.newHashSet(1L, 2L, 3L)), 5L, 3L))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("Start position must not exceed end position");
+ }
+
+ @Test
+ public void testForEachInRangeAcrossBitmaps() {
+ long lastPosInFirstBitmap = BITMAP_OFFSET - 1;
+ PositionDeleteIndex index =
+ indexOf(lastPosInFirstBitmap - 1, lastPosInFirstBitmap, BITMAP_OFFSET, BITMAP_OFFSET + 1);
+
+ assertThat(positionsInRange(index, lastPosInFirstBitmap - 2, BITMAP_OFFSET + 3))
+ .containsExactly(
+ lastPosInFirstBitmap - 1, lastPosInFirstBitmap, BITMAP_OFFSET, BITMAP_OFFSET + 1);
+ }
+
+ @Test
+ public void testForEachInRangeAcrossContainers() {
+ PositionDeleteIndex index =
+ indexOf(CONTAINER_OFFSET - 2, CONTAINER_OFFSET - 1, CONTAINER_OFFSET, CONTAINER_OFFSET + 1);
+
+ assertThat(positionsInRange(index, CONTAINER_OFFSET - 3, CONTAINER_OFFSET + 3))
+ .containsExactly(
+ CONTAINER_OFFSET - 2, CONTAINER_OFFSET - 1, CONTAINER_OFFSET, CONTAINER_OFFSET + 1);
+ }
+
+ @Test
+ public void testForEachInRangeMatchesIsDeleted() {
+ Random random = new Random(SEED);
+ Set<Long> positions = Sets.newHashSet();
+ for (int index = 0; index < 20000; index++) {
+ positions.add((long) random.nextInt(300000));
+ }
+
+ PositionDeleteIndex bitmapIndex = new BitmapPositionDeleteIndex();
+ positions.forEach(bitmapIndex::delete);
+
+ // an index that implements only isDeleted, so the default traversal is used
+ PositionDeleteIndex defaultIndex = new SetBackedPositionDeleteIndex(positions);
+
+ for (int index = 0; index < 500; index++) {
+ long posStart = random.nextInt(300000);
+ long posEnd = posStart + 1 + random.nextInt(6000);
+ List<Long> expected = deletedPositionsInRange(positions, posStart, posEnd);
+ assertThat(positionsInRange(bitmapIndex, posStart, posEnd))
+ .as("bitmap at %s", posStart)
+ .isEqualTo(expected);
+ assertThat(positionsInRange(defaultIndex, posStart, posEnd))
+ .as("default at %s", posStart)
+ .isEqualTo(expected);
+ }
+ }
+
+ @Test
+ public void testForEachInRangeMatchesIsDeletedAcrossSignedIntBoundary() {
+ // a 32-bit position at or above Integer.MAX_VALUE does not fit in a signed int, and neither
+ // does the end of a range that reaches it, so sweep every range around the boundary
+ long boundary = Integer.MAX_VALUE + 1L;
+ Set<Long> positions = Sets.newHashSet();
+
+ BitmapPositionDeleteIndex index = new BitmapPositionDeleteIndex();
+ index.delete(boundary - 4, boundary + 4);
+ for (long pos = boundary - 4; pos < boundary + 4; pos++) {
+ positions.add(pos);
+ }
+
+ index.serialize(); // triggers runLengthEncode
+
+ for (long posStart = boundary - 12; posStart <= boundary + 6; posStart++) {
+ for (long posEnd = posStart; posEnd <= posStart + 20; posEnd++) {
+ assertThat(positionsInRange(index, posStart, posEnd))
+ .as("range [%s, %s)", posStart, posEnd)
+ .isEqualTo(deletedPositionsInRange(positions, posStart, posEnd));
+ }
+ }
+ }
+
@Test
public void testMergeBitmapIndexWithNonEmpty() {
long pos1 = 10L; // Container 0 (high bits = 0)
@@ -235,4 +368,61 @@ public class TestBitmapPositionDeleteIndex {
index.forEach(positions::add);
return positions;
}
+
+ private static List<Long> positionsInRange(
+ PositionDeleteIndex index, long posStartInclusive, long posEndExclusive) {
+ List<Long> positions = Lists.newArrayList();
+ index.forEachInRange(posStartInclusive, posEndExclusive, positions::add);
+ return positions;
+ }
+
+ private static List<Long> deletedPositionsInRange(
+ Set<Long> positions, long posStart, long posEnd) {
+ List<Long> deleted = Lists.newArrayList();
+ for (long pos = posStart; pos < posEnd; pos++) {
+ if (positions.contains(pos)) {
+ deleted.add(pos);
+ }
+ }
+ return deleted;
+ }
+
+ private static PositionDeleteIndex indexOf(long... positions) {
+ PositionDeleteIndex index = new BitmapPositionDeleteIndex();
+ for (long position : positions) {
+ index.delete(position);
+ }
+ return index;
+ }
+
+ /** An index that implements only the required methods, so the default traversal is used. */
+ private static class SetBackedPositionDeleteIndex implements PositionDeleteIndex {
+ private final Set<Long> positions;
+
+ SetBackedPositionDeleteIndex(Set<Long> positions) {
+ this.positions = positions;
+ }
+
+ @Override
+ public void delete(long position) {
+ positions.add(position);
+ }
+
+ @Override
+ public void delete(long posStart, long posEnd) {
+ for (long pos = posStart; pos < posEnd; pos++) {
+ positions.add(pos);
+ }
+ }
+
+ @Override
+ public boolean isDeleted(long position) {
+ return positions.contains(position);
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return positions.isEmpty();
+ }
+ }
}
diff --git a/core/src/test/java/org/apache/iceberg/deletes/TestRoaringPositionBitmap.java b/core/src/test/java/org/apache/iceberg/deletes/TestRoaringPositionBitmap.java
index 68b73ed0a2..58d0e2b7b1 100644
--- a/core/src/test/java/org/apache/iceberg/deletes/TestRoaringPositionBitmap.java
+++ b/core/src/test/java/org/apache/iceberg/deletes/TestRoaringPositionBitmap.java
@@ -350,6 +350,131 @@ public class TestRoaringPositionBitmap {
assertThat(bitmap1.cardinality()).isEqualTo(4);
}
+ @TestTemplate
+ public void testForEachInRange() {
+ RoaringPositionBitmap bitmap = new RoaringPositionBitmap();
+ bitmap.setRange(10L, 20L);
+
+ assertThat(positionsInRange(bitmap, 12L, 15L)).containsExactly(12L, 13L, 14L);
+ assertThat(positionsInRange(bitmap, 0L, 10L)).isEmpty();
+ assertThat(positionsInRange(bitmap, 20L, 30L)).isEmpty();
+ }
+
+ @TestTemplate
+ public void testForEachInRangeEmptyRange() {
+ RoaringPositionBitmap bitmap = new RoaringPositionBitmap();
+ bitmap.setRange(10L, 20L);
+
+ assertThat(positionsInRange(bitmap, 15L, 15L)).isEmpty();
+ }
+
+ @TestTemplate
+ public void testForEachInRangeInvalidRange() {
+ RoaringPositionBitmap bitmap = new RoaringPositionBitmap();
+
+ assertThatThrownBy(() -> bitmap.forEachInRange(20L, 10L, pos -> {}))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("Start position must not exceed end position");
+ }
+
+ @TestTemplate
+ public void testForEachInRangeAcrossContainers() {
+ RoaringPositionBitmap bitmap = new RoaringPositionBitmap();
+ bitmap.setRange(CONTAINER_OFFSET - 2, CONTAINER_OFFSET + 2);
+
+ assertThat(positionsInRange(bitmap, CONTAINER_OFFSET - 3, CONTAINER_OFFSET + 3))
+ .containsExactly(
+ CONTAINER_OFFSET - 2, CONTAINER_OFFSET - 1, CONTAINER_OFFSET, CONTAINER_OFFSET + 1);
+ }
+
+ @TestTemplate
+ public void testForEachInRangeAcrossKeys() {
+ RoaringPositionBitmap bitmap = new RoaringPositionBitmap();
+ bitmap.setRange(BITMAP_OFFSET - 2, BITMAP_OFFSET + 2);
+
+ assertThat(positionsInRange(bitmap, BITMAP_OFFSET - 3, BITMAP_OFFSET + 3))
+ .containsExactly(BITMAP_OFFSET - 2, BITMAP_OFFSET - 1, BITMAP_OFFSET, BITMAP_OFFSET + 1);
+ }
+
+ @TestTemplate
+ public void testForEachInRangeSpanningThreeKeys() {
+ RoaringPositionBitmap bitmap = new RoaringPositionBitmap();
+
+ long posStart = BITMAP_OFFSET - 1;
+ long posEnd = (2 * BITMAP_OFFSET) + 1;
+ bitmap.set(posStart);
+ bitmap.set(BITMAP_OFFSET); // first position of the second bitmap
+ bitmap.set(2 * BITMAP_OFFSET); // first position of the third bitmap
+
+ // the middle bitmap is covered in full, which the underlying range API cannot express in a
+ // single call because its length is an int
+ assertThat(positionsInRange(bitmap, posStart, posEnd))
+ .containsExactly(posStart, BITMAP_OFFSET, 2 * BITMAP_OFFSET);
+ }
+
+ @TestTemplate
+ public void testForEachInRangeBeyondAllocatedBitmaps() {
+ RoaringPositionBitmap bitmap = new RoaringPositionBitmap();
+ bitmap.setRange(1L, 4L);
+
+ // no bitmap is allocated for this key, so the range produces nothing
+ assertThat(positionsInRange(bitmap, 3 * BITMAP_OFFSET, 3 * BITMAP_OFFSET + 1000)).isEmpty();
+ }
+
+ @TestTemplate
+ public void testForEachInRangeAfterRunLengthEncode() {
+ RoaringPositionBitmap bitmap = new RoaringPositionBitmap();
+ bitmap.setRange(1000L, 40000L);
+ bitmap.runLengthEncode();
+
+ assertThat(positionsInRange(bitmap, 999L, 1004L)).containsExactly(1000L, 1001L, 1002L, 1003L);
+ assertThat(positionsInRange(bitmap, 39998L, 40003L)).containsExactly(39998L, 39999L);
+ }
+
+ @TestTemplate
+ public void testForEachInRangeMatchesContains() {
+ Random random = new Random(seed);
+ RoaringPositionBitmap bitmap = new RoaringPositionBitmap();
+ Set<Long> positions = Sets.newHashSet();
+
+ for (int index = 0; index < 20000; index++) {
+ long pos = random.nextInt(300000);
+ bitmap.set(pos);
+ positions.add(pos);
+ }
+
+ for (int index = 0; index < 500; index++) {
+ long posStart = random.nextInt(300000);
+ long posEnd = posStart + 1 + random.nextInt(6000);
+
+ List<Long> expected = Lists.newArrayList();
+ for (long pos = posStart; pos < posEnd; pos++) {
+ if (positions.contains(pos)) {
+ expected.add(pos);
+ }
+ }
+
+ assertThat(positionsInRange(bitmap, posStart, posEnd))
+ .as("range [%s, %s)", posStart, posEnd)
+ .isEqualTo(expected);
+ }
+ }
+
+ @TestTemplate
+ public void testForEachInRangeAcrossSignedIntBoundary() {
+ long boundary = Integer.MAX_VALUE + 1L;
+ RoaringPositionBitmap bitmap = new RoaringPositionBitmap();
+ bitmap.setRange(boundary - 4, boundary + 4);
+ bitmap.runLengthEncode();
+
+ assertThat(positionsInRange(bitmap, boundary - 1, boundary + 1))
+ .containsExactly(boundary - 1, boundary);
+ assertThat(positionsInRange(bitmap, boundary - 4, boundary + 4))
+ .hasSize(8)
+ .startsWith(boundary - 4)
+ .endsWith(boundary + 3);
+ }
+
@TestTemplate
public void testCardinality() {
RoaringPositionBitmap bitmap = new RoaringPositionBitmap();
@@ -587,6 +712,13 @@ public class TestRoaringPositionBitmap {
}
}
+ private static List<Long> positionsInRange(
+ RoaringPositionBitmap bitmap, long posStartInclusive, long posEndExclusive) {
+ List<Long> positions = Lists.newArrayList();
+ bitmap.forEachInRange(posStartInclusive, posEndExclusive, positions::add);
+ return positions;
+ }
+
private static long nextLong(Random random, long minInclusive, long maxExclusive) {
return minInclusive + (long) (random.nextDouble() * (maxExclusive - minInclusive));
}
diff --git a/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/data/vectorized/ColumnarBatchUtil.java b/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/data/vectorized/ColumnarBatchUtil.java
index 89fe4538bd..bddc144fb2 100644
--- a/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/data/vectorized/ColumnarBatchUtil.java
+++ b/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/data/vectorized/ColumnarBatchUtil.java
@@ -19,6 +19,7 @@
package org.apache.iceberg.spark.data.vectorized;
import java.util.Arrays;
+import java.util.function.LongConsumer;
import java.util.function.Predicate;
import org.apache.iceberg.data.DeleteFilter;
import org.apache.iceberg.deletes.PositionDeleteIndex;
@@ -64,6 +65,15 @@ public class ColumnarBatchUtil {
}
PositionDeleteIndex deletedPositions = deletes.deletedRowPositions();
+
+ if (!deletes.hasEqDeletes()) {
+ // positions in a batch form a contiguous ascending range, so the index can be traversed once
+ // for the whole range instead of being probed once per row
+ return deletedPositions == null
+ ? null
+ : buildRowIdMapping(deletedPositions, deletes, rowStartPosInBatch, batchSize);
+ }
+
Predicate<InternalRow> eqDeleteFilter = deletes.eqDeletedRowFilter();
ColumnarBatchRow row = new ColumnarBatchRow(columnVectors);
int[] rowIdMapping = new int[batchSize];
@@ -83,6 +93,21 @@ public class ColumnarBatchUtil {
return liveRowId == batchSize ? null : Pair.of(rowIdMapping, liveRowId);
}
+ // builds the mapping from the deleted positions in the batch range, without probing per row
+ private static Pair<int[], Integer> buildRowIdMapping(
+ PositionDeleteIndex deletedPositions,
+ DeleteFilter<InternalRow> deletes,
+ long rowStartPosInBatch,
+ int batchSize) {
+ RowIdMappingCollector collector =
+ new RowIdMappingCollector(deletes, rowStartPosInBatch, batchSize);
+ deletedPositions.forEachInRange(rowStartPosInBatch, rowStartPosInBatch + batchSize, collector);
+ collector.appendRemainingLiveRows();
+
+ int liveRowId = collector.liveRowCount();
+ return liveRowId == batchSize ? null : Pair.of(collector.rowIdMapping(), liveRowId);
+ }
+
/**
* Builds a boolean array to indicate if a row is deleted or not.
*
@@ -119,6 +144,20 @@ public class ColumnarBatchUtil {
}
PositionDeleteIndex deletedPositions = deletes.deletedRowPositions();
+
+ if (!deletes.hasEqDeletes()) {
+ // positions in a batch form a contiguous ascending range, so the index can be traversed once
+ // for the whole range instead of being probed once per row
+ if (deletedPositions != null) {
+ IsDeletedCollector collector =
+ new IsDeletedCollector(deletes, isDeleted, rowStartPosInBatch);
+ deletedPositions.forEachInRange(
+ rowStartPosInBatch, rowStartPosInBatch + batchSize, collector);
+ }
+
+ return isDeleted;
+ }
+
Predicate<InternalRow> eqDeleteFilter = deletes.eqDeletedRowFilter();
ColumnarBatchRow row = new ColumnarBatchRow(columnVectors);
@@ -178,4 +217,72 @@ public class ColumnarBatchUtil {
return columnVectors;
}
}
+
+ /**
+ * Consumes deleted positions in ascending order, filling the gaps between them with live row IDs.
+ */
+ private static class RowIdMappingCollector implements LongConsumer {
+ private final DeleteFilter<InternalRow> deletes;
+ private final long rowStartPosInBatch;
+ private final int batchSize;
+ private final int[] rowIdMapping;
+ private int nextRowId = 0;
+ private int liveRowId = 0;
+
+ RowIdMappingCollector(
+ DeleteFilter<InternalRow> deletes, long rowStartPosInBatch, int batchSize) {
+ this.deletes = deletes;
+ this.rowStartPosInBatch = rowStartPosInBatch;
+ this.batchSize = batchSize;
+ this.rowIdMapping = new int[batchSize];
+ }
+
+ @Override
+ public void accept(long pos) {
+ int deletedRowId = (int) (pos - rowStartPosInBatch);
+ for (int rowId = nextRowId; rowId < deletedRowId; rowId++) {
+ rowIdMapping[liveRowId] = rowId;
+ liveRowId++;
+ }
+
+ deletes.incrementDeleteCount();
+ this.nextRowId = deletedRowId + 1;
+ }
+
+ /** Appends the live rows that follow the last deleted position in the batch. */
+ void appendRemainingLiveRows() {
+ for (int rowId = nextRowId; rowId < batchSize; rowId++) {
+ rowIdMapping[liveRowId] = rowId;
+ liveRowId++;
+ }
+ }
+
+ int liveRowCount() {
+ return liveRowId;
+ }
+
+ int[] rowIdMapping() {
+ return rowIdMapping;
+ }
+ }
+
+ /** Consumes deleted positions in a batch range, marking them in the given array. */
+ private static class IsDeletedCollector implements LongConsumer {
+ private final DeleteFilter<InternalRow> deletes;
+ private final boolean[] isDeleted;
+ private final long rowStartPosInBatch;
+
+ IsDeletedCollector(
+ DeleteFilter<InternalRow> deletes, boolean[] isDeleted, long rowStartPosInBatch) {
+ this.deletes = deletes;
+ this.isDeleted = isDeleted;
+ this.rowStartPosInBatch = rowStartPosInBatch;
+ }
+
+ @Override
+ public void accept(long pos) {
+ isDeleted[(int) (pos - rowStartPosInBatch)] = true;
+ deletes.incrementDeleteCount();
+ }
+ }
}
diff --git a/spark/v4.2/spark/src/test/java/org/apache/iceberg/spark/data/vectorized/TestColumnarBatchUtil.java b/spark/v4.2/spark/src/test/java/org/apache/iceberg/spark/data/vectorized/TestColumnarBatchUtil.java
index dadbe3e788..468242f610 100644
--- a/spark/v4.2/spark/src/test/java/org/apache/iceberg/spark/data/vectorized/TestColumnarBatchUtil.java
+++ b/spark/v4.2/spark/src/test/java/org/apache/iceberg/spark/data/vectorized/TestColumnarBatchUtil.java
@@ -21,14 +21,21 @@ package org.apache.iceberg.spark.data.vectorized;
import static java.util.Collections.nCopies;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Arrays;
+import java.util.List;
+import java.util.Random;
import java.util.function.Predicate;
import java.util.stream.Stream;
import org.apache.iceberg.Schema;
import org.apache.iceberg.data.DeleteFilter;
+import org.apache.iceberg.deletes.Deletes;
import org.apache.iceberg.deletes.PositionDeleteIndex;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
import org.apache.spark.sql.catalyst.InternalRow;
import org.apache.spark.sql.vectorized.ColumnVector;
import org.junit.jupiter.api.BeforeEach;
@@ -48,13 +55,7 @@ public class TestColumnarBatchUtil {
@Test
public void testBuildRowIdMappingNoDeletes() {
when(deleteFilter.hasPosDeletes()).thenReturn(true);
- PositionDeleteIndex deletedRowPos = mock(PositionDeleteIndex.class);
-
- for (long i = 0; i <= 10; i++) {
- when(deletedRowPos.isDeleted(i)).thenReturn(false);
- }
-
- when(deleteFilter.deletedRowPositions()).thenReturn(deletedRowPos);
+ when(deleteFilter.deletedRowPositions()).thenReturn(positionIndex());
var rowIdMapping = ColumnarBatchUtil.buildRowIdMapping(columnVectors, deleteFilter, 0, 10);
assertThat(rowIdMapping).isNull();
}
@@ -62,14 +63,8 @@ public class TestColumnarBatchUtil {
@Test
public void testBuildRowIdMappingPositionDeletesOnly() {
when(deleteFilter.hasPosDeletes()).thenReturn(true);
- PositionDeleteIndex deletedRowPos = mock(PositionDeleteIndex.class);
-
// 5 position deletes
- for (long i = 98; i < 103; i++) {
- when(deletedRowPos.isDeleted(i)).thenReturn(true);
- }
-
- when(deleteFilter.deletedRowPositions()).thenReturn(deletedRowPos);
+ when(deleteFilter.deletedRowPositions()).thenReturn(positionIndex(98, 99, 100, 101, 102));
var rowIdMapping = ColumnarBatchUtil.buildRowIdMapping(columnVectors, deleteFilter, 0, 200);
assertThat(rowIdMapping).isNotNull();
@@ -96,6 +91,7 @@ public class TestColumnarBatchUtil {
// Mock DeleteFilter
when(deleteFilter.hasPosDeletes()).thenReturn(false);
+ when(deleteFilter.hasEqDeletes()).thenReturn(true);
when(deleteFilter.deletedRowPositions()).thenReturn(null);
when(deleteFilter.eqDeletedRowFilter()).thenReturn(eqDeletePredicate);
@@ -121,11 +117,9 @@ public class TestColumnarBatchUtil {
Stream.of(rawEqDelete).map(Predicate::negate).reduce(Predicate::and).orElse(t -> true);
when(deleteFilter.eqDeletedRowFilter()).thenReturn(eqDeletePredicate);
- PositionDeleteIndex deletedRowPos = mock(PositionDeleteIndex.class);
- when(deletedRowPos.isDeleted(1)).thenReturn(true); // 41
- when(deletedRowPos.isDeleted(4)).thenReturn(true); // 44
when(deleteFilter.hasPosDeletes()).thenReturn(true);
- when(deleteFilter.deletedRowPositions()).thenReturn(deletedRowPos);
+ when(deleteFilter.hasEqDeletes()).thenReturn(true);
+ when(deleteFilter.deletedRowPositions()).thenReturn(positionIndex(1, 4)); // 41 and 44
var rowIdMapping = ColumnarBatchUtil.buildRowIdMapping(columnVectors, deleteFilter, 0, 5);
@@ -141,11 +135,8 @@ public class TestColumnarBatchUtil {
void testBuildRowIdMappingEmptyColumVectors() {
ColumnVector[] columnVectorsZero = new ColumnVector[0];
- PositionDeleteIndex deletedRowPos = mock(PositionDeleteIndex.class);
- when(deletedRowPos.isDeleted(1)).thenReturn(true);
- when(deletedRowPos.isDeleted(4)).thenReturn(true);
when(deleteFilter.hasPosDeletes()).thenReturn(true);
- when(deleteFilter.deletedRowPositions()).thenReturn(deletedRowPos);
+ when(deleteFilter.deletedRowPositions()).thenReturn(positionIndex(1, 4));
var rowIdMapping = ColumnarBatchUtil.buildRowIdMapping(columnVectorsZero, deleteFilter, 0, 0);
@@ -164,12 +155,9 @@ public class TestColumnarBatchUtil {
Stream.of(rawEqDelete).map(Predicate::negate).reduce(Predicate::and).orElse(t -> true);
when(deleteFilter.eqDeletedRowFilter()).thenReturn(eqDeletePredicate);
- PositionDeleteIndex deletedRowPos = mock(PositionDeleteIndex.class);
- when(deletedRowPos.isDeleted(0)).thenReturn(true); // 40
- when(deletedRowPos.isDeleted(1)).thenReturn(true); // 41
- when(deletedRowPos.isDeleted(4)).thenReturn(true); // 44
when(deleteFilter.hasPosDeletes()).thenReturn(true);
- when(deleteFilter.deletedRowPositions()).thenReturn(deletedRowPos);
+ when(deleteFilter.hasEqDeletes()).thenReturn(true);
+ when(deleteFilter.deletedRowPositions()).thenReturn(positionIndex(0, 1, 4)); // 40, 41, 44
var rowIdMapping = ColumnarBatchUtil.buildRowIdMapping(columnVectors, deleteFilter, 0, 5);
@@ -184,12 +172,7 @@ public class TestColumnarBatchUtil {
@Test
void testBuildIsDeletedPositionDeletes() {
- PositionDeleteIndex deletedRowPos = mock(PositionDeleteIndex.class);
- when(deleteFilter.deletedRowPositions()).thenReturn(deletedRowPos);
-
- for (long i = 98; i < 100; i++) {
- when(deletedRowPos.isDeleted(i)).thenReturn(true);
- }
+ when(deleteFilter.deletedRowPositions()).thenReturn(positionIndex(98, 99));
var isDeleted = ColumnarBatchUtil.buildIsDeleted(columnVectors, deleteFilter, 0, 100);
@@ -213,6 +196,7 @@ public class TestColumnarBatchUtil {
// Mimic real eqDeletedRowFilter(): keep row only if it does NOT match delete condition
Predicate<InternalRow> eqDeletePredicate =
Stream.of(rawEqDelete).map(Predicate::negate).reduce(Predicate::and).orElse(t -> true);
+ when(deleteFilter.hasEqDeletes()).thenReturn(true);
when(deleteFilter.eqDeletedRowFilter()).thenReturn(eqDeletePredicate);
var isDeleted = ColumnarBatchUtil.buildIsDeleted(columnVectors, deleteFilter, 0, 5);
@@ -236,11 +220,9 @@ public class TestColumnarBatchUtil {
Stream.of(rawEqDelete).map(Predicate::negate).reduce(Predicate::and).orElse(t -> true);
when(deleteFilter.eqDeletedRowFilter()).thenReturn(eqDeletePredicate);
- PositionDeleteIndex deletedRowPos = mock(PositionDeleteIndex.class);
- when(deletedRowPos.isDeleted(1)).thenReturn(true); // 41
- when(deletedRowPos.isDeleted(4)).thenReturn(true); // 44
when(deleteFilter.hasPosDeletes()).thenReturn(true);
- when(deleteFilter.deletedRowPositions()).thenReturn(deletedRowPos);
+ when(deleteFilter.hasEqDeletes()).thenReturn(true);
+ when(deleteFilter.deletedRowPositions()).thenReturn(positionIndex(1, 4)); // 41 and 44
var isDeleted = ColumnarBatchUtil.buildIsDeleted(columnVectors, deleteFilter, 0, 5);
@@ -288,6 +270,116 @@ public class TestColumnarBatchUtil {
assertThat(result.length).isEqualTo(3);
}
+ @Test
+ void testBuildRowIdMappingNonZeroBatchStart() {
+ // batches after the first one start at a non-zero position in the file
+ long rowStartPosInBatch = 1_000_000L;
+ when(deleteFilter.hasPosDeletes()).thenReturn(true);
+ when(deleteFilter.deletedRowPositions())
+ .thenReturn(positionIndex(1_000_000L, 1_000_003L, 1_000_009L));
+
+ var rowIdMapping =
+ ColumnarBatchUtil.buildRowIdMapping(columnVectors, deleteFilter, rowStartPosInBatch, 10);
+
+ assertThat(rowIdMapping).isNotNull();
+ int[] rowIds = (int[]) rowIdMapping.first();
+ int liveRows = (Integer) rowIdMapping.second();
+
+ assertThat(liveRows).isEqualTo(7);
+ assertThat(Arrays.copyOf(rowIds, liveRows)).containsExactly(1, 2, 4, 5, 6, 7, 8);
+ verify(deleteFilter, times(3)).incrementDeleteCount();
+ }
+
+ @Test
+ void testBuildRowIdMappingDeletesOutsideBatch() {
+ // the index covers the whole file, so most batches see no deletes at all
+ when(deleteFilter.hasPosDeletes()).thenReturn(true);
+ when(deleteFilter.deletedRowPositions()).thenReturn(positionIndex(5L, 6L, 500L, 501L));
+
+ var rowIdMapping = ColumnarBatchUtil.buildRowIdMapping(columnVectors, deleteFilter, 100L, 100);
+
+ assertThat(rowIdMapping).isNull();
+ verify(deleteFilter, times(0)).incrementDeleteCount();
+ }
+
+ @Test
+ void testBuildRowIdMappingAllRowsDeletedByPosition() {
+ when(deleteFilter.hasPosDeletes()).thenReturn(true);
+ when(deleteFilter.deletedRowPositions()).thenReturn(positionIndex(0L, 1L, 2L, 3L, 4L));
+
+ var rowIdMapping = ColumnarBatchUtil.buildRowIdMapping(columnVectors, deleteFilter, 0, 5);
+
+ assertThat(rowIdMapping).isNotNull();
+ assertThat((Integer) rowIdMapping.second()).isEqualTo(0);
+ verify(deleteFilter, times(5)).incrementDeleteCount();
+ }
+
+ @Test
+ void testBuildIsDeletedNonZeroBatchStart() {
+ when(deleteFilter.deletedRowPositions()).thenReturn(positionIndex(1_000_002L, 1_000_004L));
+
+ var isDeleted = ColumnarBatchUtil.buildIsDeleted(columnVectors, deleteFilter, 1_000_000L, 5);
+
+ assertThat(isDeleted).containsExactly(false, false, true, false, true);
+ verify(deleteFilter, times(2)).incrementDeleteCount();
+ }
+
+ @Test
+ void testPositionOnlyPathMatchesPerRowPath() {
+ // the range traversal must agree with a straightforward per-row probe on every batch
+ Random random = new Random(20260818L);
+ long fileSize = 300_000L;
+ List<Long> deletedPositions = Lists.newArrayList();
+ for (long pos = 0; pos < fileSize; pos++) {
+ if (random.nextInt(1000) < 7) {
+ deletedPositions.add(pos);
+ }
+ }
+
+ PositionDeleteIndex index = positionIndex(deletedPositions);
+ int batchSize = 5000;
+
+ for (long batchStart = 0; batchStart < fileSize; batchStart += batchSize) {
+ DeleteFilter<InternalRow> filter = mock(DeleteFilter.class);
+ when(filter.deletedRowPositions()).thenReturn(index);
+
+ var rowIdMapping =
+ ColumnarBatchUtil.buildRowIdMapping(columnVectors, filter, batchStart, batchSize);
+
+ int[] expected = new int[batchSize];
+ int expectedLiveRows = 0;
+ for (int rowId = 0; rowId < batchSize; rowId++) {
+ if (!index.isDeleted(batchStart + rowId)) {
+ expected[expectedLiveRows] = rowId;
+ expectedLiveRows++;
+ }
+ }
+
+ if (expectedLiveRows == batchSize) {
+ assertThat(rowIdMapping).as("batch at %s", batchStart).isNull();
+ } else {
+ assertThat(rowIdMapping).as("batch at %s", batchStart).isNotNull();
+ assertThat((Integer) rowIdMapping.second()).isEqualTo(expectedLiveRows);
+ assertThat(Arrays.copyOf((int[]) rowIdMapping.first(), expectedLiveRows))
+ .as("batch at %s", batchStart)
+ .containsExactly(Arrays.copyOf(expected, expectedLiveRows));
+ verify(filter, times(batchSize - expectedLiveRows)).incrementDeleteCount();
+ }
+ }
+ }
+
+ private static PositionDeleteIndex positionIndex(long... positions) {
+ List<Long> list = Lists.newArrayList();
+ for (long position : positions) {
+ list.add(position);
+ }
+ return positionIndex(list);
+ }
+
+ private static PositionDeleteIndex positionIndex(List<Long> positions) {
+ return Deletes.toPositionIndex(CloseableIterable.withNoopClose(positions));
+ }
+
private ColumnVector[] mockColumnVector() {
// Create a mocked Int column vector with values: 40, 41, 42, 43, 44
ColumnVector intVector = mock(ColumnVector.class);