Skip to content
Open
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
33 changes: 33 additions & 0 deletions cpp/src/gandiva/cache_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@

#include <gtest/gtest.h>

#include "gandiva/configuration.h"
#include "gandiva/expression_cache_key.h"
#include "gandiva/tree_expr_builder.h"

namespace gandiva {

class TestCacheKey {
Expand Down Expand Up @@ -107,4 +111,33 @@ TEST(TestCache, TestGetCacheCapacityEnvVar) {
}
}

// Two distinct Configuration instances with identical content must produce equal
// ExpressionCacheKeys, so the compiled-expression cache hits instead of recompiling.
// Regression test for the key comparing the configuration shared_ptr by identity.
TEST(TestCache, TestExpressionCacheKeyConfigByValue) {
auto f0 = arrow::field("f0", arrow::int32());
auto f1 = arrow::field("f1", arrow::int32());
auto schema = arrow::schema({f0, f1});
auto expr =
TreeExprBuilder::MakeExpression("add", {f0, f1}, arrow::field("out", arrow::int32()));
ExpressionVector exprs = {expr};
const auto mode = SelectionVector::Mode::MODE_NONE;

// Distinct instances, identical content.
auto cfg1 = std::make_shared<Configuration>(true);
auto cfg2 = std::make_shared<Configuration>(true);
ASSERT_NE(cfg1, cfg2); // different pointers
ASSERT_EQ(*cfg1, *cfg2); // equal content

ExpressionCacheKey k1(schema, cfg1, exprs, mode);
ExpressionCacheKey k2(schema, cfg2, exprs, mode);
ASSERT_EQ(k1.Hash(), k2.Hash());
ASSERT_TRUE(k1 == k2); // fails if configuration is compared by identity

// Different configuration content must still produce a different key.
auto cfg3 = std::make_shared<Configuration>(false); // optimize = false
ExpressionCacheKey k3(schema, cfg3, exprs, mode);
ASSERT_FALSE(k1 == k3);
}

} // namespace gandiva
9 changes: 8 additions & 1 deletion cpp/src/gandiva/expression_cache_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,15 @@ class ExpressionCacheKey {
return false;
}

// Compare the configuration by value, not by shared_ptr identity. Hash() already
// folds in configuration_->Hash() by content, so two distinct Configuration
// instances with identical content must also compare equal here; comparing the
// pointers instead caused spurious cache misses and needless recompiles.
if (configuration_ != other.configuration_) {
return false;
if (configuration_ == nullptr || other.configuration_ == nullptr ||
!(*configuration_ == *other.configuration_)) {
return false;
}
}

if (mode_ != other.mode_) {
Expand Down
Loading