Implement Spanned to retrieve source locations on AST nodes (#1435)

Co-authored-by: Ifeanyi Ubah <ify1992@yahoo.com>
Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
This commit is contained in:
Mark-Oliver Junge 2024-11-26 17:22:30 +01:00 committed by GitHub
parent 0adec33b94
commit 3c8fd74804
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 3092 additions and 399 deletions

View file

@ -19,12 +19,14 @@
//! Test SQL syntax specific to MySQL. The parser based on the generic dialect
//! is also tested (on the inputs it can handle).
use helpers::attached_token::AttachedToken;
use matches::assert_matches;
use sqlparser::ast::MysqlInsertPriority::{Delayed, HighPriority, LowPriority};
use sqlparser::ast::*;
use sqlparser::dialect::{GenericDialect, MySqlDialect};
use sqlparser::parser::{ParserError, ParserOptions};
use sqlparser::tokenizer::Span;
use sqlparser::tokenizer::Token;
use test_utils::*;
@ -142,16 +144,19 @@ fn parse_flush() {
ObjectName(vec![
Ident {
value: "mek".to_string(),
quote_style: Some('`')
quote_style: Some('`'),
span: Span::empty(),
},
Ident {
value: "table1".to_string(),
quote_style: Some('`')
quote_style: Some('`'),
span: Span::empty(),
}
]),
ObjectName(vec![Ident {
value: "table2".to_string(),
quote_style: None
quote_style: None,
span: Span::empty(),
}])
]
}
@ -179,16 +184,19 @@ fn parse_flush() {
ObjectName(vec![
Ident {
value: "mek".to_string(),
quote_style: Some('`')
quote_style: Some('`'),
span: Span::empty(),
},
Ident {
value: "table1".to_string(),
quote_style: Some('`')
quote_style: Some('`'),
span: Span::empty(),
}
]),
ObjectName(vec![Ident {
value: "table2".to_string(),
quote_style: None
quote_style: None,
span: Span::empty(),
}])
]
}
@ -205,16 +213,19 @@ fn parse_flush() {
ObjectName(vec![
Ident {
value: "mek".to_string(),
quote_style: Some('`')
quote_style: Some('`'),
span: Span::empty(),
},
Ident {
value: "table1".to_string(),
quote_style: Some('`')
quote_style: Some('`'),
span: Span::empty(),
}
]),
ObjectName(vec![Ident {
value: "table2".to_string(),
quote_style: None
quote_style: None,
span: Span::empty(),
}])
]
}
@ -1058,12 +1069,14 @@ fn parse_escaped_quote_identifiers_with_escape() {
Statement::Query(Box::new(Query {
with: None,
body: Box::new(SetExpr::Select(Box::new(Select {
select_token: AttachedToken::empty(),
distinct: None,
top: None,
top_before_distinct: false,
projection: vec![SelectItem::UnnamedExpr(Expr::Identifier(Ident {
value: "quoted ` identifier".into(),
quote_style: Some('`'),
span: Span::empty(),
}))],
into: None,
from: vec![],
@ -1109,12 +1122,14 @@ fn parse_escaped_quote_identifiers_with_no_escape() {
Statement::Query(Box::new(Query {
with: None,
body: Box::new(SetExpr::Select(Box::new(Select {
select_token: AttachedToken::empty(),
distinct: None,
top: None,
top_before_distinct: false,
projection: vec![SelectItem::UnnamedExpr(Expr::Identifier(Ident {
value: "quoted `` identifier".into(),
quote_style: Some('`'),
span: Span::empty(),
}))],
into: None,
from: vec![],
@ -1153,12 +1168,15 @@ fn parse_escaped_backticks_with_escape() {
Statement::Query(Box::new(Query {
with: None,
body: Box::new(SetExpr::Select(Box::new(Select {
select_token: AttachedToken::empty(),
distinct: None,
top: None,
top_before_distinct: false,
projection: vec![SelectItem::UnnamedExpr(Expr::Identifier(Ident {
value: "`quoted identifier`".into(),
quote_style: Some('`'),
span: Span::empty(),
}))],
into: None,
from: vec![],
@ -1201,12 +1219,15 @@ fn parse_escaped_backticks_with_no_escape() {
Statement::Query(Box::new(Query {
with: None,
body: Box::new(SetExpr::Select(Box::new(Select {
select_token: AttachedToken::empty(),
distinct: None,
top: None,
top_before_distinct: false,
projection: vec![SelectItem::UnnamedExpr(Expr::Identifier(Ident {
value: "``quoted identifier``".into(),
quote_style: Some('`'),
span: Span::empty(),
}))],
into: None,
from: vec![],
@ -1846,6 +1867,8 @@ fn parse_select_with_numeric_prefix_column_name() {
assert_eq!(
q.body,
Box::new(SetExpr::Select(Box::new(Select {
select_token: AttachedToken::empty(),
distinct: None,
top: None,
top_before_distinct: false,
@ -1902,6 +1925,8 @@ fn parse_select_with_concatenation_of_exp_number_and_numeric_prefix_column() {
assert_eq!(
q.body,
Box::new(SetExpr::Select(Box::new(Select {
select_token: AttachedToken::empty(),
distinct: None,
top: None,
top_before_distinct: false,
@ -2055,7 +2080,8 @@ fn parse_delete_with_order_by() {
vec![OrderByExpr {
expr: Expr::Identifier(Ident {
value: "id".to_owned(),
quote_style: None
quote_style: None,
span: Span::empty(),
}),
asc: Some(false),
nulls_first: None,
@ -2136,7 +2162,8 @@ fn parse_alter_table_add_column() {
},
column_position: Some(MySQLColumnPosition::After(Ident {
value: String::from("foo"),
quote_style: None
quote_style: None,
span: Span::empty(),
})),
},]
);
@ -2187,6 +2214,7 @@ fn parse_alter_table_add_columns() {
column_position: Some(MySQLColumnPosition::After(Ident {
value: String::from("foo"),
quote_style: None,
span: Span::empty(),
})),
},
]
@ -2247,6 +2275,7 @@ fn parse_alter_table_change_column() {
column_position: Some(MySQLColumnPosition::After(Ident {
value: String::from("foo"),
quote_style: None,
span: Span::empty(),
})),
};
let sql4 = "ALTER TABLE orders CHANGE COLUMN description desc TEXT NOT NULL AFTER foo";
@ -2286,6 +2315,7 @@ fn parse_alter_table_change_column_with_column_position() {
column_position: Some(MySQLColumnPosition::After(Ident {
value: String::from("total_count"),
quote_style: None,
span: Span::empty(),
})),
};
@ -2342,6 +2372,7 @@ fn parse_alter_table_modify_column() {
column_position: Some(MySQLColumnPosition::After(Ident {
value: String::from("foo"),
quote_style: None,
span: Span::empty(),
})),
};
let sql4 = "ALTER TABLE orders MODIFY COLUMN description TEXT NOT NULL AFTER foo";
@ -2379,6 +2410,7 @@ fn parse_alter_table_modify_column_with_column_position() {
column_position: Some(MySQLColumnPosition::After(Ident {
value: String::from("total_count"),
quote_style: None,
span: Span::empty(),
})),
};
@ -2397,6 +2429,8 @@ fn parse_alter_table_modify_column_with_column_position() {
#[test]
fn parse_substring_in_select() {
use sqlparser::tokenizer::Span;
let sql = "SELECT DISTINCT SUBSTRING(description, 0, 1) FROM test";
match mysql().one_statement_parses_to(
sql,
@ -2407,13 +2441,15 @@ fn parse_substring_in_select() {
Box::new(Query {
with: None,
body: Box::new(SetExpr::Select(Box::new(Select {
select_token: AttachedToken::empty(),
distinct: Some(Distinct::Distinct),
top: None,
top_before_distinct: false,
projection: vec![SelectItem::UnnamedExpr(Expr::Substring {
expr: Box::new(Expr::Identifier(Ident {
value: "description".to_string(),
quote_style: None
quote_style: None,
span: Span::empty(),
})),
substring_from: Some(Box::new(Expr::Value(number("0")))),
substring_for: Some(Box::new(Expr::Value(number("1")))),
@ -2424,7 +2460,8 @@ fn parse_substring_in_select() {
relation: TableFactor::Table {
name: ObjectName(vec![Ident {
value: "test".to_string(),
quote_style: None
quote_style: None,
span: Span::empty(),
}]),
alias: None,
args: None,
@ -2730,6 +2767,7 @@ fn parse_hex_string_introducer() {
Statement::Query(Box::new(Query {
with: None,
body: Box::new(SetExpr::Select(Box::new(Select {
select_token: AttachedToken::empty(),
distinct: None,
top: None,
top_before_distinct: false,