vec_sum/vec_avg/vec_product fail with GROUP BY and on partitioned tables
What type of bug is this?
Unexpected error, Incorrect result
What subsystems are affected?
Query Engine
Minimal reproduce step
-- no GROUP BY: works
SELECT vec_to_string(vec_sum(v))
FROM (SELECT '[1,1]' AS v UNION ALL SELECT '[2,2]' UNION ALL SELECT '[3,3]') t;
-- with GROUP BY: fails
SELECT h, vec_to_string(vec_sum(v))
FROM (SELECT 'a' AS h, '[1,1]' AS v UNION ALL SELECT 'a', '[2,2]' UNION ALL SELECT 'b', '[3,3]') t
GROUP BY h;
-- partitioned table, no GROUP BY: fails
CREATE TABLE gp (ts TIMESTAMP TIME INDEX, k INT, v VECTOR(2), PRIMARY KEY(k))
PARTITION ON COLUMNS (k) (k < 10, k >= 10 AND k < 20, k >= 20);
INSERT INTO gp VALUES (1000,1,'[1,1]'),(2000,11,'[2,2]'),(3000,21,'[3,3]');
SELECT vec_to_string(vec_sum(v)) FROM gp;What did you expect to see?
a -> [3,3], b -> [3,3] for the grouped query, [6,6] for the partitioned table.
What did you see instead?
Grouped query:
ERROR 1815 (HY000): Internal error: expect creating `VEC_SUM` with only one input field, actual 2.Partitioned table:
ERROR 1815 (HY000): (Internal): Internal error: 1003The client only sees the masked status code. The server log has the same root error, raised by the region-side partial aggregate __vec_sum_state, whose input is the full table scan (ts, k, v):
ERROR servers::mysql::writer: Failed to handle mysql query, code: Internal, error: Failed to execute query
...
4: Internal("expect creating `VEC_SUM` with only one input field, actual 3")vec_avg fails the same way, and vec_product has the same check. The accumulator factories check args.schema.fields().len() != 1 (src/common/function/src/aggrs/vector/sum.rs:58, avg.rs:65, product.rs:58), which is the width of the aggregate input schema, not the number of arguments. It only passes when the input is exactly one column.
Separately, NULL handling is inconsistent: on an unpartitioned table holding [1,1], [2,2], [3,3], NULL, [5,5], [6,6], [7,7], vec_sum returns NULL while vec_avg skips the NULL and returns [4,4]. The merge paths also mishandle NULL states once the error above is fixed: VectorSum::update returns early at the first NULL state and drops the rest of the batch (sum.rs:80-130); VectorProduct::update drops the whole state batch, including non-NULL states before the NULL, when any state is NULL (product.rs:86-119).
What operating system did you use?
macOS 26 arm64
What version of GreptimeDB did you use?
v1.2.1 (same code on main @ d910183cc4)
Source: GreptimeTeam/greptimedb