Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 148 additions & 0 deletions be/src/vec/functions/date_format_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,154 @@ struct yyyyMMImpl {
}
};

struct dd_HHImpl {
constexpr static size_t row_size = 5;
template <typename DateType>
size_t static date_to_str(const DateType& date_value, char* buf) {
int i = 0;
put_two_digits(date_value.day(), buf, i);
buf[i++] = ' ';
put_two_digits(date_value.hour(), buf, i);
return i;
}
};

struct dd_HH_mmImpl {
constexpr static size_t row_size = 8;
template <typename DateType>
size_t static date_to_str(const DateType& date_value, char* buf) {
int i = 0;
put_two_digits(date_value.day(), buf, i);
buf[i++] = ' ';
put_two_digits(date_value.hour(), buf, i);
buf[i++] = ':';
put_two_digits(date_value.minute(), buf, i);
return i;
}
};

struct dd_HH_mm_ssImpl {
constexpr static size_t row_size = 11;
template <typename DateType>
size_t static date_to_str(const DateType& date_value, char* buf) {
int i = 0;
put_two_digits(date_value.day(), buf, i);
buf[i++] = ' ';
put_two_digits(date_value.hour(), buf, i);
buf[i++] = ':';
put_two_digits(date_value.minute(), buf, i);
buf[i++] = ':';
put_two_digits(date_value.second(), buf, i);
return i;
}
};

struct dd_HH_mm_ss_SSSSSSImpl {
constexpr static size_t row_size = 18;
template <typename DateType>
size_t static date_to_str(const DateType& date_value, char* buf) {
int i = 0;
put_two_digits(date_value.day(), buf, i);
buf[i++] = ' ';
put_two_digits(date_value.hour(), buf, i);
buf[i++] = ':';
put_two_digits(date_value.minute(), buf, i);
buf[i++] = ':';
put_two_digits(date_value.second(), buf, i);
buf[i++] = '.';
int length = common::count_digits_fast(date_value.microsecond());
std::fill(buf + i, buf + i + 6 - length, '0');
std::to_chars(buf + i + 6 - length, buf + i + 6, date_value.microsecond());
return i + 6;
}
};

struct HH_mmImpl {
constexpr static size_t row_size = 5;
template <typename DateType>
size_t static date_to_str(const DateType& date_value, char* buf) {
int i = 0;
put_two_digits(date_value.hour(), buf, i);
buf[i++] = ':';
put_two_digits(date_value.minute(), buf, i);
return i;
}
};

struct HH_mm_ssImpl {
constexpr static size_t row_size = 8;
template <typename DateType>
size_t static date_to_str(const DateType& date_value, char* buf) {
int i = 0;
put_two_digits(date_value.hour(), buf, i);
buf[i++] = ':';
put_two_digits(date_value.minute(), buf, i);
buf[i++] = ':';
put_two_digits(date_value.second(), buf, i);
return i;
}
};

struct HH_mm_ss_SSSSSSImpl {
constexpr static size_t row_size = 15;
template <typename DateType>
size_t static date_to_str(const DateType& date_value, char* buf) {
int i = 0;
put_two_digits(date_value.hour(), buf, i);
buf[i++] = ':';
put_two_digits(date_value.minute(), buf, i);
buf[i++] = ':';
put_two_digits(date_value.second(), buf, i);
buf[i++] = '.';
int length = common::count_digits_fast(date_value.microsecond());
std::fill(buf + i, buf + i + 6 - length, '0');
std::to_chars(buf + i + 6 - length, buf + i + 6, date_value.microsecond());
return i + 6;
}
};

struct mm_ssImpl {
constexpr static size_t row_size = 5;
template <typename DateType>
size_t static date_to_str(const DateType& date_value, char* buf) {
int i = 0;
put_two_digits(date_value.minute(), buf, i);
buf[i++] = ':';
put_two_digits(date_value.second(), buf, i);
return i;
}
};

struct mm_ss_SSSSSSImpl {
constexpr static size_t row_size = 12;
template <typename DateType>
size_t static date_to_str(const DateType& date_value, char* buf) {
int i = 0;
put_two_digits(date_value.minute(), buf, i);
buf[i++] = ':';
put_two_digits(date_value.second(), buf, i);
buf[i++] = '.';
int length = common::count_digits_fast(date_value.microsecond());
std::fill(buf + i, buf + i + 6 - length, '0');
std::to_chars(buf + i + 6 - length, buf + i + 6, date_value.microsecond());
return i + 6;
}
};

