Skip to main content

store_api/storage/
projection.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
17/// A nested field access path.
18///
19/// Each path represents a field access on a nested column.
20///
21/// Example:
22/// - `j.a.b` -> `["j", "a", "b"]`
23pub type NestedPath = Vec<String>;
24
25/// Projection information for a table scan.
26#[derive(Default, Debug, Clone, PartialEq)]
27pub struct ProjectionInput {
28    /// Top-level column projection.
29    ///
30    /// The indices are based on the schema exposed by the table scan input,
31    /// such as the schema passed to `TableProvider::scan`.
32    ///
33    /// Only the root columns with the specified schema indices are needed.
34    pub projection: Vec<usize>,
35    /// Nested field access paths used for sub-field projection.
36    ///
37    /// It extends and refines the top-level projection by specifying nested
38    /// field accesses inside complex columns such as JSON or struct columns.
39    ///
40    /// In other words:
41    /// - `projection` determines **which root columns are needed**
42    /// - `nested_paths` further determines **which sub-fields inside those
43    ///   columns are required**
44    ///
45    /// Each path starts with the root column name and continues with
46    /// nested field names.
47    pub nested_paths: Vec<NestedPath>,
48}
49
50impl ProjectionInput {
51    pub fn new(projection: Vec<usize>) -> Self {
52        Self {
53            projection,
54            nested_paths: Vec::new(),
55        }
56    }
57
58    pub fn with_nested_paths(mut self, nested_paths: Vec<NestedPath>) -> Self {
59        self.nested_paths = nested_paths;
60        self
61    }
62}
63
64impl From<Vec<usize>> for ProjectionInput {
65    fn from(projection: Vec<usize>) -> Self {
66        Self::new(projection)
67    }
68}
69
70impl Display for ProjectionInput {
71    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
72        write!(
73            f,
74            "ProjectionInput {{ projection: {:?}, nested_paths: {:?} }}",
75            self.projection, self.nested_paths
76        )
77    }
78}