mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-10 01:25:21 +00:00
updated Interval definitions
This commit is contained in:
parent
48c1b8b62b
commit
dcc0f53f8e
@ -311,6 +311,121 @@ struct ToStartOfSecondImpl
|
||||
using FactorTransform = ZeroTransform;
|
||||
};
|
||||
|
||||
struct ToStartOfMillisecondImpl
|
||||
{
|
||||
static constexpr auto name = "toStartOfMillisecond";
|
||||
|
||||
static inline DateTime64 execute(const DateTime64 & datetime64, Int64 scale_multiplier, const DateLUTImpl &)
|
||||
{
|
||||
// given that scale is 6, scale_multiplier is 1000000
|
||||
// for DateTime64 value of 123.456789:
|
||||
// 123456789 - 789 = 123456000
|
||||
// for DateTime64 value of -123.456789:
|
||||
// -123456789 - (1000 + (-789)) = -123457000
|
||||
|
||||
if (scale_multiplier <= 1000){
|
||||
return datetime64 * (1000 / scale_multiplier);
|
||||
}
|
||||
else
|
||||
{
|
||||
auto droppable_part_with_sign = DecimalUtils::getFractionalPartWithScaleMultiplier<DateTime64, true>(datetime64, scale_multiplier / 1000);
|
||||
|
||||
if (droppable_part_with_sign < 0)
|
||||
droppable_part_with_sign += scale_multiplier;
|
||||
|
||||
return datetime64 - droppable_part_with_sign;
|
||||
}
|
||||
}
|
||||
|
||||
static inline UInt32 execute(UInt32, const DateLUTImpl &)
|
||||
{
|
||||
throw Exception("Illegal type DateTime of argument for function " + std::string(name), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
||||
}
|
||||
static inline UInt32 execute(Int32, const DateLUTImpl &)
|
||||
{
|
||||
return dateIsNotSupported(name);
|
||||
}
|
||||
static inline UInt32 execute(UInt16, const DateLUTImpl &)
|
||||
{
|
||||
return dateIsNotSupported(name);
|
||||
}
|
||||
|
||||
using FactorTransform = ZeroTransform;
|
||||
};
|
||||
|
||||
struct ToStartOfMicrosecondImpl
|
||||
{
|
||||
static constexpr auto name = "toStartOfMicrosecond";
|
||||
|
||||
static inline DateTime64 execute(const DateTime64 & datetime64, Int64 scale_multiplier, const DateLUTImpl &)
|
||||
{
|
||||
// @see ToStartOfMillisecondImpl
|
||||
|
||||
if (scale_multiplier <= 1000000){
|
||||
return datetime64 * (1000000 / scale_multiplier);
|
||||
}
|
||||
else
|
||||
{
|
||||
auto droppable_part_with_sign = DecimalUtils::getFractionalPartWithScaleMultiplier<DateTime64, true>(datetime64, scale_multiplier / 1000000);
|
||||
|
||||
if (droppable_part_with_sign < 0)
|
||||
droppable_part_with_sign += scale_multiplier;
|
||||
|
||||
return datetime64 - droppable_part_with_sign;
|
||||
}
|
||||
}
|
||||
|
||||
static inline UInt32 execute(UInt32, const DateLUTImpl &)
|
||||
{
|
||||
throw Exception("Illegal type DateTime of argument for function " + std::string(name), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
||||
}
|
||||
static inline UInt32 execute(Int32, const DateLUTImpl &)
|
||||
{
|
||||
return dateIsNotSupported(name);
|
||||
}
|
||||
static inline UInt32 execute(UInt16, const DateLUTImpl &)
|
||||
{
|
||||
return dateIsNotSupported(name);
|
||||
}
|
||||
|
||||
using FactorTransform = ZeroTransform;
|
||||
};
|
||||
|
||||
struct ToStartOfNanosecondImpl
|
||||
{
|
||||
static constexpr auto name = "toStartOfNanosecond";
|
||||
|
||||
static inline DateTime64 execute(const DateTime64 & datetime64, Int64 scale_multiplier, const DateLUTImpl &)
|
||||
{
|
||||
// @see ToStartOfMillisecondImpl
|
||||
if (scale_multiplier == 1000000000){
|
||||
return datetime64;
|
||||
}
|
||||
else if (scale_multiplier <= 1000000000){
|
||||
return datetime64 * (1000000000 / scale_multiplier);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw Exception("Illegal type of argument for function " + std::string(name) + ", DateTime64 expected", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
||||
}
|
||||
}
|
||||
|
||||
static inline UInt32 execute(UInt32, const DateLUTImpl &)
|
||||
{
|
||||
throw Exception("Illegal type DateTime of argument for function " + std::string(name), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
||||
}
|
||||
static inline UInt32 execute(Int32, const DateLUTImpl &)
|
||||
{
|
||||
return dateIsNotSupported(name);
|
||||
}
|
||||
static inline UInt32 execute(UInt16, const DateLUTImpl &)
|
||||
{
|
||||
return dateIsNotSupported(name);
|
||||
}
|
||||
|
||||
using FactorTransform = ZeroTransform;
|
||||
};
|
||||
|
||||
struct ToStartOfFiveMinuteImpl
|
||||
{
|
||||
static constexpr auto name = "toStartOfFiveMinute";
|
||||
|
@ -88,6 +88,20 @@ public:
|
||||
Int64 scale = DataTypeDateTime64::default_scale;
|
||||
if (const auto * dt64 = checkAndGetDataType<DataTypeDateTime64>(arguments[0].type.get()))
|
||||
scale = dt64->getScale();
|
||||
auto source_scale = scale;
|
||||
|
||||
if constexpr (std::is_same_v<ToStartOfMillisecondImpl, Transform>)
|
||||
{
|
||||
scale = std::max(source_scale, static_cast<Int64>(3));
|
||||
}
|
||||
else if constexpr (std::is_same_v<ToStartOfMicrosecondImpl, Transform>)
|
||||
{
|
||||
scale = std::max(source_scale, static_cast<Int64>(6));
|
||||
}
|
||||
else if constexpr (std::is_same_v<ToStartOfNanosecondImpl, Transform>)
|
||||
{
|
||||
scale = std::max(source_scale, static_cast<Int64>(9));
|
||||
}
|
||||
|
||||
return std::make_shared<ToDataType>(scale, extractTimeZoneNameFromFunctionArguments(arguments, 1, 0));
|
||||
}
|
||||
|
@ -82,6 +82,31 @@ struct ToStartOfTransform;
|
||||
TRANSFORM_TIME(Second)
|
||||
#undef TRANSFORM_TIME
|
||||
|
||||
#define TRANSFORM_SUBSECONDS(INTERVAL_KIND, DEF_SCALE) \
|
||||
template<> \
|
||||
struct ToStartOfTransform<IntervalKind::INTERVAL_KIND> \
|
||||
{ \
|
||||
static Int64 execute(Int64 t, UInt64 delta, const UInt32 scale) \
|
||||
{ \
|
||||
if (scale <= DEF_SCALE) \
|
||||
{ \
|
||||
auto val = t * DecimalUtils::scaleMultiplier<DateTime64>(DEF_SCALE - scale); \
|
||||
if (delta == 1) \
|
||||
return val; \
|
||||
else \
|
||||
return val - (val % delta); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
return t - (t % (delta * DecimalUtils::scaleMultiplier<DateTime64>(scale - DEF_SCALE))) ; \
|
||||
} \
|
||||
} \
|
||||
};
|
||||
TRANSFORM_SUBSECONDS(Millisecond, 3)
|
||||
TRANSFORM_SUBSECONDS(Microsecond, 6)
|
||||
TRANSFORM_SUBSECONDS(Nanosecond, 9)
|
||||
#undef TRANSFORM_SUBSECONDS
|
||||
|
||||
template <IntervalKind::Kind unit>
|
||||
struct AddTime;
|
||||
|
||||
@ -117,6 +142,25 @@ struct ToStartOfTransform;
|
||||
ADD_TIME(Second, 1)
|
||||
#undef ADD_TIME
|
||||
|
||||
#define ADD_SUBSECONDS(INTERVAL_KIND, DEF_SCALE) \
|
||||
template <> \
|
||||
struct AddTime<IntervalKind::INTERVAL_KIND> \
|
||||
{ \
|
||||
static inline NO_SANITIZE_UNDEFINED Int64 execute(Int64 t, UInt64 delta, const UInt32 scale) \
|
||||
{ \
|
||||
if (scale < DEF_SCALE) \
|
||||
{ \
|
||||
return t + delta * DecimalUtils::scaleMultiplier<DateTime64>(DEF_SCALE - scale); \
|
||||
} \
|
||||
else \
|
||||
return t + delta * DecimalUtils::scaleMultiplier<DateTime64>(scale - DEF_SCALE); \
|
||||
} \
|
||||
};
|
||||
ADD_SUBSECONDS(Millisecond, 3)
|
||||
ADD_SUBSECONDS(Microsecond, 6)
|
||||
ADD_SUBSECONDS(Nanosecond, 9)
|
||||
#undef ADD_SUBSECONDS
|
||||
|
||||
template <TimeWindowFunctionName type>
|
||||
struct TimeWindowImpl
|
||||
{
|
||||
|
@ -11,6 +11,9 @@ void registerFunctionToDayOfWeek(FunctionFactory &);
|
||||
void registerFunctionToDayOfYear(FunctionFactory &);
|
||||
void registerFunctionToHour(FunctionFactory &);
|
||||
void registerFunctionToMinute(FunctionFactory &);
|
||||
void registerFunctionToStartOfNanosecond(FunctionFactory &);
|
||||
void registerFunctionToStartOfMicrosecond(FunctionFactory &);
|
||||
void registerFunctionToStartOfMillisecond(FunctionFactory &);
|
||||
void registerFunctionToStartOfSecond(FunctionFactory &);
|
||||
void registerFunctionToSecond(FunctionFactory &);
|
||||
void registerFunctionToStartOfDay(FunctionFactory &);
|
||||
@ -99,6 +102,9 @@ void registerFunctionsDateTime(FunctionFactory & factory)
|
||||
registerFunctionToStartOfMonth(factory);
|
||||
registerFunctionToStartOfQuarter(factory);
|
||||
registerFunctionToStartOfYear(factory);
|
||||
registerFunctionToStartOfNanosecond(factory);
|
||||
registerFunctionToStartOfMicrosecond(factory);
|
||||
registerFunctionToStartOfMillisecond(factory);
|
||||
registerFunctionToStartOfSecond(factory);
|
||||
registerFunctionToStartOfMinute(factory);
|
||||
registerFunctionToStartOfFiveMinute(factory);
|
||||
|
30
src/Functions/toStartOfSubsecond.cpp
Normal file
30
src/Functions/toStartOfSubsecond.cpp
Normal file
@ -0,0 +1,30 @@
|
||||
#include <Functions/FunctionFactory.h>
|
||||
#include <Functions/DateTimeTransforms.h>
|
||||
#include <Functions/FunctionDateOrDateTimeToSomething.h>
|
||||
|
||||
|
||||
namespace DB
|
||||
{
|
||||
|
||||
using FunctionToStartOfMillisecond = FunctionDateOrDateTimeToSomething<DataTypeDateTime64, ToStartOfMillisecondImpl>;
|
||||
|
||||
void registerFunctionToStartOfMillisecond(FunctionFactory & factory)
|
||||
{
|
||||
factory.registerFunction<FunctionToStartOfMillisecond>();
|
||||
}
|
||||
|
||||
using FunctionToStartOfMicrosecond = FunctionDateOrDateTimeToSomething<DataTypeDateTime64, ToStartOfMicrosecondImpl>;
|
||||
|
||||
void registerFunctionToStartOfMicrosecond(FunctionFactory & factory)
|
||||
{
|
||||
factory.registerFunction<FunctionToStartOfMicrosecond>();
|
||||
}
|
||||
|
||||
using FunctionToStartOfNanosecond = FunctionDateOrDateTimeToSomething<DataTypeDateTime64, ToStartOfNanosecondImpl>;
|
||||
|
||||
void registerFunctionToStartOfNanosecond(FunctionFactory & factory)
|
||||
{
|
||||
factory.registerFunction<FunctionToStartOfMillisecond>();
|
||||
}
|
||||
|
||||
}
|
@ -10,7 +10,7 @@ bool parseIntervalKind(IParser::Pos & pos, Expected & expected, IntervalKind & r
|
||||
if (ParserKeyword("NANOSECOND").ignore(pos, expected) || ParserKeyword("SQL_TSI_NANOSECOND").ignore(pos, expected)
|
||||
|| ParserKeyword("NS").ignore(pos, expected))
|
||||
{
|
||||
result = IntervalKind::Microsecond;
|
||||
result = IntervalKind::Nanosecond;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -57,6 +57,7 @@ namespace ErrorCodes
|
||||
{
|
||||
extern const int ARGUMENT_OUT_OF_BOUND;
|
||||
extern const int BAD_ARGUMENTS;
|
||||
extern const int SYNTAX_ERROR;
|
||||
extern const int ILLEGAL_COLUMN;
|
||||
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
||||
extern const int INCORRECT_QUERY;
|
||||
@ -778,11 +779,12 @@ UInt32 StorageWindowView::getWindowUpperBound(UInt32 time_sec)
|
||||
switch (window_interval_kind)
|
||||
{
|
||||
case IntervalKind::Nanosecond:
|
||||
throw Exception("Fractional seconds are not supported by windows yet", ErrorCodes::SYNTAX_ERROR);
|
||||
throw Exception("Fractional seconds are not supported by window view yet", ErrorCodes::SYNTAX_ERROR);
|
||||
case IntervalKind::Microsecond:
|
||||
throw Exception("Fractional seconds are not supported by windows yet", ErrorCodes::SYNTAX_ERROR);
|
||||
throw Exception("Fractional seconds are not supported by window view yet", ErrorCodes::SYNTAX_ERROR);
|
||||
case IntervalKind::Millisecond:
|
||||
throw Exception("Fractional seconds are not supported by windows yet", ErrorCodes::SYNTAX_ERROR);
|
||||
throw Exception("Fractional seconds are not supported by window view yet", ErrorCodes::SYNTAX_ERROR);
|
||||
|
||||
#define CASE_WINDOW_KIND(KIND) \
|
||||
case IntervalKind::KIND: \
|
||||
{ \
|
||||
|
Loading…
Reference in New Issue
Block a user