struct ss_SSSSSSImpl {
constexpr static size_t row_size = 9;
template <typename DateType>
size_t static date_to_str(const DateType& date_value, char* buf) {
int i = 0;
put_two_digits(date_value.second(), buf, i);
buf[i++] = '.';
int length = common::count_digits_fast(date_value.microsecond());
std::fill(buf + i, buf + i + 6 - length, '0');
std::to_chars(buf + i + 6 - length, buf + i + 6, date_value.microsecond());
return i + 6;
}
};

struct yyyyImpl {
constexpr static size_t row_size = 4;
template <typename DateType>
Expand Down
47 changes: 47 additions & 0 deletions be/src/vec/functions/date_time_transforms.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,53 @@ struct MonthNameImpl {
}
};

template <typename FormatImpl, const char* FuncName>
struct DateTimeV2FormatImpl {
static constexpr PrimitiveType OpArgType = TYPE_DATETIMEV2;
using ArgType = typename PrimitiveTypeTraits<TYPE_DATETIMEV2>::CppType;
static constexpr auto name = FuncName;
static constexpr auto max_size = FormatImpl::row_size;

static auto execute(const ArgType& dt, ColumnString::Chars& res_data, size_t& offset,
const char* const* /*names_ptr*/, FunctionContext* /*context*/) {
auto* buf = reinterpret_cast<char*>(&res_data[offset]);
offset += FormatImpl::date_to_str(dt, buf);
return offset;
}

static DataTypes get_variadic_argument_types() {
return {std::make_shared<typename PrimitiveTypeTraits<TYPE_DATETIMEV2>::DataType>()};
}
};

inline constexpr char kYearMonthName[] = "year_month";
inline constexpr char kDayHourName[] = "day_hour";
inline constexpr char kDayMinuteName[] = "day_minute";
inline constexpr char kDaySecondName[] = "day_second";
inline constexpr char kDayMicrosecondName[] = "day_microsecond";
inline constexpr char kHourMinuteName[] = "hour_minute";
inline constexpr char kHourSecondName[] = "hour_second";
inline constexpr char kHourMicrosecondName[] = "hour_microsecond";
inline constexpr char kMinuteSecondName[] = "minute_second";
inline constexpr char kMinuteMicrosecondName[] = "minute_microsecond";
inline constexpr char kSecondMicrosecondName[] = "second_microsecond";

using YearMonthImpl = DateTimeV2FormatImpl<time_format_type::yyyy_MMImpl, kYearMonthName>;
using DayHourImpl = DateTimeV2FormatImpl<time_format_type::dd_HHImpl, kDayHourName>;
using DayMinuteImpl = DateTimeV2FormatImpl<time_format_type::dd_HH_mmImpl, kDayMinuteName>;
using DaySecondImpl = DateTimeV2FormatImpl<time_format_type::dd_HH_mm_ssImpl, kDaySecondName>;
using DayMicrosecondImpl =
DateTimeV2FormatImpl<time_format_type::dd_HH_mm_ss_SSSSSSImpl, kDayMicrosecondName>;
using HourMinuteImpl = DateTimeV2FormatImpl<time_format_type::HH_mmImpl, kHourMinuteName>;
using HourSecondImpl = DateTimeV2FormatImpl<time_format_type::HH_mm_ssImpl, kHourSecondName>;
using HourMicrosecondImpl =
DateTimeV2FormatImpl<time_format_type::HH_mm_ss_SSSSSSImpl, kHourMicrosecondName>;
using MinuteSecondImpl = DateTimeV2FormatImpl<time_format_type::mm_ssImpl, kMinuteSecondName>;
using MinuteMicrosecondImpl =
DateTimeV2FormatImpl<time_format_type::mm_ss_SSSSSSImpl, kMinuteMicrosecondName>;
using SecondMicrosecondImpl =
DateTimeV2FormatImpl<time_format_type::ss_SSSSSSImpl, kSecondMicrosecondName>;

template <PrimitiveType PType>
struct DateFormatImpl {
using DateType = typename PrimitiveTypeTraits<PType>::CppType;
Expand Down
32 changes: 31 additions & 1 deletion be/src/vec/functions/datetime_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ template <typename DateValueType>
datelike_to_string<DateValueType>(arg0), delta);
}

