Skip to content

Commit

Permalink
[refactor](datatype) Introduce a toolkits for clarify datelike types …
Browse files Browse the repository at this point in the history
…usage (#26810)
  • Loading branch information
zclllyybb authored Nov 22, 2023
1 parent 19c36dc commit a68b62b
Show file tree
Hide file tree
Showing 5 changed files with 280 additions and 432 deletions.
61 changes: 44 additions & 17 deletions be/src/util/datetype_cast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,67 +17,94 @@

#pragma once

#include <type_traits>

#include "vec/columns/columns_number.h"
#include "vec/core/types.h"
#include "vec/data_types/data_type_date.h"
#include "vec/data_types/data_type_date_time.h"
#include "vec/data_types/data_type_time_v2.h"
#include "vec/runtime/vdatetime_value.h"

/*
* We use these function family to clarify our types of datelike type. for example:
* DataTypeDate -------------------> ColumnDate -----------------------> Int64
* | TypeToColumn ValueTypeOfColumn
* | TypeToValueType
* VecDateTimeValue
*/
namespace doris::date_cast {

// DataTypeDate -> ColumnDate
// e.g. DataTypeDate -> ColumnDate
template <typename DataType>
struct DatatypeToColumn {};
struct TypeToColumn {};
template <>
struct DatatypeToColumn<vectorized::DataTypeDate> {
struct TypeToColumn<vectorized::DataTypeDate> {
using type = vectorized::ColumnDate;
};
template <>
struct DatatypeToColumn<vectorized::DataTypeDateTime> {
struct TypeToColumn<vectorized::DataTypeDateTime> {
using type = vectorized::ColumnDateTime;
};
template <>
struct DatatypeToColumn<vectorized::DataTypeDateV2> {
struct TypeToColumn<vectorized::DataTypeDateV2> {
using type = vectorized::ColumnDateV2;
};
template <>
struct DatatypeToColumn<vectorized::DataTypeDateTimeV2> {
struct TypeToColumn<vectorized::DataTypeDateTimeV2> {
using type = vectorized::ColumnDateTimeV2;
};

template <typename DataType>
using DateToColumnV = DatatypeToColumn<DataType>::type;
using TypeToColumnV = TypeToColumn<DataType>::type;

// DateTypeDate -> VecDateTimeValue
// e.g. DateTypeDate -> VecDateTimeValue
template <typename DataType>
struct DateToDateValueType {};
struct TypeToValueType {};
template <>
struct DateToDateValueType<vectorized::DataTypeDate> {
struct TypeToValueType<vectorized::DataTypeDate> {
using type = VecDateTimeValue;
};
template <>
struct DateToDateValueType<vectorized::DataTypeDateTime> {
struct TypeToValueType<vectorized::DataTypeDateTime> {
using type = VecDateTimeValue;
};
template <>
struct DateToDateValueType<vectorized::DataTypeDateV2> {
struct TypeToValueType<vectorized::DataTypeDateV2> {
using type = DateV2Value<DateV2ValueType>;
};
template <>
struct DateToDateValueType<vectorized::DataTypeDateTimeV2> {
struct TypeToValueType<vectorized::DataTypeDateTimeV2> {
using type = DateV2Value<DateTimeV2ValueType>;
};

template <typename DataType>
using DateToDateValueTypeV = DateToDateValueType<DataType>::type;
using TypeToValueTypeV = TypeToValueType<DataType>::type;

// ColumnDate -> Int64 (see also columns_number.h)
// e.g. ColumnDate -> Int64 (see also columns_number.h)
template <typename ColumnType>
requires requires { typename ColumnType::value_type; }
struct ValueTypeOfDateColumn {
struct ValueTypeOfColumn {
using type = ColumnType::value_type;
};

template <typename ColumnType>
using ValueTypeOfDateColumnV = ValueTypeOfDateColumn<ColumnType>::type;
using ValueTypeOfColumnV = ValueTypeOfColumn<ColumnType>::type;

// check V1 or V2 for both DateType/ColumnType/ValueType/ValueOfColumnType
template <typename Type>
constexpr bool IsV1() {
return static_cast<bool>(std::is_same_v<Type, vectorized::DataTypeDate> ||
std::is_same_v<Type, vectorized::DataTypeDateTime> ||
std::is_same_v<Type, VecDateTimeValue> ||
std::is_same_v<Type, vectorized::ColumnDate> ||
std::is_same_v<Type, vectorized::ColumnDateTime> ||
std::is_same_v<Type, vectorized::Int64>);
}

template <typename Type>
constexpr bool IsV2() {
return !IsV1<Type>();
}

} // namespace doris::date_cast
13 changes: 4 additions & 9 deletions be/src/vec/functions/function_convert_tz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,10 @@
namespace doris::vectorized {

void register_function_convert_tz(SimpleFunctionFactory& factory) {
factory.register_function<
FunctionConvertTZ<ConvertTZImpl<DateV2Value<DateTimeV2ValueType>, DataTypeDateTimeV2>,
DataTypeDateTimeV2>>();
factory.register_function<FunctionConvertTZ<ConvertTZImpl<VecDateTimeValue, DataTypeDateTime>,
DataTypeDateTime>>();
factory.register_function<FunctionConvertTZ<
ConvertTZImpl<DateV2Value<DateV2ValueType>, DataTypeDateV2>, DataTypeDateV2>>();
factory.register_function<
FunctionConvertTZ<ConvertTZImpl<VecDateTimeValue, DataTypeDate>, DataTypeDate>>();
factory.register_function<FunctionConvertTZ<DataTypeDate>>();
factory.register_function<FunctionConvertTZ<DataTypeDateTime>>();
factory.register_function<FunctionConvertTZ<DataTypeDateV2>>();
factory.register_function<FunctionConvertTZ<DataTypeDateTimeV2>>();
}

} // namespace doris::vectorized
Loading

0 comments on commit a68b62b

Please sign in to comment.