Skip to content

Commit

Permalink
Work on var length
Browse files Browse the repository at this point in the history
  • Loading branch information
jnidzwetzki committed Feb 1, 2024
1 parent da8fb28 commit 3fe816f
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 11 deletions.
20 changes: 11 additions & 9 deletions src/ts_catalog/continuous_agg.c
Original file line number Diff line number Diff line change
Expand Up @@ -1484,8 +1484,9 @@ ts_continuous_agg_bucket_width(const ContinuousAgg *agg)
}

/*
* Calls one of time_bucket_ng() versions depending on the arguments. This is
* a common procedure used by ts_compute_* below.
* Calls the desired time bucket function depending on the arguments. If the experimental flag is
* set on ContinuousAggsBucketFunction, one of time_bucket_ng() versions is used. This is a common
* procedure used by ts_compute_* below.
*/
static Datum
generic_time_bucket(const ContinuousAggsBucketFunction *bf, Datum timestamp)
Expand Down Expand Up @@ -1628,7 +1629,8 @@ void
ts_compute_inscribed_bucketed_refresh_window_variable(int64 *start, int64 *end,
const ContinuousAggsBucketFunction *bf)
{
Datum start_old, end_old, start_new, end_new;
Datum start_old, end_old, start_aligned, end_aliged;

/*
* It's OK to use TIMESTAMPOID here. Variable-sized buckets can be used
* only for dates, timestamps and timestamptz's. For all these types our
Expand All @@ -1639,16 +1641,16 @@ ts_compute_inscribed_bucketed_refresh_window_variable(int64 *start, int64 *end,
start_old = ts_internal_to_time_value(*start, TIMESTAMPOID);
end_old = ts_internal_to_time_value(*end, TIMESTAMPOID);

start_new = generic_time_bucket(bf, start_old);
end_new = generic_time_bucket(bf, end_old);
start_aligned = generic_time_bucket(bf, start_old);
end_aliged = generic_time_bucket(bf, end_old);

if (DatumGetTimestamp(start_new) != DatumGetTimestamp(start_old))
if (DatumGetTimestamp(start_aligned) != DatumGetTimestamp(start_old))
{
start_new = generic_add_interval(bf, start_new);
start_aligned = generic_add_interval(bf, start_aligned);
}

*start = ts_time_value_to_internal(start_new, TIMESTAMPOID);
*end = ts_time_value_to_internal(end_new, TIMESTAMPOID);
*start = ts_time_value_to_internal(start_aligned, TIMESTAMPOID);
*end = ts_time_value_to_internal(end_aliged, TIMESTAMPOID);
}

/*
Expand Down
1 change: 0 additions & 1 deletion tsl/src/continuous_aggs/refresh.c
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,6 @@ continuous_agg_refresh_internal(ContinuousAgg *cagg, const InternalTimeRange *re
/* No bucketing when open ended */
if (!(start_isnull && end_isnull))
{
// TODO: Fixme
if (ts_continuous_agg_bucket_width_variable(cagg))
{
refresh_window = *refresh_window_arg;
Expand Down
77 changes: 76 additions & 1 deletion tsl/test/sql/cagg_query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ INSERT INTO temperature
FROM generate_series('2020-01-01 00:00:00 PST'::timestamptz,
'2020-01-01 23:59:59 PST','1m') time;

-- Crate CAggs
-- Create CAggs
CREATE MATERIALIZED VIEW cagg_4_hours
WITH (timescaledb.continuous, timescaledb.materialized_only = false) AS
SELECT time_bucket('4 hour', time), max(value)
Expand All @@ -343,14 +343,17 @@ CREATE MATERIALIZED VIEW cagg_4_hours_origin
GROUP BY 1 ORDER BY 1;

-- Query the CAggs and check that all buckets are materialized
SELECT time_bucket('4 hour', time), max(value) FROM temperature GROUP BY 1 ORDER BY 1;
SELECT * FROM cagg_4_hours;
ALTER MATERIALIZED VIEW cagg_4_hours SET (timescaledb.materialized_only=true);
SELECT * FROM cagg_4_hours;

SELECT time_bucket('4 hour', time, '30m'::interval), max(value) FROM temperature GROUP BY 1 ORDER BY 1;
SELECT * FROM cagg_4_hours_offset;
ALTER MATERIALIZED VIEW cagg_4_hours_offset SET (timescaledb.materialized_only=true);
SELECT * FROM cagg_4_hours_offset;

SELECT time_bucket('4 hour', time, '2000-01-01 01:00:00 PST'::timestamptz), max(value) FROM temperature GROUP BY 1 ORDER BY 1;
SELECT * FROM cagg_4_hours_origin;
ALTER MATERIALIZED VIEW cagg_4_hours_origin SET (timescaledb.materialized_only=true);
SELECT * FROM cagg_4_hours_origin;
Expand Down Expand Up @@ -386,3 +389,75 @@ SELECT * FROM cagg_4_hours_offset;
SELECT * FROM cagg_4_hours_origin;
ALTER MATERIALIZED VIEW cagg_4_hours_origin SET (timescaledb.materialized_only=true);
SELECT * FROM cagg_4_hours_origin;


--- Test with variable width buckets (use februrary, since hourly origins are not supported with variable sized buckets)
TRUNCATE temperature;
INSERT INTO temperature
SELECT time, 5
FROM generate_series('2000-02-01 01:00:00 PST'::timestamptz,
'2000-02-01 23:59:59 PST','1m') time;

INSERT INTO temperature
SELECT time, 6
FROM generate_series('2020-02-01 01:00:00 PST'::timestamptz,
'2020-02-01 23:59:59 PST','1m') time;

CREATE MATERIALIZED VIEW cagg_1_year
WITH (timescaledb.continuous, timescaledb.materialized_only = false) AS
SELECT time_bucket('1 year', time), max(value)
FROM temperature
GROUP BY 1 ORDER BY 1;

CREATE MATERIALIZED VIEW cagg_1_year_offset
WITH (timescaledb.continuous, timescaledb.materialized_only = false) AS
SELECT time_bucket('1 year', time, '30m'::interval), max(value)
FROM temperature
GROUP BY 1 ORDER BY 1;

CREATE MATERIALIZED VIEW cagg_1_year_origin
WITH (timescaledb.continuous, timescaledb.materialized_only = false) AS
SELECT time_bucket('1 year', time, origin=>'2000-01-02 01:00:00 PST'::timestamptz, timezone=>'PST'), max(value)
FROM temperature
GROUP BY 1 ORDER BY 1;

--- Check CAgg data
SELECT time_bucket('1 year', time), max(value) FROM temperature GROUP BY 1 ORDER BY 1;
SELECT * FROM cagg_1_year;
ALTER MATERIALIZED VIEW cagg_1_year SET (timescaledb.materialized_only=true);
SELECT * FROM cagg_1_year;

SELECT time_bucket('1 year', time, '30m'::interval), max(value) FROM temperature GROUP BY 1 ORDER BY 1;
SELECT * FROM cagg_1_year_offset;
ALTER MATERIALIZED VIEW cagg_1_year_offset SET (timescaledb.materialized_only=true);
SELECT * FROM cagg_1_year_offset;

SELECT time_bucket('1 year', time, origin=>'2000-02-01 01:00:00 PST'::timestamptz, timezone=>'PST'), max(value) FROM temperature GROUP BY 1 ORDER BY 1;
SELECT * FROM cagg_1_year_origin;
ALTER MATERIALIZED VIEW cagg_1_year_origin SET (timescaledb.materialized_only=true);
SELECT * FROM cagg_1_year_origin;

-- Check the real-time functionality
ALTER MATERIALIZED VIEW cagg_1_year SET (timescaledb.materialized_only=false);
ALTER MATERIALIZED VIEW cagg_1_year_offset SET (timescaledb.materialized_only=false);
ALTER MATERIALIZED VIEW cagg_1_year_origin SET (timescaledb.materialized_only=false);

-- Insert new data
INSERT INTO temperature values('2020-02-02 01:05:00 PST', 2222);
INSERT INTO temperature values('2020-02-02 01:35:00 PST', 5555);
INSERT INTO temperature values('2021-02-02 05:05:00 PST', 8888);

-- Query the CAggs
SELECT * FROM cagg_1_year;
SELECT * FROM cagg_1_year_offset;
SELECT * FROM cagg_1_year_origin;

-- Update materialized data
CALL refresh_continuous_aggregate('cagg_1_year', NULL, NULL);
CALL refresh_continuous_aggregate('cagg_1_year_offset', NULL, NULL);
CALL refresh_continuous_aggregate('cagg_1_year_origin', NULL, NULL);

-- Query the CAggs
SELECT * FROM cagg_1_year;
SELECT * FROM cagg_1_year_offset;
SELECT * FROM cagg_1_year_origin;

0 comments on commit 3fe816f

Please sign in to comment.