diff --git a/be/src/runtime/datetime_value.h b/be/src/runtime/datetime_value.h index d821e1f19e53d7..b5d6f333650cc8 100644 --- a/be/src/runtime/datetime_value.h +++ b/be/src/runtime/datetime_value.h @@ -278,6 +278,7 @@ class DateTimeValue { int year() const { return _year; } int month() const { return _month; } + int quarter() const { return (_month - 1) / 3 + 1; } int day() const { return _day; } int hour() const { return _hour; } int minute() const { return _minute; } @@ -310,6 +311,7 @@ class DateTimeValue { // Weekday, from 0(Mon) to 6(Sun) inline uint8_t weekday() const { return calc_weekday(daynr(), false); } + inline auto day_of_week() const { return (weekday() + 1) % 7 + 1; } // The bits in week_format has the following meaning: // WEEK_MONDAY_FIRST (0) diff --git a/be/src/vec/CMakeLists.txt b/be/src/vec/CMakeLists.txt index 40f8fc96af1df3..05f1abf378565f 100644 --- a/be/src/vec/CMakeLists.txt +++ b/be/src/vec/CMakeLists.txt @@ -97,6 +97,8 @@ set(VEC_FILES functions/divide.cpp functions/is_null.cpp functions/is_not_null.cpp + functions/to_time_function.cpp + functions/time_of_function.cpp sink/mysql_result_writer.cpp sink/result_sink.cpp sink/vdata_stream_sender.cpp diff --git a/be/src/vec/functions/date_time_transforms.h b/be/src/vec/functions/date_time_transforms.h new file mode 100644 index 00000000000000..df9f0fdd5b3e26 --- /dev/null +++ b/be/src/vec/functions/date_time_transforms.h @@ -0,0 +1,699 @@ +// 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. + +#pragma once + +#include "runtime/datetime_value.h" + +#include "vec/core/types.h" +#include "vec/common/exception.h" +#include "vec/columns/column_vector.h" +#include "vec/functions/function_helpers.h" +//#include "vec/functions/extract_time_zone_from_function_arguments.h> + + +namespace doris::vectorized +{ + +namespace ErrorCodes +{ + extern const int ILLEGAL_TYPE_OF_ARGUMENT; + extern const int ILLEGAL_COLUMN; +} + +/** Transformations. + * Represents two functions - from datetime (UInt32) and from date (UInt16). + * + * Also, the "factor transformation" F is defined for the T transformation. + * This is a transformation of F such that its value identifies the region of monotonicity for T + * (for a fixed value of F, the transformation T is monotonic). + * + * Or, figuratively, if T is similar to taking the remainder of division, then F is similar to division. + * + * Example: for transformation T "get the day number in the month" (2015-02-03 -> 3), + * factor-transformation F is "round to the nearest month" (2015-02-03 -> 2015-02-01). + */ + +static inline UInt32 dateIsNotSupported(const char * name) +{ + throw Exception("Illegal type Date of argument for function " + std::string(name), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT); +} + +///// This factor transformation will say that the function is monotone everywhere. +//struct ZeroTransform +//{ +// static inline UInt16 execute(UInt32, const DateLUTImpl &) { return 0; } +// static inline UInt16 execute(UInt16, const DateLUTImpl &) { return 0; } +//}; + +//struct ToDateImpl +//{ +// static constexpr auto name = "toDate"; +// +// static inline UInt16 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return UInt16(time_zone.toDayNum(t)); +// } +// static inline UInt16 execute(UInt16 d, const DateLUTImpl &) +// { +// return d; +// } +// +// using FactorTransform = ZeroTransform; +//}; +// +//struct ToStartOfDayImpl +//{ +// static constexpr auto name = "toStartOfDay"; +// +// static inline UInt32 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return time_zone.toDate(t); +// } +// static inline UInt32 execute(UInt16 d, const DateLUTImpl & time_zone) +// { +// return time_zone.toDate(DayNum(d)); +// } +// +// using FactorTransform = ZeroTransform; +//}; +// +//struct ToMondayImpl +//{ +// static constexpr auto name = "toMonday"; +// +// static inline UInt16 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return time_zone.toFirstDayNumOfWeek(time_zone.toDayNum(t)); +// } +// static inline UInt16 execute(UInt16 d, const DateLUTImpl & time_zone) +// { +// return time_zone.toFirstDayNumOfWeek(DayNum(d)); +// } +// +// using FactorTransform = ZeroTransform; +//}; +// +//struct ToStartOfMonthImpl +//{ +// static constexpr auto name = "toStartOfMonth"; +// +// static inline UInt16 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return time_zone.toFirstDayNumOfMonth(time_zone.toDayNum(t)); +// } +// static inline UInt16 execute(UInt16 d, const DateLUTImpl & time_zone) +// { +// return time_zone.toFirstDayNumOfMonth(DayNum(d)); +// } +// +// using FactorTransform = ZeroTransform; +//}; +// +//struct ToStartOfQuarterImpl +//{ +// static constexpr auto name = "toStartOfQuarter"; +// +// static inline UInt16 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return time_zone.toFirstDayNumOfQuarter(time_zone.toDayNum(t)); +// } +// static inline UInt16 execute(UInt16 d, const DateLUTImpl & time_zone) +// { +// return time_zone.toFirstDayNumOfQuarter(DayNum(d)); +// } +// +// using FactorTransform = ZeroTransform; +//}; +// +//struct ToStartOfYearImpl +//{ +// static constexpr auto name = "toStartOfYear"; +// +// static inline UInt16 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return time_zone.toFirstDayNumOfYear(time_zone.toDayNum(t)); +// } +// static inline UInt16 execute(UInt16 d, const DateLUTImpl & time_zone) +// { +// return time_zone.toFirstDayNumOfYear(DayNum(d)); +// } +// +// using FactorTransform = ZeroTransform; +//}; +// +// +//struct ToTimeImpl +//{ +// static constexpr auto name = "toTime"; +// +// /// When transforming to time, the date will be equated to 1970-01-02. +// static inline UInt32 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return time_zone.toTime(t) + 86400; +// } +// +// static inline UInt32 execute(UInt16, const DateLUTImpl &) +// { +// return dateIsNotSupported(name); +// } +// +// using FactorTransform = ToDateImpl; +//}; +// +//struct ToStartOfMinuteImpl +//{ +// static constexpr auto name = "toStartOfMinute"; +// +// static inline UInt32 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return time_zone.toStartOfMinute(t); +// } +// static inline UInt32 execute(UInt16, const DateLUTImpl &) +// { +// return dateIsNotSupported(name); +// } +// +// using FactorTransform = ZeroTransform; +//}; +// +//struct ToStartOfFiveMinuteImpl +//{ +// static constexpr auto name = "toStartOfFiveMinute"; +// +// static inline UInt32 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return time_zone.toStartOfFiveMinute(t); +// } +// static inline UInt32 execute(UInt16, const DateLUTImpl &) +// { +// return dateIsNotSupported(name); +// } +// +// using FactorTransform = ZeroTransform; +//}; +// +//struct ToStartOfTenMinutesImpl +//{ +// static constexpr auto name = "toStartOfTenMinutes"; +// +// static inline UInt32 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return time_zone.toStartOfTenMinutes(t); +// } +// static inline UInt32 execute(UInt16, const DateLUTImpl &) +// { +// return dateIsNotSupported(name); +// } +// +// using FactorTransform = ZeroTransform; +//}; +// +//struct ToStartOfFifteenMinutesImpl +//{ +// static constexpr auto name = "toStartOfFifteenMinutes"; +// +// static inline UInt32 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return time_zone.toStartOfFifteenMinutes(t); +// } +// static inline UInt32 execute(UInt16, const DateLUTImpl &) +// { +// return dateIsNotSupported(name); +// } +// +// using FactorTransform = ZeroTransform; +//}; +// +///// Round to start of half-an-hour length interval with unspecified offset. This transform is specific for Yandex.Metrica. +//struct TimeSlotImpl +//{ +// static constexpr auto name = "timeSlot"; +// +// static inline UInt32 execute(UInt32 t, const DateLUTImpl &) +// { +// return t / 1800 * 1800; +// } +// +// static inline UInt32 execute(UInt16, const DateLUTImpl &) +// { +// return dateIsNotSupported(name); +// } +// +// using FactorTransform = ZeroTransform; +//}; +// +//struct ToStartOfHourImpl +//{ +// static constexpr auto name = "toStartOfHour"; +// +// static inline UInt32 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return time_zone.toStartOfHour(t); +// } +// +// static inline UInt32 execute(UInt16, const DateLUTImpl &) +// { +// return dateIsNotSupported(name); +// } +// +// using FactorTransform = ZeroTransform; +//}; + +#define TIME_FUNCTION_IMPL(CLASS, UNIT, FUNCTION) \ +struct CLASS \ +{ \ + static constexpr auto name = #UNIT; \ + static inline auto execute(const Int128& t) { \ + const auto& date_time_value = (doris::DateTimeValue&)(t); \ + return date_time_value.FUNCTION; \ + } \ +} + +#define TO_TIME_FUNCTION(CLASS, UNIT) TIME_FUNCTION_IMPL(CLASS, UNIT, UNIT()) + +TO_TIME_FUNCTION(ToYearImpl, year); +TO_TIME_FUNCTION(ToQuarterImpl, quarter); +TO_TIME_FUNCTION(ToMonthImpl, month); +TO_TIME_FUNCTION(ToDayImpl, day); +TO_TIME_FUNCTION(ToHourImpl, hour); +TO_TIME_FUNCTION(ToMinuteImpl, minute); +TO_TIME_FUNCTION(ToSecondImpl, second); + +TIME_FUNCTION_IMPL(WeekOfYearImpl, weekofyear, week(mysql_week_mode(3))); +TIME_FUNCTION_IMPL(DayOfYearImpl, dayofyear, day_of_year()); +TIME_FUNCTION_IMPL(DayOfMonthImpl, dayofmonth, day()); +TIME_FUNCTION_IMPL(DayOfWeekImpl, dayofweek, day_of_week()); + +//struct ToQuarterImpl +//{ +// static constexpr auto name = "toQuarter"; +// +// static inline UInt8 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return time_zone.toQuarter(t); +// } +// static inline UInt8 execute(UInt16 d, const DateLUTImpl & time_zone) +// { +// return time_zone.toQuarter(DayNum(d)); +// } +// +// using FactorTransform = ToStartOfYearImpl; +//}; +// +//struct ToMonthImpl +//{ +// static constexpr auto name = "toMonth"; +// +// static inline UInt8 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return time_zone.toMonth(t); +// } +// static inline UInt8 execute(UInt16 d, const DateLUTImpl & time_zone) +// { +// return time_zone.toMonth(DayNum(d)); +// } +// +// using FactorTransform = ToStartOfYearImpl; +//}; +// +//struct ToDayOfMonthImpl +//{ +// static constexpr auto name = "toDayOfMonth"; +// +// static inline UInt8 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return time_zone.toDayOfMonth(t); +// } +// static inline UInt8 execute(UInt16 d, const DateLUTImpl & time_zone) +// { +// return time_zone.toDayOfMonth(DayNum(d)); +// } +// +// using FactorTransform = ToStartOfMonthImpl; +//}; +// +//struct ToDayOfWeekImpl +//{ +// static constexpr auto name = "toDayOfWeek"; +// +// static inline UInt8 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return time_zone.toDayOfWeek(t); +// } +// static inline UInt8 execute(UInt16 d, const DateLUTImpl & time_zone) +// { +// return time_zone.toDayOfWeek(DayNum(d)); +// } +// +// using FactorTransform = ToMondayImpl; +//}; +// +//struct ToDayOfYearImpl +//{ +// static constexpr auto name = "toDayOfYear"; +// +// static inline UInt16 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return time_zone.toDayOfYear(t); +// } +// static inline UInt16 execute(UInt16 d, const DateLUTImpl & time_zone) +// { +// return time_zone.toDayOfYear(DayNum(d)); +// } +// +// using FactorTransform = ToStartOfYearImpl; +//}; +// +//struct ToHourImpl +//{ +// static constexpr auto name = "toHour"; +// +// static inline UInt8 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return time_zone.toHour(t); +// } +// +// static inline UInt8 execute(UInt16, const DateLUTImpl &) +// { +// return dateIsNotSupported(name); +// } +// +// using FactorTransform = ToDateImpl; +//}; +// +//struct ToMinuteImpl +//{ +// static constexpr auto name = "toMinute"; +// +// static inline UInt8 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return time_zone.toMinute(t); +// } +// static inline UInt8 execute(UInt16, const DateLUTImpl &) +// { +// return dateIsNotSupported(name); +// } +// +// using FactorTransform = ToStartOfHourImpl; +//}; +// +//struct ToSecondImpl +//{ +// static constexpr auto name = "toSecond"; +// +// static inline UInt8 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return time_zone.toSecond(t); +// } +// static inline UInt8 execute(UInt16, const DateLUTImpl &) +// { +// return dateIsNotSupported(name); +// } +// +// using FactorTransform = ToStartOfMinuteImpl; +//}; +// +//struct ToISOYearImpl +//{ +// static constexpr auto name = "toISOYear"; +// +// static inline UInt16 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return time_zone.toISOYear(time_zone.toDayNum(t)); +// } +// static inline UInt16 execute(UInt16 d, const DateLUTImpl & time_zone) +// { +// return time_zone.toISOYear(DayNum(d)); +// } +// +// using FactorTransform = ZeroTransform; +//}; +// +//struct ToStartOfISOYearImpl +//{ +// static constexpr auto name = "toStartOfISOYear"; +// +// static inline UInt16 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return time_zone.toFirstDayNumOfISOYear(time_zone.toDayNum(t)); +// } +// static inline UInt16 execute(UInt16 d, const DateLUTImpl & time_zone) +// { +// return time_zone.toFirstDayNumOfISOYear(DayNum(d)); +// } +// +// using FactorTransform = ZeroTransform; +//}; +// +//struct ToISOWeekImpl +//{ +// static constexpr auto name = "toISOWeek"; +// +// static inline UInt8 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return time_zone.toISOWeek(time_zone.toDayNum(t)); +// } +// static inline UInt8 execute(UInt16 d, const DateLUTImpl & time_zone) +// { +// return time_zone.toISOWeek(DayNum(d)); +// } +// +// using FactorTransform = ToISOYearImpl; +//}; +// +//struct ToRelativeYearNumImpl +//{ +// static constexpr auto name = "toRelativeYearNum"; +// +// static inline UInt16 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return time_zone.toYear(t); +// } +// static inline UInt16 execute(UInt16 d, const DateLUTImpl & time_zone) +// { +// return time_zone.toYear(DayNum(d)); +// } +// +// using FactorTransform = ZeroTransform; +//}; +// +//struct ToRelativeQuarterNumImpl +//{ +// static constexpr auto name = "toRelativeQuarterNum"; +// +// static inline UInt16 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return time_zone.toRelativeQuarterNum(t); +// } +// static inline UInt16 execute(UInt16 d, const DateLUTImpl & time_zone) +// { +// return time_zone.toRelativeQuarterNum(DayNum(d)); +// } +// +// using FactorTransform = ZeroTransform; +//}; +// +//struct ToRelativeMonthNumImpl +//{ +// static constexpr auto name = "toRelativeMonthNum"; +// +// static inline UInt16 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return time_zone.toRelativeMonthNum(t); +// } +// static inline UInt16 execute(UInt16 d, const DateLUTImpl & time_zone) +// { +// return time_zone.toRelativeMonthNum(DayNum(d)); +// } +// +// using FactorTransform = ZeroTransform; +//}; +// +//struct ToRelativeWeekNumImpl +//{ +// static constexpr auto name = "toRelativeWeekNum"; +// +// static inline UInt16 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return time_zone.toRelativeWeekNum(t); +// } +// static inline UInt16 execute(UInt16 d, const DateLUTImpl & time_zone) +// { +// return time_zone.toRelativeWeekNum(DayNum(d)); +// } +// +// using FactorTransform = ZeroTransform; +//}; +// +//struct ToRelativeDayNumImpl +//{ +// static constexpr auto name = "toRelativeDayNum"; +// +// static inline UInt16 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return time_zone.toDayNum(t); +// } +// static inline UInt16 execute(UInt16 d, const DateLUTImpl &) +// { +// return static_cast(d); +// } +// +// using FactorTransform = ZeroTransform; +//}; +// +// +//struct ToRelativeHourNumImpl +//{ +// static constexpr auto name = "toRelativeHourNum"; +// +// static inline UInt32 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return time_zone.toRelativeHourNum(t); +// } +// static inline UInt32 execute(UInt16 d, const DateLUTImpl & time_zone) +// { +// return time_zone.toRelativeHourNum(DayNum(d)); +// } +// +// using FactorTransform = ZeroTransform; +//}; +// +//struct ToRelativeMinuteNumImpl +//{ +// static constexpr auto name = "toRelativeMinuteNum"; +// +// static inline UInt32 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return time_zone.toRelativeMinuteNum(t); +// } +// static inline UInt32 execute(UInt16 d, const DateLUTImpl & time_zone) +// { +// return time_zone.toRelativeMinuteNum(DayNum(d)); +// } +// +// using FactorTransform = ZeroTransform; +//}; +// +//struct ToRelativeSecondNumImpl +//{ +// static constexpr auto name = "toRelativeSecondNum"; +// +// static inline UInt32 execute(UInt32 t, const DateLUTImpl &) +// { +// return t; +// } +// static inline UInt32 execute(UInt16 d, const DateLUTImpl & time_zone) +// { +// return time_zone.fromDayNum(DayNum(d)); +// } +// +// using FactorTransform = ZeroTransform; +//}; +// +//struct ToYYYYMMImpl +//{ +// static constexpr auto name = "toYYYYMM"; +// +// static inline UInt32 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return time_zone.toNumYYYYMM(t); +// } +// static inline UInt32 execute(UInt16 d, const DateLUTImpl & time_zone) +// { +// return time_zone.toNumYYYYMM(static_cast(d)); +// } +// +// using FactorTransform = ZeroTransform; +//}; +// +//struct ToYYYYMMDDImpl +//{ +// static constexpr auto name = "toYYYYMMDD"; +// +// static inline UInt32 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return time_zone.toNumYYYYMMDD(t); +// } +// static inline UInt32 execute(UInt16 d, const DateLUTImpl & time_zone) +// { +// return time_zone.toNumYYYYMMDD(static_cast(d)); +// } +// +// using FactorTransform = ZeroTransform; +//}; +// +//struct ToYYYYMMDDhhmmssImpl +//{ +// static constexpr auto name = "toYYYYMMDDhhmmss"; +// +// static inline UInt64 execute(UInt32 t, const DateLUTImpl & time_zone) +// { +// return time_zone.toNumYYYYMMDDhhmmss(t); +// } +// static inline UInt64 execute(UInt16 d, const DateLUTImpl & time_zone) +// { +// return time_zone.toNumYYYYMMDDhhmmss(time_zone.toDate(static_cast(d))); +// } +// +// using FactorTransform = ZeroTransform; +//}; +// +// +template +struct Transformer +{ +// static void vector(const PaddedPODArray & vec_from, PaddedPODArray & vec_to, const DateLUTImpl & time_zone) + static void vector(const PaddedPODArray & vec_from, PaddedPODArray & vec_to) + { + size_t size = vec_from.size(); + vec_to.resize(size); + + for (size_t i = 0; i < size; ++i) +// vec_to[i] = Transform::execute(vec_from[i], time_zone); + vec_to[i] = Transform::execute(vec_from[i]); + } +}; + + +template +struct DateTimeTransformImpl +{ + static void execute(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/) + { + using Op = Transformer; + +// const DateLUTImpl & time_zone = extractTimeZoneFromFunctionArguments(block, arguments, 1, 0); + + const ColumnPtr source_col = block.getByPosition(arguments[0]).column; + if (const auto * sources = checkAndGetColumn>(source_col.get())) + { + auto col_to = ColumnVector::create(); +// Op::vector(sources->getData(), col_to->getData(), time_zone); + Op::vector(sources->getData(), col_to->getData()); + block.getByPosition(result).column = std::move(col_to); + } + else + { + throw Exception("Illegal column " + block.getByPosition(arguments[0]).column->getName() + + " of first argument of function " + Transform::name, + ErrorCodes::ILLEGAL_COLUMN); + } + } +}; + +} \ No newline at end of file diff --git a/be/src/vec/functions/function_date_or_datetime_to_something.h b/be/src/vec/functions/function_date_or_datetime_to_something.h new file mode 100644 index 00000000000000..a64e5562c2ac4e --- /dev/null +++ b/be/src/vec/functions/function_date_or_datetime_to_something.h @@ -0,0 +1,153 @@ +// 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/data_types/data_type_date.h" +#include "vec/data_types/data_type_date_time.h" +#include "vec/functions/function.h" +//#include "vec/functions/extract_time_zone_from_function_arguments.h" +#include "vec/functions/date_time_transforms.h" + + +namespace doris::vectorized +{ + +namespace ErrorCodes +{ + extern const int ILLEGAL_TYPE_OF_ARGUMENT; + extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH; +} + + +/// See DateTimeTransforms.h +template +class FunctionDateOrDateTimeToSomething : public IFunction +{ +public: + static constexpr auto name = Transform::name; + static FunctionPtr create() { + return std::make_shared(); + } + + String getName() const override + { + return name; + } + + bool isVariadic() const override { return true; } + size_t getNumberOfArguments() const override { return 0; } + + DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override + { + if (arguments.size() == 1) + { + if (!isDateOrDateTime(arguments[0].type)) + throw Exception( + "Illegal type " + arguments[0].type->getName() + " of argument of function " + getName() + + ". Should be a date or a date with time", + ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT); + } + else if (arguments.size() == 2) + { + if (!isDateOrDateTime(arguments[0].type)) + throw Exception( + "Illegal type " + arguments[0].type->getName() + " of argument of function " + getName() + + ". Should be a date or a date with time", + ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT); + if (!isString(arguments[1].type)) + throw Exception( + "Function " + getName() + " supports 1 or 2 arguments. The 1st argument " + "must be of type Date or DateTime. The 2nd argument (optional) must be " + "a constant string with timezone name", + ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT); + if (isDate(arguments[0].type) && std::is_same_v) + throw Exception( + "The timezone argument of function " + getName() + " is allowed only when the 1st argument has the type DateTime", + ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT); + } + else + throw Exception( + "Number of arguments for function " + getName() + " doesn't match: passed " + std::to_string(arguments.size()) + + ", should be 1 or 2", + ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH); + + /// For DateTime, if time zone is specified, attach it to type. +// if (std::is_same_v) +// return std::make_shared(extractTimeZoneNameFromFunctionArguments(arguments, 1, 0)); +// else + return std::make_shared(); + } + + bool useDefaultImplementationForConstants() const override { return true; } + ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {1}; } + + Status executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t input_rows_count) override + { + const IDataType * from_type = block.getByPosition(arguments[0]).type.get(); + WhichDataType which(from_type); + +// if (which.isDate()) +// DateTimeTransformImpl::execute(block, arguments, result, input_rows_count); +// else if (which.isDateTime()) + DateTimeTransformImpl::execute(block, arguments, result, input_rows_count); +// else +// throw Exception("Illegal type " + block.getByPosition(arguments[0]).type->getName() + " of argument of function " + getName(), +// ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT); + return Status::OK(); + } + + + bool hasInformationAboutMonotonicity() const override + { + return true; + } + +// Monotonicity getMonotonicityForRange(const IDataType & type, const Field & left, const Field & right) const override +// { +// IFunction::Monotonicity is_monotonic { true }; +// IFunction::Monotonicity is_not_monotonic; +// +// if (std::is_same_v) +// { +// is_monotonic.is_always_monotonic = true; +// return is_monotonic; +// } +// +// /// This method is called only if the function has one argument. Therefore, we do not care about the non-local time zone. +// const DateLUTImpl & date_lut = DateLUT::instance(); +// +// if (left.isNull() || right.isNull()) +// return is_not_monotonic; +// +// /// The function is monotonous on the [left, right] segment, if the factor transformation returns the same values for them. +// +// if (checkAndGetDataType(&type)) +// { +// return Transform::FactorTransform::execute(UInt16(left.get()), date_lut) +// == Transform::FactorTransform::execute(UInt16(right.get()), date_lut) +// ? is_monotonic : is_not_monotonic; +// } +// else +// { +// return Transform::FactorTransform::execute(UInt32(left.get()), date_lut) +// == Transform::FactorTransform::execute(UInt32(right.get()), date_lut) +// ? is_monotonic : is_not_monotonic; +// } +// } +}; + +} + diff --git a/be/src/vec/functions/function_string_to_string.h b/be/src/vec/functions/function_string_to_string.h index 1aee83aa5fc9b0..1542a7844fe9bc 100644 --- a/be/src/vec/functions/function_string_to_string.h +++ b/be/src/vec/functions/function_string_to_string.h @@ -1,4 +1,3 @@ - // 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 diff --git a/be/src/vec/functions/hll_hash.cpp b/be/src/vec/functions/hll_hash.cpp index 6938dc148cc376..8fe3046d168e07 100644 --- a/be/src/vec/functions/hll_hash.cpp +++ b/be/src/vec/functions/hll_hash.cpp @@ -1,4 +1,3 @@ - // 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 diff --git a/be/src/vec/functions/simple_function_factory.h b/be/src/vec/functions/simple_function_factory.h index 393adfa8ed0f05..25b1a25e3a3e8e 100644 --- a/be/src/vec/functions/simple_function_factory.h +++ b/be/src/vec/functions/simple_function_factory.h @@ -41,7 +41,17 @@ void registerFunctionModulo(SimpleFunctionFactory& factory); void registerFunctionBitmap(SimpleFunctionFactory& factory); void registerFunctionIsNull(SimpleFunctionFactory& factory); void registerFunctionIsNotNull(SimpleFunctionFactory& factory); - +void registerFunctionYear(SimpleFunctionFactory& factory); +void registerFunctionDay(SimpleFunctionFactory& factory); +void registerFunctionMonth(SimpleFunctionFactory& factory); +void registerFunctionQuarter(SimpleFunctionFactory& factory); +void registerFunctionHour(SimpleFunctionFactory& factory); +void registerFunctionMinute(SimpleFunctionFactory& factory); +void registerFunctionSecond(SimpleFunctionFactory& factory); +void registerFunctionWeekOfYear(SimpleFunctionFactory& factory); +void registerFunctionDayOfYear(SimpleFunctionFactory& factory); +void registerFunctionDayOfWeek(SimpleFunctionFactory& factory); +void registerFunctionDayOfMonth(SimpleFunctionFactory& factory); class SimpleFunctionFactory { using Creator = std::function; @@ -95,6 +105,17 @@ class SimpleFunctionFactory { registerFunctionModulo(instance); registerFunctionIsNull(instance); registerFunctionIsNotNull(instance); + registerFunctionYear(instance); + registerFunctionDay(instance); + registerFunctionMonth(instance); + registerFunctionQuarter(instance); + registerFunctionHour(instance); + registerFunctionMinute(instance); + registerFunctionSecond(instance); + registerFunctionWeekOfYear(instance); + registerFunctionDayOfYear(instance); + registerFunctionDayOfWeek(instance); + registerFunctionDayOfMonth(instance); }); return instance; } diff --git a/be/src/vec/functions/time_of_function.cpp b/be/src/vec/functions/time_of_function.cpp new file mode 100644 index 00000000000000..b6bf16d48be846 --- /dev/null +++ b/be/src/vec/functions/time_of_function.cpp @@ -0,0 +1,37 @@ +// 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 + +#include "vec/data_types/data_type_number.h" +#include "vec/functions/date_time_transforms.h" +#include "vec/functions/function_date_or_datetime_to_something.h" +#include "vec/functions/simple_function_factory.h" + +namespace doris::vectorized { + +#define REGISTER_TIME_FUNCTION(CLASS, IMPL) \ +using CLASS = FunctionDateOrDateTimeToSomething; \ +void register##CLASS(SimpleFunctionFactory& factory) { \ + factory.registerFunction(); \ +} + +REGISTER_TIME_FUNCTION(FunctionWeekOfYear, WeekOfYearImpl); +REGISTER_TIME_FUNCTION(FunctionDayOfYear, DayOfYearImpl); +REGISTER_TIME_FUNCTION(FunctionDayOfWeek, DayOfWeekImpl); +REGISTER_TIME_FUNCTION(FunctionDayOfMonth, DayOfMonthImpl); +} \ No newline at end of file diff --git a/be/src/vec/functions/to_time_function.cpp b/be/src/vec/functions/to_time_function.cpp new file mode 100644 index 00000000000000..8689f9b0eb4c35 --- /dev/null +++ b/be/src/vec/functions/to_time_function.cpp @@ -0,0 +1,39 @@ + +// 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/data_types/data_type_number.h" +#include "vec/functions/date_time_transforms.h" +#include "vec/functions/function_date_or_datetime_to_something.h" +#include "vec/functions/simple_function_factory.h" + +namespace doris::vectorized { + +#define REGISTER_TIME_FUNCTION(CLASS, IMPL) \ +using CLASS = FunctionDateOrDateTimeToSomething; \ +void register##CLASS(SimpleFunctionFactory& factory) { \ + factory.registerFunction(); \ +} + +REGISTER_TIME_FUNCTION(FunctionYear, ToYearImpl); +REGISTER_TIME_FUNCTION(FunctionQuarter, ToQuarterImpl); +REGISTER_TIME_FUNCTION(FunctionMonth, ToMonthImpl); +REGISTER_TIME_FUNCTION(FunctionDay, ToDayImpl); +REGISTER_TIME_FUNCTION(FunctionHour, ToHourImpl); +REGISTER_TIME_FUNCTION(FunctionMinute, ToMinuteImpl); +REGISTER_TIME_FUNCTION(FunctionSecond, ToSecondImpl); +} diff --git a/gensrc/script/doris_builtins_functions.py b/gensrc/script/doris_builtins_functions.py index 48bfd95df7f3ca..015c57f17d0c03 100755 --- a/gensrc/script/doris_builtins_functions.py +++ b/gensrc/script/doris_builtins_functions.py @@ -149,22 +149,22 @@ '_ZN5doris18TimestampFunctions7to_daysEPN9doris_udf15FunctionContextERKNS1_11DateTimeValE'], [['year'], 'INT', ['DATETIME'], - '_ZN5doris18TimestampFunctions4yearEPN9doris_udf15FunctionContextERKNS1_11DateTimeValE'], + '_ZN5doris18TimestampFunctions4yearEPN9doris_udf15FunctionContextERKNS1_11DateTimeValE', 'vec'], [['month'], 'INT', ['DATETIME'], - '_ZN5doris18TimestampFunctions5monthEPN9doris_udf15FunctionContextERKNS1_11DateTimeValE'], + '_ZN5doris18TimestampFunctions5monthEPN9doris_udf15FunctionContextERKNS1_11DateTimeValE', 'vec'], [['quarter'], 'INT', ['DATETIME'], - '_ZN5doris18TimestampFunctions7quarterEPN9doris_udf15FunctionContextERKNS1_11DateTimeValE'], + '_ZN5doris18TimestampFunctions7quarterEPN9doris_udf15FunctionContextERKNS1_11DateTimeValE', 'vec'], [['dayofweek'], 'INT', ['DATETIME'], - '_ZN5doris18TimestampFunctions11day_of_weekEPN9doris_udf15FunctionContextERKNS1_11DateTimeValE'], + '_ZN5doris18TimestampFunctions11day_of_weekEPN9doris_udf15FunctionContextERKNS1_11DateTimeValE', 'vec'], [['day', 'dayofmonth'], 'INT', ['DATETIME'], '_ZN5doris18TimestampFunctions12day_of_monthEPN9doris_udf' - '15FunctionContextERKNS1_11DateTimeValE'], + '15FunctionContextERKNS1_11DateTimeValE', 'vec'], [['dayofyear'], 'INT', ['DATETIME'], '_ZN5doris18TimestampFunctions11day_of_yearEPN9doris_udf' - '15FunctionContextERKNS1_11DateTimeValE'], + '15FunctionContextERKNS1_11DateTimeValE', 'vec'], [['weekofyear'], 'INT', ['DATETIME'], '_ZN5doris18TimestampFunctions12week_of_yearEPN9doris_udf' - '15FunctionContextERKNS1_11DateTimeValE'], + '15FunctionContextERKNS1_11DateTimeValE', 'vec'], [['yearweek'], 'INT', ['DATETIME'], '_ZN5doris18TimestampFunctions9year_weekEPN9doris_udf15FunctionContextERKNS1_11DateTimeValE'], [['yearweek'], 'INT', ['DATETIME', 'INT'], @@ -174,11 +174,11 @@ [['week'], 'INT', ['DATETIME', 'INT'], '_ZN5doris18TimestampFunctions4weekEPN9doris_udf15FunctionContextERKNS1_11DateTimeValERKNS1_6IntValE'], [['hour'], 'INT', ['DATETIME'], - '_ZN5doris18TimestampFunctions4hourEPN9doris_udf15FunctionContextERKNS1_11DateTimeValE'], + '_ZN5doris18TimestampFunctions4hourEPN9doris_udf15FunctionContextERKNS1_11DateTimeValE', 'vec'], [['minute'], 'INT', ['DATETIME'], - '_ZN5doris18TimestampFunctions6minuteEPN9doris_udf15FunctionContextERKNS1_11DateTimeValE'], + '_ZN5doris18TimestampFunctions6minuteEPN9doris_udf15FunctionContextERKNS1_11DateTimeValE', 'vec'], [['second'], 'INT', ['DATETIME'], - '_ZN5doris18TimestampFunctions6secondEPN9doris_udf15FunctionContextERKNS1_11DateTimeValE'], + '_ZN5doris18TimestampFunctions6secondEPN9doris_udf15FunctionContextERKNS1_11DateTimeValE', 'vec'], [['makedate'], 'DATETIME', ['INT', 'INT'], '_ZN5doris18TimestampFunctions9make_dateEPN9doris_udf15FunctionContextERKNS1_6IntValES6_'],