2019-06-09 15:19:15 +00:00
|
|
|
#pragma once
|
2021-05-02 19:04:26 +00:00
|
|
|
|
2023-10-13 10:05:19 +00:00
|
|
|
#include <Columns/ColumnString.h>
|
2019-06-09 15:19:15 +00:00
|
|
|
#include <Columns/ColumnVector.h>
|
|
|
|
#include <Columns/ColumnsNumber.h>
|
2023-10-13 10:05:19 +00:00
|
|
|
#include <Common/DateLUTImpl.h>
|
|
|
|
#include <Common/Exception.h>
|
2019-10-23 12:27:36 +00:00
|
|
|
#include <Core/DecimalFunctions.h>
|
2023-10-13 10:05:19 +00:00
|
|
|
#include <DataTypes/DataTypeString.h>
|
2021-05-24 13:53:14 +00:00
|
|
|
#include <Functions/DateTimeTransforms.h>
|
2019-06-09 15:19:15 +00:00
|
|
|
#include <Functions/FunctionHelpers.h>
|
2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2023-10-13 10:05:19 +00:00
|
|
|
#include <Functions/extractTimeZoneFromFunctionArguments.h>
|
|
|
|
#include <IO/ReadBufferFromString.h>
|
|
|
|
#include <IO/parseDateTimeBestEffort.h>
|
|
|
|
#include <base/types.h>
|
2019-06-09 15:19:15 +00:00
|
|
|
|
2021-05-02 19:04:26 +00:00
|
|
|
|
2019-06-09 15:19:15 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
}
|
|
|
|
|
2021-05-24 13:53:14 +00:00
|
|
|
|
2022-09-15 12:48:09 +00:00
|
|
|
template <typename FromType, typename ToType, typename Transform, bool is_extended_result = false>
|
2021-05-24 13:53:14 +00:00
|
|
|
struct WeekTransformer
|
2019-06-09 15:19:15 +00:00
|
|
|
{
|
2021-05-24 13:53:14 +00:00
|
|
|
explicit WeekTransformer(Transform transform_)
|
2019-10-23 12:27:36 +00:00
|
|
|
: transform(std::move(transform_))
|
|
|
|
{}
|
|
|
|
|
|
|
|
template <typename FromVectorType, typename ToVectorType>
|
2023-10-13 09:26:36 +00:00
|
|
|
void vector(const FromVectorType & vec_from, ToVectorType & vec_to, UInt8 week_mode, const DateLUTImpl & time_zone) const
|
2019-06-09 15:19:15 +00:00
|
|
|
{
|
2022-10-07 10:46:45 +00:00
|
|
|
using ValueType = typename ToVectorType::value_type;
|
2019-06-09 15:19:15 +00:00
|
|
|
size_t size = vec_from.size();
|
|
|
|
vec_to.resize(size);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
2022-10-07 10:46:45 +00:00
|
|
|
{
|
2022-09-15 12:48:09 +00:00
|
|
|
if constexpr (is_extended_result)
|
2022-10-07 10:46:45 +00:00
|
|
|
vec_to[i] = static_cast<ValueType>(transform.executeExtendedResult(vec_from[i], week_mode, time_zone));
|
2022-09-09 13:45:51 +00:00
|
|
|
else
|
2022-10-07 10:46:45 +00:00
|
|
|
vec_to[i] = static_cast<ValueType>(transform.execute(vec_from[i], week_mode, time_zone));
|
|
|
|
}
|
2019-06-09 15:19:15 +00:00
|
|
|
}
|
2019-10-23 12:27:36 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
const Transform transform;
|
2019-06-09 15:19:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-09-15 12:48:09 +00:00
|
|
|
template <typename FromDataType, typename ToDataType, bool is_extended_result = false>
|
2019-06-09 15:19:15 +00:00
|
|
|
struct CustomWeekTransformImpl
|
|
|
|
{
|
2019-10-23 12:27:36 +00:00
|
|
|
template <typename Transform>
|
2020-11-17 13:24:45 +00:00
|
|
|
static ColumnPtr execute(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/, Transform transform = {})
|
2019-06-09 15:19:15 +00:00
|
|
|
{
|
2022-09-15 12:48:09 +00:00
|
|
|
const auto op = WeekTransformer<typename FromDataType::FieldType, typename ToDataType::FieldType, Transform, is_extended_result>{std::move(transform)};
|
2019-06-09 15:19:15 +00:00
|
|
|
|
2023-10-13 09:26:36 +00:00
|
|
|
static constexpr UInt8 default_week_mode = 0;
|
|
|
|
UInt8 week_mode = default_week_mode;
|
2019-06-09 15:19:15 +00:00
|
|
|
if (arguments.size() > 1)
|
|
|
|
{
|
2020-10-17 16:48:53 +00:00
|
|
|
if (const auto * week_mode_column = checkAndGetColumnConst<ColumnUInt8>(arguments[1].column.get()))
|
2019-06-09 15:19:15 +00:00
|
|
|
week_mode = week_mode_column->getValue<UInt8>();
|
|
|
|
}
|
|
|
|
|
2020-10-17 16:48:53 +00:00
|
|
|
const DateLUTImpl & time_zone = extractTimeZoneFromFunctionArguments(arguments, 2, 0);
|
|
|
|
const ColumnPtr source_col = arguments[0].column;
|
2023-10-13 10:05:19 +00:00
|
|
|
|
|
|
|
if constexpr (std::is_same_v<FromDataType, DataTypeString>)
|
|
|
|
{
|
|
|
|
static const DateLUTImpl & utc_time_zone = DateLUT::instance("UTC");
|
|
|
|
const auto * sources = checkAndGetColumn<DataTypeString::ColumnType>(source_col.get());
|
|
|
|
|
|
|
|
auto col_to = ToDataType::ColumnType::create();
|
|
|
|
col_to->getData().resize(sources->size());
|
|
|
|
|
|
|
|
for (size_t i = 0; i < sources->size(); ++i)
|
|
|
|
{
|
|
|
|
DateTime64 dt64;
|
|
|
|
ReadBufferFromString buf(sources->getDataAt(i).toView());
|
|
|
|
parseDateTime64BestEffort(dt64, 0, buf, time_zone, utc_time_zone);
|
|
|
|
col_to->getData()[i] = static_cast<ToDataType::FieldType>(transform.execute(dt64, week_mode, time_zone));
|
|
|
|
}
|
|
|
|
|
|
|
|
return col_to;
|
|
|
|
}
|
|
|
|
else if (const auto * sources = checkAndGetColumn<typename FromDataType::ColumnType>(source_col.get()))
|
2019-06-09 15:19:15 +00:00
|
|
|
{
|
2019-10-23 12:27:36 +00:00
|
|
|
auto col_to = ToDataType::ColumnType::create();
|
|
|
|
op.vector(sources->getData(), col_to->getData(), week_mode, time_zone);
|
2020-10-17 16:48:53 +00:00
|
|
|
return col_to;
|
2019-06-09 15:19:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-09-30 07:54:35 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_COLUMN,
|
|
|
|
"Illegal column {} of first argument of function {}",
|
|
|
|
arguments[0].column->getName(), Transform::name);
|
2019-06-09 15:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|