2020-11-16 03:29:29 +00:00
|
|
|
#include <Columns/ColumnNullable.h>
|
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <Columns/ColumnFixedString.h>
|
|
|
|
#include <Columns/ColumnVector.h>
|
|
|
|
#include <Columns/ColumnsNumber.h>
|
|
|
|
#include <DataTypes/IDataType.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <DataTypes/DataTypeNullable.h>
|
2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2020-11-16 03:29:29 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/GregorianDate.h>
|
|
|
|
#include <IO/ReadBufferFromMemory.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Name, typename ToDataType, bool nullOnErrors>
|
2021-05-15 17:33:15 +00:00
|
|
|
class ExecutableFunctionToModifiedJulianDay : public IExecutableFunction
|
2020-11-16 03:29:29 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return Name::name;
|
|
|
|
}
|
|
|
|
|
2021-05-15 17:33:15 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
|
2020-11-16 03:29:29 +00:00
|
|
|
{
|
|
|
|
const IColumn * col_from = arguments[0].column.get();
|
|
|
|
const ColumnString * col_from_string = checkAndGetColumn<ColumnString>(col_from);
|
|
|
|
const ColumnFixedString * col_from_fixed_string = checkAndGetColumn<ColumnFixedString>(col_from);
|
|
|
|
|
|
|
|
const ColumnString::Chars * chars = nullptr;
|
|
|
|
const IColumn::Offsets * offsets = nullptr;
|
|
|
|
size_t fixed_string_size = 0;
|
|
|
|
|
|
|
|
if (col_from_string)
|
|
|
|
{
|
|
|
|
chars = &col_from_string->getChars();
|
|
|
|
offsets = &col_from_string->getOffsets();
|
|
|
|
}
|
|
|
|
else if (col_from_fixed_string)
|
|
|
|
{
|
|
|
|
chars = &col_from_fixed_string->getChars();
|
|
|
|
fixed_string_size = col_from_fixed_string->getN();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Illegal column {} of first argument of function {}",
|
|
|
|
col_from->getName(), Name::name);
|
2020-11-16 03:29:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
using ColVecTo = typename ToDataType::ColumnType;
|
|
|
|
typename ColVecTo::MutablePtr col_to = ColVecTo::create(input_rows_count);
|
|
|
|
typename ColVecTo::Container & vec_to = col_to->getData();
|
|
|
|
|
|
|
|
ColumnUInt8::MutablePtr col_null_map_to;
|
2020-12-17 02:20:56 +00:00
|
|
|
UInt8 * vec_null_map_to [[maybe_unused]] = nullptr;
|
2020-11-16 03:29:29 +00:00
|
|
|
if constexpr (nullOnErrors)
|
|
|
|
{
|
|
|
|
col_null_map_to = ColumnUInt8::create(input_rows_count);
|
2020-12-17 02:20:56 +00:00
|
|
|
vec_null_map_to = col_null_map_to->getData().data();
|
2020-11-16 03:29:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t current_offset = 0;
|
|
|
|
for (size_t i = 0; i < input_rows_count; ++i)
|
|
|
|
{
|
2020-12-07 15:47:57 +00:00
|
|
|
const size_t next_offset = offsets ? (*offsets)[i] : current_offset + fixed_string_size;
|
|
|
|
const size_t string_size = offsets ? next_offset - current_offset - 1 : fixed_string_size;
|
|
|
|
ReadBufferFromMemory read_buffer(&(*chars)[current_offset], string_size);
|
|
|
|
current_offset = next_offset;
|
|
|
|
|
|
|
|
if constexpr (nullOnErrors)
|
|
|
|
{
|
2023-07-04 22:39:10 +00:00
|
|
|
GregorianDate date;
|
|
|
|
|
|
|
|
int64_t res = 0;
|
|
|
|
bool success = date.tryInit(read_buffer) && date.tryToModifiedJulianDay(res);
|
|
|
|
|
|
|
|
vec_to[i] = static_cast<typename ToDataType::FieldType>(res);
|
|
|
|
vec_null_map_to[i] = !success;
|
2020-12-07 15:47:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-07-04 21:22:08 +00:00
|
|
|
const GregorianDate date(read_buffer);
|
|
|
|
vec_to[i] = static_cast<typename ToDataType::FieldType>(date.toModifiedJulianDay());
|
2020-12-07 15:47:57 +00:00
|
|
|
}
|
2020-11-16 03:29:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if constexpr (nullOnErrors)
|
|
|
|
return ColumnNullable::create(std::move(col_to), std::move(col_null_map_to));
|
|
|
|
else
|
|
|
|
return col_to;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Name, typename ToDataType, bool nullOnErrors>
|
2021-05-15 17:33:15 +00:00
|
|
|
class FunctionBaseToModifiedJulianDay : public IFunctionBase
|
2020-11-16 03:29:29 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-12-07 15:47:57 +00:00
|
|
|
explicit FunctionBaseToModifiedJulianDay(DataTypes argument_types_, DataTypePtr return_type_)
|
2020-11-16 03:29:29 +00:00
|
|
|
: argument_types(std::move(argument_types_))
|
|
|
|
, return_type(std::move(return_type_)) {}
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return Name::name;
|
|
|
|
}
|
|
|
|
|
|
|
|
const DataTypes & getArgumentTypes() const override
|
|
|
|
{
|
|
|
|
return argument_types;
|
|
|
|
}
|
|
|
|
|
|
|
|
const DataTypePtr & getResultType() const override
|
|
|
|
{
|
|
|
|
return return_type;
|
|
|
|
}
|
|
|
|
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
2021-04-29 14:48:26 +00:00
|
|
|
|
2021-05-15 17:33:15 +00:00
|
|
|
ExecutableFunctionPtr prepare(const ColumnsWithTypeAndName &) const override
|
2020-11-16 03:29:29 +00:00
|
|
|
{
|
2020-12-07 15:47:57 +00:00
|
|
|
return std::make_unique<ExecutableFunctionToModifiedJulianDay<Name, ToDataType, nullOnErrors>>();
|
2020-11-16 03:29:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isInjective(const ColumnsWithTypeAndName &) const override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasInformationAboutMonotonicity() const override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Monotonicity getMonotonicityForRange(const IDataType &, const Field &, const Field &) const override
|
|
|
|
{
|
2022-09-22 15:49:27 +00:00
|
|
|
return { .is_monotonic = true, .is_always_monotonic = true, .is_strict = true };
|
2020-11-16 03:29:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
DataTypes argument_types;
|
|
|
|
DataTypePtr return_type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Name, typename ToDataType, bool nullOnErrors>
|
2021-05-15 17:33:15 +00:00
|
|
|
class ToModifiedJulianDayOverloadResolver : public IFunctionOverloadResolver
|
2020-11-16 03:29:29 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = Name::name;
|
|
|
|
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionOverloadResolverPtr create(ContextPtr)
|
2020-11-16 03:29:29 +00:00
|
|
|
{
|
2020-12-07 15:47:57 +00:00
|
|
|
return std::make_unique<ToModifiedJulianDayOverloadResolver<Name, ToDataType, nullOnErrors>>();
|
2020-11-16 03:29:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return Name::name;
|
|
|
|
}
|
|
|
|
|
2021-05-15 17:33:15 +00:00
|
|
|
FunctionBasePtr buildImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & return_type) const override
|
2020-11-16 03:29:29 +00:00
|
|
|
{
|
|
|
|
DataTypes argument_types = { arguments[0].type };
|
|
|
|
|
2020-12-07 15:47:57 +00:00
|
|
|
return std::make_unique<FunctionBaseToModifiedJulianDay<Name, ToDataType, nullOnErrors>>(argument_types, return_type);
|
2020-11-16 03:29:29 +00:00
|
|
|
}
|
|
|
|
|
2021-05-15 17:33:15 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
2020-11-16 03:29:29 +00:00
|
|
|
{
|
|
|
|
if (!isStringOrFixedString(arguments[0]))
|
|
|
|
{
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "The argument of function {} must be String or FixedString",
|
|
|
|
getName());
|
2020-11-16 03:29:29 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 17:43:43 +00:00
|
|
|
DataTypePtr base_type = std::make_shared<ToDataType>();
|
2020-11-16 03:29:29 +00:00
|
|
|
if constexpr (nullOnErrors)
|
|
|
|
{
|
2020-12-08 17:43:43 +00:00
|
|
|
return std::make_shared<DataTypeNullable>(base_type);
|
2020-11-16 03:29:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-12-08 17:43:43 +00:00
|
|
|
return base_type;
|
2020-11-16 03:29:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isInjective(const ColumnsWithTypeAndName &) const override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-07 15:47:57 +00:00
|
|
|
struct NameToModifiedJulianDay
|
2020-11-16 03:29:29 +00:00
|
|
|
{
|
2020-12-07 15:47:57 +00:00
|
|
|
static constexpr auto name = "toModifiedJulianDay";
|
2020-11-16 03:29:29 +00:00
|
|
|
};
|
|
|
|
|
2020-12-07 15:47:57 +00:00
|
|
|
struct NameToModifiedJulianDayOrNull
|
2020-11-16 03:29:29 +00:00
|
|
|
{
|
2020-12-07 15:47:57 +00:00
|
|
|
static constexpr auto name = "toModifiedJulianDayOrNull";
|
2020-11-16 03:29:29 +00:00
|
|
|
};
|
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(ToModifiedJulianDay)
|
2020-11-16 03:29:29 +00:00
|
|
|
{
|
2020-12-07 15:47:57 +00:00
|
|
|
factory.registerFunction<ToModifiedJulianDayOverloadResolver<NameToModifiedJulianDay, DataTypeInt32, false>>();
|
|
|
|
factory.registerFunction<ToModifiedJulianDayOverloadResolver<NameToModifiedJulianDayOrNull, DataTypeInt32, true>>();
|
2020-11-16 03:29:29 +00:00
|
|
|
}
|
|
|
|
}
|