@@ -942,11 +942,6 @@ fn is_simple_all_query(search_request: &SearchRequest) -> bool {
942942 return false ;
943943 }
944944
945- // TODO: Update the logic to handle start_timestamp end_timestamp ranges
946- if search_request. start_timestamp . is_some ( ) || search_request. end_timestamp . is_some ( ) {
947- return false ;
948- }
949-
950945 let Ok ( query_ast) = serde_json:: from_str ( & search_request. query_ast ) else {
951946 return false ;
952947 } ;
@@ -1000,6 +995,29 @@ impl CanSplitDoBetter {
1000995 }
1001996 }
1002997
998+ fn is_split_contained_in_search_time_range (
999+ split : & SplitIdAndFooterOffsets ,
1000+ search_request : & SearchRequest ,
1001+ ) -> bool {
1002+ if let Some ( start) = search_request. start_timestamp {
1003+ let Some ( split_start) = split. timestamp_start else {
1004+ return false ;
1005+ } ;
1006+ if split_start < start {
1007+ return false ;
1008+ }
1009+ }
1010+ if let Some ( end) = search_request. end_timestamp {
1011+ let Some ( split_end) = split. timestamp_end else {
1012+ return false ;
1013+ } ;
1014+ if split_end >= end {
1015+ return false ;
1016+ }
1017+ }
1018+ true
1019+ }
1020+
10031021 fn to_splits_with_request (
10041022 splits : Vec < SplitIdAndFooterOffsets > ,
10051023 request : Arc < SearchRequest > ,
@@ -1012,23 +1030,32 @@ impl CanSplitDoBetter {
10121030 }
10131031
10141032 /// Calculate the number of splits which are guaranteed to deliver enough documents.
1033+ ///
1034+ /// If there's a time range and not enough splits contain at least the number of requested
1035+ /// documents, return None.
10151036 fn get_min_required_splits (
10161037 splits : & [ SplitIdAndFooterOffsets ] ,
10171038 request : & SearchRequest ,
1018- ) -> usize {
1039+ ) -> Option < usize > {
10191040 let num_requested_docs = request. start_offset + request. max_hits ;
10201041
1021- splits
1022- . into_iter ( )
1023- . map ( |split| split. num_docs )
1024- // computing the partial sum
1025- . scan ( 0u64 , |partial_sum : & mut u64 , num_docs_in_split : u64 | {
1026- * partial_sum += num_docs_in_split;
1027- Some ( * partial_sum)
1028- } )
1029- . take_while ( |partial_sum| * partial_sum < num_requested_docs)
1030- . count ( )
1031- + 1
1042+ let mut min_required_splits = 0 ;
1043+ let mut partial_sum = 0 ;
1044+
1045+ for split in splits. iter ( ) {
1046+ if !Self :: is_split_contained_in_search_time_range ( split, request) {
1047+ continue ;
1048+ }
1049+
1050+ partial_sum += split. num_docs ;
1051+
1052+ min_required_splits += 1 ;
1053+ if partial_sum >= num_requested_docs {
1054+ return Some ( min_required_splits) ;
1055+ }
1056+ }
1057+
1058+ None
10321059 }
10331060
10341061 fn optimize_split_id_higher (
@@ -1042,7 +1069,11 @@ impl CanSplitDoBetter {
10421069 return Ok ( Self :: to_splits_with_request ( splits, request) ) ;
10431070 }
10441071
1045- let min_required_splits = Self :: get_min_required_splits ( & splits, & request) ;
1072+ let Some ( min_required_splits) = Self :: get_min_required_splits ( & splits, & request) else {
1073+ // not enough splits contained in time range.
1074+ return Ok ( Self :: to_splits_with_request ( splits, request) ) ;
1075+ } ;
1076+
10461077 let mut split_with_req = Self :: to_splits_with_request ( splits, request) ;
10471078
10481079 // In this case there is no sort order, we order by split id.
@@ -1059,14 +1090,21 @@ impl CanSplitDoBetter {
10591090 request : Arc < SearchRequest > ,
10601091 mut splits : Vec < SplitIdAndFooterOffsets > ,
10611092 ) -> Result < Vec < ( SplitIdAndFooterOffsets , SearchRequest ) > , SearchError > {
1062- splits. sort_unstable_by_key ( |split| std:: cmp:: Reverse ( split. timestamp_end ( ) ) ) ;
1093+ splits. sort_unstable_by_key ( |split| {
1094+ let contained = Self :: is_split_contained_in_search_time_range ( split, & request) ;
1095+ ( !contained, std:: cmp:: Reverse ( split. timestamp_end ( ) ) )
1096+ } ) ;
10631097
10641098 if !is_simple_all_query ( & request) {
10651099 // no optimization opportunity here.
10661100 return Ok ( Self :: to_splits_with_request ( splits, request) ) ;
10671101 }
10681102
1069- let min_required_splits = Self :: get_min_required_splits ( & splits, & request) ;
1103+ let Some ( min_required_splits) = Self :: get_min_required_splits ( & splits, & request) else {
1104+ // not enough splits contained in time range.
1105+ return Ok ( Self :: to_splits_with_request ( splits, request) ) ;
1106+ } ;
1107+
10701108 let mut split_with_req = Self :: to_splits_with_request ( splits, request) ;
10711109
10721110 // We order by timestamp desc. split_with_req is sorted by timestamp_end desc.
@@ -1095,14 +1133,21 @@ impl CanSplitDoBetter {
10951133 request : Arc < SearchRequest > ,
10961134 mut splits : Vec < SplitIdAndFooterOffsets > ,
10971135 ) -> Result < Vec < ( SplitIdAndFooterOffsets , SearchRequest ) > , SearchError > {
1098- splits. sort_unstable_by_key ( |split| split. timestamp_start ( ) ) ;
1136+ splits. sort_unstable_by_key ( |split| {
1137+ let contained = Self :: is_split_contained_in_search_time_range ( split, & request) ;
1138+ ( !contained, split. timestamp_start ( ) )
1139+ } ) ;
10991140
11001141 if !is_simple_all_query ( & request) {
11011142 // no optimization opportunity here.
11021143 return Ok ( Self :: to_splits_with_request ( splits, request) ) ;
11031144 }
11041145
1105- let min_required_splits = Self :: get_min_required_splits ( & splits, & request) ;
1146+ let Some ( min_required_splits) = Self :: get_min_required_splits ( & splits, & request) else {
1147+ // not enough splits contained in time range.
1148+ return Ok ( Self :: to_splits_with_request ( splits, request) ) ;
1149+ } ;
1150+
11061151 let mut split_with_req = Self :: to_splits_with_request ( splits, request) ;
11071152
11081153 // We order by timestamp asc. split_with_req is sorted by timestamp_start.
@@ -1138,7 +1183,10 @@ impl CanSplitDoBetter {
11381183 request : Arc < SearchRequest > ,
11391184 mut splits : Vec < SplitIdAndFooterOffsets > ,
11401185 ) -> Result < Vec < ( SplitIdAndFooterOffsets , SearchRequest ) > , SearchError > {
1141- splits. sort_unstable_by_key ( |split| std:: cmp:: Reverse ( split. timestamp_end ( ) ) ) ;
1186+ splits. sort_unstable_by_key ( |split| {
1187+ let contained = Self :: is_split_contained_in_search_time_range ( split, & request) ;
1188+ ( !contained, std:: cmp:: Reverse ( split. timestamp_end ( ) ) )
1189+ } ) ;
11421190
11431191 if !is_simple_all_query ( & request) {
11441192 // no optimization opportunity here.
@@ -1151,6 +1199,9 @@ impl CanSplitDoBetter {
11511199 /// This function tries to detect upfront which splits contain the top n hits and convert other
11521200 /// split searches to count only searches. It also optimizes split order.
11531201 ///
1202+ /// To skip splits in time ranged queries, we sort the splits first by whether they are
1203+ /// contained in the search request time range.
1204+ ///
11541205 /// Returns the search_requests with their split.
11551206 fn optimize (
11561207 & self ,
0 commit comments