Skip to content

Commit daaa0be

Browse files
committed
Store buckets of samples in the TimeSeries samples table
A row of the samples tables now holds the samples of one series within one time bucket: `samples` (a sorted array merged by `timeSeriesGroupArray`), `bucket`, `min_time`, `max_time`, in an `AggregatingMergeTree` ordered by `(id, bucket)`. The bucket steps are set by `samples_bucket_step_seconds` and `recent_samples_bucket_step_seconds`. `timeSeriesSelector` returns `id, samples`, the new function `timeSeriesSliceSortedArray` cuts an interval out of a bucket.
1 parent c6e4b71 commit daaa0be

114 files changed

Lines changed: 3973 additions & 1328 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎src/AggregateFunctions/TimeSeries/AggregateFunctionTimeSeriesGroupArray.cpp‎

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include <AggregateFunctions/AggregateFunctionFactory.h>
44
#include <AggregateFunctions/FactoryHelpers.h>
5-
#include <Core/Settings.h>
65

76

87
namespace DB
@@ -11,13 +10,6 @@ namespace DB
1110
namespace ErrorCodes
1211
{
1312
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
14-
extern const int UNKNOWN_AGGREGATE_FUNCTION;
15-
}
16-
17-
namespace Setting
18-
{
19-
extern const SettingsBool enable_time_series_aggregate_functions;
20-
extern const SettingsBool enable_time_series_table;
2113
}
2214

2315

@@ -55,13 +47,10 @@ namespace
5547
"Illegal type {} of 2nd argument (value) for aggregate function {}", value_type->getName(), name);
5648
}
5749

