2019-11-16 13:54:52 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/types.h>
|
|
|
|
#include <base/EnumReflection.h>
|
2019-11-16 13:54:52 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
/// Kind of a temporal interval.
|
|
|
|
struct IntervalKind
|
|
|
|
{
|
|
|
|
enum Kind
|
|
|
|
{
|
2022-02-06 12:14:18 +00:00
|
|
|
Nanosecond,
|
|
|
|
Microsecond,
|
|
|
|
Millisecond,
|
2019-11-16 13:54:52 +00:00
|
|
|
Second,
|
|
|
|
Minute,
|
|
|
|
Hour,
|
|
|
|
Day,
|
|
|
|
Week,
|
|
|
|
Month,
|
|
|
|
Quarter,
|
|
|
|
Year,
|
|
|
|
};
|
|
|
|
Kind kind = Second;
|
|
|
|
|
2022-03-11 21:47:28 +00:00
|
|
|
IntervalKind(Kind kind_ = Second) : kind(kind_) {} /// NOLINT
|
|
|
|
operator Kind() const { return kind; } /// NOLINT
|
2019-11-16 13:54:52 +00:00
|
|
|
|
2021-09-06 14:24:03 +00:00
|
|
|
constexpr std::string_view toString() const { return magic_enum::enum_name(kind); }
|
2019-11-16 13:54:52 +00:00
|
|
|
|
|
|
|
/// Returns number of seconds in one interval.
|
|
|
|
/// For `Month`, `Quarter` and `Year` the function returns an average number of seconds.
|
2022-06-29 05:56:15 +00:00
|
|
|
Int32 toAvgSeconds() const;
|
2019-11-16 13:54:52 +00:00
|
|
|
|
|
|
|
/// Chooses an interval kind based on number of seconds.
|
|
|
|
/// For example, `IntervalKind::fromAvgSeconds(3600)` returns `IntervalKind::Hour`.
|
|
|
|
static IntervalKind fromAvgSeconds(Int64 num_seconds);
|
|
|
|
|
|
|
|
/// Returns an uppercased version of what `toString()` returns.
|
|
|
|
const char * toKeyword() const;
|
|
|
|
|
2020-06-05 17:57:33 +00:00
|
|
|
const char * toLowercasedKeyword() const;
|
|
|
|
|
2019-11-16 13:54:52 +00:00
|
|
|
/// Returns the string which can be passed to the `unit` parameter of the dateDiff() function.
|
|
|
|
/// For example, `IntervalKind{IntervalKind::Day}.getDateDiffParameter()` returns "day".
|
|
|
|
const char * toDateDiffUnit() const;
|
|
|
|
|
|
|
|
/// Returns the name of the function converting a number to the interval data type.
|
|
|
|
/// For example, `IntervalKind{IntervalKind::Day}.getToIntervalDataTypeFunctionName()`
|
|
|
|
/// returns "toIntervalDay".
|
|
|
|
const char * toNameOfFunctionToIntervalDataType() const;
|
|
|
|
|
|
|
|
/// Returns the name of the function extracting time part from a date or a time.
|
|
|
|
/// For example, `IntervalKind{IntervalKind::Day}.getExtractTimePartFunctionName()`
|
|
|
|
/// returns "toDayOfMonth".
|
|
|
|
const char * toNameOfFunctionExtractTimePart() const;
|
2020-08-12 13:50:54 +00:00
|
|
|
|
|
|
|
/// Converts the string representation of an interval kind to its IntervalKind equivalent.
|
|
|
|
/// Returns false if the conversion unsucceeded.
|
|
|
|
/// For example, `IntervalKind::tryParseString('second', result)` returns `result` equals `IntervalKind::Kind::Second`.
|
|
|
|
static bool tryParseString(const std::string & kind, IntervalKind::Kind & result);
|
2019-11-16 13:54:52 +00:00
|
|
|
};
|
2021-10-31 16:22:20 +00:00
|
|
|
|
2022-03-11 21:47:28 +00:00
|
|
|
/// NOLINTNEXTLINE
|
2021-10-31 16:22:20 +00:00
|
|
|
#define FOR_EACH_INTERVAL_KIND(M) \
|
2022-02-06 12:14:18 +00:00
|
|
|
M(Nanosecond) \
|
|
|
|
M(Microsecond) \
|
|
|
|
M(Millisecond) \
|
2021-10-31 16:22:20 +00:00
|
|
|
M(Second) \
|
|
|
|
M(Minute) \
|
|
|
|
M(Hour) \
|
|
|
|
M(Day) \
|
|
|
|
M(Week) \
|
|
|
|
M(Month) \
|
|
|
|
M(Quarter) \
|
|
|
|
M(Year)
|
|
|
|
|
2019-11-16 13:54:52 +00:00
|
|
|
}
|