Skip to content

Commit

Permalink
Fix segfault creating cagg with NULL bucket width
Browse files Browse the repository at this point in the history
When creating a Continuous Aggregate using a NULL `bucket_width` in the
`time_bucket` function it lead to a segfault, for example:

CREATE MATERIALIZED VIEW cagg WITH (timescaledb.continuous) AS
SELECT time_bucket(NULL, time), count(*)
FROM metrics
GROUP BY 1;

Fixed it by raising an ERROR if a NULL `bucked_width` is used during the
Continuous Aggregate query validation.
  • Loading branch information
fabriziomello committed Nov 9, 2023
1 parent b423d14 commit d0021d5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions .unreleased/pr_6297
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes: #6297 Fix segfault when creating a cagg using a NULL width in time bucket function
5 changes: 5 additions & 0 deletions tsl/src/continuous_aggs/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@ caggtimebucket_validate(CAggTimebucketInfo *tbinfo, List *groupClause, List *tar
Const *width = castNode(Const, width_arg);
tbinfo->bucket_width_type = width->consttype;

if (width->constisnull)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("invalid bucket width for time bucket function")));

if (width->consttype == INTERVALOID)
{
tbinfo->interval = DatumGetIntervalP(width->constvalue);
Expand Down
8 changes: 8 additions & 0 deletions tsl/test/expected/cagg_errors.out
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,14 @@ SELECT time_bucket('1 week', timec)
GROUP BY time_bucket('1 week', timec);
ERROR: cannot create continuous aggregate with CREATE VIEW
HINT: Use CREATE MATERIALIZED VIEW to create a continuous aggregate.
-- invalid `bucket_width` for `time_bucket` function
CREATE MATERIALIZED VIEW mat_m1 WITH (timescaledb.continuous, timescaledb.materialized_only=false)
AS
SELECT time_bucket(NULL, timec), sum(temperature), min(location)
FROM conditions
GROUP BY 1
WITH NO DATA;
ERROR: invalid bucket width for time bucket function
-- row security on table
create table rowsec_tab( a bigint, b integer, c integer);
select table_name from create_hypertable( 'rowsec_tab', 'a', chunk_time_interval=>10);
Expand Down
8 changes: 8 additions & 0 deletions tsl/test/sql/cagg_errors.sql
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,14 @@ SELECT time_bucket('1 week', timec)
FROM conditions
GROUP BY time_bucket('1 week', timec);

-- invalid `bucket_width` for `time_bucket` function
CREATE MATERIALIZED VIEW mat_m1 WITH (timescaledb.continuous, timescaledb.materialized_only=false)
AS
SELECT time_bucket(NULL, timec), sum(temperature), min(location)
FROM conditions
GROUP BY 1
WITH NO DATA;

-- row security on table
create table rowsec_tab( a bigint, b integer, c integer);
select table_name from create_hypertable( 'rowsec_tab', 'a', chunk_time_interval=>10);
Expand Down

0 comments on commit d0021d5

Please sign in to comment.