ClickHouse/src/Functions/FunctionCustomWeekToDateOrDate32.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

88 lines
3.5 KiB
C++
Raw Normal View History

#pragma once
2024-07-11 12:00:05 +00:00
#include <Core/Settings.h>
#include <Functions/IFunctionCustomWeek.h>
namespace DB
{
2024-09-18 12:20:53 +00:00
namespace Setting
{
extern const SettingsBool enable_extended_results_for_datetime_functions;
}
namespace ErrorCodes
{
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
}
template <typename Transform>
class FunctionCustomWeekToDateOrDate32 : public IFunctionCustomWeek<Transform>
{
private:
const bool enable_extended_results_for_datetime_functions = false;
public:
2023-10-13 09:26:36 +00:00
static FunctionPtr create(ContextPtr context)
{
2023-10-13 09:26:36 +00:00
return std::make_shared<FunctionCustomWeekToDateOrDate32>(context);
}
2023-10-13 09:26:36 +00:00
explicit FunctionCustomWeekToDateOrDate32(ContextPtr context)
2024-09-18 12:20:53 +00:00
: enable_extended_results_for_datetime_functions(context->getSettingsRef()[Setting::enable_extended_results_for_datetime_functions])
{
}
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
{
this->checkArguments(arguments, /*is_result_type_date_or_date32*/ true, Transform::value_may_be_string);
const IDataType * from_type = arguments[0].type.get();
WhichDataType which(from_type);
if ((which.isDate32() || which.isDateTime64()) && enable_extended_results_for_datetime_functions)
return std::make_shared<DataTypeDate32>();
return std::make_shared<DataTypeDate>();
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override
{
const IDataType * from_type = arguments[0].type.get();
WhichDataType which(from_type);
if (which.isDate())
return CustomWeekTransformImpl<DataTypeDate, DataTypeDate>::execute(arguments, result_type, input_rows_count, Transform{});
if (which.isDate32())
{
if (enable_extended_results_for_datetime_functions)
2023-10-13 09:26:36 +00:00
return CustomWeekTransformImpl<DataTypeDate32, DataTypeDate32, /*is_extended_result*/ true>::execute(
arguments, result_type, input_rows_count, Transform{});
return CustomWeekTransformImpl<DataTypeDate32, DataTypeDate>::execute(arguments, result_type, input_rows_count, Transform{});
}
if (which.isDateTime())
return CustomWeekTransformImpl<DataTypeDateTime, DataTypeDate>::execute(arguments, result_type, input_rows_count, Transform{});
if (which.isDateTime64())
{
if (enable_extended_results_for_datetime_functions)
2023-10-13 09:26:36 +00:00
return CustomWeekTransformImpl<DataTypeDateTime64, DataTypeDate32, /*is_extended_result*/ true>::execute(
arguments,
result_type,
input_rows_count,
TransformDateTime64<Transform>{assert_cast<const DataTypeDateTime64 *>(from_type)->getScale()});
2023-10-13 09:26:36 +00:00
return CustomWeekTransformImpl<DataTypeDateTime64, DataTypeDate>::execute(
arguments,
result_type,
input_rows_count,
TransformDateTime64<Transform>{assert_cast<const DataTypeDateTime64 *>(from_type)->getScale()});
}
if (Transform::value_may_be_string && which.isString())
return CustomWeekTransformImpl<DataTypeString, DataTypeDate>::execute(
arguments, result_type, input_rows_count, Transform{}); // TODO
throw Exception(
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"Illegal type {} of argument of function {}",
arguments[0].type->getName(),
this->getName());
}
};
}