Skip to main content

CombineFn

Type Alias CombineFn 

Source
pub type CombineFn = unsafe extern "C" fn(info: duckdb_function_info, source: *mut duckdb_aggregate_state, target: *mut duckdb_aggregate_state, count: idx_t);
Expand description

Merges source states into target states.

Both source and target point to arrays of count state pointers.

§Pitfall L1: Combine must propagate ALL config fields

A target state is not a copy of anything: it was set up by your StateInitFn (for FfiState<T>, that is T::default()) and may have seen no update at all. All configuration fields must be copied from source, not just accumulated data values.

§Leave source unchanged

Read the source states; do not modify them. A window’s segment tree combines one of its shared node states into every frame that covers it, from several threads at once (WindowSegmentTreePart::WindowSegmentValue and FlushStates, window_segment_tree.cpp), so a combine that consumes its source is right for the first frame that reads it and wrong for the rest: a sum that moved its value out gave 4985 of 5000 rows wrong over ROWS BETWEEN 100 PRECEDING AND CURRENT ROW in the fifth audit’s regression test. Writing to a source another thread is reading is also a data race. Copy or clone what the target needs.

After combine returns, DuckDB may call DestroyFn on the source states, so do not keep pointers into them either.