store_api/storage/
requests.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::fmt::{Display, Formatter};
16
17use common_recordbatch::OrderOption;
18use datafusion_expr::expr::Expr;
19use strum::Display;
20
21use crate::storage::SequenceNumber;
22
23/// A hint on how to select rows from a time-series.
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Display)]
25pub enum TimeSeriesRowSelector {
26    /// Only keep the last row of each time-series.
27    LastRow,
28}
29
30/// A hint on how to distribute time-series data on the scan output.
31#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Display)]
32pub enum TimeSeriesDistribution {
33    /// Data are distributed by time window first. The scanner will
34    /// return all data within one time window before moving to the next one.
35    TimeWindowed,
36    /// Data are organized by time-series first. The scanner will return
37    /// all data for one time-series before moving to the next one.
38    PerSeries,
39}
40
41#[derive(Default, Clone, Debug, PartialEq, Eq)]
42pub struct ScanRequest {
43    /// Indices of columns to read, `None` to read all columns. This indices is
44    /// based on table schema.
45    pub projection: Option<Vec<usize>>,
46    /// Filters pushed down
47    pub filters: Vec<Expr>,
48    /// Expected output ordering. This is only a hint and isn't guaranteed.
49    pub output_ordering: Option<Vec<OrderOption>>,
50    /// limit can be used to reduce the amount scanned
51    /// from the datasource as a performance optimization.
52    /// If set, it contains the amount of rows needed by the caller,
53    /// The data source should return *at least* this number of rows if available.
54    pub limit: Option<usize>,
55    /// Optional hint to select rows from time-series.
56    pub series_row_selector: Option<TimeSeriesRowSelector>,
57    /// Optional constraint on the sequence number of the rows to read.
58    /// If set, only rows with a sequence number lesser or equal to this value
59    /// will be returned.
60    pub sequence: Option<SequenceNumber>,
61    /// Optional constraint on the minimal sequence number in the SST files.
62    /// If set, only the SST files that contain sequences greater than this value will be scanned.
63    pub sst_min_sequence: Option<SequenceNumber>,
64    /// Optional hint for the distribution of time-series data.
65    pub distribution: Option<TimeSeriesDistribution>,
66}
67
68impl Display for ScanRequest {
69    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
70        enum Delimiter {
71            None,
72            Init,
73        }
74
75        impl Delimiter {
76            fn as_str(&mut self) -> &str {
77                match self {
78                    Delimiter::None => {
79                        *self = Delimiter::Init;
80                        ""
81                    }
82                    Delimiter::Init => ", ",
83                }
84            }
85        }
86
87        let mut delimiter = Delimiter::None;
88
89        write!(f, "ScanRequest {{ ")?;
90        if let Some(projection) = &self.projection {
91            write!(f, "{}projection: {:?}", delimiter.as_str(), projection)?;
92        }
93        if !self.filters.is_empty() {
94            write!(
95                f,
96                "{}filters: [{}]",
97                delimiter.as_str(),
98                self.filters
99                    .iter()
100                    .map(|f| f.to_string())
101                    .collect::<Vec<_>>()
102                    .join(", ")
103            )?;
104        }
105        if let Some(output_ordering) = &self.output_ordering {
106            write!(
107                f,
108                "{}output_ordering: {:?}",
109                delimiter.as_str(),
110                output_ordering
111            )?;
112        }
113        if let Some(limit) = &self.limit {
114            write!(f, "{}limit: {}", delimiter.as_str(), limit)?;
115        }
116        if let Some(series_row_selector) = &self.series_row_selector {
117            write!(
118                f,
119                "{}series_row_selector: {}",
120                delimiter.as_str(),
121                series_row_selector
122            )?;
123        }
124        if let Some(sequence) = &self.sequence {
125            write!(f, "{}sequence: {}", delimiter.as_str(), sequence)?;
126        }
127        if let Some(sst_min_sequence) = &self.sst_min_sequence {
128            write!(
129                f,
130                "{}sst_min_sequence: {}",
131                delimiter.as_str(),
132                sst_min_sequence
133            )?;
134        }
135        if let Some(distribution) = &self.distribution {
136            write!(f, "{}distribution: {}", delimiter.as_str(), distribution)?;
137        }
138        write!(f, " }}")
139    }
140}
141
142#[cfg(test)]
143mod tests {
144    use datafusion_expr::{binary_expr, col, lit, Operator};
145
146    use super::*;
147
148    #[test]
149    fn test_display_scan_request() {
150        let request = ScanRequest {
151            ..Default::default()
152        };
153        assert_eq!(request.to_string(), "ScanRequest {  }");
154
155        let request = ScanRequest {
156            projection: Some(vec![1, 2]),
157            filters: vec![
158                binary_expr(col("i"), Operator::Gt, lit(1)),
159                binary_expr(col("s"), Operator::Eq, lit("x")),
160            ],
161            limit: Some(10),
162            ..Default::default()
163        };
164        assert_eq!(
165            request.to_string(),
166            r#"ScanRequest { projection: [1, 2], filters: [i > Int32(1), s = Utf8("x")], limit: 10 }"#
167        );
168
169        let request = ScanRequest {
170            filters: vec![
171                binary_expr(col("i"), Operator::Gt, lit(1)),
172                binary_expr(col("s"), Operator::Eq, lit("x")),
173            ],
174            limit: Some(10),
175            ..Default::default()
176        };
177        assert_eq!(
178            request.to_string(),
179            r#"ScanRequest { filters: [i > Int32(1), s = Utf8("x")], limit: 10 }"#
180        );
181
182        let request = ScanRequest {
183            projection: Some(vec![1, 2]),
184            limit: Some(10),
185            ..Default::default()
186        };
187        assert_eq!(
188            request.to_string(),
189            "ScanRequest { projection: [1, 2], limit: 10 }"
190        );
191    }
192}