pipeline/manager/
util.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 common_time::Timestamp;
16use datafusion_expr::{col, lit, Expr};
17use datatypes::timestamp::TimestampNanosecond;
18
19use crate::error::{InvalidPipelineVersionSnafu, Result};
20use crate::table::{
21    PIPELINE_TABLE_CREATED_AT_COLUMN_NAME, PIPELINE_TABLE_PIPELINE_NAME_COLUMN_NAME,
22    PIPELINE_TABLE_PIPELINE_SCHEMA_COLUMN_NAME,
23};
24use crate::PipelineVersion;
25
26pub fn to_pipeline_version(version_str: Option<&str>) -> Result<PipelineVersion> {
27    match version_str {
28        Some(version) => {
29            let ts = Timestamp::from_str_utc(version)
30                .map_err(|_| InvalidPipelineVersionSnafu { version }.build())?;
31            Ok(Some(TimestampNanosecond(ts)))
32        }
33        None => Ok(None),
34    }
35}
36
37pub(crate) fn prepare_dataframe_conditions(
38    schema: &str,
39    name: &str,
40    version: PipelineVersion,
41) -> Expr {
42    let mut conditions = vec![
43        col(PIPELINE_TABLE_PIPELINE_NAME_COLUMN_NAME).eq(lit(name)),
44        col(PIPELINE_TABLE_PIPELINE_SCHEMA_COLUMN_NAME).eq(lit(schema)),
45    ];
46
47    if let Some(v) = version {
48        conditions
49            .push(col(PIPELINE_TABLE_CREATED_AT_COLUMN_NAME).eq(lit(v.0.to_iso8601_string())));
50    }
51
52    conditions.into_iter().reduce(Expr::and).unwrap()
53}
54
55pub(crate) fn generate_pipeline_cache_key(
56    schema: &str,
57    name: &str,
58    version: PipelineVersion,
59) -> String {
60    match version {
61        Some(version) => format!("{}/{}/{}", schema, name, i64::from(version)),
62        None => format!("{}/{}/latest", schema, name),
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69
70    #[test]
71    fn test_to_pipeline_version() {
72        let none_result = to_pipeline_version(None);
73        assert!(none_result.is_ok());
74        assert!(none_result.unwrap().is_none());
75
76        let some_result = to_pipeline_version(Some("2023-01-01 00:00:00Z"));
77        assert!(some_result.is_ok());
78        assert_eq!(
79            some_result.unwrap(),
80            Some(TimestampNanosecond::new(1672531200000000000))
81        );
82
83        let invalid = to_pipeline_version(Some("invalid"));
84        assert!(invalid.is_err());
85    }
86
87    #[test]
88    fn test_generate_pipeline_cache_key() {
89        let schema = "test_schema";
90        let name = "test_name";
91        let latest = generate_pipeline_cache_key(schema, name, None);
92        assert_eq!(latest, "test_schema/test_name/latest");
93
94        let versioned = generate_pipeline_cache_key(
95            schema,
96            name,
97            Some(TimestampNanosecond::new(1672531200000000000)),
98        );
99        assert_eq!(versioned, "test_schema/test_name/1672531200000000000");
100    }
101}