Skip to content

Commit e6c286c

Browse files
committed
extract virtual func
1 parent 1a8dae9 commit e6c286c

File tree

3 files changed

+37
-27
lines changed

3 files changed

+37
-27
lines changed

be/src/vec/exprs/vectorized_fn_call.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
#include "vec/exprs/vliteral.h"
5656
#include "vec/functions/array/function_array_distance.h"
5757
#include "vec/functions/function_agg_state.h"
58-
#include "vec/functions/function_default.h"
5958
#include "vec/functions/function_fake.h"
6059
#include "vec/functions/function_java_udf.h"
6160
#include "vec/functions/function_rpc.h"
@@ -155,25 +154,10 @@ Status VectorizedFnCall::prepare(RuntimeState* state, const RowDescriptor& desc,
155154
_prepare_finished = true;
156155

157156
FunctionContext* fn_ctx = context->fn_context(_fn_context_index);
158-
if (_function_name == "default" && !_children.empty()) {
159-
auto default_state = std::make_shared<DefaultFunctionState>();
160-
auto* slot_ref = dynamic_cast<VSlotRef*>(_children[0].get());
161-
if (slot_ref != nullptr && slot_ref->slot_id() != -1) {
162-
const auto* slot_desc = state->desc_tbl().get_slot_descriptor(slot_ref->slot_id());
163-
164-
default_state->slot_id = slot_ref->slot_id();
165-
default_state->is_nullable = slot_desc->is_nullable();
166-
default_state->has_default_value = slot_desc->has_default_value();
167-
if (default_state->has_default_value) {
168-
default_state->default_value = slot_desc->col_default_value();
169-
}
170-
}
171-
fn_ctx->set_function_state(FunctionContext::FRAGMENT_LOCAL, default_state);
172-
}
173-
174157
if (fn().__isset.dict_function) {
175158
fn_ctx->set_dict_function(fn().dict_function);
176159
}
160+
RETURN_IF_ERROR(_function->prepare_with_children(fn_ctx, state, desc, context, _children));
177161
return Status::OK();
178162
}
179163

be/src/vec/functions/function.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,12 @@ class IFunctionBase {
210210
return Status::OK();
211211
}
212212

213+
virtual Status prepare_with_children(FunctionContext* fn_context, RuntimeState* state,
214+
const RowDescriptor& desc, VExprContext* context,
215+
const std::vector<VExprSPtr>& children) {
216+
return Status::OK();
217+
}
218+
213219
virtual bool is_use_default_implementation_for_constants() const = 0;
214220

215221
virtual bool is_udf_function() const { return false; }
@@ -464,6 +470,12 @@ class DefaultFunction final : public IFunctionBase {
464470
return function->close(context, scope);
465471
}
466472

473+
Status prepare_with_children(FunctionContext* fn_context, RuntimeState* state,
474+
const RowDescriptor& desc, VExprContext* context,
475+
const std::vector<VExprSPtr>& children) override {
476+
return function->prepare_with_children(fn_context, state, desc, context, children);
477+
}
478+
467479
Status evaluate_inverted_index(
468480
const ColumnsWithTypeAndName& args,
469481
const std::vector<vectorized::IndexFieldNameAndTypePair>& data_type_with_names,

be/src/vec/functions/function_default.cpp

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@
2626
#include "runtime/descriptors.h"
2727
#include "runtime/primitive_type.h"
2828
#include "runtime/runtime_state.h"
29-
#include "util/binary_cast.hpp"
3029
#include "vec/columns/column_const.h"
3130
#include "vec/columns/column_nullable.h"
3231
#include "vec/core/column_with_type_and_name.h"
3332
#include "vec/data_types/data_type_nullable.h"
3433
#include "vec/data_types/serde/data_type_serde.h"
35-
#include "vec/functions/function.h"
34+
#include "vec/exprs/vslot_ref.h"
3635
#include "vec/functions/simple_function_factory.h"
3736
#include "vec/runtime/vdatetime_value.h"
3837

@@ -52,6 +51,27 @@ class FunctionDefault : public IFunction {
5251

5352
bool use_default_implementation_for_nulls() const override { return false; }
5453

54+
Status prepare_with_children(FunctionContext* fn_context, RuntimeState* state,
55+
const RowDescriptor& desc, VExprContext* context,
56+
const std::vector<VExprSPtr>& children) override {
57+
if (!children.empty()) {
58+
auto default_state = std::make_shared<DefaultFunctionState>();
59+
auto* slot_ref = dynamic_cast<VSlotRef*>(children[0].get());
60+
if (slot_ref != nullptr && slot_ref->slot_id() != -1) {
61+
const auto* slot_desc = state->desc_tbl().get_slot_descriptor(slot_ref->slot_id());
62+
63+
default_state->slot_id = slot_ref->slot_id();
64+
default_state->is_nullable = slot_desc->is_nullable();
65+
default_state->has_default_value = slot_desc->has_default_value();
66+
if (default_state->has_default_value) {
67+
default_state->default_value = slot_desc->col_default_value();
68+
}
69+
}
70+
fn_context->set_function_state(FunctionContext::FRAGMENT_LOCAL, default_state);
71+
}
72+
return Status::OK();
73+
}
74+
5575
Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
5676
uint32_t result, size_t input_rows_count) const override {
5777
ColumnWithTypeAndName& result_info = block.get_by_position(result);
@@ -150,10 +170,6 @@ class FunctionDefault : public IFunction {
150170
MutableColumnPtr res_col = nested_type->create_column();
151171

152172
switch (primitive_type) {
153-
case TYPE_DATE:
154-
case TYPE_DATETIME:
155-
insert_min_datetime_value<TYPE_DATETIME>(res_col);
156-
break;
157173
case TYPE_DATEV2:
158174
insert_min_datetime_value<TYPE_DATEV2>(res_col);
159175
break;
@@ -175,13 +191,11 @@ class FunctionDefault : public IFunction {
175191

176192
template <PrimitiveType Type>
177193
static void insert_min_datetime_value(MutableColumnPtr& res_col) {
194+
DCHECK(Type == TYPE_DATEV2 || Type == TYPE_DATETIMEV2);
178195
using ItemType = typename PrimitiveTypeTraits<Type>::ColumnItemType;
179196
ItemType min_value;
180197

181-
if constexpr (Type == TYPE_DATE || Type == TYPE_DATETIME) {
182-
min_value =
183-
binary_cast<VecDateTimeValue, ItemType>(VecDateTimeValue::datetime_min_value());
184-
} else if constexpr (Type == TYPE_DATEV2) {
198+
if constexpr (Type == TYPE_DATEV2) {
185199
min_value = MIN_DATE_V2;
186200
} else if constexpr (Type == TYPE_DATETIMEV2) {
187201
min_value = MIN_DATETIME_V2;

0 commit comments

Comments
 (0)