Skip to content

Commit

Permalink
Disable vectorized sum for expressions
Browse files Browse the repository at this point in the history
This is not supported at the moment, we can only aggregate bare columns,
but we forgot to check this at the planning stage, so it fails.
  • Loading branch information
akuzm committed Dec 8, 2023
1 parent d3e0c2b commit 95cf092
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .unreleased/fix_6393
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixes: #6393 Disable vectorized sum for expressions.

7 changes: 7 additions & 0 deletions tsl/src/partialize_agg.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ is_vectorizable_agg_path(PlannerInfo *root, AggPath *agg_path, Path *path)
if (aggref->aggfnoid != F_SUM_INT4)
return false;

/* Can aggregate only a bare decompressed column, not an expression. */
TargetEntry *argument = castNode(TargetEntry, linitial(aggref->args));
if (!IsA(argument->expr, Var))
{
return false;
}

return true;
}

Expand Down
83 changes: 83 additions & 0 deletions tsl/test/expected/vectorized_aggregation.out
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,89 @@ SELECT sum(int_value) FROM testtable;
Output: compress_hyper_2_20_chunk."time", compress_hyper_2_20_chunk.segment_by_value, compress_hyper_2_20_chunk.int_value, compress_hyper_2_20_chunk.float_value, compress_hyper_2_20_chunk._ts_meta_count, compress_hyper_2_20_chunk._ts_meta_sequence_num, compress_hyper_2_20_chunk._ts_meta_min_1, compress_hyper_2_20_chunk._ts_meta_max_1
(57 rows)

--Vectorized aggregation not possible for expression
SELECT sum(abs(int_value)) FROM testtable;
sum
--------
311405
(1 row)

