feat(promql): execute independent selector scans in fused binary trees

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
This commit is contained in:
discord9
2026-10-25 20:10:32 +08:00
parent 222b1916ec
commit 8dbf08d252
9 changed files with 1199 additions and 588 deletions
+146 -137
View File
@@ -93,9 +93,9 @@ const POLL_BUDGET: usize = 64;
/// The statement grid and output naming that travel with a compiled tree.
///
/// Source schemas belong to the tree's input nodes. This binding supplies the grid that addresses
/// evaluated chunks and the names of their output columns; the logical node checks that the root
/// produces values on that grid. Raw frames carry each series' labels and timestamps, rather than
/// reading them from these output names.
/// evaluated chunks, the time column and the full raw tag names needed by the scanner. Binary
/// matching may reduce the output tags. The logical node checks that the root produces values on
/// that grid; frames carry each series' actual labels and timestamps.
#[derive(Clone, Debug)]
pub struct FusedSeriesBinding {
/// The statement's grid, which is the grid every evaluated chunk is addressed on.
@@ -108,7 +108,7 @@ pub struct FusedSeriesBinding {
pub metric_name: Option<Arc<str>>,
/// The output column holding the grid timestamps.
pub time_index: String,
/// The logical tag names the output carries, read from each series' labels.
/// The full logical tag names used to bind raw scans, before binary output-label projection.
///
/// [`METRIC_NAME`] is special: it is resolved to the series' metric name instead of a column,
/// which is how a reader sees that a range function dropped it.
@@ -225,7 +225,7 @@ impl FusedSeries {
/// The physical node that runs raw frame sources as this node's inputs.
///
/// Each tuple binds a frame producer to its label source and ordering. Building the node
/// rejects unsupported multi-source physical execution, an incompatible frame, or an output
/// rejects incompatible frames or an output
/// schema the conversion cannot classify.
pub fn to_execution_plan(
&self,
@@ -484,19 +484,13 @@ pub(crate) fn tree_source_fields(tree: &NativeTree) -> DataFusionResult<Vec<Vec<
.collect())
}
fn validate_physical_tree(tree: &NativeTree) -> DataFusionResult<Arc<SeriesSchema>> {
let inputs = tree_inputs(tree);
if inputs.is_empty() || inputs.iter().any(|input| input.source_index != 0) {
fn validate_physical_tree(tree: &NativeTree) -> DataFusionResult<Vec<Arc<SeriesSchema>>> {
let schemas = collect_source_schemas(tree)?;
if schemas.is_empty() {
return Err(DataFusionError::Plan(
"FusedSeries physical plan requires one raw source index 0".into(),
"FusedSeries physical plan requires at least one raw source".into(),
));
}
let schemas = collect_source_schemas(tree)?;
let [schema] = schemas.as_slice() else {
return Err(DataFusionError::Plan(
"FusedSeries physical plan requires one raw source".into(),
));
};
if !tree
.root()
.output
@@ -507,7 +501,7 @@ fn validate_physical_tree(tree: &NativeTree) -> DataFusionResult<Arc<SeriesSchem
"FusedSeries output is not evaluated samples".into(),
));
}
Ok(Arc::clone(schema))
Ok(schemas)
}
/// Where one output column of [`FusedSeriesExec`] comes from.
@@ -662,13 +656,10 @@ pub struct FusedSeriesExec {
promql: Arc<str>,
/// The tree this node runs, shared by every partition's execution.
tree: Arc<NativeTree>,
/// The raw series schema the tree's input was compiled against, which every frame is decoded
/// against.
series_schema: Arc<SeriesSchema>,
/// How the input frames name their series: labels encoded in the key, or carried along.
label_source: RawLabelSource,
/// The ordering the input frames guarantee.
ordering: SeriesOrdering,
/// Each source's compiled raw schema, used to decode only that source's frames.
series_schemas: Vec<Arc<SeriesSchema>>,
/// Physical input plans, preserving each source's frame binding and ordering.
inputs: Vec<(Arc<dyn ExecutionPlan>, RawLabelSource, SeriesOrdering)>,
/// Where each output column comes from.
classification: Arc<[OutputColumn]>,
/// The output column holding the grid timestamps, kept so this node can be rebuilt over a new
@@ -676,8 +667,6 @@ pub struct FusedSeriesExec {
time_column: String,
/// The tag column names, indexed by [`OutputColumn::Tag`].
tags: Arc<[String]>,
/// The plan producing the raw series frames.
input: Arc<dyn ExecutionPlan>,
/// The columns this node produces.
output_schema: SchemaRef,
/// Per-node metrics, registered by each execution of the tree.
@@ -706,91 +695,81 @@ impl FusedSeriesExec {
time_column: String,
tags: Vec<String>,
) -> DataFusionResult<Self> {
let [(input, label_source, ordering)] = inputs.as_slice() else {
let raw_schemas = validate_physical_tree(&tree)?;
if inputs.len() != raw_schemas.len() || inputs.is_empty() {
return Err(DataFusionError::Plan(format!(
"FusedSeriesExec requires exactly one physical source, got {}",
inputs.len()
"FusedSeries physical input count {} does not match its {} raw sources",
inputs.len(),
raw_schemas.len()
)));
};
let input = Arc::clone(input);
let label_source = label_source.clone();
let ordering = *ordering;
let raw_schema = validate_physical_tree(&tree)?;
// Building a state is what proves the runtime executes this tree: a node kind it does not
// run yet is refused here, so a plan that is built is a plan that starts. The throwaway
// state is dropped at once, returning its (unbounded) reservations.
}
// Building a state proves the runtime executes this tree; drop the validation state now.
let _ = NativeExecutionState::try_new(&tree, Arc::new(UnboundedMemoryPool::default()))?;
let output_series = tree
.root()
.output
.samples()
.ok_or_else(|| DataFusionError::Plan("FusedSeries output is not samples".into()))?;
let classification = classify_output(&output_schema, output_series, &tags, &time_column)?;
let input_properties = input.properties();
let partition_count = input_properties.partitioning.partition_count();
if partition_count == 0 {
return Err(DataFusionError::Plan(
"FusedSeriesExec input has no partitions".into(),
));
}
if tree_inputs(&tree)
.iter()
.any(|input| !ordering_satisfies(ordering, input.ordering))
let mut output_tags = tags.clone();
if let NativeExpr::Binary { matching, .. } = &tree.root().expr
&& matching.cardinality == crate::vector_match::Cardinality::OneToOne
{
return Err(DataFusionError::Plan(
"FusedSeries producer ordering does not satisfy every input".into(),
));
match &matching.labels {
crate::vector_match::MatchingLabels::On(labels) => {
output_tags.retain(|tag| labels.iter().any(|label| label.as_ref() == tag));
}
crate::vector_match::MatchingLabels::Ignoring(labels) => {
output_tags.retain(|tag| !labels.iter().any(|label| label.as_ref() == tag));
}
}
}
let classification =
classify_output(&output_schema, output_series, &output_tags, &time_column)?;
let partition_count = inputs[0].0.properties().partitioning.partition_count();
for (index, (input, label_source, ordering)) in inputs.iter().enumerate() {
let count = input.properties().partitioning.partition_count();
if count == 0 {
return Err(DataFusionError::Plan(format!(
"FusedSeries input {index} has no partitions"
)));
}
let required_ordering = if inputs.len() > 1 && count > 1 {
SeriesOrdering::Contiguous
} else {
*ordering
};
if tree_inputs(&tree)
.iter()
.filter(|node| node.source_index == index)
.any(|node| !ordering_satisfies(required_ordering, node.ordering))
{
return Err(DataFusionError::Plan(format!(
"FusedSeries producer ordering for source {index} does not satisfy every input"
)));
}
ensure_frame_matches_label_source(input.schema().as_ref(), label_source)?;
unpack_raw_series(
Box::pin(RecordBatchStreamAdapter::new(
input.schema(),
futures::stream::empty(),
)),
Some(raw_schemas[index].clone()),
)?;
}
// The frame states its own label layout and series ordering; this plan's binding is an
// expectation checked against that declaration, not an input to decoding.
ensure_frame_matches_label_source(input.schema().as_ref(), &label_source)?;
// The frame is checked against what the tree's input reads, so a frame this node cannot
// decode is reported when the plan is built. The shared decoder is the authority on that
// layout, so it is asked rather than copied: handing it an empty stream of this schema makes
// it validate the declared frame with no batch to decode, which is the check
// `unpack_raw_series` makes before its first poll too.
unpack_raw_series(
Box::pin(RecordBatchStreamAdapter::new(
input.schema(),
futures::stream::empty(),
)),
Some(raw_schema.clone()),
)?;
// One evaluator per input partition, and the input's own partitioning kept as it is.
//
// A window holds the state of the series it is reading and closes it on the chunk carrying
// `is_last`, so it can answer a series only if the whole series arrives in the partition it
// reads. The input guarantees that: a raw series frame is produced by `SeriesScan`, whose
// contract is that the same primary key always reaches the same partition, and the frame
// decoder holds each series' chunks contiguous within that partition.
//
// What is *not* asked of the optimizer is a distribution. A `KeyPartitioned` or
// `HashPartitioned` requirement is classed as a hash requirement, and a hash requirement
// makes a hash exchange necessary whenever the child has more than one partition - even if
// the child already satisfies it, and even when the partition counts match. A hash exchange
// does not preserve order, so it would interleave the chunks of one series and the decoder
// would close a series it has not finished. `UnspecifiedDistribution` leaves the child's
// layout alone, and `benefits_from_input_partitioning` is false so no round-robin is added
// to raise parallelism either. Between them no exchange can appear between the scan and this
// node, and the scan's guarantee arrives intact.
// As many partitions as the input hands over: the node runs one evaluator per partition and
// answers each as it settles.
let properties = Arc::new(PlanProperties::new(
EquivalenceProperties::new(output_schema.clone()),
Partitioning::UnknownPartitioning(partition_count),
input_properties.emission_type,
input_properties.boundedness,
Partitioning::UnknownPartitioning(if inputs.len() > 1 { 1 } else { partition_count }),
inputs[0].0.properties().emission_type,
inputs[0].0.properties().boundedness,
));
Ok(Self {
promql,
tree,
series_schema: raw_schema,
label_source,
ordering,
series_schemas: raw_schemas,
inputs,
classification: classification.into(),
time_column,
tags: tags.into(),
input,
tags: output_tags.into(),
output_schema,
metric: ExecutionPlanMetricsSet::new(),
properties,
@@ -834,18 +813,23 @@ impl ExecutionPlan for FusedSeriesExec {
/// the scan it reads from guarantees a series stays in one partition, so the requirement says
/// only that the layout is left alone.
fn input_distribution_requirements(&self) -> InputDistributionRequirements {
InputDistributionRequirements::new(vec![Distribution::UnspecifiedDistribution])
InputDistributionRequirements::new(
self.inputs
.iter()
.map(|(_, _, _)| Distribution::UnspecifiedDistribution)
.collect(),
)
}
/// Splitting the input further would not help: a second partition is a second set of series
/// answered by a second tree, which this node already runs, and raising the count is what an
/// optimizer does by adding a round-robin exchange that reorders nothing it can rely on.
fn benefits_from_input_partitioning(&self) -> Vec<bool> {
vec![false]
vec![false; self.inputs.len()]
}
fn children(&self) -> Vec<&Arc<dyn ExecutionPlan>> {
vec![&self.input]
self.inputs.iter().map(|(input, _, _)| input).collect()
}
fn apply_expressions(
@@ -860,17 +844,23 @@ impl ExecutionPlan for FusedSeriesExec {
self: Arc<Self>,
children: Vec<Arc<dyn ExecutionPlan>>,
) -> DataFusionResult<Arc<dyn ExecutionPlan>> {
let [child] = <[Arc<dyn ExecutionPlan>; 1]>::try_from(children).map_err(|children| {
DataFusionError::Plan(format!(
"FusedSeriesExec requires exactly one child, got {}",
if children.len() != self.inputs.len() {
return Err(DataFusionError::Plan(format!(
"FusedSeriesExec requires {} children, got {}",
self.inputs.len(),
children.len()
))
})?;
)));
}
let children = children
.into_iter()
.zip(&self.inputs)
.map(|(child, (_, label_source, ordering))| (child, label_source.clone(), *ordering))
.collect();
// Rebuilt rather than patched: the new child's schema is what the frame is validated
// against, so a child that changed shape is refused instead of decoded with a stale layout.
Ok(Arc::new(Self::try_new(
Arc::clone(&self.promql),
vec![(child, self.label_source.clone(), self.ordering)],
children,
Arc::clone(&self.tree),
self.output_schema.clone(),
self.time_column.clone(),
@@ -883,23 +873,45 @@ impl ExecutionPlan for FusedSeriesExec {
partition: usize,
context: Arc<TaskContext>,
) -> DataFusionResult<SendableRecordBatchStream> {
let input = self.input.execute(partition, Arc::clone(&context))?;
// Batches become raw series chunks through the shared frame decoder, which is the inverse of
// what the producer packed: one chunk per frame row, each series closed exactly once by the
// row carrying `is_last`.
let input = unpack_raw_series(input, Some(Arc::clone(&self.series_schema)))?;
// A source bound in key mode carries its identity encoded and no decoded labels, and a chunk
// handed to the tree has to name the series it holds.
let input =
materialize_raw_labels(input).map_err(|e| DataFusionError::External(Box::new(e)))?;
if self.inputs.len() > 1 && partition != 0 {
return Err(DataFusionError::Execution(
"multi-source FusedSeries only has output partition 0".into(),
));
}
let state = NativeExecutionState::try_new(
&self.tree,
Arc::clone(&context.runtime_env().memory_pool),
)?;
state.register_metrics(&self.metric, partition);
let input_ids = state.input_ids(0).collect();
let mut streams = VecDeque::new();
for (source, (input, _, _)) in self.inputs.iter().enumerate() {
let count = input.properties().partitioning.partition_count();
if self.inputs.len() == 1 && partition >= count {
return Err(DataFusionError::Execution(format!(
"FusedSeries input {source} has no partition {partition}"
)));
}
let start = if self.inputs.len() > 1 { 0 } else { partition };
let end = if self.inputs.len() > 1 {
count
} else {
partition + 1
};
let partitions = start..end;
for child_partition in partitions {
let frames = input.execute(child_partition, Arc::clone(&context))?;
let raw =
unpack_raw_series(frames, Some(Arc::clone(&self.series_schemas[source])))?;
let raw = materialize_raw_labels(raw)
.map_err(|error| DataFusionError::External(Box::new(error)))?;
streams.push_back((source, raw));
}
}
let input_ids = (0..self.series_schemas.len())
.map(|source| state.input_ids(source).collect())
.collect();
Ok(Box::pin(FusedSeriesStream {
input: Some(input),
inputs: streams,
executor: Some(NativeExecutor::new(state)),
input_ids,
classification: Arc::clone(&self.classification),
@@ -1007,16 +1019,15 @@ fn field_column(samples: &Samples) -> DataFusionResult<ArrayRef> {
/// The batches one execution of [`FusedSeriesExec`] yields.
pub struct FusedSeriesStream {
/// The input frames, read as raw series chunks, or `None` once the stream ended in an error.
/// Source-indexed partition streams, each decoded independently and consumed to EOF in order.
///
/// The decoder behind it holds its current batch, the chunks it decoded from it and its own
/// source, so a terminated stream that kept it alive would hold upstream resources until the
/// consumer drops the stream.
input: Option<SendableRawSeriesStream>,
/// Each decoder holds its current batch, decoded chunks and upstream source. Clearing this
/// queue on error releases them even if the consumer keeps the terminated stream.
inputs: VecDeque<(usize, SendableRawSeriesStream)>,
/// The execution of this node's tree, or `None` once it has been closed.
executor: Option<NativeExecutor>,
/// Runtime inputs bound to the sole physical child.
input_ids: Vec<InputId>,
/// Runtime input IDs for each source index.
input_ids: Vec<Vec<InputId>>,
/// Where each output column comes from.
classification: Arc<[OutputColumn]>,
/// The tag column names, indexed by `OutputColumn::Tag`.
@@ -1031,13 +1042,17 @@ pub struct FusedSeriesStream {
impl FusedSeriesStream {
/// Hands one raw chunk over and queues what it settled.
fn push(&mut self, chunk: RawSeriesChunk) -> DataFusionResult<()> {
fn push(&mut self, source: usize, chunk: RawSeriesChunk) -> DataFusionResult<()> {
let Some(executor) = self.executor.as_mut() else {
return Err(DataFusionError::Execution(
"FusedSeriesExec stream pushed a chunk after its execution closed".to_string(),
));
};
for input_id in &self.input_ids {
for input_id in self.input_ids.get(source).ok_or_else(|| {
DataFusionError::Execution(format!(
"FusedSeries has no runtime input IDs for source {source}"
))
})? {
executor.push_raw(*input_id, chunk.clone())?;
}
let mut settled = Vec::new();
@@ -1090,7 +1105,7 @@ impl FusedSeriesStream {
fn fail(&mut self) {
self.done = true;
self.executor = None;
self.input = None;
self.inputs.clear();
// A batch queued before the error was settled by a run that has now stopped.
self.ready.clear();
}
@@ -1133,11 +1148,7 @@ impl Stream for FusedSeriesStream {
if this.done {
return Poll::Ready(None);
}
// An error is the only thing that clears the input, and it ends the stream with it.
let Some(input) = this.input.as_mut() else {
// The input ended, so what the tree answers now comes from what it held. One chunk
// per iteration keeps it a stream, and `None` is both "nothing more" and the point
// where the execution releases its reservations.
let Some((source, input)) = this.inputs.front_mut() else {
match this.pull_one() {
Ok(Some(chunk)) => {
if let Err(error) = this.convert(vec![chunk]) {
@@ -1156,26 +1167,24 @@ impl Stream for FusedSeriesStream {
}
continue;
};
let polled = input.as_mut().poll_next(cx);
let (source, polled) = (*source, input.as_mut().poll_next(cx));
match polled {
Poll::Pending => return Poll::Pending,
Poll::Ready(None) => {
// The input ended, so every series it opened has to be closed. The tree settles
// what it was holding, and the answers are pulled below in this same poll
// rather than collected first.
this.input = None;
if let Err(error) = this.close_input() {
this.fail();
return Poll::Ready(Some(Err(error)));
this.inputs.pop_front();
if this.inputs.is_empty() {
if let Err(error) = this.close_input() {
this.fail();
return Poll::Ready(Some(Err(error)));
}
}
}
Poll::Ready(Some(Err(error))) => {
// An input error stops the execution: a stopped evaluator must not be reused.
this.fail();
return Poll::Ready(Some(Err(DataFusionError::External(Box::new(error)))));
}
Poll::Ready(Some(Ok(chunk))) => {
if let Err(error) = this.push(chunk) {
if let Err(error) = this.push(source, chunk) {
this.fail();
return Poll::Ready(Some(Err(error)));
}
@@ -138,6 +138,14 @@ fn two_field_schema() -> Arc<SeriesSchema> {
})
}
/// A one-field raw schema with a distinct name for independent-source decoding tests.
fn other_field_schema() -> Arc<SeriesSchema> {
Arc::new(SeriesSchema {
fields: Arc::from([FieldSpec { name: OTHER.into() }]),
eval_grid: None,
})
}
/// The tree `rate(x[5m])` compiles to over `schema`: raw samples folded by one window onto the grid.
fn rate_tree_of(schema: Arc<SeriesSchema>) -> NativeTree {
let grid = grid();
@@ -189,11 +197,9 @@ fn binary_tree(left_schema: Arc<SeriesSchema>, right_schema: Arc<SeriesSchema>)
/// A binary tree over `op` whose operands are bound to the given source indexes.
///
/// `left_schema` is the operand at `root.left` and `right_schema` the one at `root.right`, so a
/// subtracting tree answers `left - right`. One source read at both operands is what a statement
/// that names one selector twice plans to; two different indexes are what a statement whose
/// operands are two scans plans to, which the runtime refuses for now. The tree is the same shape
/// either way, and the renderer names the index each input reads, so the trees are distinct
/// computations.
/// subtracting tree answers `left - right`. Equal indexes model two leaves sharing one source;
/// distinct dense indexes model independent physical sources. The renderer names each input index,
/// so trees with otherwise identical operands remain distinct computations.
fn binary_tree_over_sources(
op: BinaryOp,
left_schema: Arc<SeriesSchema>,
@@ -494,6 +500,19 @@ fn output_schema() -> SchemaRef {
]))
}
/// A one-to-one `on(host)` binary output schema.
fn binary_output_schema() -> SchemaRef {
Arc::new(Schema::new(vec![
Field::new(VALUE, DataType::Float64, true),
Field::new(HOST, DataType::Utf8, true),
Field::new(
TIME,
DataType::Timestamp(TimeUnit::Millisecond, None),
false,
),
]))
}
/// The output of [`zone_node`]: the field, `host`, `zone`, the dropped name and the grid times.
fn zone_output_schema() -> SchemaRef {
Arc::new(Schema::new(vec![
@@ -838,12 +857,17 @@ impl Drop for StagedStream {
fn fused(tree: NativeTree) -> FusedSeries {
let binding = binding(&tree);
let output_schema = if matches!(&tree.root().expr, NativeExpr::Binary { .. }) {
binary_output_schema()
} else {
output_schema()
};
FusedSeries::try_new(
Arc::from(PROMQL),
tree,
binding,
vec![input_plan(frame_schema())],
output_schema().to_dfschema_ref().unwrap(),
output_schema.to_dfschema_ref().unwrap(),
)
.expect("the binding matches the tree")
}
@@ -958,6 +982,20 @@ fn frames_of(schema: &SeriesSchema, chunks: &[RawSeriesChunk]) -> Vec<RecordBatc
chunks.iter().map(|chunk| frame_of(schema, chunk)).collect()
}
/// One frame with a different raw field layout, used to verify source-local decoding.
fn other_field_chunk(host: &str, value: f64) -> RecordBatch {
let schema = other_field_schema();
let chunk = chunk_of(
&schema,
host,
metric(&[(HOST, host)], Some(METRIC)),
&[GRID_START],
vec![vec![Some(value)]],
true,
);
frame_of(&schema, &chunk)
}
/// The frames carrying `chunks` of the one-field schema of these tests, one batch each.
fn frames(chunks: &[RawSeriesChunk]) -> Vec<RecordBatch> {
frames_of(&raw_schema(), chunks)
@@ -996,6 +1034,23 @@ fn one_series_frames() -> Vec<RecordBatch> {
])
}
fn binary_values(batches: &[RecordBatch]) -> Vec<f64> {
batches
.iter()
.flat_map(|batch| {
let values = batch
.column_by_name(VALUE)
.unwrap()
.as_any()
.downcast_ref::<Float64Array>()
.unwrap();
(0..values.len())
.filter_map(|row| values.is_valid(row).then(|| values.value(row)))
.collect::<Vec<_>>()
})
.collect()
}
/// The rows of `batches`: timestamp, value, host and metric name.
type Row = (i64, Option<f64>, Option<String>, Option<String>);
@@ -1572,26 +1627,25 @@ fn repeated_source_bindings_validate_fields_and_second_ordering() {
assert!(error.to_string().contains("ordering"), "{error}");
}
/// Physical trees may have logical sources 0 and 1, but their physical adapter remains one-source.
#[test]
fn logical_two_source_tree_is_rejected_by_physical_adapter() {
/// Logical trees with distinct dense sources are accepted by the physical adapter.
#[tokio::test]
async fn logical_two_source_tree_is_accepted_by_physical_adapter() {
let tree = binary_tree_over_sources(BinaryOp::Add, raw_schema(), raw_schema(), 0, 1);
let node = FusedSeries::try_new(
Arc::from("left + right"),
tree.clone(),
binding(&tree),
vec![input_plan(frame_schema()), input_plan(frame_schema())],
output_schema().to_dfschema_ref().unwrap(),
binary_output_schema().to_dfschema_ref().unwrap(),
)
.expect("the logical node accepts two dense sources");
let calls = Arc::new((0..2).map(|_| AtomicUsize::new(0)).collect::<Vec<_>>());
let source = || {
Arc::new(CountedExec {
input: memory_exec(Vec::new()),
calls: Arc::clone(&calls),
calls: Arc::new((0..1).map(|_| AtomicUsize::new(0)).collect()),
}) as Arc<dyn ExecutionPlan>
};
let error = node
let plan = node
.to_execution_plan(vec![
(
source(),
@@ -1604,20 +1658,247 @@ fn logical_two_source_tree_is_rejected_by_physical_adapter() {
SeriesOrdering::Contiguous,
),
])
.expect_err("the physical boundary refuses the two-source runtime");
assert!(error.to_string().contains("source") || error.to_string().contains("input"));
.expect("the physical boundary accepts independent source bindings");
assert_eq!(2, plan.children().len());
let results = collect(Arc::clone(&plan), SessionContext::default().task_ctx())
.await
.unwrap();
assert!(rows(&results).is_empty());
for child in plan.children() {
let counted = child
.downcast_ref::<CountedExec>()
.expect("child preserves its counted execution plan");
assert_eq!(1, counted.calls[0].load(Ordering::SeqCst));
}
}
/// Independent sources execute in source order and gather all child partitions, without zipping.
#[tokio::test]
async fn independent_sources_execute_all_partitions_sequentially() {
let tree = binary_tree_over_sources(BinaryOp::Sub, raw_schema(), other_field_schema(), 0, 1);
let left_calls = Arc::new((0..2).map(|_| AtomicUsize::new(0)).collect());
let right_calls = Arc::new((0..3).map(|_| AtomicUsize::new(0)).collect());
let left = Arc::new(CountedExec {
input: memory_exec_partitioned(vec![
vec![frame(&series_chunk("web-2", &[(GRID_START, 8.0)], true))],
vec![frame(&series_chunk("web-1", &[(GRID_START, 9.0)], true))],
]),
calls: Arc::clone(&left_calls),
});
let right = Arc::new(CountedExec {
input: memory_exec_partitioned_with(
frame_schema_of(&other_field_schema()),
vec![
vec![other_field_chunk("web-2", 3.0)],
vec![other_field_chunk("web-1", 2.0)],
vec![other_field_chunk("web-3", 4.0)],
],
),
calls: Arc::clone(&right_calls),
});
let node = FusedSeries::try_new(
Arc::from("left - right"),
tree.clone(),
binding(&tree),
vec![
input_plan(frame_schema()),
input_plan(frame_schema_of(&other_field_schema())),
],
binary_output_schema().to_dfschema_ref().unwrap(),
)
.unwrap();
let exec = node
.to_execution_plan(vec![
(left, RawLabelSource::Explicit, SeriesOrdering::Contiguous),
(right, RawLabelSource::Explicit, SeriesOrdering::Contiguous),
])
.unwrap();
assert_eq!(1, exec.properties().output_partitioning().partition_count());
let results = collect(Arc::clone(&exec), SessionContext::default().task_ctx())
.await
.unwrap();
let mut actual = binary_values(&results);
actual.sort_by(f64::total_cmp);
assert_eq!(vec![5.0, 7.0], actual);
assert_eq!(
vec![0, 0],
calls
vec![1, 1],
left_calls
.iter()
.map(|call| call.load(Ordering::SeqCst))
.map(|c| c.load(Ordering::SeqCst))
.collect::<Vec<_>>()
);
assert_eq!(
vec![1, 1, 1],
right_calls
.iter()
.map(|c| c.load(Ordering::SeqCst))
.collect::<Vec<_>>()
);
}
/// The physical boundary accepts one source only and rejects zero or multiple sources early.
/// On-label matching projects binary output tags while retaining all raw tags for frame decoding.
#[tokio::test]
async fn binary_output_tags_are_projected_but_raw_binding_is_full() {
let tree = binary_tree_over_sources(BinaryOp::Sub, raw_schema(), raw_schema(), 0, 1);
let node = FusedSeries::try_new(
Arc::from("left - right"),
tree.clone(),
binding(&tree),
vec![input_plan(frame_schema()), input_plan(frame_schema())],
binary_output_schema().to_dfschema_ref().unwrap(),
)
.unwrap();
let left = memory_exec(vec![frame(&chunk_of(
&raw_schema(),
"web-1",
metric(&[(HOST, "web-1"), (ZONE, "west")], Some(METRIC)),
&[GRID_START],
vec![vec![Some(9.0)]],
true,
))]);
let right = memory_exec(vec![frame(&chunk_of(
&raw_schema(),
"web-1",
metric(&[(HOST, "web-1"), (ZONE, "east")], Some(METRIC)),
&[GRID_START],
vec![vec![Some(2.0)]],
true,
))]);
let exec = node
.to_execution_plan(vec![
(left, RawLabelSource::Explicit, SeriesOrdering::Contiguous),
(right, RawLabelSource::Explicit, SeriesOrdering::Contiguous),
])
.unwrap();
let results = collect(exec, SessionContext::default().task_ctx())
.await
.unwrap();
assert_eq!(vec![7.0], binary_values(&results));
assert!(
results
.iter()
.all(|batch| batch.column_by_name(ZONE).is_none())
);
}
/// Empty earlier sources do not stop later sources, and one-to-one matching can yield no rows.
#[tokio::test]
async fn empty_first_source_does_not_end_gather_early() {
let tree = binary_tree_over_sources(BinaryOp::Sub, raw_schema(), raw_schema(), 0, 1);
let node = FusedSeries::try_new(
Arc::from("left - right"),
tree.clone(),
binding(&tree),
vec![input_plan(frame_schema()), input_plan(frame_schema())],
binary_output_schema().to_dfschema_ref().unwrap(),
)
.unwrap();
let second = memory_exec(vec![frame(&series_chunk(
"web-1",
&[(GRID_START, 2.0)],
true,
))]);
let exec = node
.to_execution_plan(vec![
(
memory_exec(Vec::new()),
RawLabelSource::Explicit,
SeriesOrdering::Contiguous,
),
(second, RawLabelSource::Explicit, SeriesOrdering::Contiguous),
])
.unwrap();
let results = collect(exec, SessionContext::default().task_ctx())
.await
.unwrap();
assert!(rows(&results).is_empty());
}
/// A multiparty gather requires Contiguous input, not global KeyThenTime ordering.
#[test]
fn physical_plan_requires_exactly_one_source() {
fn multiparty_gather_rejects_key_then_time_leaf_requirement() {
let tree = declaring_orderings(
&binary_tree_over_sources(BinaryOp::Add, raw_schema(), raw_schema(), 0, 1),
&[SeriesOrdering::KeyThenTime, SeriesOrdering::Contiguous],
);
let node = FusedSeries::try_new(
Arc::from("left + right"),
tree.clone(),
binding(&tree),
vec![input_plan(frame_schema()), input_plan(frame_schema())],
binary_output_schema().to_dfschema_ref().unwrap(),
)
.unwrap();
let error = node
.to_execution_plan(vec![
(
memory_exec_partitioned(vec![vec![], vec![]]),
RawLabelSource::Explicit,
SeriesOrdering::KeyThenTime,
),
(
memory_exec_partitioned(vec![vec![], vec![]]),
RawLabelSource::Explicit,
SeriesOrdering::Contiguous,
),
])
.expect_err("concatenated partitions are not globally KeyThenTime");
assert!(error.to_string().contains("ordering"), "{error}");
}
/// A later source failure drops streams already opened for earlier sources and releases state.
#[tokio::test]
async fn later_source_error_drops_all_gathered_inputs() {
let tree = binary_tree_over_sources(BinaryOp::Sub, raw_schema(), raw_schema(), 0, 1);
let node = FusedSeries::try_new(
Arc::from("left - right"),
tree.clone(),
binding(&tree),
vec![input_plan(frame_schema()), input_plan(frame_schema())],
binary_output_schema().to_dfschema_ref().unwrap(),
)
.unwrap();
let dropped = [
Arc::new(AtomicBool::new(false)),
Arc::new(AtomicBool::new(false)),
];
let left_schema = frame_schema();
let left_drop = Arc::clone(&dropped[0]);
let left = staged(left_schema.clone(), move || {
Box::pin(
StagedStream::new(
left_schema.clone(),
vec![frame(&series_chunk("web-1", &[(GRID_START, 9.0)], true))],
)
.dropped(&left_drop),
)
});
let right_schema = frame_schema();
let right_drop = Arc::clone(&dropped[1]);
let right = staged(right_schema.clone(), move || {
Box::pin(
StagedStream::new(right_schema.clone(), Vec::new())
.error("later source failed")
.dropped(&right_drop),
)
});
let exec = node
.to_execution_plan(vec![
(left, RawLabelSource::Explicit, SeriesOrdering::Contiguous),
(right, RawLabelSource::Explicit, SeriesOrdering::Contiguous),
])
.unwrap();
let pool = Arc::new(GreedyMemoryPool::new(1 << 20));
let mut stream = exec.execute(0, task_context(&pool)).unwrap();
let error = stream.next().await.unwrap().unwrap_err();
assert!(error.to_string().contains("later source failed"));
assert!(dropped.iter().all(|flag| flag.load(Ordering::SeqCst)));
assert_eq!(0, pool.reserved());
}
/// The physical boundary rejects zero sources and mismatched physical source counts.
#[test]
fn physical_plan_requires_matching_source_count() {
let node = fused(rate_tree());
let zero = node
.to_execution_plan(Vec::new())
@@ -1634,7 +1915,7 @@ fn physical_plan_requires_exactly_one_source() {
};
let many = node
.to_execution_plan(vec![tuple(), tuple()])
.expect_err("physical execution cannot run with multiple sources");
.expect_err("physical input count differs from the tree's source count");
assert!(many.to_string().contains("source") || many.to_string().contains("input"));
}
@@ -1824,7 +2105,7 @@ fn an_input_with_no_partitions_is_rejected_when_the_plan_is_built() {
)));
let error = plan(rate_tree(), empty).expect_err("there is no partition to run");
assert!(
error.to_string().contains("input has no partitions"),
error.to_string().contains("input 0 has no partitions"),
"refused for another reason: {error}"
);
// The only count refused is zero: two partitions build and answer, which is what the
@@ -2088,10 +2369,10 @@ async fn repeated_source_runs_once_per_partition_with_ordered_operands() {
})
.collect::<Vec<_>>();
let input = memory_exec_partitioned(partitions);
let calls = Arc::new((0..4).map(|_| AtomicUsize::new(0)).collect::<Vec<_>>());
let calls_by_partition = Arc::new((0..4).map(|_| AtomicUsize::new(0)).collect::<Vec<_>>());
let counted = Arc::new(CountedExec {
input,
calls: Arc::clone(&calls),
calls: Arc::clone(&calls_by_partition),
});
let exec = plan(subtracted_scaled_tree(), counted)
.expect("the repeated source tree runs over one physical child");
@@ -2111,7 +2392,38 @@ async fn repeated_source_runs_once_per_partition_with_ordered_operands() {
.try_collect::<Vec<_>>()
.await
.expect("the partition completes without errors");
let actual = rows(&batches);
let actual = batches
.iter()
.flat_map(|batch| {
let time = batch
.column_by_name(TIME)
.expect("binary output has time")
.as_any()
.downcast_ref::<TimestampMillisecondArray>()
.expect("time output is millisecond timestamp");
let value = batch
.column_by_name(VALUE)
.expect("binary output has value")
.as_any()
.downcast_ref::<Float64Array>()
.expect("value output is Float64");
let host = batch
.column_by_name(HOST)
.expect("binary output retains host")
.as_any()
.downcast_ref::<StringArray>()
.expect("host output is Utf8");
(0..batch.num_rows())
.map(|row| {
(
time.value(row),
value.is_valid(row).then(|| value.value(row)),
host.is_valid(row).then(|| host.value(row).to_string()),
)
})
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
total_rows += actual.len();
let host = format!("web-{partition}");
assert_eq!(GRID_LEN as usize, actual.len(), "{host}: {actual:?}");
@@ -2125,7 +2437,6 @@ async fn repeated_source_runs_once_per_partition_with_ordered_operands() {
.iter()
.all(|row| row.2.as_deref() == Some(host.as_str()))
);
assert!(actual.iter().all(|row| row.3.is_none()));
let expected_values = (0..GRID_LEN as usize)
.map(|index| Some(-((partition * 10 + index + 1) as f64)))
.collect::<Vec<_>>();
@@ -2133,13 +2444,13 @@ async fn repeated_source_runs_once_per_partition_with_ordered_operands() {
expected_values,
actual.iter().map(|row| row.1).collect::<Vec<_>>()
);
assert_eq!(1, calls[partition].load(Ordering::SeqCst));
assert_eq!(1, calls_by_partition[partition].load(Ordering::SeqCst));
assert_eq!(0, pool.reserved(), "{host} released reservations at EOF");
}
assert_eq!(4 * GRID_LEN as usize, total_rows);
assert_eq!(
vec![1, 1, 1, 1],
calls
calls_by_partition
.iter()
.map(|call| call.load(Ordering::SeqCst))
.collect::<Vec<_>>()
+40 -44
View File
@@ -107,14 +107,12 @@ fn plan_fused_series(
"FusedSeries physical child count mismatch".into(),
));
}
if physical_inputs.len() != 1 {
return Err(DataFusionError::Plan(
"FusedSeries currently requires one physical child".into(),
));
}
let Some(scan) = physical_inputs[0].downcast_ref::<RegionScanExec>() else {
if physical_inputs
.iter()
.any(|input| input.downcast_ref::<RegionScanExec>().is_none())
{
return Ok(None);
};
}
let binding = fused.binding();
let Some(metric_name) = binding.metric_name.as_deref() else {
@@ -133,43 +131,41 @@ fn plan_fused_series(
.cloned()
.collect::<Vec<_>>();
let source_fields = super::fused_series::tree_source_fields(fused.tree())?;
let [field_names] = source_fields.as_slice() else {
if source_fields.len() != physical_inputs.len() {
return Err(DataFusionError::Plan(
"FusedSeries physical plan requires one raw source".into(),
"FusedSeries physical plan source count mismatch".into(),
));
};
let field_names = field_names.clone();
let scan = if scan.is_partition_set() {
Arc::new(scan.clone())
} else {
let partitions = table::table::scan::assign_partition_ranges(
scan.get_partition_ranges(),
target_partitions.max(1),
);
Arc::new(
scan.with_new_partitions(partitions, target_partitions.max(1))
.map_err(|error| DataFusionError::External(error.into_inner()))?,
)
};
// The scan resolves the label source and the ordering it will declare itself, against its own
// metadata snapshot, and hands both back through the leaf it builds. Naming them here instead
// would be a second copy of a binding only the scanner can make: the physical metadata and
// column ids a `RawLabelSource::PrimaryKey` carries are not reachable from a region scan's
// public surface, so the layer that resolves them is the scanner.
let Some(series_scan) =
scan.try_to_named_series_scan(metric_name, &logical_tags, &field_names)?
else {
return Err(DataFusionError::Plan(format!(
"the region scan under FusedSeries could not serve a raw series frame for metric \
{metric_name:?}: the scanner refused the raw series path for this scan, so this node \
has no input to read"
)));
};
let label_source = series_scan.label_source().clone();
let ordering = series_scan.raw_series_ordering();
fused
.to_execution_plan(vec![(series_scan, label_source, ordering)])
.map(Some)
}
let mut series_inputs = Vec::with_capacity(physical_inputs.len());
for (physical, field_names) in physical_inputs.iter().zip(source_fields) {
let Some(scan) = physical.downcast_ref::<RegionScanExec>() else {
return Ok(None);
};
let scan = if scan.is_partition_set() {
Arc::new(scan.clone())
} else {
let partitions = table::table::scan::assign_partition_ranges(
scan.get_partition_ranges(),
target_partitions.max(1),
);
Arc::new(
scan.with_new_partitions(partitions, target_partitions.max(1))
.map_err(|error| DataFusionError::External(error.into_inner()))?,
)
};
let Some(series_scan) =
scan.try_to_named_series_scan(metric_name, &logical_tags, &field_names)?
else {
return Err(DataFusionError::Plan(format!(
"the region scan under FusedSeries could not serve a raw series frame for metric \
{metric_name:?}: the scanner refused the raw series path for this scan"
)));
};
series_inputs.push((
series_scan.clone() as Arc<dyn ExecutionPlan>,
series_scan.label_source().clone(),
series_scan.raw_series_ordering(),
));
}
fused.to_execution_plan(series_inputs).map(Some)
}
+36 -11
View File
@@ -56,6 +56,7 @@ use crate::metrics::PUSH_DOWN_FALLBACK_ERRORS_TOTAL;
use crate::optimizer::expr_depth;
use crate::options::ScheduledTimeExtension;
use crate::plan::ExtractExpr;
use crate::promql::planner::native::{independent_scan_pair, same_table_scan};
use crate::query_engine::DefaultSerializer;
#[cfg(test)]
@@ -505,6 +506,24 @@ struct PlanRewriter {
new_child_plan: Option<LogicalPlan>,
}
fn is_admitted_pair_plan(plan: &LogicalPlan) -> bool {
let LogicalPlan::Extension(extension) = plan else {
return false;
};
let Some(fused) = extension.node.as_any().downcast_ref::<FusedSeries>() else {
return false;
};
let children = fused.children();
children.len() == 2
&& children
.iter()
.all(|child| matches!(child, LogicalPlan::TableScan(_)))
&& {
same_table_scan(&children[0], &children[1])
&& independent_scan_pair(fused.tree().root())
}
}
fn is_fused_series_plan(plan: &LogicalPlan) -> bool {
matches!(plan, LogicalPlan::Extension(extension) if extension.node.as_any().is::<FusedSeries>())
}
@@ -824,17 +843,23 @@ impl PlanRewriter {
// if there is a new child plan, use it as the new root
on_node = new_child_plan;
}
let mut rewriter = EnforceDistRequirementRewriter::new(
std::mem::take(&mut self.column_requirements),
self.level,
);
debug!(
"PlanRewriter: enforce column requirements for node: {on_node} with rewriter: {rewriter:?}"
);
on_node = on_node.rewrite(&mut rewriter)?.data;
debug!(
"PlanRewriter: after enforced column requirements with rewriter: {rewriter:?} for node:\n{on_node}"
);
let bypass_multi_input_gate =
self.skip_encodability_gate && is_admitted_pair_plan(&on_node);
if bypass_multi_input_gate {
self.column_requirements.clear();
} else {
let mut rewriter = EnforceDistRequirementRewriter::new(
std::mem::take(&mut self.column_requirements),
self.level,
);
debug!(
"PlanRewriter: enforce column requirements for node: {on_node} with rewriter: {rewriter:?}"
);
on_node = on_node.rewrite(&mut rewriter)?.data;
debug!(
"PlanRewriter: after enforced column requirements with rewriter: {rewriter:?} for node:\n{on_node}"
);
}
debug!(
"PlanRewriter: expand on node: {on_node} with partition col alias mapping: {:?}",
+175 -17
View File
@@ -29,7 +29,7 @@ use store_api::metric_engine_consts::DATA_SCHEMA_TSID_COLUMN_NAME;
use crate::dist_plan::MergeScanLogicalPlan;
use crate::dist_plan::analyzer::AliasMapping;
use crate::dist_plan::merge_sort::{MergeSortLogicalPlan, merge_sort_transformer};
use crate::promql::planner::native::{pointwise_operand, two_occurrences_pair_one_source};
use crate::promql::planner::native::{independent_scan_pair, pointwise_operand, same_table_scan};
pub struct StepTransformAction {
extra_parent_plans: Vec<LogicalPlan>,
@@ -244,11 +244,20 @@ impl Categorizer {
}
name if name == FusedSeries::name() => {
let fused = plan.as_any().downcast_ref::<FusedSeries>().unwrap();
// The raw-series reader can be placed region-side only over one TableScan child.
let [child] = fused.children() else {
return Commutativity::NonCommutative;
};
if !matches!(child, LogicalPlan::TableScan(_)) {
// The raw-series reader supports one scan, or the narrow same-table two-scan
// one-to-one case. This classification is for region-local execution only.
let children = fused.children();
let pair = children.len() == 2
&& children
.iter()
.all(|child| matches!(child, LogicalPlan::TableScan(_)))
&& same_table_scan(&children[0], &children[1])
&& independent_scan_pair(fused.tree().root());
if children.len() == 1 {
if !matches!(children[0], LogicalPlan::TableScan(_)) {
return Commutativity::NonCommutative;
}
} else if !pair {
return Commutativity::NonCommutative;
}
let binding = fused.binding();
@@ -256,20 +265,13 @@ impl Categorizer {
return Commutativity::NonCommutative;
}
let root = fused.tree().root();
let per_series =
pointwise_operand(root) || two_occurrences_pair_one_source(root, &binding.tags);
let per_series = pointwise_operand(root) || independent_scan_pair(root);
if reads_across_series(root) && !per_series {
return Commutativity::NonCommutative;
}
// A region evaluates the whole tree over the series its scan returns, so this is
// only correct while each series reaches exactly one region: a series split
// across regions would be ranged per fragment, and a range over fragments is not
// the range over the series. Native admission
// (`promql::planner::native::try_plan_native_fused`) only builds the node when
// every partition column of the table is one of the binding's tag columns, so
// the partition key is a function of the label set and a series cannot straddle
// regions.
// The two-scan exception describes only in-process, same-table region-local
// execution. It is not a claim that matching is globally commutative across regions.
Commutativity::Commutative
}
name if name == SeriesNormalize::name()
@@ -412,7 +414,7 @@ mod tests {
use super::*;
use crate::dist_plan::DistPlannerAnalyzer;
use crate::optimizer::test_util::mock_table_provider;
use crate::promql::native_compile::{compile, statement_grid};
use crate::promql::native_compile::{compile, compile_selectors, statement_grid};
use crate::query_engine::DefaultSerializer;
#[test]
@@ -470,6 +472,52 @@ mod tests {
fused_series_plan_named(input, Some("some_metric"))
}
fn fused_pair_plan(left: LogicalPlan, right: LogicalPlan) -> LogicalPlan {
let mut stmt = native_statement();
stmt.expr =
parser::parse("some_metric{host=\"left\"} + on(host) some_metric{host=\"right\"}")
.unwrap();
let series_schema = Arc::new(SeriesSchema {
fields: Arc::from([FieldSpec { name: "v0".into() }]),
eval_grid: None,
});
let tree = compile_selectors(
&stmt,
&|_| Ok(Arc::clone(&series_schema)),
SeriesOrdering::Contiguous,
)
.unwrap()
.tree;
let binding = FusedSeriesBinding {
grid: statement_grid(&stmt).unwrap(),
time_index: "ts".to_string(),
tags: vec!["host".to_string()],
metric_name: Some(Arc::from("some_metric")),
};
let output_schema: DFSchemaRef = Arc::new(
DFSchema::new_with_metadata(
vec![(
None,
Arc::new(Field::new("v0", ArrowDataType::Float64, true)),
)],
Default::default(),
)
.unwrap(),
);
LogicalPlan::Extension(Extension {
node: Arc::new(
FusedSeries::try_new(
Arc::from("some_metric{host=\"left\"} + on(host) some_metric{host=\"right\"}"),
tree,
binding,
vec![left, right],
output_schema,
)
.unwrap(),
),
})
}
/// The same node, with the metric name it binds chosen by the caller.
fn fused_series_plan_named(input: LogicalPlan, metric_name: Option<&str>) -> LogicalPlan {
let stmt = native_statement();
@@ -537,6 +585,26 @@ mod tests {
.unwrap()
}
fn table_scan_plan_with_filter(value: &str) -> LogicalPlan {
let LogicalPlan::TableScan(mut scan) = table_scan_plan() else {
unreachable!();
};
scan.filters
.push(datafusion_expr::col("k0").eq(datafusion_expr::lit(value)));
LogicalPlan::TableScan(scan)
}
fn remote_fused_pair(plan: &LogicalPlan) -> Option<&FusedSeries> {
if let Some(merge_scan) = as_merge_scan(plan)
&& let LogicalPlan::Extension(extension) = merge_scan.input()
&& let Some(fused) = extension.node.as_any().downcast_ref::<FusedSeries>()
&& fused.children().len() == 2
{
return Some(fused);
}
plan.inputs().into_iter().find_map(remote_fused_pair)
}
fn holds_fused_series(plan: &LogicalPlan) -> bool {
if let LogicalPlan::Extension(extension) = plan
&& extension.node.as_any().is::<FusedSeries>()
@@ -651,6 +719,10 @@ mod tests {
analyzer.analyze(plan, &ConfigOptions::default()).unwrap()
}
fn analyzed(analyzer: DistPlannerAnalyzer, plan: LogicalPlan) -> DfResult<LogicalPlan> {
analyzer.analyze(plan, &ConfigOptions::default())
}
/// 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;
@@ -752,6 +824,92 @@ mod tests {
assert!(!reads_across_series(single_selector.root()));
}
#[test]
fn same_table_two_scan_fused_pair_is_region_local_commutative() {
let pair = fused_pair_plan(table_scan_plan(), table_scan_plan());
let LogicalPlan::Extension(extension) = pair else {
unreachable!();
};
assert!(matches!(
Categorizer::check_extension_plan(extension.node.as_ref(), &AliasMapping::default()),
Commutativity::Commutative
));
let other_table = LogicalPlanBuilder::scan(
"other_metric",
Arc::new(DefaultTableSource::new(Arc::new(mock_table_provider(
RegionId::new(1, 1),
)))),
None,
)
.unwrap()
.build()
.unwrap();
let pair = fused_pair_plan(table_scan_plan(), other_table);
let LogicalPlan::Extension(extension) = pair else {
unreachable!();
};
assert!(matches!(
Categorizer::check_extension_plan(extension.node.as_ref(), &AliasMapping::default()),
Commutativity::NonCommutative
));
}
#[test]
fn in_process_pair_bypasses_generic_multi_input_requirement_gate_only_for_root_pair() {
let left = table_scan_plan_with_filter("left");
let right = table_scan_plan_with_filter("right");
let pair = fused_pair_plan(left, right);
let result = analyzed(DistPlannerAnalyzer::new(true), pair.clone());
assert!(
result.is_ok(),
"in-process root pair should be placeable: {result:?}"
);
let analyzed_plan = result.unwrap();
let fused = remote_fused_pair(&analyzed_plan).expect("one remote root pair");
assert_eq!(2, fused.children().len());
let remote_scan = first_merge_scan(&analyzed_plan).expect("one merge scan");
let LogicalPlan::Extension(remote_pair) = remote_scan.input() else {
panic!("expected fused remote input: {}", remote_scan.input());
};
let remote_fused = remote_pair
.node
.as_any()
.downcast_ref::<FusedSeries>()
.unwrap();
let LogicalPlan::TableScan(left) = &remote_fused.children()[0] else {
unreachable!()
};
let LogicalPlan::TableScan(right) = &remote_fused.children()[1] else {
unreachable!()
};
assert_ne!(left.filters, right.filters);
let inputs = fused.tree().root();
let NativeExpr::Binary { left, right, .. } = &inputs.expr else {
panic!("expected binary root: {:?}", inputs.expr);
};
let NativeExpr::Instant { input: left, .. } = &left.expr else {
panic!("expected left selector input: {:?}", left.expr);
};
let NativeExpr::Input(left) = &left.expr else {
panic!("expected left source: {:?}", left.expr);
};
let NativeExpr::Instant { input: right, .. } = &right.expr else {
panic!("expected right selector input: {:?}", right.expr);
};
let NativeExpr::Input(right) = &right.expr else {
panic!("expected right source: {:?}", right.expr);
};
assert_eq!((0, 1), (left.source_index, right.source_index));
let not_in_process = analyzed(DistPlannerAnalyzer::new(false), pair);
assert!(
not_in_process.is_err()
|| remote_fused_pair(not_in_process.as_ref().unwrap()).is_none(),
"wire transport must not carry the pair exception"
);
}
/// The encoding gate is what kept the node on the frontend, and it only exists for a plan that
/// has to be encoded.
///
+106 -292
View File
@@ -16,9 +16,10 @@
//!
//! [`PromPlanner::try_plan_native_fused`] offers a small experimental subset to the native
//! compiler: a vector selector (optionally parenthesized), or nested unary `rate`, `increase`,
//! `sum_over_time`, and `abs` calls over a selector. It replaces that expression with one
//! [`FusedSeries`] node over the selector's table scan. Other expression shapes keep the existing
//! planner's route.
//! `sum_over_time`, and `abs` calls over a selector, plus a one-to-one arithmetic match between two
//! such operands from the same ordinary Mito table. It replaces that expression with one
//! [`FusedSeries`] node, retaining a separate scan for each selector occurrence. Other expression
//! shapes keep the existing planner's route.
//!
//! What the node needs from here is three things the existing planner already resolves: the raw
//! series schema the tree's input was compiled against (the selector's selected field columns), the
@@ -41,7 +42,7 @@ use datafusion_expr::Expr;
use datatypes::series::SeriesOrdering;
use datatypes::series::samples::{FieldSpec, SeriesSchema};
use promql::extension_plan::{FusedSeries, FusedSeriesBinding};
use promql::native::plan::{NativeExpr, NativeNode, NativeTree, reads_across_series};
use promql::native::plan::{NativeExpr, NativeNode, reads_across_series};
use promql::vector_match::{Cardinality, MatchingLabels};
use promql_parser::label::Matchers;
use promql_parser::parser::value::ValueType;
@@ -51,7 +52,6 @@ use promql_parser::parser::{
use session::context::QueryContextRef;
use snafu::ResultExt;
use store_api::metric_engine_consts::{DATA_SCHEMA_TABLE_ID_COLUMN_NAME, METRIC_ENGINE_NAME};
use table::metadata::TableMeta;
use crate::promql::error::{DataFusionPlanningSnafu, Result};
use crate::promql::native_compile::{compile_selectors, statement_grid};
@@ -93,22 +93,7 @@ pub(crate) fn native_promql_requested(query_ctx: &QueryContextRef) -> bool {
/// `Interleaved`, so a tree compiled here runs on either scanner.
const NATIVE_ORDERING: SeriesOrdering = SeriesOrdering::Contiguous;
/// The primary-key column names that carry pipeline metadata rather than series identity.
///
/// A window, a map and a binary operation drop these three labels wherever they read them, while
/// this route reports a series under the primary key it carries. A key column with one of these
/// names therefore keeps the existing plan however whole the rest of the key is: a node that drops
/// one of them can leave two raw series on one canonical label set, and the label set then no
/// longer names the series the partition holds.
const METADATA_KEY_TAGS: [&str; 3] = ["__name__", "__type__", "__unit__"];
/// One prepared occurrence of a selector: the scan that reads it, the raw-series layout it was
/// prepared against, and the columns its series are reported under.
///
/// The last three are the node's rather than one scan's — the binding names a time index, a tag list
/// and a metric name once — so they are compared across the occurrences and then travel to the
/// binding together. The first two are what [`same_source`] compares, which is the check that two
/// occurrences of one selector are one scan.
/// One prepared occurrence of a selector: its scan, raw-series layout and reporting columns.
type PreparedInput = (
LogicalPlan,
Arc<SeriesSchema>,
@@ -126,8 +111,9 @@ impl PromPlanner {
/// tree is compiled against.
///
/// `None` is not a failure: unsupported shapes and queries this route cannot bind faithfully
/// continue through the existing planner. Admission is limited to selectors and unary calls
/// named above, plus the scan, schema, matcher, and grid constraints checked below.
/// continue through the existing planner. Admission is limited to selectors, the unary calls
/// named above, and a same-table one-to-one arithmetic pair, with the scan, schema, matcher,
/// and grid constraints checked below.
pub(super) async fn try_plan_native_fused(
&mut self,
prom_expr: &PromExpr,
@@ -262,6 +248,8 @@ impl PromPlanner {
// default `None`. A metric-engine table is served only through the logical rewrite
// recognised above, which is what makes its series the table's own.
if !supports_native_raw_series(&table_meta.engine)
|| (selectors.len() == 2
&& !matches!(table_meta.engine.as_str(), MITO_ENGINE | MITO2_ENGINE))
|| (table_meta.engine == METRIC_ENGINE_NAME && !has_verified_metric_rewrite)
{
return Ok(None);
@@ -282,13 +270,11 @@ impl PromPlanner {
{
return Ok(None);
}
// FusedSeries evaluates each region independently, so partition keys must identify a
// series.
if !table
.table_info()
.meta
.partition_column_names()
.all(|column| tags.iter().any(|tag| tag == column))
// Preserve the existing single-selector guard that its partition keys are tags.
if selectors.len() == 1
&& !table_meta
.partition_column_names()
.all(|column| tags.iter().any(|tag| tag == column))
{
return Ok(None);
}
@@ -302,15 +288,6 @@ impl PromPlanner {
}) {
return Ok(None);
}
// Two occurrences are admitted only when the scan's raw series keys name one
// logical series each, so that either occurrence reads the same series in whichever
// partition holds it. A single occurrence needs no such proof: it reads its own scan
// once, and the checks above are the ones it has always had.
if selectors.len() == 2
&& !identity_keys_are_injective(&matchers, table_meta, &tags)
{
return Ok(None);
}
let series_schema = Arc::new(SeriesSchema {
fields: fields
.iter()
@@ -339,11 +316,8 @@ impl PromPlanner {
let Some(mut prepared) = preparation? else {
return Ok(None);
};
// The time index, the tag list and the metric name are the node's, not one scan's: the
// output schema and the binding name them once, and every occurrence reports its series
// under them. Two occurrences that disagree are two different reportings, which one node
// cannot be built for. Both occurrences of an admitted pair share the layout, so the first
// one's schema also speaks for the node below.
// The time index, the raw tag list and metric name are shared binding properties. The
// output tags may be narrower for a PromQL `on(...)` vector match.
let Some((_, series_schema, metric, time, tags)) = prepared.first() else {
return Ok(None);
};
@@ -450,55 +424,22 @@ impl PromPlanner {
.context(DataFusionPlanningSnafu);
}
}
// Two occurrences are admitted only when both read one scan plan: the node then runs one
// execution per partition of that one child, and both sides of the match see the same series
// in the same partition. Two actual scans — a different table, different matchers, different
// bounds — stay with the existing planner, which is where a match over more than one source
// is answered rather than in a region that holds part of it.
if selectors.len() == 2 && !same_source(&prepared[0], &prepared[1]) {
if selectors.len() == 2 && !same_table_scan(&prepared[0].0, &prepared[1].0) {
return Ok(None);
}
let mut children = prepared
let children = prepared
.into_iter()
.map(|(scan, _, _, _, _)| scan)
.collect::<Vec<_>>();
// A tree run once per scan partition has to read one series at a time: an aggregate folds a
// label set's series together and a vector operation (`sort`, `absent`, `topk`,
// `histogram_quantile`, ...) reads the whole vector at a grid point, so one region's share
// of the series cannot answer either. Declining here is the only place that still works:
// once the `FusedSeries` node exists the logical plan is fixed, and the categorizer merely
// declining to push it down would leave a node the physical layer must build anyway. What
// does read one series at a time is one occurrence's samples - a selection, a window, a map
// over either, or a constant broadcast - and, for a statement that names one selector
// twice, a one-to-one match whose key holds the whole identity over one scan, which the
// collapse below reads as that scan.
// A tree run once per region must be point-wise over a series. Binary matches between two
// series are supported only for the narrowly admitted one-to-one root pair.
let root = compiled.tree.root();
if reads_across_series(root)
&& !(pointwise_operand(root)
|| (selectors.len() == 2 && two_occurrences_pair_one_source(root, &tags)))
&& !(pointwise_operand(root) || (selectors.len() == 2 && independent_scan_pair(root)))
{
return Ok(None);
}
// Two occurrences of one selector read the same series only when the tree between them is
// point-wise, both scans are the same scan, and the match key holds the whole identity,
// which the checks above have just admitted. Both occurrences are then one child, so the
// tree is rebuilt with both inputs bound to it. A tree this route admits but cannot rebind
// is a compiler defect it declines (`NotImplemented`) rather than rewrites, and the existing
// plan answers the query; an error of any other kind is a defect to report.
let tree = if selectors.len() == 2 {
match collapsed_pair_tree(&compiled.tree) {
Ok(tree) => {
// Both occurrences read the one plan, so the two prepared children are that
// same plan and only one of them is kept.
children.truncate(1);
tree
}
Err(DataFusionError::NotImplemented(_)) => return Ok(None),
Err(e) => return Err(e).context(DataFusionPlanningSnafu),
}
} else {
compiled.tree
};
let tree = compiled.tree;
let root_fields = tree
.root()
.output
@@ -517,7 +458,8 @@ impl PromPlanner {
}) {
return Ok(None);
}
let output_schema = fused_output_schema(&root_fields, &tags, &time)?;
let output_tags = fused_output_tags(tree.root(), &tags);
let output_schema = fused_output_schema(&root_fields, &output_tags, &time)?;
let node = FusedSeries::try_new(
Arc::from(prom_expr.to_string()),
tree,
@@ -621,33 +563,8 @@ fn scan_time_filter(start: i64, end: i64, inclusive: bool, time_column: &str) ->
lower.and(column.lt_eq(upper))
}
/// Whether a tree is two occurrences of one selector matched point-wise over the whole identity.
///
/// The claim is about one series: it is whole in the partition it was scanned in, so a tree whose
/// nodes each read one series separately can run one execution per partition. What breaks that is a
/// node that reads a *series* rather than its samples point by point — an aggregate folds a group,
/// and two vectors matched against each other pair one series with another — so those keep the
/// existing plan. The remaining shapes are the ones a scan's own samples support: an instant
/// selection or a window directly over the raw input, a point-wise map over either, and a binary
/// operation with a constant on one side, which is a broadcast over the same positions.
///
/// A one-to-one match is admitted without rejecting its fill values: the compiler binds
/// `fill_left`/`fill_right` for a one-to-one match too, and what the admission rests on is the key
/// and the operands, not the absence of a fill — see [`matching_keys_on_identity`].
///
/// The pair's scans are the caller's claim: this function reads the tree and the tags, and a caller
/// that admits a pair on two actual scans would still be admitting a match over two sources. Both
/// the planner that collapses a pair onto one scan and the categorizer that places the node
/// region-side ask this, so the two cannot disagree about which pairs read one series: a pair
/// admitted here but refused there becomes a node the physical layer cannot build.
///
/// The raw keys themselves are proven to name one logical series each by the caller's preparation,
/// which checks the table's primary key against the tags this binding reports
/// (`identity_keys_are_injective`). That is a planning-time check over the scan the planner
/// prepared, not a property this function re-derives: a tree and binding built by anything other
/// than this planner carries no such proof, so a manual `FusedSeries` node is only as safe as the
/// binding its builder passed.
pub(crate) fn two_occurrences_pair_one_source(root: &NativeNode, tags: &[String]) -> bool {
/// Whether `root` is a supported one-to-one match between two independent scan occurrences.
pub(crate) fn independent_scan_pair(root: &NativeNode) -> bool {
let NativeExpr::Binary {
left,
right,
@@ -656,14 +573,58 @@ pub(crate) fn two_occurrences_pair_one_source(root: &NativeNode, tags: &[String]
else {
return false;
};
// A root with a constant on one side is a broadcast of that constant, not two occurrences
// matched, and `group_left`/`group_right` pair a group against one series.
let supported_operator = matches!(
matching.op,
promql::vector_match::BinaryOp::Add
| promql::vector_match::BinaryOp::Sub
| promql::vector_match::BinaryOp::Mul
| promql::vector_match::BinaryOp::Div
| promql::vector_match::BinaryOp::Mod
| promql::vector_match::BinaryOp::Pow
| promql::vector_match::BinaryOp::Atan2
);
let supported_labels = match &matching.labels {
MatchingLabels::On(names) => !names.is_empty(),
MatchingLabels::Ignoring(names) => names.is_empty(),
};
matching.cardinality == Cardinality::OneToOne
&& supported_operator
&& supported_labels
&& pointwise_operand(left)
&& pointwise_operand(right)
// A key that does not hold the whole identity pairs series Prometheus keeps apart, and a
// partition that holds one of them cannot tell that the other is missing.
&& matching_keys_on_identity(&matching.labels, tags)
}
/// Whether two prepared plans scan the same table. Filters, projections and bounds are
/// occurrence-specific and deliberately do not participate in this check.
pub(crate) fn same_table_scan(left: &LogicalPlan, right: &LogicalPlan) -> bool {
let (LogicalPlan::TableScan(left), LogicalPlan::TableScan(right)) = (left, right) else {
return false;
};
left.table_name == right.table_name
}
/// Tags exposed by a binary PromQL match. The binding retains all raw tags for decoding.
fn fused_output_tags(root: &NativeNode, tags: &[String]) -> Vec<String> {
let NativeExpr::Binary { matching, .. } = &root.expr else {
return tags.to_vec();
};
if matching.cardinality != Cardinality::OneToOne {
return tags.to_vec();
}
match &matching.labels {
MatchingLabels::On(names) if !names.is_empty() => tags
.iter()
.filter(|tag| names.iter().any(|name| name.as_ref() == tag.as_str()))
.cloned()
.collect(),
MatchingLabels::Ignoring(names) => tags
.iter()
.filter(|tag| !names.iter().any(|name| name.as_ref() == tag.as_str()))
.cloned()
.collect(),
MatchingLabels::On(_) => tags.to_vec(),
}
}
/// Whether `node` reads samples point-wise, so that one partitioned execution is one answer.
@@ -703,142 +664,6 @@ pub(crate) fn pointwise_operand(node: &NativeNode) -> bool {
}
}
/// Whether `labels` matches on the whole series identity `tags` names.
///
/// `on(...)` keeps the identity when every tag is listed, because two series that differ in any
/// tag then differ in the key. Anything else — a tag left out of `on`, or a tag named in
/// `ignoring` — pairs two series that Prometheus reads as two, and each partition would then
/// answer for the half it happens to hold.
pub(crate) fn matching_keys_on_identity(labels: &MatchingLabels, tags: &[String]) -> bool {
match labels {
MatchingLabels::On(names) => tags
.iter()
.all(|tag| names.iter().any(|name| name.as_ref() == tag.as_str())),
MatchingLabels::Ignoring(names) => tags
.iter()
.all(|tag| !names.iter().any(|name| name.as_ref() == tag.as_str())),
}
}
/// Whether the raw series keys `meta` declares name one logical series each.
///
/// The label set this route reports is the primary key it reads off a series, so the key is only a
/// faithful identity when it is the whole label set and its values are distinct strings: a hidden
/// key column (a metric engine's internals), a key column with a metadata name a window or a map
/// drops, and a key column that can read both NULL and the empty string — Prometheus's one absent
/// label, but two raw keys — all keep the existing plan. A matcher that rejects the empty value is
/// what makes a nullable key column safe: it leaves no row whose key could read either way.
fn identity_keys_are_injective(matchers: &Matchers, meta: &TableMeta, tags: &[String]) -> bool {
// A table with no key column has one label set for every series, and a metric engine keeps its
// identity in columns the planner hides from `tags`.
if tags.is_empty() || meta.engine == METRIC_ENGINE_NAME {
return false;
}
// The planner hides exactly `__table_id` and `__tsid`; a key that differs from the columns this
// route reports means part of the identity is not in the label set.
if !meta.row_key_column_names().eq(tags.iter()) {
return false;
}
tags.iter().all(|tag| {
if METADATA_KEY_TAGS.contains(&tag.as_str()) {
return false;
}
let Some(column) = meta.schema.column_schema_by_name(tag) else {
return false;
};
// The label set is reported as strings, which is what a string key column already is.
if column.data_type != datatypes::prelude::ConcreteDataType::string_datatype() {
return false;
}
// A non-nullable column cannot read NULL, so an empty value and an absent one are two
// different keys.
!column.is_nullable()
|| matchers
.matchers
.iter()
.any(|matcher| matcher.name == tag.as_str() && !matcher.is_match(""))
})
}
/// Whether two prepared occurrences read the same scan, and so produce the same series.
///
/// Everything below the node is compared, not only the table name: two selectors over one table
/// with different matchers or different bounds read different rows, and a partition that holds one
/// of them is not the partition that holds the other. DataFusion's own scan comparison covers the
/// table name, the projection, the schema, the filters and the fetch; the source is compared by
/// identity, because it is what resolves those rows in the first place. The reporting columns — the
/// metric name, the time index and the tags — are not compared here: they are checked once for
/// every prepared occurrence, before this runs.
fn same_source(left: &PreparedInput, right: &PreparedInput) -> bool {
let (left_input, left_schema, ..) = left;
let (right_input, right_schema, ..) = right;
let (LogicalPlan::TableScan(left_scan), LogicalPlan::TableScan(right_scan)) =
(left_input, right_input)
else {
return false;
};
// Both schemas are raw series schemas, so the fields are what the tree's input was compiled
// against, in the order it reads them.
let same_fields = left_schema
.fields
.iter()
.map(|field| &field.name)
.eq(right_schema.fields.iter().map(|field| &field.name));
same_fields && left_scan == right_scan && Arc::ptr_eq(&left_scan.source, &right_scan.source)
}
/// Rebuilds an admitted pair's tree so both of its occurrences read the one child prepared for them.
///
/// The compiler numbers occurrences in binding order, so an admitted pair's two input nodes are
/// numbered `0` and `1`; the planner prepared one plan for them, which is child `0`. Giving every
/// input node that index is what makes the executor hand each chunk of that child to both of them,
/// and what makes the node report one child for the whole match.
///
/// The tree is rebuilt rather than patched in place because a node's inputs are shared: the rebuilt
/// tree is validated again by [`NativeTree::try_new`], so a rebind can never produce a tree the
/// runtime would refuse. A tree whose inputs are not exactly those two occurrences is declined.
fn collapsed_pair_tree(tree: &NativeTree) -> datafusion::error::Result<NativeTree> {
let mut root = tree.root().clone();
let mut occurrences = 0;
let numbered = rebind_pair_inputs(&mut root, &mut occurrences);
if !numbered || occurrences != 2 {
return Err(DataFusionError::NotImplemented(format!(
"a two-occurrence tree whose {occurrences} input(s) are not the two occurrences \
numbered from zero, which this route cannot bind to one child"
)));
}
NativeTree::try_new(root)
}
/// Rebinds every input below `node` to child `0`, counting them and checking their numbering.
///
/// The walk visits inputs in the order the compiler bound them — an operand before the next one, the
/// left operand before the right — so occurrence `i` of the walk is the input the compiler numbered
/// `i`. `false` means the tree is not the pair this route admitted: a node carried a number the walk
/// did not expect, or a shape the admission does not cover. An `Aggregate` or a `Vector` below the
/// root is one of those shapes — neither is point-wise, so the pair was never admitted — and the
/// walk refuses it rather than rebinding a tree this route does not run.
fn rebind_pair_inputs(node: &mut NativeNode, next: &mut usize) -> bool {
match &mut node.expr {
NativeExpr::Input(bound) => {
let numbered = bound.source_index == *next;
bound.source_index = 0;
*next += 1;
numbered
}
NativeExpr::Scalar(_) => true,
NativeExpr::Instant { input, .. }
| NativeExpr::Window { input, .. }
| NativeExpr::Map { input, .. } => rebind_pair_inputs(input, next),
NativeExpr::Binary { left, right, .. } => {
let left_numbered = rebind_pair_inputs(left, next);
let right_numbered = rebind_pair_inputs(right, next);
left_numbered && right_numbered
}
NativeExpr::Aggregate { .. } | NativeExpr::Vector { .. } => false,
}
}
/// The single-selector route is limited to a selector or supported unary calls over one selector.
fn admitted_selector(expr: &PromExpr) -> Option<&VectorSelector> {
match expr {
@@ -949,12 +774,10 @@ mod tests {
use datatypes::series::SeriesOrdering;
use datatypes::series::samples::{FieldSpec, SeriesSchema};
use promql::native::plan::{NativeExpr, NativeNode, NativeTree};
use promql::vector_match::MatchingLabels;
use super::{
NATIVE_PROMQL_EXTENSION_KEY, admitted_selector, collapsed_pair_tree,
matching_keys_on_identity, native_promql_requested, pointwise_operand, scan_time_filter,
two_occurrences_pair_one_source,
NATIVE_PROMQL_EXTENSION_KEY, admitted_selector, fused_output_tags, independent_scan_pair,
native_promql_requested, scan_time_filter,
};
fn compiled_test_tree(query: &str) -> NativeTree {
@@ -1001,45 +824,36 @@ mod tests {
}
#[test]
fn pair_input_collapse_rebinds_only_two_numbered_occurrences() {
let tree = compiled_test_tree("x + x");
assert_eq!(input_sources(tree.root()), vec![0, 1]);
let collapsed = collapsed_pair_tree(&tree).unwrap();
assert_eq!(input_sources(collapsed.root()), vec![0, 0]);
assert_eq!(
tree.to_string().replace("source=1", "source=0"),
collapsed.to_string()
);
assert!(collapsed_pair_tree(&compiled_test_tree("x")).is_err());
assert!(collapsed_pair_tree(&compiled_test_tree("x + x + x")).is_err());
fn independent_scan_pair_keeps_both_numbered_occurrences() {
for query in [
"x + on(host) x",
"x - (x * 2)",
"rate(x[5s]) + rate(x[10s] offset 2s)",
] {
let tree = compiled_test_tree(query);
assert_eq!(input_sources(tree.root()), vec![0, 1], "{query}");
assert!(independent_scan_pair(tree.root()), "{query}");
}
assert!(!independent_scan_pair(
compiled_test_tree("sum(x) + x").root()
));
assert!(!independent_scan_pair(
compiled_test_tree("x + on() x").root()
));
assert!(!independent_scan_pair(
compiled_test_tree("x == on(host) x").root()
));
}
#[test]
fn binary_admission_requires_a_pointwise_whole_identity_match() {
let tags = vec!["host".to_string(), "rack".to_string()];
let pair = compiled_test_tree("x + on(host, rack) x");
assert!(two_occurrences_pair_one_source(pair.root(), &tags));
assert!(!pointwise_operand(pair.root()));
let partial = compiled_test_tree("x + on(host) x");
assert!(!two_occurrences_pair_one_source(partial.root(), &tags));
let nested_pair = compiled_test_tree("x - (x * 2)");
assert!(!pointwise_operand(nested_pair.root()));
assert!(two_occurrences_pair_one_source(nested_pair.root(), &tags));
let collapsed = collapsed_pair_tree(&nested_pair).unwrap();
assert_eq!(input_sources(collapsed.root()), vec![0, 0]);
assert_eq!(
nested_pair.to_string().replace("source=1", "source=0"),
collapsed.to_string(),
"collapsing inputs must preserve the noncommutative operand order"
);
assert!(matching_keys_on_identity(
&MatchingLabels::Ignoring(vec![]),
&tags
));
assert!(!matching_keys_on_identity(
&MatchingLabels::Ignoring(vec!["host".into()]),
&tags
));
fn match_output_tags_follow_promql_vector_matching() {
let tags = vec!["host".to_string(), "kind".to_string()];
let on = compiled_test_tree("x + on(host) x");
assert_eq!(vec!["host"], fused_output_tags(on.root(), &tags));
let ignoring = compiled_test_tree("x + x");
assert_eq!(tags, fused_output_tags(ignoring.root(), &tags));
let empty_on = compiled_test_tree("x + on() x");
assert_eq!(tags, fused_output_tags(empty_on.root(), &tags));
}
#[test]
+81 -12
View File
@@ -2399,19 +2399,88 @@ async fn native_fused_unsupported_shape_falls_back() {
}
}
/// Identical selectors share their scan while the binary operation remains in the native tree.
/// Independent selector occurrences remain distinct scans in the native tree.
#[tokio::test]
async fn native_fused_same_source_binary_uses_one_child() {
let plan = build_native_fused_plan("some_metric + some_metric", true).await;
let fused = find_fused_series(&plan).expect("the plan holds a native node");
assert_eq!("some_metric + some_metric", fused.promql());
assert_eq!(1, fused.inputs().len());
assert!(
matches!(fused.tree().root().expr, NativeExpr::Binary { .. }),
"the shared-source binary is not in the native tree: {:?}",
fused.tree().root().expr
);
async fn native_fused_binary_keeps_independent_scan_occurrences() {
for query in [
"some_metric + some_metric",
"some_metric{tag_0=\"a\"} + on(tag_0) some_metric{tag_0=\"b\"}",
"rate(some_metric[5m]) + on(tag_0) rate(some_metric[10m] offset 2m)",
] {
let plan = build_native_fused_plan(query, true).await;
let fused = find_fused_series(&plan).expect("the plan holds a native node");
assert_eq!(parser::parse(query).unwrap().to_string(), fused.promql());
assert_eq!(2, fused.inputs().len(), "{query}");
let scans = fused
.inputs()
.iter()
.map(|input| match input {
LogicalPlan::TableScan(scan) => scan,
other => panic!("expected direct scan, got {other}"),
})
.collect::<Vec<_>>();
assert_eq!(scans[0].table_name, scans[1].table_name, "{query}");
fn sources(node: &promql::native::plan::NativeNode, out: &mut Vec<usize>) {
match &node.expr {
NativeExpr::Input(input) => out.push(input.source_index),
NativeExpr::Instant { input, .. }
| NativeExpr::Window { input, .. }
| NativeExpr::Map { input, .. } => sources(input, out),
NativeExpr::Binary { left, right, .. } => {
sources(left, out);
sources(right, out);
}
NativeExpr::Scalar(_)
| NativeExpr::Aggregate { .. }
| NativeExpr::Vector { .. } => {}
}
}
let mut source_indices = Vec::new();
sources(fused.tree().root(), &mut source_indices);
assert_eq!(vec![0, 1], source_indices, "{query}");
assert!(matches!(
fused.tree().root().expr,
NativeExpr::Binary { .. }
));
let filters = scans
.iter()
.map(|scan| {
scan.filters
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
match query {
"some_metric{tag_0=\"a\"} + on(tag_0) some_metric{tag_0=\"b\"}" => {
assert!(filters[0].iter().any(|filter| filter.contains("a")));
assert!(filters[1].iter().any(|filter| filter.contains("b")));
}
"rate(some_metric[5m]) + on(tag_0) rate(some_metric[10m] offset 2m)" => {
assert!(
filters[0].iter().any(|filter| filter.contains("-300000")),
"{:?}",
filters[0]
);
assert!(
filters[1].iter().any(|filter| filter.contains("-720000")),
"{:?}",
filters[1]
);
assert!(
filters[0].iter().any(|filter| filter.contains("100000000")),
"{:?}",
filters[0]
);
assert!(
filters[1].iter().any(|filter| filter.contains("99880000")),
"{:?}",
filters[1]
);
}
_ => {}
}
}
}
/// A label rewrite stays on the existing plan, which is the one that projects its new label.
+267 -46
View File
@@ -1965,6 +1965,63 @@ fn assert_delta_rows(
}
}
fn assert_two_source_native_plan(
plan: &Arc<dyn datafusion::physical_plan::ExecutionPlan>,
require_multiple_nonempty_partitions: bool,
) {
use datafusion::physical_plan::metrics::MetricValue;
assert_eq!(plan.name(), "FusedSeriesExec");
assert_eq!(plan.output_partitioning().partition_count(), 1);
assert_eq!(plan.children().len(), 2);
let schema = plan.schema();
assert_eq!(
schema.fields().len(),
3,
"binary output must have ts, value, host"
);
assert!(schema.field_with_name("ts").is_ok());
assert!(schema.field_with_name("host").is_ok());
assert!(schema.field_with_name("kind").is_err());
let children = plan.children();
assert!(!Arc::ptr_eq(children[0], children[1]));
for (source, child) in children.iter().enumerate() {
assert_eq!(child.output_partitioning().partition_count(), 4);
let scan = child
.downcast_ref::<CooperativeExec>()
.map_or(*child, |cooperative| cooperative.input());
let scan = scan.downcast_ref::<SeriesScanExec>().unwrap_or_else(|| {
panic!("source {source} is not a SeriesScanExec: {scan:?}; plan={plan:?}")
});
let metrics = scan.metrics().expect("series scan metrics");
assert!(metrics.output_rows().expect("series scan output rows") > 0);
let mut nonempty_partitions = [false; 4];
for metric in metrics.iter() {
if let MetricValue::OutputRows(count) = metric.value()
&& let Some(partition) = metric.partition()
&& let Some(nonempty) = nonempty_partitions.get_mut(partition)
{
*nonempty |= count.value() > 0;
}
}
if require_multiple_nonempty_partitions {
assert!(
nonempty_partitions
.iter()
.filter(|nonempty| **nonempty)
.count()
> 1,
"source {source} must produce rows on multiple physical scan partitions: {nonempty_partitions:?}"
);
}
}
let display = datafusion::physical_plan::displayable(plan.as_ref())
.indent(false)
.to_string();
assert_eq!(display.matches("source=0").count(), 1, "{display}");
assert_eq!(display.matches("source=1").count(), 1, "{display}");
}
fn assert_four_native_partitions(plan: &Arc<dyn datafusion::physical_plan::ExecutionPlan>) {
use datafusion::physical_plan::metrics::MetricValue;
@@ -2080,15 +2137,191 @@ async fn native_promql_executes_fused_node_and_matches_legacy() {
assert_eq!(native_rows, legacy_rows);
}
/// Regression for the two occurrences of one selector that read one scan (#9896).
///
/// `m{host!=""} - (m{host!=""} * 2)` names one selector twice; the native route binds both tree
/// inputs to the one physical child it prepares, which needs an ordinary Mito table keyed by the
/// whole label set (`host` NOT NULL, empty match rejected). Series scans read 8,192-row batches
/// and hand whole series to successive partitions, so the fixture must exceed one batch: 512
/// hosts x 64 points is 32,768 rows, i.e. at least four batches reaching all four partitions.
/// Distinct selector scans in a binary expression retain independent bindings and bounds (#9896).
#[tokio::test(flavor = "multi_thread")]
async fn native_promql_two_occurrences_route_one_scan_per_partition() {
async fn native_promql_binary_join_uses_two_independent_scans() {
// Enough rows per source for each independent scan to span multiple 8,192-row read batches.
const HOSTS: usize = 4_608;
const POINTS: i64 = 6;
const BATCH_ROWS: usize = 512;
fn insert_request(table: &str, schema: &[ColumnSchema], rows: Vec<Row>) -> RowInsertRequests {
RowInsertRequests {
inserts: vec![RowInsertRequest {
table_name: table.to_string(),
rows: Some(Rows {
schema: schema.to_vec(),
rows,
}),
}],
}
}
common_telemetry::init_default_ut_logging();
let config = mito2::config::MitoConfig {
experimental_series_scan_v2: true,
..Default::default()
};
let (standalone, observer) = native_test_instance(
GreptimeDbStandaloneBuilder::new("native_promql_two_scan_binary_e2e")
.with_mito_config(config),
)
.await;
let instance = standalone.fe_instance().clone();
let table = "native_two_scan_pair";
execute_all(
&instance,
&format!(
"CREATE TABLE {table} (host STRING, kind STRING, ts TIMESTAMP TIME INDEX, val DOUBLE, PRIMARY KEY(host, kind))"
),
QueryContext::arc(),
)
.await;
let schema = vec![
ColumnSchema {
column_name: "host".to_string(),
datatype: ColumnDataType::String as i32,
semantic_type: SemanticType::Tag as i32,
..Default::default()
},
ColumnSchema {
column_name: "kind".to_string(),
datatype: ColumnDataType::String as i32,
semantic_type: SemanticType::Tag as i32,
..Default::default()
},
ColumnSchema {
column_name: "ts".to_string(),
datatype: ColumnDataType::TimestampMillisecond as i32,
semantic_type: SemanticType::Timestamp as i32,
..Default::default()
},
ColumnSchema {
column_name: "val".to_string(),
datatype: ColumnDataType::Float64 as i32,
semantic_type: SemanticType::Field as i32,
..Default::default()
},
];
let mut batch = Vec::with_capacity(BATCH_ROWS);
for host in 0..HOSTS {
for (kind, intercept, slope) in [("a", 100.0, 2.0), ("b", 1_000.0, 5.0)] {
for point in 0..POINTS {
batch.push(Row {
values: vec![
ValueData::StringValue(format!("h{host:04}")).into(),
ValueData::StringValue(kind.to_string()).into(),
ValueData::TimestampMillisecondValue(point * 1_000).into(),
ValueData::F64Value(intercept + slope * point as f64).into(),
],
});
if batch.len() == BATCH_ROWS {
let rows = std::mem::replace(&mut batch, Vec::with_capacity(BATCH_ROWS));
instance
.handle_row_inserts(
insert_request(table, &schema, rows),
QueryContext::arc(),
false,
false,
)
.await
.unwrap();
}
}
}
}
if !batch.is_empty() {
instance
.handle_row_inserts(
insert_request(table, &schema, batch),
QueryContext::arc(),
false,
false,
)
.await
.unwrap();
}
execute_all(
&instance,
&format!("ADMIN FLUSH_TABLE('{table}')"),
QueryContext::arc(),
)
.await;
// Distinct matcher inputs produce an independently checked noncommutative result.
let instant_sql =
format!("TQL EVAL (5, 5, '1s') {table}{{kind=\"a\"}} - on(host) {table}{{kind=\"b\"}}");
observer.clear();
let instant_rows =
native_result_rows(native_tql_output(&instance, &instant_sql, true).await).await;
let instant_plan = observer.take();
assert_two_source_native_plan(&instant_plan, true);
let instant_expected = (0..HOSTS)
.map(|host| (5_000, format!("h{host:04}"), -915.0))
.collect::<Vec<_>>();
assert_eq!(instant_rows, instant_expected);
let instant_legacy =
native_result_rows(native_tql_output(&instance, &instant_sql, false).await).await;
assert_eq!(instant_rows, instant_legacy);
// Independent range windows: left reads (1, 5], right (1, 3] after offset.
// Positive, distinct counter slopes make the exact binary result 2 - 5 = -3 per host.
let sql = format!(
"TQL EVAL (5, 5, '1s') rate({table}{{kind=\"a\"}}[4s] @ 5) - on(host) rate({table}{{kind=\"b\"}}[2s] @ 4 offset 1s)"
);
observer.clear();
let native_rows = native_result_rows(native_tql_output(&instance, &sql, true).await).await;
let plan = observer.take();
assert_eq!(plan.name(), "FusedSeriesExec");
assert_two_source_native_plan(&plan, true);
let input_samples = plan
.metrics()
.expect("fused metrics")
.iter()
.filter_map(|metric| match metric.value() {
datafusion::physical_plan::metrics::MetricValue::Count { name, count }
if name.as_ref() == "native.root.input_samples" =>
{
Some(count.value())
}
_ => None,
})
.sum::<usize>();
assert_eq!(
input_samples,
HOSTS * 2,
"the binary consumes one evaluated value from each operand per host"
);
for (metric_name, expected_samples) in [
("native.root.left.input_samples", HOSTS * 4),
("native.root.right.input_samples", HOSTS * 2),
] {
let source_samples = plan
.metrics()
.expect("fused metrics")
.iter()
.filter_map(|metric| match metric.value() {
datafusion::physical_plan::metrics::MetricValue::Count { name, count }
if name.as_ref() == metric_name =>
{
Some(count.value())
}
_ => None,
})
.sum::<usize>();
assert_eq!(source_samples, expected_samples, "{metric_name}");
}
let expected = (0..HOSTS)
.map(|host| (5_000, format!("h{host:04}"), -3.0))
.collect::<Vec<_>>();
assert_eq!(native_rows, expected);
let legacy_rows = native_result_rows(native_tql_output(&instance, &sql, false).await).await;
assert_eq!(native_rows, legacy_rows);
}
/// Repeated selectors retain correct binary results while using independent physical scans.
#[tokio::test(flavor = "multi_thread")]
async fn native_promql_repeated_selector_binary_uses_independent_scans() {
// 512 hosts * 64 points = 32,768 rows, generated in 512-row insert batches: at least four
// 8,192-row read batches, so the ordinary scan spreads the series over all four partitions.
const HOSTS: usize = 512;
@@ -2214,24 +2447,7 @@ async fn native_promql_two_occurrences_route_one_scan_per_partition() {
let native_rows = native_result_rows(native_tql_output(&instance, &sql, true).await).await;
let plan = observer.take();
assert_eq!(plan.name(), "FusedSeriesExec");
assert_eq!(
plan.children().len(),
1,
"the two occurrences must read one physical child"
);
assert_four_native_partitions(&plan);
let display = datafusion::physical_plan::displayable(plan.as_ref())
.indent(false)
.to_string();
assert_eq!(
display.matches("source=0").count(),
2,
"both tree inputs must route to the one child:\n{display}"
);
assert!(
!display.contains("source=1"),
"no tree input may route to a child that does not exist:\n{display}"
);
assert_two_source_native_plan(&plan, true);
assert_eq!(native_rows, expected);
let legacy_rows = native_result_rows(native_tql_output(&instance, &sql, false).await).await;
assert_eq!(native_rows, legacy_rows);
@@ -2244,23 +2460,16 @@ async fn native_promql_two_occurrences_route_one_scan_per_partition() {
native_result_rows(native_tql_output(&instance, &direct_sql, true).await).await;
let direct_plan = observer.take();
assert_eq!(direct_plan.name(), "FusedSeriesExec");
assert_eq!(direct_plan.children().len(), 1);
assert_four_native_partitions(&direct_plan);
let direct_display = datafusion::physical_plan::displayable(direct_plan.as_ref())
.indent(false)
.to_string();
assert_eq!(
direct_display.matches("source=0").count(),
2,
"{direct_display}"
);
assert!(!direct_display.contains("source=1"), "{direct_display}");
assert_two_source_native_plan(&direct_plan, true);
let direct_expected = (0..POINTS_PER_HOST)
.flat_map(|point| {
(0..HOSTS).map(move |host| (point * 1_000, host_name(host), value(host, point) * 2.0))
})
.collect::<Vec<_>>();
assert_eq!(direct_rows, direct_expected);
let direct_legacy =
native_result_rows(native_tql_output(&instance, &direct_sql, false).await).await;
assert_eq!(direct_rows, direct_legacy);
// A single selector with an equality matcher and an explicit range anchor/offset must push
// down both identity and time bounds. For h000, values equal the sample index; the first
@@ -2326,19 +2535,23 @@ async fn native_promql_two_occurrences_route_one_scan_per_partition() {
"matcher and anchored range must scan only the three h000 samples"
);
// Different bounds/offsets must not be fused together: h000 at t=1 is 1, while the
// Different bounds/offsets remain independent: h000 at t=1 is 1, while the
// one-second-offset occurrence reads t=0, so the exact answer is 1 - (0 * 2) = 1.
let different_bounds = format!(
"TQL EVAL (1, 1, '1s') {table}{{host=\"h000\"}} - ({table}{{host=\"h000\"}} offset 1s * 2)"
"TQL EVAL (1, 1, '1s') {table}{{host=\"h000\"}} - on(host) ({table}{{host=\"h000\"}} offset 1s * 2)"
);
observer.clear();
let different_bounds_rows =
native_result_rows(native_tql_output(&instance, &different_bounds, true).await).await;
assert!(observer.fused().is_none(), "{different_bounds}");
let different_bounds_plan = observer.take();
assert_two_source_native_plan(&different_bounds_plan, false);
assert_eq!(
different_bounds_rows,
vec![(1_000, "h000".to_string(), 1.0)]
);
let different_bounds_legacy =
native_result_rows(native_tql_output(&instance, &different_bounds, false).await).await;
assert_eq!(different_bounds_rows, different_bounds_legacy);
// on() drops host from the output labels; inspect that shape directly instead of using the
// host-requiring native_result_rows helper.
@@ -2389,14 +2602,22 @@ async fn native_promql_two_occurrences_route_one_scan_per_partition() {
unlabeled_rows(native_tql_output(&instance, &on_empty_sql, false).await).await;
assert_eq!(on_empty_rows, on_empty_legacy);
// A group match drops the full series identity; different matchers, group matching, and a
// subquery all stay on the ordinary planner path.
// Different matcher forms can still be joined natively while preserving both source bounds.
let different_matchers =
format!(r#"TQL EVAL (0, 0, '1s') {table}{{host!=""}} - ({table}{{host=~"h.+"}} * 2)"#);
observer.clear();
let different_matcher_rows =
native_result_rows(native_tql_output(&instance, &different_matchers, true).await).await;
let different_matcher_plan = observer.take();
assert_two_source_native_plan(&different_matcher_plan, false);
assert_eq!(different_matcher_rows, expected[..HOSTS].to_vec());
let different_matcher_legacy =
native_result_rows(native_tql_output(&instance, &different_matchers, false).await).await;
assert_eq!(different_matcher_rows, different_matcher_legacy);
// Group matching drops the full series identity, and a subquery stays on the ordinary planner path.
let fallback_expected = expected[..HOSTS].to_vec();
for (sql, expected_rows) in [
(
format!(r#"TQL EVAL (0, 0, '1s') {table}{{host!=""}} - ({table}{{host=~"h.+"}} * 2)"#),
fallback_expected.clone(),
),
(
format!(
r#"TQL EVAL (0, 0, '1s') {table}{{host!=""}} - on(host) group_left() ({table}{{host!=""}} * 2)"#
@@ -38,10 +38,18 @@ TQL EVAL (4, 4, '1s') fused_multi_standalone{host!="",kind!=""} + on(host, kind)
-- SQLNESS SORT_RESULT 3 1
TQL EVAL (4, 4, '1s') fused_multi_standalone{host!="",kind!=""} == bool fused_multi_standalone{host!="",kind!=""};
-- Different matchers cannot share one fused scan; on(host) keeps host: h1 55 and h2 110.
-- Independent matcher-bearing inputs match on host: h1 55 and h2 110.
-- SQLNESS SORT_RESULT 3 1
TQL EVAL (4, 4, '1s') fused_multi_standalone{kind="left"} + on(host) fused_multi_standalone{kind="right"};
-- Noncommutative matching detects swapped or reused inputs: h1 -45 and h2 -90.
-- SQLNESS SORT_RESULT 3 1
TQL EVAL (4, 4, '1s') fused_multi_standalone{kind="left"} - on(host) fused_multi_standalone{kind="right"};
-- Each range input keeps its own bounds: left ends at 2s, right at 4s; rates differ by -9/-18.
-- SQLNESS SORT_RESULT 3 1
TQL EVAL (4, 4, '1s') rate(fused_multi_standalone{kind="left"}[3s] @ 3 offset 1s) - on(host) rate(fused_multi_standalone{kind="right"}[2s] @ 4);
-- group_right keeps the right-side kind: h1/left 10 and h1/right 55.
-- SQLNESS SORT_RESULT 3 1
TQL EVAL (4, 4, '1s') fused_multi_standalone{kind="left",host="h1"} + on(host) group_right fused_multi_standalone{host="h1"};