From 91c9db32ba57f5ba1e4335a323c3abdf482d1496 Mon Sep 17 00:00:00 2001 From: Logan Date: Wed, 11 Nov 2026 15:04:31 -0800 Subject: [PATCH] GH: Gandiva: compare Configuration by value in the compiled-expression cache key ExpressionCacheKey::Hash() folds in configuration->Hash() by content, but operator== compared the configuration shared_ptr by identity. Two distinct Configuration instances with identical content therefore hashed equal yet compared unequal, so the compiled-expression cache missed and Gandiva recompiled an expression it had already built. Performance bug only (never a wrong result); impact scales with how often callers build fresh Configuration objects rather than sharing one. Compare *configuration_ by value (Configuration::operator== already exists), with a null-safe guard. Adds TestExpressionCacheKeyConfigByValue. --- cpp/src/gandiva/cache_test.cc | 33 ++++++++++++++++++++++++++ cpp/src/gandiva/expression_cache_key.h | 9 ++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/cpp/src/gandiva/cache_test.cc b/cpp/src/gandiva/cache_test.cc index d371db59dfc2..ab60293094b2 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 e7522042a7d4..1b972d211a47 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_) {