You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by GitBox <gi...@apache.org> on 2022/10/16 15:55:53 UTC

[GitHub] [doris] lsy3993 opened a new pull request, #13400: [function](date function)add new date function 'last_day'

lsy3993 opened a new pull request, #13400:
URL: https://github.com/apache/doris/pull/13400

   # Proposed changes
   
   Issue Number: close #13139
   
   ## Problem summary
   
   Describe your changes.
   
   ## Checklist(Required)
   
   1. Does it affect the original behavior: 
       - [ ] Yes
       - [x] No
       - [ ] I don't know
   2. Has unit tests been added:
       - [x] Yes
       - [ ] No
       - [ ] No Need
   3. Has document been added or modified:
       - [x] Yes
       - [ ] No
       - [ ] No Need
   4. Does it need to update dependencies:
       - [ ] Yes
       - [x] No
   5. Are there any changes that cannot be rolled back:
       - [ ] Yes (If Yes, please explain WHY)
       - [x] No
   
   ## Further comments
   
   If this is a relatively large or complex change, kick off the discussion at [dev@doris.apache.org](mailto:dev@doris.apache.org) by explaining why you chose the solution you did and what alternatives you considered, etc...
   
   


-- 
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: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [doris] stalary commented on a diff in pull request #13400: [function](date function)add new date function 'last_day'

Posted by GitBox <gi...@apache.org>.
stalary commented on code in PR #13400:
URL: https://github.com/apache/doris/pull/13400#discussion_r996533238


##########
be/src/vec/functions/function_timestamp.cpp:
##########
@@ -569,6 +569,164 @@ class FunctionUnixTimestamp : public IFunction {
     }
 };
 
