mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-11-17 16:24:07 +00:00
refactor(promql): remove duplicated planner facts (#10239)
Signed-off-by: discord9 <discord9@163.com>
This commit is contained in:
@@ -2234,9 +2234,6 @@ pub struct FusedSeriesExec {
|
||||
matching_parallelism: Option<usize>,
|
||||
/// Whether this node is a partition-local canonical producer.
|
||||
canonical_producer: bool,
|
||||
/// Test-only inspection of the physical routing contract; no execution behavior depends on it.
|
||||
#[cfg(feature = "test-utils")]
|
||||
canonical_producer_for_test: bool,
|
||||
/// This node's partitioning, emission and boundedness.
|
||||
properties: Arc<PlanProperties>,
|
||||
}
|
||||
@@ -2633,8 +2630,6 @@ impl FusedSeriesExec {
|
||||
format,
|
||||
matching_parallelism,
|
||||
canonical_producer,
|
||||
#[cfg(feature = "test-utils")]
|
||||
canonical_producer_for_test: canonical_producer,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -2647,7 +2642,7 @@ impl FusedSeriesExec {
|
||||
#[cfg(feature = "test-utils")]
|
||||
pub fn is_canonical_producer_exec(plan: &dyn ExecutionPlan) -> bool {
|
||||
plan.downcast_ref::<FusedSeriesExec>()
|
||||
.is_some_and(|fused| fused.canonical_producer_for_test)
|
||||
.is_some_and(|fused| fused.canonical_producer)
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for FusedSeriesExec {
|
||||
|
||||
@@ -1778,6 +1778,64 @@ fn canonical_producer_node() -> FusedSeries {
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fused_series_canonical_producer_exec_tracks_physical_routing() {
|
||||
let canonical = canonical_producer_node();
|
||||
let ordinary = FusedSeries::try_new_with_format(
|
||||
Arc::from(PROMQL),
|
||||
canonical.tree().clone(),
|
||||
canonical.binding().clone(),
|
||||
vec![input_plan(frame_schema())],
|
||||
canonical.schema().clone(),
|
||||
FusedOutputFormat::SeriesFrame,
|
||||
)
|
||||
.unwrap();
|
||||
let input = || {
|
||||
(
|
||||
memory_exec_with(frame_schema(), vec![]),
|
||||
RawLabelSource::Explicit,
|
||||
SeriesOrdering::Contiguous,
|
||||
)
|
||||
};
|
||||
let router = canonical.to_execution_plan(vec![input()]).unwrap();
|
||||
let producer = router.children()[0];
|
||||
let ordinary_exec = ordinary.to_execution_plan(vec![input()]).unwrap();
|
||||
let rebuilt = Arc::clone(producer)
|
||||
.replace_children(
|
||||
vec![Arc::clone(producer.children()[0])],
|
||||
ReplaceChildrenOptions::new(ChildrenPropertiesMode::Recompute),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert!(router.downcast_ref::<SeriesRepartitionExec>().is_some());
|
||||
assert!(
|
||||
producer
|
||||
.downcast_ref::<super::FusedSeriesExec>()
|
||||
.unwrap()
|
||||
.canonical_producer
|
||||
);
|
||||
assert!(
|
||||
!ordinary_exec
|
||||
.downcast_ref::<super::FusedSeriesExec>()
|
||||
.unwrap()
|
||||
.canonical_producer
|
||||
);
|
||||
assert!(
|
||||
rebuilt
|
||||
.downcast_ref::<super::FusedSeriesExec>()
|
||||
.unwrap()
|
||||
.canonical_producer
|
||||
);
|
||||
|
||||
#[cfg(feature = "test-utils")]
|
||||
{
|
||||
assert!(!super::is_canonical_producer_exec(router.as_ref()));
|
||||
assert!(super::is_canonical_producer_exec(producer.as_ref()));
|
||||
assert!(!super::is_canonical_producer_exec(ordinary_exec.as_ref()));
|
||||
assert!(super::is_canonical_producer_exec(rebuilt.as_ref()));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fused_series_encoding_preserves_state_and_canonical_producer() {
|
||||
// A state-exporting producer encodes like any other node: serialization is the node's own
|
||||
|
||||
@@ -110,10 +110,6 @@ pub(crate) fn native_promql_requested(query_ctx: &QueryContextRef) -> bool {
|
||||
.is_some_and(|value| value.eq_ignore_ascii_case("true"))
|
||||
}
|
||||
|
||||
/// The analyzer rule an engine that plans distributed statements installs on its session; see
|
||||
/// [`DistPlannerAnalyzer`](crate::dist_plan::analyzer::DistPlannerAnalyzer).
|
||||
const DIST_PLANNER_RULE: &str = "DistPlannerAnalyzer";
|
||||
|
||||
/// Whether a plan built here is split into a frontend plan over regions reached over the wire.
|
||||
///
|
||||
/// This is the one condition under which every scan this route prepares has to be a
|
||||
@@ -123,16 +119,9 @@ const DIST_PLANNER_RULE: &str = "DistPlannerAnalyzer";
|
||||
/// keep running region-locally, so those stay and this returns `false`.
|
||||
///
|
||||
/// An engine that plans no distributed statement plans no region at all, and its plans are the
|
||||
/// standalone ones: it installs the distributed planner as an analyzer rule exactly when it was
|
||||
/// built to split statements, so the rule says whether this engine has a region to satisfy.
|
||||
/// standalone ones. The engine records whether it was built to split statements.
|
||||
fn plans_over_remote_regions(query_engine_state: &QueryEngineState) -> bool {
|
||||
!query_engine_state.in_process_region_transport()
|
||||
&& query_engine_state
|
||||
.session_state()
|
||||
.analyzer()
|
||||
.rules
|
||||
.iter()
|
||||
.any(|rule| rule.name() == DIST_PLANNER_RULE)
|
||||
!query_engine_state.in_process_region_transport() && query_engine_state.with_dist_planner()
|
||||
}
|
||||
|
||||
/// Whether this executable plan is the native raw PromQL topology, the one plan shape whose
|
||||
@@ -1314,7 +1303,7 @@ impl PromPlanner {
|
||||
// what each of them reads.
|
||||
if required_placement || matches!(locality, InputLocality::SinglePartition) {
|
||||
let segment_metric = if cross_table { None } else { metric.clone() };
|
||||
let plan = Box::pin(segment_tree(
|
||||
let plan = segment_tree(
|
||||
prom_expr,
|
||||
&tree,
|
||||
&children,
|
||||
@@ -1335,8 +1324,7 @@ impl PromPlanner {
|
||||
// a column that is not a tag. An unpartitioned table's scans are whole in one
|
||||
// region, so its cuts keep the bare-scan form.
|
||||
self_contained || any_split_series || (per_source_inputs && any_partitioned),
|
||||
))
|
||||
.await?;
|
||||
)?;
|
||||
if let Some(plan) = plan {
|
||||
return Ok(Some((plan, single_input_context)));
|
||||
}
|
||||
@@ -2247,7 +2235,7 @@ fn build_inner_segment(
|
||||
/// order, and recursive segments publish frames for their parents to consume. Only the statement
|
||||
/// root publishes rows.
|
||||
#[allow(clippy::too_many_arguments, clippy::type_complexity)]
|
||||
async fn segment_tree(
|
||||
fn segment_tree(
|
||||
prom_expr: &PromExpr,
|
||||
tree: &NativeTree,
|
||||
children: &[LogicalPlan],
|
||||
@@ -2318,7 +2306,7 @@ async fn segment_tree(
|
||||
let Some(cut_tree) = NativeTree::try_new(cut.clone()).ok() else {
|
||||
return Ok(None);
|
||||
};
|
||||
let plan = Box::pin(segment_tree(
|
||||
let plan = segment_tree(
|
||||
prom_expr,
|
||||
&cut_tree,
|
||||
¤t_children,
|
||||
@@ -2333,8 +2321,7 @@ async fn segment_tree(
|
||||
canonical_segmentation,
|
||||
root_kind,
|
||||
remote_raw_boundary,
|
||||
))
|
||||
.await?;
|
||||
)?;
|
||||
match plan {
|
||||
Some(plan) => {
|
||||
let Some(cut_tags) = fused_output_tags(cut, ¤t_tags) else {
|
||||
@@ -3434,7 +3421,8 @@ mod tests {
|
||||
NativeType, aggregate_reads_histogram, calls_rate_or_increase, collect_selectors,
|
||||
cut_segments, fused_output_schema, fused_output_tags, hybrid_binary_admitted,
|
||||
independent_scan_pair, native_output_tags, native_promql_requested,
|
||||
native_raw_plan_bounds_region_reads, node_children, scan_time_filter,
|
||||
native_raw_plan_bounds_region_reads, node_children, plans_over_remote_regions,
|
||||
scan_time_filter,
|
||||
};
|
||||
|
||||
#[test]
|
||||
@@ -3453,6 +3441,43 @@ mod tests {
|
||||
assert!(!calls_rate_or_increase(&expr), "{query}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn plans_over_remote_regions_matches_engine_construction() {
|
||||
use catalog::memory::new_memory_catalog_manager;
|
||||
use common_base::Plugins;
|
||||
|
||||
use crate::options::QueryOptions;
|
||||
use crate::query_engine::QueryEngineState;
|
||||
|
||||
for (with_dist_planner, in_process_region_transport, expected) in [
|
||||
(false, false, false),
|
||||
(false, true, false),
|
||||
(true, false, true),
|
||||
(true, true, false),
|
||||
] {
|
||||
let state = QueryEngineState::try_new_with_region_transport(
|
||||
new_memory_catalog_manager().unwrap(),
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
with_dist_planner,
|
||||
in_process_region_transport,
|
||||
Plugins::default(),
|
||||
QueryOptions::default(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(state.with_dist_planner(), with_dist_planner);
|
||||
assert_eq!(
|
||||
plans_over_remote_regions(&state),
|
||||
expected,
|
||||
"with_dist_planner={with_dist_planner}, in_process_region_transport={in_process_region_transport}"
|
||||
);
|
||||
assert_eq!(plans_over_remote_regions(&state.clone()), expected);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn native_raw_region_read_budget_needs_the_native_topology() {
|
||||
let schema = Arc::new(datafusion::arrow::datatypes::Schema::new(vec![
|
||||
|
||||
@@ -101,6 +101,8 @@ pub struct QueryEngineState {
|
||||
extension_rules: Vec<Arc<dyn ExtensionAnalyzerRule + Send + Sync>>,
|
||||
plugins: Plugins,
|
||||
experimental_native_promql: bool,
|
||||
/// Whether this engine plans distributed statements.
|
||||
with_dist_planner: bool,
|
||||
/// Whether this engine's regions are reached in process, which is what lets a region take a
|
||||
/// `LogicalPlan` instead of Substrait bytes.
|
||||
in_process_region_transport: bool,
|
||||
@@ -338,6 +340,7 @@ impl QueryEngineState {
|
||||
extension_rules,
|
||||
plugins,
|
||||
experimental_native_promql: options.experimental_native_promql,
|
||||
with_dist_planner,
|
||||
in_process_region_transport,
|
||||
scalar_functions: Arc::new(RwLock::new(HashMap::new())),
|
||||
})
|
||||
@@ -491,6 +494,11 @@ impl QueryEngineState {
|
||||
self.experimental_native_promql
|
||||
}
|
||||
|
||||
/// Whether this engine plans distributed statements.
|
||||
pub(crate) fn with_dist_planner(&self) -> bool {
|
||||
self.with_dist_planner
|
||||
}
|
||||
|
||||
/// Whether a region read is served in this process, so a region can take a `LogicalPlan`.
|
||||
pub(crate) fn in_process_region_transport(&self) -> bool {
|
||||
self.in_process_region_transport
|
||||
|
||||
Reference in New Issue
Block a user