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
5 changes: 5 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mod redshift;
mod snowflake;
mod spark;
mod sqlite;
mod teradata;

use core::any::{Any, TypeId};
use core::fmt::Debug;
Expand All @@ -54,6 +55,7 @@ pub use self::snowflake::parse_snowflake_stage_name;
pub use self::snowflake::SnowflakeDialect;
pub use self::spark::SparkSqlDialect;
pub use self::sqlite::SQLiteDialect;
pub use self::teradata::TeradataDialect;

/// Macro for streamlining the creation of derived `Dialect` objects.
/// The generated struct includes `new()` and `default()` constructors.
Expand Down Expand Up @@ -1841,6 +1843,7 @@ pub fn dialect_from_str(dialect_name: impl AsRef<str>) -> Option<Box<dyn Dialect
"databricks" => Some(Box::new(DatabricksDialect {})),
"spark" | "sparksql" => Some(Box::new(SparkSqlDialect {})),
"oracle" => Some(Box::new(OracleDialect {})),
"teradata" => Some(Box::new(TeradataDialect {})),
_ => None,
}
}
Expand Down Expand Up @@ -1894,6 +1897,8 @@ mod tests {
assert!(parse_dialect("DuckDb").is::<DuckDbDialect>());
assert!(parse_dialect("DataBricks").is::<DatabricksDialect>());
assert!(parse_dialect("databricks").is::<DatabricksDialect>());
assert!(parse_dialect("teradata").is::<TeradataDialect>());
assert!(parse_dialect("Teradata").is::<TeradataDialect>());

// error cases
assert!(dialect_from_str("Unknown").is_none());
Expand Down
92 changes: 92 additions & 0 deletions src/dialect/teradata.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// 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 [Teradata](https://docs.teradata.com/).
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TeradataDialect;

impl Dialect for TeradataDialect {
/// See <https://docs.teradata.com/r/Enterprise_IntelliFlex_VMware/SQL-Fundamentals/Basic-SQL-Syntax/Object-Names>
fn identifier_quote_style(&self, _identifier: &str) -> Option<char> {
Some('"')
}

/// See <https://docs.teradata.com/r/Enterprise_IntelliFlex_VMware/SQL-Fundamentals/Basic-SQL-Syntax/Working-with-Unicode-Delimited-Identifiers>
fn is_delimited_identifier_start(&self, ch: char) -> bool {
ch == '"'
}

/// See <https://docs.teradata.com/r/Enterprise_IntelliFlex_VMware/International-Character-Set-Support/Managing-International-Language-Support/Object-Names/Rules-for-Object-Naming>
fn is_identifier_start(&self, ch: char) -> bool {
ch.is_alphabetic() || ch == '_' || ch == '#' || ch == '$'
}

// See <https://docs.teradata.com/r/Enterprise_IntelliFlex_VMware/SQL-Fundamentals/Basic-SQL-Syntax/Object-Names>
fn is_identifier_part(&self, ch: char) -> bool {
ch.is_alphanumeric() || self.is_identifier_start(ch)
}

/// See <https://docs.teradata.com/r/Enterprise_IntelliFlex_VMware/SQL-Data-Manipulation-Language/SELECT-Statements/GROUP-BY-Clause/GROUP-BY-Clause-Syntax>
fn supports_group_by_expr(&self) -> bool {
true
}

/// Teradata has no native `BOOLEAN` data type.
///
/// See <https://docs.teradata.com/r/Enterprise_IntelliFlex_VMware/SQL-Data-Types-and-Literals>
fn supports_boolean_literals(&self) -> bool {
false
}

/// See <https://docs.teradata.com/r/Enterprise_IntelliFlex_VMware/SQL-Data-Types-and-Literals/Data-Literals/Interval-Literals>
fn require_interval_qualifier(&self) -> bool {
true
}

/// See <https://docs.teradata.com/r/Enterprise_IntelliFlex_VMware/SQL-Data-Definition-Language-Syntax-and-Examples/Comment-Help-and-Show-Statements/COMMENT-Comment-Placing-Form>
fn supports_comment_on(&self) -> bool {
true
}

/// See <https://docs.teradata.com/r/Enterprise_IntelliFlex_VMware/SQL-Data-Definition-Language-Syntax-and-Examples/Table-Statements/CREATE-TABLE-and-CREATE-TABLE-AS>
fn supports_create_table_select(&self) -> bool {
true
}

/// See <https://docs.teradata.com/r/Enterprise_IntelliFlex_VMware/SQL-Stored-Procedures-and-Embedded-SQL/Dynamic-Embedded-SQL-Statements/Dynamic-SQL-Statement-Syntax/EXECUTE-IMMEDIATE>
fn supports_execute_immediate(&self) -> bool {
true
}

/// See <https://docs.teradata.com/r/Enterprise_IntelliFlex_VMware/SQL-Data-Manipulation-Language/SELECT-Statements/Select-List-Syntax/TOP-Clause>
fn supports_top_before_distinct(&self) -> bool {
true
}

/// See <https://docs.teradata.com/r/Enterprise_IntelliFlex_VMware/SQL-Functions-Expressions-and-Predicates/Ordered-Analytical/Window-Aggregate-Functions>
fn supports_window_function_null_treatment_arg(&self) -> bool {
true
}

/// See <https://docs.teradata.com/r/Enterprise_IntelliFlex_VMware/SQL-Data-Types-and-Literals/Data-Literals/Character-String-Literals>
fn supports_string_literal_concatenation(&self) -> bool {
true
}
}
1 change: 1 addition & 0 deletions src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ pub fn all_dialects() -> TestedDialects {
Box::new(DatabricksDialect {}),
Box::new(ClickHouseDialect {}),
Box::new(OracleDialect {}),
Box::new(TeradataDialect {}),
])
}

