timescale / timescaledb

An open-source time-series SQL database optimized for fast ingest and complex queries. Packaged as a PostgreSQL extension.
https://www.timescale.com/
Other
16.82k stars 852 forks source link

[Bug]: average(time_weight('linear')) yields incorrect result #6999

Open relativityboy opened 4 weeks ago

relativityboy commented 4 weeks ago

What type of bug is this?

Incorrect result

What subsystems and features are affected?

Continuous aggregate

What happened?

When creating a 1-minute materialized view where the value col is average(time_weighted('linear' ... )) as value with 3 entries in the source data table at (seconds, value) [(0,1), (10, 11), (50, 51)] the expected output for the time weighted average is 30.17.

Expected equivalent calculation (((1 + 11) / 2) * 10 + ((11 + 51) / 2) * 40 + ((51 + 51) / 2) * 10) / 60

The materialized view yields a value of 26.0

TimescaleDB version affected

2.14.2

PostgreSQL version used

15.6

What operating system did you use?

MacOS

What installation method did you use?

Docker

What platform did you run on?

On prem/Self-hosted

Relevant log output and stack trace

No response

How can we reproduce the bug?

Create a materialized view at 1 minute agg pulling data from a table with start_time, value columns. Create aggregation with value as described above. (actual sql is on work machine and not sure if I'm allowed to share)