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
58 changes: 50 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,9 @@ 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 module_id = 0LL;
std::shared_ptr<Projector> projector;
std::shared_ptr<ProjectorHolder> holder;
Expand Down Expand Up @@ -677,14 +677,22 @@ 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);
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 +705,21 @@ 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);
}

///
/// \brief Resizable buffer which resizes by doing a callback into java.
///
Expand Down Expand Up @@ -902,9 +925,8 @@ 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 module_id = 0LL;
std::shared_ptr<Filter> filter;
std::shared_ptr<FilterHolder> holder;
Expand Down Expand Up @@ -959,13 +981,20 @@ 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);
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 +1006,19 @@ 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 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
Expand Up @@ -18,6 +18,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
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 +114,20 @@ 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));
}

private static Filter makeInternal(
Schema schema, Condition condition, long configurationId, boolean cacheOnly)
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 +137,13 @@ 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 =
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,11 @@ 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;

native long buildProjector(
byte[] schemaBuf, byte[] exprListBuf, int selectionVectorType, long configId)
throws GandivaException;
Expand Down Expand Up @@ -86,6 +91,10 @@ 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;

native long buildFilter(byte[] schemaBuf, byte[] conditionBuf, long configId)
throws GandivaException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.apache.arrow.gandiva.exceptions.EvaluatorClosedException;
import org.apache.arrow.gandiva.exceptions.GandivaException;
import org.apache.arrow.gandiva.exceptions.UnsupportedTypeException;
Expand Down Expand Up @@ -194,6 +195,35 @@ 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<Projector> tryMakeFromCache(
Schema schema, List<ExpressionTree> 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<Projector> tryMakeFromCache(
Schema schema,
List<ExpressionTree> exprs,
SelectionVectorType selectionVectorType,
ConfigurationBuilder.ConfigOptions options)
throws GandivaException {
return Optional.ofNullable(
makeInternal(
schema, exprs, selectionVectorType, JniLoader.getConfiguration(options), true));
}

private static Projector makeInternal(
Schema schema,
List<ExpressionTree> exprs,
SelectionVectorType selectionVectorType,
long configurationId,
boolean cacheOnly)
throws GandivaException {
// serialize the schema and the list of expressions as a protobuf
GandivaTypes.ExpressionList.Builder builder = GandivaTypes.ExpressionList.newBuilder();
for (ExpressionTree expr : exprs) {
Expand All @@ -213,8 +243,14 @@ public static Projector make(
// 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);
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());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* 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.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.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());
} 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();
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.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());
} finally {
o0Evaluator.close();
}
var o3Evaluator = Filter.make(schema, condition, o3);
try {
var hit = Filter.tryMakeFromCache(schema, condition, o3);
assertTrue(hit.isPresent());
hit.orElseThrow().close();
} finally {
o3Evaluator.close();
}
}
}
Loading