mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-11-23 19:24:11 +00:00
feat(query): add incremental raw series frame streams
Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
This commit is contained in:
@@ -25,6 +25,7 @@ datafusion-common.workspace = true
|
||||
datafusion-expr.workspace = true
|
||||
datafusion-proto.workspace = true
|
||||
datatypes.workspace = true
|
||||
futures-util.workspace = true
|
||||
once_cell.workspace = true
|
||||
prost.workspace = true
|
||||
serde.workspace = true
|
||||
@@ -36,7 +37,6 @@ store-api.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
common-base.workspace = true
|
||||
futures-util.workspace = true
|
||||
serde_json.workspace = true
|
||||
tokio.workspace = true
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ pub mod prelude;
|
||||
pub mod prometheus;
|
||||
pub mod promql_annotations;
|
||||
pub mod request;
|
||||
pub mod series_frame;
|
||||
pub mod stream;
|
||||
#[cfg(any(test, feature = "testing"))]
|
||||
pub mod test_util;
|
||||
|
||||
@@ -0,0 +1,363 @@
|
||||
// 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.
|
||||
|
||||
//! Streaming boundary between finite raw-series chunks and DataFusion record batches.
|
||||
//!
|
||||
//! Schema fields identify codec layout and ordinal payload fields; additional application metadata
|
||||
//! is permitted when stable across the stream. The caller-provided [`RawLabelSource`] is the label
|
||||
//! binding, not authenticated by the frame schema or field provenance. This adapter preserves scan
|
||||
//! candidates only: it is not a residual filter, authorization check, remote transport, or
|
||||
//! compaction boundary.
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::pin::Pin;
|
||||
use std::sync::Arc;
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
use common_error::ext::BoxedError;
|
||||
use common_recordbatch::{DfRecordBatch, DfSendableRecordBatchStream};
|
||||
use datafusion_common::DataFusionError;
|
||||
use datatypes::error::InvalidSeriesSnafu;
|
||||
use datatypes::series::MetricName;
|
||||
use datatypes::series::frame::{
|
||||
RawLabelMode, pack_raw_series_chunk, raw_series_frame_schema, unpack_raw_series_frame,
|
||||
};
|
||||
use datatypes::series::raw::{RawSeriesChunk, RawSeriesOrdering};
|
||||
use datatypes::series::samples::SeriesSchema;
|
||||
use futures_util::Stream;
|
||||
use store_api::region_engine::{RawLabelSource, RawSeriesStream, SendableRawSeriesStream};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
/// Packs a storage raw-series stream into DataFusion record batches incrementally.
|
||||
pub fn pack_raw_series(
|
||||
source: SendableRawSeriesStream,
|
||||
) -> datafusion_common::Result<DfSendableRecordBatchStream> {
|
||||
let schema = source.schema().clone();
|
||||
let label_source = source.label_source().clone();
|
||||
let ordering = source.ordering();
|
||||
let mode = label_mode(&label_source);
|
||||
let frame_schema = raw_series_frame_schema(&schema, mode).map_err(codec_error)?;
|
||||
Ok(Box::pin(PackStream {
|
||||
source: Some(source),
|
||||
schema,
|
||||
frame_schema,
|
||||
mode,
|
||||
validator: SequenceValidator::new(ordering),
|
||||
pending: Vec::<DfRecordBatch>::new().into_iter(),
|
||||
}))
|
||||
}
|
||||
|
||||
/// Unpacks DataFusion batches using the caller's explicit scan binding and ordering.
|
||||
///
|
||||
/// The supplied binding is retained unchanged and is not validated as field provenance.
|
||||
pub fn unpack_raw_series(
|
||||
source: DfSendableRecordBatchStream,
|
||||
expected: Arc<SeriesSchema>,
|
||||
label_source: RawLabelSource,
|
||||
ordering: RawSeriesOrdering,
|
||||
) -> datafusion_common::Result<SendableRawSeriesStream> {
|
||||
let mode = label_mode(&label_source);
|
||||
let frame_schema = source.schema();
|
||||
// Validate even empty streams' declared schema and layout before returning the stream.
|
||||
let empty = DfRecordBatch::new_empty(frame_schema.clone());
|
||||
unpack_raw_series_frame(&expected, mode, &empty).map_err(codec_error)?;
|
||||
Ok(Box::pin(UnpackStream {
|
||||
source: Some(source),
|
||||
schema: expected,
|
||||
frame_schema,
|
||||
label_source,
|
||||
ordering,
|
||||
validator: SequenceValidator::new(ordering),
|
||||
pending: Vec::new().into_iter(),
|
||||
}))
|
||||
}
|
||||
|
||||
fn label_mode(source: &RawLabelSource) -> RawLabelMode {
|
||||
match source {
|
||||
RawLabelSource::PrimaryKey { .. } => RawLabelMode::Key,
|
||||
RawLabelSource::Explicit => RawLabelMode::Explicit,
|
||||
}
|
||||
}
|
||||
|
||||
fn codec_error(error: datatypes::error::Error) -> DataFusionError {
|
||||
DataFusionError::External(Box::new(BoxedError::new(error)))
|
||||
}
|
||||
|
||||
fn upstream_error(error: BoxedError) -> DataFusionError {
|
||||
DataFusionError::External(Box::new(error))
|
||||
}
|
||||
|
||||
fn invalid(reason: impl Into<String>) -> DataFusionError {
|
||||
DataFusionError::External(Box::new(BoxedError::new(
|
||||
InvalidSeriesSnafu {
|
||||
reason: reason.into(),
|
||||
}
|
||||
.build(),
|
||||
)))
|
||||
}
|
||||
|
||||
/// Cross-chunk validation state. Contiguous ordering retains complete owned keys for recurrence
|
||||
/// detection, so memory is O(total distinct complete-key bytes + distinct keys), plus current-key
|
||||
/// and metric state, until stream termination; there is no eviction or cap. This owns key bytes,
|
||||
/// never Arrow buffers that might pin larger payloads.
|
||||
struct SequenceValidator {
|
||||
ordering: RawSeriesOrdering,
|
||||
previous_key: Option<Vec<u8>>,
|
||||
previous_timestamp: Option<i64>,
|
||||
previous_metric: Option<MetricName>,
|
||||
previous_is_last: bool,
|
||||
closed: HashSet<Vec<u8>>,
|
||||
}
|
||||
|
||||
impl SequenceValidator {
|
||||
fn new(ordering: RawSeriesOrdering) -> Self {
|
||||
Self {
|
||||
ordering,
|
||||
previous_key: None,
|
||||
previous_timestamp: None,
|
||||
previous_metric: None,
|
||||
previous_is_last: false,
|
||||
closed: HashSet::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn finish(&self) -> datafusion_common::Result<()> {
|
||||
if self.previous_key.is_some() && !self.previous_is_last {
|
||||
return Err(invalid("raw series stream ended before key was final"));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate(&mut self, chunk: &RawSeriesChunk) -> datafusion_common::Result<()> {
|
||||
let key = chunk.read_key.as_slice();
|
||||
let timestamp = chunk
|
||||
.time
|
||||
.timestamp(chunk.time.len() - 1)
|
||||
.ok_or_else(|| invalid("empty raw series chunk"))?;
|
||||
if let Some(previous) = &self.previous_key {
|
||||
match previous.as_slice().cmp(key) {
|
||||
std::cmp::Ordering::Equal => {
|
||||
if self.previous_is_last {
|
||||
return Err(invalid("raw series key recurred after final chunk"));
|
||||
}
|
||||
if self.previous_metric != chunk.metric {
|
||||
return Err(invalid("raw series metric changed within key"));
|
||||
}
|
||||
let first = chunk
|
||||
.time
|
||||
.timestamp(0)
|
||||
.ok_or_else(|| invalid("empty raw series chunk"))?;
|
||||
if self.previous_timestamp.is_some_and(|last| first <= last) {
|
||||
return Err(invalid(
|
||||
"raw series timestamps are not increasing across chunks",
|
||||
));
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
if !self.previous_is_last {
|
||||
return Err(invalid("raw series key changed before final chunk"));
|
||||
}
|
||||
if self.ordering == RawSeriesOrdering::ReadKeyThenTime
|
||||
&& previous.as_slice() > key
|
||||
{
|
||||
return Err(invalid("raw series keys are not ordered"));
|
||||
}
|
||||
if self.ordering == RawSeriesOrdering::Contiguous && self.closed.contains(key) {
|
||||
return Err(invalid("raw series key recurred after its contiguous run"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let owned_key = key.to_vec();
|
||||
if chunk.is_last && self.ordering == RawSeriesOrdering::Contiguous {
|
||||
self.closed.insert(owned_key.clone());
|
||||
}
|
||||
self.previous_key = Some(owned_key);
|
||||
self.previous_timestamp = Some(timestamp);
|
||||
self.previous_metric = chunk.metric.clone();
|
||||
self.previous_is_last = chunk.is_last;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
struct PackStream {
|
||||
source: Option<SendableRawSeriesStream>,
|
||||
schema: Arc<SeriesSchema>,
|
||||
frame_schema: datatypes::arrow::datatypes::SchemaRef,
|
||||
mode: RawLabelMode,
|
||||
validator: SequenceValidator,
|
||||
pending: std::vec::IntoIter<DfRecordBatch>,
|
||||
}
|
||||
|
||||
impl PackStream {
|
||||
fn finish(&mut self) {
|
||||
self.source = None;
|
||||
self.pending = Vec::new().into_iter();
|
||||
self.validator = SequenceValidator::new(self.validator.ordering);
|
||||
}
|
||||
}
|
||||
|
||||
impl Stream for PackStream {
|
||||
type Item = datafusion_common::Result<DfRecordBatch>;
|
||||
|
||||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
let this = &mut *self;
|
||||
loop {
|
||||
if let Some(batch) = this.pending.next() {
|
||||
return Poll::Ready(Some(Ok(batch)));
|
||||
}
|
||||
let polled = match this.source.as_mut() {
|
||||
Some(source) => source.as_mut().poll_next(cx),
|
||||
None => return Poll::Ready(None),
|
||||
};
|
||||
match polled {
|
||||
Poll::Pending => return Poll::Pending,
|
||||
Poll::Ready(None) => {
|
||||
let result = this.validator.finish();
|
||||
this.finish();
|
||||
return Poll::Ready(result.err().map(Err));
|
||||
}
|
||||
Poll::Ready(Some(Err(error))) => {
|
||||
this.finish();
|
||||
return Poll::Ready(Some(Err(upstream_error(error))));
|
||||
}
|
||||
Poll::Ready(Some(Ok(chunk))) => {
|
||||
let result = (|| {
|
||||
let batches = pack_raw_series_chunk(&this.schema, this.mode, &chunk)
|
||||
.map_err(codec_error)?;
|
||||
this.validator.validate(&chunk)?;
|
||||
Ok(batches)
|
||||
})();
|
||||
match result {
|
||||
Ok(batches) => this.pending = batches.into_iter(),
|
||||
Err(error) => {
|
||||
this.finish();
|
||||
return Poll::Ready(Some(Err(error)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl datafusion::physical_plan::RecordBatchStream for PackStream {
|
||||
fn schema(&self) -> datatypes::arrow::datatypes::SchemaRef {
|
||||
self.frame_schema.clone()
|
||||
}
|
||||
}
|
||||
|
||||
struct UnpackStream {
|
||||
source: Option<DfSendableRecordBatchStream>,
|
||||
schema: Arc<SeriesSchema>,
|
||||
frame_schema: datatypes::arrow::datatypes::SchemaRef,
|
||||
label_source: RawLabelSource,
|
||||
ordering: RawSeriesOrdering,
|
||||
validator: SequenceValidator,
|
||||
pending: std::vec::IntoIter<RawSeriesChunk>,
|
||||
}
|
||||
|
||||
impl RawSeriesStream for UnpackStream {
|
||||
fn schema(&self) -> &Arc<SeriesSchema> {
|
||||
&self.schema
|
||||
}
|
||||
|
||||
fn label_source(&self) -> &RawLabelSource {
|
||||
&self.label_source
|
||||
}
|
||||
|
||||
fn ordering(&self) -> RawSeriesOrdering {
|
||||
self.ordering
|
||||
}
|
||||
}
|
||||
|
||||
impl UnpackStream {
|
||||
fn finish(&mut self) {
|
||||
self.source = None;
|
||||
self.pending = Vec::new().into_iter();
|
||||
self.validator = SequenceValidator::new(self.ordering);
|
||||
}
|
||||
}
|
||||
|
||||
impl Stream for UnpackStream {
|
||||
type Item = Result<RawSeriesChunk, BoxedError>;
|
||||
|
||||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
let this = &mut *self;
|
||||
loop {
|
||||
if let Some(chunk) = this.pending.next() {
|
||||
return Poll::Ready(Some(Ok(chunk)));
|
||||
}
|
||||
let polled = match this.source.as_mut() {
|
||||
Some(source) => source.as_mut().poll_next(cx),
|
||||
None => return Poll::Ready(None),
|
||||
};
|
||||
match polled {
|
||||
Poll::Pending => return Poll::Pending,
|
||||
Poll::Ready(None) => {
|
||||
let result = this.validator.finish();
|
||||
this.finish();
|
||||
return Poll::Ready(result.err().map(|error| {
|
||||
Err(BoxedError::new(crate::error::Error::GeneralDataFusion {
|
||||
error,
|
||||
location: snafu::Location::default(),
|
||||
}))
|
||||
}));
|
||||
}
|
||||
Poll::Ready(Some(Err(error))) => {
|
||||
this.finish();
|
||||
return Poll::Ready(Some(Err(BoxedError::new(
|
||||
crate::error::Error::GeneralDataFusion {
|
||||
error,
|
||||
location: snafu::Location::default(),
|
||||
},
|
||||
))));
|
||||
}
|
||||
Poll::Ready(Some(Ok(batch))) => {
|
||||
let result = (|| {
|
||||
if batch.schema() != this.frame_schema {
|
||||
return Err(invalid("raw frame schema mismatch"));
|
||||
}
|
||||
unpack_raw_series_frame(
|
||||
&this.schema,
|
||||
label_mode(&this.label_source),
|
||||
&batch,
|
||||
)
|
||||
.map_err(codec_error)
|
||||
.and_then(|chunks| {
|
||||
for chunk in &chunks {
|
||||
this.validator.validate(chunk)?;
|
||||
}
|
||||
Ok(chunks)
|
||||
})
|
||||
})();
|
||||
match result {
|
||||
Ok(chunks) => this.pending = chunks.into_iter(),
|
||||
Err(error) => {
|
||||
this.finish();
|
||||
return Poll::Ready(Some(Err(BoxedError::new(
|
||||
crate::error::Error::GeneralDataFusion {
|
||||
error,
|
||||
location: snafu::Location::default(),
|
||||
},
|
||||
))));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,322 @@
|
||||
// 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.
|
||||
|
||||
use std::pin::Pin;
|
||||
use std::sync::Arc;
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
use common_error::ext::BoxedError;
|
||||
use datatypes::arrow::array::{Array, Float64Array, UInt32Array};
|
||||
use datatypes::arrow::buffer::{Buffer, ScalarBuffer};
|
||||
use datatypes::series::histogram::{BucketSideView, CounterResetHint, HistogramBatchBuilder};
|
||||
use datatypes::series::raw::{RawSeriesChunk, RawSeriesOrdering, RawTimeAxis};
|
||||
use datatypes::series::samples::{
|
||||
DenseSamplesRef, FieldSpec, HistogramSamples, SampleRef, Samples, SeriesFields, SeriesSchema,
|
||||
};
|
||||
use datatypes::series::{Label, MetricName};
|
||||
use futures_util::{Stream, StreamExt};
|
||||
use store_api::region_engine::{RawLabelSource, RawSeriesStream, SendableRawSeriesStream};
|
||||
|
||||
use super::super::{pack_raw_series, unpack_raw_series};
|
||||
|
||||
struct RawFixture {
|
||||
schema: Arc<SeriesSchema>,
|
||||
binding: RawLabelSource,
|
||||
chunk: Option<RawSeriesChunk>,
|
||||
}
|
||||
|
||||
impl Stream for RawFixture {
|
||||
type Item = Result<RawSeriesChunk, BoxedError>;
|
||||
|
||||
fn poll_next(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
Poll::Ready(self.chunk.take().map(Ok))
|
||||
}
|
||||
}
|
||||
|
||||
impl RawSeriesStream for RawFixture {
|
||||
fn schema(&self) -> &Arc<SeriesSchema> {
|
||||
&self.schema
|
||||
}
|
||||
|
||||
fn label_source(&self) -> &RawLabelSource {
|
||||
&self.binding
|
||||
}
|
||||
|
||||
fn ordering(&self) -> RawSeriesOrdering {
|
||||
RawSeriesOrdering::Contiguous
|
||||
}
|
||||
}
|
||||
|
||||
fn histogram_batch() -> Arc<datatypes::series::histogram::HistogramBatch> {
|
||||
let custom = [1.5, 4.5];
|
||||
let positive_offsets = [1];
|
||||
let positive_lengths = [2];
|
||||
let positive_buckets = [3.25, 7.5];
|
||||
let negative_offsets = [-2];
|
||||
let negative_lengths = [1];
|
||||
let negative_buckets = [0.75];
|
||||
let mut builder = HistogramBatchBuilder::new();
|
||||
for (sum, start_timestamp) in [(10.5, Some(-123)), (-9.5, Some(456)), (7.25, None)] {
|
||||
builder
|
||||
.push(datatypes::series::histogram::HistogramView {
|
||||
schema: -7,
|
||||
zero_threshold: 0.125,
|
||||
sum,
|
||||
count: 17.25,
|
||||
zero_count: 2.5,
|
||||
reset_hint: CounterResetHint::Unrecognized(37),
|
||||
start_timestamp,
|
||||
custom_values: &custom,
|
||||
positive: BucketSideView {
|
||||
span_offsets: &positive_offsets,
|
||||
span_lengths: &positive_lengths,
|
||||
buckets: &positive_buckets,
|
||||
},
|
||||
negative: BucketSideView {
|
||||
span_offsets: &negative_offsets,
|
||||
span_lengths: &negative_lengths,
|
||||
buckets: &negative_buckets,
|
||||
},
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
builder.finish().unwrap()
|
||||
}
|
||||
|
||||
fn assert_histogram(
|
||||
view: datatypes::series::histogram::HistogramView<'_>,
|
||||
sum: f64,
|
||||
start: Option<i64>,
|
||||
) {
|
||||
assert_eq!(view.schema, -7);
|
||||
assert_eq!(view.zero_threshold, 0.125);
|
||||
assert_eq!(view.sum.to_bits(), sum.to_bits());
|
||||
assert_eq!(view.count, 17.25);
|
||||
assert_eq!(view.zero_count, 2.5);
|
||||
assert_eq!(view.reset_hint, CounterResetHint::Unrecognized(37));
|
||||
assert_eq!(view.start_timestamp, start);
|
||||
assert_eq!(view.custom_values, [1.5, 4.5]);
|
||||
assert_eq!(view.positive.span_offsets, [1]);
|
||||
assert_eq!(view.positive.span_lengths, [2]);
|
||||
assert_eq!(view.positive.buckets, [3.25, 7.5]);
|
||||
assert_eq!(view.negative.span_offsets, [-2]);
|
||||
assert_eq!(view.negative.span_lengths, [1]);
|
||||
assert_eq!(view.negative.buckets, [0.75]);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn streaming_round_trip_preserves_sliced_multifield_payload_and_owners() {
|
||||
let schema = Arc::new(SeriesSchema {
|
||||
fields: vec!["float", "histogram", "mixed"]
|
||||
.into_iter()
|
||||
.map(|name| FieldSpec {
|
||||
name: Arc::from(name),
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.into(),
|
||||
eval_grid: None,
|
||||
});
|
||||
let time_owner = ScalarBuffer::from(vec![0, 11, 22, 33, 44]);
|
||||
let times = RawTimeAxis::try_new(time_owner.slice(1, 3)).unwrap();
|
||||
let float_owner = Float64Array::from(vec![
|
||||
Some(90.0),
|
||||
None,
|
||||
Some(f64::from_bits(0x7ff8_0000_0000_0042)),
|
||||
Some(-0.0),
|
||||
Some(80.0),
|
||||
]);
|
||||
let float_a = float_owner.slice(1, 2);
|
||||
let float_b = float_owner.slice(3, 1);
|
||||
let floats = Samples::try_from_chunks(vec![
|
||||
Samples::from_float(float_a.clone()),
|
||||
Samples::from_float(float_b.clone()),
|
||||
])
|
||||
.unwrap();
|
||||
|
||||
let table_owner = histogram_batch();
|
||||
let table_weak = Arc::downgrade(&table_owner);
|
||||
let source_table = table_owner.as_arrow().clone();
|
||||
let sliced_table = Arc::new(
|
||||
datatypes::series::histogram::HistogramBatch::try_from_arrow(source_table.slice(1, 2))
|
||||
.unwrap(),
|
||||
);
|
||||
let sliced_weak = Arc::downgrade(&sliced_table);
|
||||
let index_owner = UInt32Array::from(vec![Some(2), Some(0), Some(1), Some(0), Some(1)]);
|
||||
let indices = index_owner.slice(1, 3);
|
||||
let hist = Samples::try_from_histogram(indices.clone(), sliced_table.clone()).unwrap();
|
||||
let mixed_float_owner = Float64Array::from(vec![Some(1.25), None, None]);
|
||||
let mixed_index_owner = UInt32Array::from(vec![None, Some(0), None]);
|
||||
let mixed = Samples::try_from_mixed(
|
||||
mixed_float_owner.clone(),
|
||||
HistogramSamples::try_new(mixed_index_owner.clone(), sliced_table.clone()).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
let chunk = RawSeriesChunk::try_new(
|
||||
&schema,
|
||||
Buffer::from(vec![0xaa_u8, 0x10, 0xbb]),
|
||||
Some(
|
||||
MetricName::try_new(
|
||||
Some(Arc::from("cpu")),
|
||||
vec![Label {
|
||||
name: Arc::from("host"),
|
||||
value: Arc::from("a"),
|
||||
}],
|
||||
)
|
||||
.unwrap(),
|
||||
),
|
||||
times,
|
||||
SeriesFields::try_from_vec(vec![floats, hist, mixed]).unwrap(),
|
||||
true,
|
||||
)
|
||||
.unwrap();
|
||||
let input_times_ptr = match &chunk.time {
|
||||
RawTimeAxis::Explicit(values) => values.as_ptr(),
|
||||
};
|
||||
let input_float_ptr = float_a.values().as_ptr();
|
||||
let input_hist_indices_ptr = indices.values().as_ptr();
|
||||
let input_mixed_float_ptr = mixed_float_owner.values().as_ptr();
|
||||
let input_mixed_indices_ptr = mixed_index_owner.values().as_ptr();
|
||||
|
||||
let input: SendableRawSeriesStream = Box::pin(RawFixture {
|
||||
schema: schema.clone(),
|
||||
binding: RawLabelSource::Explicit,
|
||||
chunk: Some(chunk),
|
||||
});
|
||||
let frames = pack_raw_series(input).unwrap();
|
||||
let mut decoded = unpack_raw_series(
|
||||
frames,
|
||||
schema,
|
||||
RawLabelSource::Explicit,
|
||||
RawSeriesOrdering::Contiguous,
|
||||
)
|
||||
.unwrap();
|
||||
let mut output = Vec::new();
|
||||
while let Some(item) = decoded.next().await {
|
||||
output.push(item.unwrap());
|
||||
}
|
||||
assert_eq!(
|
||||
output.len(),
|
||||
2,
|
||||
"the float field's segment boundary yields two frames"
|
||||
);
|
||||
|
||||
// Validate buffer sharing while the source owners are still alive.
|
||||
let DenseSamplesRef::Float(decoded_float) = output[0].fields.get(0).unwrap().segments()[0]
|
||||
.dense()
|
||||
.unwrap()
|
||||
else {
|
||||
panic!("expected float")
|
||||
};
|
||||
assert_eq!(decoded_float.values().as_ptr(), input_float_ptr);
|
||||
let DenseSamplesRef::Histogram(decoded_hist) =
|
||||
output[0].fields.get(1).unwrap().dense().unwrap()
|
||||
else {
|
||||
panic!("expected histogram")
|
||||
};
|
||||
assert_eq!(
|
||||
decoded_hist.indices().values().as_ptr(),
|
||||
input_hist_indices_ptr
|
||||
);
|
||||
assert!(
|
||||
decoded_hist
|
||||
.batch()
|
||||
.as_arrow()
|
||||
.to_data()
|
||||
.ptr_eq(&sliced_table.as_arrow().to_data())
|
||||
);
|
||||
let DenseSamplesRef::Mixed(decoded_mixed) = output[0].fields.get(2).unwrap().dense().unwrap()
|
||||
else {
|
||||
panic!("expected mixed")
|
||||
};
|
||||
assert_eq!(
|
||||
decoded_mixed.floats().values().as_ptr(),
|
||||
input_mixed_float_ptr
|
||||
);
|
||||
assert_eq!(
|
||||
decoded_mixed.histograms().indices().values().as_ptr(),
|
||||
input_mixed_indices_ptr
|
||||
);
|
||||
assert!(
|
||||
decoded_mixed
|
||||
.histograms()
|
||||
.batch()
|
||||
.as_arrow()
|
||||
.to_data()
|
||||
.ptr_eq(&sliced_table.as_arrow().to_data())
|
||||
);
|
||||
let RawTimeAxis::Explicit(first_times) = &output[0].time;
|
||||
assert_eq!(first_times.as_ptr(), input_times_ptr);
|
||||
|
||||
drop(decoded);
|
||||
drop(time_owner);
|
||||
drop(float_owner);
|
||||
drop(float_a);
|
||||
drop(float_b);
|
||||
drop(index_owner);
|
||||
drop(indices);
|
||||
drop(mixed_float_owner);
|
||||
drop(mixed_index_owner);
|
||||
drop(source_table);
|
||||
drop(table_owner);
|
||||
drop(sliced_table);
|
||||
assert!(table_weak.upgrade().is_none());
|
||||
assert!(sliced_weak.upgrade().is_none());
|
||||
|
||||
let expected_histograms = [(-9.5, Some(456)), (7.25, None), (-9.5, Some(456))];
|
||||
let mut times_seen = Vec::new();
|
||||
let mut float_seen = Vec::new();
|
||||
let mut histogram_seen = Vec::new();
|
||||
let mut mixed_seen = Vec::new();
|
||||
for chunk in &output {
|
||||
assert_eq!(chunk.read_key.as_slice(), [0xaa, 0x10, 0xbb]);
|
||||
assert_eq!(chunk.metric.as_ref().unwrap().name(), Some("cpu"));
|
||||
assert_eq!(chunk.metric.as_ref().unwrap().get("host"), Some("a"));
|
||||
assert_eq!(chunk.is_last, times_seen.len() + chunk.time.len() == 3);
|
||||
for i in 0..chunk.time.len() {
|
||||
let position = times_seen.len();
|
||||
times_seen.push(chunk.time.timestamp(i).unwrap());
|
||||
match chunk.fields.get(0).unwrap().sample(i) {
|
||||
SampleRef::Float(value) => float_seen.push(Some(value.to_bits())),
|
||||
SampleRef::Missing => float_seen.push(None),
|
||||
SampleRef::Histogram(_) => panic!("expected float field"),
|
||||
}
|
||||
let SampleRef::Histogram(value) = chunk.fields.get(1).unwrap().sample(i) else {
|
||||
panic!("expected histogram")
|
||||
};
|
||||
let (sum, start) = expected_histograms[position];
|
||||
assert_histogram(value, sum, start);
|
||||
histogram_seen.push(value.sum);
|
||||
match (position, chunk.fields.get(2).unwrap().sample(i)) {
|
||||
(0, SampleRef::Float(value)) => mixed_seen.push(Some(value.to_bits())),
|
||||
(1, SampleRef::Histogram(value)) => {
|
||||
assert_histogram(value, -9.5, Some(456));
|
||||
mixed_seen.push(None);
|
||||
}
|
||||
(2, SampleRef::Missing) => mixed_seen.push(None),
|
||||
_ => panic!("mixed payload variant did not match expected position {position}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
assert_eq!(times_seen, [11, 22, 33]);
|
||||
assert_eq!(
|
||||
float_seen,
|
||||
[
|
||||
None,
|
||||
Some(f64::from_bits(0x7ff8_0000_0000_0042).to_bits()),
|
||||
Some((-0.0_f64).to_bits())
|
||||
]
|
||||
);
|
||||
assert_eq!(histogram_seen, [-9.5, 7.25, -9.5]);
|
||||
assert_eq!(mixed_seen, [Some(1.25_f64.to_bits()), None, None]);
|
||||
}
|
||||
Reference in New Issue
Block a user