Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for DEFERRED, IMMEDIATE, and EXCLUSIVE in SQLite's BEGIN TRANSACTION command #1067

Merged
merged 4 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
32 changes: 31 additions & 1 deletion src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1819,6 +1819,8 @@
StartTransaction {
modes: Vec<TransactionMode>,
begin: bool,
/// Only for SQLite
modifier: Option<TransactionModifier>,
},
/// `SET TRANSACTION ...`
SetTransaction {
Expand Down Expand Up @@ -3107,9 +3109,14 @@
Statement::StartTransaction {
modes,
begin: syntax_begin,
modifier,
} => {
if *syntax_begin {
write!(f, "BEGIN TRANSACTION")?;
if let Some(modifier) = *modifier {
write!(f, "BEGIN {} TRANSACTION", modifier)?;
} else {
write!(f, "BEGIN TRANSACTION")?;
}
} else {
write!(f, "START TRANSACTION")?;
}
Expand Down Expand Up @@ -4292,6 +4299,29 @@
}
}

/// Sqlite specific syntax
///
/// https://sqlite.org/lang_transaction.html

Check failure on line 4304 in src/ast/mod.rs

View workflow job for this annotation

GitHub Actions / docs

this URL is not a hyperlink
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum TransactionModifier {
Deferred,
Immediate,
Exclusive,
}

