refactor(promql): build native runtime nodes through one tree builder (#10779)

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
This commit is contained in:
dennis zhuang
2026-12-01 13:37:18 +00:00
committed by GitHub
parent 029ecd8ecf
commit 9ec4ad4d2a
3 changed files with 292 additions and 608 deletions
File diff suppressed because it is too large Load Diff
+64 -37
View File
@@ -44,9 +44,9 @@ fn is_resources_exhausted(error: &DataFusionError) -> bool {
use super::{
BinaryRuntime, InputId, InputSlot, InstantRuntime, NativeChunk, NativeExecutionState,
NativeExecutor, NodeState, PairKernel, Reader, RetainedInput, RuntimeNode, RuntimeOp,
SourceFacts, buffered_fields, buffered_time, evaluated_bytes, grid_chunk_bytes, grid_end_ms,
grid_position, map_chunk, map_workspace_bytes, raw_bytes, vector_series_with_k,
NativeExecutor, NodeState, PairKernel, Reader, RetainedInput, RuntimeNode, RuntimeOp, Side,
SourceFacts, TreeBuilder, buffered_fields, buffered_time, evaluated_bytes, grid_chunk_bytes,
grid_end_ms, grid_position, map_chunk, map_workspace_bytes, raw_bytes, vector_series_with_k,
vector_workspace_bytes,
};
use crate::evaluated::EvaluatedChunk;
@@ -416,18 +416,9 @@ fn window_and_limitk_collect_only_unified_contributions() {
let build_with = |node: NativeNode, sources: &SourceFacts| {
let tree = NativeTree::try_new(node).unwrap();
let mut inputs = Vec::new();
let mut binary_before = 0;
RuntimeNode::build(
tree.root(),
&pool(),
&mut inputs,
&mut binary_before,
"root".to_string(),
None,
sources,
)
.unwrap()
TreeBuilder::new(&pool(), None, sources)
.build(tree.root(), "root".to_string())
.unwrap()
};
let build = |node: NativeNode| build_with(node, &SourceFacts::none());
@@ -931,10 +922,18 @@ fn binary_charge(
) -> (usize, usize) {
let (mut state, mut binary) = binary_runtime(&pool(), schema);
state
.retain_binary(&mut binary, 0, NativeChunk::Evaluated(left.clone()))
.retain_binary(
&mut binary,
Side::Left,
NativeChunk::Evaluated(left.clone()),
)
.unwrap();
state
.retain_binary(&mut binary, 1, NativeChunk::Evaluated(right.clone()))
.retain_binary(
&mut binary,
Side::Right,
NativeChunk::Evaluated(right.clone()),
)
.unwrap();
let retained = state.buffer.reservation.size();
let assembly = binary
@@ -7679,10 +7678,10 @@ fn a_binary_operand_reserves_what_it_retains() {
let pool = bounded_pool(rows + descriptors);
let (mut state, mut binary) = binary_runtime(&pool, &schema);
state
.retain_binary(&mut binary, 0, NativeChunk::Evaluated(first))
.retain_binary(&mut binary, Side::Left, NativeChunk::Evaluated(first))
.unwrap();
state
.retain_binary(&mut binary, 0, NativeChunk::Evaluated(second))
.retain_binary(&mut binary, Side::Left, NativeChunk::Evaluated(second))
.unwrap();
assert!(
state.buffer.reservation.size() >= rows,
@@ -7705,10 +7704,10 @@ fn a_binary_operand_reserves_what_it_retains() {
// one reserved for and then retained leaves the operand exactly where the reservation put it.
let own = evaluated_chunk(&schema, 0, vec![1.0, 2.0], true);
let (mut own_state, mut own_binary) = binary_runtime(&pool, &schema);
let pending = own_binary.pending(0, &own).unwrap();
let pending = own_binary.pending(Side::Left, &own);
let before = own_state.buffer.reservation.size();
own_state
.retain_binary(&mut own_binary, 0, NativeChunk::Evaluated(own))
.retain_binary(&mut own_binary, Side::Left, NativeChunk::Evaluated(own))
.unwrap();
assert_eq!(
own_state.buffer.reservation.size() - before,
@@ -7769,12 +7768,12 @@ fn a_binary_operand_bills_each_histogram_table_it_retains() {
let (mut state, mut binary) = binary_runtime(&pool, &schema);
let before = state.buffer.reservation.size();
state
.retain_binary(&mut binary, 0, NativeChunk::Evaluated(first))
.retain_binary(&mut binary, Side::Left, NativeChunk::Evaluated(first))
.unwrap();
let first_cost = state.buffer.reservation.size() - before;
let before = state.buffer.reservation.size();
state
.retain_binary(&mut binary, 0, NativeChunk::Evaluated(second))
.retain_binary(&mut binary, Side::Left, NativeChunk::Evaluated(second))
.unwrap();
let second_cost = state.buffer.reservation.size() - before;
@@ -7822,12 +7821,12 @@ fn a_binary_operand_bills_a_shared_histogram_table_once() {
let (mut state, mut binary) = binary_runtime(&pool, &schema);
let before = state.buffer.reservation.size();
state
.retain_binary(&mut binary, 0, NativeChunk::Evaluated(first))
.retain_binary(&mut binary, Side::Left, NativeChunk::Evaluated(first))
.unwrap();
let first_cost = state.buffer.reservation.size() - before;
let before = state.buffer.reservation.size();
state
.retain_binary(&mut binary, 0, NativeChunk::Evaluated(second))
.retain_binary(&mut binary, Side::Left, NativeChunk::Evaluated(second))
.unwrap();
let second_cost = state.buffer.reservation.size() - before;
@@ -7891,7 +7890,11 @@ fn a_pool_that_holds_one_histogram_table_refuses_a_second() {
// anything; the pool below is sized from it and half a table.
let (mut probe_state, mut probe_binary) = binary_runtime(&pool(), &schema);
probe_state
.retain_binary(&mut probe_binary, 0, NativeChunk::Evaluated(first.clone()))
.retain_binary(
&mut probe_binary,
Side::Left,
NativeChunk::Evaluated(first.clone()),
)
.unwrap();
let one_chunk = probe_state.buffer.reservation.size();
let budget = one_chunk + bytes / 2;
@@ -7902,12 +7905,16 @@ fn a_pool_that_holds_one_histogram_table_refuses_a_second() {
refused_state
.retain_binary(
&mut refused_binary,
0,
Side::Left,
NativeChunk::Evaluated(first.clone()),
)
.unwrap();
let error = refused_state
.retain_binary(&mut refused_binary, 0, NativeChunk::Evaluated(second_own))
.retain_binary(
&mut refused_binary,
Side::Left,
NativeChunk::Evaluated(second_own),
)
.unwrap_err();
assert!(
is_resources_exhausted(&error),
@@ -7922,10 +7929,18 @@ fn a_pool_that_holds_one_histogram_table_refuses_a_second() {
// The second slice of the one table the operand holds does fit.
let (mut shared_state, mut shared_binary) = binary_runtime(&bounded_pool(budget), &schema);
shared_state
.retain_binary(&mut shared_binary, 0, NativeChunk::Evaluated(first))
.retain_binary(
&mut shared_binary,
Side::Left,
NativeChunk::Evaluated(first),
)
.unwrap();
shared_state
.retain_binary(&mut shared_binary, 0, NativeChunk::Evaluated(second_shared))
.retain_binary(
&mut shared_binary,
Side::Left,
NativeChunk::Evaluated(second_shared),
)
.unwrap();
assert!(
shared_state.buffer.reservation.size() > one_chunk,
@@ -8200,11 +8215,11 @@ fn an_assembly_bound_covers_the_capacity_its_flatten_grows_to() {
let (mut state, mut binary) = binary_runtime(&pool, &schema);
for chunk in [first, second.clone()] {
state
.retain_binary(&mut binary, 0, NativeChunk::Evaluated(chunk))
.retain_binary(&mut binary, Side::Left, NativeChunk::Evaluated(chunk))
.unwrap();
}
state
.retain_binary(&mut binary, 1, NativeChunk::Evaluated(second))
.retain_binary(&mut binary, Side::Right, NativeChunk::Evaluated(second))
.unwrap();
let assembly = binary.assembly_bytes().unwrap();
assert!(
@@ -8248,10 +8263,18 @@ fn an_assembly_the_pool_cannot_hold_is_refused_before_it_allocates() {
let pool = bounded_pool(retained + assembly - 1);
let (mut state, mut binary) = binary_runtime(&pool, &schema);
state
.retain_binary(&mut binary, 0, NativeChunk::Evaluated(left.clone()))
.retain_binary(
&mut binary,
Side::Left,
NativeChunk::Evaluated(left.clone()),
)
.unwrap();
state
.retain_binary(&mut binary, 1, NativeChunk::Evaluated(right.clone()))
.retain_binary(
&mut binary,
Side::Right,
NativeChunk::Evaluated(right.clone()),
)
.unwrap();
assert_eq!(state.buffer.reservation.size(), retained);
let error = state.compute_binary(&mut binary).unwrap_err();
@@ -8274,10 +8297,10 @@ fn an_assembly_the_pool_cannot_hold_is_refused_before_it_allocates() {
let pool = bounded_pool(retained + assembly);
let (mut state, mut binary) = binary_runtime(&pool, &schema);
state
.retain_binary(&mut binary, 0, NativeChunk::Evaluated(left))
.retain_binary(&mut binary, Side::Left, NativeChunk::Evaluated(left))
.unwrap();
state
.retain_binary(&mut binary, 1, NativeChunk::Evaluated(right))
.retain_binary(&mut binary, Side::Right, NativeChunk::Evaluated(right))
.unwrap();
let assembled = binary.assemble().unwrap();
assert_eq!(assembled[0].len(), 1, "one closed series is one series");
@@ -8353,7 +8376,11 @@ fn a_mixed_nested_operand_assembles_into_one_series_with_its_holes() {
let (mut state, mut binary) = binary_runtime(&pool, &schema);
for chunk in [&left_a, &left_b] {
state
.retain_binary(&mut binary, 0, NativeChunk::Evaluated(chunk.clone()))
.retain_binary(
&mut binary,
Side::Left,
NativeChunk::Evaluated(chunk.clone()),
)
.unwrap();
}
let assembled = binary.assemble().unwrap();
+1 -1
View File
@@ -1638,7 +1638,7 @@ impl<'de> serde::Deserialize<'de> for NativeTree {
/// Whether the native runtime builds `tree`.
///
/// The native route asks this before committing a statement, because a refusal raised during
/// physical planning cannot fall back. It mirrors what `RuntimeNode::build` and
/// physical planning cannot fall back. It mirrors what `TreeBuilder::build` and
/// `NativeExecutionState::try_new` refuse beyond [`NativeNode::validate`]:
///
/// - a root of raw samples;