Skip to content

Conversation

natalya-aksman
Copy link
Member

@natalya-aksman natalya-aksman commented Aug 27, 2025

Fixes #4894

Gapfill used to error out when aggregates were used in expressions with group-by columns like (agg + gby_var) and when gapfill target entries were not matching the table column order.
It is fixed by fixing group-by vars to match execution group-by vars.

Copy link

@dbeck, @mkindahl: please review this pull request.

Powered by pull-review

@natalya-aksman natalya-aksman requested review from svenklemm and akuzm and removed request for dbeck August 27, 2025 18:21
Copy link

codecov bot commented Aug 27, 2025

Codecov Report

❌ Patch coverage is 90.90909% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.48%. Comparing base (0843d8b) to head (554622c).
⚠️ Report is 77 commits behind head on main.

Files with missing lines Patch % Lines
tsl/src/nodes/gapfill/gapfill_exec.c 85.71% 1 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8550      +/-   ##
==========================================
+ Coverage   82.37%   82.48%   +0.10%     
==========================================
  Files         248      248              
  Lines       47632    47633       +1     
  Branches    12101    12104       +3     
==========================================
+ Hits        39237    39289      +52     
- Misses       3503     3507       +4     
+ Partials     4892     4837      -55     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@natalya-aksman natalya-aksman force-pushed the fix_gapfill_bug_over_aggregates_in_expressions branch from abcc4c1 to 428a9cc Compare August 27, 2025 18:53
@svenklemm svenklemm added this to the v2.22.0 milestone Aug 28, 2025
@akuzm
Copy link
Member

akuzm commented Aug 28, 2025

One thing I don't understand about this: suppose you have coalesce(x, min(y)) in the output of gapfill. In the gapfilled tuple, you're supposed to compute coalesce(x, null). But how can you compute x based on the gapfilled tuple, if x is not present there at all?

@akuzm
Copy link
Member

akuzm commented Aug 28, 2025

Looks like this is still not correct:

test=# WITH hourly(time, signal, value, level_a, level_b, level_c, agg) AS (
  VALUES
    ('2022-10-01T00:00:00Z'::timestamptz, 2, 685::real, 1, -1, -1, 2),
    ('2022-10-01T00:00:00Z'::timestamptz, 2, 686::real, 1, -1, -1, 3),
    ('2022-10-01T02:00:00Z'::timestamptz, 2, 686::real, 1, -1, -1, 2),
    ('2022-10-01T02:00:00Z'::timestamptz, 2, 687::real, 1, -1, -1, 3),
    ('2022-10-01T03:00:00Z'::timestamptz, 2, 687::real, 1, -1, -1, 2),
    ('2022-10-01T03:00:00Z'::timestamptz, 2, 688::real, 1, -1, -1, 3)
)
SELECT
  time_bucket_gapfill('1 hour', time) AS time, 1 p1, 2 p2,
  (agg + 1) * max(value)                      
FROM hourly
WHERE
  time >= '2022-10-01T00:00:00Z'
  AND time <  '2022-10-01T05:59:59Z'
GROUP BY 1, agg + 1;
ERROR:  XX000: attribute number 7 exceeds number of columns 5
LOCATION:  CheckVarSlotCompatibility, execExprInterp.c:2015

@natalya-aksman
Copy link
Member Author

One thing I don't understand about this: suppose you have coalesce(x, min(y)) in the output of gapfill. In the gapfilled tuple, you're supposed to compute coalesce(x, null). But how can you compute x based on the gapfilled tuple, if x is not present there at all?

When we have aggregates, the only vars which can be used in the target list are aggregates and grouping columns. Grouping columns are guaranteed to be computed and provided by the aggregate node. I.e. x is guaranteed to be present in the gapfill tuple as a groping column, so we can refer to it in this expression.

@natalya-aksman
Copy link
Member Author

Looks like this is still not correct:
...
FROM hourly
WHERE
time >= '2022-10-01T00:00:00Z'
AND time < '2022-10-01T05:59:59Z'
GROUP BY 1, agg + 1;
ERROR: XX000: attribute number 7 exceeds number of columns 5
LOCATION: CheckVarSlotCompatibility, execExprInterp.c:2015

We need to cover any grouping expressions, not just vars. Will look into it.