// Throw for operations with a datelike and a string interval(e.g., date_add(date, interval '-1 2:3:4' day_second))
template <typename DateValueType>
[[noreturn]] inline void throw_out_of_bound_date_string(const char* op, DateValueType arg0,
std::string_view delta) {
throw Exception(ErrorCode::OUT_OF_BOUND, "Operation {} of {}, {} out of range", op,
datelike_to_string<DateValueType>(arg0), delta);
}

// Throw for operations with a single integer argument (e.g., from_days daynr)
[[noreturn]] inline void throw_out_of_bound_int(const char* op, int64_t value) {
throw Exception(ErrorCode::OUT_OF_BOUND, "Operation {} of {} out of range", op, value);
Expand Down Expand Up @@ -111,7 +119,7 @@ template <typename DateValueType>
}

// Helper to get time unit name for error messages
inline const char* get_time_unit_name(TimeUnit unit) {
constexpr const char* get_time_unit_name(TimeUnit unit) {
switch (unit) {
case TimeUnit::YEAR:
return "year_add";
Expand All @@ -133,6 +141,28 @@ inline const char* get_time_unit_name(TimeUnit unit) {
return "millisecond_add";
case TimeUnit::MICROSECOND:
return "microsecond_add";
case TimeUnit::YEAR_MONTH:
return "year_month_add";
case TimeUnit::DAY_HOUR:
return "day_hour_add";
case TimeUnit::DAY_MINUTE:
return "day_minute_add";
case TimeUnit::DAY_SECOND:
return "day_second_add";
case TimeUnit::DAY_MICROSECOND:
return "day_microsecond_add";
case TimeUnit::HOUR_MINUTE:
return "hour_minute_add";
case TimeUnit::HOUR_SECOND:
return "hour_second_add";
case TimeUnit::HOUR_MICROSECOND:
return "hour_microsecond_add";
case TimeUnit::MINUTE_SECOND:
return "minute_second_add";
case TimeUnit::MINUTE_MICROSECOND:
return "minute_microsecond_add";
case TimeUnit::SECOND_MICROSECOND:
return "second_microsecond_add";
default:
return "date_add";
}
Expand Down
63 changes: 39 additions & 24 deletions be/src/vec/functions/function_date_or_datetime_computation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,30 @@ using FunctionTimestamptzAddMonths =
FunctionDateOrDateTimeComputation<AddMonthsImpl<TYPE_TIMESTAMPTZ>>;
using FunctionTimestamptzAddYears =
FunctionDateOrDateTimeComputation<AddYearsImpl<TYPE_TIMESTAMPTZ>>;
#define FUNCTION_TIME_UNION_CAL(TYPE) \
using FunctionDatetimeAdd##TYPE = \
FunctionDateOrDateTimeComputation<Add##TYPE##Impl<TYPE_DATETIMEV2>>; \
using FunctionDatetimeSub##TYPE = \
FunctionDateOrDateTimeComputation<Subtract##TYPE##Impl<TYPE_DATETIMEV2>>; \
using FunctionTimestamptzAdd##TYPE = \
FunctionDateOrDateTimeComputation<Add##TYPE##Impl<TYPE_TIMESTAMPTZ>>; \
using FunctionTimestamptzSub##TYPE = \
FunctionDateOrDateTimeComputation<Subtract##TYPE##Impl<TYPE_TIMESTAMPTZ>>;

FUNCTION_TIME_UNION_CAL(SecondMicrosecond);
FUNCTION_TIME_UNION_CAL(MinuteMicrosecond);
FUNCTION_TIME_UNION_CAL(MinuteSecond);
FUNCTION_TIME_UNION_CAL(HourMicrosecond);
FUNCTION_TIME_UNION_CAL(HourSecond);
FUNCTION_TIME_UNION_CAL(HourMinute);
FUNCTION_TIME_UNION_CAL(DayMicrosecond);
FUNCTION_TIME_UNION_CAL(DaySecond);
FUNCTION_TIME_UNION_CAL(DayMinute);
FUNCTION_TIME_UNION_CAL(DayHour);
FUNCTION_TIME_UNION_CAL(YearMonth);

using FunctionDatetimeAddQuarters =
FunctionDateOrDateTimeComputation<AddQuartersImpl<TYPE_DATETIMEV2>>;
using FunctionDatetimeAddDaySecond =
FunctionDateOrDateTimeComputation<AddDaySecondImpl<TYPE_DATETIMEV2>>;
using FunctionDatetimeAddDayHour =
FunctionDateOrDateTimeComputation<AddDayHourImpl<TYPE_DATETIMEV2>>;
using FunctionDatetimeAddMinuteSecond =
FunctionDateOrDateTimeComputation<AddMinuteSecondImpl<TYPE_DATETIMEV2>>;
using FunctionDatetimeAddSecondMicrosecond =
FunctionDateOrDateTimeComputation<AddSecondMicrosecondImpl<TYPE_DATETIMEV2>>;
using FunctionDatetimeSubMicroseconds =
FunctionDateOrDateTimeComputation<SubtractMicrosecondsImpl<TYPE_DATETIMEV2>>;
using FunctionDatetimeSubMilliseconds =
Expand All @@ -139,14 +152,6 @@ using FunctionDatetimeSubYears =

using FunctionTimestamptzAddQuarters =
FunctionDateOrDateTimeComputation<AddQuartersImpl<TYPE_TIMESTAMPTZ>>;
using FunctionTimestamptzAddDaySecond =
FunctionDateOrDateTimeComputation<AddDaySecondImpl<TYPE_TIMESTAMPTZ>>;
using FunctionTimestamptzAddDayHour =
FunctionDateOrDateTimeComputation<AddDayHourImpl<TYPE_TIMESTAMPTZ>>;
using FunctionTimestamptzAddMinuteSecond =
FunctionDateOrDateTimeComputation<AddMinuteSecondImpl<TYPE_TIMESTAMPTZ>>;
using FunctionTimestamptzAddSecondMicrosecond =
FunctionDateOrDateTimeComputation<AddSecondMicrosecondImpl<TYPE_TIMESTAMPTZ>>;
using FunctionTimestamptzSubMicroseconds =
FunctionDateOrDateTimeComputation<SubtractMicrosecondsImpl<TYPE_TIMESTAMPTZ>>;
using FunctionTimestamptzSubMilliseconds =
Expand Down Expand Up @@ -250,10 +255,6 @@ void register_function_date_time_computation(SimpleFunctionFactory& factory) {
factory.register_function<FunctionDatetimeAddMonths>();
factory.register_function<FunctionDatetimeAddYears>();
factory.register_function<FunctionDatetimeAddQuarters>();
factory.register_function<FunctionDatetimeAddDaySecond>();
factory.register_function<FunctionDatetimeAddDayHour>();
factory.register_function<FunctionDatetimeAddMinuteSecond>();
factory.register_function<FunctionDatetimeAddSecondMicrosecond>();
factory.register_function<FunctionTimestamptzAddMicroseconds>();
factory.register_function<FunctionTimestamptzAddMilliseconds>();
factory.register_function<FunctionTimestamptzAddSeconds>();
Expand All @@ -264,10 +265,24 @@ void register_function_date_time_computation(SimpleFunctionFactory& factory) {
factory.register_function<FunctionTimestamptzAddMonths>();
factory.register_function<FunctionTimestamptzAddYears>();
factory.register_function<FunctionTimestamptzAddQuarters>();
factory.register_function<FunctionTimestamptzAddDaySecond>();
factory.register_function<FunctionTimestamptzAddDayHour>();
factory.register_function<FunctionTimestamptzAddMinuteSecond>();
factory.register_function<FunctionTimestamptzAddSecondMicrosecond>();

#define REGISTER_TIME_UNION_CAL(TYPE) \
factory.register_function<FunctionDatetimeAdd##TYPE>(); \
factory.register_function<FunctionDatetimeSub##TYPE>(); \
factory.register_function<FunctionTimestamptzAdd##TYPE>(); \
factory.register_function<FunctionTimestamptzSub##TYPE>();

REGISTER_TIME_UNION_CAL(SecondMicrosecond);
REGISTER_TIME_UNION_CAL(MinuteMicrosecond);
REGISTER_TIME_UNION_CAL(MinuteSecond);
REGISTER_TIME_UNION_CAL(HourMicrosecond);
REGISTER_TIME_UNION_CAL(HourSecond);
REGISTER_TIME_UNION_CAL(HourMinute);
REGISTER_TIME_UNION_CAL(DayMicrosecond);
REGISTER_TIME_UNION_CAL(DaySecond);
REGISTER_TIME_UNION_CAL(DayMinute);
REGISTER_TIME_UNION_CAL(DayHour);
REGISTER_TIME_UNION_CAL(YearMonth);

factory.register_function<FunctionSubDays>();
factory.register_function<FunctionSubMonths>();
Expand Down
Loading
Loading