12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
+ use std:: collections:: HashMap ;
15
16
use std:: collections:: HashSet ;
16
17
use std:: collections:: VecDeque ;
17
18
use std:: ops:: ControlFlow ;
@@ -20,10 +21,11 @@ use std::sync::atomic::AtomicUsize;
20
21
use std:: sync:: atomic:: Ordering ;
21
22
use std:: sync:: Arc ;
22
23
24
+ use async_channel:: Receiver ;
23
25
use async_channel:: Sender ;
24
26
use databend_common_base:: base:: tokio:: sync:: Barrier ;
25
- use databend_common_catalog:: runtime_filter_info:: RuntimeFilterInfo ;
26
27
use databend_common_catalog:: runtime_filter_info:: RuntimeFilterReady ;
28
+ use databend_common_catalog:: runtime_filter_info:: RuntimeFiltersForScan ;
27
29
use databend_common_catalog:: table_context:: TableContext ;
28
30
use databend_common_column:: bitmap:: Bitmap ;
29
31
use databend_common_exception:: ErrorCode ;
@@ -79,7 +81,7 @@ use crate::pipelines::processors::transforms::hash_join::FixedKeyHashJoinHashTab
79
81
use crate :: pipelines:: processors:: transforms:: hash_join:: HashJoinHashTable ;
80
82
use crate :: pipelines:: processors:: transforms:: hash_join:: SerializerHashJoinHashTable ;
81
83
use crate :: pipelines:: processors:: transforms:: hash_join:: SingleBinaryHashJoinHashTable ;
82
- use crate :: pipelines:: processors:: transforms:: RuntimeFiltersMeta ;
84
+ use crate :: pipelines:: processors:: transforms:: RemoteRuntimeFilters ;
83
85
use crate :: pipelines:: processors:: HashJoinState ;
84
86
use crate :: sessions:: QueryContext ;
85
87
@@ -119,7 +121,14 @@ pub struct HashJoinBuildState {
119
121
120
122
/// Spill related states.
121
123
pub ( crate ) memory_settings : MemorySettings ,
122
- pub ( crate ) runtime_filter_sender : Option < Sender < RuntimeFiltersMeta > > ,
124
+ pub ( crate ) rf_channels : Option < RuntimeFilterChannels > ,
125
+ }
126
+
127
+ pub struct RuntimeFilterChannels {
128
+ /// send runtime filter to `RuntimeFilterSourceProcessor`
129
+ pub ( crate ) rf_src_send : Sender < RemoteRuntimeFilters > ,
130
+ /// receive runtime filter from `RuntimeFilterSinkProcessor`
131
+ pub ( crate ) rf_sink_recv : Receiver < RemoteRuntimeFilters > ,
123
132
}
124
133
125
134
impl HashJoinBuildState {
@@ -131,7 +140,7 @@ impl HashJoinBuildState {
131
140
build_projections : & ColumnSet ,
132
141
hash_join_state : Arc < HashJoinState > ,
133
142
num_threads : usize ,
134
- rf_src_send : Option < Sender < RuntimeFiltersMeta > > ,
143
+ rf_channels : Option < RuntimeFilterChannels > ,
135
144
) -> Result < Arc < HashJoinBuildState > > {
136
145
let hash_key_types = build_keys
137
146
. iter ( )
@@ -168,7 +177,7 @@ impl HashJoinBuildState {
168
177
build_hash_table_tasks : Default :: default ( ) ,
169
178
mutex : Default :: default ( ) ,
170
179
memory_settings,
171
- runtime_filter_sender : rf_src_send ,
180
+ rf_channels ,
172
181
} ) )
173
182
}
174
183
@@ -311,7 +320,6 @@ impl HashJoinBuildState {
311
320
if self . hash_join_state . spilled_partitions . read ( ) . is_empty ( ) {
312
321
self . add_runtime_filter ( & build_chunks, build_num_rows) ?;
313
322
} else {
314
- self . send_runtime_filter_meta ( Default :: default ( ) ) ?;
315
323
self . set_bloom_filter_ready ( false ) ?;
316
324
}
317
325
@@ -844,15 +852,16 @@ impl HashJoinBuildState {
844
852
845
853
fn add_runtime_filter ( & self , build_chunks : & [ DataBlock ] , build_num_rows : usize ) -> Result < ( ) > {
846
854
let mut bloom_filter_ready = false ;
847
- let mut runtime_filters_meta = RuntimeFiltersMeta :: default ( ) ;
855
+ let mut runtime_filters = HashMap :: new ( ) ;
848
856
for rf in self . runtime_filter_desc ( ) {
849
- let mut runtime_filter = RuntimeFilterInfo :: default ( ) ;
857
+ let mut runtime_filter = RuntimeFiltersForScan :: default ( ) ;
850
858
if rf. enable_inlist_runtime_filter && build_num_rows < INLIST_RUNTIME_FILTER_THRESHOLD {
851
859
self . inlist_runtime_filter (
852
860
& mut runtime_filter,
853
861
build_chunks,
854
862
& rf. build_key ,
855
863
& rf. probe_key ,
864
+ rf. id ,
856
865
) ?;
857
866
}
858
867
if rf. enable_bloom_runtime_filter {
@@ -861,6 +870,7 @@ impl HashJoinBuildState {
861
870
& mut runtime_filter,
862
871
& rf. build_key ,
863
872
& rf. probe_key ,
873
+ rf. id ,
864
874
) ?;
865
875
}
866
876
if rf. enable_min_max_runtime_filter {
@@ -869,25 +879,26 @@ impl HashJoinBuildState {
869
879
& mut runtime_filter,
870
880
& rf. build_key ,
871
881
& rf. probe_key ,
882
+ rf. id ,
872
883
) ?;
873
884
}
874
885
if !runtime_filter. is_empty ( ) {
875
886
bloom_filter_ready |= !runtime_filter. is_blooms_empty ( ) ;
876
- runtime_filters_meta. add ( rf. scan_id , & runtime_filter) ;
877
- self . ctx . set_runtime_filter ( ( rf. scan_id , runtime_filter) ) ;
887
+ runtime_filters. insert ( rf. scan_id , runtime_filter) ;
878
888
}
879
889
}
880
- self . send_runtime_filter_meta ( runtime_filters_meta ) ?;
890
+ self . send_runtime_filter_meta ( runtime_filters ) ?;
881
891
self . set_bloom_filter_ready ( bloom_filter_ready) ?;
882
892
Ok ( ( ) )
883
893
}
884
894
885
895
fn bloom_runtime_filter (
886
896
& self ,
887
897
data_blocks : & [ DataBlock ] ,
888
- runtime_filter : & mut RuntimeFilterInfo ,
898
+ runtime_filter : & mut RuntimeFiltersForScan ,
889
899
build_key : & Expr ,
890
900
probe_key : & Expr < String > ,
901
+ rf_id : usize ,
891
902
) -> Result < ( ) > {
892
903
if !build_key. data_type ( ) . remove_nullable ( ) . is_number ( )
893
904
&& !build_key. data_type ( ) . remove_nullable ( ) . is_string ( )
@@ -925,23 +936,24 @@ impl HashJoinBuildState {
925
936
hashes_vec. push ( hash) ;
926
937
} ) ;
927
938
let filter = BinaryFuse16 :: try_from ( & hashes_vec) ?;
928
- runtime_filter. add_bloom ( ( id. to_string ( ) , filter) ) ;
939
+ runtime_filter. add_bloom ( rf_id , ( id. to_string ( ) , filter) ) ;
929
940
Ok ( ( ) )
930
941
}
931
942
932
943
fn inlist_runtime_filter (
933
944
& self ,
934
- runtime_filter : & mut RuntimeFilterInfo ,
945
+ runtime_filter : & mut RuntimeFiltersForScan ,
935
946
data_blocks : & [ DataBlock ] ,
936
947
build_key : & Expr ,
937
948
probe_key : & Expr < String > ,
949
+ rf_id : usize ,
938
950
) -> Result < ( ) > {
939
951
if let Some ( distinct_build_column) =
940
952
dedup_build_key_column ( & self . func_ctx , data_blocks, build_key) ?
941
953
{
942
954
if let Some ( filter) = inlist_filter ( probe_key, distinct_build_column. clone ( ) ) ? {
943
955
info ! ( "inlist_filter: {:?}" , filter. sql_display( ) ) ;
944
- runtime_filter. add_inlist ( filter) ;
956
+ runtime_filter. add_inlist ( rf_id , filter) ;
945
957
}
946
958
}
947
959
Ok ( ( ) )
@@ -950,9 +962,10 @@ impl HashJoinBuildState {
950
962
fn min_max_runtime_filter (
951
963
& self ,
952
964
data_blocks : & [ DataBlock ] ,
953
- runtime_filter : & mut RuntimeFilterInfo ,
965
+ runtime_filter : & mut RuntimeFiltersForScan ,
954
966
build_key : & Expr ,
955
967
probe_key : & Expr < String > ,
968
+ rf_id : usize ,
956
969
) -> Result < ( ) > {
957
970
if !build_key. runtime_filter_supported_types ( ) {
958
971
return Ok ( ( ) ) ;
@@ -1045,7 +1058,7 @@ impl HashJoinBuildState {
1045
1058
} ;
1046
1059
if let Some ( min_max_filter) = min_max_filter {
1047
1060
info ! ( "min_max_filter: {:?}" , min_max_filter. sql_display( ) ) ;
1048
- runtime_filter. add_min_max ( min_max_filter) ;
1061
+ runtime_filter. add_min_max ( rf_id , min_max_filter) ;
1049
1062
}
1050
1063
}
1051
1064
Ok ( ( ) )
@@ -1079,10 +1092,24 @@ impl HashJoinBuildState {
1079
1092
. any ( |rf| rf. enable_min_max_runtime_filter )
1080
1093
}
1081
1094
1082
- fn send_runtime_filter_meta ( & self , runtime_filter : RuntimeFiltersMeta ) -> Result < ( ) > {
1083
- if let Some ( sender) = self . runtime_filter_sender . as_ref ( ) {
1084
- sender. send_blocking ( runtime_filter) . unwrap ( ) ;
1085
- sender. close ( ) ;
1095
+ fn send_runtime_filter_meta (
1096
+ & self ,
1097
+ mut rf : HashMap < usize , RuntimeFiltersForScan > ,
1098
+ ) -> Result < ( ) > {
1099
+ if let Some ( channels) = self . rf_channels . as_ref ( ) {
1100
+ channels
1101
+ . rf_src_send
1102
+ . send_blocking ( rf. into ( ) )
1103
+ . map_err ( |_| ErrorCode :: TokioError ( "send runtime filter meta failed" ) ) ?;
1104
+ channels. rf_src_send . close ( ) ;
1105
+ let merged_rf = channels
1106
+ . rf_sink_recv
1107
+ . recv_blocking ( )
1108
+ . map_err ( |_| ErrorCode :: TokioError ( "receive runtime filter meta failed" ) ) ?;
1109
+ rf = merged_rf. into ( ) ;
1110
+ }
1111
+ for ( scan_id, runtime_filter) in rf. into_iter ( ) {
1112
+ self . ctx . set_runtime_filter ( ( scan_id, runtime_filter) ) ;
1086
1113
}
1087
1114
Ok ( ( ) )
1088
1115
}
0 commit comments