exprs,
SelectionVectorType selectionVectorType,
@@ -202,13 +202,19 @@ public static synchronized Projector make(
// Invoke the JNI layer to create the LLVM module representing the expressions
GandivaTypes.Schema schemaBuf = ArrowTypeHelper.arrowSchemaToProtobuf(schema);
+ byte[] schemaBytes = schemaBuf.toByteArray();
+ byte[] exprBytes = builder.build().toByteArray();
JniWrapper wrapper = JniLoader.getInstance().getWrapper();
+ // No lock here, deliberately. This method used to be `static synchronized`, which serialized
+ // every LLVM compilation in the process, because concurrent builds for the same native
+ // expression-cache key could race and fail with a duplicate-LLVM-symbol error (GH-601).
+ // That race was a double read of the native object cache inside Projector::Make(), and it is
+ // fixed in Gandiva itself -- see "GH-601: Fix TOCTOU race in Gandiva's LLVM object cache read"
+ // in the arrow C++ tree. Concurrent buildProjector() calls are safe against a Gandiva that
+ // contains that fix; do not re-add a lock here without first checking the native side.
long moduleId =
wrapper.buildProjector(
- schemaBuf.toByteArray(),
- builder.build().toByteArray(),
- selectionVectorType.getNumber(),
- configurationId);
+ schemaBytes, exprBytes, selectionVectorType.getNumber(), configurationId);
logger.debug("Created module for the projector with id {}", moduleId);
return new Projector(wrapper, moduleId, schema, exprs.size());
}
diff --git a/gandiva/src/test/java/org/apache/arrow/gandiva/evaluator/MakeConcurrencyBenchmark.java b/gandiva/src/test/java/org/apache/arrow/gandiva/evaluator/MakeConcurrencyBenchmark.java
new file mode 100644
index 0000000000..397e850894
--- /dev/null
+++ b/gandiva/src/test/java/org/apache/arrow/gandiva/evaluator/MakeConcurrencyBenchmark.java
@@ -0,0 +1,277 @@
+/*
+ * 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.arrow.gandiva.evaluator;
+
+import com.google.common.collect.Lists;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CyclicBarrier;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicLong;
+import org.apache.arrow.gandiva.expression.ExpressionTree;
+import org.apache.arrow.gandiva.expression.TreeBuilder;
+import org.apache.arrow.gandiva.expression.TreeNode;
+import org.apache.arrow.vector.types.pojo.Field;
+import org.apache.arrow.vector.types.pojo.Schema;
+import org.junit.jupiter.api.Disabled;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Measures the throughput of concurrent {@link Projector#make} calls, to quantify what removing the
+ * process-wide lock on {@code make()} actually buys (GH-601).
+ *
+ * {@code Projector.make()} used to be declared {@code static synchronized}, so every LLVM module
+ * build in the JVM serialized on one monitor regardless of whether the calls were related. That
+ * monitor was {@code Projector.class}, which means the old behaviour can be reproduced exactly by
+ * wrapping the call in {@code synchronized (Projector.class)}. This benchmark runs both arms in a
+ * single JVM over identical pre-generated inputs, so the comparison does not depend on two builds
+ * or two machine states.
+ *
+ *
Two workloads, because a cache hit is not free:
+ *
+ *
+ * - cold — every call uses a distinct expression, so each one is a real LLVM compile.
+ * This is the case the original JFR profile was showing: unrelated concurrent queries queuing
+ * behind one lock.
+ *
- warm — every call shares one expression, so all but the first hit Gandiva's native
+ * object cache. A hit still constructs a fresh TargetMachine, LLJIT, LLVMContext and Module,
+ * installs ~150 absolute symbols, decomposes the expression tree and relocates the cached
+ * object, so it is milliseconds of real work that the old lock also serialized.
+ *
+ *
+ * Do not expect linear scaling from the unlocked arm. Two process-wide native locks remain on
+ * this path — the expression cache's own mutex, taken on every lookup and insert, and the JNI
+ * module-id map's mutex, taken on every make and every close. Where the curve flattens is where
+ * those start to dominate.
+ *
+ *
Disabled by default; this is a benchmark, not a test. Run it with:
+ *
+ *
+ * mvn -Parrow-jni -pl gandiva -Dtest=MakeConcurrencyBenchmark -DfailIfNoTests=false \
+ * -Djunit.jupiter.conditions.deactivate='*' \
+ * -Darrow.cpp.build.dir=/path/to/dir/containing/libgandiva_jni.so test
+ *
+ *
+ * Keep {@code COLD_MAKES * THREAD_COUNTS.length} well under Gandiva's native cache capacity
+ * (default 5000, overridable only via the {@code GANDIVA_CACHE_SIZE} environment variable, which is
+ * read once per process) so that LRU eviction does not perturb the cold numbers.
+ */
+@Disabled("perf benchmark; run explicitly, see class javadoc")
+public class MakeConcurrencyBenchmark extends BaseEvaluatorTest {
+
+ private static final int[] THREAD_COUNTS = {1, 2, 4, 8, 16};
+
+ /** Distinct compiles per measured cold run. Each is tens of ms, so keep this modest. */
+ private static final int COLD_MAKES = 256;
+
+ /** Cache-hit makes per measured warm run. Each is ~ms, so this can be larger. */
+ private static final int WARM_MAKES = 2048;
+
+ /** Distinct expressions burned by warmup passes, reserved outside the measured key ranges. */
+ private static final int WARMUP_MAKES = 64;
+
+ /** Monotonic source of distinct cache keys, so no two runs in this JVM collide. */
+ private static final AtomicLong KEY_SEQUENCE = new AtomicLong();
+
+ /** Whether the timed call emulates the old {@code static synchronized} declaration. */
+ private enum Arm {
+ LOCKED("legacy (static synchronized)"),
+ UNLOCKED("current (no lock)");
+
+ private final String label;
+
+ Arm(String label) {
+ this.label = label;
+ }
+ }
+
+ /**
+ * Builds one expression whose native cache key is unique to {@code key}.
+ *
+ *
Varying an integer literal is the cheapest way to get a distinct key: Gandiva's cache key is
+ * derived from the printed form of the expression, and changing the literal leaves the generated
+ * IR the same size, so per-compile cost stays flat across the sweep. Note this deliberately
+ * avoids any {@code like()} function — Gandiva mixes the thread id into the cache key for those,
+ * which would make the hit/miss ratio depend on thread count.
+ */
+ private List distinctExpr(long key) {
+ Field x = Field.nullable("x", int32);
+ TreeNode sum =
+ TreeBuilder.makeFunction(
+ "add",
+ Lists.newArrayList(TreeBuilder.makeField(x), TreeBuilder.makeLiteral((int) key)),
+ int32);
+ return Lists.newArrayList(TreeBuilder.makeExpression(sum, Field.nullable("out", int32)));
+ }
+
+ private Schema schema() {
+ return new Schema(Lists.newArrayList(Field.nullable("x", int32)));
+ }
+
+ /**
+ * Runs {@code numMakes} Projector.make() calls across {@code numThreads} threads and returns the
+ * wall-clock nanos for the whole batch.
+ *
+ * @param keys the cache key to use for each call; pre-generated so expression construction is not
+ * inside the timed region. Repeat a value to force a cache hit.
+ */
+ private long timeMakes(Arm arm, int numThreads, List keys) throws Exception {
+ final Schema schema = schema();
+
+ // Build every expression up front: only make() should be inside the timed region.
+ List> exprs = new ArrayList<>(keys.size());
+ for (Long key : keys) {
+ exprs.add(distinctExpr(key));
+ }
+
+ ExecutorService pool = Executors.newFixedThreadPool(numThreads);
+ List failures = Collections.synchronizedList(new ArrayList<>());
+ // All threads meet here, so the measured window is contention and not pool ramp-up.
+ CyclicBarrier gate = new CyclicBarrier(numThreads + 1);
+
+ try {
+ List> futures = new ArrayList<>();
+ for (int t = 0; t < numThreads; t++) {
+ final int threadIndex = t;
+ futures.add(
+ pool.submit(
+ () -> {
+ try {
+ gate.await(60, TimeUnit.SECONDS);
+ // Stripe the work so every thread does an equal share.
+ for (int i = threadIndex; i < exprs.size(); i += numThreads) {
+ makeOnce(arm, schema, exprs.get(i));
+ }
+ } catch (Throwable e) {
+ failures.add(e);
+ }
+ }));
+ }
+
+ gate.await(60, TimeUnit.SECONDS);
+ long start = System.nanoTime();
+ for (Future> future : futures) {
+ future.get(10, TimeUnit.MINUTES);
+ }
+ long elapsed = System.nanoTime() - start;
+
+ if (!failures.isEmpty()) {
+ AssertionError error = new AssertionError("make() failed during benchmark");
+ error.initCause(failures.get(0));
+ throw error;
+ }
+ return elapsed;
+ } finally {
+ pool.shutdownNow();
+ }
+ }
+
+ /**
+ * One make/close pair. The LOCKED arm holds the {@code Projector.class} monitor across the call,
+ * which is exactly what {@code public static synchronized Projector make(...)} did. Verified that
+ * this mechanism works the same as the original sycnhronized method call.
+ */
+ private void makeOnce(Arm arm, Schema schema, List exprs) throws Exception {
+ Projector projector = null;
+ try {
+ if (arm == Arm.LOCKED) {
+ synchronized (Projector.class) {
+ projector = Projector.make(schema, exprs);
+ }
+ } else {
+ projector = Projector.make(schema, exprs);
+ }
+ } finally {
+ // Projector is not AutoCloseable and has no Cleaner: a dropped reference leaks a whole LLJIT,
+ // which at these iteration counts will exhaust memory.
+ if (projector != null) {
+ projector.close();
+ }
+ }
+ }
+
+ /** Distinct key per call: every make() is a real LLVM compile. */
+ private List coldKeys(int count) {
+ List keys = new ArrayList<>(count);
+ for (int i = 0; i < count; i++) {
+ keys.add(KEY_SEQUENCE.incrementAndGet());
+ }
+ return keys;
+ }
+
+ /** One shared key: all but the first make() hits the native object cache. */
+ private List warmKeys(int count) {
+ long key = KEY_SEQUENCE.incrementAndGet();
+ List keys = new ArrayList<>(count);
+ for (int i = 0; i < count; i++) {
+ keys.add(key);
+ }
+ return keys;
+ }
+
+ private void warmup() throws Exception {
+ // Absorbs JVM JIT, the one-time JniLoader native load, and LLVM's one-time target init.
+ timeMakes(Arm.UNLOCKED, 4, coldKeys(WARMUP_MAKES));
+ }
+
+ @Test
+ public void benchmarkConcurrentMake() throws Exception {
+ warmup();
+
+ StringBuilder table = new StringBuilder();
+ table.append("\n### Projector.make() throughput (makes/sec, higher is better)\n\n");
+ table.append("cold = distinct expression per call (real LLVM compile)\n");
+ table.append("warm = one shared expression (native object-cache hits)\n\n");
+ table.append("| threads | cold legacy | cold current | cold speedup ");
+ table.append("| warm legacy | warm current | warm speedup |\n");
+ table.append("|--------:|------------:|-------------:|-------------:");
+ table.append("|------------:|-------------:|-------------:|\n");
+
+ for (int numThreads : THREAD_COUNTS) {
+ double coldLocked = rate(COLD_MAKES, timeMakes(Arm.LOCKED, numThreads, coldKeys(COLD_MAKES)));
+ double coldFree = rate(COLD_MAKES, timeMakes(Arm.UNLOCKED, numThreads, coldKeys(COLD_MAKES)));
+ double warmLocked = rate(WARM_MAKES, timeMakes(Arm.LOCKED, numThreads, warmKeys(WARM_MAKES)));
+ double warmFree = rate(WARM_MAKES, timeMakes(Arm.UNLOCKED, numThreads, warmKeys(WARM_MAKES)));
+
+ table.append(
+ String.format(
+ "| %7d | %11.1f | %12.1f | %11.2fx | %11.1f | %12.1f | %11.2fx |%n",
+ numThreads,
+ coldLocked,
+ coldFree,
+ coldFree / coldLocked,
+ warmLocked,
+ warmFree,
+ warmFree / warmLocked));
+ }
+
+ table.append("\nArms: legacy = ").append(Arm.LOCKED.label);
+ table.append(", current = ").append(Arm.UNLOCKED.label).append('\n');
+ table.append("Cores available: ").append(Runtime.getRuntime().availableProcessors());
+ table.append(" (speedup is bounded by this, not by thread count)\n");
+
+ System.out.println(table);
+ }
+
+ private static double rate(int numMakes, long elapsedNanos) {
+ return numMakes * 1_000_000_000.0 / elapsedNanos;
+ }
+}
diff --git a/gandiva/src/test/java/org/apache/arrow/gandiva/evaluator/ProjectorTest.java b/gandiva/src/test/java/org/apache/arrow/gandiva/evaluator/ProjectorTest.java
index f2590226b1..f2d626f7ab 100644
--- a/gandiva/src/test/java/org/apache/arrow/gandiva/evaluator/ProjectorTest.java
+++ b/gandiva/src/test/java/org/apache/arrow/gandiva/evaluator/ProjectorTest.java
@@ -19,6 +19,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -29,10 +30,13 @@
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collections;
import java.util.List;
import java.util.Set;
+import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;
import org.apache.arrow.gandiva.exceptions.GandivaException;
@@ -99,12 +103,93 @@ List binaryBufs(String[] strings) {
return varBufs(strings, utf16Charset);
}
+ /**
+ * Builds projectors concurrently and fails if any of them errors out.
+ *
+ * Failures here surface as a {@link GandivaException} carrying a native {@code CodeGenError},
+ * not as a JVM crash -- see GH-601, where concurrent builds for the same native expression-cache
+ * key raced and produced "Duplicate definition of symbol 'expr_0_0'". Every exception must
+ * therefore be collected and re-thrown; swallowing them makes this test unable to observe the
+ * very bug it exists for.
+ *
+ * @param schemas schemas to pick from, one per task, round-robin
+ * @param exprs the expressions to compile
+ * @param configOptions custom configuration, or null for the default
+ */
+ private void makeProjectorsConcurrently(
+ List schemas,
+ List exprs,
+ ConfigurationBuilder.ConfigOptions configOptions)
+ throws Exception {
+ final int numTasks = 1000;
+ ExecutorService executors = Executors.newFixedThreadPool(16);
+ List failures = Collections.synchronizedList(new ArrayList<>());
+
+ try {
+ IntStream.range(0, numTasks)
+ .forEach(
+ i -> {
+ Schema schema = schemas.get(i % schemas.size());
+ executors.submit(
+ () -> {
+ Projector evaluator = null;
+ try {
+ evaluator =
+ configOptions == null
+ ? Projector.make(schema, exprs)
+ : Projector.make(schema, exprs, configOptions);
+ assertNotNull(evaluator);
+ } catch (Throwable t) {
+ failures.add(t);
+ } finally {
+ if (evaluator != null) {
+ try {
+ evaluator.close();
+ } catch (Throwable t) {
+ failures.add(t);
+ }
+ }
+ }
+ });
+ });
+ executors.shutdown();
+ assertTrue(
+ executors.awaitTermination(100, java.util.concurrent.TimeUnit.SECONDS),
+ "projector builds did not finish within the timeout");
+ } finally {
+ executors.shutdownNow();
+ }
+
+ if (!failures.isEmpty()) {
+ AssertionError error =
+ new AssertionError(
+ failures.size()
+ + " of "
+ + numTasks
+ + " concurrent Projector.make() calls failed; first failure attached");
+ error.initCause(failures.get(0));
+ throw error;
+ }
+ }
+
+ private List greaterThanExpr(Field a, Field b) {
+ TreeNode aNode = TreeBuilder.makeField(a);
+ TreeNode bNode = TreeBuilder.makeField(b);
+ List args = Lists.newArrayList(aNode, bNode);
+
+ TreeNode cond = TreeBuilder.makeFunction("greater_than", args, boolType);
+ TreeNode ifNode = TreeBuilder.makeIf(cond, aNode, bNode, int64);
+
+ ExpressionTree expr = TreeBuilder.makeExpression(ifNode, Field.nullable("c", int64));
+ return Lists.newArrayList(expr);
+ }
+
private void testMakeProjectorParallel(ConfigurationBuilder.ConfigOptions configOptions)
- throws InterruptedException {
+ throws Exception {
List schemas = Lists.newArrayList();
Field a = Field.nullable("a", int64);
Field b = Field.nullable("b", int64);
- IntStream.range(0, 1000)
+ IntStream.range(0, 100)
.forEach(
i -> {
Field c = Field.nullable("" + i, int64);
@@ -112,40 +197,94 @@ private void testMakeProjectorParallel(ConfigurationBuilder.ConfigOptions config
schemas.add(new Schema(cols));
});
- TreeNode aNode = TreeBuilder.makeField(a);
- TreeNode bNode = TreeBuilder.makeField(b);
- List args = Lists.newArrayList(aNode, bNode);
+ // Build projectors in parallel across many distinct schemas. This is the throughput case:
+ // the cache keys mostly differ, so these compilations should proceed independently.
+ makeProjectorsConcurrently(schemas, greaterThanExpr(a, b), configOptions);
+ }
- TreeNode cond = TreeBuilder.makeFunction("greater_than", args, boolType);
- TreeNode ifNode = TreeBuilder.makeIf(cond, aNode, bNode, int64);
+ /**
+ * The actual GH-601 shape: every thread races to build the *same* native expression-cache entry,
+ * starting from a genuine cache miss.
+ *
+ * Two details matter, and both were missing from the older parallel test. First, the threads
+ * are released from a {@link CyclicBarrier} rather than trickling in as the executor ramps up, so
+ * the builds genuinely overlap. Second, each round uses a fresh schema: once a key is in the
+ * native cache every later build takes the cached path and the race window is gone, so a single
+ * key gives at most one chance to observe it.
+ *
+ *
Be aware of what this test is and is not. Against a Gandiva without the native fix it does
+ * reproduce the duplicate-symbol failure, but only at roughly one round in 300 -- at the round
+ * count below it will usually pass even on affected builds. Treat it as a smoke test that
+ * concurrent same-key builds succeed and produce usable projectors. The reliable regression guard
+ * for GH-601 is the native test (TestConcurrentMake in
+ * cpp/src/gandiva/tests/concurrent_make_test.cc), which reproduces within a handful of iterations
+ * because it races the cache read directly without the protobuf and JNI round trip in between.
+ */
+ private void testMakeProjectorParallelSameKey(ConfigurationBuilder.ConfigOptions configOptions)
+ throws Exception {
+ final int numThreads = 16;
+ final int numRounds = 50;
- ExpressionTree expr = TreeBuilder.makeExpression(ifNode, Field.nullable("c", int64));
- List exprs = Lists.newArrayList(expr);
+ Field a = Field.nullable("a", int64);
+ Field b = Field.nullable("b", int64);
+ List exprs = greaterThanExpr(a, b);
- // build projectors in parallel choosing schema at random
- // this should hit the same cache entry thus exposing
- // any threading issues.
- ExecutorService executors = Executors.newFixedThreadPool(16);
+ ExecutorService executors = Executors.newFixedThreadPool(numThreads);
+ List failures = Collections.synchronizedList(new ArrayList<>());
- IntStream.range(0, 1000)
- .forEach(
- i -> {
+ try {
+ for (int round = 0; round < numRounds; round++) {
+ // Unique per round => the first thread to arrive genuinely misses the native cache.
+ Field c = Field.nullable("c" + round, int64);
+ final Schema schema = new Schema(Lists.newArrayList(a, b, c));
+ final CyclicBarrier gate = new CyclicBarrier(numThreads);
+
+ List> futures = new ArrayList<>();
+ for (int i = 0; i < numThreads; i++) {
+ futures.add(
executors.submit(
() -> {
+ Projector evaluator = null;
try {
- Projector evaluator =
+ gate.await(30, java.util.concurrent.TimeUnit.SECONDS);
+ evaluator =
configOptions == null
- ? Projector.make(schemas.get((int) (Math.random() * 100)), exprs)
- : Projector.make(
- schemas.get((int) (Math.random() * 100)), exprs, configOptions);
- evaluator.close();
- } catch (GandivaException e) {
- e.printStackTrace();
+ ? Projector.make(schema, exprs)
+ : Projector.make(schema, exprs, configOptions);
+ assertNotNull(evaluator);
+ } catch (Throwable t) {
+ failures.add(t);
+ } finally {
+ if (evaluator != null) {
+ try {
+ evaluator.close();
+ } catch (Throwable t) {
+ failures.add(t);
+ }
+ }
}
- });
- });
- executors.shutdown();
- executors.awaitTermination(100, java.util.concurrent.TimeUnit.SECONDS);
+ }));
+ }
+ for (Future> future : futures) {
+ future.get(120, java.util.concurrent.TimeUnit.SECONDS);
+ }
+ }
+ } finally {
+ executors.shutdownNow();
+ }
+
+ if (!failures.isEmpty()) {
+ AssertionError error =
+ new AssertionError(
+ failures.size()
+ + " concurrent same-cache-key Projector.make() calls failed across "
+ + numRounds
+ + " rounds of "
+ + numThreads
+ + " threads; first failure attached");
+ error.initCause(failures.get(0));
+ throw error;
+ }
}
@Test
@@ -156,6 +295,14 @@ public void testMakeProjectorParallel() throws Exception {
new ConfigurationBuilder.ConfigOptions().withTargetCPU(false).withOptimize(false));
}
+ @Test
+ public void testMakeProjectorParallelSameKey() throws Exception {
+ // Only the default configuration. The configuration is part of the native cache key, so varying
+ // it just builds unrelated cache entries -- it adds runtime without exercising anything new in
+ // the same-key race this test is about.
+ testMakeProjectorParallelSameKey(null);
+ }
+
// Will be fixed by https://issues.apache.org/jira/browse/ARROW-4371
@Disabled
@Test
diff --git a/memory/memory-core/src/main/java/org/apache/arrow/memory/BaseAllocator.java b/memory/memory-core/src/main/java/org/apache/arrow/memory/BaseAllocator.java
index 72946e7fb7..1a591cca08 100644
--- a/memory/memory-core/src/main/java/org/apache/arrow/memory/BaseAllocator.java
+++ b/memory/memory-core/src/main/java/org/apache/arrow/memory/BaseAllocator.java
@@ -432,6 +432,20 @@ public AllocationReservation newReservation() {
@Override
public synchronized void close() {
+ try {
+ closeImpl();
+ } catch (Exception e) {
+ logger.warn(
+ "BufferAllocator.close() of {} got an exception {} - Details {}",
+ this,
+ e,
+ this.toVerboseString());
+
+ throw e;
+ }
+ }
+
+ private synchronized void closeImpl() {
/*
* Some owners may close more than once because of complex cleanup and shutdown
* procedures.
diff --git a/pom.xml b/pom.xml
index 53f12b4f56..3050286744 100644
--- a/pom.xml
+++ b/pom.xml
@@ -100,7 +100,7 @@ under the License.
33.7.1-jre
4.2.17.Final
1.84.0
- 4.35.1
+ 3.25.5
2.22.0
3.5.0
25.2.10
diff --git a/vector/pom.xml b/vector/pom.xml
index aba35a606a..e0a71b5bd7 100644
--- a/vector/pom.xml
+++ b/vector/pom.xml
@@ -89,6 +89,14 @@ under the License.
+
+
+ codegen
+
+ ${basedir}/src/main/codegen
+
+
org.apache.maven.plugins
diff --git a/vector/src/main/java/org/apache/arrow/vector/complex/LargeListVector.java b/vector/src/main/java/org/apache/arrow/vector/complex/LargeListVector.java
index 9368b5ce3c..66d629cf1f 100644
--- a/vector/src/main/java/org/apache/arrow/vector/complex/LargeListVector.java
+++ b/vector/src/main/java/org/apache/arrow/vector/complex/LargeListVector.java
@@ -305,10 +305,12 @@ public void exportCDataBuffers(List buffers, ArrowBuf buffersPtr, long
/** Set the reader and writer indexes for the inner buffers. */
private void setReaderAndWriterIndex() {
+ final long requiredOffsetBufferCapacity = (long) (valueCount + 1) * OFFSET_WIDTH;
validityBuffer.readerIndex(0);
offsetBuffer.readerIndex(0);
if (valueCount == 0) {
validityBuffer.writerIndex(0);
+ ensureEmptyOffsetBufferCapacity(requiredOffsetBufferCapacity);
} else {
validityBuffer.writerIndex(BitVectorHelper.getValidityBufferSizeFromCount(valueCount));
}
@@ -319,6 +321,19 @@ private void setReaderAndWriterIndex() {
offsetBuffer.writerIndex((long) (valueCount + 1) * OFFSET_WIDTH);
}
+ private void ensureEmptyOffsetBufferCapacity(long requiredCapacity) {
+ if (offsetBuffer.capacity() >= requiredCapacity) {
+ return;
+ }
+ long previousOffsetAllocationSizeInBytes = offsetAllocationSizeInBytes;
+ ArrowBuf oldOffsetBuffer = offsetBuffer;
+ offsetBuffer = allocateOffsetBuffer(requiredCapacity);
+ offsetBuffer.setBytes(
+ 0, oldOffsetBuffer, 0, Math.min(oldOffsetBuffer.capacity(), requiredCapacity));
+ offsetAllocationSizeInBytes = previousOffsetAllocationSizeInBytes;
+ oldOffsetBuffer.getReferenceManager().release();
+ }
+
/**
* Get the inner vectors.
*
@@ -674,24 +689,30 @@ public void splitAndTransfer(int startIndex, int length) {
startIndex,
length,
valueCount);
- final long startPoint = offsetBuffer.getLong((long) startIndex * OFFSET_WIDTH);
- final long sliceLength =
- offsetBuffer.getLong((long) (startIndex + length) * OFFSET_WIDTH) - startPoint;
to.clear();
- to.offsetBuffer = to.allocateOffsetBuffer((length + 1) * OFFSET_WIDTH);
- /* splitAndTransfer offset buffer */
- for (int i = 0; i < length + 1; i++) {
- final long relativeOffset =
- offsetBuffer.getLong((long) (startIndex + i) * OFFSET_WIDTH) - startPoint;
- to.offsetBuffer.setLong((long) i * OFFSET_WIDTH, relativeOffset);
+ if (length > 0) {
+ final long startPoint = offsetBuffer.getLong((long) startIndex * OFFSET_WIDTH);
+ final long sliceLength =
+ offsetBuffer.getLong((long) (startIndex + length) * OFFSET_WIDTH) - startPoint;
+ to.offsetBuffer = to.allocateOffsetBuffer((length + 1) * OFFSET_WIDTH);
+ /* splitAndTransfer offset buffer */
+ for (int i = 0; i < length + 1; i++) {
+ final long relativeOffset =
+ offsetBuffer.getLong((long) (startIndex + i) * OFFSET_WIDTH) - startPoint;
+ to.offsetBuffer.setLong((long) i * OFFSET_WIDTH, relativeOffset);
+ }
+ /* splitAndTransfer validity buffer */
+ splitAndTransferValidityBuffer(startIndex, length, to);
+ /* splitAndTransfer data buffer */
+ dataTransferPair.splitAndTransfer(
+ checkedCastToInt(startPoint), checkedCastToInt(sliceLength));
+ to.lastSet = length - 1;
+ to.setValueCount(length);
+ } else {
+ to.ensureEmptyOffsetBufferCapacity(OFFSET_WIDTH);
+ dataTransferPair.splitAndTransfer(0, 0);
+ to.setValueCount(0);
}
- /* splitAndTransfer validity buffer */
- splitAndTransferValidityBuffer(startIndex, length, to);
- /* splitAndTransfer data buffer */
- dataTransferPair.splitAndTransfer(
- checkedCastToInt(startPoint), checkedCastToInt(sliceLength));
- to.lastSet = length - 1;
- to.setValueCount(length);
}
@Override
diff --git a/vector/src/main/java/org/apache/arrow/vector/complex/ListVector.java b/vector/src/main/java/org/apache/arrow/vector/complex/ListVector.java
index 6c3993df63..2802cd7577 100644
--- a/vector/src/main/java/org/apache/arrow/vector/complex/ListVector.java
+++ b/vector/src/main/java/org/apache/arrow/vector/complex/ListVector.java
@@ -263,10 +263,12 @@ public void exportCDataBuffers(List buffers, ArrowBuf buffersPtr, long
/** Set the reader and writer indexes for the inner buffers. */
private void setReaderAndWriterIndex() {
+ final long requiredOffsetBufferCapacity = (long) (valueCount + 1) * OFFSET_WIDTH;
validityBuffer.readerIndex(0);
offsetBuffer.readerIndex(0);
if (valueCount == 0) {
validityBuffer.writerIndex(0);
+ ensureEmptyOffsetBufferCapacity(requiredOffsetBufferCapacity);
} else {
validityBuffer.writerIndex(BitVectorHelper.getValidityBufferSizeFromCount(valueCount));
}
@@ -277,6 +279,19 @@ private void setReaderAndWriterIndex() {
offsetBuffer.writerIndex((long) (valueCount + 1) * OFFSET_WIDTH);
}
+ private void ensureEmptyOffsetBufferCapacity(long requiredCapacity) {
+ if (offsetBuffer.capacity() >= requiredCapacity) {
+ return;
+ }
+ long previousOffsetAllocationSizeInBytes = offsetAllocationSizeInBytes;
+ ArrowBuf oldOffsetBuffer = offsetBuffer;
+ offsetBuffer = allocateOffsetBuffer(requiredCapacity);
+ offsetBuffer.setBytes(
+ 0, oldOffsetBuffer, 0, Math.min(oldOffsetBuffer.capacity(), requiredCapacity));
+ offsetAllocationSizeInBytes = previousOffsetAllocationSizeInBytes;
+ oldOffsetBuffer.getReferenceManager().release();
+ }
+
/**
* Get the inner vectors.
*
@@ -572,6 +587,10 @@ public void splitAndTransfer(int startIndex, int length) {
dataTransferPair.splitAndTransfer(startPoint, sliceLength);
to.lastSet = length - 1;
to.setValueCount(length);
+ } else {
+ to.ensureEmptyOffsetBufferCapacity(OFFSET_WIDTH);
+ dataTransferPair.splitAndTransfer(0, 0);
+ to.setValueCount(0);
}
}
diff --git a/vector/src/main/java/org/apache/arrow/vector/complex/StructVector.java b/vector/src/main/java/org/apache/arrow/vector/complex/StructVector.java
index 5e5bb7fc21..21ea9c6f05 100644
--- a/vector/src/main/java/org/apache/arrow/vector/complex/StructVector.java
+++ b/vector/src/main/java/org/apache/arrow/vector/complex/StructVector.java
@@ -529,7 +529,7 @@ public void reAlloc() {
super.reAlloc();
}
- private void reallocValidityBuffer() {
+ protected void reallocValidityBuffer() {
final int currentBufferCapacity = checkedCastToInt(validityBuffer.capacity());
long newAllocationSize = getNewAllocationSize(currentBufferCapacity);
diff --git a/vector/src/test/java/org/apache/arrow/vector/TestLargeListVector.java b/vector/src/test/java/org/apache/arrow/vector/TestLargeListVector.java
index b6823fbf3c..54f5f198a6 100644
--- a/vector/src/test/java/org/apache/arrow/vector/TestLargeListVector.java
+++ b/vector/src/test/java/org/apache/arrow/vector/TestLargeListVector.java
@@ -966,6 +966,91 @@ public void testGetBufferSizeFor() {
}
}
+ @Test
+ public void testEmptyLargeListOffsetBuffer() {
+ try (LargeListVector list = LargeListVector.empty("list", allocator)) {
+ list.addOrGetVector(FieldType.nullable(MinorType.INT.getType()));
+ list.allocateNew();
+ list.setValueCount(0);
+
+ assertEmptyLargeListOffsetBuffer(list);
+ }
+ }
+
+ @Test
+ public void testUnallocatedEmptyLargeListOffsetBuffer() {
+ try (LargeListVector list = LargeListVector.empty("list", allocator)) {
+ list.addOrGetVector(FieldType.nullable(MinorType.INT.getType()));
+ list.setValueCount(0);
+
+ assertEmptyLargeListOffsetBuffer(list);
+ }
+ }
+
+ @Test
+ public void testSplitAndTransferEmptyLargeListOffsetBuffer() {
+ try (LargeListVector source = LargeListVector.empty("source", allocator);
+ LargeListVector target = LargeListVector.empty("target", allocator)) {
+ source.addOrGetVector(FieldType.nullable(MinorType.INT.getType()));
+ target.addOrGetVector(FieldType.nullable(MinorType.INT.getType()));
+ source.allocateNew();
+ source.setValueCount(0);
+
+ TransferPair transferPair = source.makeTransferPair(target);
+ transferPair.splitAndTransfer(0, 0);
+
+ assertEmptyLargeListOffsetBuffer(target);
+ }
+ }
+
+ @Test
+ public void testSplitAndTransferEmptyLargeListAllocatesOffsetBuffer() {
+ try (LargeListVector fromVector = LargeListVector.empty("fromVector", allocator);
+ LargeListVector toVector = LargeListVector.empty("toVector", allocator)) {
+ fromVector.addOrGetVector(FieldType.nullable(MinorType.INT.getType()));
+ fromVector.allocateNew();
+ fromVector.setValueCount(0);
+
+ TransferPair transferPair = fromVector.makeTransferPair(toVector);
+ transferPair.splitAndTransfer(0, 0);
+
+ assertAllocatedEmptyLargeListOffsetBuffer(toVector);
+ }
+ }
+
+ @Test
+ public void testSplitAndTransferEmptyNestedLargeListAllocatesOffsetBuffers() {
+ try (LargeListVector fromVector = LargeListVector.empty("fromVector", allocator);
+ LargeListVector toVector = LargeListVector.empty("toVector", allocator)) {
+ fromVector.addOrGetVector(FieldType.nullable(MinorType.LARGELIST.getType()));
+ LargeListVector childVector = (LargeListVector) fromVector.getDataVector();
+ childVector.addOrGetVector(FieldType.nullable(MinorType.INT.getType()));
+ fromVector.allocateNew();
+ fromVector.setValueCount(0);
+
+ TransferPair transferPair = fromVector.makeTransferPair(toVector);
+ transferPair.splitAndTransfer(0, 0);
+
+ assertAllocatedEmptyLargeListOffsetBuffer(toVector);
+ assertAllocatedEmptyLargeListOffsetBuffer((LargeListVector) toVector.getDataVector());
+ }
+ }
+
+ private ArrowBuf assertEmptyLargeListOffsetBuffer(LargeListVector list) {
+ List buffers = list.getFieldBuffers();
+ ArrowBuf offsetBuffer = buffers.get(1);
+ assertEquals(LargeListVector.OFFSET_WIDTH, offsetBuffer.readableBytes());
+ assertTrue(offsetBuffer.capacity() >= LargeListVector.OFFSET_WIDTH);
+ assertEquals(0L, offsetBuffer.getLong(0));
+ return offsetBuffer;
+ }
+
+ private void assertAllocatedEmptyLargeListOffsetBuffer(LargeListVector list) {
+ ArrowBuf offsetBuffer = list.getOffsetBuffer();
+ assertTrue(offsetBuffer.capacity() >= LargeListVector.OFFSET_WIDTH);
+ assertEquals(0L, offsetBuffer.getLong(0));
+ }
+
@Test
public void testIsEmpty() {
try (final LargeListVector vector = LargeListVector.empty("list", allocator)) {
@@ -1105,7 +1190,7 @@ public void testCopyValueSafeForExtensionType() throws Exception {
}
@Test
- public void testEmptyLargeListOffsetBuffer() {
+ public void testEmptyLargeListOffsetBufferSize() {
// Test that LargeListVector has correct readableBytes after allocation.
// According to Arrow spec, offset buffer must have N+1 entries.
// Even when N=0, it should contain [0].
diff --git a/vector/src/test/java/org/apache/arrow/vector/TestListVector.java b/vector/src/test/java/org/apache/arrow/vector/TestListVector.java
index 0c90b32abc..60b675a1b5 100644
--- a/vector/src/test/java/org/apache/arrow/vector/TestListVector.java
+++ b/vector/src/test/java/org/apache/arrow/vector/TestListVector.java
@@ -1142,6 +1142,91 @@ public void testGetBufferSizeFor() {
}
}
+ @Test
+ public void testEmptyListOffsetBuffer() {
+ try (ListVector list = ListVector.empty("list", allocator)) {
+ list.addOrGetVector(FieldType.nullable(MinorType.INT.getType()));
+ list.allocateNew();
+ list.setValueCount(0);
+
+ assertEmptyListOffsetBuffer(list);
+ }
+ }
+
+ @Test
+ public void testUnallocatedEmptyListOffsetBuffer() {
+ try (ListVector list = ListVector.empty("list", allocator)) {
+ list.addOrGetVector(FieldType.nullable(MinorType.INT.getType()));
+ list.setValueCount(0);
+
+ assertEmptyListOffsetBuffer(list);
+ }
+ }
+
+ @Test
+ public void testSplitAndTransferEmptyListOffsetBuffer() {
+ try (ListVector source = ListVector.empty("source", allocator);
+ ListVector target = ListVector.empty("target", allocator)) {
+ source.addOrGetVector(FieldType.nullable(MinorType.INT.getType()));
+ target.addOrGetVector(FieldType.nullable(MinorType.INT.getType()));
+ source.allocateNew();
+ source.setValueCount(0);
+
+ TransferPair transferPair = source.makeTransferPair(target);
+ transferPair.splitAndTransfer(0, 0);
+
+ assertEmptyListOffsetBuffer(target);
+ }
+ }
+
+ @Test
+ public void testSplitAndTransferEmptyListAllocatesOffsetBuffer() {
+ try (ListVector fromVector = ListVector.empty("fromVector", allocator);
+ ListVector toVector = ListVector.empty("toVector", allocator)) {
+ fromVector.addOrGetVector(FieldType.nullable(MinorType.INT.getType()));
+ fromVector.allocateNew();
+ fromVector.setValueCount(0);
+
+ TransferPair transferPair = fromVector.makeTransferPair(toVector);
+ transferPair.splitAndTransfer(0, 0);
+
+ assertAllocatedEmptyListOffsetBuffer(toVector);
+ }
+ }
+
+ @Test
+ public void testSplitAndTransferEmptyNestedListAllocatesOffsetBuffers() {
+ try (ListVector fromVector = ListVector.empty("fromVector", allocator);
+ ListVector toVector = ListVector.empty("toVector", allocator)) {
+ fromVector.addOrGetVector(FieldType.nullable(MinorType.LIST.getType()));
+ ListVector childVector = (ListVector) fromVector.getDataVector();
+ childVector.addOrGetVector(FieldType.nullable(MinorType.INT.getType()));
+ fromVector.allocateNew();
+ fromVector.setValueCount(0);
+
+ TransferPair transferPair = fromVector.makeTransferPair(toVector);
+ transferPair.splitAndTransfer(0, 0);
+
+ assertAllocatedEmptyListOffsetBuffer(toVector);
+ assertAllocatedEmptyListOffsetBuffer((ListVector) toVector.getDataVector());
+ }
+ }
+
+ private ArrowBuf assertEmptyListOffsetBuffer(ListVector list) {
+ List buffers = list.getFieldBuffers();
+ ArrowBuf offsetBuffer = buffers.get(1);
+ assertEquals(BaseRepeatedValueVector.OFFSET_WIDTH, offsetBuffer.readableBytes());
+ assertTrue(offsetBuffer.capacity() >= BaseRepeatedValueVector.OFFSET_WIDTH);
+ assertEquals(0, offsetBuffer.getInt(0));
+ return offsetBuffer;
+ }
+
+ private void assertAllocatedEmptyListOffsetBuffer(ListVector list) {
+ ArrowBuf offsetBuffer = list.getOffsetBuffer();
+ assertTrue(offsetBuffer.capacity() >= BaseRepeatedValueVector.OFFSET_WIDTH);
+ assertEquals(0, offsetBuffer.getInt(0));
+ }
+
@Test
public void testIsEmpty() {
try (final ListVector vector = ListVector.empty("list", allocator)) {
@@ -1380,7 +1465,7 @@ public void testCopyValueSafeForExtensionType() throws Exception {
}
@Test
- public void testEmptyListOffsetBuffer() {
+ public void testEmptyListOffsetBufferSize() {
// Test that ListVector has correct readableBytes after allocation.
// According to Arrow spec, offset buffer must have N+1 entries.
// Even when N=0, it should contain [0].
diff --git a/vector/src/test/java/org/apache/arrow/vector/TestSplitAndTransfer.java b/vector/src/test/java/org/apache/arrow/vector/TestSplitAndTransfer.java
index adf4eba10c..a51c3e74fa 100644
--- a/vector/src/test/java/org/apache/arrow/vector/TestSplitAndTransfer.java
+++ b/vector/src/test/java/org/apache/arrow/vector/TestSplitAndTransfer.java
@@ -28,10 +28,13 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import org.apache.arrow.memory.ArrowBuf;
import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.memory.RootAllocator;
+import org.apache.arrow.vector.complex.BaseRepeatedValueVector;
import org.apache.arrow.vector.complex.DenseUnionVector;
import org.apache.arrow.vector.complex.FixedSizeListVector;
+import org.apache.arrow.vector.complex.LargeListVector;
import org.apache.arrow.vector.complex.LargeListViewVector;
import org.apache.arrow.vector.complex.ListVector;
import org.apache.arrow.vector.complex.MapVector;
@@ -111,13 +114,16 @@ private void populateDenseUnionVector(final DenseUnionVector vector, int valueCo
@Test
public void testWithEmptyVector() {
// MapVector use TransferImpl from ListVector
- ListVector listVector = ListVector.empty("", allocator);
- TransferPair transferPair = listVector.getTransferPair(allocator);
- transferPair.splitAndTransfer(0, 0);
- assertEquals(0, transferPair.getTo().getValueCount());
+ try (ListVector listVector = ListVector.empty("", allocator)) {
+ TransferPair transferPair = listVector.getTransferPair(allocator);
+ try (ValueVector toVector = transferPair.getTo()) {
+ transferPair.splitAndTransfer(0, 0);
+ assertEquals(0, toVector.getValueCount());
+ }
+ }
// BaseFixedWidthVector
IntVector intVector = new IntVector("", allocator);
- transferPair = intVector.getTransferPair(allocator);
+ TransferPair transferPair = intVector.getTransferPair(allocator);
transferPair.splitAndTransfer(0, 0);
assertEquals(0, transferPair.getTo().getValueCount());
// BaseVariableWidthVector
@@ -911,6 +917,11 @@ public void testListVectorZeroStartIndexAndLength() {
tp.splitAndTransfer(0, 0);
assertEquals(valueCount, newListVector.getValueCount());
+ List buffers = newListVector.getFieldBuffers();
+ ArrowBuf offsetBuffer = buffers.get(1);
+ assertEquals(BaseRepeatedValueVector.OFFSET_WIDTH, offsetBuffer.readableBytes());
+ assertTrue(offsetBuffer.capacity() >= BaseRepeatedValueVector.OFFSET_WIDTH);
+ assertEquals(0, offsetBuffer.getInt(0));
newListVector.clear();
}
@@ -935,6 +946,29 @@ public void testLargeListViewVectorZeroStartIndexAndLength() {
}
}
+ @Test
+ public void testLargeListVectorZeroStartIndexAndLength() {
+ try (final LargeListVector listVector = LargeListVector.empty("largelist", allocator);
+ final LargeListVector newListVector = LargeListVector.empty("newList", allocator)) {
+
+ listVector.allocateNew();
+ final int valueCount = 0;
+ listVector.setValueCount(valueCount);
+
+ final TransferPair tp = listVector.makeTransferPair(newListVector);
+
+ tp.splitAndTransfer(0, 0);
+ assertEquals(valueCount, newListVector.getValueCount());
+ List buffers = newListVector.getFieldBuffers();
+ ArrowBuf offsetBuffer = buffers.get(1);
+ assertEquals(LargeListVector.OFFSET_WIDTH, offsetBuffer.readableBytes());
+ assertTrue(offsetBuffer.capacity() >= LargeListVector.OFFSET_WIDTH);
+ assertEquals(0L, offsetBuffer.getLong(0));
+
+ newListVector.clear();
+ }
+ }
+
@Test
public void testStructVectorZeroStartIndexAndLength() {
Map metadata = new HashMap<>();