Skip to main content

risingwave_sqlparser/ast/
legacy_source.rs

1// Copyright 2025 RisingWave Labs
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
15//! Content of this file can be deleted once we stop supporting `create source` syntax v1.
16//! New features shall NOT touch this file.
17
18use std::fmt;
19
20use winnow::ModalResult;
21
22use crate::ast::FormatEncodeOptions;
23use crate::parser::{Parser, StrError};
24use crate::parser_err;
25
26#[derive(Debug, Clone, PartialEq, Eq, Hash)]
27pub enum CompatibleFormatEncode {
28    V2(FormatEncodeOptions),
29}
30
31impl fmt::Display for CompatibleFormatEncode {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        match self {
34            CompatibleFormatEncode::V2(inner) => {
35                write!(f, "{}", inner)
36            }
37        }
38    }
39}
40
41impl CompatibleFormatEncode {
42    pub(crate) fn into_v2(self) -> FormatEncodeOptions {
43        match self {
44            CompatibleFormatEncode::V2(inner) => inner,
45        }
46    }
47}
48
49impl From<FormatEncodeOptions> for CompatibleFormatEncode {
50    fn from(value: FormatEncodeOptions) -> Self {
51        Self::V2(value)
52    }
53}
54
55pub fn parse_format_encode(p: &mut Parser<'_>) -> ModalResult<CompatibleFormatEncode> {
56    if let Some(schema_v2) = p.parse_schema()? {
57        if schema_v2.key_encode.is_some() {
58            parser_err!("key encode clause is not supported in source schema");
59        }
60        Ok(CompatibleFormatEncode::V2(schema_v2))
61    } else {
62        p.expected("description of the format")
63    }
64}
65
66pub fn get_delimiter(chars: &str) -> Result<u8, StrError> {
67    match chars {
68        "," => Ok(b','),   // comma
69        ";" => Ok(b';'),   // semicolon
70        "\t" => Ok(b'\t'), // tab
71        other => Err(StrError(format!(
72            "The delimiter should be one of ',', ';', E'\\t', but got {other:?}",
73        ))),
74    }
75}