tests_fuzz/ir/
create_expr.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::collections::HashMap;
16use std::fmt::Display;
17
18use datatypes::value::Value;
19use derive_builder::Builder;
20use partition::partition::PartitionDef;
21use serde::{Deserialize, Serialize};
22
23use crate::ir::{Column, Ident};
24
25/// The column options
26#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
27pub enum ColumnOption {
28    Null,
29    NotNull,
30    DefaultValue(Value),
31    DefaultFn(String),
32    TimeIndex,
33    PrimaryKey,
34}
35
36impl Display for ColumnOption {
37    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38        match self {
39            ColumnOption::Null => write!(f, "NULL"),
40            ColumnOption::NotNull => write!(f, "NOT NULL"),
41            ColumnOption::DefaultFn(s) => write!(f, "DEFAULT {}", s),
42            ColumnOption::DefaultValue(s) => match s {
43                Value::String(value) => {
44                    write!(f, "DEFAULT \'{}\'", value.as_utf8())
45                }
46                _ => write!(f, "DEFAULT {}", s),
47            },
48            ColumnOption::TimeIndex => write!(f, "TIME INDEX"),
49            ColumnOption::PrimaryKey => write!(f, "PRIMARY KEY"),
50        }
51    }
52}
53
54/// A naive create table expr builder.
55#[derive(Debug, Builder, Clone, Serialize, Deserialize)]
56pub struct CreateTableExpr {
57    #[builder(setter(into))]
58    pub table_name: Ident,
59    pub columns: Vec<Column>,
60    #[builder(default)]
61    pub if_not_exists: bool,
62
63    // GreptimeDB specific options
64    #[builder(default, setter(into))]
65    pub partition: Option<PartitionDef>,
66    #[builder(default, setter(into))]
67    pub engine: String,
68    #[builder(default, setter(into))]
69    pub options: HashMap<String, Value>,
70    pub primary_keys: Vec<usize>,
71}
72
73#[derive(Debug, Builder, Clone, Serialize, Deserialize)]
74pub struct CreateDatabaseExpr {
75    #[builder(setter(into))]
76    pub database_name: Ident,
77    #[builder(default)]
78    pub if_not_exists: bool,
79}