diff --git a/gandiva/src/main/cpp/id_to_module_map.h b/gandiva/src/main/cpp/id_to_module_map.h index 98100955b5..ffb5e79ad9 100644 --- a/gandiva/src/main/cpp/id_to_module_map.h +++ b/gandiva/src/main/cpp/id_to_module_map.h @@ -38,9 +38,12 @@ class IdToModuleMap { } void Erase(jlong module_id) { - mtx_.lock(); - map_.erase(module_id); - mtx_.unlock(); + // Destruction can tear down an LLVM module. Release the map lock first. + typename decltype(map_)::node_type removed; + { + std::lock_guard guard(mtx_); + removed = map_.extract(module_id); + } } HOLDER Lookup(jlong module_id) { diff --git a/gandiva/src/main/cpp/jni_common.cc b/gandiva/src/main/cpp/jni_common.cc index 2851250072..bff3e44828 100644 --- a/gandiva/src/main/cpp/jni_common.cc +++ b/gandiva/src/main/cpp/jni_common.cc @@ -601,9 +601,10 @@ void releaseProjectorInput(jbyteArray schema_arr, jbyte* schema_bytes, env->ReleaseByteArrayElements(exprs_arr, exprs_bytes, JNI_ABORT); } -JNIEXPORT jlong JNICALL Java_org_apache_arrow_gandiva_evaluator_JniWrapper_buildProjector( - JNIEnv* env, jobject obj, jbyteArray schema_arr, jbyteArray exprs_arr, - jint selection_vector_type, jlong configuration_id) { +static jlong BuildProjector(JNIEnv* env, jbyteArray schema_arr, jbyteArray exprs_arr, + jint selection_vector_type, jlong configuration_id, + bool cache_only, jlong fallback_configuration_id = 0, + jbooleanArray fallback_used = nullptr) { jlong module_id = 0LL; std::shared_ptr projector; std::shared_ptr holder; @@ -677,7 +678,23 @@ JNIEXPORT jlong JNICALL Java_org_apache_arrow_gandiva_evaluator_JniWrapper_build break; } // good to invoke the evaluator now - status = Projector::Make(schema_ptr, expr_vector, mode, config, &projector); + if (fallback_used != nullptr) { + bool used_fallback = false; + status = Projector::TryMakeFromCacheWithFallback( + schema_ptr, expr_vector, mode, config, + ConfigHolder::MapLookup(fallback_configuration_id), &used_fallback, &projector); + jboolean flag = used_fallback ? JNI_TRUE : JNI_FALSE; + env->SetBooleanArrayRegion(fallback_used, 0, 1, &flag); + if (env->ExceptionCheck()) { + releaseProjectorInput(schema_arr, schema_bytes, exprs_arr, exprs_bytes, env); + return 0; + } + } else { + status = + cache_only + ? Projector::TryMakeFromCache(schema_ptr, expr_vector, mode, config, &projector) + : Projector::Make(schema_ptr, expr_vector, mode, config, &projector); + } if (!status.ok()) { ss << "Failed to make LLVM module due to " << status.message() << "\n"; @@ -685,6 +702,11 @@ JNIEXPORT jlong JNICALL Java_org_apache_arrow_gandiva_evaluator_JniWrapper_build goto err_out; } + if (projector == nullptr) { + releaseProjectorInput(schema_arr, schema_bytes, exprs_arr, exprs_bytes, env); + return 0; + } + // store the result in a map holder = std::shared_ptr( new ProjectorHolder(schema_ptr, ret_types, std::move(projector))); @@ -697,6 +719,31 @@ JNIEXPORT jlong JNICALL Java_org_apache_arrow_gandiva_evaluator_JniWrapper_build return module_id; } +JNIEXPORT jlong JNICALL Java_org_apache_arrow_gandiva_evaluator_JniWrapper_buildProjector( + JNIEnv* env, jobject obj, jbyteArray schema_arr, jbyteArray exprs_arr, + jint selection_vector_type, jlong configuration_id) { + return BuildProjector(env, schema_arr, exprs_arr, selection_vector_type, + configuration_id, false); +} + +JNIEXPORT jlong JNICALL +Java_org_apache_arrow_gandiva_evaluator_JniWrapper_buildProjectorFromCache( + JNIEnv* env, jobject obj, jbyteArray schema_arr, jbyteArray exprs_arr, + jint selection_vector_type, jlong configuration_id) { + return BuildProjector(env, schema_arr, exprs_arr, selection_vector_type, + configuration_id, true); +} + +JNIEXPORT jlong JNICALL +Java_org_apache_arrow_gandiva_evaluator_JniWrapper_buildProjectorFromCacheWithFallback( + JNIEnv* env, jobject obj, jbyteArray schema_arr, jbyteArray exprs_arr, + jint selection_vector_type, jlong configuration_id, jlong fallback_configuration_id, + jbooleanArray fallback_used) { + return BuildProjector(env, schema_arr, exprs_arr, selection_vector_type, + configuration_id, true, + fallback_configuration_id, fallback_used); +} + /// /// \brief Resizable buffer which resizes by doing a callback into java. /// @@ -902,9 +949,9 @@ void releaseFilterInput(jbyteArray schema_arr, jbyte* schema_bytes, env->ReleaseByteArrayElements(condition_arr, condition_bytes, JNI_ABORT); } -JNIEXPORT jlong JNICALL Java_org_apache_arrow_gandiva_evaluator_JniWrapper_buildFilter( - JNIEnv* env, jobject obj, jbyteArray schema_arr, jbyteArray condition_arr, - jlong configuration_id) { +static jlong BuildFilter(JNIEnv* env, jbyteArray schema_arr, jbyteArray condition_arr, + jlong configuration_id, bool cache_only, jlong fallback_configuration_id = 0, + jbooleanArray fallback_used = nullptr) { jlong module_id = 0LL; std::shared_ptr filter; std::shared_ptr holder; @@ -959,13 +1006,33 @@ JNIEXPORT jlong JNICALL Java_org_apache_arrow_gandiva_evaluator_JniWrapper_build } // good to invoke the filter builder now - status = Filter::Make(schema_ptr, condition_ptr, config, &filter); + if (fallback_used != nullptr) { + bool used_fallback = false; + status = Filter::TryMakeFromCacheWithFallback( + schema_ptr, condition_ptr, config, + ConfigHolder::MapLookup(fallback_configuration_id), &used_fallback, &filter); + jboolean flag = used_fallback ? JNI_TRUE : JNI_FALSE; + env->SetBooleanArrayRegion(fallback_used, 0, 1, &flag); + if (env->ExceptionCheck()) { + releaseFilterInput(schema_arr, schema_bytes, condition_arr, condition_bytes, env); + return 0; + } + } else { + status = cache_only + ? Filter::TryMakeFromCache(schema_ptr, condition_ptr, config, &filter) + : Filter::Make(schema_ptr, condition_ptr, config, &filter); + } if (!status.ok()) { ss << "Failed to make LLVM module due to " << status.message() << "\n"; releaseFilterInput(schema_arr, schema_bytes, condition_arr, condition_bytes, env); goto err_out; } + if (filter == nullptr) { + releaseFilterInput(schema_arr, schema_bytes, condition_arr, condition_bytes, env); + return 0; + } + // store the result in a map holder = std::shared_ptr(new FilterHolder(schema_ptr, std::move(filter))); module_id = filter_modules_.Insert(holder); @@ -977,6 +1044,28 @@ JNIEXPORT jlong JNICALL Java_org_apache_arrow_gandiva_evaluator_JniWrapper_build return module_id; } +JNIEXPORT jlong JNICALL Java_org_apache_arrow_gandiva_evaluator_JniWrapper_buildFilter( + JNIEnv* env, jobject obj, jbyteArray schema_arr, jbyteArray condition_arr, + jlong configuration_id) { + return BuildFilter(env, schema_arr, condition_arr, configuration_id, false); +} + +JNIEXPORT jlong JNICALL +Java_org_apache_arrow_gandiva_evaluator_JniWrapper_buildFilterFromCache( + JNIEnv* env, jobject obj, jbyteArray schema_arr, jbyteArray condition_arr, + jlong configuration_id) { + return BuildFilter(env, schema_arr, condition_arr, configuration_id, true); +} + +JNIEXPORT jlong JNICALL +Java_org_apache_arrow_gandiva_evaluator_JniWrapper_buildFilterFromCacheWithFallback( + JNIEnv* env, jobject obj, jbyteArray schema_arr, jbyteArray condition_arr, + jlong configuration_id, jlong fallback_configuration_id, + jbooleanArray fallback_used) { + return BuildFilter(env, schema_arr, condition_arr, configuration_id, true, + fallback_configuration_id, fallback_used); +} + JNIEXPORT jint JNICALL Java_org_apache_arrow_gandiva_evaluator_JniWrapper_evaluateFilter( JNIEnv* env, jobject cls, jlong module_id, jint num_rows, jlongArray buf_addrs, jlongArray buf_sizes, jint jselection_vector_type, jlong out_buf_addr, diff --git a/gandiva/src/main/java/org/apache/arrow/gandiva/evaluator/CacheHit.java b/gandiva/src/main/java/org/apache/arrow/gandiva/evaluator/CacheHit.java new file mode 100644 index 0000000000..040dea167a --- /dev/null +++ b/gandiva/src/main/java/org/apache/arrow/gandiva/evaluator/CacheHit.java @@ -0,0 +1,37 @@ +/* + * 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; + +/** An evaluator constructed from cached object code. */ +public final class CacheHit { + private final T evaluator; + private final boolean fallback; + + CacheHit(T evaluator, boolean fallback) { + this.evaluator = evaluator; + this.fallback = fallback; + } + + public T getEvaluator() { + return evaluator; + } + + /** Whether the fallback configuration supplied this evaluator. */ + public boolean isFallback() { + return fallback; + } +} diff --git a/gandiva/src/main/java/org/apache/arrow/gandiva/evaluator/Filter.java b/gandiva/src/main/java/org/apache/arrow/gandiva/evaluator/Filter.java index 06577a516e..eb98163379 100644 --- a/gandiva/src/main/java/org/apache/arrow/gandiva/evaluator/Filter.java +++ b/gandiva/src/main/java/org/apache/arrow/gandiva/evaluator/Filter.java @@ -18,6 +18,8 @@ import java.util.ArrayList; import java.util.List; +import java.util.Optional; +import org.apache.arrow.gandiva.exceptions.CacheProbeException; import org.apache.arrow.gandiva.exceptions.EvaluatorClosedException; import org.apache.arrow.gandiva.exceptions.GandivaException; import org.apache.arrow.gandiva.expression.ArrowTypeHelper; @@ -113,6 +115,61 @@ public static Filter make(Schema schema, Condition condition, boolean optimize) */ public static Filter make(Schema schema, Condition condition, long configurationId) throws GandivaException { + return makeInternal(schema, condition, configurationId, false); + } + + /** Constructs an evaluator from cached object code, without compiling on a miss. */ + public static Optional tryMakeFromCache( + Schema schema, Condition condition, ConfigurationBuilder.ConfigOptions options) + throws GandivaException { + return Optional.ofNullable( + makeInternal(schema, condition, JniLoader.getConfiguration(options), true)); + } + + /** + * Checks the preferred configuration, then the fallback after a miss, without compiling. + * + * @return the cached evaluator and its configuration, or empty if both probes miss + * @throws CacheProbeException when cache lookup fails + */ + public static Optional> tryMakeFromCacheWithFallback( + Schema schema, + Condition condition, + ConfigurationBuilder.ConfigOptions preferred, + ConfigurationBuilder.ConfigOptions fallback) + throws GandivaException { + boolean[] fallbackUsed = {false}; + try { + Filter evaluator = + makeInternal( + schema, + condition, + JniLoader.getConfiguration(preferred), + true, + JniLoader.getConfiguration(fallback), + fallbackUsed); + return evaluator == null + ? Optional.empty() + : Optional.of(new CacheHit<>(evaluator, fallbackUsed[0])); + } catch (GandivaException failure) { + throw new CacheProbeException(fallbackUsed[0], failure); + } + } + + private static Filter makeInternal( + Schema schema, Condition condition, long configurationId, boolean cacheOnly) + throws GandivaException { + return makeInternal(schema, condition, configurationId, cacheOnly, 0, null); + } + + private static Filter makeInternal( + Schema schema, + Condition condition, + long configurationId, + boolean cacheOnly, + long fallbackConfigurationId, + boolean[] fallbackUsed) + throws GandivaException { // Invoke the JNI layer to create the LLVM module representing the filter. GandivaTypes.Condition conditionBuf = condition.toProtobuf(); GandivaTypes.Schema schemaBuf = ArrowTypeHelper.arrowSchemaToProtobuf(schema); @@ -122,7 +179,20 @@ public static Filter make(Schema schema, Condition condition, long configuration // No lock here, deliberately -- see the equivalent comment in Projector.make(). The // duplicate-LLVM-symbol race from GH-601 is fixed in Gandiva's native Filter::Make(), so this // no longer needs to be serialized. - long moduleId = wrapper.buildFilter(schemaBytes, conditionBytes, configurationId); + long moduleId; + if (fallbackUsed != null) { + moduleId = + wrapper.buildFilterFromCacheWithFallback( + schemaBytes, conditionBytes, configurationId, fallbackConfigurationId, fallbackUsed); + } else { + moduleId = + cacheOnly + ? wrapper.buildFilterFromCache(schemaBytes, conditionBytes, configurationId) + : wrapper.buildFilter(schemaBytes, conditionBytes, configurationId); + } + if (cacheOnly && moduleId == 0) { + return null; + } logger.debug("Created module for the filter with id {}", moduleId); return new Filter(wrapper, moduleId, schema); } diff --git a/gandiva/src/main/java/org/apache/arrow/gandiva/evaluator/JniWrapper.java b/gandiva/src/main/java/org/apache/arrow/gandiva/evaluator/JniWrapper.java index acb25ebae2..37121a7497 100644 --- a/gandiva/src/main/java/org/apache/arrow/gandiva/evaluator/JniWrapper.java +++ b/gandiva/src/main/java/org/apache/arrow/gandiva/evaluator/JniWrapper.java @@ -36,6 +36,21 @@ public class JniWrapper { * @param configId Configuration to gandiva. * @return A moduleId that is passed to the evaluateProjector() and closeProjector() methods */ + /** Returns zero on an object-cache miss without compiling. */ + native long buildProjectorFromCache( + byte[] schemaBuf, byte[] exprListBuf, int selectionVectorType, long configId) + throws GandivaException; + + /** Reports which configuration was last checked, including when the probe fails. */ + native long buildProjectorFromCacheWithFallback( + byte[] schemaBuf, + byte[] exprListBuf, + int selectionVectorType, + long configId, + long fallbackConfigId, + boolean[] fallbackUsed) + throws GandivaException; + native long buildProjector( byte[] schemaBuf, byte[] exprListBuf, int selectionVectorType, long configId) throws GandivaException; @@ -86,6 +101,19 @@ native void evaluateProjector( * @param configId Configuration to gandiva. * @return A moduleId that is passed to the evaluateFilter() and closeFilter() methods */ + /** Returns zero on an object-cache miss without compiling. */ + native long buildFilterFromCache(byte[] schemaBuf, byte[] conditionBuf, long configId) + throws GandivaException; + + /** Reports which configuration was last checked, including when the probe fails. */ + native long buildFilterFromCacheWithFallback( + byte[] schemaBuf, + byte[] conditionBuf, + long configId, + long fallbackConfigId, + boolean[] fallbackUsed) + throws GandivaException; + native long buildFilter(byte[] schemaBuf, byte[] conditionBuf, long configId) throws GandivaException; diff --git a/gandiva/src/main/java/org/apache/arrow/gandiva/evaluator/Projector.java b/gandiva/src/main/java/org/apache/arrow/gandiva/evaluator/Projector.java index 13fc040f34..43b7824b95 100644 --- a/gandiva/src/main/java/org/apache/arrow/gandiva/evaluator/Projector.java +++ b/gandiva/src/main/java/org/apache/arrow/gandiva/evaluator/Projector.java @@ -18,6 +18,8 @@ import java.util.ArrayList; import java.util.List; +import java.util.Optional; +import org.apache.arrow.gandiva.exceptions.CacheProbeException; import org.apache.arrow.gandiva.exceptions.EvaluatorClosedException; import org.apache.arrow.gandiva.exceptions.GandivaException; import org.apache.arrow.gandiva.exceptions.UnsupportedTypeException; @@ -194,6 +196,90 @@ public static Projector make( SelectionVectorType selectionVectorType, long configurationId) throws GandivaException { + return makeInternal(schema, exprs, selectionVectorType, configurationId, false); + } + + /** Constructs an evaluator from cached object code, without compiling on a miss. */ + public static Optional tryMakeFromCache( + Schema schema, List exprs, ConfigurationBuilder.ConfigOptions options) + throws GandivaException { + return tryMakeFromCache(schema, exprs, SelectionVectorType.SV_NONE, options); + } + + /** Constructs a cached evaluator for the specified selection-vector mode. */ + public static Optional tryMakeFromCache( + Schema schema, + List exprs, + SelectionVectorType selectionVectorType, + ConfigurationBuilder.ConfigOptions options) + throws GandivaException { + return Optional.ofNullable( + makeInternal( + schema, exprs, selectionVectorType, JniLoader.getConfiguration(options), true)); + } + + /** Checks preferred and fallback configurations for a projector without a selection vector. */ + public static Optional> tryMakeFromCacheWithFallback( + Schema schema, + List exprs, + ConfigurationBuilder.ConfigOptions preferred, + ConfigurationBuilder.ConfigOptions fallback) + throws GandivaException { + return tryMakeFromCacheWithFallback( + schema, exprs, SelectionVectorType.SV_NONE, preferred, fallback); + } + + /** + * Checks the preferred configuration, then the fallback after a miss, without compiling. + * + * @return the cached evaluator and its configuration, or empty if both probes miss + * @throws CacheProbeException when cache lookup fails + */ + public static Optional> tryMakeFromCacheWithFallback( + Schema schema, + List exprs, + SelectionVectorType selectionVectorType, + ConfigurationBuilder.ConfigOptions preferred, + ConfigurationBuilder.ConfigOptions fallback) + throws GandivaException { + boolean[] fallbackUsed = {false}; + try { + Projector evaluator = + makeInternal( + schema, + exprs, + selectionVectorType, + JniLoader.getConfiguration(preferred), + true, + JniLoader.getConfiguration(fallback), + fallbackUsed); + return evaluator == null + ? Optional.empty() + : Optional.of(new CacheHit<>(evaluator, fallbackUsed[0])); + } catch (GandivaException failure) { + throw new CacheProbeException(fallbackUsed[0], failure); + } + } + + private static Projector makeInternal( + Schema schema, + List exprs, + SelectionVectorType selectionVectorType, + long configurationId, + boolean cacheOnly) + throws GandivaException { + return makeInternal(schema, exprs, selectionVectorType, configurationId, cacheOnly, 0, null); + } + + private static Projector makeInternal( + Schema schema, + List exprs, + SelectionVectorType selectionVectorType, + long configurationId, + boolean cacheOnly, + long fallbackConfigurationId, + boolean[] fallbackUsed) + throws GandivaException { // serialize the schema and the list of expressions as a protobuf GandivaTypes.ExpressionList.Builder builder = GandivaTypes.ExpressionList.newBuilder(); for (ExpressionTree expr : exprs) { @@ -212,9 +298,27 @@ public static Projector make( // 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( - schemaBytes, exprBytes, selectionVectorType.getNumber(), configurationId); + long moduleId; + if (fallbackUsed != null) { + moduleId = + wrapper.buildProjectorFromCacheWithFallback( + schemaBytes, + exprBytes, + selectionVectorType.getNumber(), + configurationId, + fallbackConfigurationId, + fallbackUsed); + } else { + moduleId = + cacheOnly + ? wrapper.buildProjectorFromCache( + schemaBytes, exprBytes, selectionVectorType.getNumber(), configurationId) + : wrapper.buildProjector( + schemaBytes, exprBytes, selectionVectorType.getNumber(), configurationId); + } + if (cacheOnly && moduleId == 0) { + return null; + } logger.debug("Created module for the projector with id {}", moduleId); return new Projector(wrapper, moduleId, schema, exprs.size()); } diff --git a/gandiva/src/main/java/org/apache/arrow/gandiva/exceptions/CacheProbeException.java b/gandiva/src/main/java/org/apache/arrow/gandiva/exceptions/CacheProbeException.java new file mode 100644 index 0000000000..b5eeaba20b --- /dev/null +++ b/gandiva/src/main/java/org/apache/arrow/gandiva/exceptions/CacheProbeException.java @@ -0,0 +1,32 @@ +/* + * 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.exceptions; + +/** Reports which configuration failed during an ordered cache probe. */ +public final class CacheProbeException extends GandivaException { + private final boolean fallback; + + public CacheProbeException(boolean fallback, GandivaException cause) { + super(cause.getMessage(), cause); + this.fallback = fallback; + } + + /** Whether the preferred probe missed and the fallback probe failed. */ + public boolean isFallback() { + return fallback; + } +} diff --git a/gandiva/src/test/java/org/apache/arrow/gandiva/evaluator/CacheOnlyEvaluatorTest.java b/gandiva/src/test/java/org/apache/arrow/gandiva/evaluator/CacheOnlyEvaluatorTest.java new file mode 100644 index 0000000000..27655513ea --- /dev/null +++ b/gandiva/src/test/java/org/apache/arrow/gandiva/evaluator/CacheOnlyEvaluatorTest.java @@ -0,0 +1,118 @@ +/* + * 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 static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.List; +import org.apache.arrow.gandiva.expression.TreeBuilder; +import org.apache.arrow.gandiva.ipc.GandivaTypes.SelectionVectorType; +import org.apache.arrow.vector.types.pojo.ArrowType; +import org.apache.arrow.vector.types.pojo.Field; +import org.apache.arrow.vector.types.pojo.Schema; +import org.junit.jupiter.api.Test; + +class CacheOnlyEvaluatorTest extends BaseEvaluatorTest { + @Test + void projectorMissDoesNotCompileAndOptimizationKeysAreDistinct() throws Exception { + Field input = Field.nullable("java_cache_only_project", new ArrowType.Int(32, true)); + Schema schema = new Schema(List.of(input)); + var expr = + TreeBuilder.makeExpression( + TreeBuilder.makeFunction( + "add", + List.of(TreeBuilder.makeField(input), TreeBuilder.makeField(input)), + input.getType()), + Field.nullable("result", input.getType())); + var o3 = new ConfigurationBuilder.ConfigOptions().withOptimize(true); + var o0 = new ConfigurationBuilder.ConfigOptions().withOptimize(false); + assertTrue(Projector.tryMakeFromCacheWithFallback(schema, List.of(expr), o3, o0).isEmpty()); + assertTrue(Projector.tryMakeFromCache(schema, List.of(expr), o3).isEmpty()); + assertTrue(Projector.tryMakeFromCache(schema, List.of(expr), o3).isEmpty()); + var o0Evaluator = Projector.make(schema, List.of(expr), o0); + try { + assertTrue(Projector.tryMakeFromCache(schema, List.of(expr), o3).isEmpty()); + var fallbackHit = + Projector.tryMakeFromCacheWithFallback(schema, List.of(expr), o3, o0).orElseThrow(); + assertTrue(fallbackHit.isFallback()); + fallbackHit.getEvaluator().close(); + } finally { + o0Evaluator.close(); + } + var o3Evaluator = Projector.make(schema, List.of(expr), o3); + try { + var hit = Projector.tryMakeFromCache(schema, List.of(expr), o3); + assertTrue(hit.isPresent()); + hit.orElseThrow().close(); + var preferredHit = + Projector.tryMakeFromCacheWithFallback(schema, List.of(expr), o3, o0).orElseThrow(); + assertFalse(preferredHit.isFallback()); + preferredHit.getEvaluator().close(); + assertTrue( + Projector.tryMakeFromCache(schema, List.of(expr), SelectionVectorType.SV_INT16, o3) + .isEmpty()); + assertTrue( + Projector.tryMakeFromCache( + schema, + List.of(expr), + new ConfigurationBuilder.ConfigOptions().withOptimize(true).withTargetCPU(false)) + .isEmpty()); + } finally { + o3Evaluator.close(); + } + } + + @Test + void filterMissDoesNotCompileAndOptimizationKeysAreDistinct() throws Exception { + Field input = Field.nullable("java_cache_only_filter", new ArrowType.Int(32, true)); + Schema schema = new Schema(List.of(input)); + var condition = + TreeBuilder.makeCondition( + TreeBuilder.makeFunction( + "less_than", + List.of(TreeBuilder.makeField(input), TreeBuilder.makeLiteral(7)), + new ArrowType.Bool())); + var o3 = new ConfigurationBuilder.ConfigOptions().withOptimize(true); + var o0 = new ConfigurationBuilder.ConfigOptions().withOptimize(false); + assertTrue(Filter.tryMakeFromCacheWithFallback(schema, condition, o3, o0).isEmpty()); + assertTrue(Filter.tryMakeFromCache(schema, condition, o3).isEmpty()); + assertTrue(Filter.tryMakeFromCache(schema, condition, o3).isEmpty()); + var o0Evaluator = Filter.make(schema, condition, o0); + try { + assertTrue(Filter.tryMakeFromCache(schema, condition, o3).isEmpty()); + var fallbackHit = + Filter.tryMakeFromCacheWithFallback(schema, condition, o3, o0).orElseThrow(); + assertTrue(fallbackHit.isFallback()); + fallbackHit.getEvaluator().close(); + } finally { + o0Evaluator.close(); + } + var o3Evaluator = Filter.make(schema, condition, o3); + try { + var hit = Filter.tryMakeFromCache(schema, condition, o3); + assertTrue(hit.isPresent()); + hit.orElseThrow().close(); + var preferredHit = + Filter.tryMakeFromCacheWithFallback(schema, condition, o3, o0).orElseThrow(); + assertFalse(preferredHit.isFallback()); + preferredHit.getEvaluator().close(); + } finally { + o3Evaluator.close(); + } + } +}