// 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. #![warn(clippy::all)] //! Test SQL syntax specific to SQLite. The parser based on the //! generic dialect is also tested (on the inputs it can handle). #[macro_use] mod test_utils; use sqlparser::ast::helpers::attached_token::AttachedToken; use sqlparser::keywords::Keyword; use test_utils::*; use sqlparser::ast::SelectItem::UnnamedExpr; use sqlparser::ast::Value::Placeholder; use sqlparser::ast::*; use sqlparser::dialect::{AnsiDialect, GenericDialect, MySqlDialect, SQLiteDialect}; use sqlparser::parser::{Parser, ParserError, ParserOptions}; use sqlparser::tokenizer::{Location, Span, Token}; #[test] fn pragma_no_value() { let sql = "PRAGMA cache_size"; match sqlite_and_generic().verified_stmt(sql) { Statement::Pragma { name, value: None, is_eq: false, } => { assert_eq!("cache_size", name.to_string()); } _ => unreachable!(), } } #[test] fn pragma_eq_style() { let sql = "PRAGMA cache_size = 10"; match sqlite_and_generic().verified_stmt(sql) { Statement::Pragma { name, value: Some(val), is_eq: true, } => { assert_eq!("cache_size", name.to_string()); assert_eq!("10", val.to_string()); } _ => unreachable!(), } } #[test] fn pragma_function_style() { let sql = "PRAGMA cache_size(10)"; match sqlite_and_generic().verified_stmt(sql) { Statement::Pragma { name, value: Some(val), is_eq: false, } => { assert_eq!("cache_size", name.to_string()); assert_eq!("10", val.to_string()); } _ => unreachable!(), } } #[test] fn pragma_eq_string_style() { let sql = "PRAGMA table_info = 'sqlite_master'"; match sqlite_and_generic().verified_stmt(sql) { Statement::Pragma { name, value: Some(val), is_eq: true, } => { assert_eq!("table_info", name.to_string()); assert_eq!("'sqlite_master'", val.to_string()); } _ => unreachable!(), } } #[test] fn pragma_function_string_style() { let sql = "PRAGMA table_info(\"sqlite_master\")"; match sqlite_and_generic().verified_stmt(sql) { Statement::Pragma { name, value: Some(val), is_eq: false, } => { assert_eq!("table_info", name.to_string()); assert_eq!("\"sqlite_master\"", val.to_string()); } _ => unreachable!(), } } #[test] fn pragma_eq_placeholder_style() { let sql = "PRAGMA table_info = ?"; match sqlite_and_generic().verified_stmt(sql) { Statement::Pragma { name, value: Some(val), is_eq: true, } => { assert_eq!("table_info", name.to_string()); assert_eq!("?", val.to_string()); } _ => unreachable!(), } } #[test] fn parse_create_table_without_rowid() { let sql = "CREATE TABLE t (a INT) WITHOUT ROWID"; match sqlite_and_generic().verified_stmt(sql) { Statement::CreateTable(CreateTable { name, sqlite_table_options, .. }) => { assert_eq!( sqlite_table_options.options, vec![SqliteTableOption::WithoutRowid] ); assert_eq!("t", name.to_string()); } _ => unreachable!(), } } #[test] fn parse_create_virtual_table() { let sql = "CREATE VIRTUAL TABLE IF NOT EXISTS t USING module_name (arg1, arg2)"; match sqlite_and_generic().verified_stmt(sql) { Statement::CreateVirtualTable { name, if_not_exists: true, module_name, module_args, } => { assert_eq!("t", name.to_string()); assert_eq!("module_name", module_name.to_string()); assert_eq!( vec!["arg1", "arg2"], module_args .iter() .map(|arg| arg.value.as_str()) .collect::>() ); } _ => unreachable!(), } let sql = "CREATE VIRTUAL TABLE t USING module_name"; sqlite_and_generic().verified_stmt(sql); } #[test] fn parse_create_view_temporary_if_not_exists() { let sql = "CREATE TEMPORARY VIEW IF NOT EXISTS myschema.myview AS SELECT foo FROM bar"; match sqlite_and_generic().verified_stmt(sql) { Statement::CreateView(CreateView { name, columns, query, or_replace, materialized, options, cluster_by, comment, with_no_schema_binding: late_binding, if_not_exists, temporary, .. }) => { assert_eq!("myschema.myview", name.to_string()); assert_eq!(Vec::::new(), columns); assert_eq!("SELECT foo FROM bar", query.to_string()); assert!(!materialized); assert!(!or_replace); assert_eq!(options, CreateTableOptions::None); assert_eq!(cluster_by, vec![]); assert!(comment.is_none()); assert!(!late_binding); assert!(if_not_exists); assert!(temporary); } _ => unreachable!(), } } #[test] fn double_equality_operator() { // Sqlite supports this operator: https://www.sqlite.org/lang_expr.html#binaryops let input = "SELECT a==b FROM t"; let expected = "SELECT a = b FROM t"; let _ = sqlite_and_generic().one_statement_parses_to(input, expected); } #[test] fn parse_create_table_auto_increment() { let sql = "CREATE TABLE foo (bar INT PRIMARY KEY AUTOINCREMENT)"; match sqlite_and_generic().verified_stmt(sql) { Statement::CreateTable(CreateTable { name, columns, .. }) => { assert_eq!(name.to_string(), "foo"); assert_eq!( vec![ColumnDef { name: "bar".into(), data_type: DataType::Int(None), options: vec![ ColumnOptionDef { name: None, option: ColumnOption::PrimaryKey(PrimaryKeyConstraint { name: None, index_name: None, index_type: None, columns: vec![], include: vec![], index_options: vec![], characteristics: None, on_conflict: None, }), }, ColumnOptionDef { name: None, option: ColumnOption::DialectSpecific(vec![Token::make_keyword( "AUTOINCREMENT" )]), }, ], }], columns ); } _ => unreachable!(), } } #[test] fn parse_create_table_primary_key_asc_desc() { let expected_column_def = |kind| ColumnDef { name: "bar".into(), data_type: DataType::Int(None), options: vec![ ColumnOptionDef { name: None, option: ColumnOption::PrimaryKey(PrimaryKeyConstraint { name: None, index_name: None, index_type: None, columns: vec![], include: vec![], index_options: vec![], characteristics: None, on_conflict: 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)"; match sqlite().verified_stmt(sql) { Statement::CreateTable(CreateTable { name, columns, .. }) => { assert_eq!(name.to_string(), "`PRIMARY`"); assert_eq!( vec![ ColumnDef { name: Ident::with_quote('"', "KEY"), data_type: DataType::Int(None), options: vec![], }, ColumnDef { name: Ident::with_quote('[', "INDEX"), data_type: DataType::Int(None), options: vec![], }, ], columns ); } _ => unreachable!(), } } #[test] fn parse_create_table_gencol() { let sql_default = "CREATE TABLE t1 (a INT, b INT GENERATED ALWAYS AS (a * 2))"; sqlite_and_generic().verified_stmt(sql_default); let sql_virt = "CREATE TABLE t1 (a INT, b INT GENERATED ALWAYS AS (a * 2) VIRTUAL)"; sqlite_and_generic().verified_stmt(sql_virt); let sql_stored = "CREATE TABLE t1 (a INT, b INT GENERATED ALWAYS AS (a * 2) STORED)"; sqlite_and_generic().verified_stmt(sql_stored); sqlite_and_generic().verified_stmt("CREATE TABLE t1 (a INT, b INT AS (a * 2))"); sqlite_and_generic().verified_stmt("CREATE TABLE t1 (a INT, b INT AS (a * 2) VIRTUAL)"); sqlite_and_generic().verified_stmt("CREATE TABLE t1 (a INT, b INT AS (a * 2) STORED)"); } #[test] fn parse_create_table_on_conflict_col() { for keyword in [ Keyword::ROLLBACK, Keyword::ABORT, Keyword::FAIL, Keyword::IGNORE, Keyword::REPLACE, ] { let sql = format!("CREATE TABLE t1 (a INT, b INT ON CONFLICT {keyword:?})"); match sqlite_and_generic().verified_stmt(&sql) { Statement::CreateTable(CreateTable { columns, .. }) => { assert_eq!( vec![ColumnOptionDef { name: None, option: ColumnOption::OnConflict(keyword), }], columns[1].options ); } _ => unreachable!(), } } } #[test] fn test_parse_create_table_on_conflict_col_err() { let sql_err = "CREATE TABLE t1 (a INT, b INT ON CONFLICT BOH)"; let err = sqlite_and_generic() .parse_sql_statements(sql_err) .unwrap_err(); assert_eq!( err, ParserError::ParserError( "Expected: one of ROLLBACK or ABORT or FAIL or IGNORE or REPLACE, found: BOH" .to_string() ) ); } #[test] fn parse_create_table_untyped() { sqlite().verified_stmt("CREATE TABLE t1 (a, b AS (a * 2), c NOT NULL)"); } #[test] fn test_placeholder() { // In postgres, this would be the absolute value operator '@' applied to the column 'xxx' // But in sqlite, this is a named parameter. // see https://www.sqlite.org/lang_expr.html#varparam let sql = "SELECT @xxx"; let ast = sqlite().verified_only_select(sql); assert_eq!( ast.projection[0], UnnamedExpr(Expr::Value( (Value::Placeholder("@xxx".into())).with_empty_span() )), ); } #[test] fn parse_colon_does_not_start_json_access() { // SQLite rejects `SELECT c:se` (`near ":se": syntax error`); no `:` traversal. for sql in ["SELECT c:se", "SELECT a:b FROM t"] { assert_eq!( sqlite().parse_sql_statements(sql), Err(ParserError::ParserError( "Expected: end of statement, found: :".to_string() )) ); } let ast = sqlite().verified_only_select("SELECT :xxx"); assert_eq!( ast.projection[0], UnnamedExpr(Expr::Value( (Value::Placeholder(":xxx".into())).with_empty_span() )), ); } #[test] fn parse_create_table_with_strict() { let sql = "CREATE TABLE Fruits (id TEXT NOT NULL PRIMARY KEY) STRICT"; if let Statement::CreateTable(CreateTable { name, sqlite_table_options, .. }) = sqlite().verified_stmt(sql) { assert_eq!(name.to_string(), "Fruits"); assert_eq!( sqlite_table_options.options, vec![SqliteTableOption::Strict] ); } } #[test] fn parse_single_quoted_identified() { sqlite().verified_only_select("SELECT 't'.*, t.'x' FROM 't'"); // TODO: add support for select 't'.x } #[test] fn parse_substring() { // SQLite supports the SUBSTRING function since v3.34, but does not support the SQL standard // SUBSTRING(expr FROM start FOR length) syntax. // https://www.sqlite.org/lang_corefunc.html#substr sqlite().verified_only_select("SELECT SUBSTRING('SQLITE', 3, 4)"); sqlite().verified_only_select("SELECT SUBSTR('SQLITE', 3, 4)"); sqlite().verified_only_select("SELECT SUBSTRING('SQLITE', 3)"); sqlite().verified_only_select("SELECT SUBSTR('SQLITE', 3)"); } #[test] fn parse_window_function_with_filter() { for func_name in [ "row_number", "rank", "max", "count", "user_defined_function", ] { let sql = format!("SELECT {func_name}(x) FILTER (WHERE y) OVER () FROM t"); let select = sqlite().verified_only_select(&sql); assert_eq!(select.to_string(), sql); assert_eq!( select.projection, vec![SelectItem::UnnamedExpr(Expr::Function(Function { name: ObjectName::from(vec![Ident::new(func_name)]), uses_odbc_syntax: false, parameters: FunctionArguments::None, args: FunctionArguments::List(FunctionArgumentList { duplicate_treatment: None, args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr( Expr::Identifier(Ident::new("x")) ))], clauses: vec![], }), null_treatment: None, over: Some(WindowType::WindowSpec(WindowSpec { window_name: None, partition_by: vec![], order_by: vec![], window_frame: None, })), filter: Some(Box::new(Expr::Identifier(Ident::new("y")))), within_group: vec![], }))] ); } } #[test] fn parse_attach_database() { let sql = "ATTACH DATABASE 'test.db' AS test"; let verified_stmt = sqlite().verified_stmt(sql); assert_eq!(sql, format!("{verified_stmt}")); match verified_stmt { Statement::AttachDatabase { schema_name, database_file_name: Expr::Value(ValueWithSpan { value: Value::SingleQuotedString(literal_name), span: _, }), database: true, } => { assert_eq!(schema_name.value, "test"); assert_eq!(literal_name, "test.db"); } _ => unreachable!(), } } #[test] fn parse_update_tuple_row_values() { // See https://github.com/sqlparser-rs/sqlparser-rs/issues/1311 assert_eq!( sqlite().verified_stmt("UPDATE x SET (a, b) = (1, 2)"), Statement::Update(Update { optimizer_hints: vec![], or: None, assignments: vec![Assignment { target: AssignmentTarget::Tuple(vec![ ObjectName::from(vec![Ident::new("a"),]), ObjectName::from(vec![Ident::new("b"),]), ]), value: Expr::Tuple(vec![ Expr::Value((Value::Number("1".parse().unwrap(), false)).with_empty_span()), Expr::Value((Value::Number("2".parse().unwrap(), false)).with_empty_span()) ]) }], selection: None, table: TableWithJoins { relation: table_from_name(ObjectName::from(vec![Ident::new("x")])), joins: vec![], }, on_cluster: None, from: None, returning: None, settings: None, output: None, order_by: vec![], limit: None, update_token: AttachedToken::empty() }) ); } #[test] fn parse_where_in_empty_list() { let sql = "SELECT * FROM t1 WHERE a IN ()"; let select = sqlite().verified_only_select(sql); if let Expr::In { target: InTarget::List(list), .. } = select.selection.as_ref().unwrap() { assert_eq!(list.len(), 0); } else { unreachable!() } sqlite_with_options(ParserOptions::new().with_trailing_commas(true)).one_statement_parses_to( "SELECT * FROM t1 WHERE a IN (,)", "SELECT * FROM t1 WHERE a IN ()", ); } #[test] fn parse_in_table_keyword_names() { // Keywords SQLite lets fall back to an identifier name a table after `IN`. for sql in [ "SELECT 1 IN view", "SELECT 1 IN offset", "SELECT 1 IN key", "SELECT 1 IN abort", ] { sqlite().verified_stmt(sql); } // Its reserved keywords do not, so the target falls back to a parenthesized list. for (sql, found) in [ ("SELECT 1 IN FROM t", "FROM"), ("SELECT 1 IN select", "select"), ("SELECT 1 IN where", "where"), ("SELECT 1 IN NULL", "NULL"), ("SELECT 1 IN table", "table"), ("SELECT a FROM t WHERE a IN AND b", "AND"), ] { assert_eq!( sqlite().parse_sql_statements(sql).unwrap_err(), ParserError::ParserError(format!("Expected: (, found: {found}")), ); } // A quoted keyword is a plain name. sqlite().verified_stmt(r#"SELECT 1 IN "from""#); } #[test] fn invalid_empty_list() { let sql = "SELECT * FROM t1 WHERE a IN (,,)"; let sqlite = sqlite_with_options(ParserOptions::new().with_trailing_commas(true)); assert_eq!( "sql parser error: Expected: an expression, found: ,", sqlite.parse_sql_statements(sql).unwrap_err().to_string() ); } #[test] fn parse_start_transaction_with_modifier() { sqlite_and_generic().verified_stmt("BEGIN DEFERRED TRANSACTION"); sqlite_and_generic().verified_stmt("BEGIN IMMEDIATE TRANSACTION"); sqlite_and_generic().verified_stmt("BEGIN EXCLUSIVE TRANSACTION"); sqlite_and_generic().verified_stmt("BEGIN DEFERRED"); sqlite_and_generic().verified_stmt("BEGIN IMMEDIATE"); sqlite_and_generic().verified_stmt("BEGIN EXCLUSIVE"); } #[test] fn test_dollar_identifier_as_placeholder() { // This relates to the discussion in issue #291. The `$id` should be treated as a placeholder, // not as an identifier in SQLite dialect. // // Reference: https://www.sqlite.org/lang_expr.html#varparam match sqlite().verified_expr("id = $id") { Expr::BinaryOp { op, left, right } => { assert_eq!(op, BinaryOperator::Eq); assert_eq!(left, Box::new(Expr::Identifier(Ident::new("id")))); assert_eq!( right, Box::new(Expr::Value( (Placeholder("$id".to_string())).with_empty_span() )) ); } _ => unreachable!(), } // $$ is a valid placeholder in SQLite match sqlite().verified_expr("id = $$") { Expr::BinaryOp { op, left, right } => { assert_eq!(op, BinaryOperator::Eq); assert_eq!(left, Box::new(Expr::Identifier(Ident::new("id")))); assert_eq!( right, Box::new(Expr::Value( (Placeholder("$$".to_string())).with_empty_span() )) ); } _ => unreachable!(), } } #[test] fn test_match_operator() { assert_eq!( sqlite().verified_expr("col MATCH 'pattern'"), Expr::BinaryOp { op: BinaryOperator::Match, left: Box::new(Expr::Identifier(Ident::new("col"))), right: Box::new(Expr::Value( (Value::SingleQuotedString("pattern".to_string())).with_empty_span() )) } ); sqlite().verified_only_select("SELECT * FROM email WHERE email MATCH 'fts5'"); } #[test] fn test_regexp_operator() { assert_eq!( sqlite().verified_expr("col REGEXP 'pattern'"), Expr::BinaryOp { op: BinaryOperator::Regexp, left: Box::new(Expr::Identifier(Ident::new("col"))), right: Box::new(Expr::Value( (Value::SingleQuotedString("pattern".to_string())).with_empty_span() )) } ); sqlite().verified_only_select(r#"SELECT count(*) FROM messages WHERE msg_text REGEXP '\d+'"#); // Should return an error, not panic for sql in ["SELECT 1 REGEXP", "SELECT 1 MATCH"] { assert_eq!( sqlite().parse_sql_statements(sql), Err(ParserError::ParserError( "Expected: an expression, found: EOF".to_string() )) ); } } #[test] fn test_glob_operator() { assert_eq!( sqlite().verified_expr("col GLOB 'pattern'"), Expr::BinaryOp { op: BinaryOperator::Glob, left: Box::new(Expr::Identifier(Ident::new("col"))), right: Box::new(Expr::Value( (Value::SingleQuotedString("pattern".to_string())).with_empty_span() )) } ); sqlite().verified_only_select(r#"SELECT count(*) FROM files WHERE name GLOB '*.txt'"#); // Should return an error, not panic assert_eq!( sqlite().parse_sql_statements("SELECT 1 GLOB"), Err(ParserError::ParserError( "Expected: an expression, found: EOF".to_string() )) ); } #[test] fn sqlite_is_operators() { for (sql, op) in [ ("a IS b", BinaryOperator::Is), ("a IS NOT b", BinaryOperator::IsNot), ] { assert_eq!( sqlite().verified_expr(sql), Expr::BinaryOp { left: Box::new(Expr::Identifier(Ident::new("a"))), op, right: Box::new(Expr::Identifier(Ident::new("b"))), } ); } assert_eq!( sqlite().verified_expr("a IS b + c"), Expr::BinaryOp { left: Box::new(Expr::Identifier(Ident::new("a"))), op: BinaryOperator::Is, right: Box::new(Expr::BinaryOp { left: Box::new(Expr::Identifier(Ident::new("b"))), op: BinaryOperator::Plus, right: Box::new(Expr::Identifier(Ident::new("c"))), }), } ); assert_eq!( sqlite().verified_expr("a IS b AND c IS NOT d"), Expr::BinaryOp { left: Box::new(Expr::BinaryOp { left: Box::new(Expr::Identifier(Ident::new("a"))), op: BinaryOperator::Is, right: Box::new(Expr::Identifier(Ident::new("b"))), }), op: BinaryOperator::And, right: Box::new(Expr::BinaryOp { left: Box::new(Expr::Identifier(Ident::new("c"))), op: BinaryOperator::IsNot, right: Box::new(Expr::Identifier(Ident::new("d"))), }), } ); assert_eq!( sqlite().verified_expr("a IS b = c"), Expr::BinaryOp { left: Box::new(Expr::BinaryOp { left: Box::new(Expr::Identifier(Ident::new("a"))), op: BinaryOperator::Is, right: Box::new(Expr::Identifier(Ident::new("b"))), }), op: BinaryOperator::Eq, right: Box::new(Expr::Identifier(Ident::new("c"))), } ); match sqlite().verified_expr("a IS b IN (1)") { Expr::In { expr, target: InTarget::List(list), negated, global, } => { assert!(!negated); assert!(!global); assert_eq!( *expr, Expr::BinaryOp { left: Box::new(Expr::Identifier(Ident::new("a"))), op: BinaryOperator::Is, right: Box::new(Expr::Identifier(Ident::new("b"))), } ); assert_eq!(list.len(), 1); assert!(matches!( list[0], Expr::Value(ValueWithSpan { value: Value::Number(_, _), .. }) )); } other => panic!("expected InList on top, got {other:?}"), } // A dangling right-hand side is rejected for sql in ["SELECT a IS", "SELECT a IS NOT"] { assert_eq!( sqlite().parse_sql_statements(sql), Err(ParserError::ParserError( "Expected: an expression, found: EOF".to_string() )) ); } } #[test] fn sqlite_is_operator_keyword_operands() { // SQLite has no `IS UNKNOWN`, `IS JSON` or `IS NORMALIZED` predicate. for right in ["json('{}')", "json", "unknown", "normalized", "nfc"] { for (op, not) in [(BinaryOperator::Is, ""), (BinaryOperator::IsNot, "NOT ")] { let sql = format!("a IS {not}{right}"); assert_eq!( sqlite().verified_expr(&sql), Expr::BinaryOp { left: Box::new(Expr::Identifier(Ident::new("a"))), op, right: Box::new(sqlite().verified_expr(right)), }, "{sql}" ); } } sqlite().verified_stmt("SELECT a IS json('{}'), a IS NOT json('{}') FROM t"); for (sql, found) in [ ("SELECT a FROM t WHERE a IS NFC NORMALIZED", "NORMALIZED"), ("SELECT a FROM t WHERE a IS NOT JSON OBJECT", "OBJECT"), ] { assert_eq!( sqlite().parse_sql_statements(sql), Err(ParserError::ParserError(format!( "Expected: end of statement, found: {found}" ))), "{sql}" ); } } #[test] fn test_update_delete_limit() { match sqlite().verified_stmt("UPDATE foo SET bar = 1 LIMIT 99") { Statement::Update(Update { limit, .. }) => { assert_eq!(limit, Some(Expr::value(number("99")))); } _ => unreachable!(), } match sqlite().verified_stmt("DELETE FROM foo LIMIT 99") { Statement::Delete(Delete { limit, .. }) => { assert_eq!(limit, Some(Expr::value(number("99")))); } _ => unreachable!(), } } #[test] fn test_create_trigger() { let statement1 = "CREATE TRIGGER trg_inherit_asset_models AFTER INSERT ON assets FOR EACH ROW BEGIN INSERT INTO users (name) SELECT pam.name FROM users AS pam; END"; match sqlite().verified_stmt(statement1) { 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, Some(TriggerPeriod::After)); 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"), } // Here we check that the variant of CREATE TRIGGER that omits the `FOR EACH ROW` clause, // which in SQLite may be implicitly assumed, is parsed correctly. let statement2 = "CREATE TRIGGER log_new_user AFTER INSERT ON users BEGIN INSERT INTO user_log (user_id, action, timestamp) VALUES (NEW.id, 'created', datetime('now')); END"; match sqlite().verified_stmt(statement2) { 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(), "log_new_user"); assert_eq!(period, Some(TriggerPeriod::After)); assert!(period_before_table); assert_eq!(events, vec![TriggerEvent::Insert]); assert_eq!(table_name.to_string(), "users"); assert!(referenced_table_name.is_none()); assert!(referencing.is_empty()); assert!(trigger_object.is_none()); assert!(condition.is_none()); assert!(!statements_as); assert!(characteristics.is_none()); } _ => unreachable!("Expected CREATE TRIGGER statement"), } let statement3 = "CREATE TRIGGER cleanup_orders AFTER DELETE ON customers BEGIN DELETE FROM orders WHERE customer_id = OLD.id; DELETE FROM invoices WHERE customer_id = OLD.id; END"; match sqlite().verified_stmt(statement3) { 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(), "cleanup_orders"); assert_eq!(period, Some(TriggerPeriod::After)); assert!(period_before_table); assert_eq!(events, vec![TriggerEvent::Delete]); assert_eq!(table_name.to_string(), "customers"); assert!(referenced_table_name.is_none()); assert!(referencing.is_empty()); assert!(trigger_object.is_none()); assert!(condition.is_none()); assert!(!statements_as); assert!(characteristics.is_none()); } _ => unreachable!("Expected CREATE TRIGGER statement"), } let statement4 = "CREATE TRIGGER trg_before_update BEFORE UPDATE ON products FOR EACH ROW WHEN NEW.price < 0 BEGIN SELECT RAISE(ABORT, 'Price cannot be negative'); END"; match sqlite().verified_stmt(statement4) { 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_before_update"); assert_eq!(period, Some(TriggerPeriod::Before)); assert!(period_before_table); assert_eq!(events, vec![TriggerEvent::Update(Vec::new())]); assert_eq!(table_name.to_string(), "products"); assert!(referenced_table_name.is_none()); assert!(referencing.is_empty()); assert_eq!( trigger_object, Some(TriggerObjectKind::ForEach(TriggerObject::Row)) ); assert!(condition.is_some()); assert!(!statements_as); assert!(characteristics.is_none()); } _ => unreachable!("Expected CREATE TRIGGER statement"), } // We test a INSTEAD OF trigger on a view let statement5 = "CREATE TRIGGER trg_instead_of_insert INSTEAD OF INSERT ON my_view BEGIN INSERT INTO my_table (col1, col2) VALUES (NEW.col1, NEW.col2); END"; match sqlite().verified_stmt(statement5) { 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_instead_of_insert"); assert_eq!(period, Some(TriggerPeriod::InsteadOf)); assert!(period_before_table); assert_eq!(events, vec![TriggerEvent::Insert]); assert_eq!(table_name.to_string(), "my_view"); assert!(referenced_table_name.is_none()); assert!(referencing.is_empty()); assert!(trigger_object.is_none()); assert!(condition.is_none()); assert!(!statements_as); assert!(characteristics.is_none()); } _ => unreachable!("Expected CREATE TRIGGER statement"), } // We test a temporary trigger let statement6 = "CREATE TEMPORARY TRIGGER temp_trigger AFTER INSERT ON temp_table BEGIN UPDATE log_table SET count = count + 1; END"; match sqlite().verified_stmt(statement6) { 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(), "temp_trigger"); assert_eq!(period, Some(TriggerPeriod::After)); assert!(period_before_table); assert_eq!(events, vec![TriggerEvent::Insert]); assert_eq!(table_name.to_string(), "temp_table"); assert!(referenced_table_name.is_none()); assert!(referencing.is_empty()); assert!(trigger_object.is_none()); assert!(condition.is_none()); assert!(!statements_as); assert!(characteristics.is_none()); } _ => 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"; sqlite().verified_stmt(statement7); } #[test] fn test_drop_trigger() { let statement = "DROP TRIGGER IF EXISTS trg_inherit_asset_models"; match sqlite().verified_stmt(statement) { Statement::DropTrigger(DropTrigger { if_exists, trigger_name, table_name, option, }) => { assert!(if_exists); assert_eq!(trigger_name.to_string(), "trg_inherit_asset_models"); assert!(table_name.is_none()); assert!(option.is_none()); } _ => unreachable!("Expected DROP TRIGGER statement"), } } #[test] fn parse_between_low_bound_precedence() { match sqlite().verified_expr("a BETWEEN b > 1 AND c") { Expr::Between { low, .. } => assert!( matches!( *low, Expr::BinaryOp { op: BinaryOperator::Gt, .. } ), "{low:?}" ), other => panic!("expected BETWEEN, got {other:?}"), } sqlite().verified_expr("a BETWEEN b LIKE 'x' AND c"); sqlite().verified_expr("a BETWEEN b IS NOT NULL AND c"); sqlite().expr_parses_to("a BETWEEN b NOTNULL AND c", "a BETWEEN b IS NOT NULL AND c"); sqlite().verified_expr("a BETWEEN b BETWEEN 1 AND 2 AND c"); // Operators after the high bound apply to the whole BETWEEN. sqlite().verified_expr("5 BETWEEN 1 AND NULL IS NULL"); sqlite().verified_expr("5 BETWEEN 1 AND 10 = 1"); assert_eq!( sqlite().parse_sql_statements("SELECT a BETWEEN (1 AND 2 FROM t"), Err(ParserError::ParserError( "Expected: ), found: FROM".to_string() )) ); } #[test] fn parse_pattern_operators_bind_at_like_precedence() { fn where_operator(sql: &str) -> BinaryOperator { let Statement::Query(query) = sqlite().verified_stmt(sql) else { panic!("expected a query"); }; let SetExpr::Select(select) = *query.body else { panic!("expected a select"); }; let Some(Expr::BinaryOp { op, .. }) = select.selection else { panic!("expected a WHERE binary operator"); }; op } // Above AND, so the pattern does not swallow the rest of the expression. for operator in ["REGEXP", "MATCH", "GLOB", "LIKE"] { let sql = format!("SELECT 1 FROM t WHERE a {operator} 'p' AND b = 1"); assert_eq!(where_operator(&sql), BinaryOperator::And, "{operator}"); } // Below string concatenation, so the pattern is not cut short either. for (operator, expected) in [ ("REGEXP", BinaryOperator::Regexp), ("MATCH", BinaryOperator::Match), ("GLOB", BinaryOperator::Glob), ] { let sql = format!("SELECT 1 FROM t WHERE a {operator} 'p' || 'q'"); assert_eq!(where_operator(&sql), expected, "{operator}"); } } #[test] fn parse_update_set_double_eq() { // SQLite treats `==` as `=` in all positions, including SET assignments. sqlite().one_statement_parses_to("UPDATE t SET a == 1", "UPDATE t SET a = 1"); sqlite().one_statement_parses_to("UPDATE t SET a == 1, b == 2", "UPDATE t SET a = 1, b = 2"); // `=` still works sqlite().verified_stmt("UPDATE t SET a = 1"); // Other dialects reject `==` in SET let res = ParserError::ParserError("Expected: =, found: ==".to_string()); assert_eq!( all_dialects_except(|d| d.supports_double_eq_assignment()) .parse_sql_statements("UPDATE t SET a == 1") .unwrap_err(), res, ); } #[test] fn test_non_bmp_identifiers() { // SQLite tokenizer treats every byte >= 0x80 as an identifier character, // so any Unicode code point above U+007F is a valid identifier start/part. sqlite().verified_stmt("SELECT 󟿾"); sqlite().verified_stmt("SELECT 𒀀"); sqlite().verified_stmt("SELECT 𒀀𒀁"); // U+DFFFE is not alphabetic, so GenericDialect rejects it as an identifier. assert_eq!( sqlparser::parser::Parser::parse_sql(&GenericDialect {}, "SELECT \u{DFFFE}"), Err(ParserError::ParserError( "Expected: an expression, found: \u{DFFFE} at Line: 1, Column: 8".to_string() )) ); // SQLite rejects U+007F as an unrecognized token. assert_eq!( sqlite().parse_sql_statements("SELECT \u{007f}"), Err(ParserError::ParserError( "Expected: an expression, found: \u{7f}".to_string() )) ); } #[test] fn parse_create_table_string_column_names() { sqlite().verified_stmt("CREATE TABLE t ('a')"); sqlite().verified_stmt(r#"CREATE TABLE '""' ('id' INT UNSIGNED NOT NULL)"#); sqlite().verified_stmt( r#"CREATE TABLE '""' ('id' INT UNSIGNED NOT NULL, 'name' TEXT NOT NULL, 'zip' INT UNSIGNED NULL)"#, ); // Generic dialect does not support this assert_eq!( sqlparser::parser::Parser::parse_sql(&GenericDialect {}, "CREATE TABLE t ('a')"), Err(ParserError::ParserError( "Expected: column name or constraint definition, found: 'a' at Line: 1, Column: 17" .to_string() )) ); } #[test] fn test_cast_empty_type() { // SQLite allows CAST(expr AS) with an empty type name (typetoken can be empty) // See https://www.sqlite.org/lang_expr.html sqlite().verified_stmt("SELECT CAST(a AS)"); // Rejected by dialects without the flag assert_eq!( TestedDialects::new(vec![Box::new(GenericDialect {})]) .parse_sql_statements("SELECT CAST(a AS)") .unwrap_err(), ParserError::ParserError("Expected: a data type name, found: )".to_string()) ); } #[test] fn test_signed_type_modifiers() { // signed and decimal modifiers round-trip verbatim for any type name sqlite().verified_stmt("CREATE TABLE t (a CHAR(+10))"); sqlite().verified_stmt("CREATE TABLE t (a CHAR(-1))"); sqlite().verified_stmt("CREATE TABLE t (a VARCHAR(2.5))"); sqlite().verified_stmt("CREATE TABLE t (a VARCHAR(.5))"); sqlite().verified_stmt("CREATE TABLE t (a INT(-1))"); sqlite().verified_stmt("CREATE TABLE t (a NUMERIC(10.5))"); sqlite().verified_stmt("CREATE TABLE t (a FLOAT(2.5))"); sqlite().verified_stmt("CREATE TABLE t (a NVARCHAR(+5))"); sqlite().verified_stmt("CREATE TABLE t (a TIMESTAMP(-1))"); sqlite().verified_stmt("SELECT CAST(x AS REAL(1.5))"); // a non-standard modifier anywhere in the list selects the custom path sqlite().verified_stmt("CREATE TABLE t (a CHAR(1, 1.5))"); sqlite().verified_stmt("CREATE TABLE t (a INT(1, -2))"); sqlite().verified_stmt("CREATE TABLE t (a DECIMAL(1, 2.5))"); // signed decimals and oversized values select the custom path in either slot sqlite().verified_stmt("CREATE TABLE t (a REAL(-1.5, +2.5))"); sqlite().verified_stmt("CREATE TABLE t (a DECIMAL(10, 99999999999999999999999))"); sqlite().verified_stmt("CREATE TABLE t (a VARCHAR(1, .5))"); for sql in [ "CREATE TABLE t (a CHAR(0.0))", "CREATE TABLE t (a CHAR(-0))", "CREATE TABLE t (a CHAR(1.))", "CREATE TABLE t (a CHAR(1e3))", "CREATE TABLE t (a CHAR(+1.5e-3))", "CREATE TABLE t (a CHAR(+1, +2))", "CREATE TABLE t (a CHAR(-1) NOT NULL DEFAULT NULL)", "CREATE TABLE t (a CHAR(1.5), b CHAR(10))", "CREATE TABLE t (a char(-1))", r#"CREATE TABLE t (a "CHAR"(-1))"#, "CREATE TABLE t (a [CHAR](-1))", "SELECT 1::CHAR(-1)", ] { sqlite().verified_stmt(sql); } sqlite().one_statement_parses_to( "CREATE TABLE t (a CHAR ( - 1 ))", "CREATE TABLE t (a CHAR(-1))", ); sqlite().one_statement_parses_to( "CREATE TABLE t (a CHAR(-1,-2))", "CREATE TABLE t (a CHAR(-1, -2))", ); sqlite().verified_stmt("SELECT abs(-1), round(1, 1.5), char(+1, -2)"); // plain unsigned integer keeps the normal AST let Statement::CreateTable(CreateTable { columns, .. }) = sqlite().verified_stmt("CREATE TABLE t (a CHAR(10))") else { panic!("expected a CREATE TABLE statement"); }; assert_eq!( columns[0].data_type, DataType::Char(Some(CharacterLength::IntegerLength { length: 10, unit: None, })) ); sqlite().verified_stmt("CREATE TABLE t (a INT(10))"); // Oversized precision renders with a space after the comma sqlite().one_statement_parses_to( "SELECT CAST(a AS DECIMAL(99999999999999999999999,2))", "SELECT CAST(a AS DECIMAL(99999999999999999999999, 2))", ); // SQLite's typetoken grammar allows at most two modifiers assert_eq!( sqlite().parse_sql_statements("CREATE TABLE t (a CHAR(1.5, 2, 3))"), Err(ParserError::ParserError( "Could not parse '1.5' as u64: invalid digit found in string".to_string() )) ); for (sql, error) in [ ( "CREATE TABLE t (a CHAR(1.5,))", "Could not parse '1.5' as u64: invalid digit found in string", ), ( "CREATE TABLE t (a CHAR(+-1))", "Expected: literal int, found: +", ), ( "CREATE TABLE t (a CHAR(NULL))", "Expected: literal int, found: NULL", ), ] { assert_eq!( sqlite().parse_sql_statements(sql), Err(ParserError::ParserError(error.to_string())), "{sql}", ); } // other dialects must still reject these assert_eq!( all_dialects_except(|d| d.is::()) .parse_sql_statements("CREATE TABLE t (a CHAR(+10))"), Err(ParserError::ParserError( "Expected: literal int, found: +".to_string() )) ); } #[test] fn parse_keyword_column_aliases() { for kw in [ "analyze", "cluster", "distribute", "end", "exclude", "explain", "fetch", "lateral", "minus", "offset", "sort", "top", "view", "with", ] { sqlite().one_statement_parses_to( &format!("SELECT a {kw} FROM t"), &format!("SELECT a AS {kw} FROM t"), ); sqlite().one_statement_parses_to(&format!("SELECT 1 {kw}"), &format!("SELECT 1 AS {kw}")); } for (sql, error) in [ ("SELECT 1 WHERE", "Expected: an expression, found: EOF"), ("SELECT 1 GROUP", "Expected: end of statement, found: GROUP"), ] { assert_eq!( sqlite().parse_sql_statements(sql), Err(ParserError::ParserError(error.to_string())), "{sql}" ); } assert_eq!( TestedDialects::new(vec![Box::new(GenericDialect {})]) .parse_sql_statements("SELECT a view FROM t"), Err(ParserError::ParserError( "Expected: end of statement, found: view".to_string() )) ); } #[test] fn parse_n_prefix_not_national_string() { // In SQLite, `n'...'` is the identifier `n` followed by a string literal. // The string becomes an implicit alias, so `t.n''` round-trips as `t.n AS ''`. sqlite().one_statement_parses_to("SELECT t.n'' FROM t", "SELECT t.n AS '' FROM t"); sqlite().one_statement_parses_to("SELECT n'' FROM t", "SELECT n AS '' FROM t"); sqlite().one_statement_parses_to("SELECT N'hello'", "SELECT N AS 'hello'"); // Other dialects still tokenize N'...' as a national string literal. all_dialects_where(|d| d.supports_national_string_literal()).verified_stmt("SELECT N'hello'"); } #[test] fn parse_create_table_options_list() { use SqliteTableOption::{Strict, WithoutRowid}; for (suffix, leading_comma, options) in [ (" WITHOUT ROWID, STRICT", false, vec![WithoutRowid, Strict]), (" STRICT, WITHOUT ROWID", false, vec![Strict, WithoutRowid]), (" STRICT, STRICT", false, vec![Strict, Strict]), (", STRICT", true, vec![Strict]), (", WITHOUT ROWID, STRICT", true, vec![WithoutRowid, Strict]), ] { let sql = format!("CREATE TABLE t (a INT){suffix}"); match sqlite_and_generic().verified_stmt(&sql) { Statement::CreateTable(CreateTable { sqlite_table_options, .. }) => assert_eq!( sqlite_table_options, SqliteTableOptions { leading_comma, options }, "{sql}" ), _ => unreachable!(), } } sqlite_and_generic().one_statement_parses_to( "CREATE TABLE t (a INT) , STRICT", "CREATE TABLE t (a INT), STRICT", ); for (suffix, error) in [ ( " WITHOUT ROWID STRICT", "Expected: end of statement, found: STRICT", ), ( " STRICT WITHOUT ROWID", "Expected: end of statement, found: WITHOUT", ), (" STRICT,", "Expected: WITHOUT ROWID or STRICT, found: EOF"), (" STRICT, WITHOUT", "Expected: ROWID, found: EOF"), (",", "Expected: end of statement, found: ,"), (", , STRICT", "Expected: end of statement, found: ,"), ] { let sql = format!("CREATE TABLE t (a INT){suffix}"); let actual = sqlite_and_generic().parse_sql_statements(&sql).unwrap_err(); assert!(actual.to_string().contains(error), "{sql}: {actual}"); } } #[test] fn parse_comma_join_constraint() { let dialects = all_dialects_where(|d| d.supports_comma_join_constraint()); for (sql, canonical) in [ ( "SELECT * FROM t, u ON t.a = u.a", "SELECT * FROM t JOIN u ON t.a = u.a", ), ( "SELECT * FROM t, u USING (a)", "SELECT * FROM t JOIN u USING(a)", ), ( "SELECT * FROM t, u ON t.a = u.a, v ON v.a = u.a", "SELECT * FROM t JOIN u ON t.a = u.a JOIN v ON v.a = u.a", ), ( "SELECT * FROM t, u ON t.a = u.a JOIN v ON v.a = u.a", "SELECT * FROM t JOIN u ON t.a = u.a JOIN v ON v.a = u.a", ), ( "UPDATE t SET a = 1 FROM u, v ON u.a = v.a", "UPDATE t SET a = 1 FROM u JOIN v ON u.a = v.a", ), ] { dialects.one_statement_parses_to(sql, canonical); } dialects.verified_stmt("SELECT * FROM t, u"); assert_eq!( all_dialects_where(|d| !d.supports_comma_join_constraint()) .parse_sql_statements("SELECT * FROM t, u ON t.a = u.a"), Err(ParserError::ParserError( "Expected: end of statement, found: ON".to_string() )) ); // Malformed inputs after a comma join constraint for (sql, error) in [ ( "SELECT * FROM t, u ON", "Expected: an expression, found: EOF", ), ( "SELECT * FROM t, u USING a", "Expected: a list of columns in parentheses, found: a", ), ( "SELECT * FROM t, u ON t.a = u.a CROSS", "Expected: JOIN or APPLY after CROSS, found: EOF", ), ] { assert_eq!( dialects.parse_sql_statements(sql), Err(ParserError::ParserError(error.to_string())) ); } } #[test] fn parse_parenthesized_table_factor() { for (sql, canonical) in [ ("SELECT * FROM (t)", "SELECT * FROM t"), ("SELECT * FROM ((t))", "SELECT * FROM t"), ("SELECT * FROM (t) AS a", "SELECT * FROM t AS a"), ("SELECT * FROM (t AS a)", "SELECT * FROM t AS a"), ( "SELECT * FROM ((SELECT 1) AS s)", "SELECT * FROM (SELECT 1) AS s", ), ("SELECT * FROM f(), (t)", "SELECT * FROM f(), t"), ] { sqlite_and_generic().one_statement_parses_to(sql, canonical); } assert_eq!( all_dialects_where(|d| !d.supports_parens_around_table_factor()) .parse_sql_statements("SELECT * FROM (t)"), Err(ParserError::ParserError( "Expected: joined table, found: )".to_string() )) ); } #[test] fn parse_indexed_by() { let index_directives = |relation: &TableFactor| match relation { TableFactor::Table { index_directives, .. } => index_directives.clone(), _ => panic!("expected TableFactor::Table"), }; let select = sqlite().verified_only_select("SELECT * FROM t INDEXED BY i WHERE a = 1"); assert_eq!( index_directives(&select.from[0].relation), vec![TableIndexDirective::IndexedBy(Ident::new("i"))] ); for sql in [ "SELECT * FROM t NOT INDEXED", "SELECT * FROM t AS x NOT INDEXED", ] { let select = sqlite().verified_only_select(sql); assert_eq!( index_directives(&select.from[0].relation), vec![TableIndexDirective::NotIndexed] ); } let select = sqlite().verified_only_select("SELECT * FROM t INDEXED BY i, t AS u NOT INDEXED"); assert_eq!( index_directives(&select.from[0].relation), vec![TableIndexDirective::IndexedBy(Ident::new("i"))] ); assert_eq!( index_directives(&select.from[1].relation), vec![TableIndexDirective::NotIndexed] ); sqlite().verified_stmt("SELECT * FROM t AS x INDEXED BY i WHERE a = 1"); sqlite().verified_stmt("DELETE FROM t INDEXED BY i WHERE a = 1"); sqlite().verified_stmt("UPDATE t INDEXED BY i SET a = 2 WHERE a = 1"); assert_eq!( all_dialects_where(|d| !d.supports_indexed_by()) .parse_sql_statements("SELECT * FROM t INDEXED BY i"), Err(ParserError::ParserError( "Expected: end of statement, found: BY".to_string() )) ); } #[test] fn display_index_directives_propagates_formatter_errors() { use core::fmt::{self, Write}; struct RejectingWriter(char); impl Write for RejectingWriter { fn write_str(&mut self, value: &str) -> fmt::Result { if value.contains(self.0) { Err(fmt::Error) } else { Ok(()) } } } for (sql, rejected) in [ ("SELECT * FROM t INDEXED BY idx_b", 'b'), ("SELECT * FROM t NOT INDEXED", 'N'), ] { let select = sqlite().verified_only_select(sql); let mut writer = RejectingWriter(rejected); assert_eq!( write!(writer, "{}", select.from[0].relation), Err(fmt::Error) ); } } #[test] fn parse_unterminated_block_comment() { sqlite().one_statement_parses_to("SELECT 1 /* x", "SELECT 1"); sqlite().one_statement_parses_to("SELECT 1 /* x;", "SELECT 1"); assert_eq!(sqlite().parse_sql_statements("/* ;"), Ok(vec![])); assert_eq!( all_dialects_where(|d| !d.supports_unterminated_block_comments()) .parse_sql_statements("SELECT 1 /* x"), Err(ParserError::TokenizerError( "Unexpected EOF while in a multi-line comment at Line: 1, Column: 14".to_string() )) ); } #[test] fn parse_over_as_column_alias_after_function() { // In SQLite, OVER is a fallback identifier so `f() OVER` aliases the result as `over`. sqlite().one_statement_parses_to("SELECT f() OVER", "SELECT f() AS OVER"); // A keyword window name is not mistaken for an alias candidate. sqlite().verified_stmt("SELECT f() OVER partition"); // Window function syntax still works when OVER is followed by `(`. sqlite().verified_stmt("SELECT f() OVER ()"); // A quoted string names the window, so OVER cannot be an alias for it sqlite().verified_stmt("SELECT f() OVER 'partition'"); sqlite().verified_stmt(r#"SELECT f() OVER "partition""#); // Other dialects keep the greedy OVER parse and reject the bare form. let generic = TestedDialects::new(vec![Box::new(GenericDialect {})]); assert_eq!( generic.parse_sql_statements("SELECT f() OVER"), Err(ParserError::ParserError( "Expected: identifier, found: EOF".to_string() )) ); } #[test] fn parse_on_conflict_target_predicate() { // SQLite's upsert target takes the same `( indexed-column, ... ) WHERE expr` as PostgreSQL's. // See let sql = "INSERT INTO t VALUES (1, 2, 3) ON CONFLICT(a) WHERE b > 0 DO NOTHING"; match sqlite().verified_stmt(sql) { Statement::Insert(Insert { on: Some(OnInsert::OnConflict(OnConflict { conflict_target: Some(ConflictTarget::Columns { columns, predicate: Some(predicate), }), action: OnConflictAction::DoNothing, })), .. }) => { assert_eq!(columns, vec![Ident::new("a")]); assert_eq!(predicate.to_string(), "b > 0"); } other => panic!("unexpected statement: {other:?}"), } } fn sqlite() -> TestedDialects { TestedDialects::new(vec![Box::new(SQLiteDialect {})]) } fn sqlite_with_options(options: ParserOptions) -> TestedDialects { TestedDialects::new_with_options(vec![Box::new(SQLiteDialect {})], options) } fn sqlite_and_generic() -> TestedDialects { TestedDialects::new(vec![ Box::new(SQLiteDialect {}), Box::new(GenericDialect {}), ]) } #[test] fn test_dollar_sign_in_identifier() { // SQLite allows $ anywhere after the first character of an identifier sqlite().verified_stmt("SELECT a$ FROM t"); sqlite().verified_stmt("SELECT 1 AS a$"); sqlite().one_statement_parses_to("CREATE TABLE t(a$ INT)", "CREATE TABLE t (a$ INT)"); sqlite().verified_stmt("INSERT INTO t$ VALUES (1)"); // AS$ is parsed as the alias name sqlite().one_statement_parses_to("SELECT a AS$", "SELECT a AS AS$"); // Dialects without $ as an identifier character still reject these identifiers TestedDialects::new(vec![Box::new(AnsiDialect {})]) .parse_sql_statements("SELECT a$ FROM t") .unwrap_err(); } #[test] fn parse_comparison_binds_tighter_than_equality() { let id = |name: &str| Expr::Identifier(Ident::new(name)); let binary = |left: Expr, op: BinaryOperator, right: Expr| Expr::BinaryOp { left: Box::new(left), op, right: Box::new(right), }; for (sql, eq_op, cmp_op) in [ ("a = b < c", BinaryOperator::Eq, BinaryOperator::Lt), ("a <> b <= c", BinaryOperator::NotEq, BinaryOperator::LtEq), ("a = b > c", BinaryOperator::Eq, BinaryOperator::Gt), ("a <> b >= c", BinaryOperator::NotEq, BinaryOperator::GtEq), ] { assert_eq!( sqlite().verified_expr(sql), binary(id("a"), eq_op, binary(id("b"), cmp_op, id("c"))), "{sql}" ); } assert_eq!( sqlite().verified_expr("a < b = c"), binary( binary(id("a"), BinaryOperator::Lt, id("b")), BinaryOperator::Eq, id("c") ), ); // `IN` and `BETWEEN` sit on the level of `=` assert_eq!( sqlite().verified_expr("a < b IN (1)"), Expr::In { expr: Box::new(binary(id("a"), BinaryOperator::Lt, id("b"))), target: InTarget::List(vec![Expr::value(number("1"))]), negated: false, global: false, }, ); assert_eq!( sqlite().verified_expr("a BETWEEN b AND c < d"), Expr::Between { expr: Box::new(id("a")), negated: false, low: Box::new(id("b")), high: Box::new(binary(id("c"), BinaryOperator::Lt, id("d"))), }, ); // Other dialects keep one level for every comparison assert_eq!( all_dialects_except(|d| d.is::()).verified_expr("a = b < c"), binary( binary(id("a"), BinaryOperator::Eq, id("b")), BinaryOperator::Lt, id("c") ), ); } #[test] fn parse_rejects_star_qualified_wildcard_in_boolean_context() { assert_eq!( sqlite().parse_sql_statements("SELECT * FROM t WHERE a.*"), Err(ParserError::ParserError( "Expected: end of statement, found: .".to_string() )), ); } #[test] fn parse_bitwise_and_or_share_precedence() { fn num(n: &str) -> Expr { Expr::value(number(n)) } fn bin(left: Expr, op: BinaryOperator, right: Expr) -> Expr { Expr::BinaryOp { left: Box::new(left), op, right: Box::new(right), } } use BinaryOperator::{BitwiseAnd, BitwiseOr, Eq, Multiply, Plus}; for (sql, expected) in [ ( "6 | 1 & 3", bin(bin(num("6"), BitwiseOr, num("1")), BitwiseAnd, num("3")), ), ( "6 & 3 | 8", bin(bin(num("6"), BitwiseAnd, num("3")), BitwiseOr, num("8")), ), ( "1 & 2 & 3", bin(bin(num("1"), BitwiseAnd, num("2")), BitwiseAnd, num("3")), ), ( "1 | 2 | 3", bin(bin(num("1"), BitwiseOr, num("2")), BitwiseOr, num("3")), ), ( "1 | 2 + 3 & 4 * 5", bin( bin(num("1"), BitwiseOr, bin(num("2"), Plus, num("3"))), BitwiseAnd, bin(num("4"), Multiply, num("5")), ), ), ( "6 | (1 & 3)", bin( num("6"), BitwiseOr, Expr::Nested(Box::new(bin(num("1"), BitwiseAnd, num("3")))), ), ), ( "1 | 2 = 3 & 4", bin( bin(num("1"), BitwiseOr, num("2")), Eq, bin(num("3"), BitwiseAnd, num("4")), ), ), ] { assert_eq!(sqlite().verified_expr(sql), expected, "{sql}"); } } #[test] fn parse_bitwise_shift_operators() { sqlite().verified_stmt("SELECT a << 1 FROM t"); sqlite().verified_stmt("SELECT a >> 1 FROM t"); let unsupported = all_dialects_where(|d| !d.supports_bitwise_shift_operators()); for (sql, token) in [ ("SELECT a << 1 FROM t", "ShiftLeft"), ("SELECT a >> 1 FROM t", "ShiftRight"), ] { assert_eq!( unsupported.parse_sql_statements(sql), Err(ParserError::ParserError(format!( "No infix parser for token {token}" ))), "{sql}" ); } } #[test] fn parse_left_shift_and_bitwise_and_are_left_associative() { assert_eq!( sqlite().verified_expr("1 << 2 & 4"), Expr::BinaryOp { left: Box::new(Expr::BinaryOp { left: Box::new(Expr::value(number("1"))), op: BinaryOperator::PGBitwiseShiftLeft, right: Box::new(Expr::value(number("2"))), }), op: BinaryOperator::BitwiseAnd, right: Box::new(Expr::value(number("4"))), } ); } #[test] fn parse_right_shift_and_bitwise_and_are_left_associative() { assert_eq!( sqlite().verified_expr("8 >> 1 & 2"), Expr::BinaryOp { left: Box::new(Expr::BinaryOp { left: Box::new(Expr::value(number("8"))), op: BinaryOperator::PGBitwiseShiftRight, right: Box::new(Expr::value(number("1"))), }), op: BinaryOperator::BitwiseAnd, right: Box::new(Expr::value(number("2"))), } ); } #[test] fn parse_bitwise_shifts_share_precedence() { use BinaryOperator::{BitwiseAnd, BitwiseOr, PGBitwiseShiftLeft, PGBitwiseShiftRight}; for (sql, first, second) in [ ("a | b << c", BitwiseOr, PGBitwiseShiftLeft), ("a | b >> c", BitwiseOr, PGBitwiseShiftRight), ("a << b | c", PGBitwiseShiftLeft, BitwiseOr), ("a >> b | c", PGBitwiseShiftRight, BitwiseOr), ("a & b << c", BitwiseAnd, PGBitwiseShiftLeft), ("a & b >> c", BitwiseAnd, PGBitwiseShiftRight), ("a << b >> c", PGBitwiseShiftLeft, PGBitwiseShiftRight), ("a >> b << c", PGBitwiseShiftRight, PGBitwiseShiftLeft), ] { assert_eq!( sqlite().verified_expr(sql), Expr::BinaryOp { left: Box::new(Expr::BinaryOp { left: Box::new(Expr::Identifier(Ident::new("a"))), op: first, right: Box::new(Expr::Identifier(Ident::new("b"))), }), op: second, right: Box::new(Expr::Identifier(Ident::new("c"))), }, "{sql}" ); } } #[test] fn parse_bitwise_shift_precedence_boundaries() { let id = |name| Expr::Identifier(Ident::new(name)); let bin = |left, op, right| Expr::BinaryOp { left: Box::new(left), op, right: Box::new(right), }; use BinaryOperator::{ BitwiseAnd, Divide, Eq, Multiply, PGBitwiseShiftLeft, PGBitwiseShiftRight, Plus, }; for (sql, expected) in [ ( "a << b + c", bin(id("a"), PGBitwiseShiftLeft, bin(id("b"), Plus, id("c"))), ), ( "a + b << c", bin(bin(id("a"), Plus, id("b")), PGBitwiseShiftLeft, id("c")), ), ( "a >> b * c", bin( id("a"), PGBitwiseShiftRight, bin(id("b"), Multiply, id("c")), ), ), ( "a / b >> c", bin(bin(id("a"), Divide, id("b")), PGBitwiseShiftRight, id("c")), ), ( "a << b = c", bin(bin(id("a"), PGBitwiseShiftLeft, id("b")), Eq, id("c")), ), ( "a = b >> c", bin(id("a"), Eq, bin(id("b"), PGBitwiseShiftRight, id("c"))), ), ( "a << (b & c)", bin( id("a"), PGBitwiseShiftLeft, Expr::Nested(Box::new(bin(id("b"), BitwiseAnd, id("c")))), ), ), ] { assert_eq!(sqlite().verified_expr(sql), expected, "{sql}"); } } #[test] fn parse_create_table_string_literal_column_with_dangling_constraint_name() { assert_eq!( sqlite() .parse_sql_statements("CREATE TABLE t ('a' CONSTRAINT c)") .unwrap_err() .to_string(), "sql parser error: Expected: constraint details after CONSTRAINT , found: )" ); } #[test] fn parse_create_table_on_conflict_table_constraint() { for keyword in [ Keyword::ROLLBACK, Keyword::ABORT, Keyword::FAIL, Keyword::IGNORE, Keyword::REPLACE, ] { let sql = format!( "CREATE TABLE t (a INT, b INT, PRIMARY KEY (a) ON CONFLICT {keyword:?}, CONSTRAINT u UNIQUE (b) ON CONFLICT {keyword:?}, CHECK (a > b) ON CONFLICT {keyword:?})" ); let Statement::CreateTable(CreateTable { constraints, .. }) = sqlite_and_generic().verified_stmt(&sql) else { unreachable!() }; let [TableConstraint::PrimaryKey(primary_key), TableConstraint::Unique(unique), TableConstraint::Check(check)] = constraints.as_slice() else { panic!("unexpected constraints: {constraints:?}") }; assert_eq!(primary_key.on_conflict, Some(keyword)); assert_eq!(unique.on_conflict, Some(keyword)); assert_eq!(check.on_conflict, Some(keyword)); } sqlite().verified_stmt( "CREATE TABLE c (x, y, z GENERATED ALWAYS AS (x + y) VIRTUAL, PRIMARY KEY (x, y) ON CONFLICT ABORT) WITHOUT ROWID", ); } #[test] fn parse_create_table_on_conflict_table_constraint_err() { assert_eq!( sqlite_and_generic() .parse_sql_statements("CREATE TABLE t (a INT, UNIQUE (a) ON CONFLICT BOH)") .unwrap_err(), ParserError::ParserError( "Expected: one of ROLLBACK or ABORT or FAIL or IGNORE or REPLACE, found: BOH" .to_string() ) ); let unsupported = all_dialects_where(|d| !d.supports_constraint_conflict_clause()); for sql in [ "CREATE TABLE t (a INT, PRIMARY KEY (a) ON CONFLICT IGNORE)", "CREATE TABLE t (a INT, UNIQUE (a) ON CONFLICT IGNORE)", "CREATE TABLE t (a INT, CHECK (a > 0) ON CONFLICT IGNORE)", ] { assert!(unsupported.parse_sql_statements(sql).is_err(), "{sql}"); } } #[test] fn parse_create_virtual_table_module_arguments_round_trip() { for sql in [ "CREATE VIRTUAL TABLE ft USING fts5 (body, tokenize = 'porter')", "CREATE VIRTUAL TABLE ft USING fts5 (a, b UNINDEXED)", "CREATE VIRTUAL TABLE ft USING fts5 (a, content='')", "CREATE VIRTUAL TABLE ft USING fts5 (a, content='it''s')", r#"CREATE VIRTUAL TABLE t USING m ("a""b", [c d], `e`)"#, "CREATE VIRTUAL TABLE c USING csv (filename=data.csv)", "CREATE VIRTUAL TABLE r USING rtree (id, minX, maxX)", "CREATE VIRTUAL TABLE t USING m (a; b)", ] { sqlite_and_generic().verified_stmt(sql); } } #[test] fn parse_create_virtual_table_module_arguments_without_unescape() { sqlite_with_options(ParserOptions::new().with_unescape(false)) .verified_stmt("CREATE VIRTUAL TABLE ft USING fts5 (a, content='it''s')"); } #[test] fn parse_create_virtual_table_module_arguments_double_quoted_string() { TestedDialects::new(vec![Box::new(MySqlDialect {})]) .verified_stmt(r#"CREATE VIRTUAL TABLE t USING m ("a""b")"#); } #[test] fn parse_create_virtual_table_module_arguments_split_on_top_level_commas() { let sql = "CREATE VIRTUAL TABLE t USING m (f(x, (y)), tokenize = 'p,q')"; let statement = Parser::parse_sql(&SQLiteDialect {}, sql).unwrap().remove(0); assert_eq!( Span::new(Location::new(1, 22), Location::new(1, 60)), statement.span() ); let Statement::CreateVirtualTable { module_args, .. } = statement else { unreachable!() }; assert_eq!( vec!["f(x, (y))", "tokenize = 'p,q'"], module_args .iter() .map(|arg| arg.value.as_str()) .collect::>() ); assert_eq!( Span::new(Location::new(1, 44), Location::new(1, 60)), module_args[1].span ); sqlite_and_generic().verified_stmt(sql); } #[test] fn parse_create_virtual_table_module_arguments_ignore_spans_in_comparison() { let sql = "CREATE VIRTUAL TABLE t USING m (tokenize = 'porter')"; let located = Parser::parse_sql(&SQLiteDialect {}, sql).unwrap().remove(0); let unlocated = sqlite_and_generic().verified_stmt(sql); let ( Statement::CreateVirtualTable { module_args: located_args, .. }, Statement::CreateVirtualTable { module_args: unlocated_args, .. }, ) = (&located, &unlocated) else { unreachable!() }; assert_ne!(located_args[0].span, unlocated_args[0].span); assert_eq!(located_args, unlocated_args); assert_eq!( Some(std::cmp::Ordering::Equal), located_args[0].partial_cmp(&unlocated_args[0]) ); assert_eq!(std::cmp::Ordering::Equal, located.cmp(&unlocated)); let hash = |statement: &Statement| { let mut hasher = std::collections::hash_map::DefaultHasher::new(); std::hash::Hash::hash(statement, &mut hasher); std::hash::Hasher::finish(&hasher) }; assert_eq!(hash(&located), hash(&unlocated)); } #[test] fn parse_create_virtual_table_module_arguments_drop_empty() { sqlite_and_generic().one_statement_parses_to( "CREATE VIRTUAL TABLE t USING m( a ,, b )", "CREATE VIRTUAL TABLE t USING m (a, b)", ); sqlite_and_generic().one_statement_parses_to( "CREATE VIRTUAL TABLE t USING m()", "CREATE VIRTUAL TABLE t USING m", ); } #[test] fn parse_create_virtual_table_module_arguments_unbalanced_err() { for (sql, error) in [ ( "CREATE VIRTUAL TABLE t USING m (a", "Expected: ), found: EOF", ), ( "CREATE VIRTUAL TABLE t USING m (f(a)", "Expected: ), found: EOF", ), ( "CREATE VIRTUAL TABLE t USING m (a))", "Expected: end of statement, found: )", ), ] { assert_eq!( ParserError::ParserError(error.to_string()), sqlite_and_generic().parse_sql_statements(sql).unwrap_err() ); } }