iffyio commented on code in PR #2071:
URL:
https://github.com/apache/datafusion-sqlparser-rs/pull/2071#discussion_r2447018699
##########
src/parser/mod.rs:
##########
@@ -5592,7 +5592,20 @@ impl<'a> Parser<'a> {
}
let name = self.parse_object_name(false)?;
- let period = self.parse_trigger_period()?;
+ let period = if dialect_of!(self is SQLiteDialect)
+ && self
+ .peek_one_of_keywords(&[
+ Keyword::INSERT,
+ Keyword::UPDATE,
+ Keyword::DELETE,
+ Keyword::TRUNCATE,
+ ])
+ .is_some()
+ {
+ None // SQLite: period can be skipped (equivalent to BEFORE)
+ } else {
+ Some(self.parse_trigger_period()?)
+ };
Review Comment:
I think we can do something like this to support optionality
```
let period = self.maybe_parse(|parser| parser.parse_trigger_period());
```
It would imply the same behavior for other dialects than sqlite but that's
okay for the parser to be more permissive
##########
tests/sqlparser_sqlite.rs:
##########
@@ -863,6 +863,50 @@ fn test_create_trigger() {
}
_ => unreachable!("Expected CREATE TRIGGER statement"),
}
+
+ // We test a trigger defined without a period (BEFORE/AFTER/INSTEAD OF)
+ let statement7 = "CREATE TRIGGER trg_inherit_asset_models INSERT ON assets
FOR EACH ROW BEGIN INSERT INTO users (name) SELECT pam.name FROM users AS pam;
END";
+ match sqlite().verified_stmt(statement7) {
+ Statement::CreateTrigger(CreateTrigger {
+ or_alter,
+ temporary,
+ or_replace,
+ is_constraint,
+ name,
+ period,
+ period_before_table,
+ events,
+ table_name,
+ referenced_table_name,
+ referencing,
+ trigger_object,
+ condition,
+ exec_body: _,
+ statements_as,
+ statements: _,
+ characteristics,
+ }) => {
+ assert!(!or_alter);
+ assert!(!temporary);
+ assert!(!or_replace);
+ assert!(!is_constraint);
+ assert_eq!(name.to_string(), "trg_inherit_asset_models");
+ assert_eq!(period, None);
+ assert!(period_before_table);
+ assert_eq!(events, vec![TriggerEvent::Insert]);
+ assert_eq!(table_name.to_string(), "assets");
+ assert!(referenced_table_name.is_none());
+ assert!(referencing.is_empty());
+ assert_eq!(
+ trigger_object,
+ Some(TriggerObjectKind::ForEach(TriggerObject::Row))
+ );
+ assert!(condition.is_none());
+ assert!(!statements_as);
+ assert!(characteristics.is_none());
+ }
+ _ => unreachable!("Expected CREATE TRIGGER statement"),
+ }
Review Comment:
```suggestion
sqlite().verified_stmt(statement7);
```
To cut down on the lines of test code I think the following would suffice,
since the other test scenarios already inspect the returned AST
##########
src/ast/ddl.rs:
##########
@@ -3029,7 +3029,9 @@ pub struct CreateTrigger {
/// FOR EACH ROW
/// EXECUTE FUNCTION trigger_function();
/// ```
- pub period: TriggerPeriod,
+ ///
+ /// This may be omitted in SQLite, the effect is equivalent to BEFORE.
Review Comment:
```suggestion
```
we can skip the description since the optionality is clear from the
representation (and the conditions can change over time)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]