1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15- use std:: collections:: HashSet ;
1615use std:: sync:: { Arc , Weak } ;
1716
1817use arrow_schema:: SchemaRef as ArrowSchemaRef ;
@@ -255,14 +254,17 @@ impl InformationSchemaTablesBuilder {
255254 // TODO(dennis): `region_stats` API is not stable in distributed cluster because of network issue etc.
256255 // But we don't want the statements such as `show tables` fail,
257256 // so using `unwrap_or_else` here instead of `?` operator.
258- let region_stats = information_extension
259- . region_stats ( )
260- . await
261- . map_err ( |e| {
262- error ! ( e; "Failed to call region_stats" ) ;
263- e
264- } )
265- . unwrap_or_else ( |_| vec ! [ ] ) ;
257+ let region_stats = {
258+ let mut x = information_extension
259+ . region_stats ( )
260+ . await
261+ . unwrap_or_else ( |e| {
262+ error ! ( e; "Failed to find region stats in information_schema, fallback to all empty" ) ;
263+ vec ! [ ]
264+ } ) ;
265+ x. sort_unstable_by_key ( |x| x. id ) ;
266+ x
267+ } ;
266268
267269 for schema_name in catalog_manager. schema_names ( & catalog_name, None ) . await ? {
268270 let mut stream = catalog_manager. tables ( & catalog_name, & schema_name, None ) ;
@@ -273,16 +275,16 @@ impl InformationSchemaTablesBuilder {
273275 // TODO(dennis): make it working for metric engine
274276 let table_region_stats =
275277 if table_info. meta . engine == MITO_ENGINE || table_info. is_physical_table ( ) {
276- let region_ids = table_info
278+ table_info
277279 . meta
278280 . region_numbers
279281 . iter ( )
280282 . map ( |n| RegionId :: new ( table_info. ident . table_id , * n) )
281- . collect :: < HashSet < _ > > ( ) ;
282-
283- region_stats
284- . iter ( )
285- . filter ( |stat| region_ids . contains ( & stat . id ) )
283+ . flat_map ( |region_id| {
284+ region_stats
285+ . binary_search_by_key ( & region_id , |x| x . id )
286+ . map ( |i| & region_stats [ i ] )
287+ } )
286288 . collect :: < Vec < _ > > ( )
287289 } else {
288290 vec ! [ ]
0 commit comments