Files
greptimedb/tests-integration/tests/native_prefix.rs
T
dennis zhuang 64943b35b6 feat(promql): read second, microsecond and nanosecond time indexes natively (#10671)
* feat(promql): read second, microsecond and nanosecond time indexes natively

The native route declined every selector over a time index that is not
in milliseconds. The existing plan wraps such a scan in a cast
projection, so the decline surfaced as selector_plan_is_not_a_table_scan,
and the region's raw series preflight refused the unit as well.

The raw series path now hands ticks over in the stored unit, and each
raw input of the native tree records that unit. Evaluation keeps the
existing plan's semantics: windows and lookbacks are decided on the
stored ticks, a kernel reads the time truncated toward zero to
milliseconds after the offset, and samples less than a millisecond
apart remain distinct samples. Since every bound is a whole
millisecond, deciding membership on ceil(tick) in milliseconds is
exact, so the evaluators keep a window time beside the payload only for
microsecond and nanosecond inputs. Scan filters are built in the
stored unit.

Histogram fields and range modifiers over a sub-millisecond time index
still decline. The append-mode decline is kept and renamed to
append_mode_table.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(promql): decline a discovered selector over mixed time index units

Logical metrics on different physical tables can store their time
index in different units, while the union a nameless selector reads is
one native input bound to one unit. The union took the first source's
unit and read the other sources' ticks in it, so a series stored in
microseconds next to one in milliseconds dropped out of its windows.
Such a selector now declines.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(promql): keep a window open until its last millisecond has been read

A sample finer than a millisecond is placed in the window ending at the
millisecond above it. A chunk that ends on such a sample has not read
the rest of that millisecond, so treating that millisecond as settled
closed the window before a later sample in the same millisecond arrived
in the next chunk. Settle only up to the millisecond the last sample lies
in, rounded down.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(promql): select only an exact sample under a zero lookback

A zero lookback admits only a sample exactly at the instant. The window
time of a sample less than a millisecond before the instant rounds up to
the instant, so the native selection picked it where the existing route
returns nothing. Compare the stored tick with the instant when the
lookback is zero, on the grid and anchored paths.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(promql): version the FusedSeries envelope for input time units

Move BoundInput::time_unit out of the positional tree encoding and append
the input leaves' units after the v1 payload as envelope v2. A v1 payload
from an older build still decodes, with millisecond inputs.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test(promql): answer two selectors over different time units, and pin the mixed-unit decline reason

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test(promql): drop the retired series scan v2 switch from the mixed-unit case

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test(promql): expect the v2 FusedSeries envelope in the native prefix test

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test(promql): adapt the time-unit tests to upstream instant and discovery changes

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test(promql): name the mixed-unit discovery refusal as unsupported

A native input carries one time unit, so discovery across physical tables with different time index units is refused. Answering needs a per-source rescale that changes the RawSeriesSource wire format; tracked in #10767.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

---------

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
2026-12-01 06:41:53 +00:00

671 lines
25 KiB
Rust

// Copyright 2023 Greptime Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! A serialized native PromQL prefix executed on a datanode over its own region scan.
use std::sync::Arc;
use api::v1::region::{QueryRequest, RegionRequestHeader};
use common_recordbatch::RecordBatches;
use common_recordbatch::adapter::DfRecordBatchStreamAdapter;
use datafusion::logical_expr::{Extension, LogicalPlan};
use datatypes::series::SeriesOrdering;
use datatypes::series::samples::SampleRef;
use datatypes::series::samples::{FieldSpec, SeriesSchema};
use datatypes::series::time::{EvalGrid, GridSpec};
use futures::{StreamExt, TryStreamExt};
use promql::extension_plan::{FusedOutputFormat, FusedSeries, FusedSeriesBinding};
use promql::native::plan::{
BoundInput, BoundWindow, NativeExpr, NativeNode, NativeTree, NativeType,
};
use promql::range_eval::{RangeFunction, RangeWindow};
use query::options::QueryOptions;
use query::parser::QueryLanguageParser;
use query::query_engine::DefaultSerializer;
use session::context::QueryContext;
use substrait::{DFLogicalSubstraitConvertor, SubstraitPlan};
use tests_integration::standalone::GreptimeDbStandaloneBuilder;
use tests_integration::test_util::execute_sql;
/// The logical frame schema a `FusedOutputFormat::SeriesFrame` node declares: the shared evaluated
/// packer's layout for the tree's evaluated root, which `try_new_with_format` compares against.
fn evaluated_frame_schema(tree: &NativeTree) -> datafusion::common::DFSchemaRef {
let NativeType::Samples(samples) = &tree.root().output else {
panic!("the prefix root evaluates to samples");
};
let stream = common_query::series_frame::pack_evaluated_series(
Box::pin(futures::stream::empty()),
Arc::clone(samples),
)
.expect("an evaluated root declares a frame");
Arc::new(
datafusion::common::DFSchema::try_from(stream.schema())
.expect("the frame schema converts to a logical schema"),
)
}
// Sends `rate[10s]` on a one-point evaluation grid for 16 hosts over 16 region-scan
// partitions to the datanode's remote-read entry point, then checks that the evaluated frames
// decode and each host reports the rate its samples produce. The former decode failure no longer
// reproduces because #10023 makes region package completion close its pending final raw chunk,
// so a chain's value and close are no longer latched apart in separate batches by the
// batch-interleaving `CoalescePartitionsExec`. That coalescing is still not chain-atomic: a chain
// spanning batches can still be cut. This one-point grid gives each host one evaluated value plus
// a possible empty closing chunk, so it does not exercise a chain spanning many batches. The
// parallelism-one path is exercised by `native_prefix_executes_over_real_mito_region`.
#[tokio::test(flavor = "multi_thread")]
async fn native_prefix_multi_partition_single_position_chains() {
let instance = GreptimeDbStandaloneBuilder::new("native_prefix_partitioned_chains")
.with_query_options(QueryOptions {
parallelism: 16,
..Default::default()
})
.with_auto_create_table(false)
.build()
.await;
let fe = instance.fe_instance();
execute_sql(fe, "CREATE TABLE native_prefix_partitioned (ts TIMESTAMP TIME INDEX, val DOUBLE, host STRING, PRIMARY KEY(host))").await;
let mut values = String::new();
for host in 0..16 {
for sample in 0..20_000 {
let timestamp = sample as u64 * 1_000;
let value = sample as f64;
values.push_str(&format!("('host{host}', {value}, {timestamp}),"));
}
}
values.pop();
execute_sql(
fe,
&format!("INSERT INTO native_prefix_partitioned(host, val, ts) VALUES {values}"),
)
.await;
execute_sql(fe, "ADMIN FLUSH_TABLE('native_prefix_partitioned')").await;
let ctx = QueryContext::arc();
let statement =
QueryLanguageParser::parse_sql("SELECT * FROM native_prefix_partitioned", &ctx).unwrap();
let planned = fe
.query_engine()
.planner()
.plan(&statement, ctx.clone())
.await
.unwrap();
let LogicalPlan::Projection(projection) = planned else {
panic!("expected projection");
};
let LogicalPlan::TableScan(scan) = projection.input.as_ref() else {
panic!("expected table scan");
};
let scan = scan.clone();
let child = LogicalPlan::TableScan(datafusion::logical_expr::logical_plan::TableScan {
projection: Some((0..scan.projected_schema.fields().len()).collect()),
..scan
});
let grid = Arc::new(EvalGrid::try_new(GridSpec::try_new(20_000, 1_000, 1).unwrap()).unwrap());
let raw = Arc::new(SeriesSchema {
fields: Arc::from([FieldSpec {
name: Arc::from("val"),
}]),
eval_grid: None,
});
let evaluated = Arc::new(SeriesSchema {
fields: Arc::clone(&raw.fields),
eval_grid: Some(Arc::clone(&grid)),
});
let raw_type = NativeType::Samples(raw);
let tree = NativeTree::try_new(NativeNode {
expr: NativeExpr::Window {
input: Box::new(NativeNode {
expr: NativeExpr::Input(BoundInput {
source_index: 0,
output: raw_type.clone(),
ordering: SeriesOrdering::KeyThenTime,
time_unit: Default::default(),
}),
output: raw_type,
}),
op: BoundWindow {
function: RangeFunction::Rate,
window: RangeWindow {
range_ms: 10_000,
offset_ms: 0,
anchor_ms: None,
modifier: None,
},
input_grid: None,
output_grid: grid.clone(),
},
},
output: NativeType::Samples(evaluated),
})
.unwrap();
let binding = FusedSeriesBinding {
grid,
metric_name: Some(Arc::from("native_prefix_partitioned")),
time_index: "ts".into(),
tags: vec!["host".into()],
};
let frame_schema = evaluated_frame_schema(&tree);
let fused = FusedSeries::try_new_with_format(
Arc::from("rate(native_prefix_partitioned[10s])"),
tree,
binding,
vec![child],
frame_schema,
FusedOutputFormat::SeriesFrame,
)
.unwrap();
let plan = LogicalPlan::Extension(Extension {
node: Arc::new(fused),
});
let bytes = DFLogicalSubstraitConvertor
.encode(&plan, DefaultSerializer)
.unwrap()
.to_vec();
let region = instance
.mito_engine
.regions()
.into_iter()
.find(|region| {
region
.metadata()
.column_metadatas
.iter()
.any(|column| column.column_schema.name == "host")
})
.unwrap();
let metadata = region.metadata();
let mut stream = instance
.datanode
.region_server()
.handle_remote_read(
QueryRequest {
header: Some(RegionRequestHeader {
query_context: Some((&*ctx).into()),
..Default::default()
}),
region_id: metadata.region_id.as_u64(),
plan: bytes,
},
ctx,
)
.await
.unwrap();
let mut batches = Vec::new();
while let Some(batch) = stream.next().await {
batches.push(batch.unwrap());
}
let schema = batches.first().expect("evaluated frames").schema.clone();
let batches = RecordBatches::try_new(schema, batches).unwrap();
// Package completion closes each region's pending final raw chunk, so the evaluated frames
// decode; ordinary partition coalescing still does not keep chains atomic. Check that every
// host's chain closes exactly once and carries the rate its own samples produce.
let decoded = common_query::series_frame::unpack_evaluated_series(
Box::pin(DfRecordBatchStreamAdapter::new(batches.as_stream())),
None,
)
.unwrap()
.try_collect::<Vec<_>>()
.await
.expect("a chain-atomic merge decodes every chain");
let mut closed = std::collections::BTreeMap::<String, (usize, Vec<f64>)>::new();
for (series, is_last) in decoded {
let host = series
.metric
.get("host")
.unwrap_or_else(|| panic!("a decoded series keeps its host label: {:?}", series.metric))
.to_string();
let entry = closed.entry(host).or_default();
if is_last {
entry.0 += 1;
}
for field in series.fields.iter() {
entry
.1
.extend(field.iter().filter_map(|sample| match sample {
SampleRef::Float(value) => Some(value),
SampleRef::Histogram(_) | SampleRef::Missing => None,
}));
}
}
assert_eq!(16, closed.len(), "one chain per host: {closed:?}");
for (host, (closes, values)) in &closed {
assert_eq!(1, *closes, "`{host}` closes its chain exactly once");
assert_eq!(
vec![1.0],
*values,
"`{host}` reports the rate of its own samples, which a cut chain would lose"
);
}
}
// The one-position case above cannot exercise a chain that spans chunks. This 8,192-position grid
// across 16 region-scan partitions does: whatever chunk boundaries the merge produces, each host's
// chain must arrive complete, in time order, and close exactly once.
#[tokio::test(flavor = "multi_thread")]
async fn native_prefix_multi_partition_multi_chunk_chains() {
let instance = GreptimeDbStandaloneBuilder::new("native_prefix_partitioned_multi_chunk_chains")
.with_query_options(QueryOptions {
parallelism: 16,
..Default::default()
})
.with_auto_create_table(false)
.build()
.await;
let fe = instance.fe_instance();
execute_sql(fe, "CREATE TABLE native_prefix_partitioned (ts TIMESTAMP TIME INDEX, val DOUBLE, host STRING, PRIMARY KEY(host))").await;
let mut values = String::new();
for host in 0..16 {
for sample in 0..20_000 {
let timestamp = sample as u64 * 1_000;
let value = sample as f64;
values.push_str(&format!("('host{host}', {value}, {timestamp}),"));
}
}
values.pop();
execute_sql(
fe,
&format!("INSERT INTO native_prefix_partitioned(host, val, ts) VALUES {values}"),
)
.await;
execute_sql(fe, "ADMIN FLUSH_TABLE('native_prefix_partitioned')").await;
let ctx = QueryContext::arc();
let statement =
QueryLanguageParser::parse_sql("SELECT * FROM native_prefix_partitioned", &ctx).unwrap();
let planned = fe
.query_engine()
.planner()
.plan(&statement, ctx.clone())
.await
.unwrap();
let LogicalPlan::Projection(projection) = planned else {
panic!("expected projection");
};
let LogicalPlan::TableScan(scan) = projection.input.as_ref() else {
panic!("expected table scan");
};
let scan = scan.clone();
let child = LogicalPlan::TableScan(datafusion::logical_expr::logical_plan::TableScan {
projection: Some((0..scan.projected_schema.fields().len()).collect()),
..scan
});
let grid =
Arc::new(EvalGrid::try_new(GridSpec::try_new(10_000, 1_000, 8_192).unwrap()).unwrap());
let raw = Arc::new(SeriesSchema {
fields: Arc::from([FieldSpec {
name: Arc::from("val"),
}]),
eval_grid: None,
});
let evaluated = Arc::new(SeriesSchema {
fields: Arc::clone(&raw.fields),
eval_grid: Some(Arc::clone(&grid)),
});
let raw_type = NativeType::Samples(raw);
let tree = NativeTree::try_new(NativeNode {
expr: NativeExpr::Window {
input: Box::new(NativeNode {
expr: NativeExpr::Input(BoundInput {
source_index: 0,
output: raw_type.clone(),
ordering: SeriesOrdering::KeyThenTime,
time_unit: Default::default(),
}),
output: raw_type,
}),
op: BoundWindow {
function: RangeFunction::Rate,
window: RangeWindow {
range_ms: 10_000,
offset_ms: 0,
anchor_ms: None,
modifier: None,
},
input_grid: None,
output_grid: grid.clone(),
},
},
output: NativeType::Samples(evaluated),
})
.unwrap();
let binding = FusedSeriesBinding {
grid,
metric_name: Some(Arc::from("native_prefix_partitioned")),
time_index: "ts".into(),
tags: vec!["host".into()],
};
let frame_schema = evaluated_frame_schema(&tree);
let fused = FusedSeries::try_new_with_format(
Arc::from("rate(native_prefix_partitioned[10s])"),
tree,
binding,
vec![child],
frame_schema,
FusedOutputFormat::SeriesFrame,
)
.unwrap();
let plan = LogicalPlan::Extension(Extension {
node: Arc::new(fused),
});
let bytes = DFLogicalSubstraitConvertor
.encode(&plan, DefaultSerializer)
.unwrap()
.to_vec();
let region = instance
.mito_engine
.regions()
.into_iter()
.find(|region| {
region
.metadata()
.column_metadatas
.iter()
.any(|column| column.column_schema.name == "host")
})
.unwrap();
let metadata = region.metadata();
let mut stream = instance
.datanode
.region_server()
.handle_remote_read(
QueryRequest {
header: Some(RegionRequestHeader {
query_context: Some((&*ctx).into()),
..Default::default()
}),
region_id: metadata.region_id.as_u64(),
plan: bytes,
},
ctx,
)
.await
.unwrap();
let mut batches = Vec::new();
while let Some(batch) = stream.next().await {
batches.push(batch.unwrap());
}
let schema = batches.first().expect("evaluated frames").schema.clone();
let batches = RecordBatches::try_new(schema, batches).unwrap();
let decoded = common_query::series_frame::unpack_evaluated_series(
Box::pin(DfRecordBatchStreamAdapter::new(batches.as_stream())),
None,
)
.unwrap()
.try_collect::<Vec<_>>()
.await
.unwrap();
let mut per_host = std::collections::BTreeMap::<String, (usize, Vec<i64>, Vec<f64>)>::new();
for (series, is_last) in decoded {
let host = series
.metric
.get("host")
.unwrap_or_else(|| panic!("a decoded series keeps its host label: {:?}", series.metric))
.to_string();
let entry = per_host.entry(host).or_default();
if is_last {
entry.0 += 1;
}
if !series.time.is_empty() {
entry
.1
.extend_from_slice(series.time.timestamps().unwrap().as_ref());
}
for field in series.fields.iter() {
entry
.2
.extend(field.iter().filter_map(|sample| match sample {
SampleRef::Float(value) => Some(value),
SampleRef::Histogram(_) | SampleRef::Missing => None,
}));
}
}
assert_eq!(16, per_host.len(), "one chain per host: {per_host:?}");
let expected_timestamps = (0..8_192).map(|i| 10_000 + i * 1_000).collect::<Vec<_>>();
for (host, (closes, timestamps, values)) in &per_host {
assert_eq!(1, *closes, "`{host}` closes its chain exactly once");
assert_eq!(
8_192,
timestamps.len(),
"`{host}` keeps every grid position across chunk boundaries"
);
assert_eq!(
&expected_timestamps, timestamps,
"`{host}` arrives in order over its whole contiguous grid"
);
assert_eq!(8_192, values.len(), "`{host}` keeps every value");
for value in values {
assert!(
(value - 1.0).abs() < 1e-12,
"`{host}` reports the rate of its own samples, which a cut chain would lose: {value}"
);
}
}
}
#[tokio::test(flavor = "multi_thread")]
async fn native_prefix_executes_over_real_mito_region() {
let instance = GreptimeDbStandaloneBuilder::new("native_prefix_region_scan")
.with_auto_create_table(false)
.build()
.await;
let fe = instance.fe_instance();
execute_sql(fe, "CREATE TABLE native_prefix (ts TIMESTAMP TIME INDEX, val DOUBLE, host STRING, PRIMARY KEY(host))").await;
execute_sql(fe, "INSERT INTO native_prefix(host, val, ts) VALUES ('a', 0.0, 1000), ('a', 10.0, 2000), ('a', 20.0, 3000), ('a', 30.0, 4000)").await;
execute_sql(fe, "ADMIN FLUSH_TABLE('native_prefix')").await;
let ctx = QueryContext::arc();
let statement = QueryLanguageParser::parse_sql("SELECT * FROM native_prefix", &ctx).unwrap();
let planned = fe
.query_engine()
.planner()
.plan(&statement, ctx.clone())
.await
.unwrap();
let datafusion::logical_expr::LogicalPlan::Projection(projection) = planned else {
panic!("expected projection: {planned:?}");
};
let datafusion::logical_expr::LogicalPlan::TableScan(scan) = projection.input.as_ref() else {
panic!("expected table scan: {projection:?}");
};
let scan = scan.clone();
let child = LogicalPlan::TableScan(datafusion::logical_expr::logical_plan::TableScan {
projection: Some((0..scan.projected_schema.fields().len()).collect()),
..scan
});
let grid = Arc::new(EvalGrid::try_new(GridSpec::try_new(3000, 1000, 2).unwrap()).unwrap());
let raw = Arc::new(SeriesSchema {
fields: Arc::from([FieldSpec {
name: Arc::from("val"),
}]),
eval_grid: None,
});
let evaluated = Arc::new(SeriesSchema {
fields: Arc::clone(&raw.fields),
eval_grid: Some(Arc::clone(&grid)),
});
// The input node's output must be the same binding as its `BoundInput.output`: the tree
// compares those two by allocation identity, so an equal-but-separate schema is refused.
let raw_type = NativeType::Samples(raw);
let tree = NativeTree::try_new(NativeNode {
expr: NativeExpr::Window {
input: Box::new(NativeNode {
expr: NativeExpr::Input(BoundInput {
source_index: 0,
output: raw_type.clone(),
ordering: SeriesOrdering::KeyThenTime,
time_unit: Default::default(),
}),
output: raw_type,
}),
op: BoundWindow {
function: RangeFunction::Rate,
window: RangeWindow {
range_ms: 3000,
offset_ms: 0,
anchor_ms: None,
modifier: None,
},
input_grid: None,
output_grid: Arc::clone(&grid),
},
},
output: NativeType::Samples(evaluated),
})
.unwrap();
let binding = FusedSeriesBinding {
grid,
metric_name: Some(Arc::from("native_prefix")),
time_index: "ts".into(),
tags: vec!["host".into()],
};
// A frame-output node declares the frame layout of its evaluated root, not the projection of
// the scan below it: `try_new_with_format` compares the two and refuses a mismatch.
let frame_schema = evaluated_frame_schema(&tree);
let fused = FusedSeries::try_new_with_format(
Arc::from("rate(native_prefix[3s])"),
tree,
binding,
vec![child],
frame_schema,
FusedOutputFormat::SeriesFrame,
)
.unwrap();
let fused_payload = fused.serialize().unwrap();
let plan = LogicalPlan::Extension(Extension {
node: Arc::new(fused),
});
let bytes = DFLogicalSubstraitConvertor
.encode(&plan, DefaultSerializer)
.unwrap()
.to_vec();
let region = instance
.mito_engine
.regions()
.into_iter()
.find(|r| {
r.metadata()
.column_metadatas
.iter()
.any(|c| c.column_schema.name == "host")
})
.unwrap();
let metadata = region.metadata();
let server = instance.datanode.region_server();
let mut stream = server
.handle_remote_read(
QueryRequest {
header: Some(RegionRequestHeader {
query_context: Some((&*ctx).into()),
..Default::default()
}),
region_id: metadata.region_id.as_u64(),
plan: bytes.clone(),
},
ctx.clone(),
)
.await
.unwrap();
let mut batches = Vec::new();
while let Some(batch) = stream.next().await {
batches.push(batch.unwrap());
}
let schema = batches.first().expect("evaluated frames").schema.clone();
let batches = RecordBatches::try_new(schema, batches).unwrap();
let series = common_query::series_frame::unpack_evaluated_series(
Box::pin(DfRecordBatchStreamAdapter::new(batches.as_stream())),
None,
)
.unwrap()
.try_collect::<Vec<_>>()
.await
.unwrap();
assert_eq!(series.len(), 1);
let (series, _) = &series[0];
// `rate` drops `__name__`, as Prometheus does for every range function except
// `last_over_time` and `first_over_time`; the series' own tags survive.
assert_eq!(series.metric.name(), None);
assert_eq!(
series
.metric
.get("host")
.map(ToString::to_string)
.as_deref(),
Some("a")
);
assert_eq!(series.time.timestamps().unwrap().as_ref(), &[3000, 4000]);
let values = series.fields.get(0).unwrap();
// At 3000 ms the window holds 0, 10, 20: an increase of 20 over 2 observed
// seconds. Prometheus would keep the 1 s leading gap (below the 1.1x average
// threshold), but a counter whose first sample is 0 cannot be extrapolated
// backwards at all: `duration_to_zero = sampled * first/value = 0` clamps it,
// so the rate is 20 * (2 + 0) / 2 / 3 = 20/3.
//
// At 4000 ms the window holds 10, 20, 30: also 20 over 2 s, but now
// `duration_to_zero = 2 * (10/20) = 1 s` does not shorten the 1 s leading gap,
// so the rate is 20 * (2 + 1 + 0) / 2 / 3 = 10.
let expected = [20.0 / 3.0, 10.0];
for (index, expected) in expected.into_iter().enumerate() {
let SampleRef::Float(value) = values.sample(index) else {
panic!("the rate field is float at position {index}");
};
assert!(
(value - expected).abs() < 1e-12,
"value at position {index} is {value}, not {expected}"
);
}
let mut tampered = bytes;
// Locate the complete FusedSeries envelope in the Substrait bytes. Its version is the first
// word and its payload length is the second; no payload-field offsets are needed here.
let envelope_at = tampered
.windows(fused_payload.len())
.position(|window| window == fused_payload.as_slice())
.expect("FusedSeries envelope in Substrait plan");
let version_bytes: [u8; 4] = tampered[envelope_at..envelope_at + 4]
.try_into()
.unwrap_or_else(|_| panic!("missing version bytes at {envelope_at}"));
let version = u32::from_le_bytes(version_bytes);
assert_eq!(version, 2, "observed version bytes: {version_bytes:?}");
let length_bytes: [u8; 4] = tampered[envelope_at + 4..envelope_at + 8]
.try_into()
.unwrap_or_else(|_| panic!("missing length bytes at {}", envelope_at + 4));
let payload_length = u32::from_le_bytes(length_bytes);
assert_eq!(
payload_length as usize,
fused_payload.len() - 8,
"observed length bytes: {length_bytes:?}"
);
tampered[envelope_at] = 99;
let error = server
.handle_remote_read(
QueryRequest {
header: Some(RegionRequestHeader {
query_context: Some((&*ctx).into()),
..Default::default()
}),
region_id: metadata.region_id.as_u64(),
plan: tampered,
},
ctx,
)
.await
.err()
.expect("invalid prefix rejected");
assert!(
format!("{error:?}").contains("unsupported FusedSeries envelope version"),
"{error:?}"
);
}