Style fixes

This commit is contained in:
Maksim Galkin 2024-05-04 22:49:10 +00:00
parent 32f267999d
commit fb6c931262

View File

@ -12,20 +12,27 @@
#include <Columns/ColumnsNumber.h> #include <Columns/ColumnsNumber.h>
#include <Interpreters/castColumn.h> #include <Interpreters/castColumn.h>
#include "Common/DateLUTImpl.h" #include "Common/DateLUTImpl.h"
#include "Common/Exception.h"
#include <Common/DateLUT.h> #include <Common/DateLUT.h>
#include <Common/typeid_cast.h> #include <Common/typeid_cast.h>
#include "Columns/IColumn.h" #include "Columns/IColumn.h"
#include "DataTypes/IDataType.h" #include "DataTypes/IDataType.h"
#include <array> #include <array>
#include <memory> #include <memory>
namespace DB namespace DB
{ {
namespace ErrorCodes
{
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
}
namespace namespace
{ {
enum class ChangeDateFunctionsNames enum class ChangeDateFunctionsNames
{ {
CHANGE_YEAR = 0, CHANGE_YEAR = 0,
@ -35,50 +42,50 @@ enum class ChangeDateFunctionsNames
CHANGE_MINUTE = 4, CHANGE_MINUTE = 4,
CHANGE_SECOND = 5 CHANGE_SECOND = 5
}; };
constexpr bool isTimeChange(const ChangeDateFunctionsNames & type) constexpr bool isTimeChange(const ChangeDateFunctionsNames & type)
{ {
return type == ChangeDateFunctionsNames::CHANGE_HOUR || return type == ChangeDateFunctionsNames::CHANGE_HOUR ||
type == ChangeDateFunctionsNames::CHANGE_MINUTE || type == ChangeDateFunctionsNames::CHANGE_MINUTE ||
type == ChangeDateFunctionsNames::CHANGE_SECOND; type == ChangeDateFunctionsNames::CHANGE_SECOND;
} }
template <typename DataType> template <typename DataType>
constexpr bool isDate32() constexpr bool isDate32()
{ {
return DataType::type_id == TypeIndex::Date32; return DataType::type_id == TypeIndex::Date32;
} }
template <typename DataType> template <typename DataType>
constexpr bool isDateTime64() constexpr bool isDateTime64()
{ {
return DataType::type_id == TypeIndex::DateTime64; return DataType::type_id == TypeIndex::DateTime64;
} }
template <typename Traits> template <typename Traits>
class FunctionChangeDate : public IFunction class FunctionChangeDate : public IFunction
{ {
public: public:
static constexpr auto name = Traits::Name; static constexpr auto name = Traits::Name;
static constexpr std::array mandatory_argument_names = {"date", "new_value"}; static constexpr std::array mandatory_argument_names = {"date", "new_value"};
String getName() const override { return name; } String getName() const override { return name; }
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; } bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
size_t getNumberOfArguments() const override { return mandatory_argument_names.size(); } size_t getNumberOfArguments() const override { return mandatory_argument_names.size(); }
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionChangeDate>(); } static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionChangeDate>(); }
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
{ {
FunctionArgumentDescriptors args{ if (arguments.size() != 2)
{mandatory_argument_names[0], &isDateOrDate32OrDateTimeOrDateTime64<IDataType>, nullptr, "Date(32) or DateTime(64)"}, throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Function {} requires 2 parameters: date, new_value. Passed {}.", getName(), arguments.size());
{mandatory_argument_names[1], &isNumber<IDataType>, nullptr, "Number"}
}; if (!isDateOrDate32OrDateTimeOrDateTime64(*arguments[0].type) || !isNumber(*arguments[1].type))
validateFunctionArgumentTypes(*this, arguments, args); throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "First argument for function {} must be Date(32) or DateTime(64), second - numeric", getName());
if (isTimeChange(Traits::EnumName)) if (isTimeChange(Traits::EnumName))
{ {
if (isDate(arguments[0].type)) if (isDate(arguments[0].type))
@ -86,10 +93,10 @@ public:
if (isDate32(arguments[0].type)) if (isDate32(arguments[0].type))
return std::make_shared<DataTypeDateTime64>(DataTypeDateTime64::default_scale); return std::make_shared<DataTypeDateTime64>(DataTypeDateTime64::default_scale);
} }
return arguments[0].type; return arguments[0].type;
} }
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override
{ {
const auto & input_type = arguments[0].type; const auto & input_type = arguments[0].type;
@ -109,8 +116,7 @@ public:
return execute<DataTypeDateTime, DataTypeDateTime>(arguments, input_type, result_type, input_rows_count); return execute<DataTypeDateTime, DataTypeDateTime>(arguments, input_type, result_type, input_rows_count);
return execute<DataTypeDateTime64, DataTypeDateTime64>(arguments, input_type, result_type, input_rows_count); return execute<DataTypeDateTime64, DataTypeDateTime64>(arguments, input_type, result_type, input_rows_count);
} }
template <typename DataType, typename ResultDataType> template <typename DataType, typename ResultDataType>
ColumnPtr execute(const ColumnsWithTypeAndName & arguments, const DataTypePtr & input_type, const DataTypePtr & result_type, size_t input_rows_count) const ColumnPtr execute(const ColumnsWithTypeAndName & arguments, const DataTypePtr & input_type, const DataTypePtr & result_type, size_t input_rows_count) const
{ {
@ -127,16 +133,16 @@ public:
} }
else else
result_column = ResultDataType::ColumnType::create(result_rows_count); result_column = ResultDataType::ColumnType::create(result_rows_count);
auto & result_data = result_column->getData(); auto & result_data = result_column->getData();
auto input_column = arguments[0].column->convertToFullIfNeeded(); auto input_column = arguments[0].column->convertToFullIfNeeded();
const auto & input_column_data = typeid_cast<const typename DataType::ColumnType &>(*input_column).getData(); const auto & input_column_data = typeid_cast<const typename DataType::ColumnType &>(*input_column).getData();
auto new_value_column = castColumn(arguments[1], std::make_shared<DataTypeFloat64>()); auto new_value_column = castColumn(arguments[1], std::make_shared<DataTypeFloat64>());
new_value_column = new_value_column->convertToFullIfNeeded(); new_value_column = new_value_column->convertToFullIfNeeded();
const auto & new_value_column_data = typeid_cast<const ColumnFloat64 &>(*new_value_column).getData(); const auto & new_value_column_data = typeid_cast<const ColumnFloat64 &>(*new_value_column).getData();
for (size_t i = 0; i < result_rows_count; ++i) for (size_t i = 0; i < result_rows_count; ++i)
{ {
if constexpr (isDateTime64<DataType>()) if constexpr (isDateTime64<DataType>())
@ -146,17 +152,17 @@ public:
Int64 deg = 1; Int64 deg = 1;
for (size_t j = 0; j < scale; ++j) for (size_t j = 0; j < scale; ++j)
deg *= 10; deg *= 10;
Int64 time = date_lut.toNumYYYYMMDDhhmmss(input_column_data[i] / deg); Int64 time = date_lut.toNumYYYYMMDDhhmmss(input_column_data[i] / deg);
Int64 fraction = input_column_data[i] % deg; Int64 fraction = input_column_data[i] % deg;
result_data[i] = getChangedDate(time, new_value_column_data[i], result_type, date_lut, deg, fraction); result_data[i] = getChangedDate(time, new_value_column_data[i], result_type, date_lut, deg, fraction);
} }
else if constexpr (isDate32<DataType>() && isDateTime64<ResultDataType>()) else if constexpr (isDate32<DataType>() && isDateTime64<ResultDataType>())
{ {
const auto & date_lut = DateLUT::instance(); const auto & date_lut = DateLUT::instance();
Int64 time = static_cast<Int64>(date_lut.toNumYYYYMMDD(DayNum(input_column_data[i]))) * 1'000'000; Int64 time = static_cast<Int64>(date_lut.toNumYYYYMMDD(DayNum(input_column_data[i]))) * 1'000'000;
result_data[i] = getChangedDate(time, new_value_column_data[i], result_type, date_lut, 1'000, 0); result_data[i] = getChangedDate(time, new_value_column_data[i], result_type, date_lut, 1'000, 0);
} }
else else
@ -167,7 +173,7 @@ public:
time = static_cast<Int64>(date_lut.toNumYYYYMMDD(DayNum(input_column_data[i]))) * 1'000'000; time = static_cast<Int64>(date_lut.toNumYYYYMMDD(DayNum(input_column_data[i]))) * 1'000'000;
else else
time = date_lut.toNumYYYYMMDDhhmmss(input_column_data[i]); time = date_lut.toNumYYYYMMDDhhmmss(input_column_data[i]);
if (isDateOrDate32(result_type)) if (isDateOrDate32(result_type))
result_data[i] = static_cast<Int32>(getChangedDate(time, new_value_column_data[i], result_type, date_lut)); result_data[i] = static_cast<Int32>(getChangedDate(time, new_value_column_data[i], result_type, date_lut));
else else
@ -177,10 +183,10 @@ public:
if (is_const) if (is_const)
return ColumnConst::create(std::move(result_column), input_rows_count); return ColumnConst::create(std::move(result_column), input_rows_count);
return result_column; return result_column;
} }
Int64 getChangedDate(Int64 time, Float64 new_value, const DataTypePtr & result_type, const DateLUTImpl & date_lut, Int64 deg = 0, Int64 fraction = 0) const Int64 getChangedDate(Int64 time, Float64 new_value, const DataTypePtr & result_type, const DateLUTImpl & date_lut, Int64 deg = 0, Int64 fraction = 0) const
{ {
auto year = time / 10'000'000'000; auto year = time / 10'000'000'000;
@ -189,7 +195,7 @@ public:
auto hours = (time % 1'000'000) / 10'000; auto hours = (time % 1'000'000) / 10'000;
auto minutes = (time % 10'000) / 100; auto minutes = (time % 10'000) / 100;
auto seconds = time % 100; auto seconds = time % 100;
Int64 min_date, max_date; Int64 min_date, max_date;
Int16 min_year, max_year; Int16 min_year, max_year;
if (isDate(result_type)) if (isDate(result_type))
@ -220,7 +226,7 @@ public:
min_year = 1900; min_year = 1900;
max_year = 2299; max_year = 2299;
} }
switch (Traits::EnumName) switch (Traits::EnumName)
{ {
case ChangeDateFunctionsNames::CHANGE_YEAR: case ChangeDateFunctionsNames::CHANGE_YEAR:
@ -256,60 +262,60 @@ public:
seconds = static_cast<UInt8>(new_value); seconds = static_cast<UInt8>(new_value);
break; break;
} }
Int64 result; Int64 result;
if (isDateOrDate32(result_type)) if (isDateOrDate32(result_type))
result = date_lut.makeDayNum(year, month, day); result = date_lut.makeDayNum(year, month, day);
else else
result = date_lut.makeDateTime(year, month, day, hours, minutes, seconds) * deg + fraction; result = date_lut.makeDateTime(year, month, day, hours, minutes, seconds) * deg + fraction;
if (result > max_date) if (result > max_date)
return max_date; return max_date;
return result; return result;
} }
}; };
struct ChangeYearTraits struct ChangeYearTraits
{ {
static constexpr auto Name = "changeYear"; static constexpr auto Name = "changeYear";
static constexpr auto EnumName = ChangeDateFunctionsNames::CHANGE_YEAR; static constexpr auto EnumName = ChangeDateFunctionsNames::CHANGE_YEAR;
}; };
struct ChangeMonthTraits struct ChangeMonthTraits
{ {
static constexpr auto Name = "changeMonth"; static constexpr auto Name = "changeMonth";
static constexpr auto EnumName = ChangeDateFunctionsNames::CHANGE_MONTH; static constexpr auto EnumName = ChangeDateFunctionsNames::CHANGE_MONTH;
}; };
struct ChangeDayTraits struct ChangeDayTraits
{ {
static constexpr auto Name = "changeDay"; static constexpr auto Name = "changeDay";
static constexpr auto EnumName = ChangeDateFunctionsNames::CHANGE_DAY; static constexpr auto EnumName = ChangeDateFunctionsNames::CHANGE_DAY;
}; };
struct ChangeHourTraits struct ChangeHourTraits
{ {
static constexpr auto Name = "changeHour"; static constexpr auto Name = "changeHour";
static constexpr auto EnumName = ChangeDateFunctionsNames::CHANGE_HOUR; static constexpr auto EnumName = ChangeDateFunctionsNames::CHANGE_HOUR;
}; };
struct ChangeMinuteTraits struct ChangeMinuteTraits
{ {
static constexpr auto Name = "changeMinute"; static constexpr auto Name = "changeMinute";
static constexpr auto EnumName = ChangeDateFunctionsNames::CHANGE_MINUTE; static constexpr auto EnumName = ChangeDateFunctionsNames::CHANGE_MINUTE;
}; };
struct ChangeSecondTraits struct ChangeSecondTraits
{ {
static constexpr auto Name = "changeSecond"; static constexpr auto Name = "changeSecond";
static constexpr auto EnumName = ChangeDateFunctionsNames::CHANGE_SECOND; static constexpr auto EnumName = ChangeDateFunctionsNames::CHANGE_SECOND;
}; };
} }
REGISTER_FUNCTION(ChangeDate) REGISTER_FUNCTION(ChangeDate)
{ {
factory.registerFunction<FunctionChangeDate<ChangeYearTraits>>(); factory.registerFunction<FunctionChangeDate<ChangeYearTraits>>();
@ -319,5 +325,5 @@ REGISTER_FUNCTION(ChangeDate)
factory.registerFunction<FunctionChangeDate<ChangeMinuteTraits>>(); factory.registerFunction<FunctionChangeDate<ChangeMinuteTraits>>();
factory.registerFunction<FunctionChangeDate<ChangeSecondTraits>>(); factory.registerFunction<FunctionChangeDate<ChangeSecondTraits>>();
} }
} }