From 4976f06a23aeb8171c8bd721ad725aa211de86d5 Mon Sep 17 00:00:00 2001 From: Steven Phillips Date: Thu, 12 Nov 2026 15:32:42 -0800 Subject: [PATCH] Add cache-only Gandiva projector and filter construction --- cpp/src/gandiva/filter.cc | 18 +++++++++++++ cpp/src/gandiva/filter.h | 9 +++++++ cpp/src/gandiva/projector.cc | 20 +++++++++++++++ cpp/src/gandiva/projector.h | 11 ++++++++ cpp/src/gandiva/tests/filter_test.cc | 31 ++++++++++++++++++++++ cpp/src/gandiva/tests/projector_test.cc | 34 +++++++++++++++++++++++++ 6 files changed, 123 insertions(+) diff --git a/cpp/src/gandiva/filter.cc b/cpp/src/gandiva/filter.cc index be67c12254f9..b959cad66221 100644 --- a/cpp/src/gandiva/filter.cc +++ b/cpp/src/gandiva/filter.cc @@ -40,6 +40,20 @@ Filter::~Filter() {} Status Filter::Make(SchemaPtr schema, ConditionPtr condition, std::shared_ptr configuration, std::shared_ptr* 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, + std::shared_ptr* 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, bool cache_only, + std::shared_ptr* 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, @@ -56,6 +70,10 @@ Status Filter::Make(SchemaPtr schema, ConditionPtr condition, std::shared_ptr 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) { diff --git a/cpp/src/gandiva/filter.h b/cpp/src/gandiva/filter.h index b4043d93c857..39cb419569fb 100644 --- a/cpp/src/gandiva/filter.h +++ b/cpp/src/gandiva/filter.h @@ -68,6 +68,11 @@ class GANDIVA_EXPORT Filter { std::shared_ptr config, std::shared_ptr* 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, + std::shared_ptr* 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' @@ -83,6 +88,10 @@ class GANDIVA_EXPORT Filter { bool GetBuiltFromCache(); private: + static Status MakeInternal(SchemaPtr schema, ConditionPtr condition, + std::shared_ptr configuration, + bool cache_only, std::shared_ptr* filter); + std::unique_ptr llvm_generator_; SchemaPtr schema_; std::shared_ptr configuration_; diff --git a/cpp/src/gandiva/projector.cc b/cpp/src/gandiva/projector.cc index eb52477ff6d6..30aa163bf96d 100644 --- a/cpp/src/gandiva/projector.cc +++ b/cpp/src/gandiva/projector.cc @@ -56,6 +56,22 @@ Status Projector::Make(SchemaPtr schema, const ExpressionVector& exprs, SelectionVector::Mode selection_vector_mode, std::shared_ptr configuration, std::shared_ptr* 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, + std::shared_ptr* 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, + bool cache_only, std::shared_ptr* 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, @@ -71,6 +87,10 @@ Status Projector::Make(SchemaPtr schema, const ExpressionVector& exprs, std::shared_ptr 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) { diff --git a/cpp/src/gandiva/projector.h b/cpp/src/gandiva/projector.h index f1ae7e4dc8cc..1a7a76a587c2 100644 --- a/cpp/src/gandiva/projector.h +++ b/cpp/src/gandiva/projector.h @@ -77,6 +77,12 @@ class GANDIVA_EXPORT Projector { std::shared_ptr configuration, std::shared_ptr* 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, + std::shared_ptr* 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'. @@ -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, + bool cache_only, std::shared_ptr* projector); + Projector(std::unique_ptr llvm_generator, SchemaPtr schema, const FieldVector& output_fields, std::shared_ptr); diff --git a/cpp/src/gandiva/tests/filter_test.cc b/cpp/src/gandiva/tests/filter_test.cc index 749000aa0cf2..7d7aaf8755e5 100644 --- a/cpp/src/gandiva/tests/filter_test.cc +++ b/cpp/src/gandiva/tests/filter_test.cc @@ -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 cached; + ASSERT_OK(Filter::TryMakeFromCache(schema, condition, optimized, &cached)); + ASSERT_EQ(cached, nullptr); + std::shared_ptr 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 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()); diff --git a/cpp/src/gandiva/tests/projector_test.cc b/cpp/src/gandiva/tests/projector_test.cc index 557e7807c596..10ec03dc98cd 100644 --- a/cpp/src/gandiva/tests/projector_test.cc +++ b/cpp/src/gandiva/tests/projector_test.cc @@ -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 cached; + ASSERT_OK(Projector::TryMakeFromCache(schema, {expr}, SelectionVector::MODE_NONE, + optimized, &cached)); + ASSERT_EQ(cached, nullptr); + std::shared_ptr 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());