2022-07-24 17:58:48 +00:00
|
|
|
#include <Functions/IFunction.h>
|
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/extractTimeZoneFromFunctionArguments.h>
|
|
|
|
#include <DataTypes/DataTypeDateTime.h>
|
2022-10-19 09:13:02 +00:00
|
|
|
#include <Columns/ColumnsDateTime.h>
|
2023-03-24 03:37:49 +00:00
|
|
|
#include <Columns/ColumnVector.h>
|
2023-06-10 12:53:02 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2022-07-24 17:58:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
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";
|
2023-06-10 12:53:02 +00:00
|
|
|
static FunctionPtr create(ContextPtr context)
|
2022-07-24 17:58:48 +00:00
|
|
|
{
|
2023-06-10 12:53:02 +00:00
|
|
|
return std::make_shared<FunctionNowInBlock>(context);
|
2022-07-24 17:58:48 +00:00
|
|
|
}
|
2023-06-10 12:53:02 +00:00
|
|
|
explicit FunctionNowInBlock(ContextPtr context)
|
|
|
|
: allow_nonconst_timezone_arguments(context->getSettings().allow_nonconst_timezone_arguments)
|
|
|
|
{}
|
2022-07-24 17:58:48 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Arguments size of function {} should be 0 or 1", getName());
|
2022-07-24 17:58:48 +00:00
|
|
|
}
|
|
|
|
if (arguments.size() == 1 && !isStringOrFixedString(arguments[0].type))
|
|
|
|
{
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Arguments of function {} should be String or FixedString",
|
|
|
|
getName());
|
2022-07-24 17:58:48 +00:00
|
|
|
}
|
|
|
|
if (arguments.size() == 1)
|
|
|
|
{
|
2023-06-10 12:53:02 +00:00
|
|
|
return std::make_shared<DataTypeDateTime>(extractTimeZoneNameFromFunctionArguments(arguments, 0, 0, allow_nonconst_timezone_arguments));
|
2022-07-24 17:58:48 +00:00
|
|
|
}
|
|
|
|
return std::make_shared<DataTypeDateTime>();
|
|
|
|
}
|
|
|
|
|
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName &, const DataTypePtr &, size_t input_rows_count) const override
|
|
|
|
{
|
2022-10-19 09:13:02 +00:00
|
|
|
return ColumnDateTime::create(input_rows_count, static_cast<UInt32>(time(nullptr)));
|
2022-07-24 17:58:48 +00:00
|
|
|
}
|
2023-06-10 12:53:02 +00:00
|
|
|
private:
|
|
|
|
const bool allow_nonconst_timezone_arguments;
|
2022-07-24 17:58:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(NowInBlock)
|
2022-07-24 17:58:48 +00:00
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionNowInBlock>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|