@philkra philkra modified the milestones: v2.22.0, v2.22.1 Sep 1, 2025
@natalya-aksman natalya-aksman force-pushed the fix_gapfill_bug_over_aggregates_in_expressions branch from 428a9cc to 2be902f Compare September 2, 2025 15:14
Fix gapfill bug when aggregates are in expressions with groupby columns, use custom_exprs
@natalya-aksman natalya-aksman force-pushed the fix_gapfill_bug_over_aggregates_in_expressions branch from 2be902f to 554622c Compare September 2, 2025 15:46
@natalya-aksman natalya-aksman merged commit 6e0e00d into timescale:main Sep 2, 2025
43 checks passed
timescale-automation pushed a commit that referenced this pull request Sep 2, 2025
…ns (#8550)

Fixes #4894

Gapfill used to error out when aggregates were used in expressions with
group-by columns like `(agg + gby_var)` and when gapfill target entries
were not matching the table column order.
It is fixed by fixing group-by vars to match execution group-by vars.

(cherry picked from commit 6e0e00d)
timescale-automation pushed a commit that referenced this pull request Sep 7, 2025
…ns (#8550)

Fixes #4894

Gapfill used to error out when aggregates were used in expressions with
group-by columns like `(agg + gby_var)` and when gapfill target entries
were not matching the table column order.
It is fixed by fixing group-by vars to match execution group-by vars.

(cherry picked from commit 6e0e00d)
@natalya-aksman natalya-aksman mentioned this pull request Sep 29, 2025
philkra added a commit that referenced this pull request Sep 30, 2025
## 2.22.1 (2025-09-30)

This release contains performance improvements and bug fixes since the
[2.22.0](https://github.com/timescale/timescaledb/releases/tag/2.20.0)
release. We recommend that you upgrade at the next available
opportunity.

This release blocks the ability to leverage **concurrent refresh
policies** in **hierarchical continous aggregates**, as potential
deadlocks can occur. If you have [concurrent refresh
policies](https://docs.tigerdata.com/use-timescale/latest/continuous-aggregates/refresh-policies/#add-concurrent-refresh-policies)
in **hierarchical** continous aggregates, [please disable the
jobs](https://docs.tigerdata.com/api/latest/jobs-automation/alter_job/#samples),
as following:

```
SELECT alter_job("<job_id_of_concurrent_policy>", scheduled => false);
```

**Bugfixes**
* [#7766](#7766) Load OSM
extension in retention background worker to drop tiered chunks
* [#8550](#8550) Error in
gapfill with expressions over aggregates and groupby columns and
out-of-order columns
* [#8593](#8593) Error on
change of invalidation method for continuous aggregate
* [#8599](#8599) Fix attnum
mismatch bug in chunk constraint checks
* [#8607](#8607) Fix
interrupted continous aggregate refresh materialization phase leaving
behind pending materialization ranges
* [#8638](#8638) `ALTER
TABLE RESET` for `orderby` settings
* [#8644](#8644) Fix
migration script for sparse index configuration
* [#8657](#8657) Fix
`CREATE TABLE WITH` when using UUIDv7 partitioning
* [#8659](#8659) Don't
propagate `ALTER TABLE` commands to foreign data wrapper chunks
* [#8693](#8693) Compressed
index not chosen for `varchar` typed `segmentby` columns
* [#8707](#8707) Block
concurrent refresh policies for hierarchical continous aggregate due to
potential deadlocks

**Thanks**
* @MKrkkl for reporting a bug in Gapfill queries with expressions over
aggregates and groupby columns
* @brandonpurcell-dev for creating a test case that showed a bug in
`CREATE TABLE WITH` when using UUIDv7 partitioning
* @snyrkill for reporting a bug when interrupting a continous aggregate
refresh

---------

Signed-off-by: Philip Krauss <[email protected]>
Co-authored-by: timescale-automation <123763385+github-actions[bot]@users.noreply.github.com>
Co-authored-by: philkra <[email protected]>
Co-authored-by: Philip Krauss <[email protected]>
Co-authored-by: Iain Cox <[email protected]>
@timescale-automation timescale-automation added the released-2.22.1 Released in 2.22.1 label Sep 30, 2025
philkra added a commit that referenced this pull request Oct 1, 2025
## 2.22.1 (2025-09-30)

This release contains performance improvements and bug fixes since the
[2.22.0](https://github.com/timescale/timescaledb/releases/tag/2.20.0)
release. We recommend that you upgrade at the next available
opportunity.

This release blocks the ability to leverage **concurrent refresh
policies** in **hierarchical continous aggregates**, as potential
deadlocks can occur. If you have [concurrent refresh
policies](https://docs.tigerdata.com/use-timescale/latest/continuous-aggregates/refresh-policies/#add-concurrent-refresh-policies)
in **hierarchical** continous aggregates, [please disable the
jobs](https://docs.tigerdata.com/api/latest/jobs-automation/alter_job/#samples),
as following:

```
SELECT alter_job("<job_id_of_concurrent_policy>", scheduled => false);
```

**Bugfixes**
* [#7766](#7766) Load OSM
extension in retention background worker to drop tiered chunks
* [#8550](#8550) Error in
gapfill with expressions over aggregates and groupby columns and
out-of-order columns
* [#8593](#8593) Error on
change of invalidation method for continuous aggregate
* [#8599](#8599) Fix attnum
mismatch bug in chunk constraint checks
* [#8607](#8607) Fix
interrupted continous aggregate refresh materialization phase leaving
behind pending materialization ranges
* [#8638](#8638) `ALTER
TABLE RESET` for `orderby` settings
* [#8644](#8644) Fix
migration script for sparse index configuration
* [#8657](#8657) Fix
`CREATE TABLE WITH` when using UUIDv7 partitioning
* [#8659](#8659) Don't
propagate `ALTER TABLE` commands to foreign data wrapper chunks
* [#8693](#8693) Compressed
index not chosen for `varchar` typed `segmentby` columns
* [#8707](#8707) Block
concurrent refresh policies for hierarchical continous aggregate due to
potential deadlocks

**Thanks**
* @MKrkkl for reporting a bug in Gapfill queries with expressions over
aggregates and groupby columns
* @brandonpurcell-dev for creating a test case that showed a bug in
`CREATE TABLE WITH` when using UUIDv7 partitioning
* @snyrkill for reporting a bug when interrupting a continous aggregate
refresh

---------

Signed-off-by: Philip Krauss <[email protected]>
Co-authored-by: timescale-automation <123763385+github-actions[bot]@users.noreply.github.com>
Co-authored-by: philkra <[email protected]>
Co-authored-by: Philip Krauss <[email protected]>
Co-authored-by: Iain Cox <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Attribute number 7 exceeds number of columns 4
6 participants