2019-06-09 15:19:15 +00:00
|
|
|
#pragma once
|
|
|
|
#include <regex>
|
|
|
|
#include <Columns/ColumnVector.h>
|
|
|
|
#include <Columns/ColumnsNumber.h>
|
|
|
|
#include <Core/Types.h>
|
2019-10-23 12:27:36 +00:00
|
|
|
#include <Core/DecimalFunctions.h>
|
2019-06-09 15:19:15 +00:00
|
|
|
#include <Functions/FunctionHelpers.h>
|
|
|
|
#include <Functions/extractTimeZoneFromFunctionArguments.h>
|
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <common/DateLUTImpl.h>
|
|
|
|
|
|
|
|
/// The default mode value to use for the WEEK() function
|
|
|
|
#define DEFAULT_WEEK_MODE 0
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CustomWeek Transformations.
|
|
|
|
*/
|
|
|
|
|
|
|
|
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, UInt8, const DateLUTImpl &) { return 0; }
|
|
|
|
static inline UInt16 execute(UInt16, UInt8, const DateLUTImpl &) { return 0; }
|
|
|
|
};
|
|
|
|
|
2019-06-18 09:48:07 +00:00
|
|
|
struct ToWeekImpl
|
2019-06-09 15:19:15 +00:00
|
|
|
{
|
2019-06-18 09:48:07 +00:00
|
|
|
static constexpr auto name = "toWeek";
|
2019-06-09 15:19:15 +00:00
|
|
|
|
|
|
|
static inline UInt8 execute(UInt32 t, UInt8 week_mode, const DateLUTImpl & time_zone)
|
|
|
|
{
|
2019-06-18 09:48:07 +00:00
|
|
|
YearWeek yw = time_zone.toYearWeek(time_zone.toDayNum(t), week_mode);
|
|
|
|
return yw.second;
|
2019-06-09 15:19:15 +00:00
|
|
|
}
|
|
|
|
static inline UInt8 execute(UInt16 d, UInt8 week_mode, const DateLUTImpl & time_zone)
|
|
|
|
{
|
2019-06-18 09:48:07 +00:00
|
|
|
YearWeek yw = time_zone.toYearWeek(DayNum(d), week_mode);
|
|
|
|
return yw.second;
|
2019-06-09 15:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
using FactorTransform = ZeroTransform;
|
|
|
|
};
|
|
|
|
|
2019-06-18 09:48:07 +00:00
|
|
|
struct ToYearWeekImpl
|
2019-06-09 15:19:15 +00:00
|
|
|
{
|
2019-06-18 09:48:07 +00:00
|
|
|
static constexpr auto name = "toYearWeek";
|
2019-06-09 15:19:15 +00:00
|
|
|
|
|
|
|
static inline UInt32 execute(UInt32 t, UInt8 week_mode, const DateLUTImpl & time_zone)
|
|
|
|
{
|
2019-06-18 09:48:07 +00:00
|
|
|
YearWeek yw = time_zone.toYearWeek(time_zone.toDayNum(t), week_mode | static_cast<UInt32>(WeekModeFlag::YEAR));
|
|
|
|
return yw.first * 100 + yw.second;
|
2019-06-09 15:19:15 +00:00
|
|
|
}
|
|
|
|
static inline UInt32 execute(UInt16 d, UInt8 week_mode, const DateLUTImpl & time_zone)
|
|
|
|
{
|
2019-06-18 09:48:07 +00:00
|
|
|
YearWeek yw = time_zone.toYearWeek(DayNum(d), week_mode | static_cast<UInt32>(WeekModeFlag::YEAR));
|
|
|
|
return yw.first * 100 + yw.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
using FactorTransform = ZeroTransform;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ToStartOfWeekImpl
|
|
|
|
{
|
|
|
|
static constexpr auto name = "toStartOfWeek";
|
|
|
|
|
|
|
|
static inline UInt16 execute(UInt32 t, UInt8 week_mode, const DateLUTImpl & time_zone)
|
|
|
|
{
|
|
|
|
return time_zone.toFirstDayNumOfWeek(time_zone.toDayNum(t), week_mode);
|
|
|
|
}
|
|
|
|
static inline UInt16 execute(UInt16 d, UInt8 week_mode, const DateLUTImpl & time_zone)
|
|
|
|
{
|
|
|
|
return time_zone.toFirstDayNumOfWeek(DayNum(d), week_mode);
|
2019-06-09 15:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
using FactorTransform = ZeroTransform;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename FromType, typename ToType, typename Transform>
|
|
|
|
struct Transformer
|
|
|
|
{
|
2019-10-23 12:27:36 +00:00
|
|
|
Transformer(Transform transform_)
|
|
|
|
: transform(std::move(transform_))
|
|
|
|
{}
|
|
|
|
|
|
|
|
template <typename FromVectorType, typename ToVectorType>
|
|
|
|
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
|
|
|
{
|
|
|
|
size_t size = vec_from.size();
|
|
|
|
vec_to.resize(size);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
2019-10-23 12:27:36 +00:00
|
|
|
vec_to[i] = 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
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-10-23 12:27:36 +00:00
|
|
|
template <typename FromDataType, typename ToDataType>
|
2019-06-09 15:19:15 +00:00
|
|
|
struct CustomWeekTransformImpl
|
|
|
|
{
|
2019-10-23 12:27:36 +00:00
|
|
|
template <typename Transform>
|
|
|
|
static void execute(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/, Transform transform = {})
|
2019-06-09 15:19:15 +00:00
|
|
|
{
|
2019-10-23 12:27:36 +00:00
|
|
|
const auto op = Transformer<typename FromDataType::FieldType, typename ToDataType::FieldType, Transform>{std::move(transform)};
|
2019-06-09 15:19:15 +00:00
|
|
|
|
|
|
|
UInt8 week_mode = DEFAULT_WEEK_MODE;
|
|
|
|
if (arguments.size() > 1)
|
|
|
|
{
|
|
|
|
if (const auto week_mode_column = checkAndGetColumnConst<ColumnUInt8>(block.getByPosition(arguments[1]).column.get()))
|
|
|
|
week_mode = week_mode_column->getValue<UInt8>();
|
|
|
|
}
|
|
|
|
|
|
|
|
const DateLUTImpl & time_zone = extractTimeZoneFromFunctionArguments(block, arguments, 2, 0);
|
|
|
|
const ColumnPtr source_col = block.getByPosition(arguments[0]).column;
|
2019-10-23 12:27:36 +00:00
|
|
|
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);
|
2019-06-09 15:19:15 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|