datafusion/test/
variable.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! System variable provider
19
20use crate::error::Result;
21use crate::scalar::ScalarValue;
22use crate::variable::VarProvider;
23use arrow::datatypes::DataType;
24
25/// System variable
26#[derive(Default, Debug)]
27pub struct SystemVar {}
28
29impl SystemVar {
30    /// new system variable
31    pub fn new() -> Self {
32        Self {}
33    }
34}
35
36impl VarProvider for SystemVar {
37    /// get system variable value
38    fn get_value(&self, var_names: Vec<String>) -> Result<ScalarValue> {
39        let s = format!("{}-{}", "system-var", var_names.concat());
40        Ok(ScalarValue::from(s))
41    }
42
43    fn get_type(&self, _: &[String]) -> Option<DataType> {
44        Some(DataType::Utf8)
45    }
46}
47
48/// user defined variable
49#[derive(Default, Debug)]
50pub struct UserDefinedVar {}
51
52impl UserDefinedVar {
53    /// new user defined variable
54    pub fn new() -> Self {
55        Self {}
56    }
57}
58
59impl VarProvider for UserDefinedVar {
60    /// Get user defined variable value
61    fn get_value(&self, var_names: Vec<String>) -> Result<ScalarValue> {
62        if var_names[0] != "@integer" {
63            let s = format!("{}-{}", "user-defined-var", var_names.concat());
64            Ok(ScalarValue::from(s))
65        } else {
66            Ok(ScalarValue::Int32(Some(41)))
67        }
68    }
69
70    fn get_type(&self, var_names: &[String]) -> Option<DataType> {
71        if var_names[0] != "@integer" {
72            Some(DataType::Utf8)
73        } else {
74            Some(DataType::Int32)
75        }
76    }
77}