2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.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>
|
2021-12-21 13:41:53 +00:00
|
|
|
#include <Common/DateLUTImpl.h>
|
2021-03-22 19:50:04 +00:00
|
|
|
#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.
|
2022-03-11 22:46:02 +00:00
|
|
|
* Example: Pacific/Pitcairn.
|
2021-03-22 19:50:04 +00:00
|
|
|
*/
|
2021-05-22 12:49:21 +00:00
|
|
|
class FunctionTimezoneOf : public IFunction
|
2021-03-22 19:50:04 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "timezoneOf";
|
|
|
|
String getName() const override { return name; }
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_unique<FunctionTimezoneOf>(); }
|
2021-05-22 12:49:21 +00:00
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
|
|
|
|
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-22 12:49:21 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & types) const override
|
|
|
|
{
|
|
|
|
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-03-22 19:50:04 +00:00
|
|
|
|
|
|
|
bool useDefaultImplementationForNulls() const override { return false; }
|
|
|
|
bool useDefaultImplementationForLowCardinalityColumns() const override { return false; }
|
2021-05-22 12:49:21 +00:00
|
|
|
ColumnNumbers getArgumentsThatDontImplyNullableReturnType(size_t /*number_of_arguments*/) const override { return {0}; }
|
2021-03-22 19:50:04 +00:00
|
|
|
|
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-24 11:25:02 +00:00
|
|
|
ColumnPtr getConstantResultForNonConstArguments(const ColumnsWithTypeAndName & arguments, const DataTypePtr &) const override
|
2021-03-22 19:50:04 +00:00
|
|
|
{
|
|
|
|
DataTypePtr type_no_nullable = removeNullable(arguments[0].type);
|
|
|
|
|
|
|
|
return DataTypeString().createColumnConst(1,
|
|
|
|
dynamic_cast<const TimezoneMixin &>(*type_no_nullable).getTimeZone().getTimeZone());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(TimezoneOf)
|
2021-03-22 19:50:04 +00:00
|
|
|
{
|
2021-05-22 12:49:21 +00:00
|
|
|
factory.registerFunction<FunctionTimezoneOf>();
|
2021-03-22 19:50:04 +00:00
|
|
|
factory.registerAlias("timeZoneOf", "timezoneOf");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|