1mod merge;
16#[cfg(test)]
17mod tests;
18
19pub(crate) use merge::merge_raw_identity_charging;
20
21use std::pin::Pin;
22use std::sync::Arc;
23use std::task::{Context, Poll};
24
25use common_error::ext::BoxedError;
26use datatypes::error::InvalidSeriesSnafu;
27use datatypes::series::SeriesOrdering;
28use datatypes::series::frame::LabelDecodeSpec;
29use datatypes::series::raw::RawSeriesChunk;
30use datatypes::series::samples::SeriesSchema;
31use futures::Stream;
32use mito_codec::labels::PrimaryKeyLabelDecoder;
33use store_api::region_engine::{RawLabelSource, RawSeriesStream, SendableRawSeriesStream};
34
35pub fn materialize_raw_labels(
41 source: SendableRawSeriesStream,
42) -> Result<SendableRawSeriesStream, BoxedError> {
43 let schema = Arc::clone(source.schema());
44 let decode = source.label_decode_spec().cloned();
45 let ordering = source.ordering();
46 if schema.eval_grid.is_some() {
47 return Err(BoxedError::new(
48 InvalidSeriesSnafu {
49 reason: "raw series schema cannot have an evaluation grid",
50 }
51 .build(),
52 ));
53 }
54 if schema.fields.is_empty() {
55 return Err(BoxedError::new(
56 InvalidSeriesSnafu {
57 reason: "raw series schema must have fields",
58 }
59 .build(),
60 ));
61 }
62 let decoder = match &decode {
63 Some(description) => {
64 Some(PrimaryKeyLabelDecoder::from_decode_spec(description).map_err(BoxedError::new)?)
65 }
66 None => None,
67 };
68
69 Ok(Box::pin(MaterializedRawSeriesStream {
70 source: Some(source),
71 schema,
72 ordering,
73 decoder,
74 label_source: RawLabelSource::Explicit,
75 }))
76}
77
78struct MaterializedRawSeriesStream {
79 source: Option<SendableRawSeriesStream>,
80 schema: Arc<SeriesSchema>,
81 ordering: SeriesOrdering,
82 decoder: Option<PrimaryKeyLabelDecoder>,
83 label_source: RawLabelSource,
84}
85
86impl Stream for MaterializedRawSeriesStream {
87 type Item = Result<RawSeriesChunk, BoxedError>;
88
89 fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
90 let this = self.get_mut();
91 let Some(source) = this.source.as_mut() else {
92 return Poll::Ready(None);
93 };
94 match source.as_mut().poll_next(cx) {
95 Poll::Pending => Poll::Pending,
96 Poll::Ready(None) => {
97 this.source = None;
98 this.decoder = None;
99 Poll::Ready(None)
100 }
101 Poll::Ready(Some(Err(error))) => {
102 this.source = None;
103 this.decoder = None;
104 Poll::Ready(Some(Err(error)))
105 }
106 Poll::Ready(Some(Ok(chunk))) => {
107 let result = this.materialize(chunk);
108 if result.is_err() {
109 this.source = None;
110 this.decoder = None;
111 }
112 Poll::Ready(Some(result))
113 }
114 }
115 }
116}
117
118impl RawSeriesStream for MaterializedRawSeriesStream {
119 fn schema(&self) -> &Arc<SeriesSchema> {
120 &self.schema
121 }
122
123 fn label_source(&self) -> &RawLabelSource {
124 &self.label_source
125 }
126
127 fn label_decode_spec(&self) -> Option<&LabelDecodeSpec> {
130 None
131 }
132
133 fn ordering(&self) -> SeriesOrdering {
134 self.ordering
135 }
136}
137
138impl MaterializedRawSeriesStream {
139 fn materialize(&mut self, chunk: RawSeriesChunk) -> Result<RawSeriesChunk, BoxedError> {
140 let metric = match (&mut self.decoder, chunk.metric) {
141 (Some(decoder), None) => Some(
142 decoder
143 .decode(&chunk.primary_key)
144 .map_err(BoxedError::new)?,
145 ),
146 (None, Some(metric)) => Some(metric),
147 (Some(_), Some(_)) => {
148 return Err(BoxedError::new(
149 InvalidSeriesSnafu {
150 reason: "raw chunk already carries labels but the source describes a key",
151 }
152 .build(),
153 ));
154 }
155 (None, None) => {
156 return Err(BoxedError::new(
157 InvalidSeriesSnafu {
158 reason: "raw chunk carries no labels and the source describes no key",
159 }
160 .build(),
161 ));
162 }
163 };
164 RawSeriesChunk::try_new(
165 &self.schema,
166 chunk.primary_key,
167 metric,
168 chunk.time,
169 chunk.fields,
170 chunk.is_last,
171 )
172 .map_err(BoxedError::new)
173 }
174}