Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ $ cargo run --example cli - [--dialectname]
"--postgres" => Box::new(PostgreSqlDialect {}),
"--ms" => Box::new(MsSqlDialect {}),
"--mysql" => Box::new(MySqlDialect {}),
"--doris" => Box::new(DorisDialect {}),
"--snowflake" => Box::new(SnowflakeDialect {}),
"--hive" => Box::new(HiveDialect {}),
"--redshift" => Box::new(RedshiftSqlDialect {}),
Expand Down
13 changes: 13 additions & 0 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2001,6 +2001,12 @@ pub enum ColumnOption {
/// ```
/// [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/invisible-columns.html
Invisible,
/// Column-level auto-increment option with an optional start value.
///
/// MySQL and Generic use this unified AST node without a start value.
/// Dialects that support a start value can use `Some`.
/// Syntax: `AUTO_INCREMENT` or `AUTO_INCREMENT(<start_value>)`.
AutoIncrement(Option<u64>),
}

impl From<UniqueConstraint> for ColumnOption {
Expand Down Expand Up @@ -2149,6 +2155,13 @@ impl fmt::Display for ColumnOption {
Invisible => {
write!(f, "INVISIBLE")
}
AutoIncrement(start) => {
f.write_str("AUTO_INCREMENT")?;
if let Some(start) = start {
write!(f, "({start})")?;
}
Ok(())
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,7 @@ impl Spanned for RaiseStatementValue {
/// - [ColumnOption::PrimaryKey]
/// - [ColumnOption::Unique]
/// - [ColumnOption::DialectSpecific]
/// - [ColumnOption::AutoIncrement]
/// - [ColumnOption::Generated]
impl Spanned for ColumnOption {
fn span(&self) -> Span {
Expand Down Expand Up @@ -849,6 +850,7 @@ impl Spanned for ColumnOption {
ColumnOption::Tags(..) => Span::empty(),
ColumnOption::Srid(..) => Span::empty(),
ColumnOption::Invisible => Span::empty(),
ColumnOption::AutoIncrement(_) => Span::empty(),
}
}
}
Expand Down
61 changes: 61 additions & 0 deletions src/dialect/doris.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

use crate::dialect::Dialect;

/// A [`Dialect`] for [Apache Doris](https://doris.apache.org/).
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DorisDialect {}

impl Dialect for DorisDialect {
fn is_delimited_identifier_start(&self, ch: char) -> bool {
ch == '`'
}

fn identifier_quote_style(&self, _identifier: &str) -> Option<char> {
Some('`')
}

fn is_identifier_start(&self, ch: char) -> bool {
ch.is_ascii_alphabetic() || ch == '_' || !ch.is_ascii()
}

fn is_identifier_part(&self, ch: char) -> bool {
self.is_identifier_start(ch) || ch.is_ascii_digit()
}

fn supports_string_literal_backslash_escape(&self) -> bool {
true
}

fn ignores_wildcard_escapes(&self) -> bool {
true
}

fn supports_numeric_prefix(&self) -> bool {
true
}

fn supports_parenthesized_auto_increment_column_option(&self) -> bool {
true
}

fn supports_column_aggregation_function_option(&self) -> bool {
true
}
}
4 changes: 4 additions & 0 deletions src/dialect/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ impl Dialect for GenericDialect {
true
}

fn supports_column_aggregation_function_option(&self) -> bool {
true
}

fn supports_named_fn_args_with_assignment_operator(&self) -> bool {
true
}
Expand Down
17 changes: 17 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod ansi;
mod bigquery;
mod clickhouse;
mod databricks;
mod doris;
mod duckdb;
mod generic;
mod hive;
Expand All @@ -43,6 +44,7 @@ pub use self::ansi::AnsiDialect;
pub use self::bigquery::BigQueryDialect;
pub use self::clickhouse::ClickHouseDialect;
pub use self::databricks::DatabricksDialect;
pub use self::doris::DorisDialect;
pub use self::duckdb::DuckDbDialect;
pub use self::generic::GenericDialect;
pub use self::hive::HiveDialect;
Expand Down Expand Up @@ -1226,6 +1228,18 @@ pub trait Dialect: Debug + Any {
false
}

/// Returns true if the dialect supports `AUTO_INCREMENT` with an optional
/// parenthesized start value in column definitions.
fn supports_parenthesized_auto_increment_column_option(&self) -> bool {
false
}

/// Returns true if the dialect supports aggregate column option functions in
/// `CREATE TABLE`, such as `SUM`, `REPLACE`, or `BITMAP_UNION`.
fn supports_column_aggregation_function_option(&self) -> bool {
false
}

/// Returns true if the dialect accepts a comma-separated list of table-level
/// options placed between the table name and the column-list parenthesis, e.g.
///
Expand Down Expand Up @@ -1883,6 +1897,7 @@ pub fn dialect_from_str(dialect_name: impl AsRef<str>) -> Option<Box<dyn Dialect
"bigquery" => Some(Box::new(BigQueryDialect)),
"ansi" => Some(Box::new(AnsiDialect {})),
"duckdb" => Some(Box::new(DuckDbDialect {})),
"doris" => Some(Box::new(DorisDialect {})),
"databricks" => Some(Box::new(DatabricksDialect {})),
"spark" | "sparksql" => Some(Box::new(SparkSqlDialect {})),
"oracle" => Some(Box::new(OracleDialect {})),
Expand Down Expand Up @@ -1938,6 +1953,8 @@ mod tests {
assert!(parse_dialect("ANSI").is::<AnsiDialect>());
assert!(parse_dialect("duckdb").is::<DuckDbDialect>());
assert!(parse_dialect("DuckDb").is::<DuckDbDialect>());
assert!(parse_dialect("doris").is::<DorisDialect>());
assert!(parse_dialect("Doris").is::<DorisDialect>());
assert!(parse_dialect("DataBricks").is::<DatabricksDialect>());
assert!(parse_dialect("databricks").is::<DatabricksDialect>());
assert!(parse_dialect("teradata").is::<TeradataDialect>());
Expand Down
4 changes: 4 additions & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ define_keywords!(
BIND,
BINDING,
BIT,
BITMAP_UNION,
BLANKSASNULL,
BLOB,
BLOCK,
Expand Down Expand Up @@ -489,6 +490,7 @@ define_keywords!(
HIGH_PRIORITY,
HISTORY,
HIVEVAR,
HLL_UNION,
HOLD,
HOSTS,
HOUR,
Expand Down Expand Up @@ -573,6 +575,7 @@ define_keywords!(
LAMBDA,
LANGUAGE,
LARGE,
LARGEINT,
LAST,
LAST_VALUE,
LATERAL,
Expand Down Expand Up @@ -828,6 +831,7 @@ define_keywords!(
PURGE,
PUT,
QUALIFY,
QUANTILE_UNION,
QUARTER,
QUERIES,
QUERY,
Expand Down
60 changes: 52 additions & 8 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9653,13 +9653,25 @@ impl<'a> Parser<'a> {
}
.into(),
))
} else if self.parse_keyword(Keyword::AUTO_INCREMENT)
&& dialect_of!(self is MySqlDialect | GenericDialect)
{
// Support AUTO_INCREMENT for MySQL
Ok(Some(ColumnOption::DialectSpecific(vec![
Token::make_keyword("AUTO_INCREMENT"),
])))
} else if self.parse_keyword(Keyword::AUTO_INCREMENT) {
if self
.dialect
.supports_parenthesized_auto_increment_column_option()
{
let start = if self.consume_token(&Token::LParen) {
let value = self.parse_literal_uint()?;
self.expect_token(&Token::RParen)?;
Some(value)
} else {
None
};
Ok(Some(ColumnOption::AutoIncrement(start)))
} else if dialect_of!(self is MySqlDialect | GenericDialect) {
Ok(Some(ColumnOption::AutoIncrement(None)))
} else {
self.prev_token();
Ok(None)
}
} else if self.parse_keyword(Keyword::AUTOINCREMENT)
&& dialect_of!(self is SQLiteDialect | GenericDialect)
{
Expand Down Expand Up @@ -9740,8 +9752,40 @@ impl<'a> Parser<'a> {
} else if self.parse_keyword(Keyword::INVISIBLE) {
Ok(Some(ColumnOption::Invisible))
} else {
Ok(None)
self.parse_optional_doris_aggregate_column_option()
}
}

fn parse_optional_doris_aggregate_column_option(
&mut self,
) -> Result<Option<ColumnOption>, ParserError> {
if !self.dialect.supports_column_aggregation_function_option() {
return Ok(None);
}

let token = self.peek_token();
let option_name = match token.token {
Token::Word(word)
if matches!(
word.keyword,
Keyword::SUM
| Keyword::MAX
| Keyword::MIN
| Keyword::REPLACE
| Keyword::HLL_UNION
| Keyword::BITMAP_UNION
| Keyword::QUANTILE_UNION
) =>
{
word.value
}
_ => return Ok(None),
};

self.next_token();
Ok(Some(ColumnOption::DialectSpecific(vec![
Token::make_keyword(&option_name),
])))
}

pub(crate) fn parse_tag(&mut self) -> Result<Tag, ParserError> {
Expand Down
1 change: 1 addition & 0 deletions src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ pub fn all_dialects() -> TestedDialects {
Box::new(HiveDialect {}),
Box::new(RedshiftSqlDialect {}),
Box::new(MySqlDialect {}),
Box::new(DorisDialect {}),
Box::new(BigQueryDialect {}),
Box::new(SQLiteDialect {}),
Box::new(DuckDbDialect {}),
Expand Down
Loading
Loading