This is an automated email from the ASF dual-hosted git repository.
alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion-sqlparser-rs.git
The following commit(s) were added to refs/heads/main by this push:
new ac956dc9 Add support for ASC and DESC in CREATE TABLE column
constraints for SQLite. (#1462)
ac956dc9 is described below
commit ac956dc963b9a857202b62b50c99f35313efbce2
Author: David Caldwell <[email protected]>
AuthorDate: Tue Oct 8 09:26:32 2024 -0700
Add support for ASC and DESC in CREATE TABLE column constraints for SQLite.
(#1462)
---
src/dialect/generic.rs | 4 ++++
src/dialect/mod.rs | 4 ++++
src/dialect/sqlite.rs | 4 ++++
src/parser/mod.rs | 14 ++++++++++++++
tests/sqlparser_sqlite.rs | 37 +++++++++++++++++++++++++++++++++++++
5 files changed, 63 insertions(+)
diff --git a/src/dialect/generic.rs b/src/dialect/generic.rs
index 8bec45b2..92d720a0 100644
--- a/src/dialect/generic.rs
+++ b/src/dialect/generic.rs
@@ -103,4 +103,8 @@ impl Dialect for GenericDialect {
fn supports_limit_comma(&self) -> bool {
true
}
+
+ fn supports_asc_desc_in_column_definition(&self) -> bool {
+ true
+ }
}
diff --git a/src/dialect/mod.rs b/src/dialect/mod.rs
index 29ad0b98..744f5a8c 100644
--- a/src/dialect/mod.rs
+++ b/src/dialect/mod.rs
@@ -557,6 +557,10 @@ pub trait Dialect: Debug + Any {
fn supports_explain_with_utility_options(&self) -> bool {
false
}
+
+ fn supports_asc_desc_in_column_definition(&self) -> bool {
+ false
+ }
}
/// This represents the operators for which precedence must be defined
diff --git a/src/dialect/sqlite.rs b/src/dialect/sqlite.rs
index 5c563bf4..95717f9f 100644
--- a/src/dialect/sqlite.rs
+++ b/src/dialect/sqlite.rs
@@ -77,4 +77,8 @@ impl Dialect for SQLiteDialect {
fn supports_limit_comma(&self) -> bool {
true
}
+
+ fn supports_asc_desc_in_column_definition(&self) -> bool {
+ true
+ }
}
diff --git a/src/parser/mod.rs b/src/parser/mod.rs
index 39173b63..34574a6b 100644
--- a/src/parser/mod.rs
+++ b/src/parser/mod.rs
@@ -6192,6 +6192,20 @@ impl<'a> Parser<'a> {
Ok(Some(ColumnOption::DialectSpecific(vec![
Token::make_keyword("AUTOINCREMENT"),
])))
+ } else if self.parse_keyword(Keyword::ASC)
+ && self.dialect.supports_asc_desc_in_column_definition()
+ {
+ // Support ASC for SQLite
+ Ok(Some(ColumnOption::DialectSpecific(vec![
+ Token::make_keyword("ASC"),
+ ])))
+ } else if self.parse_keyword(Keyword::DESC)
+ && self.dialect.supports_asc_desc_in_column_definition()
+ {
+ // Support DESC for SQLite
+ Ok(Some(ColumnOption::DialectSpecific(vec![
+ Token::make_keyword("DESC"),
+ ])))
} else if self.parse_keywords(&[Keyword::ON, Keyword::UPDATE])
&& dialect_of!(self is MySqlDialect | GenericDialect)
{
diff --git a/tests/sqlparser_sqlite.rs b/tests/sqlparser_sqlite.rs
index b5427fb3..d3e670e3 100644
--- a/tests/sqlparser_sqlite.rs
+++ b/tests/sqlparser_sqlite.rs
@@ -238,6 +238,43 @@ fn parse_create_table_auto_increment() {
}
}
+#[test]
+fn parse_create_table_primary_key_asc_desc() {
+ let expected_column_def = |kind| ColumnDef {
+ name: "bar".into(),
+ data_type: DataType::Int(None),
+ collation: None,
+ options: vec![
+ ColumnOptionDef {
+ name: None,
+ option: ColumnOption::Unique {
+ is_primary: true,
+ characteristics: None,
+ },
+ },
+ ColumnOptionDef {
+ name: None,
+ option:
ColumnOption::DialectSpecific(vec![Token::make_keyword(kind)]),
+ },
+ ],
+ };
+
+ let sql = "CREATE TABLE foo (bar INT PRIMARY KEY ASC)";
+ match sqlite_and_generic().verified_stmt(sql) {
+ Statement::CreateTable(CreateTable { columns, .. }) => {
+ assert_eq!(vec![expected_column_def("ASC")], columns);
+ }
+ _ => unreachable!(),
+ }
+ let sql = "CREATE TABLE foo (bar INT PRIMARY KEY DESC)";
+ match sqlite_and_generic().verified_stmt(sql) {
+ Statement::CreateTable(CreateTable { columns, .. }) => {
+ assert_eq!(vec![expected_column_def("DESC")], columns);
+ }
+ _ => unreachable!(),
+ }
+}
+
#[test]
fn parse_create_sqlite_quote() {
let sql = "CREATE TABLE `PRIMARY` (\"KEY\" INT, [INDEX] INT)";
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]