:EXPLAIN
SELECT sum(abs(int_value)) FROM testtable;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Finalize Aggregate
Output: sum(abs(_hyper_1_1_chunk.int_value))
-> Gather
Output: (PARTIAL sum(abs(_hyper_1_1_chunk.int_value)))
Workers Planned: 2
-> Parallel Append
-> Partial Aggregate
Output: PARTIAL sum(abs(_hyper_1_1_chunk.int_value))
-> Custom Scan (DecompressChunk) on _timescaledb_internal._hyper_1_1_chunk
Output: _hyper_1_1_chunk.int_value
-> Parallel Seq Scan on _timescaledb_internal.compress_hyper_2_11_chunk
Output: compress_hyper_2_11_chunk."time", compress_hyper_2_11_chunk.segment_by_value, compress_hyper_2_11_chunk.int_value, compress_hyper_2_11_chunk.float_value, compress_hyper_2_11_chunk._ts_meta_count, compress_hyper_2_11_chunk._ts_meta_sequence_num, compress_hyper_2_11_chunk._ts_meta_min_1, compress_hyper_2_11_chunk._ts_meta_max_1
-> Partial Aggregate
Output: PARTIAL sum(abs(_hyper_1_2_chunk.int_value))
-> Custom Scan (DecompressChunk) on _timescaledb_internal._hyper_1_2_chunk
Output: _hyper_1_2_chunk.int_value
-> Parallel Seq Scan on _timescaledb_internal.compress_hyper_2_12_chunk
Output: compress_hyper_2_12_chunk."time", compress_hyper_2_12_chunk.segment_by_value, compress_hyper_2_12_chunk.int_value, compress_hyper_2_12_chunk.float_value, compress_hyper_2_12_chunk._ts_meta_count, compress_hyper_2_12_chunk._ts_meta_sequence_num, compress_hyper_2_12_chunk._ts_meta_min_1, compress_hyper_2_12_chunk._ts_meta_max_1
-> Partial Aggregate
Output: PARTIAL sum(abs(_hyper_1_3_chunk.int_value))
-> Custom Scan (DecompressChunk) on _timescaledb_internal._hyper_1_3_chunk
Output: _hyper_1_3_chunk.int_value
-> Parallel Seq Scan on _timescaledb_internal.compress_hyper_2_13_chunk
Output: compress_hyper_2_13_chunk."time", compress_hyper_2_13_chunk.segment_by_value, compress_hyper_2_13_chunk.int_value, compress_hyper_2_13_chunk.float_value, compress_hyper_2_13_chunk._ts_meta_count, compress_hyper_2_13_chunk._ts_meta_sequence_num, compress_hyper_2_13_chunk._ts_meta_min_1, compress_hyper_2_13_chunk._ts_meta_max_1
-> Partial Aggregate
Output: PARTIAL sum(abs(_hyper_1_4_chunk.int_value))
-> Custom Scan (DecompressChunk) on _timescaledb_internal._hyper_1_4_chunk
Output: _hyper_1_4_chunk.int_value
-> Parallel Seq Scan on _timescaledb_internal.compress_hyper_2_14_chunk
Output: compress_hyper_2_14_chunk."time", compress_hyper_2_14_chunk.segment_by_value, compress_hyper_2_14_chunk.int_value, compress_hyper_2_14_chunk.float_value, compress_hyper_2_14_chunk._ts_meta_count, compress_hyper_2_14_chunk._ts_meta_sequence_num, compress_hyper_2_14_chunk._ts_meta_min_1, compress_hyper_2_14_chunk._ts_meta_max_1
-> Partial Aggregate
Output: PARTIAL sum(abs(_hyper_1_5_chunk.int_value))
-> Custom Scan (DecompressChunk) on _timescaledb_internal._hyper_1_5_chunk
Output: _hyper_1_5_chunk.int_value
-> Parallel Seq Scan on _timescaledb_internal.compress_hyper_2_15_chunk
Output: compress_hyper_2_15_chunk."time", compress_hyper_2_15_chunk.segment_by_value, compress_hyper_2_15_chunk.int_value, compress_hyper_2_15_chunk.float_value, compress_hyper_2_15_chunk._ts_meta_count, compress_hyper_2_15_chunk._ts_meta_sequence_num, compress_hyper_2_15_chunk._ts_meta_min_1, compress_hyper_2_15_chunk._ts_meta_max_1
-> Partial Aggregate
Output: PARTIAL sum(abs(_hyper_1_6_chunk.int_value))
-> Custom Scan (DecompressChunk) on _timescaledb_internal._hyper_1_6_chunk
Output: _hyper_1_6_chunk.int_value
-> Parallel Seq Scan on _timescaledb_internal.compress_hyper_2_16_chunk
Output: compress_hyper_2_16_chunk."time", compress_hyper_2_16_chunk.segment_by_value, compress_hyper_2_16_chunk.int_value, compress_hyper_2_16_chunk.float_value, compress_hyper_2_16_chunk._ts_meta_count, compress_hyper_2_16_chunk._ts_meta_sequence_num, compress_hyper_2_16_chunk._ts_meta_min_1, compress_hyper_2_16_chunk._ts_meta_max_1
-> Partial Aggregate
Output: PARTIAL sum(abs(_hyper_1_7_chunk.int_value))
-> Custom Scan (DecompressChunk) on _timescaledb_internal._hyper_1_7_chunk
Output: _hyper_1_7_chunk.int_value
-> Parallel Seq Scan on _timescaledb_internal.compress_hyper_2_17_chunk
Output: compress_hyper_2_17_chunk."time", compress_hyper_2_17_chunk.segment_by_value, compress_hyper_2_17_chunk.int_value, compress_hyper_2_17_chunk.float_value, compress_hyper_2_17_chunk._ts_meta_count, compress_hyper_2_17_chunk._ts_meta_sequence_num, compress_hyper_2_17_chunk._ts_meta_min_1, compress_hyper_2_17_chunk._ts_meta_max_1
-> Partial Aggregate
Output: PARTIAL sum(abs(_hyper_1_8_chunk.int_value))
-> Custom Scan (DecompressChunk) on _timescaledb_internal._hyper_1_8_chunk
Output: _hyper_1_8_chunk.int_value
-> Parallel Seq Scan on _timescaledb_internal.compress_hyper_2_18_chunk
Output: compress_hyper_2_18_chunk."time", compress_hyper_2_18_chunk.segment_by_value, compress_hyper_2_18_chunk.int_value, compress_hyper_2_18_chunk.float_value, compress_hyper_2_18_chunk._ts_meta_count, compress_hyper_2_18_chunk._ts_meta_sequence_num, compress_hyper_2_18_chunk._ts_meta_min_1, compress_hyper_2_18_chunk._ts_meta_max_1
-> Partial Aggregate
Output: PARTIAL sum(abs(_hyper_1_9_chunk.int_value))
-> Custom Scan (DecompressChunk) on _timescaledb_internal._hyper_1_9_chunk
Output: _hyper_1_9_chunk.int_value
-> Parallel Seq Scan on _timescaledb_internal.compress_hyper_2_19_chunk
Output: compress_hyper_2_19_chunk."time", compress_hyper_2_19_chunk.segment_by_value, compress_hyper_2_19_chunk.int_value, compress_hyper_2_19_chunk.float_value, compress_hyper_2_19_chunk._ts_meta_count, compress_hyper_2_19_chunk._ts_meta_sequence_num, compress_hyper_2_19_chunk._ts_meta_min_1, compress_hyper_2_19_chunk._ts_meta_max_1
-> Partial Aggregate
Output: PARTIAL sum(abs(_hyper_1_10_chunk.int_value))
-> Custom Scan (DecompressChunk) on _timescaledb_internal._hyper_1_10_chunk
Output: _hyper_1_10_chunk.int_value
-> Parallel Seq Scan on _timescaledb_internal.compress_hyper_2_20_chunk
Output: compress_hyper_2_20_chunk."time", compress_hyper_2_20_chunk.segment_by_value, compress_hyper_2_20_chunk.int_value, compress_hyper_2_20_chunk.float_value, compress_hyper_2_20_chunk._ts_meta_count, compress_hyper_2_20_chunk._ts_meta_sequence_num, compress_hyper_2_20_chunk._ts_meta_min_1, compress_hyper_2_20_chunk._ts_meta_max_1
-> Partial Aggregate
Output: PARTIAL sum(abs(_hyper_1_1_chunk.int_value))
-> Parallel Seq Scan on _timescaledb_internal._hyper_1_1_chunk
Output: _hyper_1_1_chunk.int_value
(70 rows)

-- Vectorized aggregation NOT possible
SET timescaledb.enable_vectorized_aggregation = OFF;
:EXPLAIN
Expand Down
8 changes: 8 additions & 0 deletions tsl/test/sql/vectorized_aggregation.sql
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ SELECT sum(int_value) FROM testtable;
:EXPLAIN
SELECT sum(int_value) FROM testtable;


--Vectorized aggregation not possible for expression
SELECT sum(abs(int_value)) FROM testtable;

:EXPLAIN
SELECT sum(abs(int_value)) FROM testtable;


-- Vectorized aggregation NOT possible
SET timescaledb.enable_vectorized_aggregation = OFF;

Expand Down

0 comments on commit 95cf092

Please sign in to comment.