From 0832f6ec3999cabf75114f71fda9bfabd6e964bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabr=C3=ADzio=20de=20Royes=20Mello?= Date: Sun, 3 Mar 2024 17:53:29 -0300 Subject: [PATCH] Fix constify cagg_watermark using window functions When querying a realtime Continuous Aggregate using window functions the new planner optimization to constify the `cagg_watermark` function call is not working because we're checking for window function usage on the query tree but actually this is a current limitation when creating a new Continuous Aggregate, but users can use window functions when querying it. Fixed it by removing the check for `query->hasWindowFuncs` to prevent the process of constification of the `cagg_watermak` function call. Fixes #6722 --- .unreleased/pr_6726 | 2 ++ tsl/src/continuous_aggs/planner.c | 2 +- tsl/test/expected/cagg_watermark-13.out | 19 +++++++++++++++++++ tsl/test/expected/cagg_watermark-14.out | 19 +++++++++++++++++++ tsl/test/expected/cagg_watermark-15.out | 19 +++++++++++++++++++ tsl/test/expected/cagg_watermark-16.out | 19 +++++++++++++++++++ tsl/test/sql/cagg_watermark.sql.in | 4 ++++ 7 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 .unreleased/pr_6726 diff --git a/.unreleased/pr_6726 b/.unreleased/pr_6726 new file mode 100644 index 00000000000..0ef9596ccc5 --- /dev/null +++ b/.unreleased/pr_6726 @@ -0,0 +1,2 @@ +Fixes: #6726 Fix constify cagg_watermark using window function when querying a CAgg +Thanks: @rovo89 For reporting constify cagg_watermark not working using window function when querying a CAgg diff --git a/tsl/src/continuous_aggs/planner.c b/tsl/src/continuous_aggs/planner.c index aa51b234fa2..d3e30203d75 100644 --- a/tsl/src/continuous_aggs/planner.c +++ b/tsl/src/continuous_aggs/planner.c @@ -193,7 +193,7 @@ could_be_realtime_cagg_query(Query *query) if (query->commandType != CMD_SELECT) return false; - if (query->hasWindowFuncs || query->hasTargetSRFs) + if (query->hasTargetSRFs) return false; /* One range table, could be a query direct on a CAgg or a CTE expression */ diff --git a/tsl/test/expected/cagg_watermark-13.out b/tsl/test/expected/cagg_watermark-13.out index 08e2ef3a12e..d51d4a15f54 100644 --- a/tsl/test/expected/cagg_watermark-13.out +++ b/tsl/test/expected/cagg_watermark-13.out @@ -1801,3 +1801,22 @@ UNION ALL TRUNCATE _timescaledb_catalog.continuous_aggs_hypertable_invalidation_log; TRUNCATE _timescaledb_catalog.continuous_aggs_invalidation_threshold; \c :TEST_DBNAME :ROLE_DEFAULT_PERM_USER +-- Issue #6722: constify cagg_watermark using window func when querying a cagg +:EXPLAIN_ANALYZE +SELECT time_bucket, lead(count) OVER (ORDER BY time_bucket) FROM small_integer_ht_cagg; + QUERY PLAN +------------------------------------------------------------------------------------------------------------------------------------------------- + WindowAgg (actual rows=6 loops=1) + -> Sort (actual rows=6 loops=1) + Sort Key: _hyper_15_64_chunk.time_bucket + Sort Method: quicksort + -> Append (actual rows=6 loops=1) + -> Index Scan using _hyper_15_64_chunk__materialized_hypertable_15_time_bucket_idx on _hyper_15_64_chunk (actual rows=6 loops=1) + Index Cond: (time_bucket < '30'::bigint) + -> HashAggregate (actual rows=0 loops=1) + Group Key: time_bucket('5'::bigint, "time") + Batches: 1 + -> Result (actual rows=0 loops=1) + One-Time Filter: false +(12 rows) + diff --git a/tsl/test/expected/cagg_watermark-14.out b/tsl/test/expected/cagg_watermark-14.out index 08e2ef3a12e..d51d4a15f54 100644 --- a/tsl/test/expected/cagg_watermark-14.out +++ b/tsl/test/expected/cagg_watermark-14.out @@ -1801,3 +1801,22 @@ UNION ALL TRUNCATE _timescaledb_catalog.continuous_aggs_hypertable_invalidation_log; TRUNCATE _timescaledb_catalog.continuous_aggs_invalidation_threshold; \c :TEST_DBNAME :ROLE_DEFAULT_PERM_USER +-- Issue #6722: constify cagg_watermark using window func when querying a cagg +:EXPLAIN_ANALYZE +SELECT time_bucket, lead(count) OVER (ORDER BY time_bucket) FROM small_integer_ht_cagg; + QUERY PLAN +------------------------------------------------------------------------------------------------------------------------------------------------- + WindowAgg (actual rows=6 loops=1) + -> Sort (actual rows=6 loops=1) + Sort Key: _hyper_15_64_chunk.time_bucket + Sort Method: quicksort + -> Append (actual rows=6 loops=1) + -> Index Scan using _hyper_15_64_chunk__materialized_hypertable_15_time_bucket_idx on _hyper_15_64_chunk (actual rows=6 loops=1) + Index Cond: (time_bucket < '30'::bigint) + -> HashAggregate (actual rows=0 loops=1) + Group Key: time_bucket('5'::bigint, "time") + Batches: 1 + -> Result (actual rows=0 loops=1) + One-Time Filter: false +(12 rows) + diff --git a/tsl/test/expected/cagg_watermark-15.out b/tsl/test/expected/cagg_watermark-15.out index 7655817bdf8..179cb80b10e 100644 --- a/tsl/test/expected/cagg_watermark-15.out +++ b/tsl/test/expected/cagg_watermark-15.out @@ -1808,3 +1808,22 @@ UNION ALL TRUNCATE _timescaledb_catalog.continuous_aggs_hypertable_invalidation_log; TRUNCATE _timescaledb_catalog.continuous_aggs_invalidation_threshold; \c :TEST_DBNAME :ROLE_DEFAULT_PERM_USER +-- Issue #6722: constify cagg_watermark using window func when querying a cagg +:EXPLAIN_ANALYZE +SELECT time_bucket, lead(count) OVER (ORDER BY time_bucket) FROM small_integer_ht_cagg; + QUERY PLAN +------------------------------------------------------------------------------------------------------------------------------------------------- + WindowAgg (actual rows=6 loops=1) + -> Sort (actual rows=6 loops=1) + Sort Key: _hyper_15_64_chunk.time_bucket + Sort Method: quicksort + -> Append (actual rows=6 loops=1) + -> Index Scan using _hyper_15_64_chunk__materialized_hypertable_15_time_bucket_idx on _hyper_15_64_chunk (actual rows=6 loops=1) + Index Cond: (time_bucket < '30'::bigint) + -> HashAggregate (actual rows=0 loops=1) + Group Key: time_bucket('5'::bigint, "time") + Batches: 1 + -> Result (actual rows=0 loops=1) + One-Time Filter: false +(12 rows) + diff --git a/tsl/test/expected/cagg_watermark-16.out b/tsl/test/expected/cagg_watermark-16.out index 7655817bdf8..179cb80b10e 100644 --- a/tsl/test/expected/cagg_watermark-16.out +++ b/tsl/test/expected/cagg_watermark-16.out @@ -1808,3 +1808,22 @@ UNION ALL TRUNCATE _timescaledb_catalog.continuous_aggs_hypertable_invalidation_log; TRUNCATE _timescaledb_catalog.continuous_aggs_invalidation_threshold; \c :TEST_DBNAME :ROLE_DEFAULT_PERM_USER +-- Issue #6722: constify cagg_watermark using window func when querying a cagg +:EXPLAIN_ANALYZE +SELECT time_bucket, lead(count) OVER (ORDER BY time_bucket) FROM small_integer_ht_cagg; + QUERY PLAN +------------------------------------------------------------------------------------------------------------------------------------------------- + WindowAgg (actual rows=6 loops=1) + -> Sort (actual rows=6 loops=1) + Sort Key: _hyper_15_64_chunk.time_bucket + Sort Method: quicksort + -> Append (actual rows=6 loops=1) + -> Index Scan using _hyper_15_64_chunk__materialized_hypertable_15_time_bucket_idx on _hyper_15_64_chunk (actual rows=6 loops=1) + Index Cond: (time_bucket < '30'::bigint) + -> HashAggregate (actual rows=0 loops=1) + Group Key: time_bucket('5'::bigint, "time") + Batches: 1 + -> Result (actual rows=0 loops=1) + One-Time Filter: false +(12 rows) + diff --git a/tsl/test/sql/cagg_watermark.sql.in b/tsl/test/sql/cagg_watermark.sql.in index fd5f5d2dd5c..df38aba20d1 100644 --- a/tsl/test/sql/cagg_watermark.sql.in +++ b/tsl/test/sql/cagg_watermark.sql.in @@ -565,3 +565,7 @@ UNION ALL TRUNCATE _timescaledb_catalog.continuous_aggs_hypertable_invalidation_log; TRUNCATE _timescaledb_catalog.continuous_aggs_invalidation_threshold; \c :TEST_DBNAME :ROLE_DEFAULT_PERM_USER + +-- Issue #6722: constify cagg_watermark using window func when querying a cagg +:EXPLAIN_ANALYZE +SELECT time_bucket, lead(count) OVER (ORDER BY time_bucket) FROM small_integer_ht_cagg;