timescale / timescaledb-toolkit

Extension for more hyperfunctions, fully compatible with TimescaleDB and PostgreSQL 📈
https://www.timescale.com
Other
385 stars 47 forks source link

raw_mcv_agg() ERROR: called `Option::unwrap()` on a `None` value when filter clause is used #822

Open fordfrog opened 1 month ago

fordfrog commented 1 month ago

timescaledb toolkit is the latest from the main branch as of now (1.19.0-dev - beac6d58a1077e082824083b19c0d216cefad5f6). timescaledb is 2.17.0

when refreshing an aggregate, i get this error:

ERROR:  called `Option::unwrap()` on a `None` value
CONTEXT:  SQL statement "INSERT INTO _timescaledb_internal._materialized_hypertable_28 SELECT * FROM _timescaledb_internal._partial_view_28 AS I WHERE I.bar_start >= $1 AND I.bar_start < $2 ;"
SQL statement "CALL refresh_continuous_aggregate(p_aggregate_name, start_timestamp, end_timestamp)"
PL/pgSQL function refresh_aggregates(text,timestamp without time zone,timestamp without time zone,interval) line 7 at CALL

it is caused by this statement in the aggregate sql:

raw_mcv_agg(100, bid_price) FILTER (WHERE (bid_price <> (0)::double precision)) AS bid_price_freq

i tracked down the issue to one ticker that has only one record for the specified period and that record is filtered out by the filter clause. this is the complete simplified select:

SELECT ticker,
    time_bucket('00:00:01'::interval, "timestamp") AS bar_start,
    raw_mcv_agg(100, bid_price) FILTER (WHERE (bid_price <> (0)::double precision)) AS bid_price_freq
   FROM ticks_bid_ask
  WHERE "timestamp" >= '2024-06-03 04:00:00' and "timestamp" < '2024-06-03 05:00:00'
  GROUP BY ticker, (time_bucket('00:00:01'::interval, "timestamp"))
  ORDER BY ticker, (time_bucket('00:00:01'::interval, "timestamp"));

removing the filter clause makes the select work without issues.

so my guess is, when checking whether there are any records to be aggregated, the filter clause is ignored, which in this case results in no records being provided and crashing on the unwrap.

i did not notice this issue with timescaledb < 1.17.0 before, but i didn't test it now.

fordfrog commented 1 month ago

issue #815 is probably related

fordfrog commented 1 month ago

so i tried the patch from the mentioned issue but it didn't solve my issue:

# call refresh_continuous_aggregate('public.tick_market_bars_s1', '2024-06-03 04:00:00', '2024-06-03 04:10:00');
ERROR:  bytes_ is null
CONTEXT:  SQL statement "INSERT INTO _timescaledb_internal._materialized_hypertable_28 SELECT * FROM _timescaledb_internal._partial_view_28 AS I WHERE I.bar_start >= $1 AND I.bar_start < $2 ;"