@@ -310,3 +310,122 @@ impl InformationExtension for DistributedInformationExtension {
310310 ) ) ) )
311311 }
312312}
313+
314+ #[ cfg( test) ]
315+ mod tests {
316+ use common_meta:: node_manager:: { DatanodeManager , DatanodeRef } ;
317+ use common_meta:: peer:: Peer ;
318+ use datafusion:: common:: tree_node:: TreeNode ;
319+ use datafusion:: logical_expr:: LogicalPlan ;
320+ use datafusion:: physical_plan:: sorts:: sort:: SortExec ;
321+ use meta_client:: client:: MetaClientBuilder ;
322+ use store_api:: sst_entry:: ManifestSstEntry ;
323+ use store_api:: storage:: ScanRequest ;
324+
325+ use super :: * ;
326+ use crate :: information_schema:: DatanodeInspectKind ;
327+
328+ struct UnusedDatanodeManager ;
329+
330+ #[ async_trait:: async_trait]
331+ impl DatanodeManager for UnusedDatanodeManager {
332+ async fn datanode ( & self , _node : & Peer ) -> DatanodeRef {
333+ panic ! ( "plan tests must not contact datanodes" )
334+ }
335+ }
336+
337+ #[ derive( Debug ) ]
338+ struct ManifestScanProvider ;
339+
340+ #[ async_trait:: async_trait]
341+ impl datafusion:: catalog:: TableProvider for ManifestScanProvider {
342+ fn schema ( & self ) -> ArrowSchemaRef {
343+ ManifestSstEntry :: schema ( ) . arrow_schema ( ) . clone ( )
344+ }
345+
346+ fn table_type ( & self ) -> datafusion:: logical_expr:: TableType {
347+ datafusion:: logical_expr:: TableType :: View
348+ }
349+
350+ fn supports_filters_pushdown (
351+ & self ,
352+ filters : & [ & datafusion:: logical_expr:: Expr ] ,
353+ ) -> Result < Vec < datafusion:: logical_expr:: TableProviderFilterPushDown > > {
354+ Ok ( vec ! [ datafusion:: logical_expr:: TableProviderFilterPushDown :: Inexact ; filters. len( ) ] )
355+ }
356+
357+ async fn scan (
358+ & self ,
359+ _state : & dyn datafusion:: catalog:: Session ,
360+ projection : Option < & Vec < usize > > ,
361+ filters : & [ datafusion:: logical_expr:: Expr ] ,
362+ limit : Option < usize > ,
363+ ) -> Result < Arc < dyn ExecutionPlan > > {
364+ let schema = ManifestSstEntry :: schema ( ) ;
365+ let schema = match projection {
366+ Some ( projection) => Arc :: new ( schema. try_project ( projection) . unwrap ( ) ) ,
367+ None => schema,
368+ } ;
369+ Ok ( Arc :: new ( DistributedInspectExec :: new (
370+ DatanodeInspectRequest {
371+ kind : DatanodeInspectKind :: SstManifest ,
372+ scan : ScanRequest {
373+ projection : projection. cloned ( ) ,
374+ filters : filters. to_vec ( ) ,
375+ limit,
376+ ..Default :: default ( )
377+ } ,
378+ } ,
379+ schema,
380+ Arc :: new ( MetaClientBuilder :: new ( 0 , api:: v1:: meta:: Role :: Frontend ) . build ( ) ) ,
381+ Arc :: new ( UnusedDatanodeManager ) ,
382+ ) ) )
383+ }
384+ }
385+
386+ #[ tokio:: test]
387+ async fn test_sql_manifest_sort_stays_on_frontend ( ) {
388+ let ctx = datafusion:: prelude:: SessionContext :: new ( ) ;
389+ ctx. register_table ( "ssts_manifest" , Arc :: new ( ManifestScanProvider ) )
390+ . unwrap ( ) ;
391+ for sql in [
392+ "SELECT region_id, file_id, min_ts, max_ts, file_size FROM ssts_manifest WHERE region_id = 42 ORDER BY min_ts LIMIT 100" ,
393+ "SELECT file_id FROM ssts_manifest WHERE region_id = 42 ORDER BY max_ts DESC LIMIT 10 OFFSET 3" ,
394+ "SELECT min_ts AS ts, file_id FROM ssts_manifest WHERE region_id = 42 ORDER BY ts DESC NULLS FIRST LIMIT 10" ,
395+ "SELECT file_id FROM ssts_manifest WHERE region_id = 42 ORDER BY min_ts, max_ts DESC LIMIT 10" ,
396+ ] {
397+ let plan = ctx
398+ . sql ( sql)
399+ . await
400+ . unwrap ( )
401+ . create_physical_plan ( )
402+ . await
403+ . unwrap ( ) ;
404+ let mut found_scan = false ;
405+ let mut found_sort = false ;
406+ plan. apply ( |plan| {
407+ found_sort |= plan. is :: < SortExec > ( ) ;
408+ if let Some ( scan) = plan. downcast_ref :: < DistributedInspectExec > ( ) {
409+ found_scan = true ;
410+ // All matching rows must reach the frontend global sort.
411+ assert_eq ! ( scan. request. scan. limit, None ) ;
412+ let remote = scan. request . clone ( ) . build_plan ( ) . unwrap ( ) ;
413+ remote. apply ( |node| {
414+ assert ! ( !matches!(
415+ node,
416+ LogicalPlan :: Sort ( _) | LogicalPlan :: Limit ( _)
417+ ) ) ;
418+ Ok ( TreeNodeRecursion :: Continue )
419+ } ) ?;
420+ }
421+ Ok ( TreeNodeRecursion :: Continue )
422+ } )
423+ . unwrap ( ) ;
424+ assert ! (
425+ found_scan && found_sort,
426+ "{}" ,
427+ datafusion:: physical_plan:: displayable( plan. as_ref( ) ) . indent( true )
428+ ) ;
429+ }
430+ }
431+ }
0 commit comments