linrrzqqq commented on code in PR #57531: URL: https://github.com/apache/doris/pull/57531#discussion_r2484850516
########## be/src/vec/functions/function_default.cpp: ########## @@ -0,0 +1,218 @@ +// 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 <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; + get_default_value_and_nullable_for_col(context, col_name, default_value, has_default_value, + 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` + 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); + } + } + + // For some complex types, only accept NULL as default value + if (is_complex_type(res_primitive_type) || res_primitive_type == TYPE_JSONB || + res_primitive_type == TYPE_VARIANT) { + if (is_nullable) { + return return_with_all_null(block, result, res_nested_type, input_rows_count); + } else { + return Status::InvalidArgument( + "Column '{}' of type '{}' must be nullable to use DEFAULT", col_name, + res_nested_type->get_name()); + } + } + + // 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); + } + + for (size_t i = 0; i < input_rows_count; ++i) { + res_col->insert(default_field); Review Comment: This is a scalar function in MySQL's behavior -- 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]
