Skip to main content

sql/statements/
create.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, Formatter};
17
18use common_catalog::consts::FILE_ENGINE;
19use datatypes::json::{JSON2_DEFAULT_MAX_AUTO_EXPANDED_PATHS, JsonSettings};
20use datatypes::prelude::ConcreteDataType;
21use datatypes::schema::{
22    FulltextOptions, SkippingIndexOptions, VectorDistanceMetric, VectorIndexEngineType,
23    VectorIndexOptions,
24};
25use itertools::Itertools;
26use serde::Serialize;
27use snafu::ResultExt;
28use sqlparser::ast::{ColumnOptionDef, DataType, Expr};
29use sqlparser_derive::{Visit, VisitMut};
30
31use crate::ast::{ColumnDef, Ident, ObjectName, Value as SqlValue};
32use crate::error::{
33    InvalidFlowQuerySnafu, InvalidSqlSnafu, Result, SetFulltextOptionSnafu,
34    SetSkippingIndexOptionSnafu,
35};
36use crate::statements::query::Query as GtQuery;
37use crate::statements::statement::Statement;
38use crate::statements::tql::Tql;
39use crate::statements::{OptionMap, sql_data_type_to_concrete_data_type};
40
41const LINE_SEP: &str = ",\n";
42const COMMA_SEP: &str = ", ";
43const INDENT: usize = 2;
44pub const VECTOR_OPT_DIM: &str = "dim";
45
46macro_rules! format_indent {
47    ($fmt: expr, $arg: expr) => {
48        format!($fmt, format_args!("{: >1$}", "", INDENT), $arg)
49    };
50    ($arg: expr) => {
51        format_indent!("{}{}", $arg)
52    };
53}
54
55macro_rules! format_list_indent {
56    ($list: expr) => {
57        $list.iter().map(|e| format_indent!(e)).join(LINE_SEP)
58    };
59}
60
61macro_rules! format_list_comma {
62    ($list: expr) => {
63        $list.iter().map(|e| format!("{}", e)).join(COMMA_SEP)
64    };
65}
66
67#[cfg(feature = "enterprise")]
68pub mod trigger;
69
70fn format_table_constraint(constraints: &[TableConstraint]) -> String {
71    constraints.iter().map(|c| format_indent!(c)).join(LINE_SEP)
72}
73
74/// Table constraint for create table statement.
75#[derive(Debug, PartialEq, Eq, Clone, Visit, VisitMut, Serialize)]
76pub enum TableConstraint {
77    /// Primary key constraint.
78    PrimaryKey { columns: Vec<Ident> },
79    /// Time index constraint.
80    TimeIndex { column: Ident },
81}
82
83impl Display for TableConstraint {
84    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
85        match self {
86            TableConstraint::PrimaryKey { columns } => {
87                write!(f, "PRIMARY KEY ({})", format_list_comma!(columns))
88            }
89            TableConstraint::TimeIndex { column } => {
90                write!(f, "TIME INDEX ({})", column)
91            }
92        }
93    }
94}
95
96#[derive(Debug, PartialEq, Eq, Clone, Visit, VisitMut, Serialize)]
97pub struct CreateTable {
98    /// Create if not exists
99    pub if_not_exists: bool,
100    pub table_id: u32,
101    /// Table name
102    pub name: ObjectName,
103    pub columns: Vec<Column>,
104    pub engine: String,
105    pub constraints: Vec<TableConstraint>,
106    /// Table options in `WITH`. All keys are lowercase.
107    pub options: OptionMap,
108    pub partitions: Option<Partitions>,
109}
110
111/// Column definition in `CREATE TABLE` statement.
112#[derive(Debug, PartialEq, Eq, Clone, Visit, VisitMut, Serialize)]
113pub struct Column {
114    /// `ColumnDef` from `sqlparser::ast`
115    pub column_def: ColumnDef,
116    /// Column extensions for greptimedb dialect.
117    pub extensions: ColumnExtensions,
118}
119
120/// Column extensions for greptimedb dialect.
121#[derive(Debug, PartialEq, Eq, Clone, Visit, VisitMut, Default, Serialize)]
122pub struct ColumnExtensions {
123    /// Vector type options.
124    pub vector_options: Option<OptionMap>,
125
126    /// Fulltext index options.
127    pub fulltext_index_options: Option<OptionMap>,
128    /// Skipping index options.
129    pub skipping_index_options: Option<OptionMap>,
130    /// Inverted index options.
131    ///
132    /// Inverted index doesn't have options at present. There won't be any options in that map.
133    pub inverted_index_options: Option<OptionMap>,
134    /// Vector index options for HNSW-based vector similarity search.
135    pub vector_index_options: Option<OptionMap>,
136    /// JSON2-specific column options.
137    pub json2_options: Option<Json2Options>,
138}
139
140/// JSON2-specific options represented in the SQL AST.
141#[derive(Debug, PartialEq, Eq, Clone, Visit, VisitMut, Default, Serialize)]
142pub struct Json2Options {
143    /// Maximum number of unhinted JSON2 paths expanded into Arrow fields.
144    pub(crate) max_auto_expanded_paths: Option<u32>,
145    /// Paths stored as explicitly typed JSON2 fields.
146    pub(crate) type_hints: Vec<JsonTypeHint>,
147}
148
149impl Display for Json2Options {
150    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
151        let mut options = Vec::with_capacity(self.type_hints.len() + 1);
152        if let Some(max) = self.max_auto_expanded_paths {
153            options.push(format!("max_auto_expanded_paths = {max}"));
154        }
155        options.extend(self.type_hints.iter().map(format_json_type_hint));
156        write!(f, "(\n    {}\n  )", options.iter().join(",\n    "))
157    }
158}
159
160#[derive(Debug, PartialEq, Eq, Clone, Visit, VisitMut, Serialize)]
161pub struct JsonTypeHint {
162    pub path: Vec<String>,
163    pub data_type: DataType,
164    pub inverted_index: bool,
165}
166
167impl Column {
168    pub fn name(&self) -> &Ident {
169        &self.column_def.name
170    }
171
172    pub fn data_type(&self) -> &DataType {
173        &self.column_def.data_type
174    }
175
176    pub fn mut_data_type(&mut self) -> &mut DataType {
177        &mut self.column_def.data_type
178    }
179
180    pub fn options(&self) -> &[ColumnOptionDef] {
181        &self.column_def.options
182    }
183
184    pub fn mut_options(&mut self) -> &mut Vec<ColumnOptionDef> {
185        &mut self.column_def.options
186    }
187}
188
189impl Display for Column {
190    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
191        if let Some(vector_options) = &self.extensions.vector_options
192            && let Some(dim) = vector_options.get(VECTOR_OPT_DIM)
193        {
194            write!(f, "{} VECTOR({})", self.column_def.name, dim)?;
195            return Ok(());
196        }
197
198        write!(f, "{} {}", self.column_def.name, self.column_def.data_type)?;
199        if let Some(options) = &self.extensions.json2_options {
200            write!(f, "{options}")?;
201        }
202        for option in &self.column_def.options {
203            write!(f, " {option}")?;
204        }
205
206        if let Some(fulltext_options) = &self.extensions.fulltext_index_options {
207            if !fulltext_options.is_empty() {
208                let options = fulltext_options.kv_pairs();
209                write!(f, " FULLTEXT INDEX WITH({})", format_list_comma!(options))?;
210            } else {
211                write!(f, " FULLTEXT INDEX")?;
212            }
213        }
214
215        if let Some(skipping_index_options) = &self.extensions.skipping_index_options {
216            if !skipping_index_options.is_empty() {
217                let options = skipping_index_options.kv_pairs();
218                write!(f, " SKIPPING INDEX WITH({})", format_list_comma!(options))?;
219            } else {
220                write!(f, " SKIPPING INDEX")?;
221            }
222        }
223
224        if let Some(inverted_index_options) = &self.extensions.inverted_index_options {
225            if !inverted_index_options.is_empty() {
226                let options = inverted_index_options.kv_pairs();
227                write!(f, " INVERTED INDEX WITH({})", format_list_comma!(options))?;
228            } else {
229                write!(f, " INVERTED INDEX")?;
230            }
231        }
232
233        if let Some(vector_index_options) = &self.extensions.vector_index_options {
234            if !vector_index_options.is_empty() {
235                let options = vector_index_options.kv_pairs();
236                write!(f, " VECTOR INDEX WITH({})", format_list_comma!(options))?;
237            } else {
238                write!(f, " VECTOR INDEX")?;
239            }
240        }
241        Ok(())
242    }
243}
244
245impl ColumnExtensions {
246    pub fn build_fulltext_options(&self) -> Result<Option<FulltextOptions>> {
247        let Some(options) = self.fulltext_index_options.as_ref() else {
248            return Ok(None);
249        };
250
251        let options: HashMap<String, String> = options.clone().into_map();
252        Ok(Some(options.try_into().context(SetFulltextOptionSnafu)?))
253    }
254
255    pub fn build_skipping_index_options(&self) -> Result<Option<SkippingIndexOptions>> {
256        let Some(options) = self.skipping_index_options.as_ref() else {
257            return Ok(None);
258        };
259
260        let options: HashMap<String, String> = options.clone().into_map();
261        Ok(Some(
262            options.try_into().context(SetSkippingIndexOptionSnafu)?,
263        ))
264    }
265
266    pub fn build_vector_index_options(&self) -> Result<Option<VectorIndexOptions>> {
267        let Some(options) = self.vector_index_options.as_ref() else {
268            return Ok(None);
269        };
270
271        let options_map: HashMap<String, String> = options.clone().into_map();
272        let mut result = VectorIndexOptions::default();
273
274        if let Some(s) = options_map.get("engine") {
275            result.engine = s.parse::<VectorIndexEngineType>().map_err(|e| {
276                InvalidSqlSnafu {
277                    msg: format!("invalid VECTOR INDEX engine: {e}"),
278                }
279                .build()
280            })?;
281        }
282
283        if let Some(s) = options_map.get("metric") {
284            result.metric = s.parse::<VectorDistanceMetric>().map_err(|e| {
285                InvalidSqlSnafu {
286                    msg: format!("invalid VECTOR INDEX metric: {e}"),
287                }
288                .build()
289            })?;
290        }
291
292        if let Some(s) = options_map.get("connectivity") {
293            let value = s.parse::<u32>().map_err(|_| {
294                InvalidSqlSnafu {
295                    msg: format!(
296                        "invalid VECTOR INDEX connectivity: {s}, expected positive integer"
297                    ),
298                }
299                .build()
300            })?;
301            if !(2..=2048).contains(&value) {
302                return InvalidSqlSnafu {
303                    msg: "VECTOR INDEX connectivity must be in the range [2, 2048].".to_string(),
304                }
305                .fail();
306            }
307            result.connectivity = value;
308        }
309
310        if let Some(s) = options_map.get("expansion_add") {
311            let value = s.parse::<u32>().map_err(|_| {
312                InvalidSqlSnafu {
313                    msg: format!(
314                        "invalid VECTOR INDEX expansion_add: {s}, expected positive integer"
315                    ),
316                }
317                .build()
318            })?;
319            if value == 0 {
320                return InvalidSqlSnafu {
321                    msg: "VECTOR INDEX expansion_add must be greater than 0".to_string(),
322                }
323                .fail();
324            }
325            result.expansion_add = value;
326        }
327
328        if let Some(s) = options_map.get("expansion_search") {
329            let value = s.parse::<u32>().map_err(|_| {
330                InvalidSqlSnafu {
331                    msg: format!(
332                        "invalid VECTOR INDEX expansion_search: {s}, expected positive integer"
333                    ),
334                }
335                .build()
336            })?;
337            if value == 0 {
338                return InvalidSqlSnafu {
339                    msg: "VECTOR INDEX expansion_search must be greater than 0".to_string(),
340                }
341                .fail();
342            }
343            result.expansion_search = value;
344        }
345
346        Ok(Some(result))
347    }
348
349    pub fn build_json_settings(&self) -> Result<Option<JsonSettings>> {
350        let Some(options) = &self.json2_options else {
351            return Ok(None);
352        };
353
354        let type_hints = options
355            .type_hints
356            .iter()
357            .map(|hint| {
358                Ok(datatypes::json::JsonTypeHint {
359                    path: hint.path.clone(),
360                    data_type: json_type_hint_concrete_data_type(&hint.data_type)?,
361                    inverted_index: hint.inverted_index,
362                })
363            })
364            .collect::<Result<Vec<_>>>()?;
365        let settings = JsonSettings::try_new(
366            type_hints,
367            options
368                .max_auto_expanded_paths
369                .or(Some(JSON2_DEFAULT_MAX_AUTO_EXPANDED_PATHS)),
370        )?;
371        Ok(Some(settings))
372    }
373
374    pub fn set_json_settings(&mut self, settings: JsonSettings) -> Result<()> {
375        let (type_hints, max_auto_expanded_paths) = settings.into_parts();
376        let type_hints = type_hints
377            .into_iter()
378            .map(|hint| {
379                let data_type = json_type_hint_sql_data_type(&hint.data_type)?;
380                Ok(JsonTypeHint {
381                    path: hint.path,
382                    data_type,
383                    inverted_index: hint.inverted_index,
384                })
385            })
386            .collect::<Result<Vec<_>>>()?;
387        self.json2_options = (max_auto_expanded_paths.is_some() || !type_hints.is_empty())
388            .then_some(Json2Options {
389                max_auto_expanded_paths,
390                type_hints,
391            });
392        Ok(())
393    }
394}
395
396fn json_type_hint_concrete_data_type(data_type: &DataType) -> Result<ConcreteDataType> {
397    let data_type = sql_data_type_to_concrete_data_type(data_type)?;
398    normalize_json_type_hint_concrete_data_type(&data_type)
399}
400
401fn normalize_json_type_hint_concrete_data_type(
402    data_type: &ConcreteDataType,
403) -> Result<ConcreteDataType> {
404    let normalized = match data_type {
405        ConcreteDataType::String(_) => ConcreteDataType::string_datatype(),
406        ConcreteDataType::Int8(_)
407        | ConcreteDataType::Int16(_)
408        | ConcreteDataType::Int32(_)
409        | ConcreteDataType::Int64(_) => ConcreteDataType::int64_datatype(),
410        ConcreteDataType::UInt8(_)
411        | ConcreteDataType::UInt16(_)
412        | ConcreteDataType::UInt32(_)
413        | ConcreteDataType::UInt64(_) => ConcreteDataType::uint64_datatype(),
414        ConcreteDataType::Float32(_) | ConcreteDataType::Float64(_) => {
415            ConcreteDataType::float64_datatype()
416        }
417        ConcreteDataType::Boolean(_) => ConcreteDataType::boolean_datatype(),
418        _ => {
419            return InvalidSqlSnafu {
420                msg: format!("unsupported JSON2 type hint data type: {data_type}"),
421            }
422            .fail();
423        }
424    };
425    Ok(normalized)
426}
427
428fn json_type_hint_sql_data_type(data_type: &ConcreteDataType) -> Result<DataType> {
429    let data_type = normalize_json_type_hint_concrete_data_type(data_type)?;
430    let sql_type = match data_type {
431        ConcreteDataType::String(_) => DataType::String(None),
432        ConcreteDataType::Int64(_) => DataType::BigInt(None),
433        ConcreteDataType::UInt64(_) => DataType::BigIntUnsigned(None),
434        ConcreteDataType::Float64(_) => DataType::Double(sqlparser::ast::ExactNumberInfo::None),
435        ConcreteDataType::Boolean(_) => DataType::Boolean,
436        _ => unreachable!("JSON2 type hint data type should have been normalized"),
437    };
438    Ok(sql_type)
439}
440
441fn format_json_type_hint(hint: &JsonTypeHint) -> String {
442    let path = hint
443        .path
444        .iter()
445        .map(|segment| format_json_path_segment(segment))
446        .join(".");
447    let inverted_index = if hint.inverted_index {
448        " INVERTED INDEX"
449    } else {
450        ""
451    };
452    format!("{} {}{}", path, hint.data_type, inverted_index)
453}
454
455fn format_json_path_segment(segment: &str) -> String {
456    format!("\"{}\"", segment.replace('"', "\"\""))
457}
458
459/// Partition on columns or values.
460///
461/// - `column_list` is the list of columns in `PARTITION ON COLUMNS` clause.
462/// - `exprs` is the list of expressions in `PARTITION ON VALUES` clause, like
463///   `host <= 'host1'`, `host > 'host1' and host <= 'host2'` or `host > 'host2'`.
464///   Each expression stands for a partition.
465#[derive(Debug, PartialEq, Eq, Clone, Visit, VisitMut, Serialize)]
466pub struct Partitions {
467    pub column_list: Vec<Ident>,
468    pub exprs: Vec<Expr>,
469}
470
471impl Partitions {
472    /// set quotes to all [Ident]s from column list
473    pub fn set_quote(&mut self, quote_style: char) {
474        self.column_list
475            .iter_mut()
476            .for_each(|c| c.quote_style = Some(quote_style));
477    }
478}
479
480#[derive(Debug, PartialEq, Eq, Clone, Visit, VisitMut)]
481pub struct PartitionEntry {
482    pub name: Ident,
483    pub value_list: Vec<SqlValue>,
484}
485
486impl Display for PartitionEntry {
487    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
488        write!(
489            f,
490            "PARTITION {} VALUES LESS THAN ({})",
491            self.name,
492            format_list_comma!(self.value_list),
493        )
494    }
495}
496
497impl Display for Partitions {
498    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
499        if !self.column_list.is_empty() {
500            write!(
501                f,
502                "PARTITION ON COLUMNS ({}) (\n{}\n)",
503                format_list_comma!(self.column_list),
504                format_list_indent!(self.exprs),
505            )?;
506        }
507        Ok(())
508    }
509}
510
511impl Display for CreateTable {
512    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
513        write!(f, "CREATE ")?;
514        if self.engine == FILE_ENGINE {
515            write!(f, "EXTERNAL ")?;
516        }
517        write!(f, "TABLE ")?;
518        if self.if_not_exists {
519            write!(f, "IF NOT EXISTS ")?;
520        }
521        writeln!(f, "{} (", &self.name)?;
522        writeln!(f, "{},", format_list_indent!(self.columns))?;
523        writeln!(f, "{}", format_table_constraint(&self.constraints))?;
524        writeln!(f, ")")?;
525        if let Some(partitions) = &self.partitions {
526            writeln!(f, "{partitions}")?;
527        }
528        writeln!(f, "ENGINE={}", &self.engine)?;
529        if !self.options.is_empty() {
530            let options = self.options.kv_pairs();
531            write!(f, "WITH(\n{}\n)", format_list_indent!(options))?;
532        }
533        Ok(())
534    }
535}
536
537#[derive(Debug, PartialEq, Eq, Clone, Visit, VisitMut, Serialize)]
538pub struct CreateDatabase {
539    pub name: ObjectName,
540    /// Create if not exists
541    pub if_not_exists: bool,
542    pub options: OptionMap,
543}
544
545impl CreateDatabase {
546    /// Creates a statement for `CREATE DATABASE`
547    pub fn new(name: ObjectName, if_not_exists: bool, options: OptionMap) -> Self {
548        Self {
549            name,
550            if_not_exists,
551            options,
552        }
553    }
554}
555
556impl Display for CreateDatabase {
557    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
558        write!(f, "CREATE DATABASE ")?;
559        if self.if_not_exists {
560            write!(f, "IF NOT EXISTS ")?;
561        }
562        write!(f, "{}", &self.name)?;
563        if !self.options.is_empty() {
564            let options = self.options.kv_pairs();
565            write!(f, "\nWITH(\n{}\n)", format_list_indent!(options))?;
566        }
567        Ok(())
568    }
569}
570
571#[derive(Debug, PartialEq, Eq, Clone, Visit, VisitMut, Serialize)]
572pub struct CreateExternalTable {
573    /// Table name
574    pub name: ObjectName,
575    pub columns: Vec<Column>,
576    pub constraints: Vec<TableConstraint>,
577    /// Table options in `WITH`. All keys are lowercase.
578    pub options: OptionMap,
579    pub if_not_exists: bool,
580    pub engine: String,
581}
582
583impl Display for CreateExternalTable {
584    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
585        write!(f, "CREATE EXTERNAL TABLE ")?;
586        if self.if_not_exists {
587            write!(f, "IF NOT EXISTS ")?;
588        }
589        writeln!(f, "{} (", &self.name)?;
590        writeln!(f, "{},", format_list_indent!(self.columns))?;
591        writeln!(f, "{}", format_table_constraint(&self.constraints))?;
592        writeln!(f, ")")?;
593        writeln!(f, "ENGINE={}", &self.engine)?;
594        if !self.options.is_empty() {
595            let options = self.options.kv_pairs();
596            write!(f, "WITH(\n{}\n)", format_list_indent!(options))?;
597        }
598        Ok(())
599    }
600}
601
602#[derive(Debug, PartialEq, Eq, Clone, Visit, VisitMut, Serialize)]
603pub struct CreateTableLike {
604    /// Table name
605    pub table_name: ObjectName,
606    /// The table that is designated to be imitated by `Like`
607    pub source_name: ObjectName,
608}
609
610impl Display for CreateTableLike {
611    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
612        let table_name = &self.table_name;
613        let source_name = &self.source_name;
614        write!(f, r#"CREATE TABLE {table_name} LIKE {source_name}"#)
615    }
616}
617
618#[derive(Debug, PartialEq, Eq, Clone, Visit, VisitMut, Serialize)]
619pub struct CreateFlow {
620    /// Flow name
621    pub flow_name: ObjectName,
622    /// Output (sink) table name
623    pub sink_table_name: ObjectName,
624    /// Whether to replace existing task
625    pub or_replace: bool,
626    /// Create if not exist
627    pub if_not_exists: bool,
628    /// `EXPIRE AFTER`
629    /// Duration in second as `i64`
630    pub expire_after: Option<i64>,
631    /// Duration for flow evaluation interval
632    /// Duration in seconds as `i64`
633    /// If not set, flow will be evaluated based on time window size and other args.
634    pub eval_interval: Option<i64>,
635    /// Phase offset of the flow evaluation schedule within `eval_interval`.
636    /// Duration in seconds as `i64`.
637    /// Must be in range `[0, eval_interval)`. Only legal together with
638    /// `eval_interval`. A value of zero (the default) means the schedule is
639    /// anchored to the Unix epoch, i.e. phases at `k * eval_interval`.
640    pub eval_offset: Option<i64>,
641    /// Comment string
642    pub comment: Option<String>,
643    /// Flow creation options from `WITH (...)`
644    pub flow_options: OptionMap,
645    /// SQL statement
646    pub query: Box<SqlOrTql>,
647}
648
649/// Either a sql query or a tql query
650#[derive(Debug, PartialEq, Eq, Clone, Visit, VisitMut, Serialize)]
651pub enum SqlOrTql {
652    Sql(GtQuery, String),
653    Tql(Tql, String),
654}
655
656impl std::fmt::Display for SqlOrTql {
657    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
658        match self {
659            Self::Sql(_, s) => write!(f, "{}", s),
660            Self::Tql(_, s) => write!(f, "{}", s),
661        }
662    }
663}
664
665impl SqlOrTql {
666    pub fn try_from_statement(
667        value: Statement,
668        original_query: &str,
669    ) -> std::result::Result<Self, crate::error::Error> {
670        match value {
671            Statement::Query(query) => Ok(Self::Sql(*query, original_query.to_string())),
672            Statement::Tql(tql) => Ok(Self::Tql(tql, original_query.to_string())),
673            _ => InvalidFlowQuerySnafu {
674                reason: format!("Expect either sql query or promql query, found {:?}", value),
675            }
676            .fail(),
677        }
678    }
679}
680
681impl Display for CreateFlow {
682    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
683        write!(f, "CREATE ")?;
684        if self.or_replace {
685            write!(f, "OR REPLACE ")?;
686        }
687        write!(f, "FLOW ")?;
688        if self.if_not_exists {
689            write!(f, "IF NOT EXISTS ")?;
690        }
691        writeln!(f, "{}", &self.flow_name)?;
692        writeln!(f, "SINK TO {}", &self.sink_table_name)?;
693        if let Some(expire_after) = &self.expire_after {
694            writeln!(f, "EXPIRE AFTER '{} s'", expire_after)?;
695        }
696        if let Some(eval_interval) = &self.eval_interval {
697            writeln!(f, "EVAL INTERVAL '{} s'", eval_interval)?;
698        }
699        // Canonical display: omit a zero offset (equivalent to the default
700        // epoch-anchored schedule). Non-zero offsets are always emitted.
701        if let Some(eval_offset) = &self.eval_offset
702            && *eval_offset != 0
703        {
704            writeln!(f, "EVAL OFFSET '{} s'", eval_offset)?;
705        }
706        if let Some(comment) = &self.comment {
707            writeln!(f, "COMMENT '{}'", comment)?;
708        }
709        if !self.flow_options.is_empty() {
710            let options = self.flow_options.kv_pairs();
711            writeln!(f, "WITH ({})", format_list_comma!(options))?;
712        }
713        write!(f, "AS {}", &self.query)
714    }
715}
716
717/// Create SQL view statement.
718#[derive(Debug, PartialEq, Eq, Clone, Visit, VisitMut, Serialize)]
719pub struct CreateView {
720    /// View name
721    pub name: ObjectName,
722    /// An optional list of names to be used for columns of the view
723    pub columns: Vec<Ident>,
724    /// The clause after `As` that defines the VIEW.
725    /// Can only be either [Statement::Query] or [Statement::Tql].
726    pub query: Box<Statement>,
727    /// Whether to replace existing VIEW
728    pub or_replace: bool,
729    /// Create VIEW only when it doesn't exists
730    pub if_not_exists: bool,
731}
732
733impl Display for CreateView {
734    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
735        write!(f, "CREATE ")?;
736        if self.or_replace {
737            write!(f, "OR REPLACE ")?;
738        }
739        write!(f, "VIEW ")?;
740        if self.if_not_exists {
741            write!(f, "IF NOT EXISTS ")?;
742        }
743        write!(f, "{} ", &self.name)?;
744        if !self.columns.is_empty() {
745            write!(f, "({}) ", format_list_comma!(self.columns))?;
746        }
747        write!(f, "AS {}", &self.query)
748    }
749}
750
751#[cfg(test)]
752mod tests {
753    use std::assert_matches;
754
755    use datatypes::json::{JsonSettings, JsonTypeHint as DatatypeJsonTypeHint};
756    use datatypes::prelude::ConcreteDataType;
757
758    use super::*;
759    use crate::dialect::GreptimeDbDialect;
760    use crate::error::Error;
761    use crate::parser::{ParseOptions, ParserContext};
762    use crate::statements::statement::Statement;
763
764    #[test]
765    fn test_display_create_table() {
766        let sql = r"create table if not exists demo(
767                             host string,
768                             ts timestamp,
769                             cpu double default 0,
770                             memory double,
771                             TIME INDEX (ts),
772                             PRIMARY KEY(host)
773                       )
774                       PARTITION ON COLUMNS (host) (
775                            host = 'a',
776                            host > 'a',
777                       )
778                       engine=mito
779                       with(ttl='7d', storage='File');
780         ";
781        let result =
782            ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
783                .unwrap();
784        assert_eq!(1, result.len());
785
786        match &result[0] {
787            Statement::CreateTable(c) => {
788                let new_sql = format!("\n{}", c);
789                assert_eq!(
790                    r#"
791CREATE TABLE IF NOT EXISTS demo (
792  host STRING,
793  ts TIMESTAMP,
794  cpu DOUBLE DEFAULT 0,
795  memory DOUBLE,
796  TIME INDEX (ts),
797  PRIMARY KEY (host)
798)
799PARTITION ON COLUMNS (host) (
800  host = 'a',
801  host > 'a'
802)
803ENGINE=mito
804WITH(
805  storage = 'File',
806  ttl = '7d'
807)"#,
808                    &new_sql
809                );
810
811                let new_result = ParserContext::create_with_dialect(
812                    &new_sql,
813                    &GreptimeDbDialect {},
814                    ParseOptions::default(),
815                )
816                .unwrap();
817                assert_eq!(result, new_result);
818            }
819            _ => unreachable!(),
820        }
821    }
822
823    #[test]
824    fn test_display_empty_partition_column() {
825        let sql = r"create table if not exists demo(
826            host string,
827            ts timestamp,
828            cpu double default 0,
829            memory double,
830            TIME INDEX (ts),
831            PRIMARY KEY(ts, host)
832            );
833        ";
834        let result =
835            ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
836                .unwrap();
837        assert_eq!(1, result.len());
838
839        match &result[0] {
840            Statement::CreateTable(c) => {
841                let new_sql = format!("\n{}", c);
842                assert_eq!(
843                    r#"
844CREATE TABLE IF NOT EXISTS demo (
845  host STRING,
846  ts TIMESTAMP,
847  cpu DOUBLE DEFAULT 0,
848  memory DOUBLE,
849  TIME INDEX (ts),
850  PRIMARY KEY (ts, host)
851)
852ENGINE=mito
853"#,
854                    &new_sql
855                );
856
857                let new_result = ParserContext::create_with_dialect(
858                    &new_sql,
859                    &GreptimeDbDialect {},
860                    ParseOptions::default(),
861                )
862                .unwrap();
863                assert_eq!(result, new_result);
864            }
865            _ => unreachable!(),
866        }
867    }
868
869    #[test]
870    fn test_validate_table_options() {
871        let sql = r"create table if not exists demo(
872            host string,
873            ts timestamp,
874            cpu double default 0,
875            memory double,
876            TIME INDEX (ts),
877            PRIMARY KEY(host)
878      )
879      PARTITION ON COLUMNS (host) ()
880      engine=mito
881      with(ttl='7d', 'compaction.type'='world');
882";
883        let result =
884            ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
885                .unwrap();
886        match &result[0] {
887            Statement::CreateTable(c) => {
888                assert_eq!(2, c.options.len());
889            }
890            _ => unreachable!(),
891        }
892
893        let sql = r"create table if not exists demo(
894            host string,
895            ts timestamp,
896            cpu double default 0,
897            memory double,
898            TIME INDEX (ts),
899            PRIMARY KEY(host)
900      )
901      PARTITION ON COLUMNS (host) ()
902      engine=mito
903      with(ttl='7d', hello='world');
904";
905        let result =
906            ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default());
907        assert_matches!(result, Err(Error::InvalidTableOption { .. }));
908
909        // A whitelisted semantic key with an in-domain value is accepted.
910        let semantic = |with: &str| {
911            let sql =
912                format!("create table demo(host string, ts timestamp time index) with({with});");
913            ParserContext::create_with_dialect(&sql, &GreptimeDbDialect {}, ParseOptions::default())
914        };
915        assert!(semantic("'greptime.semantic.signal_type'='metric'").is_ok());
916        // An out-of-domain value is rejected.
917        assert_matches!(
918            semantic("'greptime.semantic.signal_type'='spans'"),
919            Err(Error::InvalidTableOption { .. })
920        );
921        // An unknown key under the semantic prefix is rejected.
922        assert_matches!(
923            semantic("'greptime.semantic.bogus'='x'"),
924            Err(Error::InvalidTableOption { .. })
925        );
926    }
927
928    #[test]
929    fn test_display_json2_type_hints_quotes_path_segments() {
930        let sql = r#"CREATE TABLE traces (
931            log_json_data JSON2 (
932                "service.name" STRING,
933                "a.b"."c" INT64,
934                a."b.c" STRING
935            ),
936            ts TIMESTAMP TIME INDEX
937        )"#;
938        let result =
939            ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
940                .unwrap();
941
942        match &result[0] {
943            Statement::CreateTable(c) => {
944                let new_sql = format!("\n{}", c);
945                assert_eq!(
946                    r#"
947CREATE TABLE traces (
948  log_json_data JSON2(
949    "service.name" STRING,
950    "a.b"."c" BIGINT,
951    "a"."b.c" STRING
952  ),
953  ts TIMESTAMP NOT NULL,
954  TIME INDEX (ts)
955)
956ENGINE=mito
957"#,
958                    &new_sql
959                );
960
961                let new_result = ParserContext::create_with_dialect(
962                    &new_sql,
963                    &GreptimeDbDialect {},
964                    ParseOptions::default(),
965                )
966                .unwrap();
967                assert_eq!(result, new_result);
968            }
969            _ => unreachable!(),
970        }
971    }
972
973    #[test]
974    fn test_parse_json2_max_auto_expanded_paths_option() -> Result<()> {
975        let sql = r#"CREATE TABLE traces (
976            log_json_data JSON2 (
977                status_code INT64,
978                max_auto_expanded_paths = 1
979            ),
980            ts TIMESTAMP TIME INDEX
981        )"#;
982        let result = ParserContext::create_with_dialect(
983            sql,
984            &GreptimeDbDialect {},
985            ParseOptions::default(),
986        )?;
987        let Statement::CreateTable(create_table) = &result[0] else {
988            unreachable!()
989        };
990        let settings = create_table.columns[0]
991            .extensions
992            .build_json_settings()?
993            .unwrap();
994        assert_eq!(settings.max_auto_expanded_paths(), Some(1));
995        Ok(())
996    }
997
998    #[test]
999    fn test_display_json2_type_hints_quotes_numeric_segments() {
1000        let sql = r#"CREATE TABLE traces (
1001            log_json_data JSON2 (
1002                "1abc" STRING,
1003                a."2b" INT64
1004            ),
1005            ts TIMESTAMP TIME INDEX
1006        )"#;
1007        let result =
1008            ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
1009                .unwrap();
1010
1011        match &result[0] {
1012            Statement::CreateTable(c) => {
1013                let new_sql = format!("\n{}", c);
1014                assert_eq!(
1015                    r#"
1016CREATE TABLE traces (
1017  log_json_data JSON2(
1018    "1abc" STRING,
1019    "a"."2b" BIGINT
1020  ),
1021  ts TIMESTAMP NOT NULL,
1022  TIME INDEX (ts)
1023)
1024ENGINE=mito
1025"#,
1026                    &new_sql
1027                );
1028
1029                let new_result = ParserContext::create_with_dialect(
1030                    &new_sql,
1031                    &GreptimeDbDialect {},
1032                    ParseOptions::default(),
1033                )
1034                .unwrap();
1035                assert_eq!(result, new_result);
1036            }
1037            _ => unreachable!(),
1038        }
1039    }
1040
1041    #[test]
1042    fn test_json2_type_hint_rejects_default() {
1043        let sql = r#"CREATE TABLE traces (
1044            log_json_data JSON2 (
1045                status_code INT64 DEFAULT -5,
1046                duration FLOAT64 DEFAULT +1.5,
1047                error BOOLEAN DEFAULT false,
1048                message STRING DEFAULT 'unknown'
1049            ),
1050            ts TIMESTAMP TIME INDEX
1051        )"#;
1052        let err =
1053            ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
1054                .unwrap_err();
1055        assert!(err.to_string().contains("DEFAULT is not supported"));
1056    }
1057
1058    #[test]
1059    fn test_json2_type_hint_rejects_not_null() {
1060        let sql = r#"CREATE TABLE traces (
1061            log_json_data JSON2 (
1062                status_code INT64 NOT NULL DEFAULT NULL
1063            ),
1064            ts TIMESTAMP TIME INDEX
1065        )"#;
1066        let err =
1067            ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
1068                .unwrap_err();
1069        assert!(err.to_string().contains("NULL/NOT NULL is not supported"));
1070    }
1071
1072    #[test]
1073    fn test_set_json_settings_normalizes_type_hint_sql_types() -> Result<()> {
1074        let mut extensions = super::ColumnExtensions::default();
1075        let settings = JsonSettings::try_new(
1076            vec![
1077                DatatypeJsonTypeHint {
1078                    path: vec!["i".to_string()],
1079                    data_type: ConcreteDataType::int32_datatype(),
1080                    inverted_index: false,
1081                },
1082                DatatypeJsonTypeHint {
1083                    path: vec!["f".to_string()],
1084                    data_type: ConcreteDataType::float32_datatype(),
1085                    inverted_index: false,
1086                },
1087                DatatypeJsonTypeHint {
1088                    path: vec!["u".to_string()],
1089                    data_type: ConcreteDataType::uint32_datatype(),
1090                    inverted_index: false,
1091                },
1092                DatatypeJsonTypeHint {
1093                    path: vec!["s".to_string()],
1094                    data_type: ConcreteDataType::string_datatype(),
1095                    inverted_index: false,
1096                },
1097                DatatypeJsonTypeHint {
1098                    path: vec!["b".to_string()],
1099                    data_type: ConcreteDataType::boolean_datatype(),
1100                    inverted_index: false,
1101                },
1102            ],
1103            None,
1104        )?;
1105        extensions.set_json_settings(settings)?;
1106
1107        assert_eq!(
1108            extensions
1109                .json2_options
1110                .unwrap()
1111                .type_hints
1112                .iter()
1113                .map(|hint| hint.data_type.to_string())
1114                .collect::<Vec<_>>(),
1115            vec!["BIGINT", "DOUBLE", "BIGINT UNSIGNED", "STRING", "BOOLEAN"]
1116        );
1117        Ok(())
1118    }
1119
1120    #[test]
1121    fn test_set_json_settings_rejects_unsupported_type_hint_type() -> Result<()> {
1122        let err = JsonSettings::try_new(
1123            vec![DatatypeJsonTypeHint {
1124                path: vec!["u".to_string()],
1125                data_type: ConcreteDataType::date_datatype(),
1126                inverted_index: false,
1127            }],
1128            None,
1129        )
1130        .unwrap_err();
1131
1132        assert!(
1133            err.to_string()
1134                .contains("unsupported JSON2 type hint data type")
1135        );
1136        Ok(())
1137    }
1138
1139    #[test]
1140    fn test_set_empty_json_settings_omits_json2_options() -> Result<()> {
1141        let mut extensions = ColumnExtensions::default();
1142        extensions.set_json_settings(JsonSettings::default())?;
1143        assert!(extensions.json2_options.is_none());
1144        Ok(())
1145    }
1146
1147    #[test]
1148    fn test_display_create_database() {
1149        let sql = r"create database test;";
1150        let stmts =
1151            ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
1152                .unwrap();
1153        assert_eq!(1, stmts.len());
1154        assert_matches!(&stmts[0], Statement::CreateDatabase { .. });
1155
1156        match &stmts[0] {
1157            Statement::CreateDatabase(set) => {
1158                let new_sql = format!("\n{}", set);
1159                assert_eq!(
1160                    r#"
1161CREATE DATABASE test"#,
1162                    &new_sql
1163                );
1164            }
1165            _ => {
1166                unreachable!();
1167            }
1168        }
1169
1170        let sql = r"create database if not exists test;";
1171        let stmts =
1172            ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
1173                .unwrap();
1174        assert_eq!(1, stmts.len());
1175        assert_matches!(&stmts[0], Statement::CreateDatabase { .. });
1176
1177        match &stmts[0] {
1178            Statement::CreateDatabase(set) => {
1179                let new_sql = format!("\n{}", set);
1180                assert_eq!(
1181                    r#"
1182CREATE DATABASE IF NOT EXISTS test"#,
1183                    &new_sql
1184                );
1185            }
1186            _ => {
1187                unreachable!();
1188            }
1189        }
1190
1191        let sql = r#"CREATE DATABASE IF NOT EXISTS test WITH (ttl='1h');"#;
1192        let stmts =
1193            ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
1194                .unwrap();
1195        assert_eq!(1, stmts.len());
1196        assert_matches!(&stmts[0], Statement::CreateDatabase { .. });
1197
1198        match &stmts[0] {
1199            Statement::CreateDatabase(set) => {
1200                let new_sql = format!("\n{}", set);
1201                assert_eq!(
1202                    r#"
1203CREATE DATABASE IF NOT EXISTS test
1204WITH(
1205  ttl = '1h'
1206)"#,
1207                    &new_sql
1208                );
1209            }
1210            _ => {
1211                unreachable!();
1212            }
1213        }
1214    }
1215
1216    #[test]
1217    fn test_display_create_table_like() {
1218        let sql = r"create table t2 like t1;";
1219        let stmts =
1220            ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
1221                .unwrap();
1222        assert_eq!(1, stmts.len());
1223        assert_matches!(&stmts[0], Statement::CreateTableLike { .. });
1224
1225        match &stmts[0] {
1226            Statement::CreateTableLike(create) => {
1227                let new_sql = format!("\n{}", create);
1228                assert_eq!(
1229                    r#"
1230CREATE TABLE t2 LIKE t1"#,
1231                    &new_sql
1232                );
1233            }
1234            _ => {
1235                unreachable!();
1236            }
1237        }
1238    }
1239
1240    #[test]
1241    fn test_display_create_external_table() {
1242        let sql = r#"CREATE EXTERNAL TABLE city (
1243            host string,
1244            ts timestamp,
1245            cpu float64 default 0,
1246            memory float64,
1247            TIME INDEX (ts),
1248            PRIMARY KEY(host)
1249) WITH (location='/var/data/city.csv', format='csv');"#;
1250        let stmts =
1251            ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
1252                .unwrap();
1253        assert_eq!(1, stmts.len());
1254        assert_matches!(&stmts[0], Statement::CreateExternalTable { .. });
1255
1256        match &stmts[0] {
1257            Statement::CreateExternalTable(create) => {
1258                let new_sql = format!("\n{}", create);
1259                assert_eq!(
1260                    r#"
1261CREATE EXTERNAL TABLE city (
1262  host STRING,
1263  ts TIMESTAMP,
1264  cpu DOUBLE DEFAULT 0,
1265  memory DOUBLE,
1266  TIME INDEX (ts),
1267  PRIMARY KEY (host)
1268)
1269ENGINE=file
1270WITH(
1271  format = 'csv',
1272  location = '/var/data/city.csv'
1273)"#,
1274                    &new_sql
1275                );
1276            }
1277            _ => {
1278                unreachable!();
1279            }
1280        }
1281    }
1282
1283    #[test]
1284    fn test_display_create_flow() {
1285        let sql = r"CREATE FLOW filter_numbers
1286            SINK TO out_num_cnt
1287            AS SELECT number FROM numbers_input where number > 10;";
1288        let result =
1289            ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
1290                .unwrap();
1291        assert_eq!(1, result.len());
1292
1293        match &result[0] {
1294            Statement::CreateFlow(c) => {
1295                let new_sql = format!("\n{}", c);
1296                assert_eq!(
1297                    r#"
1298CREATE FLOW filter_numbers
1299SINK TO out_num_cnt
1300AS SELECT number FROM numbers_input where number > 10"#,
1301                    &new_sql
1302                );
1303
1304                let new_result = ParserContext::create_with_dialect(
1305                    &new_sql,
1306                    &GreptimeDbDialect {},
1307                    ParseOptions::default(),
1308                )
1309                .unwrap();
1310                assert_eq!(result, new_result);
1311            }
1312            _ => unreachable!(),
1313        }
1314    }
1315
1316    #[test]
1317    fn test_vector_index_options_validation() {
1318        use super::{ColumnExtensions, OptionMap};
1319
1320        // Test zero connectivity should fail
1321        let extensions = ColumnExtensions {
1322            vector_index_options: Some(OptionMap::from([(
1323                "connectivity".to_string(),
1324                "0".to_string(),
1325            )])),
1326            ..Default::default()
1327        };
1328        let result = extensions.build_vector_index_options();
1329        assert!(result.is_err());
1330        assert!(
1331            result
1332                .unwrap_err()
1333                .to_string()
1334                .contains("connectivity must be in the range [2, 2048]")
1335        );
1336
1337        // Test zero expansion_add should fail
1338        let extensions = ColumnExtensions {
1339            vector_index_options: Some(OptionMap::from([(
1340                "expansion_add".to_string(),
1341                "0".to_string(),
1342            )])),
1343            ..Default::default()
1344        };
1345        let result = extensions.build_vector_index_options();
1346        assert!(result.is_err());
1347        assert!(
1348            result
1349                .unwrap_err()
1350                .to_string()
1351                .contains("expansion_add must be greater than 0")
1352        );
1353
1354        // Test zero expansion_search should fail
1355        let extensions = ColumnExtensions {
1356            vector_index_options: Some(OptionMap::from([(
1357                "expansion_search".to_string(),
1358                "0".to_string(),
1359            )])),
1360            ..Default::default()
1361        };
1362        let result = extensions.build_vector_index_options();
1363        assert!(result.is_err());
1364        assert!(
1365            result
1366                .unwrap_err()
1367                .to_string()
1368                .contains("expansion_search must be greater than 0")
1369        );
1370
1371        // Test valid values should succeed
1372        let extensions = ColumnExtensions {
1373            vector_index_options: Some(OptionMap::from([
1374                ("connectivity".to_string(), "32".to_string()),
1375                ("expansion_add".to_string(), "200".to_string()),
1376                ("expansion_search".to_string(), "100".to_string()),
1377            ])),
1378            ..Default::default()
1379        };
1380        let result = extensions.build_vector_index_options();
1381        assert!(result.is_ok());
1382        let options = result.unwrap().unwrap();
1383        assert_eq!(options.connectivity, 32);
1384        assert_eq!(options.expansion_add, 200);
1385        assert_eq!(options.expansion_search, 100);
1386    }
1387}