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
18 changes: 18 additions & 0 deletions cpp/src/gandiva/filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ Filter::~Filter() {}
Status Filter::Make(SchemaPtr schema, ConditionPtr condition,
std::shared_ptr<Configuration> configuration,
std::shared_ptr<Filter>* filter) {
return MakeInternal(std::move(schema), std::move(condition), std::move(configuration),
false, filter);
}

Status Filter::TryMakeFromCache(SchemaPtr schema, ConditionPtr condition,
std::shared_ptr<Configuration> configuration,
std::shared_ptr<Filter>* filter) {
return MakeInternal(std::move(schema), std::move(condition), std::move(configuration),
true, filter);
}

Status Filter::MakeInternal(SchemaPtr schema, ConditionPtr condition,
std::shared_ptr<Configuration> configuration, bool cache_only,
std::shared_ptr<Filter>* filter) {
ARROW_RETURN_IF(schema == nullptr, Status::Invalid("Schema cannot be null"));
ARROW_RETURN_IF(condition == nullptr, Status::Invalid("Condition cannot be null"));
ARROW_RETURN_IF(configuration == nullptr,
Expand All @@ -56,6 +70,10 @@ Status Filter::Make(SchemaPtr schema, ConditionPtr condition,

std::shared_ptr<llvm::MemoryBuffer> prev_cached_obj;
prev_cached_obj = cache->GetObjectCode(cache_key);
if (cache_only && prev_cached_obj == nullptr) {
filter->reset();
return Status::OK();
}

// Verify if previous filter obj code was cached
if (prev_cached_obj != nullptr) {
Expand Down
9 changes: 9 additions & 0 deletions cpp/src/gandiva/filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ class GANDIVA_EXPORT Filter {
std::shared_ptr<Configuration> config,
std::shared_ptr<Filter>* filter);

/// Build only from cached object code. A miss returns OK and a null filter.
static Status TryMakeFromCache(SchemaPtr schema, ConditionPtr condition,
std::shared_ptr<Configuration> configuration,
std::shared_ptr<Filter>* filter);

/// Evaluate the specified record batch, and populate output selection vector.
///
/// \param[in] batch the record batch. schema should be the same as the one in 'Make'
Expand All @@ -83,6 +88,10 @@ class GANDIVA_EXPORT Filter {
bool GetBuiltFromCache();

private:
static Status MakeInternal(SchemaPtr schema, ConditionPtr condition,
std::shared_ptr<Configuration> configuration,
bool cache_only, std::shared_ptr<Filter>* filter);

std::unique_ptr<LLVMGenerator> llvm_generator_;
SchemaPtr schema_;
std::shared_ptr<Configuration> configuration_;
Expand Down
20 changes: 20 additions & 0 deletions cpp/src/gandiva/projector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ Status Projector::Make(SchemaPtr schema, const ExpressionVector& exprs,
SelectionVector::Mode selection_vector_mode,
std::shared_ptr<Configuration> configuration,
std::shared_ptr<Projector>* projector) {
return MakeInternal(std::move(schema), exprs, selection_vector_mode,
std::move(configuration), false, projector);
}

Status Projector::TryMakeFromCache(SchemaPtr schema, const ExpressionVector& exprs,
SelectionVector::Mode selection_vector_mode,
std::shared_ptr<Configuration> configuration,
std::shared_ptr<Projector>* projector) {
return MakeInternal(std::move(schema), exprs, selection_vector_mode,
std::move(configuration), true, projector);
}

Status Projector::MakeInternal(SchemaPtr schema, const ExpressionVector& exprs,
SelectionVector::Mode selection_vector_mode,
std::shared_ptr<Configuration> configuration,
bool cache_only, std::shared_ptr<Projector>* projector) {
ARROW_RETURN_IF(schema == nullptr, Status::Invalid("Schema cannot be null"));
ARROW_RETURN_IF(exprs.empty(), Status::Invalid("Expressions cannot be empty"));
ARROW_RETURN_IF(configuration == nullptr,
Expand All @@ -71,6 +87,10 @@ Status Projector::Make(SchemaPtr schema, const ExpressionVector& exprs,

std::shared_ptr<llvm::MemoryBuffer> prev_cached_obj;
prev_cached_obj = cache->GetObjectCode(cache_key);
if (cache_only && prev_cached_obj == nullptr) {
projector->reset();
return Status::OK();
}

// Verify if previous projector obj code was cached
if (prev_cached_obj != nullptr) {
Expand Down
11 changes: 11 additions & 0 deletions cpp/src/gandiva/projector.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ class GANDIVA_EXPORT Projector {
std::shared_ptr<Configuration> configuration,
std::shared_ptr<Projector>* projector);

/// Build only from cached object code. A miss returns OK and a null projector.
static Status TryMakeFromCache(SchemaPtr schema, const ExpressionVector& exprs,
SelectionVector::Mode selection_vector_mode,
std::shared_ptr<Configuration> configuration,
std::shared_ptr<Projector>* projector);

/// Evaluate the specified record batch, and return the allocated and populated output
/// arrays. The output arrays will be allocated from the memory pool 'pool', and added
/// to the vector 'output'.
Expand Down Expand Up @@ -125,6 +131,11 @@ class GANDIVA_EXPORT Projector {
bool GetBuiltFromCache();

private:
static Status MakeInternal(SchemaPtr schema, const ExpressionVector& exprs,
SelectionVector::Mode selection_vector_mode,
std::shared_ptr<Configuration> configuration,
bool cache_only, std::shared_ptr<Projector>* projector);

Projector(std::unique_ptr<LLVMGenerator> llvm_generator, SchemaPtr schema,
const FieldVector& output_fields, std::shared_ptr<Configuration>);

Expand Down
31 changes: 31 additions & 0 deletions cpp/src/gandiva/tests/filter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,37 @@ class TestFilter : public ::testing::Test {
arrow::MemoryPool* pool_;
};

TEST_F(TestFilter, CacheOnlyConstruction) {
auto input = field("cache_only_filter_input", int32());
auto schema = arrow::schema({input});
auto node = TreeExprBuilder::MakeFunction(
"less_than", {TreeExprBuilder::MakeField(input), TreeExprBuilder::MakeLiteral(2)},
arrow::boolean());
auto condition = TreeExprBuilder::MakeCondition(node);
auto optimized = ConfigurationBuilder().build(true);
auto unoptimized = ConfigurationBuilder().build(false);
std::shared_ptr<Filter> cached;
ASSERT_OK(Filter::TryMakeFromCache(schema, condition, optimized, &cached));
ASSERT_EQ(cached, nullptr);
std::shared_ptr<Filter> first;
ASSERT_OK(Filter::Make(schema, condition, unoptimized, &first));
ASSERT_FALSE(first->GetBuiltFromCache());
ASSERT_OK(Filter::TryMakeFromCache(schema, condition, optimized, &cached));
ASSERT_EQ(cached, nullptr);
ASSERT_OK(Filter::Make(schema, condition, optimized, &first));
ASSERT_FALSE(first->GetBuiltFromCache());
ASSERT_OK(Filter::TryMakeFromCache(schema, condition, optimized, &cached));
ASSERT_NE(cached, nullptr);
EXPECT_TRUE(cached->GetBuiltFromCache());
auto batch = arrow::RecordBatch::Make(
schema, 3, {MakeArrowArrayInt32({1, 2, 3}, {true, true, true})});
std::shared_ptr<SelectionVector> selection;
ASSERT_OK(SelectionVector::MakeInt16(3, pool_, &selection));
ASSERT_OK(cached->Evaluate(*batch, selection));
ASSERT_EQ(selection->GetNumSlots(), 1);
EXPECT_EQ(selection->GetIndex(0), 0);
}

TEST_F(TestFilter, TestFilterCache) {
// schema for input fields
auto field0 = field("f0_filter_cache", int32());
Expand Down
34 changes: 34 additions & 0 deletions cpp/src/gandiva/tests/projector_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,40 @@ class TestProjector : public ::testing::Test {
arrow::MemoryPool* pool_;
};

TEST_F(TestProjector, CacheOnlyConstruction) {
auto input = field("cache_only_project_input", int32());
auto schema = arrow::schema({input});
auto expr =
TreeExprBuilder::MakeExpression("add", {input, input}, field("result", int32()));
auto optimized = ConfigurationBuilder().build(true);
auto unoptimized = ConfigurationBuilder().build(false);
std::shared_ptr<Projector> cached;
ASSERT_OK(Projector::TryMakeFromCache(schema, {expr}, SelectionVector::MODE_NONE,
optimized, &cached));
ASSERT_EQ(cached, nullptr);
std::shared_ptr<Projector> first;
ASSERT_OK(Projector::Make(schema, {expr}, unoptimized, &first));
ASSERT_FALSE(first->GetBuiltFromCache());
ASSERT_OK(Projector::TryMakeFromCache(schema, {expr}, SelectionVector::MODE_NONE,
optimized, &cached));
ASSERT_EQ(cached, nullptr);
ASSERT_OK(Projector::Make(schema, {expr}, optimized, &first));
ASSERT_FALSE(first->GetBuiltFromCache());
ASSERT_OK(Projector::TryMakeFromCache(schema, {expr}, SelectionVector::MODE_NONE,
optimized, &cached));
ASSERT_NE(cached, nullptr);
EXPECT_TRUE(cached->GetBuiltFromCache());
auto batch = arrow::RecordBatch::Make(
schema, 3, {MakeArrowArrayInt32({1, 2, 3}, {true, true, true})});
arrow::ArrayVector output;
ASSERT_OK(cached->Evaluate(*batch, pool_, &output));
EXPECT_ARROW_ARRAY_EQUALS(MakeArrowArrayInt32({2, 4, 6}, {true, true, true}),
output[0]);
ASSERT_OK(Projector::TryMakeFromCache(schema, {expr}, SelectionVector::MODE_UINT16,
optimized, &cached));
EXPECT_EQ(cached, nullptr);
}

TEST_F(TestProjector, TestProjectCache) {
// schema for input fields
auto field0 = field("f0", int32());
Expand Down
Loading