Skip to content

Commit da3e892

Browse files
committed
Refactor cagg bucket alignment code
Refactor the bucket alignment code to reduce duplication - Extract the logics that computes the bucket start and next bucket start into corresponding functions to use at a few places - Consolidate functions that compute bucket start for both fix and variable bucket. - Clean up the inscribe/circumscribe functions
1 parent 607bbd5 commit da3e892

7 files changed

Lines changed: 149 additions & 200 deletions

File tree

src/ts_catalog/continuous_agg.c

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,28 +1451,12 @@ void
14511451
ts_compute_inscribed_bucketed_refresh_window_variable(int64 *start, int64 *end,
14521452
const ContinuousAggBucketFunction *bf)
14531453
{
1454-
Datum start_old, end_old, start_aligned, end_aliged;
1455-
1456-
/*
1457-
* It's OK to use TIMESTAMPOID here. Variable-sized buckets can be used
1458-
* only for dates, timestamps and timestamptz's. For all these types our
1459-
* internal time representation is microseconds relative the UNIX epoch.
1460-
* So the results will be correct regardless of the actual type used in
1461-
* the CAGG. For more details see ts_internal_to_time_value() implementation.
1462-
*/
1463-
start_old = ts_internal_to_time_value(*start, TIMESTAMPOID);
1464-
end_old = ts_internal_to_time_value(*end, TIMESTAMPOID);
1465-
1466-
start_aligned = generic_time_bucket(bf, start_old);
1467-
end_aliged = generic_time_bucket(bf, end_old);
1468-
1469-
if (DatumGetTimestamp(start_aligned) != DatumGetTimestamp(start_old))
1454+
int64 start_aligned = ts_cagg_variable_current_bucket_start(*start, bf);
1455+
if (start_aligned != *start)
14701456
{
1471-
start_aligned = generic_add_interval(bf, start_aligned);
1457+
*start = ts_cagg_variable_next_bucket_start(*start, bf);
14721458
}
1473-
1474-
*start = ts_time_value_to_internal(start_aligned, TIMESTAMPOID);
1475-
*end = ts_time_value_to_internal(end_aliged, TIMESTAMPOID);
1459+
*end = ts_cagg_variable_current_bucket_start(*end, bf);
14761460
}
14771461

14781462
/*
@@ -1489,28 +1473,20 @@ void
14891473
ts_compute_circumscribed_bucketed_refresh_window_variable(int64 *start, int64 *end,
14901474
const ContinuousAggBucketFunction *bf)
14911475
{
1492-
Datum start_old, end_old, start_new, end_new;
1493-
1494-
/*
1495-
* It's OK to use TIMESTAMPOID here.
1496-
* See the comment in ts_compute_inscribed_bucketed_refresh_window_variable()
1497-
*/
1498-
start_old = ts_internal_to_time_value(*start, TIMESTAMPOID);
1499-
end_old = ts_internal_to_time_value(*end, TIMESTAMPOID);
1500-
start_new = generic_time_bucket(bf, start_old);
1501-
end_new = generic_time_bucket(bf, end_old);
1476+
*start = ts_cagg_variable_current_bucket_start(*start, bf);
1477+
int64 end_new = ts_cagg_variable_current_bucket_start(*end, bf);
15021478

15031479
/* Add interval to expand to next bucket if:
15041480
* 1. end wasn't at a bucket boundary (end moved during bucketing), OR
15051481
* 2. we have a single-point at a bucket boundary (start == end after bucketing) */
1506-
if (DatumGetTimestamp(end_new) != DatumGetTimestamp(end_old) ||
1507-
DatumGetTimestamp(start_new) == DatumGetTimestamp(end_new))
1482+
if (end_new != *end || *start == end_new)
15081483
{
1509-
end_new = generic_add_interval(bf, end_new);
1484+
*end = ts_cagg_variable_next_bucket_start(*end, bf);
1485+
}
1486+
else
1487+
{
1488+
*end = end_new;
15101489
}
1511-
1512-
*start = ts_time_value_to_internal(start_new, TIMESTAMPOID);
1513-
*end = ts_time_value_to_internal(end_new, TIMESTAMPOID);
15141490
}
15151491

