mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-09-20 08:40:50 +00:00
Merge pull request #39533 from ClickHouse/now-in-block
Add function `nowInBlock`
This commit is contained in:
commit
6fdcb009ff
@ -836,7 +836,7 @@ Result:
|
||||
|
||||
## now
|
||||
|
||||
Returns the current date and time.
|
||||
Returns the current date and time at the moment of query analysis. The function is a constant expression.
|
||||
|
||||
**Syntax**
|
||||
|
||||
@ -884,14 +884,20 @@ Result:
|
||||
└──────────────────────┘
|
||||
```
|
||||
|
||||
## nowInBlock
|
||||
|
||||
Returns the current date and time at the moment of processing of each block of data. In contrast to the function `now`, it is not a constant expression, and the returned value will be different in different blocks for long-running queries.
|
||||
|
||||
It makes sense to use this function to generate the current time in long-running INSERT SELECT queries.
|
||||
|
||||
## today
|
||||
|
||||
Accepts zero arguments and returns the current date at one of the moments of request execution.
|
||||
Accepts zero arguments and returns the current date at one of the moments of query analysis.
|
||||
The same as ‘toDate(now())’.
|
||||
|
||||
## yesterday
|
||||
|
||||
Accepts zero arguments and returns yesterday’s date at one of the moments of request execution.
|
||||
Accepts zero arguments and returns yesterday’s date at one of the moments of query analysis.
|
||||
The same as ‘today() - 1’.
|
||||
|
||||
## timeSlot
|
||||
|
@ -44,7 +44,10 @@ public:
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool isDeterministic() const override { return false; }
|
||||
bool isDeterministic() const override
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool isDeterministicInScopeOfQuery() const override
|
||||
{
|
||||
|
@ -3,7 +3,6 @@
|
||||
#include <Functions/IFunction.h>
|
||||
#include <Core/DecimalFunctions.h>
|
||||
#include <Functions/FunctionFactory.h>
|
||||
#include <Core/Field.h>
|
||||
|
||||
#include <Functions/extractTimeZoneFromFunctionArguments.h>
|
||||
|
||||
|
88
src/Functions/nowInBlock.cpp
Normal file
88
src/Functions/nowInBlock.cpp
Normal file
@ -0,0 +1,88 @@
|
||||
#include <Functions/IFunction.h>
|
||||
#include <Functions/FunctionFactory.h>
|
||||
#include <Functions/extractTimeZoneFromFunctionArguments.h>
|
||||
#include <DataTypes/DataTypeDateTime.h>
|
||||
#include <Columns/ColumnsNumber.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)
|
||||
{
|
||||
return std::make_shared<FunctionNowInBlock>();
|
||||
}
|
||||
|
||||
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("Arguments size of function " + getName() + " should be 0 or 1", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
||||
}
|
||||
if (arguments.size() == 1 && !isStringOrFixedString(arguments[0].type))
|
||||
{
|
||||
throw Exception(
|
||||
"Arguments of function " + getName() + " should be String or FixedString", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
||||
}
|
||||
if (arguments.size() == 1)
|
||||
{
|
||||
return std::make_shared<DataTypeDateTime>(extractTimeZoneNameFromFunctionArguments(arguments, 0, 0));
|
||||
}
|
||||
return std::make_shared<DataTypeDateTime>();
|
||||
}
|
||||
|
||||
ColumnPtr executeImpl(const ColumnsWithTypeAndName &, const DataTypePtr &, size_t input_rows_count) const override
|
||||
{
|
||||
return ColumnUInt32::create(input_rows_count, time(nullptr));
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
void registerFunctionNowInBlock(FunctionFactory & factory)
|
||||
{
|
||||
factory.registerFunction<FunctionNowInBlock>();
|
||||
}
|
||||
|
||||
}
|
@ -43,6 +43,7 @@ void registerFunctionToRelativeMinuteNum(FunctionFactory &);
|
||||
void registerFunctionToRelativeSecondNum(FunctionFactory &);
|
||||
void registerFunctionToTime(FunctionFactory &);
|
||||
void registerFunctionNow(FunctionFactory &);
|
||||
void registerFunctionNowInBlock(FunctionFactory &);
|
||||
void registerFunctionNow64(FunctionFactory &);
|
||||
void registerFunctionToday(FunctionFactory &);
|
||||
void registerFunctionYesterday(FunctionFactory &);
|
||||
@ -126,6 +127,7 @@ void registerFunctionsDateTime(FunctionFactory & factory)
|
||||
registerFunctionToTime(factory);
|
||||
registerFunctionNow(factory);
|
||||
registerFunctionNow64(factory);
|
||||
registerFunctionNowInBlock(factory);
|
||||
registerFunctionToday(factory);
|
||||
registerFunctionYesterday(factory);
|
||||
registerFunctionTimeSlot(factory);
|
||||
|
2
tests/queries/0_stateless/02372_now_in_block.reference
Normal file
2
tests/queries/0_stateless/02372_now_in_block.reference
Normal file
@ -0,0 +1,2 @@
|
||||
2
|
||||
1
|
4
tests/queries/0_stateless/02372_now_in_block.sql
Normal file
4
tests/queries/0_stateless/02372_now_in_block.sql
Normal file
@ -0,0 +1,4 @@
|
||||
SELECT count() FROM (SELECT DISTINCT nowInBlock(), nowInBlock('Pacific/Pitcairn') FROM system.numbers LIMIT 2);
|
||||
SELECT nowInBlock(1); -- { serverError 43 }
|
||||
SELECT nowInBlock(NULL) IS NULL;
|
||||
SELECT nowInBlock('UTC', 'UTC'); -- { serverError 42 }
|
Loading…
Reference in New Issue
Block a user