Expand Down
2 changes: 1 addition & 1 deletion tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18180,7 +18180,7 @@ fn test_parse_semantic_view_table_factor() {

#[test]
fn parse_adjacent_string_literal_concatenation() {
let sql = r#"SELECT 'M' "y" 'S' "q" 'l'"#;
let sql = r#"SELECT 'M' 'y' 'S' 'q' 'l'"#;
let dialects = all_dialects_where(|d| d.supports_string_literal_concatenation());
dialects.one_statement_parses_to(sql, r"SELECT 'MySql'");

Expand Down
6 changes: 6 additions & 0 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4891,3 +4891,9 @@ fn parse_create_database_with_charset_option_ordering() {
"CREATE DATABASE mydb DEFAULT CHARACTER SET utf8mb4 DEFAULT COLLATE utf8mb4_unicode_ci",
);
}

#[test]
fn parse_adjacent_string_literal_concatenation() {
let sql = r#"SELECT 'M' "y" 'S' "q" 'l'"#;
mysql().one_statement_parses_to(sql, r"SELECT 'MySql'");
}
63 changes: 63 additions & 0 deletions tests/sqlparser_teradata.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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.

//! Test SQL syntax, specific to [sqlparser::dialect::TeradataDialect].

use sqlparser::dialect::{Dialect, TeradataDialect};
use test_utils::TestedDialects;

mod test_utils;

fn teradata() -> TestedDialects {
TestedDialects::new(vec![Box::new(TeradataDialect)])
}

#[test]
fn dialect_methods() {
let d: &dyn Dialect = &TeradataDialect;
assert_eq!(d.identifier_quote_style("x"), Some('"'));
assert!(d.is_delimited_identifier_start('"'));
assert!(!d.is_delimited_identifier_start('`'));
assert!(d.is_identifier_start('$'));
assert!(d.is_identifier_start('#'));
assert!(d.is_identifier_part('$'));
assert!(d.is_identifier_part('#'));
assert!(d.supports_group_by_expr());
assert!(!d.supports_boolean_literals());
assert!(d.require_interval_qualifier());
assert!(d.supports_comment_on());
assert!(d.supports_create_table_select());
assert!(d.supports_execute_immediate());
assert!(d.supports_top_before_distinct());
assert!(d.supports_window_function_null_treatment_arg());
assert!(d.supports_string_literal_concatenation());
}

#[test]
fn parse_identifier() {
teradata().verified_stmt(concat!(
"SELECT ",
"NULL AS foo, ",
"NULL AS _bar, ",
"NULL AS #baz, ",
"NULL AS $qux, ",
"NULL AS a$1, ",
"NULL AS a#1, ",
"NULL AS a_1, ",
r#"NULL AS "quoted id""#
));
}
Loading