zclllyybb commented on code in PR #57531:
URL: https://github.com/apache/doris/pull/57531#discussion_r2493220429
##########
be/src/vec/exprs/vectorized_fn_call.cpp:
##########
@@ -154,6 +155,22 @@ Status VectorizedFnCall::prepare(RuntimeState* state,
const RowDescriptor& desc,
_prepare_finished = true;
FunctionContext* fn_ctx = context->fn_context(_fn_context_index);
+ if (_function_name == "default" && !_children.empty()) {
Review Comment:
add a new virtual function and call
##########
be/src/vec/functions/function_default.cpp:
##########
@@ -0,0 +1,198 @@
+// 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 "vec/functions/function_default.h"
+
+#include <mysql/binary_log_types.h>
+
+#include <string>
+
+#include "common/status.h"
+#include "runtime/define_primitive_type.h"
+#include "runtime/descriptors.h"
+#include "runtime/primitive_type.h"
+#include "runtime/runtime_state.h"
+#include "util/binary_cast.hpp"
+#include "vec/columns/column_const.h"
+#include "vec/columns/column_nullable.h"
+#include "vec/core/column_with_type_and_name.h"
+#include "vec/data_types/data_type_nullable.h"
+#include "vec/data_types/serde/data_type_serde.h"
+#include "vec/functions/function.h"
+#include "vec/functions/simple_function_factory.h"
+#include "vec/runtime/vdatetime_value.h"
+
+namespace doris::vectorized {
+#include "common/compile_check_begin.h"
+
+class FunctionDefault : public IFunction {
+public:
+ static constexpr auto name = "default";
+ static FunctionPtr create() { return std::make_shared<FunctionDefault>(); }
+ String get_name() const override { return name; }
+ size_t get_number_of_arguments() const override { return 1; }
+
+ DataTypePtr get_return_type_impl(const DataTypes& arguments) const
override {
+ return make_nullable(arguments[0]);
+ }
+
+ bool use_default_implementation_for_nulls() const override { return false;
}
+
+ Status execute_impl(FunctionContext* context, Block& block, const
ColumnNumbers& arguments,
+ uint32_t result, size_t input_rows_count) const
override {
+ ColumnWithTypeAndName& result_info = block.get_by_position(result);
+ auto res_nested_type = remove_nullable(result_info.type);
+ PrimitiveType res_primitive_type =
res_nested_type->get_primitive_type();
+
+ ColumnWithTypeAndName& input_column_info =
block.get_by_position(arguments[0]);
+ const std::string& col_name = input_column_info.name;
+
+ std::string default_value;
+ bool has_default_value = false;
+ bool is_nullable = true;
+ const auto* slot_state = static_cast<const DefaultFunctionState*>(
+ context->get_function_state(FunctionContext::FRAGMENT_LOCAL));
+ if (slot_state != nullptr && slot_state->slot_id != -1) {
+ default_value = slot_state->default_value;
+ has_default_value = slot_state->has_default_value;
+ is_nullable = slot_state->is_nullable;
+ }
+
+ // For date types, if the default value is `CURRENT_TIMESTAMP` or
`CURRENT_DATE`.
+ // Reference the behavior in MySQL:
+ // if column is NULLABLE, return all NULLs
+ // else return zero datetime values like `0000-00-00 00:00:00` or
`0000-00-00`
+ // Because Doris has a range limit for date & datetime, `0000-00-00`
cannot be represented.
+ // So here we use the smallest representable value instead
+ if (is_date_type(res_primitive_type) && has_default_value &&
+ (default_value == "CURRENT_TIMESTAMP" || default_value ==
"CURRENT_DATE")) {
+ if (is_nullable) {
+ return return_with_all_null(block, result, res_nested_type,
input_rows_count);
+ } else {
+ return return_with_zero_datetime(block, result,
res_nested_type, res_primitive_type,
+ input_rows_count);
+ }
+ }
+
+ // Agg state(HLL, BITMAPA, QUANTILE_STATE) error only
+ if (is_var_len_object(res_primitive_type)) {
+ return Status::InvalidArgument(
+ "Agg type(HLL, BITMAP, QUANTILE_STATE) cannot be used for
the DEFAULT "
+ "function");
+ }
+
+ // 1. specified default value when creating table -> default_value
+ // 2. no specified default value && column is NOT NULL -> error
+ // 3. no specified default value && column is NULLABLE -> NULL
+ if (has_default_value) {
+ MutableColumnPtr res_col = res_nested_type->create_column();
+ auto null_map = ColumnUInt8::create(input_rows_count, 0);
+ Field default_field;
+
+ auto temp_column = res_nested_type->create_column();
+ auto serde = res_nested_type->get_serde();
+ StringRef default_str_ref(default_value.data(),
default_value.size());
+ DataTypeSerDe::FormatOptions options;
+ Status parse_status = serde->from_string(default_str_ref,
*temp_column, options);
+
+ if (parse_status.ok() && temp_column->size() > 0) {
+ temp_column->get(0, default_field);
+ res_col->insert(default_field);
+ block.replace_by_position(
+ result, ColumnNullable::create(
+
ColumnConst::create(std::move(res_col), input_rows_count),
+ std::move(null_map)));
+ } else [[unlikely]] {
+ return Status::FatalError("Failed to parse default value for
column '{}'",
+ col_name);
+ }
+ } else {
+ if (is_nullable) {
+ return return_with_all_null(block, result, res_nested_type,
input_rows_count);
+ } else {
+ return Status::InvalidArgument("Column '{}' is NOT NULL but
has no default value",
+ col_name);
+ }
+ }
+ return Status::OK();
+ }
+
+private:
+ static Status return_with_all_null(Block& block, uint32_t result,
+ const DataTypePtr& nested_type, size_t
input_rows_count) {
+ MutableColumnPtr res_col = nested_type->create_column();
+ res_col->insert_default();
+ auto null_map = ColumnUInt8::create(input_rows_count, 1);
+ block.replace_by_position(
+ result,
+ ColumnNullable::create(ColumnConst::create(std::move(res_col),
input_rows_count),
+ std::move(null_map)));
+ return Status::OK();
+ }
+
+ static Status return_with_zero_datetime(Block& block, uint32_t result,
+ const DataTypePtr& nested_type,
+ PrimitiveType primitive_type,
size_t input_rows_count) {
+ MutableColumnPtr res_col = nested_type->create_column();
+
+ switch (primitive_type) {
+ case TYPE_DATE:
Review Comment:
not support all V1 anymore
--
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]