15161492
/*
@@ -1521,15 +1497,14 @@ ts_compute_circumscribed_bucketed_refresh_window_variable(int64 *start, int64 *e
15211497
* val = time_bucket(bucket_size, val) + interval bucket_size
15221498
*/
15231499
int64
1524-
ts_compute_beginning_of_the_next_bucket_variable(int64 timeval,
1525-
const ContinuousAggBucketFunction *bf)
1500+
ts_cagg_variable_next_bucket_start(int64 timeval, const ContinuousAggBucketFunction *bf)
15261501
{
15271502
Datum val_new;
15281503
Datum val_old;
15291504

15301505
/*
15311506
* It's OK to use TIMESTAMPOID here.
1532-
* See the comment in ts_compute_inscribed_bucketed_refresh_window_variable()
1507+
* See the comment in ts_cagg_variable_current_bucket_start()
15331508
*/
15341509
val_old = ts_internal_to_time_value(timeval, TIMESTAMPOID);
15351510

@@ -1547,11 +1522,14 @@ ts_compute_beginning_of_the_next_bucket_variable(int64 timeval,
15471522
* val = time_bucket(bucket_size, val)
15481523
*/
15491524
int64
1550-
ts_compute_start_of_current_bucket_variable(int64 timeval, const ContinuousAggBucketFunction *bf)
1525+
ts_cagg_variable_current_bucket_start(int64 timeval, const ContinuousAggBucketFunction *bf)
15511526
{
15521527
/*
1553-
* It's OK to use TIMESTAMPOID here.
1554-
* See the comment in ts_compute_inscribed_bucketed_refresh_window_variable()
1528+
* It's OK to use TIMESTAMPOID here. Variable-sized buckets can be used
1529+
* only for dates, timestamps and timestamptz's. For all these types our
1530+
* internal time representation is microseconds relative the UNIX epoch.
1531+
* So the results will be correct regardless of the actual type used in
1532+
* the CAGG. For more details see ts_internal_to_time_value() implementation.
15551533
*/
15561534
Datum val_beg = ts_internal_to_time_value(timeval, TIMESTAMPOID);
15571535
return ts_time_value_to_internal(generic_time_bucket(bf, val_beg), TIMESTAMPOID);

src/ts_catalog/continuous_agg.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,10 @@ ts_compute_inscribed_bucketed_refresh_window_variable(int64 *start, int64 *end,
186186
extern TSDLLEXPORT void
187187
ts_compute_circumscribed_bucketed_refresh_window_variable(int64 *start, int64 *end,
188188
const ContinuousAggBucketFunction *bf);
189-
extern TSDLLEXPORT int64 ts_compute_beginning_of_the_next_bucket_variable(
190-
int64 timeval, const ContinuousAggBucketFunction *bf);
189+
extern TSDLLEXPORT int64 ts_cagg_variable_next_bucket_start(int64 timeval,
190+
const ContinuousAggBucketFunction *bf);
191191
extern TSDLLEXPORT int64
192-
ts_compute_start_of_current_bucket_variable(int64 timeval, const ContinuousAggBucketFunction *bf);
192+
ts_cagg_variable_current_bucket_start(int64 timeval, const ContinuousAggBucketFunction *bf);
193193

194194
extern TSDLLEXPORT Query *ts_continuous_agg_get_query(ContinuousAgg *cagg);
195195

src/ts_catalog/continuous_aggs_watermark.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,10 @@ cagg_compute_watermark(ContinuousAgg *cagg, int64 watermark, bool isnull)
142142
{
143143
/*
144144
* Since `value` is already bucketed, `bucketed = true` flag can
145-
* be added to ts_compute_beginning_of_the_next_bucket_variable() as
145+
* be added to ts_cagg_variable_next_bucket_start() as
146146
* an optimization, if necessary.
147147
*/
148-
watermark =
149-
ts_compute_beginning_of_the_next_bucket_variable(watermark, cagg->bucket_function);
148+
watermark = ts_cagg_variable_next_bucket_start(watermark, cagg->bucket_function);
150149
}
151150
else
152151
{

tsl/src/continuous_aggs/invalidation.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -512,10 +512,6 @@ invalidation_expand_to_bucket_boundaries(Invalidation *inv, Oid time_type_oid,
512512
int64 bucket_width = ts_continuous_agg_fixed_bucket_width(bucket_function);
513513
Assert(bucket_width > 0);
514514

515-
NullableDatum offset = INIT_NULL_DATUM;
516-
NullableDatum origin = INIT_NULL_DATUM;
517-
fill_bucket_offset_origin(bucket_function, time_type_oid, &offset, &origin);
518-
519515
/* Compute the start of the "first" bucket for the type. The min value
520516
* must be at the start of the "first" bucket or somewhere in the
521517
* bucket. If the min value falls on the exact start of the bucket we are
@@ -555,11 +551,9 @@ invalidation_expand_to_bucket_boundaries(Invalidation *inv, Oid time_type_oid,
555551
}
556552
else
557553
{
558-
inv->lowest_modified_value = ts_time_bucket_by_type_extended(bucket_width,
559-
inv->lowest_modified_value,
554+
inv->lowest_modified_value = cagg_fixed_current_bucket_start(inv->lowest_modified_value,
560555
time_type_oid,
561-
offset,
562-
origin);
556+
bucket_function);
563557
}
564558

565559
if (inv->greatest_modified_value < min_bucket_start)
@@ -574,11 +568,9 @@ invalidation_expand_to_bucket_boundaries(Invalidation *inv, Oid time_type_oid,
574568
}
575569
else
576570
{
577-
inv->greatest_modified_value = ts_time_bucket_by_type_extended(bucket_width,
578-
inv->greatest_modified_value,
571+
inv->greatest_modified_value = cagg_fixed_current_bucket_start(inv->greatest_modified_value,
579572
time_type_oid,
580-
offset,
581-
origin);
573+
bucket_function);
582574
inv->greatest_modified_value =
583575
ts_time_saturating_add(inv->greatest_modified_value, bucket_width - 1, time_type_oid);
584576
}

tsl/src/continuous_aggs/invalidation_threshold.c

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -264,27 +264,7 @@ invalidation_threshold_compute(const ContinuousAgg *cagg, const InternalTimeRang
264264
}
265265
else
266266
{
267-
if (cagg->bucket_function->bucket_fixed_interval == false)
268-
{
269-
return ts_compute_beginning_of_the_next_bucket_variable(maxval,
270-
cagg->bucket_function);
271-
}
272-
273-
int64 bucket_width = ts_continuous_agg_fixed_bucket_width(cagg->bucket_function);
274-
Assert(bucket_width > 0);
275-
NullableDatum offset = INIT_NULL_DATUM;
276-
NullableDatum origin = INIT_NULL_DATUM;
277-
fill_bucket_offset_origin(cagg->bucket_function,
278-
refresh_window->type,
279-
&offset,
280-
&origin);
281-
int64 bucket_start = ts_time_bucket_by_type_extended(bucket_width,
282-
maxval,
283-
refresh_window->type,
284-
offset,
285-
origin);
286-
/* Add one bucket to get to the end of the last bucket */
287-
return ts_time_saturating_add(bucket_start, bucket_width, refresh_window->type);
267+
return cagg_next_bucket_start(maxval, refresh_window->type, cagg->bucket_function);
288268
}
289269
}
290270

0 commit comments

Comments
 (0)