Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions gandiva/src/main/cpp/id_to_module_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::mutex> guard(mtx_);
removed = map_.extract(module_id);
}
}

HOLDER Lookup(jlong module_id) {
Expand Down
105 changes: 97 additions & 8 deletions gandiva/src/main/cpp/jni_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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> projector;
std::shared_ptr<ProjectorHolder> holder;
Expand Down Expand Up @@ -677,14 +678,35 @@ 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";
releaseProjectorInput(schema_arr, schema_bytes, exprs_arr, exprs_bytes, env);
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<ProjectorHolder>(
new ProjectorHolder(schema_ptr, ret_types, std::move(projector)));
Expand All @@ -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.
///
Expand Down Expand Up @@ -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> filter;
std::shared_ptr<FilterHolder> holder;
Expand Down Expand Up @@ -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<FilterHolder>(new FilterHolder(schema_ptr, std::move(filter)));
module_id = filter_modules_.Insert(holder);
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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<T> {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Filter> 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<CacheHit<Filter>> 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);
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
Loading
Loading