2021-05-15 17:33:15 +00:00
|
|
|
#include <Functions/IFunctionOld.h>
|
2021-03-22 19:50:04 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <DataTypes/DataTypeNullable.h>
|
|
|
|
#include <DataTypes/DataTypeDateTime.h>
|
|
|
|
#include <common/DateLUTImpl.h>
|
|
|
|
#include <Core/Field.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2021-03-24 01:12:52 +00:00
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int BAD_ARGUMENTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-22 19:50:04 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
/** timezoneOf(x) - get the name of the timezone of DateTime data type.
|
|
|
|
* Example: Europe/Moscow.
|
|
|
|
*/
|
2021-05-15 17:33:15 +00:00
|
|
|
class ExecutableFunctionTimezoneOf : public IExecutableFunction
|
2021-03-22 19:50:04 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "timezoneOf";
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
bool useDefaultImplementationForNulls() const override { return false; }
|
|
|
|
bool useDefaultImplementationForLowCardinalityColumns() const override { return false; }
|
|
|
|
|
|
|
|
/// Execute the function on the columns.
|
2021-05-15 17:33:15 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
|
2021-03-22 19:50:04 +00:00
|
|
|
{
|
|
|
|
DataTypePtr type_no_nullable = removeNullable(arguments[0].type);
|
|
|
|
|
|
|
|
return DataTypeString().createColumnConst(input_rows_count,
|
|
|
|
dynamic_cast<const TimezoneMixin &>(*type_no_nullable).getTimeZone().getTimeZone());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-05-15 17:33:15 +00:00
|
|
|
class BaseFunctionTimezoneOf : public IFunctionBase
|
2021-03-22 19:50:04 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
BaseFunctionTimezoneOf(DataTypes argument_types_, DataTypePtr return_type_)
|
|
|
|
: argument_types(std::move(argument_types_)), return_type(std::move(return_type_)) {}
|
|
|
|
|
|
|
|
static constexpr auto name = "timezoneOf";
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
bool isDeterministic() const override { return true; }
|
|
|
|
bool isDeterministicInScopeOfQuery() const override { return true; }
|
|
|
|
|
|
|
|
const DataTypes & getArgumentTypes() const override { return argument_types; }
|
|
|
|
const DataTypePtr & getResultType() const override { return return_type; }
|
|
|
|
|
2021-05-15 17:33:15 +00:00
|
|
|
ExecutableFunctionPtr prepare(const ColumnsWithTypeAndName &) const override
|
2021-03-22 19:50:04 +00:00
|
|
|
{
|
|
|
|
return std::make_unique<ExecutableFunctionTimezoneOf>();
|
|
|
|
}
|
|
|
|
|
|
|
|
ColumnPtr getResultIfAlwaysReturnsConstantAndHasArguments(const ColumnsWithTypeAndName & arguments) const override
|
|
|
|
{
|
|
|
|
DataTypePtr type_no_nullable = removeNullable(arguments[0].type);
|
|
|
|
|
|
|
|
return DataTypeString().createColumnConst(1,
|
|
|
|
dynamic_cast<const TimezoneMixin &>(*type_no_nullable).getTimeZone().getTimeZone());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
DataTypes argument_types;
|
|
|
|
DataTypePtr return_type;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-05-15 17:33:15 +00:00
|
|
|
class FunctionTimezoneOfBuilder : public IFunctionOverloadResolver
|
2021-03-22 19:50:04 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "timezoneOf";
|
|
|
|
String getName() const override { return name; }
|
2021-05-15 17:33:15 +00:00
|
|
|
static FunctionOverloadResolverPtr create(ContextPtr) { return std::make_unique<FunctionTimezoneOfBuilder>(); }
|
2021-03-22 19:50:04 +00:00
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
|
|
|
|
2021-05-15 17:33:15 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & types) const override
|
2021-03-22 19:50:04 +00:00
|
|
|
{
|
|
|
|
DataTypePtr type_no_nullable = removeNullable(types[0]);
|
|
|
|
|
|
|
|
if (isDateTime(type_no_nullable) || isDateTime64(type_no_nullable))
|
|
|
|
return std::make_shared<DataTypeString>();
|
|
|
|
else
|
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Bad argument for function {}, should be DateTime or DateTime64", name);
|
|
|
|
}
|
|
|
|
|
2021-05-15 17:33:15 +00:00
|
|
|
FunctionBasePtr buildImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & return_type) const override
|
2021-03-22 19:50:04 +00:00
|
|
|
{
|
|
|
|
return std::make_unique<BaseFunctionTimezoneOf>(DataTypes{arguments[0].type}, return_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool useDefaultImplementationForNulls() const override { return false; }
|
|
|
|
bool useDefaultImplementationForLowCardinalityColumns() const override { return false; }
|
|
|
|
ColumnNumbers getArgumentsThatDontImplyNullableReturnType(size_t /*number_of_arguments*/) const override { return {0}; }
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerFunctionTimezoneOf(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionTimezoneOfBuilder>();
|
|
|
|
factory.registerAlias("timeZoneOf", "timezoneOf");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|