Skip to content

Commit 3f55ccc

Browse files
committed
Also guard NULL bucket width in cagg rewrite path
The previous guard only fired when creating a continuous aggregate. The cagg rewrite planner hook calls the same parameter handler with is_cagg_create=false, so a NULL interval width passed straight through and later crashed in time_bucket_info_has_fixed_width when it read bf->bucket_time_width->month. Bail out as soon as we see a NULL width, raise the error for any non-rewrite caller, and let the rewrite path simply skip rewriting because bf->bucket_function stays unset. Found by the LLM fuzzer.
1 parent 2d0ff8e commit 3f55ccc

3 files changed

Lines changed: 54 additions & 35 deletions

File tree

tsl/src/continuous_aggs/common.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -403,25 +403,24 @@ process_timebucket_parameters(FuncExpr *fe, ContinuousAggBucketFunction *bf, boo
403403

404404
if (width->constisnull)
405405
{
406-
if (process_checks && is_cagg_create)
406+
if (process_checks && !for_rewrites)
407407
{
408408
ereport(ERROR,
409409
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
410410
errmsg("invalid bucket width for time bucket function")));
411411
}
412+
return;
412413
}
413-
else
414+
415+
if (width->consttype == INTERVALOID)
414416
{
415-
if (width->consttype == INTERVALOID)
416-
{
417-
bf->bucket_time_width = DatumGetIntervalP(width->constvalue);
418-
}
417+
bf->bucket_time_width = DatumGetIntervalP(width->constvalue);
418+
}
419419

420-
if (!IS_TIME_BUCKET_INFO_TIME_BASED(bf))
421-
{
422-
bf->bucket_integer_width =
423-
ts_interval_value_to_internal(width->constvalue, width->consttype);
424-
}
420+
if (!IS_TIME_BUCKET_INFO_TIME_BASED(bf))
421+
{
422+
bf->bucket_integer_width =
423+
ts_interval_value_to_internal(width->constvalue, width->consttype);
425424
}
426425
}
427426
else if (process_checks)

tsl/test/expected/cagg_rewrites.out

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -740,14 +740,26 @@ psql:include/cagg_rewrites_error.sql:110: INFO: Query cannot be rewritten with
740740
------------------------------+---------------------+-------
741741
Fri Feb 23 08:00:00 2018 PST | 31.0000000000000000 | 1
742742