58-
AggregateFunctionPtr createAggregateFunctionTimeseriesGroupArray(const std::string & name, const DataTypes & argument_types, const Array & parameters, const Settings * settings)
50+
AggregateFunctionPtr createAggregateFunctionTimeseriesGroupArray(const std::string & name, const DataTypes & argument_types, const Array & parameters, const Settings *)
5951
{
60-
if (settings && (*settings)[Setting::enable_time_series_aggregate_functions] == 0 && (*settings)[Setting::enable_time_series_table] == 0)
61-
throw Exception(
62-
ErrorCodes::UNKNOWN_AGGREGATE_FUNCTION,
63-
"Aggregate function {} is in private preview and disabled by default. Enable it with setting enable_time_series_aggregate_functions",
64-
name);
52+
/// The function is not gated by the `enable_time_series_aggregate_functions` setting: the TimeSeries table engine
53+
/// uses it in the `SimpleAggregateFunction` type of its inner tables, so the type must be parsed in any context.
6554

6655
assertNoParameters(name, parameters);
6756

@@ -110,7 +99,7 @@ The samples can be passed in one of three forms:
11099
If several samples have the same timestamp, only one of them is used: the sample with the greatest value. A NaN value loses to any other value, so a NaN value is used only if all samples at this timestamp are NaN.
111100
112101
<Note>
113-
This function is in private preview, enable it by setting `enable_time_series_aggregate_functions=true`.
102+
This function is in private preview.
114103
</Note>
115104
)";
116105
FunctionDocumentation::Syntax syntax = R"(
@@ -128,7 +117,6 @@ timeSeriesGroupArray(samples)
128117
{
129118
"Basic usage with individual values",
130119
R"(
131-
SET enable_time_series_aggregate_functions = 1;
132120
WITH
133121
[110, 120, 130, 140, 140, 100]::Array(UInt32) AS timestamps,
134122
[1, 6, 8, 17, 19, 5]::Array(Float32) AS values
@@ -150,7 +138,6 @@ FROM
150138
{
151139
"Passing multiple samples of timestamps and values as arrays of equal size",
152140
R"(
153-
SET enable_time_series_aggregate_functions = 1;
154141
WITH
155142
[110, 120, 130, 140, 140, 100]::Array(UInt32) AS timestamps,
156143
[1, 6, 8, 17, 19, 5]::Array(Float32) AS values

‎src/AggregateFunctions/TimeSeries/AggregateFunctionTimeSeriesGroupArray.h‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,16 @@ class AggregateFunctionTimeSeriesGroupArray final :
183183

184184
static DataTypePtr createResultType(const DataTypes & argument_types_)
185185
{
186-
/// With the single argument form the result type is the same as the type of that argument.
187186
if (argument_types_.size() == 1)
188-
return argument_types_[0];
187+
{
188+
/// This function can be called with `SimpleAggregateFunction(timeSeriesGroupArray, Array(Tuple(timestamp_type, value_type)))`
189+
/// as its only argument, in this case it should return `Array(Tuple(timestamp_type, value_type))`.
190+
const auto & array_type = typeid_cast<const DataTypeArray &>(*argument_types_[0]);
191+
const auto & tuple_type = typeid_cast<const DataTypeTuple &>(*array_type.getNestedType());
192+
if (tuple_type.hasExplicitNames())
193+
return std::make_shared<DataTypeArray>(std::make_shared<DataTypeTuple>(tuple_type.getElements(), tuple_type.getElementNames()));
194+
return std::make_shared<DataTypeArray>(std::make_shared<DataTypeTuple>(tuple_type.getElements()));
195+
}
189196

190197
const bool arrays_passed = (argument_types_[1]->getTypeId() == TypeIndex::Array);
191198
const auto & timestamp_type = arrays_passed ? typeid_cast<const DataTypeArray *>(argument_types_[0].get())->getNestedType() : argument_types_[0];

‎src/Common/DateLUTImpl.h‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -440,23 +440,6 @@ class DateLUTImpl
440440
return lut[toLUTIndex(v)];
441441
}
442442

443-
/// Round `value` down to a multiple of `divisor` (towards negative infinity).
444-
/// Integer division truncates towards zero, so for negative values we shift the result one step down.
445-
/// The computation goes through the remainder to avoid signed overflow when `value` is close to the
446-
/// minimum of the type - the natural expression `value + 1 - divisor` overflows there. Such values are
447-
/// far outside any valid date range, so on the boundary we saturate to the nearest representable multiple.
448-
template <typename DateOrTime, typename Divisor>
449-
static DateOrTime roundDownToMultiple(DateOrTime value, Divisor divisor)
450-
{
451-
if (value >= 0) [[likely]]
452-
return static_cast<DateOrTime>(value / divisor * divisor);
453-
454-
const Int64 v = static_cast<Int64>(value);
455-
const Int64 d = static_cast<Int64>(divisor);
456-
const Int64 rounded_towards_zero = v - v % d; /// A multiple of d in [v, 0], never overflows.
457-
return static_cast<DateOrTime>(roundDownNegativeToMultiple(v, rounded_towards_zero, d));
458-
}
459-
460443
/// Add `offset` to `base`, saturating at the boundaries of `Time` instead of overflowing (which is
461444
/// undefined behavior). Interval rounding reconstructs the result as `date + offset`; for arguments far
462445
/// outside any valid date range this sum can step just past the type boundary even though both operands
@@ -514,6 +497,23 @@ class DateLUTImpl
514497
auto getOffsetAtStartOfEpoch() const { return offset_at_start_of_epoch; }
515498
auto getTimeOffsetAtStartOfLUT() const { return offset_at_start_of_lut; }
516499

500+
/// Round `value` down to a multiple of `divisor` (towards negative infinity).
501+
/// Integer division truncates towards zero, so for negative values we shift the result one step down.
502+
/// The computation goes through the remainder to avoid signed overflow when `value` is close to the
503+
/// minimum of the type - the natural expression `value + 1 - divisor` overflows there. Such values are
504+
/// far outside any valid date range, so on the boundary we saturate to the nearest representable multiple.
505+
template <typename DateOrTime, typename Divisor>
506+
static DateOrTime roundDownToMultiple(DateOrTime value, Divisor divisor)
507+
{
508+
if (value >= 0) [[likely]]
509+
return static_cast<DateOrTime>(value / divisor * divisor);
510+
511+
const Int64 v = static_cast<Int64>(value);
512+
const Int64 d = static_cast<Int64>(divisor);
513+
const Int64 rounded_towards_zero = v - v % d; /// A multiple of d in [v, 0], never overflows.
514+
return static_cast<DateOrTime>(roundDownNegativeToMultiple(v, rounded_towards_zero, d));
515+
}
516+
517517
/// Round a negative `value` down to a multiple of `divisor` (towards negative infinity), given the
518518
/// already computed `rounded_towards_zero` == `value / divisor * divisor` (truncating division).
519519
/// Near the minimum of Int64 the next multiple down is not representable, so we saturate there.

0 commit comments

Comments
 (0)