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(
45 source: SendableRawSeriesStream,
46) -> Result<SendableRawSeriesStream, BoxedError> {
47 let schema = Arc::clone(source.schema());
48 let decode = source.label_decode_spec().cloned();
49 let ordering = source.ordering();
50 if schema.eval_grid.is_some() {
51 return Err(BoxedError::new(
52 InvalidSeriesSnafu {
53 reason: "raw series schema cannot have an evaluation grid",
54 }
55 .build(),
56 ));
57 }
58 if schema.fields.is_empty() {
59 return Err(BoxedError::new(
60 InvalidSeriesSnafu {
61 reason: "raw series schema must have fields",
62 }
63 .build(),
64 ));
65 }
66 let decoder = match &decode {
67 Some(description) => {
68 Some(PrimaryKeyLabelDecoder::from_decode_spec(description).map_err(BoxedError::new)?)
69 }
70 None => None,
71 };
72
73 Ok(Box::pin(MaterializedRawSeriesStream {
74 source: Some(source),
75 schema,
76 ordering,
77 decoder,
78 label_source: RawLabelSource::Explicit,
79 }))
80}
81
82struct MaterializedRawSeriesStream {
83 source: Option<SendableRawSeriesStream>,
84 schema: Arc<SeriesSchema>,
85 ordering: SeriesOrdering,
86 decoder: Option<PrimaryKeyLabelDecoder>,
87 label_source: RawLabelSource,
88}
89
90impl Stream for MaterializedRawSeriesStream {
91 type Item = Result<RawSeriesChunk, BoxedError>;
92
93 fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
94 let this = self.get_mut();
95 let Some(source) = this.source.as_mut() else {
96 return Poll::Ready(None);
97 };
98 match source.as_mut().poll_next(cx) {
99 Poll::Pending => Poll::Pending,
100 Poll::Ready(None) => {
101 this.source = None;
102 this.decoder = None;
103 Poll::Ready(None)
104 }
105 Poll::Ready(Some(Err(error))) => {
106 this.source = None;
107 this.decoder = None;
108 Poll::Ready(Some(Err(error)))
109 }
110 Poll::Ready(Some(Ok(chunk))) => {
111 let result = this.materialize(chunk);
112 if result.is_err() {
113 this.source = None;
114 this.decoder = None;
115 }
116 Poll::Ready(Some(result))
117 }
118 }
119 }
120}
121
122impl RawSeriesStream for MaterializedRawSeriesStream {
123 fn schema(&self) -> &Arc<SeriesSchema> {
124 &self.schema
125 }
126
127 fn label_source(&self) -> &RawLabelSource {
128 &self.label_source
129 }
130
131 fn label_decode_spec(&self) -> Option<&LabelDecodeSpec> {
134 None
135 }
136
137 fn ordering(&self) -> SeriesOrdering {
138 self.ordering
139 }
140}
141
142impl MaterializedRawSeriesStream {
143 fn materialize(&mut self, chunk: RawSeriesChunk) -> Result<RawSeriesChunk, BoxedError> {
144 let metric = match (&mut self.decoder, chunk.metric) {
145 (Some(decoder), None) => Some(
146 decoder
147 .decode(&chunk.primary_key)
148 .map_err(BoxedError::new)?,
149 ),
150 (None, Some(metric)) => Some(metric),
151 (Some(_), Some(_)) => {
152 return Err(BoxedError::new(
153 InvalidSeriesSnafu {
154 reason: "raw chunk already carries labels but the source describes a key",
155 }
156 .build(),
157 ));
158 }
159 (None, None) => {
160 return Err(BoxedError::new(
161 InvalidSeriesSnafu {
162 reason: "raw chunk carries no labels and the source describes no key",
163 }
164 .build(),
165 ));
166 }
167 };
168 RawSeriesChunk::try_new(
169 &self.schema,
170 chunk.primary_key,
171 metric,
172 chunk.time,
173 chunk.fields,
174 chunk.is_last,
175 )
176 .map_err(BoxedError::new)
177 }
178}