743+
-- NULL bucket width must not be rewritten (and must not crash, see issue
744+
-- found by the LLM fuzzer)
745+
SELECT time_bucket(NULL::interval, day) AS bucket,
746+
count(*)
747+
FROM conditions
748+
GROUP BY bucket
749+
ORDER BY 1 LIMIT 1;
750+
psql:include/cagg_rewrites_error.sql:118: INFO: Query cannot be rewritten with CAggs:
751+
bucket | count
752+
--------+-------
753+
| 14
754+
743755
-- infinity origin
744756
SELECT time_bucket(INTERVAL '3 days', day, 'infinity'::timestamptz) AS bucket,
745757
AVG(temperature),
746758
count(device_id)
747759
FROM conditions
748760
GROUP BY bucket
749761
ORDER BY 1 LIMIT 1;
750-
psql:include/cagg_rewrites_error.sql:118: INFO: Query cannot be rewritten with CAggs: invalid time bucket origin value: infinity
762+
psql:include/cagg_rewrites_error.sql:126: INFO: Query cannot be rewritten with CAggs: invalid time bucket origin value: infinity
751763
bucket | avg | count
752764
-------------------------------------+---------------------+-------
753765
Fri Jun 11 21:00:54.775807 2021 PDT | 26.0000000000000000 | 1
@@ -759,7 +771,7 @@ SELECT time_bucket(INTERVAL '3 days', day, "offset"=>'30m'::interval, origin=>'2
759771
FROM conditions
760772
GROUP BY bucket
761773
ORDER BY 1 LIMIT 1;
762-
psql:include/cagg_rewrites_error.sql:126: INFO: Query cannot be rewritten with CAggs: using offset and origin in a time_bucket function at the same time is not supported
774+
psql:include/cagg_rewrites_error.sql:134: INFO: Query cannot be rewritten with CAggs: using offset and origin in a time_bucket function at the same time is not supported
763775
bucket | avg | count
764776
------------------------------+---------------------+-------
765777
Sat Jun 12 02:30:00 2021 PDT | 24.0000000000000000 | 2
@@ -772,7 +784,7 @@ SELECT time_bucket(INTERVAL '3 days', day) AS bucket,
772784
FROM conditions_dup
773785
GROUP BY bucket
774786
ORDER BY 1 LIMIT 1;
775-
psql:include/cagg_rewrites_error.sql:136: INFO: Query cannot be rewritten with CAggs: no continuous aggregates defined on "public.conditions_dup"
787+
psql:include/cagg_rewrites_error.sql:144: INFO: Query cannot be rewritten with CAggs: no continuous aggregates defined on "public.conditions_dup"
776788
bucket | avg | count
777789
------------------------------+---------------------+-------
778790
Sun Jun 13 17:00:00 2021 PDT | 24.0000000000000000 | 3
@@ -785,7 +797,7 @@ SELECT time_bucket(INTERVAL '1 day', day) AS bucket,
785797
FROM conditions
786798
GROUP BY bucket
787799
ORDER BY 1 LIMIT 1;
788-
psql:include/cagg_rewrites_error.sql:146: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions" are matching the query:
800+
psql:include/cagg_rewrites_error.sql:154: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions" are matching the query:
789801
Caggs are not real-time: "public.cagg_view"
790802
Buckets do not match: "public.cagg1_tz" "public.cagg1_origin" "public.cagg2" "public.cagg3"
791803
Joins do not match: "public.cagg_join" "public.cagg_more_conds" "public.cagg_lateral"
@@ -800,7 +812,7 @@ SELECT time_bucket(INTERVAL '1 day', day) AS bucket,
800812
FROM conditions
801813
GROUP BY bucket, city
802814
ORDER BY 1 LIMIT 1;
803-
psql:include/cagg_rewrites_error.sql:152: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions" are matching the query:
815+
psql:include/cagg_rewrites_error.sql:160: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions" are matching the query:
804816
Caggs are not real-time: "public.cagg_view"
805817
Buckets do not match: "public.cagg1_tz" "public.cagg1_origin" "public.cagg2" "public.cagg3"
806818
Joins do not match: "public.cagg_join" "public.cagg_more_conds" "public.cagg_lateral"
@@ -816,7 +828,7 @@ SELECT time_bucket(INTERVAL '1 day', day) AS bucket,
816828
FROM conditions
817829
GROUP BY device_id, bucket
818830
ORDER BY 1 LIMIT 1;
819-
psql:include/cagg_rewrites_error.sql:159: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions" are matching the query:
831+
psql:include/cagg_rewrites_error.sql:167: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions" are matching the query:
820832
Caggs are not real-time: "public.cagg_view"
821833
Buckets do not match: "public.cagg1_tz" "public.cagg1_origin" "public.cagg2" "public.cagg3"
822834
Joins do not match: "public.cagg_join" "public.cagg_more_conds" "public.cagg_lateral"
@@ -834,7 +846,7 @@ FROM conditions
834846
GROUP BY bucket
835847
HAVING count(device_id) > 0
836848
ORDER BY 1 LIMIT 1;
837-
psql:include/cagg_rewrites_error.sql:168: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions" are matching the query:
849+
psql:include/cagg_rewrites_error.sql:176: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions" are matching the query:
838850
Buckets do not match: "public.cagg1" "public.cagg1_tz" "public.cagg1_origin" "public.cagg2" "public.cagg_join" "public.cagg_more_conds" "public.cagg_view" "public.cagg_lateral"
839851
HAVING quals do not match: "public.cagg3"
840852

@@ -854,7 +866,7 @@ WHERE location_id > 1 AND
854866
conditions.temperature > 28
855867
GROUP BY devices.name, bucket, devices.device_id
856868
ORDER BY 1 LIMIT 1;
857-
psql:include/cagg_rewrites_error.sql:181: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions" are matching the query:
869+
psql:include/cagg_rewrites_error.sql:189: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions" are matching the query:
858870
Caggs are not real-time: "public.cagg_view"
859871
Buckets do not match: "public.cagg1_tz" "public.cagg1_origin" "public.cagg2" "public.cagg3"
860872
Joins do not match: "public.cagg1" "public.cagg_join" "public.cagg_lateral"
@@ -875,7 +887,7 @@ WHERE location_id > 1 AND
875887
conditions.temperature < 20
876888
GROUP BY devices.name, bucket, devices.device_id
877889
ORDER BY 1 LIMIT 1;
878-
psql:include/cagg_rewrites_error.sql:193: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions" are matching the query:
890+
psql:include/cagg_rewrites_error.sql:201: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions" are matching the query:
879891
Caggs are not real-time: "public.cagg_view"
880892
Buckets do not match: "public.cagg1_tz" "public.cagg1_origin" "public.cagg2" "public.cagg3"
881893
Joins do not match: "public.cagg1" "public.cagg_join" "public.cagg_lateral"
@@ -896,7 +908,7 @@ WHERE location_id > 1 AND
896908
GROUP BY devices.name, bucket, devices.device_id
897909
ORDER BY 1,2,3,4
898910
LIMIT 2;
899-
psql:include/cagg_rewrites_error.sql:206: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions" are matching the query:
911+
psql:include/cagg_rewrites_error.sql:214: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions" are matching the query:
900912
Caggs are not real-time: "public.cagg_view"
901913
Buckets do not match: "public.cagg1_tz" "public.cagg1_origin" "public.cagg2" "public.cagg3"
902914
Joins do not match: "public.cagg1" "public.cagg_join" "public.cagg_lateral"
@@ -920,7 +932,7 @@ WHERE location_id < 1 AND
920932
GROUP BY devices.name, bucket, devices.device_id
921933
ORDER BY 1,2,3,4
922934
LIMIT 2;
923-
psql:include/cagg_rewrites_error.sql:220: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions" are matching the query:
935+
psql:include/cagg_rewrites_error.sql:228: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions" are matching the query:
924936
Caggs are not real-time: "public.cagg_view"
925937
Buckets do not match: "public.cagg1_tz" "public.cagg1_origin" "public.cagg2" "public.cagg3"
926938
Joins do not match: "public.cagg1" "public.cagg_join" "public.cagg_lateral"
@@ -936,7 +948,7 @@ SELECT time_bucket(INTERVAL '1 day', day, "offset"=>'15s'::interval, timezone=>
936948
FROM conditions
937949
GROUP BY device_id, bucket
938950
ORDER BY 1, 2, 3 LIMIT 3;
939-
psql:include/cagg_rewrites_error.sql:228: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions" are matching the query:
951+
psql:include/cagg_rewrites_error.sql:236: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions" are matching the query:
940952
Buckets do not match: "public.cagg1" "public.cagg1_tz" "public.cagg1_origin" "public.cagg2" "public.cagg3" "public.cagg_join" "public.cagg_more_conds" "public.cagg_view" "public.cagg_lateral"
941953

942954
bucket | avg | device_id
@@ -952,7 +964,7 @@ SELECT time_bucket(INTERVAL '1 day', day, timezone=>'Australia/Sydney') AS bucke
952964
FROM conditions
953965
GROUP BY device_id, bucket
954966
ORDER BY 1, 2, 3 LIMIT 3;
955-
psql:include/cagg_rewrites_error.sql:236: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions" are matching the query:
967+
psql:include/cagg_rewrites_error.sql:244: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions" are matching the query:
956968
Buckets do not match: "public.cagg1" "public.cagg1_tz" "public.cagg1_origin" "public.cagg2" "public.cagg3" "public.cagg_join" "public.cagg_more_conds" "public.cagg_view" "public.cagg_lateral"
957969

958970
bucket | avg | device_id
@@ -968,7 +980,7 @@ SELECT time_bucket(INTERVAL '1 day', day, "offset"=>'30m'::interval, timezone=>
968980
FROM conditions
969981
GROUP BY device_id, bucket
970982
ORDER BY 1, 2, 3 LIMIT 3;
971-
psql:include/cagg_rewrites_error.sql:244: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions" are matching the query:
983+
psql:include/cagg_rewrites_error.sql:252: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions" are matching the query:
972984
Buckets do not match: "public.cagg1" "public.cagg1_tz" "public.cagg1_origin" "public.cagg2" "public.cagg3" "public.cagg_join" "public.cagg_more_conds" "public.cagg_view" "public.cagg_lateral"
973985

974986
bucket | avg | device_id
@@ -984,7 +996,7 @@ SELECT time_bucket(INTERVAL '1 day', day, origin=>'2001-01-03') AS bucket,
984996
FROM conditions
985997
GROUP BY device_id, bucket
986998
ORDER BY 1, 2, 3 LIMIT 3;
987-
psql:include/cagg_rewrites_error.sql:252: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions" are matching the query:
999+
psql:include/cagg_rewrites_error.sql:260: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions" are matching the query:
9881000
Buckets do not match: "public.cagg1" "public.cagg1_tz" "public.cagg1_origin" "public.cagg2" "public.cagg3" "public.cagg_join" "public.cagg_more_conds" "public.cagg_view" "public.cagg_lateral"
9891001

9901002
bucket | avg | device_id
@@ -999,7 +1011,7 @@ SELECT time_bucket(INTERVAL '1 day', day, origin=>'2001-01-02 00:00:00 Europe/Be
9991011
FROM conditions
10001012
GROUP BY device_id, bucket
10011013
ORDER BY 1, 2, 3 LIMIT 3;
1002-
psql:include/cagg_rewrites_error.sql:259: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions" are matching the query:
1014+
psql:include/cagg_rewrites_error.sql:267: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions" are matching the query:
10031015
Buckets do not match: "public.cagg1" "public.cagg1_tz" "public.cagg1_origin" "public.cagg2" "public.cagg3" "public.cagg_join" "public.cagg_more_conds" "public.cagg_view" "public.cagg_lateral"
10041016

10051017
bucket | avg | device_id
@@ -1014,7 +1026,7 @@ SELECT time_bucket(3, day, 2) AS bucket,
10141026
count(device_id)
10151027
FROM conditions_int
10161028
GROUP BY bucket ORDER BY 1, 2, 3;
1017-
psql:include/cagg_rewrites_error.sql:266: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions_int" are matching the query:
1029+
psql:include/cagg_rewrites_error.sql:274: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions_int" are matching the query:
10181030
Buckets do not match: "public.cagg3_int"
10191031

10201032
bucket | avg | count
@@ -1031,7 +1043,7 @@ SELECT time_bucket(3, day) AS bucket,
10311043
count(device_id)
10321044
FROM conditions_int
10331045
GROUP BY bucket ORDER BY 1, 2, 3;
1034-
psql:include/cagg_rewrites_error.sql:272: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions_int" are matching the query:
1046+
psql:include/cagg_rewrites_error.sql:280: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions_int" are matching the query:
10351047
Buckets do not match: "public.cagg3_int"
10361048

10371049
bucket | avg | count
@@ -1048,7 +1060,7 @@ SELECT time_bucket(INTERVAL '1 day', bucket) AS bucket,
10481060
FROM cagg1, devices
10491061
WHERE devices.device_id = cagg1.device_id AND devices.device_id > 1
10501062
GROUP BY 1 ORDER BY 1 LIMIT 1;
1051-
psql:include/cagg_rewrites_error.sql:279: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.cagg1" are matching the query:
1063+
psql:include/cagg_rewrites_error.sql:287: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.cagg1" are matching the query:
10521064
WHERE/JOIN quals do not match: "public.cagg_on_cagg1"
10531065

10541066
bucket | temperature
@@ -1069,7 +1081,7 @@ GROUP BY devices.name, bucket, devices.device_id
10691081
ORDER BY 1,2,3,4
10701082
LIMIT 2;
10711083
EXECUTE prep1(28);
1072-
psql:include/cagg_rewrites_error.sql:295: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions" are matching the query:
1084+
psql:include/cagg_rewrites_error.sql:303: INFO: Query cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions" are matching the query:
10731085
Caggs are not real-time: "public.cagg_view"
10741086
Buckets do not match: "public.cagg1_tz" "public.cagg1_origin" "public.cagg2" "public.cagg3"
10751087
Joins do not match: "public.cagg1" "public.cagg_join" "public.cagg_lateral"
@@ -1094,7 +1106,7 @@ GROUP BY devices.name, bucket, devices.device_id
10941106
ORDER BY 1,2,3,4
10951107
LIMIT 2;
10961108
EXECUTE prep2(INTERVAL '1 day');
1097-
psql:include/cagg_rewrites_error.sql:311: INFO: Query cannot be rewritten with CAggs: non-immutable expression as first argument to the time bucket function
1109+
psql:include/cagg_rewrites_error.sql:319: INFO: Query cannot be rewritten with CAggs: non-immutable expression as first argument to the time bucket function
10981110
bucket | avg | count | name
10991111
------------------------------+---------------------+-------+----------
11001112
Sun Jun 20 17:00:00 2021 PDT | 31.0000000000000000 | 1 | thermo_1
@@ -1115,9 +1127,9 @@ SELECT * FROM (VALUES (1), (1)) a(v),
11151127
GROUP BY devices.name, bucket, devices.device_id) q
11161128
ORDER BY 1,2,3,4
11171129
LIMIT 2;
1118-
psql:include/cagg_rewrites_error.sql:327: INFO: Query cannot be rewritten with CAggs: no GROUP BY clause in the query
1119-
psql:include/cagg_rewrites_error.sql:327: INFO: Subquery "a" cannot be rewritten with CAggs: no GROUP BY clause in the query
1120-
psql:include/cagg_rewrites_error.sql:327: INFO: Subquery "q" cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions" are matching the query:
1130+
psql:include/cagg_rewrites_error.sql:335: INFO: Query cannot be rewritten with CAggs: no GROUP BY clause in the query
1131+
psql:include/cagg_rewrites_error.sql:335: INFO: Subquery "a" cannot be rewritten with CAggs: no GROUP BY clause in the query
1132+
psql:include/cagg_rewrites_error.sql:335: INFO: Subquery "q" cannot be rewritten with CAggs: none of continuous aggregates defined on "public.conditions" are matching the query:
11211133
Caggs are not real-time: "public.cagg_view"
11221134
Buckets do not match: "public.cagg1_tz" "public.cagg1_origin" "public.cagg2" "public.cagg3"
11231135
Joins do not match: "public.cagg1" "public.cagg_join" "public.cagg_lateral"

tsl/test/sql/include/cagg_rewrites_error.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ GROUP BY bucket
109109
HAVING count(device_id) > 0
110110
ORDER BY 1 LIMIT 1;
111111

112+
-- NULL bucket width must not be rewritten (and must not crash, see issue
113+
-- found by the LLM fuzzer)
114+
SELECT time_bucket(NULL::interval, day) AS bucket,
115+
count(*)
116+
FROM conditions
117+
GROUP BY bucket
118+
ORDER BY 1 LIMIT 1;
119+
112120
-- infinity origin
113121
SELECT time_bucket(INTERVAL '3 days', day, 'infinity'::timestamptz) AS bucket,
114122
AVG(temperature),

0 commit comments

Comments
 (0)