+template <typename Impl, typename DateType>
+class FunctionDateOrDateTimeToDate : public IFunction {
+public:
+    static constexpr auto name = Impl::name;
+    static FunctionPtr create() {
+        return std::make_shared<FunctionDateOrDateTimeToDate<Impl, DateType>>();
+    }
+
+    String get_name() const override { return name; }
+
+    bool use_default_implementation_for_nulls() const override { return true; }
+
+    size_t get_number_of_arguments() const override { return 1; }
+
+    bool is_variadic() const override { return true; }
+
+    // input DateTime and Date, return Date
+    // input DateTimeV2 and DateV2, return DateV2
+    DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) const override {
+        if constexpr (std::is_same_v<DateType, DataTypeDateTime>) {
+            return make_nullable(std::make_shared<DataTypeDate>());
+        } else if constexpr (std::is_same_v<DateType, DataTypeDate>) {
+            return make_nullable(std::make_shared<DataTypeDate>());
+        } else if constexpr (std::is_same_v<DateType, DataTypeDateV2>) {
+            return make_nullable(std::make_shared<DataTypeDateV2>());
+        } else {
+            return make_nullable(std::make_shared<DataTypeDateV2>());
+        }
+    }
+
+    DataTypes get_variadic_argument_types_impl() const override {
+        if constexpr (std::is_same_v<DateType, DataTypeDateTime>) {
+            return {std::make_shared<DataTypeDate>()};
+        } else if constexpr (std::is_same_v<DateType, DataTypeDateV2>) {
+            return {std::make_shared<DataTypeDateV2>()};
+        } else {
+            return {std::make_shared<DataTypeDateTimeV2>()};
+        }
+    }
+
+    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
+                        size_t result, size_t input_rows_count) override {
+        return Impl::execute_impl(context, block, arguments, result, input_rows_count);
+    }
+};
+
+template <typename DateType>
+struct LastDayImpl {
+    static constexpr auto name = "last_day";
+
+    static Status execute_impl(FunctionContext* context, Block& block,
+                               const ColumnNumbers& arguments, size_t result,
+                               size_t input_rows_count) {
+        auto null_map = ColumnUInt8::create(input_rows_count, 0);
+        ColumnPtr res_column;
+        ColumnPtr argument_column =
+                block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
+        if constexpr (std::is_same_v<DateType, DataTypeDateTime>) {
+            auto data_col = assert_cast<const ColumnVector<Int64>*>(argument_column.get());
+            res_column = ColumnInt64::create(input_rows_count);
+            execute_straight<VecDateTimeValue, Int64, Int64>(
+                    input_rows_count, null_map->get_data(), data_col->get_data(),
+                    static_cast<ColumnVector<Int64>*>(res_column->assume_mutable().get())
+                            ->get_data());
+
+        } else if constexpr (std::is_same_v<DateType, DataTypeDateV2>) {
+            auto data_col = assert_cast<const ColumnVector<UInt32>*>(argument_column.get());
+            res_column = ColumnVector<UInt32>::create(input_rows_count);
+            execute_straight<DateV2Value<DateV2ValueType>, UInt32, UInt32>(
+                    input_rows_count, null_map->get_data(), data_col->get_data(),
+                    static_cast<ColumnVector<UInt32>*>(res_column->assume_mutable().get())
+                            ->get_data());
+
+        } else if constexpr (std::is_same_v<DateType, DataTypeDateTimeV2>) {
+            auto data_col = assert_cast<const ColumnVector<UInt64>*>(argument_column.get());
+            res_column = ColumnVector<UInt32>::create(input_rows_count);
+            execute_straight<DateV2Value<DateTimeV2ValueType>, UInt32, UInt64>(
+                    input_rows_count, null_map->get_data(), data_col->get_data(),
+                    static_cast<ColumnVector<UInt32>*>(res_column->assume_mutable().get())
+                            ->get_data());
+        } else {
+            // neither DateTime nor DateTimeV2/DateV2, return null
+            if constexpr (std::is_same_v<DateType, DataTypeDateTime>) {
+                res_column = ColumnInt64::create(input_rows_count);
+            } else {
+                res_column = ColumnVector<UInt32>::create(input_rows_count);
+            }
+
+            null_map = ColumnUInt8::create(input_rows_count, 1);
+            block.replace_by_position(
+                    result, ColumnNullable::create(std::move(res_column), std::move(null_map)));
+            return Status::OK();
+        }
+
+        block.replace_by_position(
+                result, ColumnNullable::create(std::move(res_column), std::move(null_map)));
+        return Status::OK();
+    }
+
+    template <typename DateValueType, typename ReturnType, typename InputDateType>
+    static void execute_straight(size_t input_rows_count, NullMap& null_map,
+                                 const PaddedPODArray<InputDateType>& data_col,
+                                 PaddedPODArray<ReturnType>& res_data) {
+        for (int i = 0; i < input_rows_count; i++) {
+            if constexpr (std::is_same_v<DateValueType, VecDateTimeValue>) {
+                const auto& cur_data = data_col[i];
+                auto ts_value = binary_cast<Int64, VecDateTimeValue>(cur_data);
+                int day = get_last_month_day(ts_value.year(), ts_value.month());
+                ts_value.set_time(ts_value.year(), ts_value.month(), day, 0, 0, 0);
+                ts_value.set_type(TIME_DATE);
+                if (!ts_value.is_valid_date()) {
+                    null_map[i] = 1;
+                    continue;
+                }
+                res_data[i] = binary_cast<VecDateTimeValue, Int64>(ts_value);
+
+            } else if constexpr (std::is_same_v<DateValueType, DateV2Value<DateV2ValueType>>) {
+                const auto& cur_data = data_col[i];
+                auto ts_value = binary_cast<UInt32, DateValueType>(cur_data);
+                int day = get_last_month_day(ts_value.year(), ts_value.month());
+                ts_value.template set_time_unit<TimeUnit::DAY>(day);
+                if (!ts_value.is_valid_date()) {
+                    null_map[i] = 1;
+                    continue;
+                }
+                res_data[i] = binary_cast<DateValueType, UInt32>(ts_value);
+
+            } else {
+                const auto& cur_data = data_col[i];
+                auto ts_value = binary_cast<UInt64, DateValueType>(cur_data);
+                int day = get_last_month_day(ts_value.year(), ts_value.month());
+                ts_value.template set_time_unit<TimeUnit::DAY>(day);
+                ts_value.set_time(ts_value.year(), ts_value.month(), day, 0, 0, 0, 0);
+                if (!ts_value.is_valid_date()) {
+                    null_map[i] = 1;
+                    continue;
+                }
+                UInt64 cast_value = binary_cast<DateValueType, UInt64>(ts_value);
+                DataTypeDateTimeV2::cast_to_date_v2(cast_value, res_data[i]);
+            }
+        }
+    }
+
+    static int get_last_month_day(int year, int month) {
+        bool is_leap_year = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);

Review Comment:
   Use doris::is_leap



##########
be/src/exprs/timestamp_functions.cpp:
##########
@@ -707,6 +707,38 @@ DateTimeVal TimestampFunctions::from_days(FunctionContext* ctx, const IntVal& da
     return ts_val;
 }
 
+DateTimeVal TimestampFunctions::last_day(FunctionContext* ctx, const DateTimeVal& ts_val) {
+    if (ts_val.is_null) {
+        return DateTimeVal::null();
+    }
+
+    DateTimeValue ts_value = DateTimeValue::from_datetime_val(ts_val);
+
+    bool is_leap_year = (ts_value.year() % 400 == 0) ||

Review Comment:
   You can use doris::is_leap(year)



##########
be/src/exprs/timestamp_functions.cpp:
##########
@@ -707,6 +707,38 @@ DateTimeVal TimestampFunctions::from_days(FunctionContext* ctx, const IntVal& da
     return ts_val;
 }
 
+DateTimeVal TimestampFunctions::last_day(FunctionContext* ctx, const DateTimeVal& ts_val) {
+    if (ts_val.is_null) {
+        return DateTimeVal::null();
+    }
+
+    DateTimeValue ts_value = DateTimeValue::from_datetime_val(ts_val);
+
+    bool is_leap_year = (ts_value.year() % 400 == 0) ||

Review Comment:
   Same as get_last_month_day?



-- 
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: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [doris] lsy3993 commented on a diff in pull request #13400: [function](date function)add new date function 'last_day'

Posted by GitBox <gi...@apache.org>.
lsy3993 commented on code in PR #13400:
URL: https://github.com/apache/doris/pull/13400#discussion_r997857113


##########
regression-test/suites/query_p0/sql_functions/datetime_functions/test_date_function.groovy:
##########
@@ -424,4 +424,30 @@ suite("test_date_function") {
     qt_sql """ select minutes_sub(test_time2,1) result from ${tableName}; """
     //seconds_sub
     qt_sql """ select seconds_sub(test_time2,1) result from ${tableName}; """
+
+    // test last_day
+    sql """ SET enable_vectorized_engine = TRUE; """

Review Comment:
   done



-- 
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: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [doris] lsy3993 commented on a diff in pull request #13400: [function](date function)add new date function 'last_day'

Posted by GitBox <gi...@apache.org>.
lsy3993 commented on code in PR #13400:
URL: https://github.com/apache/doris/pull/13400#discussion_r996558643


##########
be/src/vec/functions/function_timestamp.cpp:
##########
@@ -569,6 +569,164 @@ class FunctionUnixTimestamp : public IFunction {
     }
 };
 
+template <typename Impl, typename DateType>
+class FunctionDateOrDateTimeToDate : public IFunction {
+public:
+    static constexpr auto name = Impl::name;
+    static FunctionPtr create() {
+        return std::make_shared<FunctionDateOrDateTimeToDate<Impl, DateType>>();
+    }
+
+    String get_name() const override { return name; }
+
+    bool use_default_implementation_for_nulls() const override { return true; }
+
+    size_t get_number_of_arguments() const override { return 1; }
+
+    bool is_variadic() const override { return true; }
+
+    // input DateTime and Date, return Date
+    // input DateTimeV2 and DateV2, return DateV2
+    DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) const override {
+        if constexpr (std::is_same_v<DateType, DataTypeDateTime>) {
+            return make_nullable(std::make_shared<DataTypeDate>());
+        } else if constexpr (std::is_same_v<DateType, DataTypeDate>) {
+            return make_nullable(std::make_shared<DataTypeDate>());
+        } else if constexpr (std::is_same_v<DateType, DataTypeDateV2>) {
+            return make_nullable(std::make_shared<DataTypeDateV2>());
+        } else {
+            return make_nullable(std::make_shared<DataTypeDateV2>());
+        }
+    }
+
+    DataTypes get_variadic_argument_types_impl() const override {
+        if constexpr (std::is_same_v<DateType, DataTypeDateTime>) {
+            return {std::make_shared<DataTypeDate>()};
+        } else if constexpr (std::is_same_v<DateType, DataTypeDateV2>) {
+            return {std::make_shared<DataTypeDateV2>()};
+        } else {
+            return {std::make_shared<DataTypeDateTimeV2>()};
+        }
+    }
+
+    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
+                        size_t result, size_t input_rows_count) override {
+        return Impl::execute_impl(context, block, arguments, result, input_rows_count);
+    }
+};
+
+template <typename DateType>
+struct LastDayImpl {
+    static constexpr auto name = "last_day";
+
+    static Status execute_impl(FunctionContext* context, Block& block,
+                               const ColumnNumbers& arguments, size_t result,
+                               size_t input_rows_count) {
+        auto null_map = ColumnUInt8::create(input_rows_count, 0);
+        ColumnPtr res_column;
+        ColumnPtr argument_column =
+                block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
+        if constexpr (std::is_same_v<DateType, DataTypeDateTime>) {
+            auto data_col = assert_cast<const ColumnVector<Int64>*>(argument_column.get());
+            res_column = ColumnInt64::create(input_rows_count);
+            execute_straight<VecDateTimeValue, Int64, Int64>(
+                    input_rows_count, null_map->get_data(), data_col->get_data(),
+                    static_cast<ColumnVector<Int64>*>(res_column->assume_mutable().get())
+                            ->get_data());
+
+        } else if constexpr (std::is_same_v<DateType, DataTypeDateV2>) {
+            auto data_col = assert_cast<const ColumnVector<UInt32>*>(argument_column.get());
+            res_column = ColumnVector<UInt32>::create(input_rows_count);
+            execute_straight<DateV2Value<DateV2ValueType>, UInt32, UInt32>(
+                    input_rows_count, null_map->get_data(), data_col->get_data(),
+                    static_cast<ColumnVector<UInt32>*>(res_column->assume_mutable().get())
+                            ->get_data());
+
+        } else if constexpr (std::is_same_v<DateType, DataTypeDateTimeV2>) {
+            auto data_col = assert_cast<const ColumnVector<UInt64>*>(argument_column.get());
+            res_column = ColumnVector<UInt32>::create(input_rows_count);
+            execute_straight<DateV2Value<DateTimeV2ValueType>, UInt32, UInt64>(
+                    input_rows_count, null_map->get_data(), data_col->get_data(),
+                    static_cast<ColumnVector<UInt32>*>(res_column->assume_mutable().get())
+                            ->get_data());
+        } else {
+            // neither DateTime nor DateTimeV2/DateV2, return null
+            if constexpr (std::is_same_v<DateType, DataTypeDateTime>) {
+                res_column = ColumnInt64::create(input_rows_count);
+            } else {
+                res_column = ColumnVector<UInt32>::create(input_rows_count);
+            }
+
+            null_map = ColumnUInt8::create(input_rows_count, 1);
+            block.replace_by_position(
+                    result, ColumnNullable::create(std::move(res_column), std::move(null_map)));
+            return Status::OK();
+        }
+
+        block.replace_by_position(
+                result, ColumnNullable::create(std::move(res_column), std::move(null_map)));
+        return Status::OK();
+    }
+
+    template <typename DateValueType, typename ReturnType, typename InputDateType>
+    static void execute_straight(size_t input_rows_count, NullMap& null_map,
+                                 const PaddedPODArray<InputDateType>& data_col,
+                                 PaddedPODArray<ReturnType>& res_data) {
+        for (int i = 0; i < input_rows_count; i++) {
+            if constexpr (std::is_same_v<DateValueType, VecDateTimeValue>) {
+                const auto& cur_data = data_col[i];
+                auto ts_value = binary_cast<Int64, VecDateTimeValue>(cur_data);
+                int day = get_last_month_day(ts_value.year(), ts_value.month());
+                ts_value.set_time(ts_value.year(), ts_value.month(), day, 0, 0, 0);
+                ts_value.set_type(TIME_DATE);
+                if (!ts_value.is_valid_date()) {
+                    null_map[i] = 1;
+                    continue;
+                }
+                res_data[i] = binary_cast<VecDateTimeValue, Int64>(ts_value);
+
+            } else if constexpr (std::is_same_v<DateValueType, DateV2Value<DateV2ValueType>>) {
+                const auto& cur_data = data_col[i];
+                auto ts_value = binary_cast<UInt32, DateValueType>(cur_data);
+                int day = get_last_month_day(ts_value.year(), ts_value.month());
+                ts_value.template set_time_unit<TimeUnit::DAY>(day);
+                if (!ts_value.is_valid_date()) {
+                    null_map[i] = 1;
+                    continue;
+                }
+                res_data[i] = binary_cast<DateValueType, UInt32>(ts_value);
+
+            } else {
+                const auto& cur_data = data_col[i];
+                auto ts_value = binary_cast<UInt64, DateValueType>(cur_data);
+                int day = get_last_month_day(ts_value.year(), ts_value.month());
+                ts_value.template set_time_unit<TimeUnit::DAY>(day);
+                ts_value.set_time(ts_value.year(), ts_value.month(), day, 0, 0, 0, 0);
+                if (!ts_value.is_valid_date()) {
+                    null_map[i] = 1;
+                    continue;
+                }
+                UInt64 cast_value = binary_cast<DateValueType, UInt64>(ts_value);
+                DataTypeDateTimeV2::cast_to_date_v2(cast_value, res_data[i]);
+            }
+        }
+    }
+
+    static int get_last_month_day(int year, int month) {
+        bool is_leap_year = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);

Review Comment:
   done



##########
be/src/exprs/timestamp_functions.cpp:
##########
@@ -707,6 +707,38 @@ DateTimeVal TimestampFunctions::from_days(FunctionContext* ctx, const IntVal& da
     return ts_val;
 }
 
+DateTimeVal TimestampFunctions::last_day(FunctionContext* ctx, const DateTimeVal& ts_val) {
+    if (ts_val.is_null) {
+        return DateTimeVal::null();
+    }
+
+    DateTimeValue ts_value = DateTimeValue::from_datetime_val(ts_val);
+
+    bool is_leap_year = (ts_value.year() % 400 == 0) ||

Review Comment:
   done



-- 
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: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [doris] lsy3993 commented on a diff in pull request #13400: [function](date function)add new date function 'last_day'

Posted by GitBox <gi...@apache.org>.
lsy3993 commented on code in PR #13400:
URL: https://github.com/apache/doris/pull/13400#discussion_r997858941


##########
be/src/vec/functions/function_timestamp.cpp:
##########
@@ -569,6 +569,164 @@ class FunctionUnixTimestamp : public IFunction {
     }
 };
 
+template <typename Impl, typename DateType>
+class FunctionDateOrDateTimeToDate : public IFunction {
+public:
+    static constexpr auto name = Impl::name;
+    static FunctionPtr create() {
+        return std::make_shared<FunctionDateOrDateTimeToDate<Impl, DateType>>();
+    }
+
+    String get_name() const override { return name; }
+
+    bool use_default_implementation_for_nulls() const override { return true; }
+
+    size_t get_number_of_arguments() const override { return 1; }
+
+    bool is_variadic() const override { return true; }
+
+    // input DateTime and Date, return Date
+    // input DateTimeV2 and DateV2, return DateV2
+    DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) const override {
+        if constexpr (std::is_same_v<DateType, DataTypeDateTime>) {
+            return make_nullable(std::make_shared<DataTypeDate>());
+        } else if constexpr (std::is_same_v<DateType, DataTypeDate>) {
+            return make_nullable(std::make_shared<DataTypeDate>());
+        } else if constexpr (std::is_same_v<DateType, DataTypeDateV2>) {
+            return make_nullable(std::make_shared<DataTypeDateV2>());
+        } else {
+            return make_nullable(std::make_shared<DataTypeDateV2>());
+        }
+    }
+
+    DataTypes get_variadic_argument_types_impl() const override {
+        if constexpr (std::is_same_v<DateType, DataTypeDateTime>) {
+            return {std::make_shared<DataTypeDate>()};
+        } else if constexpr (std::is_same_v<DateType, DataTypeDateV2>) {
+            return {std::make_shared<DataTypeDateV2>()};
+        } else {
+            return {std::make_shared<DataTypeDateTimeV2>()};
+        }
+    }
+
+    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
+                        size_t result, size_t input_rows_count) override {
+        return Impl::execute_impl(context, block, arguments, result, input_rows_count);
+    }
+};
+
+template <typename DateType>
+struct LastDayImpl {
+    static constexpr auto name = "last_day";
+
+    static Status execute_impl(FunctionContext* context, Block& block,
+                               const ColumnNumbers& arguments, size_t result,
+                               size_t input_rows_count) {
+        auto null_map = ColumnUInt8::create(input_rows_count, 0);
+        ColumnPtr res_column;
+        ColumnPtr argument_column =
+                block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
+        if constexpr (std::is_same_v<DateType, DataTypeDateTime>) {
+            auto data_col = assert_cast<const ColumnVector<Int64>*>(argument_column.get());
+            res_column = ColumnInt64::create(input_rows_count);
+            execute_straight<VecDateTimeValue, Int64, Int64>(
+                    input_rows_count, null_map->get_data(), data_col->get_data(),
+                    static_cast<ColumnVector<Int64>*>(res_column->assume_mutable().get())
+                            ->get_data());
+
+        } else if constexpr (std::is_same_v<DateType, DataTypeDateV2>) {
+            auto data_col = assert_cast<const ColumnVector<UInt32>*>(argument_column.get());
+            res_column = ColumnVector<UInt32>::create(input_rows_count);
+            execute_straight<DateV2Value<DateV2ValueType>, UInt32, UInt32>(
+                    input_rows_count, null_map->get_data(), data_col->get_data(),
+                    static_cast<ColumnVector<UInt32>*>(res_column->assume_mutable().get())
+                            ->get_data());
+
+        } else if constexpr (std::is_same_v<DateType, DataTypeDateTimeV2>) {
+            auto data_col = assert_cast<const ColumnVector<UInt64>*>(argument_column.get());
+            res_column = ColumnVector<UInt32>::create(input_rows_count);
+            execute_straight<DateV2Value<DateTimeV2ValueType>, UInt32, UInt64>(
+                    input_rows_count, null_map->get_data(), data_col->get_data(),
+                    static_cast<ColumnVector<UInt32>*>(res_column->assume_mutable().get())
+                            ->get_data());
+        } else {
+            // neither DateTime nor DateTimeV2/DateV2, return null
+            if constexpr (std::is_same_v<DateType, DataTypeDateTime>) {
+                res_column = ColumnInt64::create(input_rows_count);

Review Comment:
   you are right, I delete the 'else'



-- 
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: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [doris] zhangstar333 commented on a diff in pull request #13400: [function](date function)add new date function 'last_day'

Posted by GitBox <gi...@apache.org>.
zhangstar333 commented on code in PR #13400:
URL: https://github.com/apache/doris/pull/13400#discussion_r996918671


##########
be/src/vec/functions/function_timestamp.cpp:
##########
@@ -569,6 +569,164 @@ class FunctionUnixTimestamp : public IFunction {
     }
 };
 
+template <typename Impl, typename DateType>
+class FunctionDateOrDateTimeToDate : public IFunction {
+public:
+    static constexpr auto name = Impl::name;
+    static FunctionPtr create() {
+        return std::make_shared<FunctionDateOrDateTimeToDate<Impl, DateType>>();
+    }
+
+    String get_name() const override { return name; }
+
+    bool use_default_implementation_for_nulls() const override { return true; }
+
+    size_t get_number_of_arguments() const override { return 1; }
+
+    bool is_variadic() const override { return true; }
+
+    // input DateTime and Date, return Date
+    // input DateTimeV2 and DateV2, return DateV2
+    DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) const override {
+        if constexpr (std::is_same_v<DateType, DataTypeDateTime>) {
+            return make_nullable(std::make_shared<DataTypeDate>());
+        } else if constexpr (std::is_same_v<DateType, DataTypeDate>) {
+            return make_nullable(std::make_shared<DataTypeDate>());
+        } else if constexpr (std::is_same_v<DateType, DataTypeDateV2>) {
+            return make_nullable(std::make_shared<DataTypeDateV2>());
+        } else {
+            return make_nullable(std::make_shared<DataTypeDateV2>());
+        }
+    }
+
+    DataTypes get_variadic_argument_types_impl() const override {
+        if constexpr (std::is_same_v<DateType, DataTypeDateTime>) {
+            return {std::make_shared<DataTypeDate>()};
+        } else if constexpr (std::is_same_v<DateType, DataTypeDateV2>) {
+            return {std::make_shared<DataTypeDateV2>()};
+        } else {
+            return {std::make_shared<DataTypeDateTimeV2>()};
+        }
+    }
+
+    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
+                        size_t result, size_t input_rows_count) override {
+        return Impl::execute_impl(context, block, arguments, result, input_rows_count);
+    }
+};
+
+template <typename DateType>
+struct LastDayImpl {
+    static constexpr auto name = "last_day";
+
+    static Status execute_impl(FunctionContext* context, Block& block,
+                               const ColumnNumbers& arguments, size_t result,
+                               size_t input_rows_count) {
+        auto null_map = ColumnUInt8::create(input_rows_count, 0);
+        ColumnPtr res_column;
+        ColumnPtr argument_column =
+                block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
+        if constexpr (std::is_same_v<DateType, DataTypeDateTime>) {
+            auto data_col = assert_cast<const ColumnVector<Int64>*>(argument_column.get());
+            res_column = ColumnInt64::create(input_rows_count);
+            execute_straight<VecDateTimeValue, Int64, Int64>(
+                    input_rows_count, null_map->get_data(), data_col->get_data(),
+                    static_cast<ColumnVector<Int64>*>(res_column->assume_mutable().get())
+                            ->get_data());
+
+        } else if constexpr (std::is_same_v<DateType, DataTypeDateV2>) {
+            auto data_col = assert_cast<const ColumnVector<UInt32>*>(argument_column.get());
+            res_column = ColumnVector<UInt32>::create(input_rows_count);
+            execute_straight<DateV2Value<DateV2ValueType>, UInt32, UInt32>(
+                    input_rows_count, null_map->get_data(), data_col->get_data(),
+                    static_cast<ColumnVector<UInt32>*>(res_column->assume_mutable().get())
+                            ->get_data());
+
+        } else if constexpr (std::is_same_v<DateType, DataTypeDateTimeV2>) {
+            auto data_col = assert_cast<const ColumnVector<UInt64>*>(argument_column.get());
+            res_column = ColumnVector<UInt32>::create(input_rows_count);
+            execute_straight<DateV2Value<DateTimeV2ValueType>, UInt32, UInt64>(
+                    input_rows_count, null_map->get_data(), data_col->get_data(),
+                    static_cast<ColumnVector<UInt32>*>(res_column->assume_mutable().get())
+                            ->get_data());
+        } else {
+            // neither DateTime nor DateTimeV2/DateV2, return null
+            if constexpr (std::is_same_v<DateType, DataTypeDateTime>) {
+                res_column = ColumnInt64::create(input_rows_count);

Review Comment:
   this check seems to have been done at the above



-- 
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: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [doris] Gabriel39 commented on a diff in pull request #13400: [function](date function)add new date function 'last_day'

Posted by GitBox <gi...@apache.org>.
Gabriel39 commented on code in PR #13400:
URL: https://github.com/apache/doris/pull/13400#discussion_r996914151


##########
be/src/vec/functions/function_timestamp.cpp:
##########
@@ -569,6 +569,164 @@ class FunctionUnixTimestamp : public IFunction {
     }
 };
 
+template <typename Impl, typename DateType>
+class FunctionDateOrDateTimeToDate : public IFunction {
+public:
+    static constexpr auto name = Impl::name;
+    static FunctionPtr create() {
+        return std::make_shared<FunctionDateOrDateTimeToDate<Impl, DateType>>();
+    }
+
+    String get_name() const override { return name; }
+
+    bool use_default_implementation_for_nulls() const override { return true; }

Review Comment:
   `use_default_implementation_for_constants` also need to override and set to true



##########
be/src/vec/functions/function_timestamp.cpp:
##########
@@ -569,6 +569,164 @@ class FunctionUnixTimestamp : public IFunction {
     }
 };
 
+template <typename Impl, typename DateType>
+class FunctionDateOrDateTimeToDate : public IFunction {
+public:
+    static constexpr auto name = Impl::name;
+    static FunctionPtr create() {
+        return std::make_shared<FunctionDateOrDateTimeToDate<Impl, DateType>>();
+    }
+
+    String get_name() const override { return name; }
+
+    bool use_default_implementation_for_nulls() const override { return true; }
+
+    size_t get_number_of_arguments() const override { return 1; }
+
+    bool is_variadic() const override { return true; }
+
+    // input DateTime and Date, return Date
+    // input DateTimeV2 and DateV2, return DateV2
+    DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) const override {
+        if constexpr (std::is_same_v<DateType, DataTypeDateTime>) {

Review Comment:
   Use `or` to collapse these two conditions



##########
regression-test/suites/query_p0/sql_functions/datetime_functions/test_date_function.groovy:
##########
@@ -424,4 +424,30 @@ suite("test_date_function") {
     qt_sql """ select minutes_sub(test_time2,1) result from ${tableName}; """
     //seconds_sub
     qt_sql """ select seconds_sub(test_time2,1) result from ${tableName}; """
+
+    // test last_day
+    sql """ SET enable_vectorized_engine = TRUE; """

Review Comment:
   also add a case for row-based engine



##########
be/src/vec/functions/function_timestamp.cpp:
##########
@@ -569,6 +569,164 @@ class FunctionUnixTimestamp : public IFunction {
     }
 };
 
+template <typename Impl, typename DateType>
+class FunctionDateOrDateTimeToDate : public IFunction {
+public:
+    static constexpr auto name = Impl::name;
+    static FunctionPtr create() {
+        return std::make_shared<FunctionDateOrDateTimeToDate<Impl, DateType>>();
+    }
+
+    String get_name() const override { return name; }
+
+    bool use_default_implementation_for_nulls() const override { return true; }
+
+    size_t get_number_of_arguments() const override { return 1; }
+
+    bool is_variadic() const override { return true; }
+
+    // input DateTime and Date, return Date
+    // input DateTimeV2 and DateV2, return DateV2
+    DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) const override {
+        if constexpr (std::is_same_v<DateType, DataTypeDateTime>) {
+            return make_nullable(std::make_shared<DataTypeDate>());
+        } else if constexpr (std::is_same_v<DateType, DataTypeDate>) {
+            return make_nullable(std::make_shared<DataTypeDate>());
+        } else if constexpr (std::is_same_v<DateType, DataTypeDateV2>) {
+            return make_nullable(std::make_shared<DataTypeDateV2>());
+        } else {
+            return make_nullable(std::make_shared<DataTypeDateV2>());
+        }
+    }
+
+    DataTypes get_variadic_argument_types_impl() const override {
+        if constexpr (std::is_same_v<DateType, DataTypeDateTime>) {

Review Comment:
   `DataTypeDate` is also possible as input argument



-- 
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: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [doris] lsy3993 commented on a diff in pull request #13400: [function](date function)add new date function 'last_day'

Posted by GitBox <gi...@apache.org>.
lsy3993 commented on code in PR #13400:
URL: https://github.com/apache/doris/pull/13400#discussion_r997857358


##########
be/src/vec/functions/function_timestamp.cpp:
##########
@@ -569,6 +569,164 @@ class FunctionUnixTimestamp : public IFunction {
     }
 };
 
+template <typename Impl, typename DateType>
+class FunctionDateOrDateTimeToDate : public IFunction {
+public:
+    static constexpr auto name = Impl::name;
+    static FunctionPtr create() {
+        return std::make_shared<FunctionDateOrDateTimeToDate<Impl, DateType>>();
+    }
+
+    String get_name() const override { return name; }
+
+    bool use_default_implementation_for_nulls() const override { return true; }

Review Comment:
   done



-- 
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: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [doris] lsy3993 closed pull request #13400: [function](date function)add new date function 'last_day'

Posted by GitBox <gi...@apache.org>.
lsy3993 closed pull request #13400: [function](date function)add new date function 'last_day'
URL: https://github.com/apache/doris/pull/13400


-- 
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: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org