mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-12-15 19:02:04 +00:00
72b9d75a84
SQL function toTimezone() converts a Date or DateTime into another timezone. The problem is that the timezone is part of the Date / DateTime type but not part of the internal representation (value). This led to the fact that toTimeZone() wqith non-const timezones produced wrong and misleading results until #48471 (shipped with v23.4) enforced a const timezone. Unfortunately, this PR also broke existing table definitions with non-const timezones, e.g. in ALIAS expressions. So while #48471 addressed the issue appropriately, it is really backwards-incompatible. This PR adds a setting to toggle the behavior and makes it also part of the compatibility profile.
96 lines
2.7 KiB
C++
96 lines
2.7 KiB
C++
#include <Functions/IFunction.h>
|
|
#include <Functions/FunctionFactory.h>
|
|
#include <Functions/extractTimeZoneFromFunctionArguments.h>
|
|
#include <DataTypes/DataTypeDateTime.h>
|
|
#include <Columns/ColumnsDateTime.h>
|
|
#include <Columns/ColumnVector.h>
|
|
#include <Interpreters/Context.h>
|
|
|
|
|
|
namespace DB
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
{
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
}
|
|
|
|
namespace
|
|
{
|
|
|
|
/** Returns current time at calculation of every block.
|
|
* In contrast to 'now' function, it's not a constant expression and is not a subject of constant folding.
|
|
*/
|
|
class FunctionNowInBlock : public IFunction
|
|
{
|
|
public:
|
|
static constexpr auto name = "nowInBlock";
|
|
static FunctionPtr create(ContextPtr context)
|
|
{
|
|
return std::make_shared<FunctionNowInBlock>(context);
|
|
}
|
|
explicit FunctionNowInBlock(ContextPtr context)
|
|
: allow_nonconst_timezone_arguments(context->getSettings().allow_nonconst_timezone_arguments)
|
|
{}
|
|
|
|
String getName() const override
|
|
{
|
|
return name;
|
|
}
|
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override
|
|
{
|
|
return false;
|
|
}
|
|
|
|
/// Optional timezone argument.
|
|
bool isVariadic() const override { return true; }
|
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
|
|
|
bool isDeterministic() const override
|
|
{
|
|
return false;
|
|
}
|
|
|
|
bool isDeterministicInScopeOfQuery() const override
|
|
{
|
|
return false;
|
|
}
|
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
|
|
{
|
|
if (arguments.size() > 1)
|
|
{
|
|
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Arguments size of function {} should be 0 or 1", getName());
|
|
}
|
|
if (arguments.size() == 1 && !isStringOrFixedString(arguments[0].type))
|
|
{
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Arguments of function {} should be String or FixedString",
|
|
getName());
|
|
}
|
|
if (arguments.size() == 1)
|
|
{
|
|
return std::make_shared<DataTypeDateTime>(extractTimeZoneNameFromFunctionArguments(arguments, 0, 0, allow_nonconst_timezone_arguments));
|
|
}
|
|
return std::make_shared<DataTypeDateTime>();
|
|
}
|
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName &, const DataTypePtr &, size_t input_rows_count) const override
|
|
{
|
|
return ColumnDateTime::create(input_rows_count, static_cast<UInt32>(time(nullptr)));
|
|
}
|
|
private:
|
|
const bool allow_nonconst_timezone_arguments;
|
|
};
|
|
|
|
}
|
|
|
|
REGISTER_FUNCTION(NowInBlock)
|
|
{
|
|
factory.registerFunction<FunctionNowInBlock>();
|
|
}
|
|
|
|
}
|