impl fmt::Display for TransactionModifier {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use TransactionModifier::*;
f.write_str(match self {
Deferred => "DEFERRED",
Immediate => "IMMEDIATE",
Exclusive => "EXCLUSIVE",
})
}
}

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ impl Dialect for GenericDialect {
fn supports_group_by_expr(&self) -> bool {
true
}

fn supports_start_transaction_modifier(&self) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

true
}
}
4 changes: 4 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ pub trait Dialect: Debug + Any {
fn supports_in_empty_list(&self) -> bool {
false
}
/// Returns true if the dialect supports `BEGIN {DEFERRED | IMMEDIATE | EXCLUSIVE} [TRANSACTION]` statements
fn supports_start_transaction_modifier(&self) -> bool {
false
}
/// Returns true if the dialect has a CONVERT function which accepts a type first
/// and an expression second, e.g. `CONVERT(varchar, 1)`
fn convert_type_before_value(&self) -> bool {
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ impl Dialect for SQLiteDialect {
true
}

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

fn is_identifier_part(&self, ch: char) -> bool {
self.is_identifier_start(ch) || ch.is_ascii_digit()
}
Expand Down
3 changes: 3 additions & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ define_keywords!(
DECIMAL,
DECLARE,
DEFAULT,
DEFERRED,
DELETE,
DELIMITED,
DELIMITER,
Expand Down Expand Up @@ -254,6 +255,7 @@ define_keywords!(
EVERY,
EXCEPT,
EXCLUDE,
EXCLUSIVE,
EXEC,
EXECUTE,
EXISTS,
Expand Down Expand Up @@ -321,6 +323,7 @@ define_keywords!(
IF,
IGNORE,
ILIKE,
IMMEDIATE,
IMMUTABLE,
IN,
INCLUDE,
Expand Down
13 changes: 13 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7834,14 +7834,27 @@ impl<'a> Parser<'a> {
Ok(Statement::StartTransaction {
modes: self.parse_transaction_modes()?,
begin: false,
modifier: None,
})
}

pub fn parse_begin(&mut self) -> Result<Statement, ParserError> {
let modifier = if !self.dialect.supports_start_transaction_modifier() {
None
} else if self.parse_keyword(Keyword::DEFERRED) {
Some(TransactionModifier::Deferred)
} else if self.parse_keyword(Keyword::IMMEDIATE) {
Some(TransactionModifier::Immediate)
} else if self.parse_keyword(Keyword::EXCLUSIVE) {
Some(TransactionModifier::Exclusive)
} else {
None
};
let _ = self.parse_one_of_keywords(&[Keyword::TRANSACTION, Keyword::WORK]);
Ok(Statement::StartTransaction {
modes: self.parse_transaction_modes()?,
begin: true,
modifier,
})
}

Expand Down
44 changes: 44 additions & 0 deletions src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,50 @@ pub fn all_dialects() -> TestedDialects {
}
}

static ALL_DIALECT_NAMES: &[&str] = &[
"generic",
"mysql",
"postgresql",
"hive",
"sqlite",
"snowflake",
"redshift",
"mssql",
"clickhouse",
"bigquery",
"ansi",
"duckdb",
];

pub fn partition_all_dialects_by_inclusion(
dialect_names: Vec<&str>,
) -> (TestedDialects, TestedDialects) {
for dialect_name in &dialect_names {
assert!(
ALL_DIALECT_NAMES.contains(dialect_name),
"Unknown dialect: {}",
dialect_name
);
}

let (included_dialect_names, excluded_dialect_names) = ALL_DIALECT_NAMES
.iter()
.partition(|&dialect_name| dialect_names.contains(dialect_name));

let build_tested_dialects = |names: Vec<&str>| TestedDialects {
dialects: names
.iter()
.map(|&name| dialect_from_str(name).unwrap())
.collect(),
options: None,
};

(
build_tested_dialects(included_dialect_names),
build_tested_dialects(excluded_dialect_names),
)
}

pub fn assert_eq_vec<T: ToString>(expected: &[&str], actual: &[T]) {
assert_eq!(
expected,
Expand Down
31 changes: 30 additions & 1 deletion tests/sqlparser_sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use test_utils::*;
use sqlparser::ast::SelectItem::UnnamedExpr;
use sqlparser::ast::*;
use sqlparser::dialect::{GenericDialect, SQLiteDialect};
use sqlparser::parser::ParserOptions;
use sqlparser::parser::{ParserError, ParserOptions};
use sqlparser::tokenizer::Token;

#[test]
Expand Down Expand Up @@ -431,6 +431,35 @@ fn invalid_empty_list() {
);
}

#[test]
fn parse_start_transaction_with_modifier() {
let (supported_dialects, unsupported_dialects) =
partition_all_dialects_by_inclusion(vec!["generic", "sqlite"]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it isn't clear to me that using a String is preferable here to simply explicitly referring to SqliteDialect and GenericDialect

Using a string defers any errors to runtime rather than compile time

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback!

(I have not yet been able to learn Rust properly, so I may be missing the point..)
I first tried to pass the arguments like this

partition_all_dialects_by_inclusion(vec![Box::new(SQLiteDialect {}), Box::new(GenericDialect {})])

but I got the following error indicating that trait objects cannot be compared.

error[E0277]: can't compare `dyn dialect::Dialect` with `dyn dialect::Dialect`
    --> src/test_utils.rs:230:36
     |
230  |         .filter(|d| !dialect_names.contains(d))
     |                                    ^^^^^^^^ no implementation for `dyn dialect::Dialect == dyn dialect::Dialect`
     |
     = help: the trait `PartialEq` is not implemented for `dyn dialect::Dialect`
     = note: required for `Box<dyn dialect::Dialect>` to implement `PartialEq`

I couldn't think of a suitable way to deal with this, so I changed to using String which can be compared.
But yes, this is not a good solution, so you can skip it!


supported_dialects.verified_stmt("BEGIN DEFERRED TRANSACTION");
supported_dialects.verified_stmt("BEGIN IMMEDIATE TRANSACTION");
supported_dialects.verified_stmt("BEGIN EXCLUSIVE TRANSACTION");
supported_dialects.one_statement_parses_to("BEGIN DEFERRED", "BEGIN DEFERRED TRANSACTION");
supported_dialects.one_statement_parses_to("BEGIN IMMEDIATE", "BEGIN IMMEDIATE TRANSACTION");
supported_dialects.one_statement_parses_to("BEGIN EXCLUSIVE", "BEGIN EXCLUSIVE TRANSACTION");

let res = unsupported_dialects.parse_sql_statements("BEGIN DEFERRED");
assert_eq!(
ParserError::ParserError("Expected end of statement, found: DEFERRED".to_string()),
res.unwrap_err(),
);
let res = unsupported_dialects.parse_sql_statements("BEGIN IMMEDIATE");
assert_eq!(
ParserError::ParserError("Expected end of statement, found: IMMEDIATE".to_string()),
res.unwrap_err(),
);
let res = unsupported_dialects.parse_sql_statements("BEGIN EXCLUSIVE");
assert_eq!(
ParserError::ParserError("Expected end of statement, found: EXCLUSIVE".to_string()),
res.unwrap_err(),
);
}

fn sqlite() -> TestedDialects {
TestedDialects {
dialects: vec![Box::new(SQLiteDialect {})],
Expand Down
Loading