Copilot commented on code in PR #62355:
URL: https://github.com/apache/doris/pull/62355#discussion_r3063974135


##########
be/test/exec/runtime_filter/vruntimefilter_wrapper_sampling_test.cpp:
##########
@@ -0,0 +1,177 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include <glog/logging.h>
+#include <gtest/gtest.h>
+
+#include "exec/runtime_filter/runtime_filter_selectivity.h"
+#include "exec/runtime_filter/runtime_filter_test_utils.h"
+#include "exprs/vexpr_context.h"
+#include "exprs/vruntimefilter_wrapper.h"
+
+namespace doris {
+
+// Minimal VExpr implementation for testing VRuntimeFilterWrapper in isolation.
+class StubVExpr : public VExpr {
+public:
+    StubVExpr() : VExpr(make_texpr_node()) {}
+
+    const std::string& expr_name() const override {
+        static const std::string name = "StubVExpr";
+        return name;
+    }
+
+    Status execute(VExprContext*, Block*, int*) const override { return 
Status::OK(); }
+
+    Status execute_column(VExprContext*, const Block*, Selector*, size_t,
+                          ColumnPtr&) const override {
+        return Status::OK();
+    }
+
+private:
+    static TExprNode make_texpr_node() {
+        return TExprNodeBuilder(
+                       TExprNodeType::SLOT_REF,
+                       TTypeDescBuilder()
+                               .set_types(TTypeNodeBuilder()
+                                                  
.set_type(TTypeNodeType::SCALAR)
+                                                  
.set_scalar_type(TPrimitiveType::INT)
+                                                  .build())
+                               .build(),
+                       0)
+                .build();
+    }
+};
+
+class VRuntimeFilterWrapperSamplingTest : public RuntimeFilterTest {};
+
+// Test that VRuntimeFilterWrapper stores and propagates sampling_frequency
+// through open() to VExprContext. This is the core fix for the bug where
+// sampling_frequency was lost when _append_rf_into_conjuncts creates a new
+// VExprContext via VExprContext::create_shared(expr).
+TEST_F(VRuntimeFilterWrapperSamplingTest, open_propagates_sampling_frequency) {
+    auto stub = std::make_shared<StubVExpr>();
+    auto node = TExprNodeBuilder(
+                        TExprNodeType::SLOT_REF,
+                        TTypeDescBuilder()
+                                .set_types(TTypeNodeBuilder()
+                                                   
.set_type(TTypeNodeType::SCALAR)
+                                                   
.set_scalar_type(TPrimitiveType::INT)
+                                                   .build())
+                                .build(),
+                        0)
+                        .build();
+
+    const int expected_frequency = 32;
+    auto wrapper = VRuntimeFilterWrapper::create_shared(node, stub, 0.4, 
false, /*filter_id=*/1,
+                                                        expected_frequency);
+
+    // Simulate the VExprContext recreation that happens in 
_append_rf_into_conjuncts.
+    // A fresh VExprContext has default sampling_frequency = DISABLE_SAMPLING 
(-1).
+    auto context = std::make_shared<VExprContext>(wrapper);
+    
ASSERT_EQ(context->get_runtime_filter_selectivity().maybe_always_true_can_ignore(),
 false);
+
+    RowDescriptor row_desc;
+    ASSERT_TRUE(wrapper->prepare(_runtime_states[0].get(), row_desc, 
context.get()).ok());
+    ASSERT_TRUE(wrapper->open(_runtime_states[0].get(), context.get(),
+                              FunctionContext::FRAGMENT_LOCAL)
+                        .ok());
+
+    // After open(), sampling_frequency should be propagated from 
VRuntimeFilterWrapper
+    // to VExprContext. Verify by accumulating low-selectivity data and 
checking
+    // that always_true can now be detected.
+    auto& selectivity = context->get_runtime_filter_selectivity();
+    selectivity.update_judge_selectivity(1, 2000, 50000, 0.1);
+    EXPECT_TRUE(selectivity.maybe_always_true_can_ignore());
+}
+
+// Test that default sampling_frequency (DISABLE_SAMPLING) disables the 
always_true
+// optimization, matching the behavior when disable_always_true_logic is set.
+TEST_F(VRuntimeFilterWrapperSamplingTest, 
default_sampling_frequency_disables_optimization) {
+    auto stub = std::make_shared<StubVExpr>();
+    auto node = TExprNodeBuilder(
+                        TExprNodeType::SLOT_REF,
+                        TTypeDescBuilder()
+                                .set_types(TTypeNodeBuilder()
+                                                   
.set_type(TTypeNodeType::SCALAR)
+                                                   
.set_scalar_type(TPrimitiveType::INT)
+                                                   .build())
+                                .build(),
+                        0)
+                        .build();
+
+    // No sampling_frequency argument - uses default DISABLE_SAMPLING
+    auto wrapper = VRuntimeFilterWrapper::create_shared(node, stub, 0.4, 
false, /*filter_id=*/1);
+
+    auto context = std::make_shared<VExprContext>(wrapper);
+    RowDescriptor row_desc;
+    ASSERT_TRUE(wrapper->prepare(_runtime_states[0].get(), row_desc, 
context.get()).ok());
+    ASSERT_TRUE(wrapper->open(_runtime_states[0].get(), context.get(),
+                              FunctionContext::FRAGMENT_LOCAL)
+                        .ok());
+
+    // Even with low-selectivity data, always_true should NOT be detected
+    // because sampling is disabled
+    auto& selectivity = context->get_runtime_filter_selectivity();
+    selectivity.update_judge_selectivity(1, 2000, 50000, 0.1);
+    EXPECT_FALSE(selectivity.maybe_always_true_can_ignore());
+}
+
+// Test that sampling_frequency survives VExprContext recreation, which is the
+// exact scenario that caused the original bug.
+TEST_F(VRuntimeFilterWrapperSamplingTest, 
sampling_frequency_survives_context_recreation) {
+    auto stub = std::make_shared<StubVExpr>();
+    auto node = TExprNodeBuilder(
+                        TExprNodeType::SLOT_REF,
+                        TTypeDescBuilder()
+                                .set_types(TTypeNodeBuilder()
+                                                   
.set_type(TTypeNodeType::SCALAR)
+                                                   
.set_scalar_type(TPrimitiveType::INT)
+                                                   .build())
+                                .build(),
+                        0)
+                        .build();
+
+    const int expected_frequency = 32;
+    auto wrapper = VRuntimeFilterWrapper::create_shared(node, stub, 0.4, 
false, /*filter_id=*/1,
+                                                        expected_frequency);
+
+    // First context - prepare and open work
+    auto context1 = std::make_shared<VExprContext>(wrapper);
+    RowDescriptor row_desc;
+    ASSERT_TRUE(wrapper->prepare(_runtime_states[0].get(), row_desc, 
context1.get()).ok());
+    ASSERT_TRUE(wrapper->open(_runtime_states[0].get(), context1.get(),
+                              FunctionContext::FRAGMENT_LOCAL)
+                        .ok());
+
+    // Simulate context recreation (what _append_rf_into_conjuncts does):
+    // Create a brand new VExprContext with the same VRuntimeFilterWrapper.
+    // The new context starts with default sampling_frequency = -1.
+    auto context2 = std::make_shared<VExprContext>(wrapper);
+    
EXPECT_FALSE(context2->get_runtime_filter_selectivity().maybe_always_true_can_ignore());
+
+    // After open() on the new context, sampling_frequency should be propagated
+    ASSERT_TRUE(wrapper->open(_runtime_states[0].get(), context2.get(),
+                              FunctionContext::THREAD_LOCAL)

Review Comment:
   The test calls wrapper->open(..., FunctionContext::THREAD_LOCAL) for a 
freshly created VExprContext. However, _append_rf_into_conjuncts creates a 
non-clone context and VExprContext::open() will use FRAGMENT_LOCAL scope in 
that case. Using THREAD_LOCAL here doesn’t reflect the real scenario being 
regressed and could miss FRAGMENT_LOCAL-only behavior (e.g., const-column 
setup).
   



##########
be/test/exec/runtime_filter/vruntimefilter_wrapper_sampling_test.cpp:
##########
@@ -0,0 +1,177 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include <glog/logging.h>
+#include <gtest/gtest.h>
+
+#include "exec/runtime_filter/runtime_filter_selectivity.h"
+#include "exec/runtime_filter/runtime_filter_test_utils.h"
+#include "exprs/vexpr_context.h"
+#include "exprs/vruntimefilter_wrapper.h"
+
+namespace doris {
+
+// Minimal VExpr implementation for testing VRuntimeFilterWrapper in isolation.
+class StubVExpr : public VExpr {
+public:
+    StubVExpr() : VExpr(make_texpr_node()) {}
+
+    const std::string& expr_name() const override {
+        static const std::string name = "StubVExpr";
+        return name;
+    }
+
+    Status execute(VExprContext*, Block*, int*) const override { return 
Status::OK(); }
+
+    Status execute_column(VExprContext*, const Block*, Selector*, size_t,
+                          ColumnPtr&) const override {
+        return Status::OK();
+    }
+
+private:
+    static TExprNode make_texpr_node() {
+        return TExprNodeBuilder(
+                       TExprNodeType::SLOT_REF,
+                       TTypeDescBuilder()
+                               .set_types(TTypeNodeBuilder()
+                                                  
.set_type(TTypeNodeType::SCALAR)
+                                                  
.set_scalar_type(TPrimitiveType::INT)
+                                                  .build())
+                               .build(),
+                       0)
+                .build();
+    }
+};
+
+class VRuntimeFilterWrapperSamplingTest : public RuntimeFilterTest {};
+
+// Test that VRuntimeFilterWrapper stores and propagates sampling_frequency
+// through open() to VExprContext. This is the core fix for the bug where
+// sampling_frequency was lost when _append_rf_into_conjuncts creates a new
+// VExprContext via VExprContext::create_shared(expr).
+TEST_F(VRuntimeFilterWrapperSamplingTest, open_propagates_sampling_frequency) {
+    auto stub = std::make_shared<StubVExpr>();
+    auto node = TExprNodeBuilder(
+                        TExprNodeType::SLOT_REF,
+                        TTypeDescBuilder()
+                                .set_types(TTypeNodeBuilder()
+                                                   
.set_type(TTypeNodeType::SCALAR)
+                                                   
.set_scalar_type(TPrimitiveType::INT)
+                                                   .build())
+                                .build(),
+                        0)
+                        .build();
+
+    const int expected_frequency = 32;
+    auto wrapper = VRuntimeFilterWrapper::create_shared(node, stub, 0.4, 
false, /*filter_id=*/1,
+                                                        expected_frequency);
+
+    // Simulate the VExprContext recreation that happens in 
_append_rf_into_conjuncts.
+    // A fresh VExprContext has default sampling_frequency = DISABLE_SAMPLING 
(-1).
+    auto context = std::make_shared<VExprContext>(wrapper);
+    
ASSERT_EQ(context->get_runtime_filter_selectivity().maybe_always_true_can_ignore(),
 false);
+
+    RowDescriptor row_desc;
+    ASSERT_TRUE(wrapper->prepare(_runtime_states[0].get(), row_desc, 
context.get()).ok());
+    ASSERT_TRUE(wrapper->open(_runtime_states[0].get(), context.get(),
+                              FunctionContext::FRAGMENT_LOCAL)
+                        .ok());
+
+    // After open(), sampling_frequency should be propagated from 
VRuntimeFilterWrapper
+    // to VExprContext. Verify by accumulating low-selectivity data and 
checking
+    // that always_true can now be detected.
+    auto& selectivity = context->get_runtime_filter_selectivity();
+    selectivity.update_judge_selectivity(1, 2000, 50000, 0.1);
+    EXPECT_TRUE(selectivity.maybe_always_true_can_ignore());
+}
+
+// Test that default sampling_frequency (DISABLE_SAMPLING) disables the 
always_true
+// optimization, matching the behavior when disable_always_true_logic is set.
+TEST_F(VRuntimeFilterWrapperSamplingTest, 
default_sampling_frequency_disables_optimization) {
+    auto stub = std::make_shared<StubVExpr>();
+    auto node = TExprNodeBuilder(
+                        TExprNodeType::SLOT_REF,
+                        TTypeDescBuilder()
+                                .set_types(TTypeNodeBuilder()
+                                                   
.set_type(TTypeNodeType::SCALAR)
+                                                   
.set_scalar_type(TPrimitiveType::INT)
+                                                   .build())
+                                .build(),
+                        0)
+                        .build();
+
+    // No sampling_frequency argument - uses default DISABLE_SAMPLING
+    auto wrapper = VRuntimeFilterWrapper::create_shared(node, stub, 0.4, 
false, /*filter_id=*/1);
+
+    auto context = std::make_shared<VExprContext>(wrapper);
+    RowDescriptor row_desc;
+    ASSERT_TRUE(wrapper->prepare(_runtime_states[0].get(), row_desc, 
context.get()).ok());
+    ASSERT_TRUE(wrapper->open(_runtime_states[0].get(), context.get(),
+                              FunctionContext::FRAGMENT_LOCAL)
+                        .ok());
+
+    // Even with low-selectivity data, always_true should NOT be detected
+    // because sampling is disabled
+    auto& selectivity = context->get_runtime_filter_selectivity();
+    selectivity.update_judge_selectivity(1, 2000, 50000, 0.1);
+    EXPECT_FALSE(selectivity.maybe_always_true_can_ignore());
+}
+
+// Test that sampling_frequency survives VExprContext recreation, which is the
+// exact scenario that caused the original bug.
+TEST_F(VRuntimeFilterWrapperSamplingTest, 
sampling_frequency_survives_context_recreation) {
+    auto stub = std::make_shared<StubVExpr>();
+    auto node = TExprNodeBuilder(
+                        TExprNodeType::SLOT_REF,
+                        TTypeDescBuilder()
+                                .set_types(TTypeNodeBuilder()
+                                                   
.set_type(TTypeNodeType::SCALAR)
+                                                   
.set_scalar_type(TPrimitiveType::INT)
+                                                   .build())
+                                .build(),
+                        0)
+                        .build();
+
+    const int expected_frequency = 32;
+    auto wrapper = VRuntimeFilterWrapper::create_shared(node, stub, 0.4, 
false, /*filter_id=*/1,
+                                                        expected_frequency);
+
+    // First context - prepare and open work
+    auto context1 = std::make_shared<VExprContext>(wrapper);
+    RowDescriptor row_desc;
+    ASSERT_TRUE(wrapper->prepare(_runtime_states[0].get(), row_desc, 
context1.get()).ok());
+    ASSERT_TRUE(wrapper->open(_runtime_states[0].get(), context1.get(),
+                              FunctionContext::FRAGMENT_LOCAL)
+                        .ok());
+
+    // Simulate context recreation (what _append_rf_into_conjuncts does):
+    // Create a brand new VExprContext with the same VRuntimeFilterWrapper.
+    // The new context starts with default sampling_frequency = -1.
+    auto context2 = std::make_shared<VExprContext>(wrapper);
+    
EXPECT_FALSE(context2->get_runtime_filter_selectivity().maybe_always_true_can_ignore());
+
+    // After open() on the new context, sampling_frequency should be propagated
+    ASSERT_TRUE(wrapper->open(_runtime_states[0].get(), context2.get(),
+                              FunctionContext::THREAD_LOCAL)
+                        .ok());

Review Comment:
   In this test, the recreated context (context2) is opened by calling 
VRuntimeFilterWrapper::open() directly without preparing/opening the 
VExprContext itself. In production (_append_rf_into_conjuncts), the flow is 
VExprContext::create_shared(expr) followed by conjunct->prepare(...) and 
conjunct->open(...), which sets internal flags and drives open via the context. 
To better match real behavior (and avoid brittleness if open/prepare semantics 
change), consider using context2->prepare(...) and context2->open(...) instead 
of invoking wrapper->open() directly.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to