zclllyybb commented on code in PR #57531:
URL: https://github.com/apache/doris/pull/57531#discussion_r2701517349
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Default.java:
##########
@@ -15,55 +15,57 @@
// specific language governing permissions and limitations
// under the License.
-package org.apache.doris.nereids.trees.expressions.functions.scalar;
+package org.apache.doris.nereids.trees.expressions;
-import org.apache.doris.catalog.FunctionSignature;
-import org.apache.doris.nereids.trees.expressions.Expression;
-import
org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
-import org.apache.doris.nereids.trees.expressions.functions.PropagateNullable;
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.trees.expressions.functions.AlwaysNullable;
import org.apache.doris.nereids.trees.expressions.shape.UnaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
-import org.apache.doris.nereids.types.QuantileStateType;
-import org.apache.doris.nereids.types.StringType;
+import org.apache.doris.nereids.types.DataType;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import java.util.List;
/**
- * Function 'quantile_state_to_base64'.
+ * Default value expression.
*/
-public class QuantileStateToBase64 extends ScalarFunction
- implements UnaryExpression, ExplicitlyCastableSignature,
PropagateNullable {
+public class Default extends Expression
+ implements UnaryExpression, AlwaysNullable {
- public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
-
FunctionSignature.ret(StringType.INSTANCE).args(QuantileStateType.INSTANCE)
- );
+ public Default(Expression child) {
+ super(ImmutableList.of(child));
+ }
/**
* constructor with 1 argument.
*/
- public QuantileStateToBase64(Expression arg) {
- super("quantile_state_to_base64", arg);
+ public Default(Expression arg, DataType targetType) {
+ super(ImmutableList.of(arg));
}
- /**
- * withChildren.
- */
@Override
- public QuantileStateToBase64 withChildren(List<Expression> children) {
+ public Default withChildren(List<Expression> children) {
Preconditions.checkArgument(children.size() == 1);
- return new QuantileStateToBase64(children.get(0));
+ return new Default(children.get(0));
+ }
+
+ @Override
+ public DataType getDataType() {
+ return child().getDataType();
}
@Override
- public List<FunctionSignature> getSignatures() {
- return SIGNATURES;
+ public void checkLegalityBeforeTypeCoercion() {
Review Comment:
also check after rewrite
##########
fe/fe-core/src/main/java/org/apache/doris/analysis/DefaultExpr.java:
##########
Review Comment:
do we really still need DefaultExpr? could Default directly used and
translate to send to BE?
##########
be/src/vec/exprs/vdefault_expr.cpp:
##########
@@ -0,0 +1,181 @@
+// 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/exprs/vdefault_expr.h"
+
+#include <cctz/time_zone.h>
+#include <fmt/format.h>
+
+#include "runtime/descriptors.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/block.h"
+#include "vec/data_types/serde/data_type_serde.h"
+#include "vec/exprs/vexpr_context.h"
+#include "vec/exprs/vslot_ref.h"
+#include "vec/runtime/timestamptz_value.h"
+#include "vec/runtime/vdatetime_value.h"
+
+namespace doris::vectorized {
+
+Status VDefaultExpr::prepare(RuntimeState* state, const RowDescriptor& desc,
+ VExprContext* context) {
+ RETURN_IF_ERROR(VExpr::prepare(state, desc, context));
+ DCHECK_EQ(_children.size(), 1);
+
+ _expr_name = fmt::format("default({})", _children[0]->expr_name());
+
+ auto* slot_ref = dynamic_cast<VSlotRef*>(_children[0].get());
+ if (slot_ref != nullptr && slot_ref->slot_id() != -1) {
+ const auto* slot_desc =
state->desc_tbl().get_slot_descriptor(slot_ref->slot_id());
+ if (slot_desc) {
+ _slot_id = slot_ref->slot_id();
+ _is_nullable = slot_desc->is_nullable();
+ _has_default_value = slot_desc->has_default_value();
+ if (_has_default_value) {
+ _default_value = slot_desc->col_default_value();
+ }
+ }
+ }
+ _prepare_finished = true;
+ return Status::OK();
+}
+
+Status VDefaultExpr::open(RuntimeState* state, VExprContext* context,
+ FunctionContext::FunctionStateScope scope) {
+ RETURN_IF_ERROR(VExpr::open(state, context, scope));
+ int64_t timestamp_ms = state->timestamp_ms();
+ int32_t nano_seconds = state->nano_seconds();
+ cctz::time_zone timezone_obj = state->timezone_obj();
+
+ auto res_nested_type = remove_nullable(_data_type);
+ PrimitiveType res_primitive_type = res_nested_type->get_primitive_type();
+
+ // Agg state(HLL, BITMAPA, QUANTILE_STATE) error only
+ if (is_var_len_object(res_primitive_type)) {
Review Comment:
could this be checked in FE?
##########
be/src/vec/exprs/vdefault_expr.cpp:
##########
@@ -0,0 +1,181 @@
+// 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/exprs/vdefault_expr.h"
+
+#include <cctz/time_zone.h>
+#include <fmt/format.h>
+
+#include "runtime/descriptors.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/block.h"
+#include "vec/data_types/serde/data_type_serde.h"
+#include "vec/exprs/vexpr_context.h"
+#include "vec/exprs/vslot_ref.h"
+#include "vec/runtime/timestamptz_value.h"
+#include "vec/runtime/vdatetime_value.h"
+
+namespace doris::vectorized {
+
+Status VDefaultExpr::prepare(RuntimeState* state, const RowDescriptor& desc,
+ VExprContext* context) {
+ RETURN_IF_ERROR(VExpr::prepare(state, desc, context));
+ DCHECK_EQ(_children.size(), 1);
+
+ _expr_name = fmt::format("default({})", _children[0]->expr_name());
+
+ auto* slot_ref = dynamic_cast<VSlotRef*>(_children[0].get());
+ if (slot_ref != nullptr && slot_ref->slot_id() != -1) {
+ const auto* slot_desc =
state->desc_tbl().get_slot_descriptor(slot_ref->slot_id());
+ if (slot_desc) {
+ _slot_id = slot_ref->slot_id();
+ _is_nullable = slot_desc->is_nullable();
+ _has_default_value = slot_desc->has_default_value();
+ if (_has_default_value) {
+ _default_value = slot_desc->col_default_value();
+ }
+ }
+ }
+ _prepare_finished = true;
+ return Status::OK();
+}
+
+Status VDefaultExpr::open(RuntimeState* state, VExprContext* context,
+ FunctionContext::FunctionStateScope scope) {
+ RETURN_IF_ERROR(VExpr::open(state, context, scope));
+ int64_t timestamp_ms = state->timestamp_ms();
+ int32_t nano_seconds = state->nano_seconds();
+ cctz::time_zone timezone_obj = state->timezone_obj();
+
+ auto res_nested_type = remove_nullable(_data_type);
+ PrimitiveType res_primitive_type = res_nested_type->get_primitive_type();
+
+ // 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_valueni
+ // 2. no specified default value && column is NULLABLE -> NULL
+ // 3. no specified default value && column is NOT NULL -> error
+ if (_has_default_value) {
+ if ((is_date_type(res_primitive_type) || res_primitive_type ==
TYPE_TIMESTAMPTZ) &&
+ (_default_value.starts_with("CURRENT_TIMESTAMP") || _default_value
== "CURRENT_DATE")) {
+ int precision = -1;
+ if (_default_value.size() > 17) {
+ precision = std::atoi(_default_value.substr(18, 1).c_str());
Review Comment:
use our `string_parser.hpp`'s utils
##########
be/src/vec/exprs/vdefault_expr.cpp:
##########
@@ -0,0 +1,181 @@
+// 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/exprs/vdefault_expr.h"
+
+#include <cctz/time_zone.h>
+#include <fmt/format.h>
+
+#include "runtime/descriptors.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/block.h"
+#include "vec/data_types/serde/data_type_serde.h"
+#include "vec/exprs/vexpr_context.h"
+#include "vec/exprs/vslot_ref.h"
+#include "vec/runtime/timestamptz_value.h"
+#include "vec/runtime/vdatetime_value.h"
+
+namespace doris::vectorized {
+
+Status VDefaultExpr::prepare(RuntimeState* state, const RowDescriptor& desc,
+ VExprContext* context) {
+ RETURN_IF_ERROR(VExpr::prepare(state, desc, context));
+ DCHECK_EQ(_children.size(), 1);
+
+ _expr_name = fmt::format("default({})", _children[0]->expr_name());
+
+ auto* slot_ref = dynamic_cast<VSlotRef*>(_children[0].get());
+ if (slot_ref != nullptr && slot_ref->slot_id() != -1) {
+ const auto* slot_desc =
state->desc_tbl().get_slot_descriptor(slot_ref->slot_id());
+ if (slot_desc) {
+ _slot_id = slot_ref->slot_id();
+ _is_nullable = slot_desc->is_nullable();
+ _has_default_value = slot_desc->has_default_value();
+ if (_has_default_value) {
+ _default_value = slot_desc->col_default_value();
+ }
+ }
+ }
+ _prepare_finished = true;
+ return Status::OK();
+}
+
+Status VDefaultExpr::open(RuntimeState* state, VExprContext* context,
+ FunctionContext::FunctionStateScope scope) {
+ RETURN_IF_ERROR(VExpr::open(state, context, scope));
+ int64_t timestamp_ms = state->timestamp_ms();
+ int32_t nano_seconds = state->nano_seconds();
+ cctz::time_zone timezone_obj = state->timezone_obj();
+
+ auto res_nested_type = remove_nullable(_data_type);
+ PrimitiveType res_primitive_type = res_nested_type->get_primitive_type();
+
+ // 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_valueni
+ // 2. no specified default value && column is NULLABLE -> NULL
+ // 3. no specified default value && column is NOT NULL -> error
+ if (_has_default_value) {
+ if ((is_date_type(res_primitive_type) || res_primitive_type ==
TYPE_TIMESTAMPTZ) &&
+ (_default_value.starts_with("CURRENT_TIMESTAMP") || _default_value
== "CURRENT_DATE")) {
+ int precision = -1;
+ if (_default_value.size() > 17) {
+ precision = std::atoi(_default_value.substr(18, 1).c_str());
+ } else {
+ precision = res_nested_type->get_scale();
+ }
+
+ switch (res_primitive_type) {
+ case TYPE_DATEV2:
+
RETURN_IF_ERROR(write_current_time_value<TYPE_DATEV2>(timestamp_ms,
nano_seconds,
+
timezone_obj, precision));
+ break;
+ case TYPE_DATETIMEV2:
+ RETURN_IF_ERROR(write_current_time_value<TYPE_DATETIMEV2>(
+ timestamp_ms, nano_seconds, timezone_obj, precision));
+ break;
+ case TYPE_TIMESTAMPTZ:
+ RETURN_IF_ERROR(write_current_time_value<TYPE_TIMESTAMPTZ>(
+ timestamp_ms, nano_seconds, timezone_obj, precision));
+ break;
+ default:
+ return Status::FatalError(
+ "Unknown date type in DefaultExpr for
CURRENT_DATE/TIMESTAMP");
+ }
+ } else {
+ 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) {
+ Field default_field;
+ temp_column->get(0, default_field);
+ _cached_column = _data_type->create_column_const(1,
default_field);
+ } else [[unlikely]] {
+ return Status::FatalError("Failed to parse default value for
column '{}'",
+ _children[0]->expr_name());
+ }
+ }
+ } else if (_is_nullable) {
+ _cached_column = _data_type->create_column_const(1, Field());
+ } else {
+ return Status::InvalidArgument("Column '{}' is NOT NULL but has no
default value",
+ _children[0]->expr_name());
+ }
+
+ _open_finished = true;
+ return Status::OK();
+}
+
+const std::string& VDefaultExpr::expr_name() const {
+ return _expr_name;
+}
+
+std::string VDefaultExpr::debug_string() const {
+ std::stringstream out;
+ out << "DefaultExpr(slot_id=" << _slot_id << ", is_nullable=" <<
_is_nullable
Review Comment:
add the _default_value here. and its type if possible
##########
be/src/vec/exprs/vdefault_expr.cpp:
##########
@@ -0,0 +1,181 @@
+// 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/exprs/vdefault_expr.h"
+
+#include <cctz/time_zone.h>
+#include <fmt/format.h>
+
+#include "runtime/descriptors.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/block.h"
+#include "vec/data_types/serde/data_type_serde.h"
+#include "vec/exprs/vexpr_context.h"
+#include "vec/exprs/vslot_ref.h"
+#include "vec/runtime/timestamptz_value.h"
+#include "vec/runtime/vdatetime_value.h"
+
+namespace doris::vectorized {
+
+Status VDefaultExpr::prepare(RuntimeState* state, const RowDescriptor& desc,
+ VExprContext* context) {
+ RETURN_IF_ERROR(VExpr::prepare(state, desc, context));
+ DCHECK_EQ(_children.size(), 1);
+
+ _expr_name = fmt::format("default({})", _children[0]->expr_name());
+
+ auto* slot_ref = dynamic_cast<VSlotRef*>(_children[0].get());
Review Comment:
oh, when will it be null?
##########
be/src/vec/exprs/vdefault_expr.cpp:
##########
@@ -0,0 +1,181 @@
+// 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/exprs/vdefault_expr.h"
+
+#include <cctz/time_zone.h>
+#include <fmt/format.h>
+
+#include "runtime/descriptors.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/block.h"
+#include "vec/data_types/serde/data_type_serde.h"
+#include "vec/exprs/vexpr_context.h"
+#include "vec/exprs/vslot_ref.h"
+#include "vec/runtime/timestamptz_value.h"
+#include "vec/runtime/vdatetime_value.h"
+
+namespace doris::vectorized {
+
+Status VDefaultExpr::prepare(RuntimeState* state, const RowDescriptor& desc,
+ VExprContext* context) {
+ RETURN_IF_ERROR(VExpr::prepare(state, desc, context));
+ DCHECK_EQ(_children.size(), 1);
+
+ _expr_name = fmt::format("default({})", _children[0]->expr_name());
+
+ auto* slot_ref = dynamic_cast<VSlotRef*>(_children[0].get());
+ if (slot_ref != nullptr && slot_ref->slot_id() != -1) {
Review Comment:
when will this branch be false?
##########
be/src/vec/exprs/vdefault_expr.cpp:
##########
@@ -0,0 +1,181 @@
+// 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/exprs/vdefault_expr.h"
+
+#include <cctz/time_zone.h>
+#include <fmt/format.h>
+
+#include "runtime/descriptors.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/block.h"
+#include "vec/data_types/serde/data_type_serde.h"
+#include "vec/exprs/vexpr_context.h"
+#include "vec/exprs/vslot_ref.h"
+#include "vec/runtime/timestamptz_value.h"
+#include "vec/runtime/vdatetime_value.h"
+
+namespace doris::vectorized {
+
+Status VDefaultExpr::prepare(RuntimeState* state, const RowDescriptor& desc,
+ VExprContext* context) {
+ RETURN_IF_ERROR(VExpr::prepare(state, desc, context));
+ DCHECK_EQ(_children.size(), 1);
+
+ _expr_name = fmt::format("default({})", _children[0]->expr_name());
+
+ auto* slot_ref = dynamic_cast<VSlotRef*>(_children[0].get());
Review Comment:
assert_cast
--
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]