zclllyybb commented on code in PR #57531:
URL: https://github.com/apache/doris/pull/57531#discussion_r2486468809


##########
fe/fe-core/src/main/java/org/apache/doris/analysis/ColumnDef.java:
##########
@@ -433,7 +433,7 @@ public void analyze(boolean isOlap) throws 
AnalysisException {
                     throw new AnalysisException("complex type have to use 
aggregate function: " + name);
                 }
             }
-            if (isAllowNull) {
+            if (!isAllowNull) {

Review Comment:
   seems wrong?



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Default.java:
##########
@@ -0,0 +1,85 @@
+// 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.
+
+package org.apache.doris.nereids.trees.expressions.functions.scalar;
+
+import org.apache.doris.catalog.FunctionSignature;
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.SlotReference;
+import org.apache.doris.nereids.trees.expressions.functions.AlwaysNullable;
+import org.apache.doris.nereids.trees.expressions.functions.CustomSignature;
+import org.apache.doris.nereids.trees.expressions.shape.UnaryExpression;
+import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
+import org.apache.doris.nereids.types.DataType;
+
+import com.google.common.base.Preconditions;
+
+import java.util.List;
+
+/**
+ * ScalarFunction 'default'. This function returns the default value of a 
column.
+ */
+public class Default extends ScalarFunction
+        implements UnaryExpression, CustomSignature, AlwaysNullable {
+
+    /**
+     * constructor with 1 argument.
+     */
+    public Default(Expression arg) {
+        super("default", arg);
+    }
+
+    /** constructor for withChildren and reuse signature */
+    private Default(ScalarFunctionParams functionParams) {
+        super(functionParams);
+    }
+
+    /**
+     * withChildren.
+     */
+    @Override
+    public Default withChildren(List<Expression> children) {
+        Preconditions.checkArgument(children.size() == 1);

Review Comment:
   can this make sure only accept one argument? please add test for more than 
one args in BE and foldByBE paths



##########
be/src/vec/functions/function_default.cpp:
##########
@@ -0,0 +1,223 @@
+// 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`
+        // 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);
+            }
+        }
+
+        // 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);
+                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:
+    void get_default_value_and_nullable_for_col(FunctionContext* context,
+                                                const std::string& column_name,
+                                                std::string& default_value, 
bool& has_default_value,
+                                                bool& is_nullable) const {
+        RuntimeState* state = context->state();
+        const DescriptorTbl& desc_tbl = state->desc_tbl();
+
+        SlotDescriptor* target_slot = nullptr;
+        for (auto* tuple_desc : desc_tbl.get_tuple_descs()) {
+            for (auto* slot : tuple_desc->slots()) {
+                if (slot->col_name() == column_name) {

Review Comment:
   what if multi columns with same name but in different table so have 
different types and default value?



-- 
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