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/17 10:59:38 UTC

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

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