mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-11-21 18:24:07 +00:00
feat(promql): fold pi() and answer quantile as a complete aggregate (#10433)
* feat(promql): fold pi() and answer quantile as a complete aggregate pi() folds through constant_value like any other constant, so pi() * x, clamp_max(x, pi()) and quantile(pi()/4, x) all compile natively. The value is std::f64::consts::PI on both routes. Note this is wider than the legacy planner, which does not treat a call as a literal and so rejects those parameter positions; it is an extension of constant support, not a narrowing. quantile now binds a Complete fold that retains the group's input and answers with the existing aggregate_quantile kernel. A quantile has no partial kernel, so split.rs and the state binding are untouched and a Partial/Final quantile cannot exist: validate refuses it and check_cut declines the cut. A partitioned input therefore gathers above per-series prefixes instead of folding per partition, which is the only correct distributed shape for a non-associative aggregate -- the raw-source boundary keeps fragments from being read as whole series, and the router merges a raw identity once every input reaches EOF. The retained input is not a fixed-width state: a quantile keeps every group until EOF and cannot spill, so it is bounded by pool admission, which the module doc now states. Its parameter is sparse, covering the grid positions the input actually holds rather than the window they span. Two points 100k positions apart used to allocate an 800KB dense parameter before any admission; now the parameter and the kernel's position/value copy are charged by row bound before allocation, and retaining a series reserves the Vec capacity it is about to grow to rather than one slot. A test runs two distant points under a 64KiB pool and finishes with nothing left reserved. quantile over a histogram no longer falls back: the legacy planner groups it with min, max, stddev and stdvar, skipping histogram samples and reporting an info, so the admission check now refuses only sum and avg, which fold the payload. The native runtime asserts the same info as min over a histogram-only chunk. Signed-off-by: discord9 <discord9@163.com> * fix(promql): bill a quantile's retained chunks the way a binary operand is billed series_bytes charges the whole Arrow parent buffer a slice points into, and every histogram table it references, so a series handed over as many slices of one buffer was charged once per chunk: two thousand one-row slices of a 16 KB array came to ~32 MB against a 4 MiB pool and were refused around the 260th, while min over the same input answered. Retained chunks are now billed as a binary operand bills its operand: operand_chunk_bytes measures what each chunk holds on its own, with the parent buffer counted once across its slices, and unbilled_tables bills each histogram table once, remembered by allocation address. QuantileInput::bytes() reports real residency -- its own size, the vector's capacity and the folded held bytes -- instead of a per-chunk sum, so every resize settles on what the node actually keeps. The capacity pre-booking stays and is kept out of held, so a vector slot is not charged twice. The regression hands one array over as 2000 single-row slices under a 4 MiB pool and asserts min and quantile answer the same and both return their reservation. Restoring the old billing makes it fail with ResourcesExhausted, which is the failure it exists to catch. Signed-off-by: discord9 <discord9@163.com> * fix(promql): bill a quantile's retained input once per allocation The previous change stopped charging every chunk for the whole parent buffer, but the figure was still not self-consistent in three ways. operand_chunk_bytes subtracted the whole of fields_bytes() and added back only the row bytes of the slice, so a small slice of a big buffer was billed as small while the whole allocation stayed resident, and a validity bitmap was not counted at all. Retained chunks are now billed per Arrow allocation, keyed on the buffer's data pointer and charged at capacity, the same way unbilled_tables bills histogram tables. The no-identity alternative -- bill every chunk for the whole buffer -- over-estimates and would refuse a legal query, which is the reported bug mirrored. series_bytes includes size_of::<Series>(), and QuantileInput::bytes() also counted every vector slot, so each used slot paid for its Series twice. The chunk figure now carries only what sits beside the samples and the struct is left to the vector. pending() returned the incoming chunk's figure for every fold, but the quantile retainer books its own figure before the push, so that backing was reserved twice and could refuse a query before any memory was actually held. finish_quantile also settled on the output while its input was still alive by value; it now drops the input first. Billing only; no answer changes. A 2000-chunk input goes from 1,757,960 to 1,181,968 bytes (the 2000 x 288 of double-counted structs plus one 16 KB allocation billed once), and every quantile billing test ends with the pool empty. Five mutations -- rows-only figure, no buffer dedup, per-chunk series_bytes, grid-wide parameter, struct added back per chunk -- each fail the test that covers them. Signed-off-by: discord9 <discord9@163.com> * test(promql): cover a quantile over no input and drop its redundant guards A quantile whose input holds no sample in range has no positions to build a parameter from, so it hands the kernel an empty scalar and answers nothing. No test reached that: replacing the fallback with an error left every quantile and aggregate test green. The new one runs the node without pushing a chunk at all and asserts an empty answer with the pool back to zero. new_quantile also re-checked the stage, the kind and the grid. validate already refuses a Partial or Final quantile -- aggregate_state_binding has no state for one -- and the node's schema is the tree's validated output, so those checks could not fire. The quantile is now passed in from the arm that destructured it. The billing test's comment described the operand_chunk_bytes model; the quantile bills chunk_bytes_beside_samples plus the Arrow allocations its slices point into. Signed-off-by: discord9 <discord9@163.com> * test(promql): answer a quantile over a multi-input tree instead of declining it quantile stopped being a kind the runtime refuses, so the query that pinned that refusal now answers natively. It moved out of the declines test into its own case, which asserts the existing plan answers 3/16, that the route plans a fused node for it, and that the native rows equal the legacy ones. The grouping-by-a-value-field case stays where it was. Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com>
This commit is contained in:
@@ -1712,19 +1712,32 @@ fn subtracted_scaled_tree() -> NativeTree {
|
||||
.expect("the bound tree is legal")
|
||||
}
|
||||
|
||||
/// The same tree asking for `quantile`, whose state the runtime does not keep yet.
|
||||
fn quantile_aggregate_tree() -> NativeTree {
|
||||
/// The same tree grouped by a value field, which no kernel can address.
|
||||
///
|
||||
/// A group is identified by labels, and a value field is not one: its value varies between the
|
||||
/// samples of one series, so a group keyed by it would be a group per point. The runtime refuses the
|
||||
/// grouping rather than folding series into groups the previous route keeps apart.
|
||||
fn ungroupable_aggregate_tree() -> NativeTree {
|
||||
let complete = aggregate_tree();
|
||||
let root = complete.root();
|
||||
let NativeExpr::Aggregate { input, op } = &root.expr else {
|
||||
let NativeExpr::Aggregate { input, .. } = &root.expr else {
|
||||
unreachable!("the fixture is an aggregate over a window");
|
||||
};
|
||||
let field = root
|
||||
.output
|
||||
.samples()
|
||||
.expect("the aggregate produces samples")
|
||||
.fields
|
||||
.first()
|
||||
.expect("the aggregate has a value field")
|
||||
.name
|
||||
.clone();
|
||||
NativeTree::try_new(NativeNode {
|
||||
expr: NativeExpr::Aggregate {
|
||||
input: input.clone(),
|
||||
op: BoundAggregate {
|
||||
kind: AggregateKind::Quantile { quantile: 0.5 },
|
||||
group: op.group.clone(),
|
||||
kind: AggregateKind::Sum,
|
||||
group: Grouping::By(Arc::new([field])),
|
||||
stage: AggregateStage::Complete,
|
||||
},
|
||||
},
|
||||
@@ -3685,20 +3698,19 @@ async fn a_series_spanning_chunks_keeps_absolute_positions_and_closes_once() {
|
||||
|
||||
/// A tree the runtime does not execute is refused when the execution plan is built.
|
||||
///
|
||||
/// The runtime folds the supported `Complete` kinds now, so the tree below asks for one it still
|
||||
/// refuses on purpose: `quantile` needs the parameter and the member buffer that the other kinds
|
||||
/// do not. What the test pins is the refusal happening while the plan is built, not the particular
|
||||
/// kind of node that provokes it.
|
||||
/// The runtime folds every supported `Complete` kind now, `quantile` among them, so the tree below
|
||||
/// asks for a grouping no kernel can address - a group by a value field. What the test pins is the
|
||||
/// refusal happening while the plan is built, not the particular kind of node that provokes it.
|
||||
#[tokio::test]
|
||||
async fn a_tree_the_runtime_cannot_run_is_rejected_when_the_exec_is_built() {
|
||||
let node = fused(quantile_aggregate_tree());
|
||||
let node = fused(ungroupable_aggregate_tree());
|
||||
let error = plan_of(node, memory_exec(Vec::new()))
|
||||
.expect_err("the runtime does not execute a quantile aggregate");
|
||||
.expect_err("the runtime does not fold a group by a value field");
|
||||
assert!(
|
||||
matches!(error, DataFusionError::NotImplemented(_)),
|
||||
"the runtime refuses the tree it cannot run: {error}"
|
||||
);
|
||||
assert!(error.to_string().contains("Quantile"), "{error}");
|
||||
assert!(error.to_string().contains("per-point"), "{error}");
|
||||
}
|
||||
|
||||
/// A tree evaluated on another grid than the node declares is refused before it runs.
|
||||
|
||||
@@ -28,20 +28,44 @@
|
||||
//! each missing the other partitions' contributions. The stages that exist for that shape are
|
||||
//! [`AggregateStage::Partial`] and [`AggregateStage::Final`], which carry kernel accumulator
|
||||
//! state between executions.
|
||||
//!
|
||||
//! `quantile` is the one aggregate here that is not a fold of each point's values. Its answer is the
|
||||
//! φ-quantile of the group's whole sample set at that point, so there is no fixed-width accumulator
|
||||
//! that answers it, and no state that combines two partitions' quantiles into the quantile of their
|
||||
//! union. What the node keeps instead is its input: every series it was handed, until the input
|
||||
//! ends, when [`crate::aggregate::aggregate_quantile`] answers from all of it at once. That is also
|
||||
//! why `quantile` exists only as [`AggregateStage::Complete`] here, and why a plan that would have
|
||||
//! to fold it per partition has to keep the previous route instead of splitting it.
|
||||
//!
|
||||
//! There is no spill either: a `quantile` keeps the whole input of every group, at once, until the
|
||||
//! last chunk arrives, so what it holds is not bounded by the number of groups the way a fixed-width
|
||||
//! accumulator's is -- it is bounded by the pool. Each chunk is admitted before it is kept and the
|
||||
//! reservation settles on the input retained so far ([`AggregateRuntime::accumulate`]), so a
|
||||
//! statement whose groups' samples do not fit the pool is refused rather than served from disk, and
|
||||
//! a caller that needs an answer in bounded memory needs a wider pool rather than a wider node.
|
||||
//! Everything else in this runtime trades memory for the width of a point: a fold's state per group
|
||||
//! and point, this one's whole input.
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::sync::Arc;
|
||||
|
||||
use common_query::promql_annotations::PromqlAnnotationCollector;
|
||||
use datafusion::error::{DataFusionError, Result};
|
||||
use datatypes::series::samples::{Series, SeriesSchema};
|
||||
use datatypes::series::SeriesLabels;
|
||||
use datatypes::series::samples::{FieldSpec, Series, SeriesSchema};
|
||||
use datatypes::series::time::{EvalGrid, GridRange, TimeAxis, union_positions};
|
||||
|
||||
use crate::aggregate::{
|
||||
AggregateOp, FloatAggregator, FloatMerger, Grouping, HistogramSamples, histogram_samples,
|
||||
validate_aggregate_series,
|
||||
AggregateOp, FloatAggregator, FloatMerger, Grouping, HistogramSamples, aggregate_quantile,
|
||||
histogram_samples, validate_aggregate_series,
|
||||
};
|
||||
use crate::evaluated::EvaluatedChunk;
|
||||
use crate::grid_series::series_bytes;
|
||||
use crate::native::execute::{NodeBuffer, evaluated_bytes};
|
||||
use crate::grid_series::{GridSeriesBuilder, series_bytes};
|
||||
use crate::map::{constant_series, validate_grid_series};
|
||||
use crate::native::execute::{
|
||||
NodeBuffer, chunk_bytes_beside_samples, evaluated_bytes, unbilled_sample_buffers,
|
||||
unbilled_tables,
|
||||
};
|
||||
use crate::native::plan::{
|
||||
AggregateKind, AggregateStage, AggregateStateSchema, BoundAggregate, Grouping as BoundGrouping,
|
||||
aggregate_state_binding,
|
||||
@@ -54,6 +78,89 @@ pub(crate) enum AggregateOutput {
|
||||
State(StateChunk),
|
||||
}
|
||||
|
||||
/// What a `Complete` aggregate keeps while its input arrives.
|
||||
///
|
||||
/// The eight point-wise aggregates fold each grid point's values into fixed-width state; `quantile`
|
||||
/// keeps the samples themselves, because the φ-quantile of a group is not a value its members' values
|
||||
/// fold to.
|
||||
#[derive(Clone, Copy)]
|
||||
enum Fold {
|
||||
/// One fixed-width accumulator per point and field.
|
||||
Pointwise(AggregateOp),
|
||||
/// The whole input, kept until it ends.
|
||||
Quantile,
|
||||
}
|
||||
|
||||
/// The input a `quantile` aggregate keeps, until it has all of it.
|
||||
///
|
||||
/// The members are the series the node was handed, in the order they arrived: that order is the order
|
||||
/// [`aggregate_quantile`] folds a group's members in, and a series delivered in several chunks
|
||||
/// contributes at each position it holds, so the pieces of one series answer what the whole series
|
||||
/// would have. Nothing here is grouped: the kernel groups, and it groups the same way for a chunk as
|
||||
/// for a series of its own.
|
||||
struct QuantileInput {
|
||||
/// The φ the query wrote. The kernel reads it as Prometheus does, so a value outside `[0, 1]`
|
||||
/// answers `-Infinity` or `+Infinity` rather than being refused here.
|
||||
quantile: f64,
|
||||
/// One entry per series this node was handed.
|
||||
series: Vec<Series>,
|
||||
/// The histogram tables the retained chunks index into, by the address of the allocation behind
|
||||
/// their `Arc`.
|
||||
///
|
||||
/// A table is one allocation however many chunks of a series index into it, so it is billed once
|
||||
/// -- by the first chunk that references it -- exactly as a binary operand bills the tables of
|
||||
/// the chunks it retains. That is what keeps a series delivered in a thousand chunks from costing
|
||||
/// a thousand tables, and what keeps a table from costing nothing: the per-chunk figure leaves
|
||||
/// its tables out ([`chunk_bytes_beside_samples`]), so a table in neither this set nor a
|
||||
/// reservation is a table nothing covers.
|
||||
tables: HashSet<usize>,
|
||||
/// The Arrow allocations the retained chunks' samples live in, by the address of the allocation
|
||||
/// behind their `Buffer`.
|
||||
///
|
||||
/// The chunks of one Arrow batch are the same story as its tables one level down, and the same
|
||||
/// answer: what a retainer keeps is the allocation its slices point into, and one allocation is
|
||||
/// one however many chunks slice it, so it is billed once -- by the first chunk that references
|
||||
/// it ([`unbilled_sample_buffers`]) -- rather than once per chunk. That is what keeps a series
|
||||
/// handed over as a thousand slices of one buffer from costing that buffer a thousand times. A
|
||||
/// chunk that holds a small slice of a large allocation is charged the allocation, because the
|
||||
/// allocation is what stays resident; its rows would be the smaller and wrong figure.
|
||||
buffers: HashSet<usize>,
|
||||
/// The bytes the retained chunks hold, folded in as they arrive.
|
||||
///
|
||||
/// Each chunk is measured once, by what it holds on its own ([`chunk_bytes_beside_samples`]):
|
||||
/// its axis, its descriptors and its labels, beside the allocations it is the first to reference
|
||||
/// -- the Arrow buffers its samples live in and the histogram tables they index into, each
|
||||
/// billed once as [`unbilled_sample_buffers`] and [`unbilled_tables`] report them. Neither the
|
||||
/// allocation those samples are slices of nor the struct that carries the chunk is part of the
|
||||
/// figure: the first is billed as the allocation it is and not as the rows a chunk holds of it,
|
||||
/// and the second is the slot the vector holds the chunk in. [`Self::bytes`] adds that vector
|
||||
/// from the capacity it holds, which is why the struct is not counted here as well.
|
||||
held: usize,
|
||||
}
|
||||
|
||||
impl QuantileInput {
|
||||
/// The bytes this input holds: the chunks it retained and the vector that carries them.
|
||||
///
|
||||
/// The retained figure is [`Self::held`], which is folded in chunk by chunk, and the vector is
|
||||
/// counted by the capacity it really holds -- the capacity `retain_quantile` reserved before it
|
||||
/// grew -- so this is what the node has to reserve rather than a second estimate of it.
|
||||
///
|
||||
/// The two do not overlap. A chunk's `Series` is carried inside one slot of that vector, so
|
||||
/// `held` counts what the chunk holds beside the struct ([`chunk_bytes_beside_samples`]) and the
|
||||
/// capacity here counts every slot the vector owns, occupied or not. Counting the struct in both
|
||||
/// would charge each occupied slot twice; counting it in neither would leave every slot the
|
||||
/// vector holds uncovered.
|
||||
fn bytes(&self) -> usize {
|
||||
std::mem::size_of::<Self>()
|
||||
.saturating_add(
|
||||
self.series
|
||||
.capacity()
|
||||
.saturating_mul(std::mem::size_of::<Series>()),
|
||||
)
|
||||
.saturating_add(self.held)
|
||||
}
|
||||
}
|
||||
|
||||
/// One aggregate node's states, from the first chunk it is handed to the series it finishes with.
|
||||
pub(crate) struct AggregateRuntime {
|
||||
/// What the node computes, as the tree bound it: the kind it folds by and the stage it reports.
|
||||
@@ -62,15 +169,18 @@ pub(crate) struct AggregateRuntime {
|
||||
/// addressed on. A `Complete` aggregate neither adds nor drops a field, and it evaluates on its
|
||||
/// input's grid, so one schema describes both sides.
|
||||
schema: Arc<SeriesSchema>,
|
||||
/// The kind this node folds by, as the runtime's own enum, so the chunk check can ask what the
|
||||
/// operation accepts.
|
||||
fold_op: AggregateOp,
|
||||
/// What this node keeps between chunks, as the runtime's own enum, so the chunk check can ask
|
||||
/// what the operation accepts.
|
||||
fold: Fold,
|
||||
/// The query's annotation collector, once the driver installed it. Only an operation that skips
|
||||
/// histogram samples has anything to say, and it says it once.
|
||||
annotations: Option<PromqlAnnotationCollector>,
|
||||
/// The fold. `None` once [`Self::compute`] has taken it, which is also what says the node has
|
||||
/// nothing left to fold into.
|
||||
accumulator: Option<FloatAggregator>,
|
||||
/// The whole input of a `quantile` aggregate. `None` for every other kind, and `None` once the
|
||||
/// kernel has answered from it.
|
||||
retained: Option<QuantileInput>,
|
||||
merger: Option<FloatMerger>,
|
||||
state_schema: Option<Arc<AggregateStateSchema>>,
|
||||
/// Whether the fold has been finished and its series handed over. A node computes once: a second
|
||||
@@ -84,15 +194,16 @@ pub(crate) struct AggregateRuntime {
|
||||
impl AggregateRuntime {
|
||||
/// The PromQL name of the aggregate this node folds, as an annotation names it.
|
||||
fn aggregate_name(&self) -> &'static str {
|
||||
match self.fold_op {
|
||||
AggregateOp::Sum => "sum",
|
||||
AggregateOp::Count => "count",
|
||||
AggregateOp::Avg => "avg",
|
||||
AggregateOp::Min => "min",
|
||||
AggregateOp::Max => "max",
|
||||
AggregateOp::Group => "group",
|
||||
AggregateOp::Stddev => "stddev",
|
||||
AggregateOp::Stdvar => "stdvar",
|
||||
match self.fold {
|
||||
Fold::Pointwise(AggregateOp::Sum) => "sum",
|
||||
Fold::Pointwise(AggregateOp::Count) => "count",
|
||||
Fold::Pointwise(AggregateOp::Avg) => "avg",
|
||||
Fold::Pointwise(AggregateOp::Min) => "min",
|
||||
Fold::Pointwise(AggregateOp::Max) => "max",
|
||||
Fold::Pointwise(AggregateOp::Group) => "group",
|
||||
Fold::Pointwise(AggregateOp::Stddev) => "stddev",
|
||||
Fold::Pointwise(AggregateOp::Stdvar) => "stdvar",
|
||||
Fold::Quantile => "quantile",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,10 +239,11 @@ impl AggregateRuntime {
|
||||
AggregateKind::Group => AggregateOp::Group,
|
||||
AggregateKind::Stddev => AggregateOp::Stddev,
|
||||
AggregateKind::Stdvar => AggregateOp::Stdvar,
|
||||
other => {
|
||||
return Err(DataFusionError::NotImplemented(format!(
|
||||
"the native runtime does not execute the `{other:?}` aggregate yet: only the supported `Complete` aggregate kinds are computed here"
|
||||
)));
|
||||
// The one kind with no accumulator: it keeps its input instead, which is a state of its
|
||||
// own and is built by [`Self::new_quantile`]. The φ is the only parameter an aggregate
|
||||
// carries, and the arm that names the kind is the one place it is read.
|
||||
AggregateKind::Quantile { quantile } => {
|
||||
return Self::new_quantile(op, quantile, schema, buffer);
|
||||
}
|
||||
};
|
||||
let (state_kind, state_components) = aggregate_state_binding(op.kind).ok_or_else(|| {
|
||||
@@ -151,10 +263,11 @@ impl AggregateRuntime {
|
||||
buffer.resize(merger.state_bytes())?;
|
||||
return Ok(Self {
|
||||
op,
|
||||
fold_op: kind,
|
||||
fold: Fold::Pointwise(kind),
|
||||
annotations: None,
|
||||
schema,
|
||||
accumulator: None,
|
||||
retained: None,
|
||||
merger: Some(merger),
|
||||
state_schema,
|
||||
computed: false,
|
||||
@@ -182,26 +295,79 @@ impl AggregateRuntime {
|
||||
buffer.resize(initial.state_bytes())?;
|
||||
Ok(Self {
|
||||
op,
|
||||
fold_op: kind,
|
||||
fold: Fold::Pointwise(kind),
|
||||
annotations: None,
|
||||
schema,
|
||||
accumulator: Some(initial),
|
||||
retained: None,
|
||||
merger: None,
|
||||
state_schema,
|
||||
computed: false,
|
||||
})
|
||||
}
|
||||
|
||||
/// What folding `chunk` reserves while it is folded.
|
||||
/// The states of a `Complete` `quantile` aggregate over `schema`, whose φ is `quantile`.
|
||||
///
|
||||
/// The chunk is not retained: it is folded into the states and dropped at the end of the call,
|
||||
/// so what this node holds afterwards is the fold and none of the input. The charge is therefore
|
||||
/// the chunk's own bytes -- the figure [`evaluated_bytes`] reports, which is one pass over the
|
||||
/// chunk and no state between chunks -- and not the `operand_chunk_bytes` figure a binary
|
||||
/// operand keeps, which exists because such an operand holds a series for the whole execution
|
||||
/// and so must count the slices of one buffer instead of the buffer once per chunk.
|
||||
/// A `quantile` is answered from its group's whole sample set, so a stage that saw only part of
|
||||
/// a group would answer the quantile of a subset of it: the quantile of two partitions'
|
||||
/// quantiles is not the quantile of their union, and there is no state to export or merge
|
||||
/// either. That is a property of the operation and not of this runtime, so what refuses a
|
||||
/// `Partial` or a `Final` one is the tree's own check -- [`aggregate_state_binding`] has no
|
||||
/// binding for a `quantile`, which [`crate::native::plan::NativeNode::validate`] reports, and
|
||||
/// [`crate::native::split`] declines a `quantile` for the same reason -- and this starts the one
|
||||
/// stage that is left.
|
||||
///
|
||||
/// The same check is what puts the input on a grid: a `Complete` aggregate is validated to
|
||||
/// answer its input's values on its input's grid, and `schema` is the layout the node reports,
|
||||
/// so the grid the kernel reads its parameter on is there before this runs.
|
||||
fn new_quantile(
|
||||
op: BoundAggregate,
|
||||
quantile: f64,
|
||||
schema: Arc<SeriesSchema>,
|
||||
buffer: &mut NodeBuffer,
|
||||
) -> Result<Self> {
|
||||
let retained = QuantileInput {
|
||||
quantile,
|
||||
series: Vec::new(),
|
||||
tables: HashSet::new(),
|
||||
buffers: HashSet::new(),
|
||||
held: 0,
|
||||
};
|
||||
buffer.resize(retained.bytes())?;
|
||||
Ok(Self {
|
||||
op,
|
||||
fold: Fold::Quantile,
|
||||
annotations: None,
|
||||
schema,
|
||||
accumulator: None,
|
||||
retained: Some(retained),
|
||||
merger: None,
|
||||
state_schema: None,
|
||||
computed: false,
|
||||
})
|
||||
}
|
||||
|
||||
/// What consuming `chunk` reserves while it is consumed.
|
||||
///
|
||||
/// A point-wise fold drops the chunk at the end of the call and keeps only its states, so the
|
||||
/// charge is the chunk's own bytes -- the figure [`evaluated_bytes`] reports, which is one pass
|
||||
/// over the chunk and no state between chunks -- and not the `operand_chunk_bytes` figure a
|
||||
/// binary operand keeps, which exists because such an operand holds a series for the whole
|
||||
/// execution and so must count the slices of one buffer instead of the buffer once per chunk.
|
||||
///
|
||||
/// A `quantile` keeps the chunk instead, and what it holds is not the chunk alone: it is the
|
||||
/// allocations the chunk's samples live in, billed once each, beside the chunk's own descriptors
|
||||
/// ([`Self::retain_quantile`], which is [`chunk_bytes_beside_samples`] plus
|
||||
/// [`unbilled_sample_buffers`] plus [`unbilled_tables`]). That figure is read from the chunk by
|
||||
/// the retainer itself, before it retains it, so nothing is held before it is reserved. Booking
|
||||
/// the chunk again here would reserve the same backing twice -- once for the chunk being handed
|
||||
/// over and once for the allocations it points into -- which is not a safer estimate but a
|
||||
/// refusal of an input whose own figure the pool admits.
|
||||
pub(crate) fn pending(&self, chunk: &EvaluatedChunk) -> usize {
|
||||
evaluated_bytes(chunk)
|
||||
match self.fold {
|
||||
Fold::Pointwise(_) => evaluated_bytes(chunk),
|
||||
Fold::Quantile => 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Folds one chunk into its group.
|
||||
@@ -209,18 +375,24 @@ impl AggregateRuntime {
|
||||
/// A chunk that closes its series closes nothing here: a group is not a series, so the last
|
||||
/// contribution of one member says nothing about the group. The fold keeps its states, which is
|
||||
/// what makes a group's answer its whole group's answer once the input ends.
|
||||
///
|
||||
/// A `quantile` keeps the chunk itself, for the same reason: the answer is the group's sample set,
|
||||
/// so the samples have to survive until the input does.
|
||||
pub(crate) fn accumulate(
|
||||
&mut self,
|
||||
chunk: EvaluatedChunk,
|
||||
buffer: &mut NodeBuffer,
|
||||
pending: usize,
|
||||
) -> Result<()> {
|
||||
let Fold::Pointwise(kind) = self.fold else {
|
||||
return self.retain_quantile(chunk, buffer);
|
||||
};
|
||||
// `Series` keeps its fields public, so a chunk can be built by struct literal against
|
||||
// another schema; the states are indexed by absolute position on this node's grid, so a
|
||||
// chunk of another field count or another grid would be folded into the wrong places.
|
||||
validate_aggregate_series(&self.schema, &chunk.series, self.fold_op)
|
||||
validate_aggregate_series(&self.schema, &chunk.series, kind)
|
||||
.map_err(|error| folded_chunk(&self.describe(), error))?;
|
||||
if histogram_samples(self.fold_op) == HistogramSamples::Ignored
|
||||
if histogram_samples(kind) == HistogramSamples::Ignored
|
||||
&& !crate::map::is_float_series(&chunk.series)
|
||||
&& let Some(collector) = &self.annotations
|
||||
{
|
||||
@@ -244,6 +416,79 @@ impl AggregateRuntime {
|
||||
buffer.resize(accumulator.state_bytes())
|
||||
}
|
||||
|
||||
/// Keeps one chunk of a `quantile`'s input until the input ends.
|
||||
///
|
||||
/// The chunk is kept whole rather than folded: the answer is the φ-quantile of the group's sample
|
||||
/// set, and no fixed-width state holds a sample set. What it costs is measured the way a binary
|
||||
/// operand measures the chunks it retains, and for the same reason: what a `quantile` keeps is a
|
||||
/// series for the rest of the execution, so the allocation one buffer its chunks are slices of is
|
||||
/// counted once rather than once per chunk -- and counted at all when the chunk holds a small
|
||||
/// slice of it, because the allocation is what stays resident. It is the chunk's own descriptors
|
||||
/// ([`chunk_bytes_beside_samples`]) beside those allocations: the Arrow buffers its samples live
|
||||
/// in, once each ([`unbilled_sample_buffers`]), and the histogram tables they index into, once
|
||||
/// each ([`unbilled_tables`]). The figure is reserved before the push -- the push is what makes
|
||||
/// the node hold it -- and the reservation then settles on the whole input, which is what this
|
||||
/// node holds between chunks.
|
||||
fn retain_quantile(&mut self, chunk: EvaluatedChunk, buffer: &mut NodeBuffer) -> Result<()> {
|
||||
// A quantile reads float values and skips a histogram sample, so a chunk only has to fit the
|
||||
// schema and the grid -- exactly what the kernel's own input check asks for.
|
||||
validate_grid_series(&self.schema, &chunk.series)
|
||||
.map_err(|error| folded_chunk(&self.describe(), error))?;
|
||||
if !crate::map::is_float_series(&chunk.series)
|
||||
&& let Some(collector) = &self.annotations
|
||||
{
|
||||
// Prometheus reports once that it skipped samples it cannot order; the kernel folds the
|
||||
// float samples it can read.
|
||||
collector.record_info(format!(
|
||||
"{}: ignored native histogram samples",
|
||||
self.aggregate_name()
|
||||
));
|
||||
self.annotations = None;
|
||||
}
|
||||
let Some(retained) = self.retained.as_mut() else {
|
||||
return Err(no_fold(&self.describe()));
|
||||
};
|
||||
// What this chunk adds to what the node holds: the allocations its samples live in and the
|
||||
// histogram tables it is the first chunk to reference -- each billed once, because one
|
||||
// allocation is one however many chunks slice it -- plus the descriptors the chunk keeps.
|
||||
let (buffers, buffer_bytes) = unbilled_sample_buffers(&chunk.series, &retained.buffers);
|
||||
let (tables, table_bytes) = unbilled_tables(&chunk.series, &retained.tables);
|
||||
let bytes = chunk_bytes_beside_samples(&chunk.series)
|
||||
.saturating_add(buffer_bytes)
|
||||
.saturating_add(table_bytes);
|
||||
buffer.grow(bytes)?;
|
||||
// The slot the chunk is about to take, and - when the vector has to grow for it - the
|
||||
// capacity it grows to. `Vec::push` doubles, so booking one slot would let the real
|
||||
// allocation outrun what the pool admitted; the target is booked instead, and the settle
|
||||
// below then reads back the exact capacity the vector holds. The vector's slots are not part
|
||||
// of `held`: [`QuantileInput::bytes`] counts them from that capacity, so booking them here
|
||||
// and folding them there would charge them twice.
|
||||
let needed = retained.series.len().saturating_add(1);
|
||||
if needed > retained.series.capacity() {
|
||||
let target = retained.series.capacity().saturating_mul(2).max(needed);
|
||||
buffer.grow(
|
||||
target
|
||||
.saturating_sub(retained.series.capacity())
|
||||
.saturating_mul(std::mem::size_of::<Series>()),
|
||||
)?;
|
||||
retained
|
||||
.series
|
||||
.try_reserve_exact(target.saturating_sub(retained.series.len()))
|
||||
.map_err(|error| {
|
||||
DataFusionError::Execution(format!(
|
||||
"cannot retain the `quantile` input: {error}"
|
||||
))
|
||||
})?;
|
||||
}
|
||||
retained.series.push(chunk.series);
|
||||
// From here the node keeps those tables and allocations alive itself, so a chunk arriving
|
||||
// later that references one is charged its own descriptors and not the allocation again.
|
||||
retained.tables.extend(tables);
|
||||
retained.buffers.extend(buffers);
|
||||
retained.held = retained.held.saturating_add(bytes);
|
||||
buffer.resize(retained.bytes())
|
||||
}
|
||||
|
||||
/// Merges one state chunk into this final aggregate.
|
||||
pub(crate) fn accumulate_state(
|
||||
&mut self,
|
||||
@@ -296,7 +541,15 @@ impl AggregateRuntime {
|
||||
}
|
||||
|
||||
/// The number of retained groups, for budgeting the output queue backing storage.
|
||||
///
|
||||
/// A point-wise fold knows its groups because it keyed them. A `quantile` has not grouped
|
||||
/// anything: the kernel does that when it answers. What it reports here is therefore the number
|
||||
/// of members it retained, which is an upper bound -- every group has at least one member -- and
|
||||
/// only ever sizes a reservation.
|
||||
pub(crate) fn groups(&self) -> usize {
|
||||
if let Some(input) = self.retained.as_ref() {
|
||||
return input.series.len();
|
||||
}
|
||||
self.accumulator.as_ref().map_or(0, FloatAggregator::groups)
|
||||
}
|
||||
|
||||
@@ -314,6 +567,10 @@ impl AggregateRuntime {
|
||||
return Ok(None);
|
||||
}
|
||||
self.computed = true;
|
||||
if let Some(input) = self.retained.take() {
|
||||
let output = self.finish_quantile(input, buffer, queue_bytes)?;
|
||||
return Ok(Some(AggregateOutput::Samples(output)));
|
||||
}
|
||||
if let Some(accumulator) = self.accumulator.take() {
|
||||
return match self.op.stage {
|
||||
AggregateStage::Complete => {
|
||||
@@ -353,6 +610,187 @@ impl AggregateRuntime {
|
||||
}
|
||||
Err(no_fold(&self.describe()))
|
||||
}
|
||||
|
||||
/// Answers a `quantile` from the input it kept, and hands the series over.
|
||||
///
|
||||
/// The parameter is the number the query wrote, so every point it covers carries it: the kernel
|
||||
/// reads `q` per point from an unlabelled single-field scalar on the aggregate's own grid, and a
|
||||
/// point that scalar has no value at is a point with no result. That case cannot arise from a
|
||||
/// constant, but the kernel keeps the rule for the parameter it is written for.
|
||||
///
|
||||
/// The parameter covers the points the input holds and not the window they sit in: the kernel
|
||||
/// reads `q` only where a member has a sample, so a sparse input's parameter is as wide as its
|
||||
/// rows. A parameter over the envelope would instead allocate every position between two
|
||||
/// far-apart points -- a span sized by the statement's range rather than by the data it reads,
|
||||
/// which the pool never admitted. The parameter is booked from the bound its rows give before it
|
||||
/// exists, and settles on its exact figure once it is built.
|
||||
///
|
||||
/// The reservation covers what is held while the kernel runs -- the input, that parameter, and
|
||||
/// the kernel's workspace -- and then settles on the series the kernel answered, exactly as a
|
||||
/// point-wise fold's finish does. The input is what stops being held when the kernel is done with
|
||||
/// it, so it is dropped before the settle rather than when this call returns: a settle read
|
||||
/// beside a live input would name a set that is never held, and the figure it left behind would
|
||||
/// be the one the next node's admission had to fit into.
|
||||
fn finish_quantile(
|
||||
&self,
|
||||
input: QuantileInput,
|
||||
buffer: &mut NodeBuffer,
|
||||
queue_bytes: usize,
|
||||
) -> Result<Vec<Series>> {
|
||||
let grid = self.schema.eval_grid.clone().ok_or_else(|| {
|
||||
DataFusionError::Execution(
|
||||
"a `quantile` aggregate needs an evaluated grid to read its parameter on".into(),
|
||||
)
|
||||
})?;
|
||||
let workspace = quantile_workspace_bytes(&self.schema, &input);
|
||||
// Admitted before the parameter exists, because building it is what makes this node hold
|
||||
// it: the bound is what a parameter over the input's points can take, and the resize below
|
||||
// settles on what it did take.
|
||||
buffer.resize(
|
||||
input
|
||||
.bytes()
|
||||
.saturating_add(quantile_parameter_bound_bytes(&input))
|
||||
.saturating_add(workspace)
|
||||
.saturating_add(queue_bytes),
|
||||
)?;
|
||||
let positions = quantile_positions(&input);
|
||||
let parameter = quantile_parameter(&grid, input.quantile, &positions)?;
|
||||
// The collected positions are what the bound above covered and what this node no longer
|
||||
// holds, so they go before the figure settles.
|
||||
drop(positions);
|
||||
let held = input
|
||||
.bytes()
|
||||
.saturating_add(series_bytes(¶meter))
|
||||
.saturating_add(workspace);
|
||||
buffer.resize(held.saturating_add(queue_bytes))?;
|
||||
let names: Vec<&str> = match &self.op.group {
|
||||
BoundGrouping::By(labels) | BoundGrouping::Without(labels) => {
|
||||
labels.iter().map(|label| label.as_ref()).collect()
|
||||
}
|
||||
};
|
||||
let grouping = match &self.op.group {
|
||||
BoundGrouping::By(_) => Grouping::By(&names),
|
||||
BoundGrouping::Without(_) => Grouping::Without(&names),
|
||||
};
|
||||
let output = aggregate_quantile(&self.schema, &input.series, ¶meter, grouping)
|
||||
.map_err(|error| folded_chunk(&self.describe(), error))?;
|
||||
drop(names);
|
||||
drop(parameter);
|
||||
// The input is what this node held until the kernel answered, and it is given back before
|
||||
// the reservation settles on the answer: the settle below is what the node holds from then
|
||||
// on, so it must be read from the set that is really alive -- the answer and the queue --
|
||||
// rather than from the answer beside an input this call only drops on its way out. The
|
||||
// kernel's answer holds nothing of the input: the builder copies the samples it keeps.
|
||||
drop(input);
|
||||
let output_vec_bytes = output
|
||||
.capacity()
|
||||
.saturating_mul(std::mem::size_of::<Series>());
|
||||
charge_samples(buffer, &output, output_vec_bytes, queue_bytes)?;
|
||||
Ok(output)
|
||||
}
|
||||
}
|
||||
|
||||
/// The field the `quantile` parameter is bound under, which the kernel reads as its scalar.
|
||||
const QUANTILE_PARAMETER_FIELD: &str = "scalar";
|
||||
|
||||
/// The points the retained input holds, once each, in ascending order.
|
||||
///
|
||||
/// A group's answer is decided at the points at least one of its members holds a sample at, and its
|
||||
/// members are drawn from the input, so these are every point the kernel can ask its parameter for.
|
||||
/// They are the positions the input's rows sit at -- not the window they span, which two far-apart
|
||||
/// points make as wide as the grid.
|
||||
fn quantile_positions(input: &QuantileInput) -> Vec<usize> {
|
||||
union_positions(input.series.iter().map(|series| &series.time)).collect()
|
||||
}
|
||||
|
||||
/// The parameter a `quantile` reads at each point: the φ the query wrote, unlabelled and single
|
||||
/// field, at `positions` and nowhere else.
|
||||
///
|
||||
/// The kernel reads `q` per point and treats a point it has no value at as a point with no result,
|
||||
/// so the parameter has to cover exactly the points that can be asked for and may omit every other
|
||||
/// one. [`GridSeriesBuilder`] is what makes that cheap: the axis keeps the runs of consecutive
|
||||
/// positions rather than a row per position, so two far-apart points cost two rows and two spans
|
||||
/// instead of every position between them.
|
||||
///
|
||||
/// An input holding no point has nothing to decide, which the kernel answers with no output: an
|
||||
/// empty scalar over the grid says the same thing and costs nothing.
|
||||
fn quantile_parameter(grid: &Arc<EvalGrid>, quantile: f64, positions: &[usize]) -> Result<Series> {
|
||||
let schema = Arc::new(SeriesSchema {
|
||||
fields: Arc::from([FieldSpec {
|
||||
name: QUANTILE_PARAMETER_FIELD.into(),
|
||||
}]),
|
||||
eval_grid: Some(Arc::clone(grid)),
|
||||
});
|
||||
let metric = SeriesLabels::try_new(None, Vec::new())
|
||||
.map_err(|error| DataFusionError::External(Box::new(error)))?;
|
||||
let mut builder = GridSeriesBuilder::try_new(Arc::clone(&schema))?;
|
||||
for position in positions {
|
||||
builder.push(*position, &[Some(quantile)])?;
|
||||
}
|
||||
match builder.finish(metric)? {
|
||||
Some(parameter) => Ok(parameter),
|
||||
None => constant_series(
|
||||
&schema,
|
||||
GridRange::try_new(Arc::clone(grid), 0, 0)
|
||||
.map_err(|error| DataFusionError::External(Box::new(error)))?,
|
||||
quantile,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// The most a `quantile` parameter over `input` can cost, before it is built.
|
||||
///
|
||||
/// The parameter holds one value and one position per point it covers, and one span per run of
|
||||
/// them; a run is at least one point long, so the input's rows bound all of those. The figure is
|
||||
/// what the pool admits ahead of the allocation, and [`series_bytes`] is what it settles on once
|
||||
/// the parameter exists: what is admitted is what the parameter really takes, wherever its points
|
||||
/// sit on the grid.
|
||||
fn quantile_parameter_bound_bytes(input: &QuantileInput) -> usize {
|
||||
let rows: usize = input.series.iter().map(|series| series.time.len()).sum();
|
||||
let per_point = std::mem::size_of::<usize>() // its position, while the points are collected
|
||||
.saturating_add(std::mem::size_of::<f64>()) // the value it carries
|
||||
.saturating_add(2 * std::mem::size_of::<usize>()) // the span of a run of one point
|
||||
.saturating_add(std::mem::size_of::<f64>()); // the array's validity slot for it
|
||||
std::mem::size_of::<Series>()
|
||||
.saturating_add(std::mem::size_of::<TimeAxis>())
|
||||
.saturating_add(rows.saturating_mul(per_point))
|
||||
}
|
||||
|
||||
/// The bytes the `quantile` kernel needs beside its retained input while it answers.
|
||||
///
|
||||
/// [`aggregate_quantile`] reports no workspace as it allocates, so this is an estimate reserved once
|
||||
/// before it runs, in the same spirit as the whole-vector kernels'. What it covers: the state it
|
||||
/// keeps per member (one position list, one heap entry and one cursor per field), its own copy of the
|
||||
/// parameter (one `(position, φ)` pair per point the parameter covers, which the input's rows bound
|
||||
/// because the parameter is only built over the points the input holds), the temporaries of one grid
|
||||
/// point (its members' values, the copy the kernel sorts, and one row of the width it folds), and the
|
||||
/// rows it builds -- the group being built and the groups it has already handed over. The row count is
|
||||
/// the input's own: a group's rows are the points at least one of its members holds a sample at, so
|
||||
/// the input's rows bound every group's rows and their sum.
|
||||
fn quantile_workspace_bytes(schema: &SeriesSchema, input: &QuantileInput) -> usize {
|
||||
let width = schema.fields.len();
|
||||
let members = input.series.len();
|
||||
let rows: usize = input.series.iter().map(|series| series.time.len()).sum();
|
||||
let per_member = std::mem::size_of::<(usize, usize, usize)>()
|
||||
.saturating_add(std::mem::size_of::<usize>())
|
||||
.saturating_add(width.saturating_mul(4 * std::mem::size_of::<usize>()));
|
||||
let per_point = members
|
||||
.saturating_mul(2 * std::mem::size_of::<f64>())
|
||||
.saturating_add(width.saturating_mul(std::mem::size_of::<Option<f64>>()));
|
||||
// One row, charged twice: the builder holds one group at a time and the series it has already
|
||||
// handed over are held beside it.
|
||||
let per_row = 2 * width
|
||||
.saturating_mul(std::mem::size_of::<f64>())
|
||||
.saturating_add(2 * std::mem::size_of::<usize>())
|
||||
.saturating_add(1);
|
||||
// The parameter copy, charged twice for the capacity a growing vector keeps over its length.
|
||||
let parameter_copy = 2 * rows.saturating_mul(std::mem::size_of::<(usize, f64)>());
|
||||
members
|
||||
.saturating_mul(per_member)
|
||||
.saturating_add(per_point)
|
||||
.saturating_add(rows.saturating_mul(per_row))
|
||||
.saturating_add(parameter_copy)
|
||||
.saturating_add(members.saturating_mul(std::mem::size_of::<Series>()))
|
||||
}
|
||||
|
||||
/// Charges the series a finished aggregate hands over, plus the queue they enter.
|
||||
|
||||
@@ -26,7 +26,7 @@ use common_query::promql_annotations::PromqlAnnotationCollector;
|
||||
use datafusion::error::{DataFusionError, Result};
|
||||
use datafusion::execution::memory_pool::{MemoryConsumer, MemoryPool, MemoryReservation};
|
||||
use datafusion::physical_plan::metrics::ExecutionPlanMetricsSet;
|
||||
use datatypes::arrow::array::Float64Array;
|
||||
use datatypes::arrow::array::{Array, ArrowPrimitiveType, Float64Array, PrimitiveArray};
|
||||
use datatypes::arrow::buffer::{Buffer, ScalarBuffer};
|
||||
use datatypes::series::histogram::HistogramView;
|
||||
use datatypes::series::raw::{RawSeriesChunk, RawTimestamps};
|
||||
@@ -159,13 +159,43 @@ pub(super) fn evaluated_bytes(chunk: &EvaluatedChunk) -> usize {
|
||||
std::mem::size_of::<EvaluatedChunk>().saturating_add(fields_bytes(&chunk.series.fields))
|
||||
}
|
||||
|
||||
/// The bytes one chunk of a binary operand holds on its own: [`series_bytes`] with the samples its
|
||||
/// rows occupy in place of the allocations they are slices of.
|
||||
/// The bytes one chunk holds beside its samples: its axis, the runs that axis is made of, one
|
||||
/// descriptor per segment and its labels.
|
||||
///
|
||||
/// Histogram tables are excluded here; [`unbilled_tables`] bills them once per operand.
|
||||
pub(crate) fn operand_chunk_bytes(series: &Series) -> usize {
|
||||
/// It is what is left of [`series_bytes`] once the allocations its samples are slices of are taken
|
||||
/// out and the `Series` struct itself is left to whatever holds the chunk: the struct is one slot of
|
||||
/// the container a retainer keeps its chunks in, so a retainer that counts that container -- a
|
||||
/// `quantile` counts its vector by capacity -- has already counted it, and one that does not count
|
||||
/// it anywhere would have to add it back.
|
||||
///
|
||||
/// This is the figure of a retainer that bills the allocations its samples live in itself
|
||||
/// ([`unbilled_sample_buffers`]): what the chunk holds on its own, plus that residency, once each.
|
||||
pub(crate) fn chunk_bytes_beside_samples(series: &Series) -> usize {
|
||||
series_bytes(series)
|
||||
.saturating_sub(fields_bytes(&series.fields))
|
||||
.saturating_sub(std::mem::size_of::<Series>())
|
||||
}
|
||||
|
||||
/// The bytes one chunk a binary operand retains holds on its own.
|
||||
///
|
||||
/// It is [`chunk_bytes_beside_samples`] with the samples a chunk's rows occupy in place of the
|
||||
/// allocations they are slices of, and with the struct the chunk's `Series` carries: a series
|
||||
/// delivered in a thousand chunks holds a thousand slices of one buffer, so counting that buffer
|
||||
/// once per chunk would make the operand's figure grow with the number of chunks rather than with
|
||||
/// the data in them. The slices of one series add up to the allocation they share, which is the
|
||||
/// figure a deduplication across the chunks would arrive at.
|
||||
///
|
||||
/// The struct is part of this figure because an operand keeps one entry per retained chunk and
|
||||
/// counts no container of its own, so the entry and the chunk in it are one figure here. A retainer
|
||||
/// that does count its container takes [`chunk_bytes_beside_samples`] instead.
|
||||
///
|
||||
/// The histogram tables a chunk's segments reference are not part of the figure: a table is one
|
||||
/// allocation however many chunks index into it, so it is not a per-chunk cost at all. Dropping it
|
||||
/// here is therefore only half the story -- it must not become a figure the operand never charges.
|
||||
/// [`unbilled_tables`] is the other half, and the operand bills each table once through it.
|
||||
pub(crate) fn operand_chunk_bytes(series: &Series) -> usize {
|
||||
chunk_bytes_beside_samples(series)
|
||||
.saturating_add(std::mem::size_of::<Series>())
|
||||
.saturating_add(sample_slice_bytes(&series.fields))
|
||||
}
|
||||
|
||||
@@ -202,11 +232,122 @@ fn sample_slice_bytes(fields: &SeriesFields) -> usize {
|
||||
.fold(0usize, usize::saturating_add)
|
||||
}
|
||||
|
||||
/// The Arrow allocations `series`' samples live in that `billed` does not hold yet, and the bytes
|
||||
/// they hold.
|
||||
///
|
||||
/// [`sample_slice_bytes`] counts the rows a chunk holds, which is the figure of a caller that bills
|
||||
/// the samples without the allocations they are slices of. That is exact only when the chunks of a
|
||||
/// series are non-overlapping slices that add up to the buffers they point into. It is wrong in both
|
||||
/// directions otherwise: a chunk holding one row of a large batch keeps the whole buffer resident
|
||||
/// while its rows are eight bytes, and two chunks whose slices overlap hold the bytes between them
|
||||
/// once while each is charged for them. What is resident is the allocation, and an allocation is one
|
||||
/// however many chunks slice it, so it is counted once, by the first chunk to reference it --
|
||||
/// exactly as [`unbilled_tables`] counts a histogram table.
|
||||
///
|
||||
/// Counting every chunk for the whole buffer it slices would need no identity at all, but it
|
||||
/// over-estimates by the number of chunks: a series handed over as a thousand slices of one buffer
|
||||
/// would be charged that buffer a thousand times, which refuses an input whose buffers do fit the
|
||||
/// pool. The identity is therefore read, and read from Arrow's own handle on the allocation
|
||||
/// ([`Buffer::data_ptr`]): every slice of one buffer reports the address of the allocation it points
|
||||
/// into, whatever its offset inside it, and the address names that allocation for as long as it is
|
||||
/// remembered -- a chunk referencing a buffer keeps it alive, so no later allocation can be given
|
||||
/// the same address. The bytes are the buffer's capacity, which is the allocation and not the slice,
|
||||
/// plus the one slot the identity takes in the set that remembers it. The caller counts the figure
|
||||
/// as it stands and adds the identities to `billed` once the chunk is retained, which is what makes
|
||||
/// the two halves agree.
|
||||
///
|
||||
/// Every array a chunk holds samples in contributes: the values of a float segment, the indices of a
|
||||
/// histogram one, both of a mixed one, and the validity bitmap beside each of them -- a bitmap is an
|
||||
/// allocation of its own, and no figure that counts rows can see it. A buffer two segments or two
|
||||
/// fields of the one chunk reference is listed once.
|
||||
pub(crate) fn unbilled_sample_buffers(
|
||||
series: &Series,
|
||||
billed: &HashSet<usize>,
|
||||
) -> (Vec<usize>, usize) {
|
||||
let mut fresh: Vec<usize> = Vec::new();
|
||||
let mut bytes = 0usize;
|
||||
for field in series.fields.iter() {
|
||||
for segment in field.segments() {
|
||||
match segment.dense() {
|
||||
Some(DenseSamplesRef::Float(values)) => {
|
||||
unbilled_array_buffers(values, &mut fresh, billed, &mut bytes)
|
||||
}
|
||||
Some(DenseSamplesRef::Histogram(histograms)) => {
|
||||
unbilled_array_buffers(histograms.indices(), &mut fresh, billed, &mut bytes)
|
||||
}
|
||||
Some(DenseSamplesRef::Mixed(mixed)) => {
|
||||
unbilled_array_buffers(mixed.floats(), &mut fresh, billed, &mut bytes);
|
||||
unbilled_array_buffers(
|
||||
mixed.histograms().indices(),
|
||||
&mut fresh,
|
||||
billed,
|
||||
&mut bytes,
|
||||
);
|
||||
}
|
||||
// `segments` hands out dense segments only, so a nested chunk never reaches here.
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
(fresh, bytes)
|
||||
}
|
||||
|
||||
/// Adds the allocation a sample array's values live in, and the one its validity bitmap lives in,
|
||||
/// to `fresh` and `bytes`, skipping the ones `billed` holds and the ones already reported.
|
||||
fn unbilled_array_buffers<T: ArrowPrimitiveType>(
|
||||
values: &PrimitiveArray<T>,
|
||||
fresh: &mut Vec<usize>,
|
||||
billed: &HashSet<usize>,
|
||||
bytes: &mut usize,
|
||||
) {
|
||||
unbilled_buffer(values.values().inner(), fresh, billed, bytes);
|
||||
if let Some(nulls) = values.nulls() {
|
||||
unbilled_buffer(nulls.buffer(), fresh, billed, bytes);
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds the allocation `buffer` is a slice of to `fresh` and `bytes`, once.
|
||||
fn unbilled_buffer(
|
||||
buffer: &Buffer,
|
||||
fresh: &mut Vec<usize>,
|
||||
billed: &HashSet<usize>,
|
||||
bytes: &mut usize,
|
||||
) {
|
||||
let held = buffer_bytes(buffer);
|
||||
if held == 0 {
|
||||
// An array whose values are all valid carries an empty bitmap, which owns nothing.
|
||||
return;
|
||||
}
|
||||
let identity = buffer.data_ptr().as_ptr() as usize;
|
||||
if billed.contains(&identity) || fresh.contains(&identity) {
|
||||
return;
|
||||
}
|
||||
fresh.push(identity);
|
||||
*bytes = bytes
|
||||
.saturating_add(held)
|
||||
.saturating_add(std::mem::size_of::<usize>());
|
||||
}
|
||||
|
||||
/// The histogram tables `series` is the first to reference, and the bytes they hold.
|
||||
///
|
||||
/// The caller adds these identities to `billed` when it retains the chunk, so one allocation several
|
||||
/// chunks index into is billed once. The identity is the address of the allocation behind the `Arc`.
|
||||
fn unbilled_tables(series: &Series, billed: &HashSet<usize>) -> (Vec<usize>, usize) {
|
||||
/// A table is one allocation the chunks of a series share, so it is neither a per-chunk cost --
|
||||
/// counting it per chunk would make a series delivered in a thousand chunks cost a thousand tables
|
||||
/// -- nor no cost at all, which is what leaving it out of every chunk leaves the figure at. What it
|
||||
/// is, is a cost of the operand that first retains a chunk referencing it: the identities it returns
|
||||
/// are the tables `billed` does not hold yet, and the figure is what those tables hold
|
||||
/// ([`table_bytes`]). The caller bills the figure as it stands and adds the identities to `billed`
|
||||
/// once the chunk is retained, which is what makes the two halves of the figure agree.
|
||||
///
|
||||
/// The identity is the address of the allocation behind the `Arc`. It names one table for as long as
|
||||
/// it is remembered, because a chunk referencing a table keeps it alive and no later allocation can
|
||||
/// be given the same address. A table two segments or two fields of the one chunk reference is
|
||||
/// listed once. Each identity is also one slot in the set that remembers it, which is charged as it
|
||||
/// is written -- one entry at a time, like the boundary list's, and not from the capacity the set
|
||||
/// grows to -- so remembering a table is paid for beside the table itself.
|
||||
///
|
||||
/// Both nodes that retain chunks instead of folding them read this: a binary operand and a
|
||||
/// `quantile` input. Neither counts a table per chunk, and neither lets it go unbilled.
|
||||
pub(crate) fn unbilled_tables(series: &Series, billed: &HashSet<usize>) -> (Vec<usize>, usize) {
|
||||
let mut fresh: Vec<usize> = Vec::new();
|
||||
let mut bytes = 0usize;
|
||||
for field in series.fields.iter() {
|
||||
|
||||
@@ -50,7 +50,7 @@ use super::{
|
||||
vector_workspace_bytes,
|
||||
};
|
||||
use crate::evaluated::EvaluatedChunk;
|
||||
use crate::grid_series::merge_same_labelset;
|
||||
use crate::grid_series::{merge_same_labelset, series_bytes};
|
||||
use crate::histogram_eval::{HistogramFunction, NativeHistogramFunction};
|
||||
use crate::info::InfoSelector;
|
||||
use crate::instant_eval::{instant_candidate_span, select_instants};
|
||||
@@ -12282,6 +12282,406 @@ fn aggregate_sparse_output_budget_uses_rows_not_grid_length() {
|
||||
assert_eq!(pool.reserved(), 0);
|
||||
}
|
||||
|
||||
/// `quantile` answers what the kernel answers, because the node keeps the input the kernel reads.
|
||||
///
|
||||
/// The node is not a second implementation of the quantile: it retains its members and hands them to
|
||||
/// [`crate::aggregate::aggregate_quantile`] with a constant parameter over the rows they hold, so the
|
||||
/// assertion is against that kernel over the same series. Positions, values and the group each row
|
||||
/// belongs to must agree row for row. The input is uneven on purpose - a group's members hold
|
||||
/// different points, and one point's values come only from one member - which is what a per-point
|
||||
/// fold would answer and this must not.
|
||||
#[test]
|
||||
fn quantile_answers_what_the_kernel_answers() {
|
||||
let grid = grid(0, 60_000, 6);
|
||||
let schema = evaluated_schema(Arc::clone(&grid));
|
||||
let members = [
|
||||
(
|
||||
job_instance_metric("requests", "api", "a"),
|
||||
vec![Some(1.0), None, Some(3.0), Some(4.0), None, Some(6.0)],
|
||||
),
|
||||
(
|
||||
job_instance_metric("requests", "api", "b"),
|
||||
vec![None, Some(2.0), Some(5.0), None, Some(2.0), None],
|
||||
),
|
||||
(
|
||||
job_instance_metric("requests", "db", "c"),
|
||||
vec![Some(10.0), Some(20.0), None, None, Some(50.0), Some(60.0)],
|
||||
),
|
||||
];
|
||||
let chunks: Vec<EvaluatedChunk> = members
|
||||
.iter()
|
||||
.map(|(metric, values)| {
|
||||
evaluated_chunk_of(&schema, metric.clone(), 0, values.clone(), true)
|
||||
})
|
||||
.collect();
|
||||
for grouping in [
|
||||
Grouping::By(Arc::new([])),
|
||||
Grouping::By(Arc::new(["job".into()])),
|
||||
] {
|
||||
let tree = NativeTree::try_new(aggregate(
|
||||
evaluated_input(Arc::clone(&grid)),
|
||||
BoundAggregate {
|
||||
kind: AggregateKind::Quantile { quantile: 0.9 },
|
||||
group: grouping.clone(),
|
||||
stage: AggregateStage::Complete,
|
||||
},
|
||||
))
|
||||
.unwrap();
|
||||
let pool = bounded_pool(1 << 20);
|
||||
let mut execution = executor(&tree, Arc::clone(&pool));
|
||||
for chunk in &chunks {
|
||||
execution
|
||||
.push_evaluated(InputId::new(0), chunk.clone())
|
||||
.unwrap();
|
||||
}
|
||||
let output = execution.finish().unwrap();
|
||||
assert!(
|
||||
!output.is_empty(),
|
||||
"{grouping:?}: the quantile answered nothing"
|
||||
);
|
||||
|
||||
// The kernel over the same members, with the parameter this node builds: the φ as an
|
||||
// unlabelled single-field scalar over every point the input's rows sit at.
|
||||
let series: Vec<Series> = chunks.iter().map(|chunk| chunk.series.clone()).collect();
|
||||
let parameter_schema = SeriesSchema {
|
||||
fields: Arc::from([FieldSpec {
|
||||
name: "scalar".into(),
|
||||
}]),
|
||||
eval_grid: Some(Arc::clone(&grid)),
|
||||
};
|
||||
let parameter = crate::map::constant_series(
|
||||
¶meter_schema,
|
||||
GridRange::try_new(Arc::clone(&grid), 0, 6).unwrap(),
|
||||
0.9,
|
||||
)
|
||||
.unwrap();
|
||||
let names: Vec<&str> = match &grouping {
|
||||
Grouping::By(labels) | Grouping::Without(labels) => {
|
||||
labels.iter().map(|label| label.as_ref()).collect()
|
||||
}
|
||||
};
|
||||
let kernel_grouping = match &grouping {
|
||||
Grouping::By(_) => crate::aggregate::Grouping::By(&names),
|
||||
Grouping::Without(_) => crate::aggregate::Grouping::Without(&names),
|
||||
};
|
||||
let expected =
|
||||
crate::aggregate::aggregate_quantile(&schema, &series, ¶meter, kernel_grouping)
|
||||
.unwrap();
|
||||
drop(names);
|
||||
let expected: Vec<EvaluatedChunk> = expected
|
||||
.into_iter()
|
||||
.map(|series| EvaluatedChunk::try_new(series, true).unwrap())
|
||||
.collect();
|
||||
assert_eq!(groups_of(&expected), groups_of(&output), "{grouping:?}");
|
||||
assert_eq!(pool.reserved(), 0, "{grouping:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A `quantile`'s parameter is as wide as the points the input holds, not the span they sit in.
|
||||
///
|
||||
/// The two points are 99_999 positions apart on a 100_000-point grid, so a parameter over the window
|
||||
/// between them carries a value for every position in it -- 800 KB at this width -- and no admission
|
||||
/// ever sized it. That pool is 64 KiB, which the dense span could not be admitted into and which this
|
||||
/// fold answers in: the parameter is built over the input's own positions, so the gaps cost a span
|
||||
/// and not a row. What the fold took is released again when it answers.
|
||||
#[test]
|
||||
fn quantile_parameter_costs_the_points_held_not_the_span_between_them() {
|
||||
let grid = grid(0, 1, 100_000);
|
||||
let schema = evaluated_schema(Arc::clone(&grid));
|
||||
let tree = NativeTree::try_new(aggregate(
|
||||
evaluated_input(Arc::clone(&grid)),
|
||||
BoundAggregate {
|
||||
kind: AggregateKind::Quantile { quantile: 0.5 },
|
||||
group: Grouping::By(Arc::new([])),
|
||||
stage: AggregateStage::Complete,
|
||||
},
|
||||
))
|
||||
.unwrap();
|
||||
let pool = bounded_pool(64 * 1024);
|
||||
let mut execution = executor(&tree, Arc::clone(&pool));
|
||||
execution
|
||||
.push_evaluated(
|
||||
InputId::new(0),
|
||||
sparse_chunk(
|
||||
&schema,
|
||||
metric(),
|
||||
&[(0, 1), (99_999, 1)],
|
||||
vec![1.0, 3.0],
|
||||
true,
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
let output = execution.finish().unwrap();
|
||||
assert_eq!(
|
||||
groups_of(&output),
|
||||
vec![unlabelled(vec![(0, Some(1.0)), (99_999, Some(3.0))])]
|
||||
);
|
||||
assert_eq!(pool.reserved(), 0);
|
||||
}
|
||||
|
||||
/// A `quantile` whose input holds no sample at all answers nothing, rather than failing on the
|
||||
/// parameter it has no point to write the φ at.
|
||||
///
|
||||
/// The node hands the empty input to [`crate::aggregate::aggregate_quantile`] with the φ as an
|
||||
/// unlabelled single-field scalar, and that scalar is built over exactly the points the retained
|
||||
/// members sit at. An input with no member sits at none of them, so the builder it is written
|
||||
/// through finishes with no series at all -- and there is no point the kernel could ask for a φ it
|
||||
/// has no value at. The statement is still a legal one whose answer is the empty vector:
|
||||
/// `quantile(0.5, m)` over a range `m` holds no sample in is exactly this case, and Prometheus
|
||||
/// answers it with no series rather than with an error. The parameter is therefore an empty scalar
|
||||
/// over the grid instead -- the same thing said in the form the kernel reads -- which is what keeps
|
||||
/// "no sample in range" an answer of no samples rather than a failure to build a parameter.
|
||||
#[test]
|
||||
fn a_quantile_over_no_input_answers_nothing() {
|
||||
let grid = grid(0, 60_000, 4);
|
||||
let tree = NativeTree::try_new(aggregate(
|
||||
evaluated_input(Arc::clone(&grid)),
|
||||
BoundAggregate {
|
||||
kind: AggregateKind::Quantile { quantile: 0.5 },
|
||||
group: Grouping::By(Arc::new([])),
|
||||
stage: AggregateStage::Complete,
|
||||
},
|
||||
))
|
||||
.unwrap();
|
||||
let pool = bounded_pool(1 << 20);
|
||||
let mut execution = executor(&tree, Arc::clone(&pool));
|
||||
// Nothing is pushed: the selection under this aggregate held no sample in the range it was
|
||||
// asked for, so its leaf hands over no chunk at all and the fold keeps no member.
|
||||
let output = run(&mut execution);
|
||||
assert!(
|
||||
output.is_empty(),
|
||||
"a quantile over no input answered {:?}",
|
||||
groups_of(&output)
|
||||
);
|
||||
assert_eq!(pool.reserved(), 0);
|
||||
}
|
||||
|
||||
/// A `quantile` bills the buffer its chunks are slices of once, not once per chunk.
|
||||
///
|
||||
/// One 2000-sample Arrow allocation is handed over as 2000 one-row chunks, which is what a source
|
||||
/// handing over the rows of one batch does: every chunk is a slice of the same 16 KB buffer, so the
|
||||
/// series holds 16 KB of values and one descriptor per chunk. Charging each chunk for the allocation
|
||||
/// its slice points into -- `series_bytes`, which reads the capacity of the buffer a slice keeps
|
||||
/// alive -- bills 2000 x ~16.3 KB ≈ 32 MB, and the 4 MiB pool below refuses this input at about the
|
||||
/// 260th chunk (4 MiB / 16.3 KB), while `min` over the same chunks answers, because a point-wise fold
|
||||
/// holds one chunk at a time and never the input. Retained the way this node bills its input --
|
||||
/// `chunk_bytes_beside_samples` per chunk, plus the allocations the chunks' samples are slices of,
|
||||
/// each billed once by the first chunk that references one (`unbilled_sample_buffers`) -- the input
|
||||
/// costs its rows, its descriptors and one charge for the buffer they slice: under a megabyte, so
|
||||
/// the fold answers and leaves the pool empty.
|
||||
#[test]
|
||||
fn a_quantile_bills_a_shared_buffer_once() {
|
||||
const ROWS: usize = 2_000;
|
||||
const POOL: usize = 4 * 1024 * 1024;
|
||||
let grid = grid(0, 1, ROWS as u64);
|
||||
let schema = evaluated_schema(Arc::clone(&grid));
|
||||
let batch = Float64Array::from((0..ROWS).map(|row| row as f64).collect::<Vec<f64>>());
|
||||
let chunks: Vec<EvaluatedChunk> = (0..ROWS)
|
||||
.map(|row| {
|
||||
evaluated_array_chunk_of(&schema, metric(), row, batch.slice(row, 1), row + 1 == ROWS)
|
||||
})
|
||||
.collect();
|
||||
// What a one-row chunk is charged by the figure this test is about: the buffer it is a slice of,
|
||||
// not the eight bytes of the row it holds. The whole input is that buffer twice over -- once
|
||||
// spent on the values and once billed per chunk -- which is what the pool would refuse.
|
||||
let per_chunk = series_bytes(&chunks[0].series);
|
||||
assert!(
|
||||
per_chunk >= ROWS * std::mem::size_of::<f64>(),
|
||||
"a one-row chunk is charged {per_chunk} bytes, less than the {} the buffer it is a slice of \
|
||||
holds, so this test is not reproducing the per-chunk billing it is about",
|
||||
ROWS * std::mem::size_of::<f64>()
|
||||
);
|
||||
let per_chunk_total = per_chunk.saturating_mul(ROWS);
|
||||
assert!(
|
||||
per_chunk_total > 7 * POOL,
|
||||
"billing each chunk for its buffer costs {per_chunk_total} bytes, which a {POOL}-byte pool \
|
||||
would admit, so this input does not reproduce the double billing"
|
||||
);
|
||||
|
||||
let run = |kind: AggregateKind, pool: Arc<dyn MemoryPool>| {
|
||||
let tree = NativeTree::try_new(aggregate(
|
||||
evaluated_input(Arc::clone(&grid)),
|
||||
BoundAggregate {
|
||||
kind,
|
||||
group: Grouping::By(Arc::new([])),
|
||||
stage: AggregateStage::Complete,
|
||||
},
|
||||
))
|
||||
.unwrap();
|
||||
let mut execution = executor(&tree, Arc::clone(&pool));
|
||||
for chunk in &chunks {
|
||||
execution
|
||||
.push_evaluated(InputId::new(0), chunk.clone())
|
||||
.unwrap();
|
||||
// A source queue holds the chunk the caller just pushed, so a pull per push keeps what
|
||||
// the pool holds to what the nodes retain rather than to the whole input stream. An
|
||||
// aggregate answers once, when its input ends, so nothing has settled here yet.
|
||||
assert!(
|
||||
execution.next_chunk().unwrap().is_none(),
|
||||
"{kind:?} answered early"
|
||||
);
|
||||
}
|
||||
let output = execution.finish().unwrap();
|
||||
assert_eq!(
|
||||
pool.reserved(),
|
||||
0,
|
||||
"{kind:?} left the pool holding input it was finished with"
|
||||
);
|
||||
output
|
||||
};
|
||||
let minimum = run(AggregateKind::Min, bounded_pool(POOL));
|
||||
let quantile = run(
|
||||
AggregateKind::Quantile { quantile: 0.5 },
|
||||
bounded_pool(POOL),
|
||||
);
|
||||
// Every point of this input holds exactly one value, so the φ-quantile of a point is that value
|
||||
// and the two aggregates answer the same series.
|
||||
assert_eq!(groups_of(&quantile), groups_of(&minimum));
|
||||
}
|
||||
|
||||
/// A `quantile` bills the allocation its chunks' slices point into, once, and not the rows it holds of
|
||||
/// it.
|
||||
///
|
||||
/// A one-row chunk of a 100 000-row batch keeps that batch's whole 800 KB buffer resident, so that is
|
||||
/// what retaining it costs: its row is eight bytes, and a figure that stopped at the rows would leave
|
||||
/// the buffer the chunk pins to nothing -- a chunk holding a small slice of a large batch under-billed
|
||||
/// by everything the batch holds. One allocation is also one however many chunks slice it, so a second
|
||||
/// one-row chunk of the same batch is charged its descriptors and not the buffer again: billing every
|
||||
/// chunk for the buffer it slices would refuse an input whose buffer fits the pool.
|
||||
#[test]
|
||||
fn a_quantile_bills_the_allocation_its_slices_point_into_once() {
|
||||
const ROWS: usize = 100_000;
|
||||
// What the batch's values occupy, and so what one chunk of it keeps alive however few rows it
|
||||
// holds of it.
|
||||
let residency = ROWS * std::mem::size_of::<f64>();
|
||||
let grid = grid(0, 1, ROWS as u64);
|
||||
let schema = evaluated_schema(Arc::clone(&grid));
|
||||
let batch = Float64Array::from((0..ROWS).map(|row| row as f64).collect::<Vec<f64>>());
|
||||
let tree = NativeTree::try_new(aggregate(
|
||||
evaluated_input(Arc::clone(&grid)),
|
||||
BoundAggregate {
|
||||
kind: AggregateKind::Quantile { quantile: 0.5 },
|
||||
group: Grouping::By(Arc::new([])),
|
||||
stage: AggregateStage::Complete,
|
||||
},
|
||||
))
|
||||
.unwrap();
|
||||
let pool = bounded_pool(16 * residency);
|
||||
let mut execution = executor(&tree, Arc::clone(&pool));
|
||||
|
||||
// The first row of the batch, which is a slice of the whole buffer.
|
||||
execution
|
||||
.push_evaluated(
|
||||
InputId::new(0),
|
||||
evaluated_array_chunk_of(&schema, metric(), 0, batch.slice(0, 1), false),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(
|
||||
execution.next_chunk().unwrap().is_none(),
|
||||
"the quantile answered before its input ended"
|
||||
);
|
||||
let one = execution.state().reserved_bytes();
|
||||
assert!(
|
||||
one >= residency,
|
||||
"a retained chunk holding one row of a {residency}-byte buffer is charged {one} bytes, less \
|
||||
than the buffer it keeps alive"
|
||||
);
|
||||
|
||||
// A second row of the same buffer: the same allocation, sliced again.
|
||||
let second = evaluated_array_chunk_of(&schema, metric(), 1, batch.slice(1, 1), true);
|
||||
execution
|
||||
.push_evaluated(InputId::new(0), second.clone())
|
||||
.unwrap();
|
||||
assert!(
|
||||
execution.next_chunk().unwrap().is_none(),
|
||||
"the quantile answered before its input ended"
|
||||
);
|
||||
let both = execution.state().reserved_bytes();
|
||||
assert!(
|
||||
both < residency + residency / 2,
|
||||
"two chunks slicing one {residency}-byte buffer are charged {both} bytes, so the buffer is \
|
||||
counted once per chunk rather than once"
|
||||
);
|
||||
// What the second chunk cost is its own descriptors and the slot it takes in the vector: the
|
||||
// allocation it slices is already billed, and its `Series` is that slot rather than a second
|
||||
// figure beside it. The delta is exact, because the settle reads back the capacity the vector
|
||||
// really holds, so a chunk billed twice -- or a buffer billed again -- shows up here.
|
||||
let second_cost = both.saturating_sub(one);
|
||||
let expected = super::chunk_bytes_beside_samples(&second.series)
|
||||
.saturating_add(std::mem::size_of::<Series>());
|
||||
assert!(
|
||||
second_cost <= expected,
|
||||
"the second chunk of one allocation cost {second_cost} bytes, more than the {expected} its \
|
||||
own descriptors and one slot hold"
|
||||
);
|
||||
|
||||
// Both rows are one member of the one group, so each point answers from its own row.
|
||||
let output = execution.finish().unwrap();
|
||||
assert_eq!(
|
||||
groups_of(&output),
|
||||
vec![unlabelled(vec![(0, Some(0.0)), (1, Some(1.0))])]
|
||||
);
|
||||
assert_eq!(pool.reserved(), 0);
|
||||
}
|
||||
|
||||
/// A `quantile` skips a histogram sample and says so, exactly as `min` does.
|
||||
///
|
||||
/// The φ-quantile orders the values of its group and a native histogram is not one it can order, so
|
||||
/// Prometheus answers the quantile of the float samples and skips the histogram ones -- which for a
|
||||
/// group of nothing but histograms is no answer at all. `min` reads a histogram the same way and
|
||||
/// records the same info while doing it; the two are one branch in the legacy planner, and this pins
|
||||
/// that the native fold agrees with them rather than answering from samples it dropped.
|
||||
#[test]
|
||||
fn a_quantile_skips_histogram_samples_and_reports_it_like_min() {
|
||||
let grid = grid(0, 60_000, 2);
|
||||
let schema = evaluated_schema(Arc::clone(&grid));
|
||||
for (kind, expected_info) in [
|
||||
(
|
||||
AggregateKind::Quantile { quantile: 0.5 },
|
||||
"quantile: ignored native histogram samples",
|
||||
),
|
||||
(AggregateKind::Min, "min: ignored native histogram samples"),
|
||||
] {
|
||||
let tree = NativeTree::try_new(aggregate(
|
||||
evaluated_input(Arc::clone(&grid)),
|
||||
BoundAggregate {
|
||||
kind,
|
||||
group: Grouping::By(Arc::new([])),
|
||||
stage: AggregateStage::Complete,
|
||||
},
|
||||
))
|
||||
.unwrap();
|
||||
let collector = common_query::promql_annotations::PromqlAnnotationCollector::default();
|
||||
let mut state = NativeExecutionState::try_new(&tree, pool(), None).unwrap();
|
||||
state.set_annotations(Some(collector.clone()));
|
||||
let mut execution = NativeExecutor::new(state);
|
||||
execution
|
||||
.push_evaluated(
|
||||
InputId::new(0),
|
||||
evaluated_histogram_chunk_of(
|
||||
&schema,
|
||||
metric(),
|
||||
0,
|
||||
vec![Some(0), None],
|
||||
&histogram_table(1, 2),
|
||||
true,
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
let output = run(&mut execution);
|
||||
assert!(
|
||||
output.is_empty(),
|
||||
"{kind:?} answered a group it had no value for: {:?}",
|
||||
groups_of(&output)
|
||||
);
|
||||
let mut warnings = Vec::new();
|
||||
let mut infos = Vec::new();
|
||||
collector.append_to(&mut warnings, &mut infos);
|
||||
assert!(warnings.is_empty(), "{kind:?}: {warnings:?}");
|
||||
assert_eq!(infos, vec![expected_info.to_string()], "{kind:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// An aggregate retains its fold before finish and release returns its reservation.
|
||||
#[test]
|
||||
fn aggregate_fold_state_is_released_after_input() {
|
||||
@@ -12317,29 +12717,81 @@ fn aggregate_fold_state_is_released_after_input() {
|
||||
assert_eq!(pool.reserved(), 0);
|
||||
}
|
||||
|
||||
/// An aggregate kind this runtime does not compute is refused, naming what was found.
|
||||
/// A `quantile` carries no state, so a tree cannot even bind it as anything but `Complete`.
|
||||
///
|
||||
/// `quantile` is answered from its group's whole sample set, so a stage that saw part of a group
|
||||
/// would answer the quantile of a subset of it, and the quantile of two partial groups is not a
|
||||
/// function of their quantiles. There is no state layout to export or merge, which is what the tree's
|
||||
/// own validation refuses while it is built: a `Partial` or `Final` node has nowhere to put its
|
||||
/// state. That refusal is the only one there is -- the runtime starts from a validated tree and so
|
||||
/// starts the `Complete` stage alone, without a second check of its own.
|
||||
#[test]
|
||||
fn an_aggregate_kind_the_runtime_does_not_compute_is_named() {
|
||||
fn a_quantile_stage_the_runtime_does_not_compute_is_named() {
|
||||
let grid = || grid(0, 60_000, 4);
|
||||
let kind_tree = NativeTree::try_new(aggregate(
|
||||
evaluated_input(grid()),
|
||||
BoundAggregate {
|
||||
kind: AggregateKind::Quantile { quantile: 0.5 },
|
||||
let schema = || evaluated_schema(grid());
|
||||
// The state layout a `Sum` exports is enough to build the stage's tree: what this test asks is
|
||||
// which stages the runtime starts, and a `quantile` state is refused before it is read.
|
||||
let (state_kind, components) =
|
||||
crate::native::plan::aggregate_state_binding(AggregateKind::Sum).unwrap();
|
||||
let state_schema = || {
|
||||
Arc::new(AggregateStateSchema {
|
||||
group: Grouping::By(Arc::new([])),
|
||||
stage: AggregateStage::Complete,
|
||||
},
|
||||
))
|
||||
.unwrap();
|
||||
let error = match NativeExecutionState::try_new(&kind_tree, pool(), None) {
|
||||
Ok(_) => panic!("the runtime accepted a Quantile aggregate"),
|
||||
Err(error) => error,
|
||||
kind: state_kind,
|
||||
components: components.clone().into(),
|
||||
accumulators: schema().fields.clone(),
|
||||
grid: grid(),
|
||||
})
|
||||
};
|
||||
let state_input = || NativeNode {
|
||||
expr: NativeExpr::Input(BoundInput {
|
||||
output: NativeType::AggregateState(state_schema()),
|
||||
source_index: 0,
|
||||
ordering: SeriesOrdering::Contiguous,
|
||||
}),
|
||||
output: NativeType::AggregateState(state_schema()),
|
||||
};
|
||||
let quantile_node = |stage| {
|
||||
let (input, output) = match stage {
|
||||
AggregateStage::Complete => (evaluated_input(grid()), evaluated(grid())),
|
||||
AggregateStage::Partial => (
|
||||
evaluated_input(grid()),
|
||||
NativeType::AggregateState(state_schema()),
|
||||
),
|
||||
AggregateStage::Final => (state_input(), evaluated(grid())),
|
||||
};
|
||||
aggregate_with_output(
|
||||
input,
|
||||
BoundAggregate {
|
||||
kind: AggregateKind::Quantile { quantile: 0.5 },
|
||||
group: Grouping::By(Arc::new([])),
|
||||
stage,
|
||||
},
|
||||
output,
|
||||
)
|
||||
};
|
||||
for stage in [AggregateStage::Partial, AggregateStage::Final] {
|
||||
let error = match NativeTree::try_new(quantile_node(stage)) {
|
||||
Ok(_) => panic!("the tree accepted a {stage:?} Quantile aggregate"),
|
||||
Err(error) => error,
|
||||
};
|
||||
assert!(
|
||||
matches!(error, DataFusionError::NotImplemented(_)),
|
||||
"{stage:?}: {error}"
|
||||
);
|
||||
let message = error.to_string();
|
||||
assert!(message.contains("Quantile"), "{stage:?}: {message}");
|
||||
assert!(
|
||||
message.contains("aggregate state is not defined"),
|
||||
"{stage:?}: {message}"
|
||||
);
|
||||
}
|
||||
// The `Complete` stage of the same kind is what this runtime does compute.
|
||||
let complete = NativeTree::try_new(quantile_node(AggregateStage::Complete))
|
||||
.expect("a complete quantile is a legal tree");
|
||||
assert!(
|
||||
matches!(error, DataFusionError::NotImplemented(_)),
|
||||
"Quantile: {error}"
|
||||
NativeExecutionState::try_new(&complete, pool(), None).is_ok(),
|
||||
"the runtime rejected a Complete quantile"
|
||||
);
|
||||
let message = error.to_string();
|
||||
assert!(message.contains("Quantile"), "Quantile: {message}");
|
||||
|
||||
let out = grid();
|
||||
let group = Grouping::By(Arc::new(["job".into()]));
|
||||
|
||||
@@ -671,11 +671,11 @@ impl NativeNode {
|
||||
// sample whatever their arguments are, constant or per point.
|
||||
NativeExpr::ParameterizedHistogram { .. } => Some(FieldKind::Float),
|
||||
// Every aggregate answers floats: `count`/`group` answer a count or a `1` whatever the
|
||||
// samples they counted, `min`/`max`/`stddev`/`stdvar` fold the float samples and skip
|
||||
// the histogram ones, and `sum`/`avg` - the only two that would fold the payload - keep
|
||||
// a histogram out at admission. Naming it here is what lets `sum(count_over_time(h[5m]))`
|
||||
// - a float built from histograms - declare a float column instead of keeping the
|
||||
// existing plan.
|
||||
// samples they counted, `min`/`max`/`stddev`/`stdvar`/`quantile` fold the float samples
|
||||
// and skip the histogram ones, and `sum`/`avg` - the only two that would fold the
|
||||
// payload - keep a histogram out at admission. Naming it here is what lets
|
||||
// `sum(count_over_time(h[5m]))` - a float built from histograms - declare a float
|
||||
// column instead of keeping the existing plan.
|
||||
NativeExpr::Aggregate { .. } => Some(FieldKind::Float),
|
||||
_ => None,
|
||||
}
|
||||
@@ -1559,7 +1559,9 @@ impl<'de> serde::Deserialize<'de> for NativeTree {
|
||||
/// - a binary node that is neither a vector–vector operation nor a broadcast the runtime performs:
|
||||
/// two constant operands (no series to carry the result), a scalar broadcast carrying
|
||||
/// vector-matching modifiers, or `bool` on an arithmetic operator, which has no `bool` form;
|
||||
/// - the `quantile` aggregate, which has no kernel on this route at any stage;
|
||||
/// - a `quantile` aggregate at any stage but `Complete`, which would need a partial state it does
|
||||
/// not keep (`validate` refuses one for the same reason; this arm keeps the predicate and the
|
||||
/// runtime agreeing on it);
|
||||
/// - an aggregate below `Final` whose `by` names a value field, which would fold series the
|
||||
/// existing plan keeps apart;
|
||||
/// - `topk`, `bottomk`, `limitk`, `sort` and `sort_desc` over a vector carrying more than one
|
||||
@@ -1719,7 +1721,14 @@ fn aggregate_supported(node: &NativeNode, input: &NativeNode, op: &BoundAggregat
|
||||
| AggregateKind::Group
|
||||
| AggregateKind::Stddev
|
||||
| AggregateKind::Stdvar => {}
|
||||
AggregateKind::Quantile { .. } => return false,
|
||||
AggregateKind::Quantile { .. } => {
|
||||
// A quantile is one `Complete` fold over its group's whole input: it keeps no state,
|
||||
// so `validate` refuses a `Partial` or `Final` one and there is nothing to merge. The
|
||||
// runtime answers the `Complete` stage, which is the only shape compiled.
|
||||
if op.stage != AggregateStage::Complete {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if op.stage == AggregateStage::Final {
|
||||
return true;
|
||||
|
||||
@@ -3103,16 +3103,6 @@ fn native_tree_supported_declines_the_shapes_the_runtime_refuses() {
|
||||
output: evaluated(fields(1), Arc::clone(&out)),
|
||||
},
|
||||
),
|
||||
(
|
||||
"the quantile aggregate",
|
||||
aggregate_with(
|
||||
one_field(),
|
||||
Arc::clone(&out),
|
||||
AggregateKind::Quantile { quantile: 0.9 },
|
||||
Grouping::By(Arc::new(["job".into()])),
|
||||
AggregateStage::Complete,
|
||||
),
|
||||
),
|
||||
(
|
||||
"an aggregate whose `by` names a value field",
|
||||
aggregate_with(
|
||||
@@ -3228,27 +3218,27 @@ fn native_tree_supported_declines_the_shapes_the_runtime_refuses() {
|
||||
|
||||
/// A refusal below the root is still a refusal: admission reads the whole tree, not its root.
|
||||
///
|
||||
/// Each case below holds a `quantile` — which has no kernel on this route, at any stage or depth —
|
||||
/// under a node whose own shape the runtime does build. A predicate that stopped recursing into
|
||||
/// one of these variants' inputs, or into a parameterised node's parameters, would admit a tree
|
||||
/// the runtime then refuses, and a refusal raised while the runtime is built cannot fall back: the
|
||||
/// statement loses the plan it had instead of keeping it.
|
||||
/// Each case below holds an aggregate grouping by a value field — a shape the runtime refuses, at
|
||||
/// any depth — under a node whose own shape the runtime does build. A predicate that stopped
|
||||
/// recursing into one of these variants' inputs, or into a parameterised node's parameters, would
|
||||
/// admit a tree the runtime then refuses, and a refusal raised while the runtime is built cannot
|
||||
/// fall back: the statement loses the plan it had instead of keeping it.
|
||||
#[test]
|
||||
fn native_tree_supported_declines_a_refused_shape_nested_under_another() {
|
||||
let out = grid(0, 60_000, 4);
|
||||
let one_field = || window_fields(input_with_fields(1), Arc::clone(&out), 1);
|
||||
// The declined shape every case nests, one level down from a node the runtime builds.
|
||||
let quantile = || {
|
||||
let refused = || {
|
||||
aggregate_with(
|
||||
one_field(),
|
||||
Arc::clone(&out),
|
||||
AggregateKind::Quantile { quantile: 0.9 },
|
||||
Grouping::By(Arc::new(["job".into()])),
|
||||
AggregateKind::Sum,
|
||||
Grouping::By(Arc::new(["f0".into()])),
|
||||
AggregateStage::Complete,
|
||||
)
|
||||
};
|
||||
// The info side `info` reads, which `validate` requires to be the times of an instant
|
||||
// selection; only the base side of the node carries the quantile.
|
||||
// selection; only the base side of the node carries the refused shape.
|
||||
let info_times = || {
|
||||
merge_over(NativeNode {
|
||||
expr: NativeExpr::Instant {
|
||||
@@ -3266,10 +3256,10 @@ fn native_tree_supported_declines_a_refused_shape_nested_under_another() {
|
||||
};
|
||||
let cases = vec![
|
||||
(
|
||||
"a quantile under a binary operation",
|
||||
"a refused shape under a binary operation",
|
||||
NativeNode {
|
||||
expr: NativeExpr::Binary {
|
||||
left: Box::new(quantile()),
|
||||
left: Box::new(refused()),
|
||||
right: Box::new(one_field()),
|
||||
matching: add(),
|
||||
},
|
||||
@@ -3277,10 +3267,10 @@ fn native_tree_supported_declines_a_refused_shape_nested_under_another() {
|
||||
},
|
||||
),
|
||||
(
|
||||
"a quantile under a set operation",
|
||||
"a refused shape under a set operation",
|
||||
NativeNode {
|
||||
expr: NativeExpr::Set {
|
||||
left: Box::new(quantile()),
|
||||
left: Box::new(refused()),
|
||||
right: Box::new(one_field()),
|
||||
op: super::BoundSetOp {
|
||||
op: SetOp::Or,
|
||||
@@ -3291,10 +3281,10 @@ fn native_tree_supported_declines_a_refused_shape_nested_under_another() {
|
||||
},
|
||||
),
|
||||
(
|
||||
"a quantile under info",
|
||||
"a refused shape under info",
|
||||
NativeNode {
|
||||
expr: NativeExpr::Info {
|
||||
base: Box::new(quantile()),
|
||||
base: Box::new(refused()),
|
||||
info: Box::new(info_times()),
|
||||
selector: InfoSelector {
|
||||
name_matchers: Vec::new(),
|
||||
@@ -3306,9 +3296,9 @@ fn native_tree_supported_declines_a_refused_shape_nested_under_another() {
|
||||
},
|
||||
),
|
||||
(
|
||||
"a quantile under an aggregate",
|
||||
"a refused shape under an aggregate",
|
||||
aggregate_with(
|
||||
quantile(),
|
||||
refused(),
|
||||
Arc::clone(&out),
|
||||
AggregateKind::Sum,
|
||||
Grouping::By(Arc::new(["job".into()])),
|
||||
@@ -3316,10 +3306,10 @@ fn native_tree_supported_declines_a_refused_shape_nested_under_another() {
|
||||
),
|
||||
),
|
||||
(
|
||||
"a quantile under a parameterized topk",
|
||||
"a refused shape under a parameterized topk",
|
||||
NativeNode {
|
||||
expr: NativeExpr::ParameterizedTopK {
|
||||
input: Box::new(quantile()),
|
||||
input: Box::new(refused()),
|
||||
op: BoundVectorOp::TopK {
|
||||
k: 0.0,
|
||||
top: true,
|
||||
@@ -3413,6 +3403,18 @@ fn native_tree_supported_admits_the_shapes_the_runtime_builds() {
|
||||
};
|
||||
let cases = vec![
|
||||
("an evaluated input leaf", input_on(Arc::clone(&out))),
|
||||
(
|
||||
// A quantile is one Complete fold: it keeps no state, so there is nothing to merge and
|
||||
// nothing the runtime refuses.
|
||||
"the quantile aggregate",
|
||||
aggregate_with(
|
||||
one_field(),
|
||||
Arc::clone(&out),
|
||||
AggregateKind::Quantile { quantile: 0.9 },
|
||||
Grouping::By(Arc::new(["job".into()])),
|
||||
AggregateStage::Complete,
|
||||
),
|
||||
),
|
||||
("a constant root", scalar_on(3.0, Arc::clone(&out))),
|
||||
(
|
||||
"an evaluation-time root",
|
||||
|
||||
@@ -37,7 +37,7 @@
|
||||
//! <expression> <operator> <scalar> e.g. x * 100, x > bool time()
|
||||
//! <scalar> <operator> <expression> e.g. 1 / x, time() - timestamp(x)
|
||||
//! <scalar> <operator> <scalar> e.g. time() - 60, time() > bool 1
|
||||
//! time(), vector(<scalar>) e.g. vector(time())
|
||||
//! time(), pi(), vector(<scalar>) e.g. vector(time()), pi() * x
|
||||
//! <date function>() e.g. hour(), which is `hour(vector(time()))`
|
||||
//! -<expression> e.g. -x
|
||||
//! <expression> <set operator> <expression> e.g. x and on(job) y, x unless y
|
||||
@@ -1379,9 +1379,9 @@ fn compile_call(context: &Context, call: &Call) -> Result<NativeNode> {
|
||||
return Ok(node);
|
||||
}
|
||||
// The name is looked up before the call's arguments are read, so a name this compiler has no
|
||||
// kernel for is refused as such whichever arguments it was given: `pi()` and `time()` have no
|
||||
// vector to map, and the rejection names the name rather than describing a vector they never
|
||||
// wrote.
|
||||
// kernel for is refused as such whichever arguments it was given, and the rejection names the
|
||||
// name rather than describing a vector it never wrote. `pi()` does not reach here: it is a
|
||||
// constant the statement itself decides, so it already folded in `compile_expr`.
|
||||
if let Some(node) = parameterized_call(context, func.name, &args.args)? {
|
||||
return Ok(node);
|
||||
}
|
||||
@@ -2712,14 +2712,22 @@ fn refuses_modifier(modifier: Option<&BinModifier>) -> bool {
|
||||
/// The number `expr` folds to, or `None` when its value is not the statement's to know.
|
||||
///
|
||||
/// Only what the statement itself decides is folded: a number literal, a group around one, a negated
|
||||
/// one, and arithmetic whose two operands both fold. A call, a selector, a range selector, an
|
||||
/// aggregate and a subquery are `None`, because their value comes from the data or from the
|
||||
/// evaluation time, and a kernel's constant is a field of the tree rather than something bound while
|
||||
/// it runs. Returning `None` rather than an error is what lets the caller report where the
|
||||
/// one, `pi()`, and arithmetic whose two operands both fold. Any other call, a selector, a range
|
||||
/// selector, an aggregate and a subquery are `None`, because their value comes from the data or from
|
||||
/// the evaluation time, and a kernel's constant is a field of the tree rather than something bound
|
||||
/// while it runs. Returning `None` rather than an error is what lets the caller report where the
|
||||
/// expression was found, which is the useful half of the refusal.
|
||||
///
|
||||
/// `pi()` belongs on the folding side because the statement itself decides it: it is one number, not
|
||||
/// a value read from the data. Folding it is what makes `pi() * x` the multiplication by
|
||||
/// [`std::f64::consts::PI`] it means, `clamp_max(x, pi())` a kernel constant like `clamp_max(x, 3)`,
|
||||
/// and `quantile(pi() / 4, x)` a parameter this compiler can bind. The number is the one the promql
|
||||
/// crate's `map_pi` answers the previous route with — both are `std::f64::consts::PI` — so the two
|
||||
/// routes cannot disagree about what `pi()` is.
|
||||
///
|
||||
/// Arithmetic uses the kernel operation named by [`BinaryOp::kind`], including
|
||||
/// [`ArithmeticOp::calculate`]'s platform math behavior. Set operators do not fold.
|
||||
/// [`ArithmeticOp::calculate`]'s platform math behavior; defining `%`, `^` or `atan2` again here
|
||||
/// would be a second answer for the two to disagree over. Set operators do not fold.
|
||||
/// Visible to the planner, which asks whether a statement's value is a constant before binding it.
|
||||
pub(crate) fn constant_value(expr: &PromExpr) -> Option<f64> {
|
||||
match expr {
|
||||
@@ -2747,8 +2755,13 @@ pub(crate) fn constant_value(expr: &PromExpr) -> Option<f64> {
|
||||
.then(|| f64::from(op.apply(left, right))),
|
||||
}
|
||||
}
|
||||
// Calls (`time()`, `scalar(v)`), selectors, range selectors, aggregates and subqueries all
|
||||
// read something this compiler cannot know before the query runs.
|
||||
// A selector, a range selector, an aggregate and a subquery read data; `time()`,
|
||||
// `scalar(v)` and the rest of the calls read something this compiler cannot know before the
|
||||
// query runs. `pi()` is not one of them: it is the one call whose value the statement itself
|
||||
// fixes, so it folds here as a literal does.
|
||||
PromExpr::Call(call) if call.func.name == "pi" && call.args.args.is_empty() => {
|
||||
Some(std::f64::consts::PI)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1292,6 +1292,8 @@ fn the_supported_math_functions_map_to_their_kernels() {
|
||||
("clamp(x, 0, 1)", "clamp 0 1"),
|
||||
("clamp_min(x, 0)", "clamp_min 0"),
|
||||
("clamp_max(x, 1)", "clamp_max 1"),
|
||||
// `pi()` is a constant, so it binds the kernel constant a literal would.
|
||||
("clamp_max(x, pi())", "clamp_max 3.141592653589793"),
|
||||
// Date functions over a vector read its values as Unix seconds.
|
||||
("day_of_month(x)", "day_of_month"),
|
||||
("day_of_week(x)", "day_of_week"),
|
||||
@@ -1316,6 +1318,51 @@ fn the_supported_math_functions_map_to_their_kernels() {
|
||||
}
|
||||
}
|
||||
|
||||
/// `pi()` is a constant the statement itself decides, so it binds where a literal binds.
|
||||
///
|
||||
/// It is a call, but not one whose value comes from the data or from the evaluation time: it is one
|
||||
/// number, the same `std::f64::consts::PI` the legacy planner's `pi` reports. Folding it is what
|
||||
/// makes an operation over it a broadcast of that number and a parameter written as `pi()` a kernel
|
||||
/// constant, rather than two shapes this compiler would have to answer separately.
|
||||
#[test]
|
||||
fn pi_folds_to_the_number_it_means() {
|
||||
let tree = compile_statement(&eval_stmt("pi()"))
|
||||
.unwrap_or_else(|error| panic!("`pi()` was refused: {error}"));
|
||||
assert_eq!(Some(std::f64::consts::PI), constant(tree.root()));
|
||||
assert_eq!((0, 5_000, 21), scalar_grid(tree.root()));
|
||||
|
||||
// Over a selector it is the multiplication it was written as, with the constant on the side the
|
||||
// query wrote it on.
|
||||
let tree = compile(
|
||||
&eval_stmt("pi() * x"),
|
||||
raw_schema(),
|
||||
SeriesOrdering::KeyThenTime,
|
||||
)
|
||||
.unwrap_or_else(|error| panic!("`pi() * x` was refused: {error}"));
|
||||
let (left, right, matching) = binary_parts(tree.root());
|
||||
assert_eq!(Some(std::f64::consts::PI), constant(left));
|
||||
assert!(matches!(right.expr, NativeExpr::Instant { .. }));
|
||||
assert_eq!(BinaryOp::Mul, matching.op);
|
||||
|
||||
// As a parameter it stays a constant, so the call is one map node and not a per-point
|
||||
// parameter: `clamp_max(x, pi())` binds the same kernel as `clamp_max(x, 3.141592653589793)`.
|
||||
let tree = compile(
|
||||
&eval_stmt("clamp_max(x, pi())"),
|
||||
raw_schema(),
|
||||
SeriesOrdering::KeyThenTime,
|
||||
)
|
||||
.unwrap_or_else(|error| panic!("`clamp_max(x, pi())` was refused: {error}"));
|
||||
let MathOp::ClampMax { max } = map_op(tree.root()) else {
|
||||
panic!(
|
||||
"the root is not a clamp_max: {:?}",
|
||||
map_parts(tree.root()).1.ops
|
||||
);
|
||||
};
|
||||
assert_eq!(std::f64::consts::PI, max);
|
||||
let (input, _) = map_parts(tree.root());
|
||||
assert!(matches!(input.expr, NativeExpr::Instant { .. }));
|
||||
}
|
||||
|
||||
/// The constants a function folds are folded by the kernel's own arithmetic, not by a second copy of
|
||||
/// it. Both numbers below are ones a hand-written copy would get wrong: a Euclidean modulo folds
|
||||
/// `-7 % 3` to 2, and `*` in place of `^` folds the power to about 9.8e8. That is the point: the
|
||||
@@ -2129,9 +2176,12 @@ fn a_statement_without_values_on_a_grid_is_refused() {
|
||||
///
|
||||
/// The vector entry above refuses it — a tree over that entry reports evaluated samples — so the
|
||||
/// statement entry is the one that binds it, and the grid it reports on is the statement's own.
|
||||
///
|
||||
/// `pi()` is one of these constants: it is a call, but the statement itself decides its value, so it
|
||||
/// folds to the number it means rather than keeping the call for the runtime.
|
||||
#[test]
|
||||
fn a_constant_statement_is_a_scalar_on_the_statement_grid_without_input() {
|
||||
for (query, value) in [("1 + 2", 3.0), ("-1", -1.0)] {
|
||||
for (query, value) in [("1 + 2", 3.0), ("-1", -1.0), ("pi()", std::f64::consts::PI)] {
|
||||
let stmt = eval_stmt(query);
|
||||
let tree = compile_statement(&stmt)
|
||||
.unwrap_or_else(|error| panic!("`{query}` was refused: {error}"));
|
||||
@@ -2180,7 +2230,7 @@ fn a_statement_that_reads_no_data_compiles_without_input() {
|
||||
}
|
||||
));
|
||||
|
||||
for (query, found) in [("x", "a selector"), ("pi()", "a call to `pi`")] {
|
||||
for (query, found) in [("x", "a selector"), ("step()", "a call to `step`")] {
|
||||
let error = compile_statement(&eval_stmt(query))
|
||||
.expect_err(&format!("`{query}` compiled, and it should not have"));
|
||||
assert!(
|
||||
|
||||
@@ -2013,13 +2013,19 @@ fn scan_time_filter(start: i64, end: i64, inclusive: bool, time_column: &str) ->
|
||||
|
||||
/// Whether any aggregate in `node` consumes histogram samples, wherever it sits in the tree.
|
||||
///
|
||||
/// The walk asks each aggregate what kind enters it rather than what the root answers, because an
|
||||
/// operator above an aggregate hides it: `sum(h) * 2` has a binary root.
|
||||
/// The walk asks each aggregate what kind enters it rather than what the tree's root answers,
|
||||
/// because an operator above an aggregate hides it: `sum(h) * 2` has a binary root. Only a
|
||||
/// histogram entering an aggregate that would fold its payload is refused; a float-producing prefix
|
||||
/// such as `count_over_time` over a histogram stays eligible, and so does an aggregate that skips the
|
||||
/// sample instead of folding it.
|
||||
fn aggregate_reads_histogram(node: &NativeNode, input_kind: FieldKind) -> bool {
|
||||
// Only `sum` and `avg` fold the payload, and this route has no histogram aggregate state yet:
|
||||
// `count` and `group` count a histogram as one present sample, `min`/`max`/`stddev`/`stdvar`
|
||||
// skip it the way Prometheus does, and `quantile` has no native kernel and keeps the existing
|
||||
// plan after this check.
|
||||
// `sum` and `avg` fold the payload and this route has no histogram aggregate state yet, so they
|
||||
// are the two that keep the existing plan. Every other kind answers a float without folding a
|
||||
// histogram's payload: `count` and `group` count a histogram as one present sample, and
|
||||
// `min`/`max`/`stddev`/`stdvar`/`quantile` skip it the way Prometheus does -- the legacy
|
||||
// planner maps all five through one branch (`create_native_histogram_aggregate_expr`), which
|
||||
// drops the samples and answers a Float NULL plus an info annotation, and the native fold does
|
||||
// the same thing at run time.
|
||||
//
|
||||
// The kind entering them has to be provably a float. "Cannot name it" is not "not a
|
||||
// histogram": a canonical merge and a set operation hand on the samples they were given
|
||||
|
||||
@@ -48,8 +48,8 @@ use promql::extension_plan::{
|
||||
FusedSeries, HistogramFold, InputLocality, RawSeriesSource, input_locality,
|
||||
};
|
||||
use promql::native::plan::{
|
||||
AggregateStage, BoundVectorOp, NativeExpr, NativeNode, NativeType, ParameterizedOp,
|
||||
unifies_its_own_contributions,
|
||||
AggregateKind, AggregateStage, BoundVectorOp, Grouping, NativeExpr, NativeNode, NativeType,
|
||||
ParameterizedOp, unifies_its_own_contributions,
|
||||
};
|
||||
use promql::vector_match::{Cardinality, MatchingLabels};
|
||||
use promql_parser::label::{Labels, METRIC_NAME};
|
||||
@@ -2833,9 +2833,12 @@ async fn native_fused_timestamp_with_multiple_fields_keeps_legacy_plan() {
|
||||
}
|
||||
|
||||
/// Literal roots need no raw scan, while a literal beside a selector broadcasts over that input.
|
||||
///
|
||||
/// `pi()` is a constant the statement itself decides, so it is bound where a literal is: as a scalar
|
||||
/// root, as the constant side of a binary operation, and as a call's parameter.
|
||||
#[tokio::test]
|
||||
async fn native_fused_supports_constant_roots_and_selector_broadcasts() {
|
||||
for (query, value) in [("1 + 2", 3.0), ("-1", -1.0)] {
|
||||
for (query, value) in [("1 + 2", 3.0), ("-1", -1.0), ("pi()", std::f64::consts::PI)] {
|
||||
let plan = build_native_fused_plan(query, true).await;
|
||||
let fused = find_fused_series(&plan).expect("constant root uses native route");
|
||||
assert!(
|
||||
@@ -2854,6 +2857,10 @@ async fn native_fused_supports_constant_roots_and_selector_broadcasts() {
|
||||
"100 / some_metric",
|
||||
"-some_metric",
|
||||
"rate(some_metric[5m]) * 100",
|
||||
// `pi()` over a selector is the multiplication it was written as, and as a parameter it is
|
||||
// the kernel constant a literal would bind.
|
||||
"pi() * some_metric",
|
||||
"clamp_max(some_metric, pi())",
|
||||
] {
|
||||
let plan = build_native_fused_plan(query, true).await;
|
||||
let fused = find_fused_series(&plan).expect("selector expression uses native route");
|
||||
@@ -4735,13 +4742,17 @@ async fn native_composed_binary_declares_only_include_labels_a_row_can_carry() {
|
||||
|
||||
/// Composition decides by what the kernels and the column can answer, not by operand shape.
|
||||
///
|
||||
/// An operand with more than one selector, and an operand that ranks one, each compile and evaluate
|
||||
/// on their own, so the pair composes.
|
||||
/// An operand with more than one selector, an operand that ranks one and an operand that folds one
|
||||
/// each compile and evaluate on their own, so the pair composes. A `quantile` is such an operand now
|
||||
/// that the runtime folds it: the fold runs inside the operand's own frame, over the series the
|
||||
/// statement's sources answer, and the pair above it matches the two frames.
|
||||
#[tokio::test]
|
||||
async fn native_composed_binary_admits_complex_operands() {
|
||||
for (query, operand_inputs) in [
|
||||
("hist * on(tag_0) (floats + on(tag_0) floats)", 2),
|
||||
("hist * on(tag_0) topk(1, floats)", 1),
|
||||
("hist * on(tag_0) quantile(0.5, floats)", 1),
|
||||
("hist * on(tag_0) sum by (tag_0) (quantile(0.5, floats))", 1),
|
||||
] {
|
||||
let plan =
|
||||
build_native_fused_plan_with_provider(query, true, native_mixed_kind_provider().await)
|
||||
@@ -4767,12 +4778,11 @@ async fn native_composed_binary_admits_complex_operands() {
|
||||
assert!(!display.contains("Join:"), "{query}: {display}");
|
||||
}
|
||||
|
||||
// A fold the split cannot carry is still refused: `quantile` folds a group's samples rather
|
||||
// than a fixed-width accumulator, and a second fold above one is a second cut. Those statements
|
||||
// keep the existing plan, which answers them.
|
||||
// An operand that folds a histogram column is still refused: `sum` has no histogram state to
|
||||
// fold into, so a second fold above one cannot be composed either.
|
||||
for query in [
|
||||
"hist * on(tag_0) quantile(0.5, floats)",
|
||||
"hist * on(tag_0) sum by (tag_0) (quantile(0.5, floats))",
|
||||
"hist * on(tag_0) sum by (tag_0) (hist)",
|
||||
"hist * on(tag_0) sum by (tag_0) (sum(hist))",
|
||||
] {
|
||||
let plan =
|
||||
build_native_fused_plan_with_provider(query, true, native_mixed_kind_provider().await)
|
||||
@@ -6864,24 +6874,18 @@ async fn native_fused_histogram_quantile_binary_drops_le_from_operand_matching()
|
||||
assert!(!display.contains("Join:"), "{display}");
|
||||
}
|
||||
|
||||
/// A legacy wrapper or a native one preserves a whole native binary below it, not a join of native
|
||||
/// operands.
|
||||
/// A whole-vector node preserves a whole native binary below it, not a join of native operands.
|
||||
///
|
||||
/// `scalar()` is a native node, so it becomes the fused root itself and reads the binary's own fused
|
||||
/// node as its single bound input. `quantile` is not a native aggregate, so its legacy fold stays
|
||||
/// above a fused node that still holds the whole binary. Either way the binary matches above its two
|
||||
/// sources rather than joining two separately evaluated native operands.
|
||||
/// `scalar()` and `quantile` are both native nodes, so each becomes the fused root itself and reads
|
||||
/// the binary's own fused node as its single bound input — `scalar()` as `ScalarOf`, `quantile` as
|
||||
/// the `Complete` aggregate it is. Either way the binary matches above its two sources rather than
|
||||
/// joining two separately evaluated native operands.
|
||||
#[tokio::test]
|
||||
async fn native_fused_binary_inside_scalar_and_quantile() {
|
||||
// `true` marks the wrapper the native compiler admits into the tree; `false` keeps the legacy
|
||||
// fold above it.
|
||||
for (query, native_wrapper) in [
|
||||
("scalar(some_metric + on(tag_0) other_metric)", true),
|
||||
("quantile(0.5, some_metric + on(tag_0) other_metric)", false),
|
||||
(
|
||||
"scalar(clamp(some_metric, 0, 1) / on(tag_0) other_metric)",
|
||||
true,
|
||||
),
|
||||
for query in [
|
||||
"scalar(some_metric + on(tag_0) other_metric)",
|
||||
"quantile(0.5, some_metric + on(tag_0) other_metric)",
|
||||
"scalar(clamp(some_metric, 0, 1) / on(tag_0) other_metric)",
|
||||
] {
|
||||
let provider = build_test_table_provider_with_options(
|
||||
&[
|
||||
@@ -6913,44 +6917,46 @@ async fn native_fused_binary_inside_scalar_and_quantile() {
|
||||
"{query}: {display}"
|
||||
);
|
||||
|
||||
let tree = if native_wrapper {
|
||||
// The wrapper is this node's root and binds the binary's fused node as its one input.
|
||||
let NativeExpr::ScalarOf { input } = &fused.tree().root().expr else {
|
||||
panic!(
|
||||
"`{query}`: the native wrapper is the fused root: {}\n{display}",
|
||||
// The wrapper is this node's root and binds the binary's fused node as its one input.
|
||||
let (input, wrapper) = match &fused.tree().root().expr {
|
||||
NativeExpr::ScalarOf { input } => (input.as_ref(), "scalar()"),
|
||||
NativeExpr::Aggregate { input, op } => {
|
||||
assert!(
|
||||
matches!(op.kind, AggregateKind::Quantile { .. }),
|
||||
"`{query}`: the fused root is not the quantile it wrote: {}\n{display}",
|
||||
fused.tree()
|
||||
);
|
||||
};
|
||||
let NativeExpr::Input(bound) = &input.expr else {
|
||||
panic!(
|
||||
"`{query}`: the wrapper reads one bound source: {}\n{display}",
|
||||
fused.tree()
|
||||
);
|
||||
};
|
||||
// The wrapper's tree has one input, so the binding names child 0: the segment that runs
|
||||
// the binary.
|
||||
assert_eq!(
|
||||
0,
|
||||
bound.source_index,
|
||||
"`{query}`: {}\n{display}",
|
||||
(input.as_ref(), "quantile()")
|
||||
}
|
||||
other => panic!(
|
||||
"`{query}`: the native wrapper is the fused root, and it is {other:?}: {}\n{display}",
|
||||
fused.tree()
|
||||
),
|
||||
};
|
||||
let NativeExpr::Input(bound) = &input.expr else {
|
||||
panic!(
|
||||
"`{query}`: {wrapper} reads one bound source: {}\n{display}",
|
||||
fused.tree()
|
||||
);
|
||||
assert_eq!(1, fused.children().len(), "`{query}`: {display}");
|
||||
let nested = find_fused_series(&fused.children()[0]).unwrap_or_else(|| {
|
||||
panic!("`{query}`: the wrapper's input is a fused node: {display}")
|
||||
});
|
||||
// The binary's node reads both sources itself, so it has one child per operand.
|
||||
assert_eq!(2, nested.children().len(), "`{query}`: {display}");
|
||||
nested.tree()
|
||||
} else {
|
||||
// The legacy fold is still there, above the one fused node that runs the binary.
|
||||
assert!(
|
||||
display.contains("Aggregate: groupBy="),
|
||||
"`{query}`: the legacy aggregate is gone: {display}"
|
||||
);
|
||||
assert_eq!(2, fused.children().len(), "`{query}`: {display}");
|
||||
fused.tree()
|
||||
};
|
||||
// The wrapper's tree has one input, so the binding names child 0: the segment that runs the
|
||||
// binary.
|
||||
assert_eq!(
|
||||
0,
|
||||
bound.source_index,
|
||||
"`{query}`: {}\n{display}",
|
||||
fused.tree()
|
||||
);
|
||||
assert_eq!(1, fused.children().len(), "`{query}`: {display}");
|
||||
let nested = find_fused_series(&fused.children()[0])
|
||||
.unwrap_or_else(|| panic!("`{query}`: the wrapper's input is a fused node: {display}"));
|
||||
// The binary's node reads both sources itself, so it has one child per operand.
|
||||
assert_eq!(2, nested.children().len(), "`{query}`: {display}");
|
||||
let tree = nested.tree();
|
||||
assert!(
|
||||
!display.contains("Aggregate: groupBy="),
|
||||
"`{query}`: {display}"
|
||||
);
|
||||
assert!(
|
||||
matches!(tree.root().expr, NativeExpr::Binary { .. }),
|
||||
"`{query}`: the binary is one native operation: {tree}\n{display}"
|
||||
@@ -6959,8 +6965,7 @@ async fn native_fused_binary_inside_scalar_and_quantile() {
|
||||
}
|
||||
|
||||
/// A shape neither the split nor the node can keep correct keeps the existing plan: a group by a
|
||||
/// value field cannot be named by the state rows, and a kernel the compiler does not fold keeps it
|
||||
/// too.
|
||||
/// value field cannot be named by the state rows.
|
||||
///
|
||||
/// `sum(m) / m` and `m + sum(m)` are not on this list. The fold and the plain selector are two
|
||||
/// occurrences, so each reads its own source and the operation above them answers over both; the
|
||||
@@ -6971,13 +6976,13 @@ async fn native_fused_binary_inside_scalar_and_quantile() {
|
||||
/// makes a new sample at the evaluation instant rather than inheriting a stored one's time - and
|
||||
/// `instant_eval.rs` says so where it projects the grid's own timestamps. The segmentation route
|
||||
/// answers those, and `native_promql_timestamp_over_a_computed_vector` pins the values.
|
||||
///
|
||||
/// `quantile` is not on this list either: it has no mergeable state, so it cannot be split, but a
|
||||
/// `Complete` one is a node this runtime computes - the names in this list are the shapes even that
|
||||
/// node cannot answer.
|
||||
#[tokio::test]
|
||||
async fn native_fused_unsplittable_aggregate_falls_back() {
|
||||
for query in [
|
||||
"quantile(0.5, some_metric)",
|
||||
"sum(quantile(0.5, some_metric))",
|
||||
"sum by (field_0) (some_metric)",
|
||||
] {
|
||||
for query in ["sum by (field_0) (some_metric)"] {
|
||||
let native = build_native_fused_plan(query, true).await;
|
||||
let without_flag = build_native_fused_plan(query, false).await;
|
||||
assert!(
|
||||
@@ -6998,6 +7003,227 @@ async fn native_fused_unsplittable_aggregate_falls_back() {
|
||||
}
|
||||
}
|
||||
|
||||
/// A `quantile` is native as the one `Complete` aggregate it can be.
|
||||
///
|
||||
/// Its answer is the φ-quantile of a group's whole sample set, so the runtime keeps the input and
|
||||
/// answers from all of it once the input ends. The tree therefore carries one `Complete` aggregate
|
||||
/// with the φ the query wrote and the grouping it named, and it carries it under a second aggregate
|
||||
/// too: neither the split nor the segmentation cuts a `quantile` out, because it exports no state to
|
||||
/// combine and a partition's quantile is not part of the group's.
|
||||
#[tokio::test]
|
||||
async fn native_fused_quantile_is_a_complete_aggregate() {
|
||||
for (query, quantile, grouped, columns) in [
|
||||
(
|
||||
"quantile(0.5, some_metric)",
|
||||
0.5f64,
|
||||
false,
|
||||
vec!["__name__", "quantile(0.5, some_metric)", "timestamp"],
|
||||
),
|
||||
(
|
||||
"quantile(0.9, some_metric) by (tag_0)",
|
||||
0.9f64,
|
||||
true,
|
||||
vec![
|
||||
"__name__",
|
||||
"quantile by (tag_0) (0.9, some_metric)",
|
||||
"tag_0",
|
||||
"timestamp",
|
||||
],
|
||||
),
|
||||
(
|
||||
"sum(quantile(0.5, some_metric))",
|
||||
0.5f64,
|
||||
false,
|
||||
vec!["__name__", "sum(quantile(0.5, some_metric))", "timestamp"],
|
||||
),
|
||||
] {
|
||||
let plan = build_native_fused_plan(query, true).await;
|
||||
let display = plan.display_indent().to_string();
|
||||
let fused = find_fused_series(&plan).unwrap_or_else(|| panic!("`{query}`: {display}"));
|
||||
// A quantile exports no state, so there is no split half for it: the statement is one
|
||||
// execution's work.
|
||||
assert!(find_final_split(&plan).is_none(), "`{query}`: {display}");
|
||||
let mut node = fused.tree().root();
|
||||
if query.starts_with("sum(") {
|
||||
// The outer fold reads the quantile's answer in the same execution, so the quantile is
|
||||
// the node below it rather than a boundary between two of them.
|
||||
let NativeExpr::Aggregate { input, op } = &node.expr else {
|
||||
panic!("`{query}`: the root is not the outer sum: {display}");
|
||||
};
|
||||
assert!(
|
||||
matches!(op.kind, AggregateKind::Sum) && op.stage == AggregateStage::Complete,
|
||||
"`{query}`: {display}"
|
||||
);
|
||||
node = input.as_ref();
|
||||
}
|
||||
let NativeExpr::Aggregate { input, op } = &node.expr else {
|
||||
panic!("`{query}`: the node is not an aggregate: {display}");
|
||||
};
|
||||
assert!(
|
||||
matches!(
|
||||
op.kind,
|
||||
AggregateKind::Quantile { quantile: value } if value.to_bits() == quantile.to_bits()
|
||||
),
|
||||
"`{query}`: {display}"
|
||||
);
|
||||
assert_eq!(AggregateStage::Complete, op.stage, "`{query}`: {display}");
|
||||
assert_eq!(
|
||||
grouped,
|
||||
matches!(&op.group, Grouping::By(labels) if !labels.is_empty()),
|
||||
"`{query}`: {display}"
|
||||
);
|
||||
assert!(
|
||||
matches!(
|
||||
input.expr,
|
||||
NativeExpr::Instant { .. } | NativeExpr::Input(_)
|
||||
),
|
||||
"`{query}`: the quantile reads the selection, cut or not: {display}"
|
||||
);
|
||||
// The quantile answers a float per group, under the fields and labels it read.
|
||||
assert_eq!(columns, sorted_column_names(&plan), "`{query}`");
|
||||
}
|
||||
}
|
||||
|
||||
/// A `quantile` over a histogram column folds it natively, exactly as `min` and `max` do.
|
||||
///
|
||||
/// The phi-quantile orders the samples it reads, so a histogram is one it cannot order: Prometheus
|
||||
/// answers the quantile of the float samples of the group and skips the histogram ones, which the
|
||||
/// legacy planner writes as the same drop-and-Float-NULL branch it gives `min`/`max`/`stddev`/
|
||||
/// `stdvar` (`create_native_histogram_aggregate_expr`). The native fold therefore reads a histogram
|
||||
/// where `min` reads one - it skips it the same way, and reports the same "ignored native histogram
|
||||
/// samples" info at run time - so the admission lets it through instead of keeping the plan it had.
|
||||
#[tokio::test]
|
||||
async fn native_fused_quantile_over_a_histogram_folds_it_like_min() {
|
||||
let query = "quantile(0.5, some_metric)";
|
||||
let native = build_native_fused_plan_with_provider(
|
||||
query,
|
||||
true,
|
||||
build_test_native_histogram_table_provider("some_metric").await,
|
||||
)
|
||||
.await;
|
||||
let display = native.display_indent_schema().to_string();
|
||||
let fused = find_fused_series(&native)
|
||||
.unwrap_or_else(|| panic!("a quantile over a histogram left the route: {display}"));
|
||||
// The answer is one float per group, not a column carrying the histograms it skipped. The
|
||||
// column is named after the expression now, so the type is what says it is not a histogram.
|
||||
let float_fields = fused
|
||||
.schema()
|
||||
.fields()
|
||||
.iter()
|
||||
.filter(|field| field.data_type() == &ArrowDataType::Float64)
|
||||
.count();
|
||||
assert!(
|
||||
float_fields >= 1,
|
||||
"the quantile published no float column: {display}"
|
||||
);
|
||||
assert!(
|
||||
fused
|
||||
.schema()
|
||||
.fields()
|
||||
.iter()
|
||||
.all(|field| field.data_type() != &native_histogram_value_type().as_arrow_type()),
|
||||
"a quantile answers a float, not a histogram: {display}"
|
||||
);
|
||||
// The fold is the one `Complete` aggregate the quantile can be, reading the selection it was
|
||||
// written over.
|
||||
let NativeExpr::Aggregate { input, op } = &fused.tree().root().expr else {
|
||||
panic!("the fused root is not the quantile: {display}");
|
||||
};
|
||||
assert!(
|
||||
matches!(op.kind, AggregateKind::Quantile { .. }) && op.stage == AggregateStage::Complete,
|
||||
"{display}"
|
||||
);
|
||||
assert!(
|
||||
matches!(
|
||||
input.expr,
|
||||
NativeExpr::Instant { .. } | NativeExpr::Input(_)
|
||||
),
|
||||
"the quantile reads the selection: {display}"
|
||||
);
|
||||
// The statement is planned as `min` is: the same columns, one float per group, over the same
|
||||
// labels and timestamps.
|
||||
let like_min = build_native_fused_plan_with_provider(
|
||||
"min(some_metric)",
|
||||
true,
|
||||
build_test_native_histogram_table_provider("some_metric").await,
|
||||
)
|
||||
.await;
|
||||
assert!(
|
||||
find_fused_series(&like_min).is_some(),
|
||||
"min over a histogram stopped folding natively: {}",
|
||||
like_min.display_indent()
|
||||
);
|
||||
// The columns are named after their expressions now, so the shape is what says the two are
|
||||
// planned alike: one float value column beside the name and the timestamp.
|
||||
let column_types = |plan: &LogicalPlan| {
|
||||
let mut types: Vec<_> = plan
|
||||
.schema()
|
||||
.fields()
|
||||
.iter()
|
||||
.map(|field| (field.name().clone(), field.data_type().clone()))
|
||||
.collect();
|
||||
types.sort_by(|a, b| a.0.cmp(&b.0));
|
||||
types.into_iter().map(|(_, ty)| ty).collect::<Vec<_>>()
|
||||
};
|
||||
assert_eq!(
|
||||
column_types(&native),
|
||||
column_types(&like_min),
|
||||
"a quantile over a histogram is planned like min: {display}"
|
||||
);
|
||||
// The route is the setting's doing, which is what the refusal above used to hide.
|
||||
let without_flag = build_native_fused_plan_with_provider(
|
||||
query,
|
||||
false,
|
||||
build_test_native_histogram_table_provider("some_metric").await,
|
||||
)
|
||||
.await;
|
||||
assert!(
|
||||
find_fused_series(&without_flag).is_none(),
|
||||
"the existing planner does not use the native route: {}",
|
||||
without_flag.display_indent()
|
||||
);
|
||||
}
|
||||
|
||||
/// A `quantile` is never folded once per partition, whatever shape the table behind it has.
|
||||
///
|
||||
/// The split is the route that answers an aggregate per partition and merges the state, so a
|
||||
/// `quantile` - whose state is its group's sample set - has no split half anywhere: `check_cut`
|
||||
/// declines the kind, and the tree itself refuses a `Partial` or `Final` one. What runs instead is a
|
||||
/// single `Complete` fold above the per-series prefixes. A table partitioned on a tag keeps every
|
||||
/// series whole in one region; a table that splits one series across regions is read through a raw
|
||||
/// source that reconciles its fragments, so the fold still sees whole series. Both shapes are
|
||||
/// asserted here, because "one fold, never one per partition" is the invariant and "falls back" is
|
||||
/// not: gathering the whole vector into one execution is not folding it by partition.
|
||||
#[tokio::test]
|
||||
async fn native_fused_quantile_is_never_folded_per_partition() {
|
||||
for partition_key_indices in [vec![0], vec![1]] {
|
||||
let query = "quantile(0.5, some_metric)";
|
||||
let plan = build_native_fused_plan_with_provider(
|
||||
query,
|
||||
true,
|
||||
native_engine_provider(partition_key_indices.clone()).await,
|
||||
)
|
||||
.await;
|
||||
let display = plan.display_indent().to_string();
|
||||
assert!(
|
||||
find_fused_series(&plan).is_some(),
|
||||
"partition on {partition_key_indices:?}: {display}"
|
||||
);
|
||||
// No `Final` over a `Partial`: a quantile has no state to export, so the fold is one node's
|
||||
// work over the whole vector rather than one per partition.
|
||||
assert!(
|
||||
find_final_split(&plan).is_none(),
|
||||
"partition on {partition_key_indices:?}: the quantile must not be split: {display}"
|
||||
);
|
||||
// The fold is above the prefixes: the node that holds it reads a segment rather than the
|
||||
// scan, and the statement is still named by the quantile it wrote.
|
||||
assert!(
|
||||
display.contains("quantile(0.5, some_metric)"),
|
||||
"partition on {partition_key_indices:?}: {display}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// A fold beside an independent selector is two occurrences, so each reads its own source and the
|
||||
/// operation runs above both. A scalar over the second occurrence requires the whole vector and
|
||||
/// therefore gathers the inputs before evaluation.
|
||||
@@ -10394,8 +10620,9 @@ async fn a_float_prefix_over_histograms_aggregates_natively() {
|
||||
/// function that answers histograms and scalar arithmetic over them all run here, while an
|
||||
/// aggregate that would fold them keeps the existing plan. `rate` is in the histogram-answering
|
||||
/// set, so `sum(rate(h[5m]))` is refused by the same rule as `sum(h)`.
|
||||
/// A histogram is counted by `count`/`group` and skipped by `min`/`max`/`stddev`/`stdvar`, so all
|
||||
/// of them fold it natively; only `sum` and `avg` fold the payload and keep the existing plan.
|
||||
/// A histogram is counted by `count`/`group` and skipped by `min`/`max`/`stddev`/`stdvar`/
|
||||
/// `quantile`, so all of them fold it natively. Only `sum` and `avg`, which fold the payload, keep
|
||||
/// the existing plan.
|
||||
///
|
||||
/// Prometheus counts every present sample whatever its type is, answers `1` for a non-empty group,
|
||||
/// and ignores a histogram where it would have to order or average the values. The second half
|
||||
@@ -10409,6 +10636,7 @@ async fn presence_and_order_aggregates_fold_histograms_natively() {
|
||||
"max(some_metric)",
|
||||
"stddev(some_metric)",
|
||||
"stdvar(some_metric)",
|
||||
"quantile(0.9, some_metric)",
|
||||
] {
|
||||
let table_provider = build_test_native_histogram_table_provider("some_metric").await;
|
||||
let plan = build_native_fused_plan_with_provider(query, true, table_provider).await;
|
||||
@@ -10427,10 +10655,6 @@ async fn presence_and_order_aggregates_fold_histograms_natively() {
|
||||
for query in [
|
||||
"sum(some_metric)",
|
||||
"avg(some_metric)",
|
||||
// `quantile` has no kernel in this runtime: the admission check lets it through and the
|
||||
// runtime's own construction refuses it, which keeps the statement on the plan it had
|
||||
// rather than making it count or skip histogram samples here.
|
||||
"quantile(0.9, some_metric)",
|
||||
// A skipping or counting aggregate at the root does not excuse the payload-folding one
|
||||
// below it: `sum` still has no histogram state to fold into.
|
||||
"count(sum(some_metric))",
|
||||
|
||||
@@ -12122,10 +12122,9 @@ async fn native_promql_answers_a_selection_that_depends_on_input_order() {
|
||||
/// The multi-input route skips the aggregate split, and the split is what refuses a tree the runtime
|
||||
/// cannot run. The native node is chosen while planning the logical plan, so a refusal the physical
|
||||
/// layer raises later is an error the statement reports, not a fall back - which would turn a query
|
||||
/// the existing plan answers into a failure. `quantile` is outside the eight complete kinds the
|
||||
/// runtime computes, and a grouping by a value field needs a per-point adapter it does not have, so
|
||||
/// both are checked here on the shape that would otherwise reach the node: two aggregates, one per
|
||||
/// logical table of one physical table, which is the multi-input case.
|
||||
/// the existing plan answers into a failure. A grouping by a value field needs a per-point adapter
|
||||
/// the runtime does not have, so it is checked here on the shape that would otherwise reach the
|
||||
/// node: two aggregates, one per logical table of one physical table, which is the multi-input case.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn native_promql_declines_a_tree_the_runtime_cannot_execute() {
|
||||
common_telemetry::init_default_ut_logging();
|
||||
@@ -12158,34 +12157,94 @@ async fn native_promql_declines_a_tree_the_runtime_cannot_execute() {
|
||||
)
|
||||
.await;
|
||||
|
||||
for query in [
|
||||
// `quantile` computes a value the eight complete kinds do not, so the runtime refuses it.
|
||||
"quantile(0.5, native_unexec_demo) / sum(native_unexec_other)",
|
||||
// The outer `count` clears the labels the inner grouping writes, so no column check stops
|
||||
// this one: the refusal is the grouping by a value field, which the runtime has no adapter
|
||||
// for.
|
||||
"count(count by (val) (native_unexec_demo)) / sum(native_unexec_other)",
|
||||
] {
|
||||
let sql = format!("TQL EVAL (0, 0, '1s') {query}");
|
||||
let (_, legacy_rows) =
|
||||
native_labelled_rows(native_tql_output(&instance, &sql, false).await).await;
|
||||
assert!(
|
||||
!legacy_rows.is_empty(),
|
||||
"{query}: the existing plan must answer this, otherwise there is nothing to keep"
|
||||
);
|
||||
// The outer `count` clears the labels the inner grouping writes, so no column check stops this
|
||||
// one: the refusal is the grouping by a value field, which the runtime has no adapter for.
|
||||
let query = "count(count by (val) (native_unexec_demo)) / sum(native_unexec_other)";
|
||||
let sql = format!("TQL EVAL (0, 0, '1s') {query}");
|
||||
let (_, legacy_rows) =
|
||||
native_labelled_rows(native_tql_output(&instance, &sql, false).await).await;
|
||||
assert!(
|
||||
!legacy_rows.is_empty(),
|
||||
"{query}: the existing plan must answer this, otherwise there is nothing to keep"
|
||||
);
|
||||
|
||||
observer.clear();
|
||||
let (_, native_rows) =
|
||||
native_labelled_rows(native_tql_output(&instance, &sql, true).await).await;
|
||||
assert_eq!(
|
||||
native_rows, legacy_rows,
|
||||
"{query}: a tree the runtime cannot execute must keep the existing plan"
|
||||
);
|
||||
assert!(
|
||||
observer.fused().is_none(),
|
||||
"{query}: the route must decline it instead of planning a node the runtime refuses"
|
||||
);
|
||||
}
|
||||
observer.clear();
|
||||
let (_, native_rows) =
|
||||
native_labelled_rows(native_tql_output(&instance, &sql, true).await).await;
|
||||
assert_eq!(
|
||||
native_rows, legacy_rows,
|
||||
"{query}: a tree the runtime cannot execute must keep the existing plan"
|
||||
);
|
||||
assert!(
|
||||
observer.fused().is_none(),
|
||||
"{query}: the route must decline it instead of planning a node the runtime refuses"
|
||||
);
|
||||
}
|
||||
|
||||
/// A `quantile` over one logical table, divided by a `sum` over another, answers natively and the
|
||||
/// rows are the existing plan's.
|
||||
///
|
||||
/// The two tables are one physical table, so the statement has two inputs and the multi-input route
|
||||
/// answers it without the aggregate split - the split is what refuses a `quantile`, whose state is
|
||||
/// the group's samples rather than one of the eight fixed-width accumulators. The runtime computes
|
||||
/// the `Complete` stage of a `quantile` itself, so the tree is answerable and the route adopts it;
|
||||
/// the answer must still be the existing plan's. Both tables carry samples of 2 and 4 on the left,
|
||||
/// so the median is their midpoint 3, and of 8 and 8 on the right, so the sum is 16: the answer is
|
||||
/// one unlabelled row at `3 / 16`.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn native_promql_answers_a_quantile_over_a_multi_input_tree() {
|
||||
common_telemetry::init_default_ut_logging();
|
||||
let (standalone, observer) = native_test_instance(GreptimeDbStandaloneBuilder::new(
|
||||
"native_promql_quantile_multi_input",
|
||||
))
|
||||
.await;
|
||||
let instance = standalone.fe_instance().clone();
|
||||
execute_all(
|
||||
&instance,
|
||||
r#"CREATE TABLE native_quant_phy (
|
||||
ts TIMESTAMP TIME INDEX,
|
||||
val DOUBLE
|
||||
) ENGINE=metric WITH ("physical_metric_table" = "true");
|
||||
CREATE TABLE native_quant_demo (
|
||||
host STRING,
|
||||
ts TIMESTAMP TIME INDEX,
|
||||
val DOUBLE,
|
||||
PRIMARY KEY (host)
|
||||
) ENGINE=metric WITH ("on_physical_table" = 'native_quant_phy');
|
||||
CREATE TABLE native_quant_other (
|
||||
host STRING,
|
||||
ts TIMESTAMP TIME INDEX,
|
||||
val DOUBLE,
|
||||
PRIMARY KEY (host)
|
||||
) ENGINE=metric WITH ("on_physical_table" = 'native_quant_phy');
|
||||
INSERT INTO native_quant_demo (host, ts, val) VALUES ('a', 0, 2), ('b', 0, 4);
|
||||
INSERT INTO native_quant_other (host, ts, val) VALUES ('a', 0, 8), ('b', 0, 8);"#,
|
||||
QueryContext::arc(),
|
||||
)
|
||||
.await;
|
||||
|
||||
let query = "quantile(0.5, native_quant_demo) / sum(native_quant_other)";
|
||||
let sql = format!("TQL EVAL (0, 0, '1s') {query}");
|
||||
let (_, legacy_rows) =
|
||||
native_labelled_rows(native_tql_output(&instance, &sql, false).await).await;
|
||||
assert_eq!(
|
||||
legacy_rows,
|
||||
vec![(0, std::collections::BTreeMap::new(), 3.0 / 16.0)],
|
||||
"{query}: the existing plan answers the median divided by the sum"
|
||||
);
|
||||
|
||||
observer.clear();
|
||||
let (_, native_rows) =
|
||||
native_labelled_rows(native_tql_output(&instance, &sql, true).await).await;
|
||||
assert!(
|
||||
observer.fused().is_some(),
|
||||
"{query}: the runtime computes a `Complete` quantile, so the route answers the tree: {}",
|
||||
display_plan_tree(&observer.root_plan())
|
||||
);
|
||||
assert_eq!(
|
||||
native_rows, legacy_rows,
|
||||
"{query}: the native answer must be the existing plan's rows"
|
||||
);
|
||||
}
|
||||
|
||||
/// OTLP delta temporality on the public TQL route: a `delta`-marked series answers the raw sample
|
||||
|
||||
Reference in New Issue
Block a user