diff --git a/cpp/src/gandiva/cache_test.cc b/cpp/src/gandiva/cache_test.cc index d371db59dfc..ab60293094b 100644 --- a/cpp/src/gandiva/cache_test.cc +++ b/cpp/src/gandiva/cache_test.cc @@ -22,6 +22,10 @@ #include +#include "gandiva/configuration.h" +#include "gandiva/expression_cache_key.h" +#include "gandiva/tree_expr_builder.h" + namespace gandiva { class TestCacheKey { @@ -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(true); + auto cfg2 = std::make_shared(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(false); // optimize = false + ExpressionCacheKey k3(schema, cfg3, exprs, mode); + ASSERT_FALSE(k1 == k3); +} + } // namespace gandiva diff --git a/cpp/src/gandiva/expression_cache_key.h b/cpp/src/gandiva/expression_cache_key.h index e7522042a7d..1b972d211a4 100644 --- a/cpp/src/gandiva/expression_cache_key.h +++ b/cpp/src/gandiva/expression_cache_key.h @@ -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_) {