Skip to main content

rustfs_s3select_api/query/
mod.rs

1// Copyright 2024 RustFS 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 s3s::dto::SelectObjectContentInput;
16use std::sync::Arc;
17
18use crate::{SelectInputMetrics, SelectObjectSnapshot};
19
20pub mod analyzer;
21pub mod ast;
22pub mod dispatcher;
23pub mod execution;
24pub mod function;
25pub mod logical_planner;
26pub mod optimizer;
27pub mod parser;
28pub mod physical_planner;
29pub mod scheduler;
30pub mod session;
31
32#[derive(Clone)]
33pub struct Context {
34    // maybe we need transfer some info?
35    pub input: Arc<SelectObjectContentInput>,
36}
37
38#[derive(Clone)]
39pub struct Query {
40    context: Context,
41    content: String,
42    snapshot: Option<Arc<SelectObjectSnapshot>>,
43    input_metrics: Arc<SelectInputMetrics>,
44}
45
46impl Query {
47    #[inline(always)]
48    pub fn new(context: Context, content: String) -> Self {
49        Self {
50            context,
51            content,
52            snapshot: None,
53            input_metrics: Arc::new(SelectInputMetrics::default()),
54        }
55    }
56
57    #[inline(always)]
58    pub fn new_with_snapshot(context: Context, content: String, snapshot: Arc<SelectObjectSnapshot>) -> Self {
59        Self {
60            context,
61            content,
62            snapshot: Some(snapshot),
63            input_metrics: Arc::new(SelectInputMetrics::default()),
64        }
65    }
66
67    pub fn context(&self) -> &Context {
68        &self.context
69    }
70
71    pub fn content(&self) -> &str {
72        self.content.as_str()
73    }
74
75    pub fn snapshot(&self) -> Option<&Arc<SelectObjectSnapshot>> {
76        self.snapshot.as_ref()
77    }
78
79    pub fn input_metrics(&self) -> &Arc<SelectInputMetrics> {
80        &self.input_metrics
81    }
82
83    pub fn for_execution(&self) -> Self {
84        Self {
85            context: self.context.clone(),
86            content: self.content.clone(),
87            snapshot: self.snapshot.clone(),
88            input_metrics: Arc::new(SelectInputMetrics::default()),
89        }
90    }
91}
92
93#[cfg(test)]
94fn test_query() -> Query {
95    use s3s::dto::{CSVInput, CSVOutput, ExpressionType, InputSerialization, OutputSerialization, SelectObjectContentRequest};
96
97    let input = SelectObjectContentInput {
98        bucket: "bucket".to_string(),
99        expected_bucket_owner: None,
100        key: "input.csv".to_string(),
101        sse_customer_algorithm: None,
102        sse_customer_key: None,
103        sse_customer_key_md5: None,
104        request: SelectObjectContentRequest {
105            expression: "SELECT * FROM S3Object".to_string(),
106            expression_type: ExpressionType::from_static(ExpressionType::SQL),
107            input_serialization: InputSerialization {
108                csv: Some(CSVInput::default()),
109                ..Default::default()
110            },
111            output_serialization: OutputSerialization {
112                csv: Some(CSVOutput::default()),
113                ..Default::default()
114            },
115            request_progress: None,
116            scan_range: None,
117        },
118    };
119    Query::new(Context { input: Arc::new(input) }, "SELECT * FROM S3Object".to_string())
120}