diff --git a/src/cmd/src/standalone.rs b/src/cmd/src/standalone.rs index ca0f3b74057..ec8305efa1c 100644 --- a/src/cmd/src/standalone.rs +++ b/src/cmd/src/standalone.rs @@ -734,6 +734,9 @@ impl StartCommand { procedure_executor.clone(), process_manager, ) + // The region servers this frontend reads are the ones built above, in this process, so a + // region input can travel as a logical plan instead of Substrait bytes. + .with_in_process_region_transport() .with_local_file_access(local_file_access); plugins::setup_frontend_plugins_post_build(&mut plugins, &plugin_opts, &fe_instance) diff --git a/src/frontend/src/instance/builder.rs b/src/frontend/src/instance/builder.rs index 624ccb2f9cd..918655d0c5e 100644 --- a/src/frontend/src/instance/builder.rs +++ b/src/frontend/src/instance/builder.rs @@ -78,6 +78,9 @@ pub struct FrontendBuilder { procedure_executor: ProcedureExecutorRef, process_manager: ProcessManagerRef, local_file_access: LocalFileAccess, + /// Whether this frontend reaches the regions of its datanodes in process. Declared by whoever + /// assembles the frontend, because only it knows what its node manager talks to. + in_process_region_transport: bool, } impl FrontendBuilder { @@ -105,6 +108,7 @@ impl FrontendBuilder { procedure_executor, process_manager, local_file_access: LocalFileAccess::Disabled, + in_process_region_transport: false, } } @@ -157,6 +161,20 @@ impl FrontendBuilder { self } + /// Declares that every region this frontend queries is read in this very process. + /// + /// The caller must own the node manager this frontend was built with, and know that its + /// `DatanodeManager` hands region reads to a `RegionServer` in this process — as standalone + /// startup does, since it passes the datanode's own region server to the node manager it + /// installs. The distributed planner then hands a region a `LogicalPlan` instead of Substrait + /// bytes, which `RegionServer::handle_read` accepts. A frontend whose node manager reaches + /// remote datanodes must leave it off: those datanodes only accept Substrait bytes + /// (`RegionServer::handle_remote_read`), so a plan that does not encode would fail there. + pub fn with_in_process_region_transport(mut self) -> Self { + self.in_process_region_transport = true; + self + } + pub fn options(&self) -> &FrontendOptions { &self.options } @@ -315,7 +333,7 @@ impl FrontendBuilder { let mut query_options = self.options.query.clone(); query_options.enable_per_region_metrics = self.options.logging.enable_per_region_metrics; - let query_engine = QueryEngineFactory::try_new_with_plugins( + let query_engine = QueryEngineFactory::try_new_with_plugins_and_region_transport( self.catalog_manager.clone(), Some(partition_manager.clone()), Some(region_query_handler.clone()), @@ -323,6 +341,7 @@ impl FrontendBuilder { Some(procedure_service_handler), Some(Arc::new(flow_service)), true, + self.in_process_region_transport, plugins.clone(), query_options, ) diff --git a/src/query/src/dist_plan/analyzer.rs b/src/query/src/dist_plan/analyzer.rs index a39514a0e43..955cc6cbe55 100644 --- a/src/query/src/dist_plan/analyzer.rs +++ b/src/query/src/dist_plan/analyzer.rs @@ -106,8 +106,35 @@ impl ExtensionOptions for DistPlannerOptions { } } -#[derive(Debug)] -pub struct DistPlannerAnalyzer; +/// Rewrites a logical plan into a distributed plan: the part above a merge scan runs on the +/// frontend, and every `MergeScan` carries the sub-plan that runs in a region. +#[derive(Debug, Default)] +pub struct DistPlannerAnalyzer { + /// Whether every region a rewritten plan reads is reached in process. + /// + /// A region input normally has to be encoded to Substrait before it can leave the frontend, and + /// [`PlanRewriter::should_expand`] cuts the plan at the first node that does not encode. When + /// regions are reached in process, that input travels as a `LogicalPlan` instead, so nothing is + /// encoded and no node has to be cut for encoding's sake. + /// + /// Whoever builds the query engine sets this, once, from the transport it wired. It is neither a + /// query-time decision nor a property of a region. + in_process_region_transport: bool, +} + +impl DistPlannerAnalyzer { + /// Creates the analyzer. + /// + /// `in_process_region_transport` must only be `true` for an engine whose region reads are served + /// by region servers in the same process. Its plans are then handed to + /// `RegionServer::handle_read` as `LogicalPlan`s; the remote read path + /// (`RegionServer::handle_remote_read`) decodes Substrait bytes and cannot do that. + pub fn new(in_process_region_transport: bool) -> Self { + Self { + in_process_region_transport, + } + } +} impl AnalyzerRule for DistPlannerAnalyzer { fn name(&self) -> &str { @@ -250,12 +277,15 @@ fn unwrap_dictionary_literals(plan: LogicalPlan) -> DfResult DfResult { + let in_process_region_transport = self.in_process_region_transport; // Use the subquery-aware transform so expression subqueries (including // nested scalar subqueries left in place by DataFusion 55's // `enable_physical_uncorrelated_scalar_subquery`) are visited at every // depth and their table scans get wrapped in `MergeScan`. - let plan = plan.transform_with_subqueries(&Self::inspect_plan_with_subquery)?; - let mut rewriter = PlanRewriter::new(&plan.data); + let plan = plan.transform_with_subqueries(&|plan| { + Self::inspect_plan_with_subquery(plan, in_process_region_transport) + })?; + let mut rewriter = PlanRewriter::new(&plan.data, in_process_region_transport); let result = plan.data.rewrite(&mut rewriter)?.data; Self::assign_merge_scan_remote_dyn_filter_producer_ids(result) } @@ -267,7 +297,10 @@ impl DistPlannerAnalyzer { Self::assign_merge_scan_remote_dyn_filter_producer_ids(result) } - fn inspect_plan_with_subquery(plan: LogicalPlan) -> DfResult> { + fn inspect_plan_with_subquery( + plan: LogicalPlan, + in_process_region_transport: bool, + ) -> DfResult> { // Workaround for https://github.com/GreptimeTeam/greptimedb/issues/5469 and https://github.com/GreptimeTeam/greptimedb/issues/5799 // FIXME(yingwen): Remove the `Limit` plan once we update DataFusion. if let LogicalPlan::Limit(_) | LogicalPlan::Distinct(_) = &plan { @@ -277,7 +310,12 @@ impl DistPlannerAnalyzer { let exprs = plan .expressions_consider_join() .into_iter() - .map(|e| e.transform(&Self::transform_subquery).map(|x| x.data)) + .map(|e| { + e.transform(|subquery_expr| { + Self::transform_subquery(subquery_expr, in_process_region_transport) + }) + .map(|x| x.data) + }) .collect::>>()?; // Some plans that are special treated (should not call `with_new_exprs` on them) @@ -289,27 +327,33 @@ impl DistPlannerAnalyzer { } } - fn transform_subquery(expr: Expr) -> DfResult> { + fn transform_subquery( + expr: Expr, + in_process_region_transport: bool, + ) -> DfResult> { match expr { Expr::Exists(exists) => Ok(Transformed::yes(Expr::Exists(Exists { - subquery: Self::handle_subquery(exists.subquery)?, + subquery: Self::handle_subquery(exists.subquery, in_process_region_transport)?, negated: exists.negated, }))), Expr::InSubquery(in_subquery) => Ok(Transformed::yes(Expr::InSubquery(InSubquery { expr: in_subquery.expr, - subquery: Self::handle_subquery(in_subquery.subquery)?, + subquery: Self::handle_subquery(in_subquery.subquery, in_process_region_transport)?, negated: in_subquery.negated, }))), Expr::ScalarSubquery(scalar_subquery) => Ok(Transformed::yes(Expr::ScalarSubquery( - Self::handle_subquery(scalar_subquery)?, + Self::handle_subquery(scalar_subquery, in_process_region_transport)?, ))), _ => Ok(Transformed::no(expr)), } } - fn handle_subquery(subquery: Subquery) -> DfResult { - let mut rewriter = PlanRewriter::new(&subquery.subquery); + fn handle_subquery( + subquery: Subquery, + in_process_region_transport: bool, + ) -> DfResult { + let mut rewriter = PlanRewriter::new(&subquery.subquery, in_process_region_transport); let mut rewrote_subquery = subquery .subquery .as_ref() @@ -400,7 +444,13 @@ enum RewriterStatus { struct PlanRewriter { /// Whether the whole plan this rewriter walks can be encoded to Substrait. /// Used by [`PlanRewriter::should_expand`] to skip the per-node encoding check. + /// + /// Only read when that gate applies, i.e. when `in_process_region_transport` is false. With + /// in-process regions the question has no answer worth computing and the field stays `false`. whole_plan_encodable: bool, + /// Whether the regions this plan reads are reached in process, so an input crosses to a region + /// as a `LogicalPlan` rather than as Substrait bytes. See [`DistPlannerAnalyzer`]. + in_process_region_transport: bool, /// Current level in the tree level: usize, /// Simulated stack for the `rewrite` recursion @@ -459,11 +509,16 @@ struct PlanRewriter { impl PlanRewriter { /// `plan` must be the root of the tree that is about to be rewritten, otherwise /// [`PlanRewriter::should_expand`] may skip a check it has to perform. - fn new(plan: &LogicalPlan) -> Self { + fn new(plan: &LogicalPlan, in_process_region_transport: bool) -> Self { Self { - whole_plan_encodable: DFLogicalSubstraitConvertor - .encode(plan, DefaultSerializer) - .is_ok(), + // Only a plan that crosses to a region as Substrait bytes has to encode at all, and + // encoding is the expensive part of a pass. With in-process regions the answer is never + // read, so it is not computed. + whole_plan_encodable: !in_process_region_transport + && DFLogicalSubstraitConvertor + .encode(plan, DefaultSerializer) + .is_ok(), + in_process_region_transport, ..Default::default() } } @@ -495,7 +550,17 @@ impl PlanRewriter { // is the dominant cost on deep PromQL plans. // When the root does not encode, the plan holds at least one node that has to stay // on the frontend and only the per-node check locates it. - if !self.whole_plan_encodable + // + // This question only exists for a plan that has to cross to a region as Substrait bytes. + // With in-process regions the same `LogicalPlan` is handed to `RegionServer::handle_read` + // never having been encoded, so encodability says nothing about where a node may run, and + // asking it would cut the plan at the first node with no serializer branch (`FusedSeries`) + // before `check_plan` could decide. Skipping it only removes a reason to expand: every + // check below still runs, and a node they call `NonCommutative`/`Unimplemented`/ + // `Unsupported` still expands, so a node no serializer lists only rides to a region when + // the categorizer places it there. + if !self.in_process_region_transport + && !self.whole_plan_encodable && let Err(e) = DFLogicalSubstraitConvertor.encode(plan, DefaultSerializer) { debug!( diff --git a/src/query/src/dist_plan/analyzer/test.rs b/src/query/src/dist_plan/analyzer/test.rs index 694f2bcde59..c0a4683b1c9 100644 --- a/src/query/src/dist_plan/analyzer/test.rs +++ b/src/query/src/dist_plan/analyzer/test.rs @@ -68,6 +68,12 @@ fn collect_merge_scan_remote_dyn_filter_producer_ids( producer_ids.extend(producer_id_list); } +/// The analyzer as an engine whose regions are reached over the wire builds it: every plan a region +/// receives from it has to be encodable. +fn analyzer() -> DistPlannerAnalyzer { + DistPlannerAnalyzer::default() +} + struct MergeScanRemoteDynFilterProducerIdCollector<'a> { producer_ids: &'a mut Vec, } @@ -395,9 +401,7 @@ fn frontend_only_histogram_folds_stay_above_merge_scan() { "{name}" ); - let result = DistPlannerAnalyzer {} - .analyze(plan, &ConfigOptions::default()) - .unwrap(); + let result = analyzer().analyze(plan, &ConfigOptions::default()).unwrap(); let result_text = result.to_string(); assert!( result_text.contains("HistogramFold:"), @@ -442,9 +446,7 @@ fn unencodable_unnest_stays_above_merge_scan() { .is_err() ); - let result = DistPlannerAnalyzer {} - .analyze(plan, &ConfigOptions::default()) - .unwrap(); + let result = analyzer().analyze(plan, &ConfigOptions::default()).unwrap(); let result_text = result.to_string(); assert!(result_text.contains("Unnest:"), "{result_text}"); @@ -481,9 +483,7 @@ fn dictionary_literals_are_unwrapped_before_pushdown() { .build() .unwrap(); - let result = DistPlannerAnalyzer {} - .analyze(plan, &ConfigOptions::default()) - .unwrap(); + let result = analyzer().analyze(plan, &ConfigOptions::default()).unwrap(); assert!( result .to_string() @@ -578,7 +578,7 @@ fn expand_proj_sort_proj() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ "Projection: t.number", @@ -625,7 +625,7 @@ fn expand_proj_sort_partial_proj() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ "Projection: t.number", @@ -661,7 +661,7 @@ fn expand_sort_limit() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ "Projection: t.pk1, t.pk2, t.pk3, t.ts, t.number", @@ -699,7 +699,7 @@ fn expand_sort_alias_limit() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ "Projection: something", @@ -738,7 +738,7 @@ fn expand_sort_alias_conflict_limit() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan.clone(), &config); + let result = analyzer().analyze(plan.clone(), &config); assert!(result.is_err(), "Expected error for ambiguous alias"); assert!(format!("{result:?}").contains("AmbiguousReference")); @@ -746,7 +746,7 @@ fn expand_sort_alias_conflict_limit() { config.extensions.insert(DistPlannerOptions { allow_query_fallback: true, }); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ "Limit: skip=0, fetch=10", @@ -780,7 +780,7 @@ fn expand_sort_alias_conflict_but_not_really_limit() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ "Projection: t.pk1", @@ -818,7 +818,7 @@ fn expand_limit_sort() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ "Sort: t.pk1 ASC NULLS LAST", @@ -853,7 +853,7 @@ fn expand_sort_limit_sort() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ "Sort: t.pk1 ASC NULLS LAST", @@ -898,7 +898,7 @@ fn expand_proj_step_aggr() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ "Projection: min(t.number)", @@ -943,7 +943,7 @@ fn expand_proj_alias_fake_part_col_aggr() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ "Projection: pk1, pk2, min(t.number)", @@ -987,7 +987,7 @@ fn expand_proj_alias_aliased_part_col_aggr() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ "Projection: pk42, pk43, min(t.number)", @@ -1022,7 +1022,7 @@ fn expand_expr_over_part_col_aggr() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ "Projection: substr(t.pk1,Int64(1),Int64(1)), t.pk2, min(t.number)", @@ -1057,7 +1057,7 @@ fn expand_part_col_aggr_step_aggr() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ "Projection: min(max(t.number))", @@ -1090,7 +1090,7 @@ fn expand_step_aggr_step_aggr() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ "Aggregate: groupBy=[[]], aggr=[[min(max(t.number))]]", @@ -1135,7 +1135,7 @@ fn expand_part_col_aggr_part_col_aggr() { assert_eq!(expected_original, plan.to_string()); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ "Projection: t.pk1, t.pk2, min(max(t.number))", @@ -1167,7 +1167,7 @@ fn expand_step_aggr_proj() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ "Projection: min(t.number)", @@ -1225,7 +1225,7 @@ fn expand_complex_col_req_sort_pql() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ "Projection: t.pk1, t.pk2", @@ -1271,7 +1271,7 @@ fn expand_proj_sort_step_aggr_limit() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ "Limit: skip=0, fetch=10", " Aggregate: groupBy=[[]], aggr=[[min(t.number)]]", @@ -1312,7 +1312,7 @@ fn expand_proj_sort_limit_step_aggr() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ "Aggregate: groupBy=[[]], aggr=[[min(t.number)]]", " Projection: t.number", @@ -1354,7 +1354,7 @@ fn expand_proj_limit_step_aggr_sort() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ "Sort: min(t.number) ASC NULLS LAST", " Aggregate: groupBy=[[]], aggr=[[min(t.number)]]", @@ -1396,7 +1396,7 @@ fn expand_proj_sort_part_col_aggr_limit() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ "Limit: skip=0, fetch=10", " Aggregate: groupBy=[[t.pk1, t.pk2]], aggr=[[min(t.number)]]", @@ -1438,7 +1438,7 @@ fn expand_proj_sort_limit_part_col_aggr() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ "Aggregate: groupBy=[[t.pk1, t.pk2]], aggr=[[min(t.number)]]", " Projection: t.number, t.pk1, t.pk2", @@ -1480,7 +1480,7 @@ fn expand_proj_part_col_aggr_limit_sort() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ "Sort: t.pk2 ASC NULLS LAST", " Projection: t.pk1, t.pk2, min(t.number)", @@ -1522,7 +1522,7 @@ fn expand_proj_part_col_aggr_sort_limit() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ "Projection: t.pk1, t.pk2, min(t.number)", " Limit: skip=0, fetch=10", @@ -1557,9 +1557,7 @@ fn test_simplify_select_now_expression() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {} - .analyze(plan.clone(), &config) - .unwrap(); + let result = analyzer().analyze(plan.clone(), &config).unwrap(); common_telemetry::info!("Analyzed plan: {}", result); @@ -1615,7 +1613,7 @@ fn sibling_merge_scans_have_unique_remote_dyn_filter_producer_ids() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let mut producer_ids = Vec::new(); collect_merge_scan_remote_dyn_filter_producer_id_list(&result, &mut producer_ids); @@ -1670,7 +1668,7 @@ fn pre_merge_scan_optimizer_eliminates_projected_false_filter() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); assert_eq!("EmptyRelation: rows=0", result.to_string()); } @@ -1718,7 +1716,7 @@ fn test_simplify_now_expression() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let plan_str = result.to_string(); common_telemetry::info!("Analyzed plan: {}", plan_str); @@ -1757,7 +1755,7 @@ fn expand_proj_limit_part_col_aggr_sort() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); // Pre-MergeScan optimizer intentionally excludes PushDownLimit, so the // remote plan shows an explicit Limit node instead of `fetch=10` on // TableScan. @@ -1802,7 +1800,7 @@ fn expand_proj_limit_sort_part_col_aggr() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); // Pre-MergeScan optimizer intentionally excludes PushDownLimit, so the // remote plan shows an explicit Limit node instead of `fetch=10` on // TableScan. @@ -1840,7 +1838,7 @@ fn expand_step_aggr_limit() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ "Limit: skip=0, fetch=10", " Projection: t.pk1, min(t.number)", @@ -1873,7 +1871,7 @@ fn expand_step_aggr_avg_limit() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ "Limit: skip=0, fetch=10", " Projection: t.pk1, avg(t.number)", @@ -1906,7 +1904,7 @@ fn expand_part_col_aggr_limit() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ "Projection: t.pk1, t.pk2, min(t.number)", " Limit: skip=0, fetch=10", @@ -1940,7 +1938,7 @@ fn transform_simple_projection_filter() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ "Distinct:", " MergeScan [is_placeholder=false]", @@ -1968,7 +1966,7 @@ fn transform_aggregator() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = "Projection: avg(t.number)\ \n MergeScan [is_placeholder=false, remote_input=[\ \nAggregate: groupBy=[[]], aggr=[[avg(t.number)]]\ @@ -1994,7 +1992,7 @@ fn transform_distinct_order() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ "Projection: t.number", " MergeScan [is_placeholder=false, remote_input=[ @@ -2022,7 +2020,7 @@ fn transform_single_limit() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = "Projection: t.number\ \n MergeScan [is_placeholder=false, remote_input=[ Limit: skip=0, fetch=1 @@ -2063,7 +2061,7 @@ fn transform_unalighed_join_with_alias() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ "Limit: skip=0, fetch=1", " LeftSemi Join: t.number = right.number", @@ -2099,7 +2097,7 @@ fn transform_subquery_sort_alias() { .build() .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ "Projection: a.pk1, a.pk2, a.pk3, a.ts, a.number", " MergeSort: a.number ASC NULLS LAST", @@ -2130,7 +2128,7 @@ fn transform_sort_subquery_alias() { .build() .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ "Projection: a.pk1, a.pk2, a.pk3, a.ts, a.number", " MergeSort: a.number ASC NULLS LAST", @@ -2169,7 +2167,7 @@ fn remote_dyn_filter_producer_ids_do_not_collide_between_subquery_and_outer_plan .unwrap() .build() .unwrap(); - let rewritten = DistPlannerAnalyzer {}.try_push_down(outer_plan).unwrap(); + let rewritten = analyzer().try_push_down(outer_plan).unwrap(); let mut producer_ids = BTreeSet::new(); collect_merge_scan_remote_dyn_filter_producer_ids(&rewritten, &mut producer_ids); @@ -2201,7 +2199,7 @@ fn date_bin_ts_group_by() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let expected = [ r#"Projection: date_bin(IntervalDayTime("IntervalDayTime { days: 0, milliseconds: 60000 }"),t.ts), min(t.number)"#, @@ -2249,9 +2247,7 @@ fn test_last_value_order_by() { try_encode_decode_substrait(&plan, ctx.state()); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {} - .analyze(plan.clone(), &config) - .unwrap(); + let result = analyzer().analyze(plan.clone(), &config).unwrap(); let expected = [ "Projection: last_value(t.ts) ORDER BY [t.ts ASC NULLS FIRST]", @@ -2312,9 +2308,7 @@ fn test_last_value_no_order_by() { try_encode_decode_substrait(&split.lower_state, ctx.state()); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {} - .analyze(plan.clone(), &config) - .unwrap(); + let result = analyzer().analyze(plan.clone(), &config).unwrap(); let expected = [ "Projection: last_value(t.ts)", @@ -2344,9 +2338,7 @@ fn test_table_scan_projection() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {} - .analyze(plan.clone(), &config) - .unwrap(); + let result = analyzer().analyze(plan.clone(), &config).unwrap(); let expected = [ "Projection: t.ts", " MergeScan [is_placeholder=false, remote_input=[", @@ -2380,9 +2372,7 @@ fn test_table_scan_cast_projection_pushdown() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {} - .analyze(plan.clone(), &config) - .unwrap(); + let result = analyzer().analyze(plan.clone(), &config).unwrap(); let expected = [ "Projection: ts", " MergeScan [is_placeholder=false, remote_input=[", @@ -2426,9 +2416,7 @@ fn test_cast_filter_simplified_after_pushdown() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {} - .analyze(plan.clone(), &config) - .unwrap(); + let result = analyzer().analyze(plan.clone(), &config).unwrap(); let expected = [ "Projection: ts", " MergeScan [is_placeholder=false, remote_input=[", @@ -2483,7 +2471,7 @@ fn test_join_side_local_filter_pushdown_into_merge_scan() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); assert_remote_table_scan_filters_are_safe(&result); let predicate = col("t1.pk1").eq(lit("v")); @@ -2555,7 +2543,7 @@ fn test_left_join_left_side_filter_pushdown_into_merge_scan() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); assert_remote_table_scan_filters_are_safe(&result); let plan_str = result.to_string(); @@ -2611,7 +2599,7 @@ fn test_join_cross_table_predicate_not_pushed_to_single_side() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); assert_remote_table_scan_filters_are_safe(&result); let plan_str = result.to_string(); @@ -2668,9 +2656,7 @@ fn scheduled_now_yields_stable_literal_in_remote_plan() { scheduled_time: Some(scheduled_time), }); - let result = DistPlannerAnalyzer {} - .analyze(plan.clone(), &config) - .unwrap(); + let result = analyzer().analyze(plan.clone(), &config).unwrap(); let result_str = result.to_string(); common_telemetry::info!("Analyzed plan with scheduled time: {}", result_str); @@ -2706,9 +2692,7 @@ fn scheduled_now_yields_stable_literal_in_remote_plan() { // Absent the extension (default config), the same plan simplifies to a // wall-clock literal — the remote_input contains a variable timestamp. let default_config = ConfigOptions::default(); - let wall_result = DistPlannerAnalyzer {} - .analyze(plan, &default_config) - .unwrap(); + let wall_result = analyzer().analyze(plan, &default_config).unwrap(); let wall_str = wall_result.to_string(); // The wall-clock result must NOT contain the scheduled literal. assert!( @@ -2769,7 +2753,7 @@ fn scheduled_current_timestamp_filter_folds_before_remote_pushdown() { scheduled_time: Some(scheduled_time), }); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); assert_remote_table_scan_filters_are_safe(&result); let result_str = result.to_string(); @@ -2816,7 +2800,7 @@ fn scheduled_none_falls_back_to_wall_clock() { scheduled_time: None, }); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let result_str = result.to_string(); // Must still simplify now() — remotely a literal, top is now(). @@ -2884,7 +2868,7 @@ fn expand_nested_scalar_subquery() { .unwrap(); let config = ConfigOptions::default(); - let result = DistPlannerAnalyzer {}.analyze(plan, &config).unwrap(); + let result = analyzer().analyze(plan, &config).unwrap(); let result_str = result.to_string(); // The main scan and both subquery levels must each be wrapped in a MergeScan. diff --git a/src/query/src/dist_plan/commutativity.rs b/src/query/src/dist_plan/commutativity.rs index 8d26605cece..c8d56cc9a04 100644 --- a/src/query/src/dist_plan/commutativity.rs +++ b/src/query/src/dist_plan/commutativity.rs @@ -417,7 +417,7 @@ mod tests { use datafusion_common::config::ConfigOptions; use datafusion_expr::LogicalPlanBuilder; use datafusion_expr::expr::ScalarFunction; - use datafusion_expr::{Extension, LogicalPlan}; + use datafusion_expr::{Extension, LogicalPlan, UserDefinedLogicalNodeCore}; use datafusion_functions::core::coalesce; use datafusion_optimizer::analyzer::AnalyzerRule; use datatypes::series::SeriesOrdering; @@ -572,6 +572,124 @@ mod tests { plan.inputs().into_iter().any(holds_fused_series) } + /// An extension node with no `ExtensionSerializer` branch: neither one under its own name nor + /// one it shares, since the serializer lists names and not shapes. It stands in for the nodes + /// that cannot reach a region as bytes for their own reasons, which the encoding gate must keep + /// honouring. The categorizer refuses it, as it refuses every name it does not know. + #[derive(Debug, PartialEq, Eq, Hash)] + struct UnserializableNode { + input: LogicalPlan, + schema: DFSchemaRef, + } + + impl PartialOrd for UnserializableNode { + fn partial_cmp(&self, other: &Self) -> Option { + let ordering = self.input.partial_cmp(&other.input); + match ordering { + Some(std::cmp::Ordering::Equal) if self.schema != other.schema => None, + ordering => ordering, + } + } + } + + impl UnserializableNode { + fn new(input: LogicalPlan) -> Self { + let schema = input.schema().clone(); + Self { input, schema } + } + } + + impl UserDefinedLogicalNodeCore for UnserializableNode { + fn name(&self) -> &str { + "UnserializableNode" + } + + fn inputs(&self) -> Vec<&LogicalPlan> { + vec![&self.input] + } + + fn schema(&self) -> &DFSchemaRef { + &self.schema + } + + fn expressions(&self) -> Vec { + vec![] + } + + fn fmt_for_explain(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "UnserializableNode") + } + + fn with_exprs_and_inputs( + &self, + _exprs: Vec, + mut inputs: Vec, + ) -> DfResult { + Ok(Self::new(inputs.swap_remove(0))) + } + } + + /// The plan that node builds over `input`. + fn unserializable_plan(input: LogicalPlan) -> LogicalPlan { + LogicalPlan::Extension(Extension { + node: Arc::new(UnserializableNode::new(input)), + }) + } + + fn holds_unserializable_node(plan: &LogicalPlan) -> bool { + if let LogicalPlan::Extension(extension) = plan + && extension.node.as_any().is::() + { + return true; + } + plan.inputs().into_iter().any(holds_unserializable_node) + } + + fn holds_table_scan(plan: &LogicalPlan) -> bool { + if matches!(plan, LogicalPlan::TableScan(_)) { + return true; + } + plan.inputs().into_iter().any(holds_table_scan) + } + + /// The merge scan `plan` is, if it is one. + fn as_merge_scan(plan: &LogicalPlan) -> Option<&MergeScanLogicalPlan> { + if let LogicalPlan::Extension(extension) = plan { + extension + .node + .as_any() + .downcast_ref::() + } else { + None + } + } + + /// The first merge scan in `plan`'s visible tree. A merge scan's remote input is not one of its + /// children, so this does not look into one. + fn first_merge_scan(plan: &LogicalPlan) -> Option<&MergeScanLogicalPlan> { + if let Some(merge_scan) = as_merge_scan(plan) { + return Some(merge_scan); + } + plan.inputs().into_iter().find_map(first_merge_scan) + } + + fn analyze(analyzer: DistPlannerAnalyzer, plan: LogicalPlan) -> LogicalPlan { + analyzer.analyze(plan, &ConfigOptions::default()).unwrap() + } + + /// Whether the remote input of any merge scan in `plan` holds a node `wanted` accepts. + fn any_remote_input_holds(plan: &LogicalPlan, wanted: fn(&LogicalPlan) -> bool) -> bool { + let mut holds = false; + plan.apply(|node| { + if let Some(merge_scan) = as_merge_scan(node) { + holds |= wanted(merge_scan.input()); + } + Ok(TreeNodeRecursion::Continue) + }) + .unwrap(); + holds + } + #[test] fn fused_series_over_its_scan_is_commutative() { let plan = fused_series_plan(table_scan_plan()); @@ -659,49 +777,82 @@ mod tests { assert!(!reads_across_series(single_selector.root())); } - /// Blocks placement, and pins the blocker rather than the intent. + /// The encoding gate is what kept the node on the frontend, and it only exists for a plan that + /// has to be encoded. /// - /// `PlanRewriter::should_expand` asks two questions about a node before it lets the descent - /// continue, and the Substrait one comes first: unless the *whole* root plan encodes, every - /// subtree that fails to encode is expanded on, whichever way `check_plan` answers. A - /// `FusedSeries` node has no serializer branch — `ExtensionSerializer` answers - /// `NotImplemented` for every name it does not list — so its subtree does not encode, the - /// boundary is drawn at the node, and the region receives the bare scan instead of the node. + /// `PlanRewriter::should_expand` asked two questions about a node before it let the descent + /// continue, and the Substrait one came first: unless the *whole* root plan encoded, every + /// subtree that failed to encode was expanded on, whichever way `check_plan` answered. A + /// `FusedSeries` node has no serializer branch — `ExtensionSerializer` answers `NotImplemented` + /// for every name it does not list — so over the wire the boundary is still drawn at the node + /// and the region receives the bare scan. /// - /// The commutativity arm above is necessary and not sufficient. This test fails the day the - /// node gains a serializer branch (or the analyzer stops asking), which is when the placement - /// assertion this one replaces can be written. + /// A plan handed to `RegionServer::handle_read` as a `LogicalPlan` is never encoded, so there + /// the gate has nothing to say and the commutativity arm decides: the node rides inside the + /// merge scan. #[test] - fn fused_series_does_not_encode_so_the_analyzer_keeps_it_on_the_frontend() { + fn fused_series_rides_to_the_region_only_when_regions_are_reached_in_process() { let plan = fused_series_plan(table_scan_plan()); assert!( DFLogicalSubstraitConvertor .encode(&plan, DefaultSerializer) .is_err(), - "the fused node encodes now, so it can be placed inside the merge scan" + "the fused node encodes now, so the encoding gate decides nothing" + ); + assert!( + DFLogicalSubstraitConvertor + .encode(&table_scan_plan(), DefaultSerializer) + .is_ok(), + "the scan under the fused node encodes, so the fused node is the only reason this plan does not" ); - let analyzed = DistPlannerAnalyzer {} - .analyze(plan, &ConfigOptions::default()) - .unwrap(); - let mut remote_input_holds_fused = false; - analyzed - .apply(|node| { - if let LogicalPlan::Extension(extension) = node - && let Some(merge_scan) = extension - .node - .as_any() - .downcast_ref::() - { - remote_input_holds_fused = holds_fused_series(merge_scan.input()); - } - Ok(TreeNodeRecursion::Continue) - }) - .unwrap(); + // Over the wire the plan has to encode, so the boundary is drawn at the node. + let encoded = analyze(DistPlannerAnalyzer::new(false), plan.clone()); assert!( - !remote_input_holds_fused, - "the analyzer placed the node despite the encoding gate: {analyzed}" + !any_remote_input_holds(&encoded, holds_fused_series), + "the analyzer placed the node although its plan has to encode: {encoded}" + ); + + // In process nothing is encoded, so the categorizer places the node. + let in_process = analyze(DistPlannerAnalyzer::new(true), plan); + assert!( + any_remote_input_holds(&in_process, holds_fused_series), + "the analyzer kept the node on the frontend although regions are reached in process: {in_process}" + ); + } + + /// Skipping the encoding gate removes one reason to expand and adds none: a node that does not + /// encode for its own reasons, and that the categorizer refuses, still draws the boundary at + /// itself when regions are reached in process. + /// + /// Without this, the skip would amount to placing every unencodable node region-side. + #[test] + fn a_plan_that_does_not_encode_for_another_reason_still_expands_at_that_node() { + let plan = unserializable_plan(table_scan_plan()); + assert!( + DFLogicalSubstraitConvertor + .encode(&plan, DefaultSerializer) + .is_err(), + "the stand-in node has a serializer branch now, so it no longer proves anything" + ); + + let analyzed = analyze(DistPlannerAnalyzer::new(true), plan); + + assert!( + !any_remote_input_holds(&analyzed, holds_unserializable_node), + "the analyzer placed a node the categorizer refuses: {analyzed}" + ); + let inputs = analyzed.inputs(); + let [input] = inputs.as_slice() else { + panic!("expected the node to keep one input in: {analyzed}"); + }; + // `expand` keeps a projection over the merge scan to restore the schema of what it wrapped. + let merge_scan = first_merge_scan(input) + .unwrap_or_else(|| panic!("expected the boundary right below the node in: {analyzed}")); + assert!( + holds_table_scan(merge_scan.input()), + "expected the scan under the node to be the region input in: {analyzed}" ); } } diff --git a/src/query/src/query_engine.rs b/src/query/src/query_engine.rs index c512b9c1e5e..1c773ad099e 100644 --- a/src/query/src/query_engine.rs +++ b/src/query/src/query_engine.rs @@ -189,7 +189,7 @@ impl QueryEngineFactory { plugins: Plugins, options: QueryOptions, ) -> datafusion::error::Result { - let state = Arc::new(QueryEngineState::try_new( + Self::try_new_with_plugins_and_region_transport( catalog_manager, partition_rule_manager, region_query_handler, @@ -197,6 +197,40 @@ impl QueryEngineFactory { procedure_service_handler, flow_service_handler, with_dist_planner, + false, + plugins, + options, + ) + } + + /// [`QueryEngineFactory::try_new_with_plugins`], additionally stating how the engine reaches + /// the regions it queries. + /// + /// `in_process_region_transport` reaches `DistPlannerAnalyzer`: when it is `true`, a region + /// input may travel as a `LogicalPlan` instead of Substrait bytes, which only a region server in + /// the same process can accept. + #[allow(clippy::too_many_arguments)] + pub fn try_new_with_plugins_and_region_transport( + catalog_manager: CatalogManagerRef, + partition_rule_manager: Option, + region_query_handler: Option, + table_mutation_handler: Option, + procedure_service_handler: Option, + flow_service_handler: Option, + with_dist_planner: bool, + in_process_region_transport: bool, + plugins: Plugins, + options: QueryOptions, + ) -> datafusion::error::Result { + let state = Arc::new(QueryEngineState::try_new_with_region_transport( + catalog_manager, + partition_rule_manager, + region_query_handler, + table_mutation_handler, + procedure_service_handler, + flow_service_handler, + with_dist_planner, + in_process_region_transport, plugins.clone(), options, )?); diff --git a/src/query/src/query_engine/state.rs b/src/query/src/query_engine/state.rs index e2095c33530..3ee6d2db3af 100644 --- a/src/query/src/query_engine/state.rs +++ b/src/query/src/query_engine/state.rs @@ -147,6 +147,39 @@ impl QueryEngineState { with_dist_planner: bool, plugins: Plugins, options: QueryOptionsNew, + ) -> DfResult { + Self::try_new_with_region_transport( + catalog_list, + partition_rule_manager, + region_query_handler, + table_mutation_handler, + procedure_service_handler, + flow_service_handler, + with_dist_planner, + false, + plugins, + options, + ) + } + + /// [`QueryEngineState::try_new`], additionally stating how the engine reaches its regions. + /// + /// `in_process_region_transport` reaches [`DistPlannerAnalyzer`]: when it is `true` the + /// distributed planner may hand a region a `LogicalPlan` instead of Substrait bytes, which only + /// the in-process read path accepts. Every other caller builds an engine whose regions are + /// reached over the wire, which is what [`QueryEngineState::try_new`] states. + #[allow(clippy::too_many_arguments)] + pub(crate) fn try_new_with_region_transport( + catalog_list: CatalogManagerRef, + partition_rule_manager: Option, + region_query_handler: Option, + table_mutation_handler: Option, + procedure_service_handler: Option, + flow_service_handler: Option, + with_dist_planner: bool, + in_process_region_transport: bool, + plugins: Plugins, + options: QueryOptionsNew, ) -> DfResult { let total_memory = get_total_memory_bytes().max(0) as u64; let memory_pool_size = options.memory_pool_size.resolve(total_memory) as usize; @@ -209,7 +242,9 @@ impl QueryEngineState { )), ); if with_dist_planner { - analyzer.rules.push(Arc::new(DistPlannerAnalyzer)); + analyzer.rules.push(Arc::new(DistPlannerAnalyzer::new( + in_process_region_transport, + ))); analyzer.rules.push(Arc::new(JsonSchemaConcretizeRule)); } analyzer.rules.push(Arc::new(FixStateUdafOrderingAnalyzer));