risingwave_stream/from_proto/
hash_agg.rs1use std::sync::Arc;
18
19use risingwave_common::hash::{HashKey, HashKeyDispatcher};
20use risingwave_common::types::DataType;
21use risingwave_expr::aggregate::AggCall;
22use risingwave_pb::stream_plan::HashAggNode;
23
24use super::agg_common::{
25 build_agg_state_storages_from_proto, build_distinct_dedup_table_from_proto,
26};
27use super::*;
28use crate::common::table::state_table::StateTableBuilder;
29use crate::executor::aggregate::{AggExecutorArgs, HashAggExecutor, HashAggExecutorExtraArgs};
30
31pub struct HashAggExecutorDispatcherArgs<S: StateStore> {
32 args: AggExecutorArgs<S, HashAggExecutorExtraArgs>,
33 group_key_types: Vec<DataType>,
34}
35
36impl<S: StateStore> HashKeyDispatcher for HashAggExecutorDispatcherArgs<S> {
37 type Output = StreamResult<Box<dyn Execute>>;
38
39 fn dispatch_impl<K: HashKey>(self) -> Self::Output {
40 Ok(HashAggExecutor::<K, S>::new(self.args)?.boxed())
41 }
42
43 fn data_types(&self) -> &[DataType] {
44 &self.group_key_types
45 }
46}
47
48pub struct HashAggExecutorBuilder;
49
50impl_stream_node_body!(HashAgg(HashAggNode) => HashAggExecutorBuilder);
51
52impl ExecutorBuilder for HashAggExecutorBuilder {
53 type Node = HashAggNode;
54
55 async fn new_boxed_executor(
56 params: ExecutorParams,
57 node: &Self::Node,
58 store: impl StateStore,
59 ) -> StreamResult<Executor> {
60 let group_key_indices = node
61 .get_group_key()
62 .iter()
63 .map(|key| *key as usize)
64 .collect::<Vec<_>>();
65 let [input]: [_; 1] = params.input.try_into().unwrap();
66 let group_key_types = group_key_indices
67 .iter()
68 .map(|idx| input.schema().fields[*idx].data_type())
69 .collect_vec();
70
71 let agg_calls: Vec<AggCall> = node
72 .get_agg_calls()
73 .iter()
74 .map(AggCall::from_protobuf)
75 .try_collect()?;
76
77 let vnodes = Some(Arc::new(params.vnode_bitmap));
78 let storages = build_agg_state_storages_from_proto(
79 node.get_agg_call_states(),
80 store.clone(),
81 vnodes.clone(),
82 ¶ms.config,
83 )
84 .await;
85 let intermediate_state_table = StateTableBuilder::new(
87 node.get_intermediate_state_table().unwrap(),
88 store.clone(),
89 vnodes.clone(),
90 )
91 .enable_preload_all_rows_by_config(¶ms.config)
92 .build()
93 .await;
94 let distinct_dedup_tables = build_distinct_dedup_table_from_proto(
95 node.get_distinct_dedup_tables(),
96 store,
97 vnodes,
98 ¶ms.config,
99 )
100 .await;
101
102 let exec = HashAggExecutorDispatcherArgs {
103 args: AggExecutorArgs {
104 version: node.version(),
105
106 input,
107 actor_ctx: params.actor_context,
108 info: params.info.clone(),
109
110 extreme_cache_size: params.config.developer.unsafe_extreme_cache_size,
111
112 agg_calls,
113 row_count_index: node.get_row_count_index() as usize,
114 storages,
115 intermediate_state_table,
116 distinct_dedup_tables,
117 watermark_epoch: params.watermark_epoch,
118 extra: HashAggExecutorExtraArgs {
119 group_key_indices,
120 chunk_size: params.config.developer.chunk_size,
121 max_dirty_groups_heap_size: (params.config.developer)
122 .hash_agg_max_dirty_groups_heap_size,
123 emit_on_window_close: node.get_emit_on_window_close(),
124 },
125 },
126 group_key_types,
127 }
128 .dispatch()?;
129 Ok((params.info, exec).into())
130 }
131}