From 3734a0f1d3879737f8fac4f9bc7f8c149befefe8 Mon Sep 17 00:00:00 2001 From: discord9 <55937128+discord9@users.noreply.github.com> Date: Mon, 19 Oct 2026 15:45:59 +0800 Subject: [PATCH] refactor(promql): use the evaluator's chunk type for native batches `native/batch.rs` defined its own `EvaluatedChunk` with the same two fields as `promql::evaluated::EvaluatedChunk`. Two definitions of one protocol drift: the evaluator's chunk could not be handed to a native node without translation, and a change to either would have to be mirrored. Use `crate::evaluated::EvaluatedChunk` instead. The type moves here with the evaluator's documentation, which states the same two rules this module did: a grid position is absolute, and `is_last` is the only thing that closes a series. The module keeps its own doc explaining the rules native nodes rely on and re-exports the type, so `native::batch::EvaluatedChunk` still names it. Signed-off-by: discord9 Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> --- src/promql/src/native/batch.rs | 46 +++++----------------------------- 1 file changed, 6 insertions(+), 40 deletions(-) diff --git a/src/promql/src/native/batch.rs b/src/promql/src/native/batch.rs index eec665130f7..f8f6f0c2b93 100644 --- a/src/promql/src/native/batch.rs +++ b/src/promql/src/native/batch.rs @@ -25,47 +25,13 @@ //! prefix still has to be followed by a final chunk even when that chunk holds no values, so a //! consumer can tell "nothing more here" from "no more input". -use datafusion::error::{DataFusionError, Result}; -use datatypes::series::samples::Series; -use datatypes::series::time::TimeAxis; +//! +//! The chunk type itself is [`crate::evaluated::EvaluatedChunk`]: one chunk protocol serves both the +//! evaluator that produces chunks and the native nodes that consume them. Defining a second one +//! here would let the two drift apart, and a chunk produced by the evaluator could not be handed to +//! a node without translation. -/// One slice of one evaluated series. -/// -/// The series' [`TimeAxis::Grid`] covers the positions this chunk has finished evaluating. Those -/// positions are absolute on the grid, and they are contiguous with the previous chunk of the same -/// series. -#[derive(Clone, Debug)] -pub struct EvaluatedChunk { - /// The evaluated values, addressed on their absolute grid positions. - pub series: Series, - /// Whether this is the final chunk of this series. - pub is_last: bool, -} - -impl EvaluatedChunk { - /// Wraps `series`, checking that it is addressed on a grid. - pub fn try_new(series: Series, is_last: bool) -> Result { - if !matches!(series.time, TimeAxis::Grid(_)) { - return Err(DataFusionError::Execution( - "an evaluated chunk must be addressed on a grid".to_string(), - )); - } - Ok(Self { series, is_last }) - } - - /// The absolute grid range this chunk covers. - /// - /// An empty range is meaningful: it reports that the chunk closed a prefix of the grid without - /// producing new values, which a consumer needs in order to advance. - pub fn range(&self) -> &datatypes::series::time::GridRange { - match &self.series.time { - TimeAxis::Grid(range) => range, - // `try_new` rejects anything else, and `series` is public, so a chunk built by struct - // literal is checked here rather than trusted. - _ => unreachable!("an evaluated chunk is always addressed on a grid"), - } - } -} +pub use crate::evaluated::EvaluatedChunk; #[cfg(test)] mod tests {