From e2becc8a4440beb38ab585aaf5799f2cdf86dc70 Mon Sep 17 00:00:00 2001 From: Mryange <2319153948@qq.com> Date: Wed, 18 Sep 2024 18:17:43 +0800 Subject: [PATCH] pick --- be/src/vec/exprs/vin_predicate.cpp | 19 ++++++++++++++++--- be/src/vec/exprs/vin_predicate.h | 3 +++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/be/src/vec/exprs/vin_predicate.cpp b/be/src/vec/exprs/vin_predicate.cpp index 1411254a2caa14..e4a4969b00ee71 100644 --- a/be/src/vec/exprs/vin_predicate.cpp +++ b/be/src/vec/exprs/vin_predicate.cpp @@ -93,10 +93,24 @@ Status VInPredicate::open(RuntimeState* state, VExprContext* context, if (scope == FunctionContext::FRAGMENT_LOCAL) { RETURN_IF_ERROR(VExpr::get_const_col(context, nullptr)); } + + _is_args_all_constant = std::all_of(_children.begin() + 1, _children.end(), + [](const VExprSPtr& expr) { return expr->is_constant(); }); _open_finished = true; return Status::OK(); } +size_t VInPredicate::skip_constant_args_size() const { + if (_is_args_all_constant && !_can_fast_execute) { + // This is an optimization. For expressions like colA IN (1, 2, 3, 4), + // where all values inside the IN clause are constants, + // a hash set is created during open, and it will not be accessed again during execute + // Here, _children[0] is colA + return 1; + } + return _children.size(); +} + void VInPredicate::close(VExprContext* context, FunctionContext::FunctionStateScope scope) { VExpr::close_function_context(context, scope, _function); VExpr::close(context, scope); @@ -115,9 +129,8 @@ Status VInPredicate::execute(VExprContext* context, Block* block, int* result_co return Status::OK(); } DCHECK(_open_finished || _getting_const_col); - // TODO: not execute const expr again, but use the const column in function context - doris::vectorized::ColumnNumbers arguments(_children.size()); - for (int i = 0; i < _children.size(); ++i) { + doris::vectorized::ColumnNumbers arguments(skip_constant_args_size()); + for (int i = 0; i < skip_constant_args_size(); ++i) { int column_id = -1; RETURN_IF_ERROR(_children[i]->execute(context, block, &column_id)); arguments[i] = column_id; diff --git a/be/src/vec/exprs/vin_predicate.h b/be/src/vec/exprs/vin_predicate.h index 4d227510b910ce..1b64005628428c 100644 --- a/be/src/vec/exprs/vin_predicate.h +++ b/be/src/vec/exprs/vin_predicate.h @@ -51,6 +51,8 @@ class VInPredicate final : public VExpr { std::string debug_string() const override; + size_t skip_constant_args_size() const; + const FunctionBasePtr function() { return _function; } bool is_not_in() const { return _is_not_in; }; @@ -62,5 +64,6 @@ class VInPredicate final : public VExpr { const bool _is_not_in; static const constexpr char* function_name = "in"; + bool _is_args_all_constant = false; }; } // namespace doris::vectorized \ No newline at end of file