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

@ -21,6 +21,8 @@
#[macro_use]
mod test_utils;
use helpers::attached_token::AttachedToken;
use sqlparser::tokenizer::Span;
use test_utils::*;
use sqlparser::ast::*;
@ -1163,6 +1165,7 @@ fn parse_copy_to() {
source: CopySource::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,
@ -1172,6 +1175,7 @@ fn parse_copy_to() {
alias: Ident {
value: "a".into(),
quote_style: None,
span: Span::empty(),
},
},
SelectItem::ExprWithAlias {
@ -1179,6 +1183,7 @@ fn parse_copy_to() {
alias: Ident {
value: "b".into(),
quote_style: None,
span: Span::empty(),
},
}
],
@ -1318,7 +1323,8 @@ fn parse_set() {
variables: OneOrManyWithParens::One(ObjectName(vec![Ident::new("a")])),
value: vec![Expr::Identifier(Ident {
value: "b".into(),
quote_style: None
quote_style: None,
span: Span::empty(),
})],
}
);
@ -1380,7 +1386,8 @@ fn parse_set() {
])),
value: vec![Expr::Identifier(Ident {
value: "b".into(),
quote_style: None
quote_style: None,
span: Span::empty(),
})],
}
);
@ -1452,6 +1459,7 @@ fn parse_set_role() {
role_name: Some(Ident {
value: "rolename".to_string(),
quote_style: Some('\"'),
span: Span::empty(),
}),
}
);
@ -1466,6 +1474,7 @@ fn parse_set_role() {
role_name: Some(Ident {
value: "rolename".to_string(),
quote_style: Some('\''),
span: Span::empty(),
}),
}
);
@ -1765,7 +1774,8 @@ fn parse_pg_on_conflict() {
selection: Some(Expr::BinaryOp {
left: Box::new(Expr::Identifier(Ident {
value: "dsize".to_string(),
quote_style: None
quote_style: None,
span: Span::empty(),
})),
op: BinaryOperator::Gt,
right: Box::new(Expr::Value(Value::Placeholder("$2".to_string())))
@ -1802,7 +1812,8 @@ fn parse_pg_on_conflict() {
selection: Some(Expr::BinaryOp {
left: Box::new(Expr::Identifier(Ident {
value: "dsize".to_string(),
quote_style: None
quote_style: None,
span: Span::empty(),
})),
op: BinaryOperator::Gt,
right: Box::new(Expr::Value(Value::Placeholder("$2".to_string())))
@ -2105,14 +2116,16 @@ fn parse_array_index_expr() {
subscript: Box::new(Subscript::Index {
index: Expr::Identifier(Ident {
value: "baz".to_string(),
quote_style: Some('"')
quote_style: Some('"'),
span: Span::empty(),
})
})
}),
subscript: Box::new(Subscript::Index {
index: Expr::Identifier(Ident {
value: "fooz".to_string(),
quote_style: Some('"')
quote_style: Some('"'),
span: Span::empty(),
})
})
},
@ -2504,6 +2517,7 @@ fn parse_array_subquery_expr() {
op: SetOperator::Union,
set_quantifier: SetQuantifier::None,
left: Box::new(SetExpr::Select(Box::new(Select {
select_token: AttachedToken::empty(),
distinct: None,
top: None,
top_before_distinct: false,
@ -2525,6 +2539,7 @@ fn parse_array_subquery_expr() {
connect_by: None,
}))),
right: Box::new(SetExpr::Select(Box::new(Select {
select_token: AttachedToken::empty(),
distinct: None,
top: None,
top_before_distinct: false,
@ -3123,6 +3138,7 @@ fn parse_custom_operator() {
left: Box::new(Expr::Identifier(Ident {
value: "relname".into(),
quote_style: None,
span: Span::empty(),
})),
op: BinaryOperator::PGCustomBinaryOperator(vec![
"database".into(),
@ -3142,6 +3158,7 @@ fn parse_custom_operator() {
left: Box::new(Expr::Identifier(Ident {
value: "relname".into(),
quote_style: None,
span: Span::empty(),
})),
op: BinaryOperator::PGCustomBinaryOperator(vec!["pg_catalog".into(), "~".into()]),
right: Box::new(Expr::Value(Value::SingleQuotedString("^(table)$".into())))
@ -3157,6 +3174,7 @@ fn parse_custom_operator() {
left: Box::new(Expr::Identifier(Ident {
value: "relname".into(),
quote_style: None,
span: Span::empty(),
})),
op: BinaryOperator::PGCustomBinaryOperator(vec!["~".into()]),
right: Box::new(Expr::Value(Value::SingleQuotedString("^(table)$".into())))
@ -3307,12 +3325,14 @@ fn parse_alter_role() {
Statement::AlterRole {
name: Ident {
value: "old_name".into(),
quote_style: None
quote_style: None,
span: Span::empty(),
},
operation: AlterRoleOperation::RenameRole {
role_name: Ident {
value: "new_name".into(),
quote_style: None
quote_style: None,
span: Span::empty(),
}
},
}
@ -3324,7 +3344,8 @@ fn parse_alter_role() {
Statement::AlterRole {
name: Ident {
value: "role_name".into(),
quote_style: None
quote_style: None,
span: Span::empty(),
},
operation: AlterRoleOperation::WithOptions {
options: vec![
@ -3353,7 +3374,8 @@ fn parse_alter_role() {
Statement::AlterRole {
name: Ident {
value: "role_name".into(),
quote_style: None
quote_style: None,
span: Span::empty(),
},
operation: AlterRoleOperation::WithOptions {
options: vec![
@ -3376,12 +3398,14 @@ fn parse_alter_role() {
Statement::AlterRole {
name: Ident {
value: "role_name".into(),
quote_style: None
quote_style: None,
span: Span::empty(),
},
operation: AlterRoleOperation::Set {
config_name: ObjectName(vec![Ident {
value: "maintenance_work_mem".into(),
quote_style: None
quote_style: None,
span: Span::empty(),
}]),
config_value: SetConfigValue::FromCurrent,
in_database: None
@ -3395,17 +3419,20 @@ fn parse_alter_role() {
[Statement::AlterRole {
name: Ident {
value: "role_name".into(),
quote_style: None
quote_style: None,
span: Span::empty(),
},
operation: AlterRoleOperation::Set {
config_name: ObjectName(vec![Ident {
value: "maintenance_work_mem".into(),
quote_style: None
quote_style: None,
span: Span::empty(),
}]),
config_value: SetConfigValue::Value(Expr::Value(number("100000"))),
in_database: Some(ObjectName(vec![Ident {
value: "database_name".into(),
quote_style: None
quote_style: None,
span: Span::empty(),
}]))
},
}]
@ -3417,17 +3444,20 @@ fn parse_alter_role() {
Statement::AlterRole {
name: Ident {
value: "role_name".into(),
quote_style: None
quote_style: None,
span: Span::empty(),
},
operation: AlterRoleOperation::Set {
config_name: ObjectName(vec![Ident {
value: "maintenance_work_mem".into(),
quote_style: None
quote_style: None,
span: Span::empty(),
}]),
config_value: SetConfigValue::Value(Expr::Value(number("100000"))),
in_database: Some(ObjectName(vec![Ident {
value: "database_name".into(),
quote_style: None
quote_style: None,
span: Span::empty(),
}]))
},
}
@ -3439,17 +3469,20 @@ fn parse_alter_role() {
Statement::AlterRole {
name: Ident {
value: "role_name".into(),
quote_style: None
quote_style: None,
span: Span::empty(),
},
operation: AlterRoleOperation::Set {
config_name: ObjectName(vec![Ident {
value: "maintenance_work_mem".into(),
quote_style: None
quote_style: None,
span: Span::empty(),
}]),
config_value: SetConfigValue::Default,
in_database: Some(ObjectName(vec![Ident {
value: "database_name".into(),
quote_style: None
quote_style: None,
span: Span::empty(),
}]))
},
}
@ -3461,7 +3494,8 @@ fn parse_alter_role() {
Statement::AlterRole {
name: Ident {
value: "role_name".into(),
quote_style: None
quote_style: None,
span: Span::empty(),
},
operation: AlterRoleOperation::Reset {
config_name: ResetConfig::ALL,
@ -3476,16 +3510,19 @@ fn parse_alter_role() {
Statement::AlterRole {
name: Ident {
value: "role_name".into(),
quote_style: None
quote_style: None,
span: Span::empty(),
},
operation: AlterRoleOperation::Reset {
config_name: ResetConfig::ConfigName(ObjectName(vec![Ident {
value: "maintenance_work_mem".into(),
quote_style: None
quote_style: None,
span: Span::empty(),
}])),
in_database: Some(ObjectName(vec![Ident {
value: "database_name".into(),
quote_style: None
quote_style: None,
span: Span::empty(),
}]))
},
}
@ -3630,7 +3667,8 @@ fn parse_drop_function() {
func_desc: vec![FunctionDesc {
name: ObjectName(vec![Ident {
value: "test_func".to_string(),
quote_style: None
quote_style: None,
span: Span::empty(),
}]),
args: None
}],
@ -3646,7 +3684,8 @@ fn parse_drop_function() {
func_desc: vec![FunctionDesc {
name: ObjectName(vec![Ident {
value: "test_func".to_string(),
quote_style: None
quote_style: None,
span: Span::empty(),
}]),
args: Some(vec![
OperateFunctionArg::with_name("a", DataType::Integer(None)),
@ -3671,7 +3710,8 @@ fn parse_drop_function() {
FunctionDesc {
name: ObjectName(vec![Ident {
value: "test_func1".to_string(),
quote_style: None
quote_style: None,
span: Span::empty(),
}]),
args: Some(vec![
OperateFunctionArg::with_name("a", DataType::Integer(None)),
@ -3689,7 +3729,8 @@ fn parse_drop_function() {
FunctionDesc {
name: ObjectName(vec![Ident {
value: "test_func2".to_string(),
quote_style: None
quote_style: None,
span: Span::empty(),
}]),
args: Some(vec![
OperateFunctionArg::with_name("a", DataType::Varchar(None)),
@ -3720,7 +3761,8 @@ fn parse_drop_procedure() {
proc_desc: vec![FunctionDesc {
name: ObjectName(vec![Ident {
value: "test_proc".to_string(),
quote_style: None
quote_style: None,
span: Span::empty(),
}]),
args: None
}],
@ -3736,7 +3778,8 @@ fn parse_drop_procedure() {
proc_desc: vec![FunctionDesc {
name: ObjectName(vec![Ident {
value: "test_proc".to_string(),
quote_style: None
quote_style: None,
span: Span::empty(),
}]),
args: Some(vec![
OperateFunctionArg::with_name("a", DataType::Integer(None)),
@ -3761,7 +3804,8 @@ fn parse_drop_procedure() {
FunctionDesc {
name: ObjectName(vec![Ident {
value: "test_proc1".to_string(),
quote_style: None
quote_style: None,
span: Span::empty(),
}]),
args: Some(vec![
OperateFunctionArg::with_name("a", DataType::Integer(None)),
@ -3779,7 +3823,8 @@ fn parse_drop_procedure() {
FunctionDesc {
name: ObjectName(vec![Ident {
value: "test_proc2".to_string(),
quote_style: None
quote_style: None,
span: Span::empty(),
}]),
args: Some(vec![
OperateFunctionArg::with_name("a", DataType::Varchar(None)),
@ -3860,6 +3905,7 @@ fn parse_dollar_quoted_string() {
alias: Ident {
value: "col_name".into(),
quote_style: None,
span: Span::empty(),
},
}
);
@ -4204,20 +4250,24 @@ fn test_simple_postgres_insert_with_alias() {
into: true,
table_name: ObjectName(vec![Ident {
value: "test_tables".to_string(),
quote_style: None
quote_style: None,
span: Span::empty(),
}]),
table_alias: Some(Ident {
value: "test_table".to_string(),
quote_style: None
quote_style: None,
span: Span::empty(),
}),
columns: vec![
Ident {
value: "id".to_string(),
quote_style: None
quote_style: None,
span: Span::empty(),
},
Ident {
value: "a".to_string(),
quote_style: None
quote_style: None,
span: Span::empty(),
}
],
overwrite: false,
@ -4267,20 +4317,24 @@ fn test_simple_postgres_insert_with_alias() {
into: true,
table_name: ObjectName(vec![Ident {
value: "test_tables".to_string(),
quote_style: None
quote_style: None,
span: Span::empty(),
}]),
table_alias: Some(Ident {
value: "test_table".to_string(),
quote_style: None
quote_style: None,
span: Span::empty(),
}),
columns: vec![
Ident {
value: "id".to_string(),
quote_style: None
quote_style: None,
span: Span::empty(),
},
Ident {
value: "a".to_string(),
quote_style: None
quote_style: None,
span: Span::empty(),
}
],
overwrite: false,
@ -4332,20 +4386,24 @@ fn test_simple_insert_with_quoted_alias() {
into: true,
table_name: ObjectName(vec![Ident {
value: "test_tables".to_string(),
quote_style: None
quote_style: None,
span: Span::empty(),
}]),
table_alias: Some(Ident {
value: "Test_Table".to_string(),
quote_style: Some('"')
quote_style: Some('"'),
span: Span::empty(),
}),
columns: vec![
Ident {
value: "id".to_string(),
quote_style: None
quote_style: None,
span: Span::empty(),
},
Ident {
value: "a".to_string(),
quote_style: None
quote_style: None,
span: Span::empty(),
}
],
overwrite: false,
@ -5017,6 +5075,7 @@ fn check_arrow_precedence(sql: &str, arrow_operator: BinaryOperator) {
left: Box::new(Expr::Identifier(Ident {
value: "foo".to_string(),
quote_style: None,
span: Span::empty(),
})),
op: arrow_operator,
right: Box::new(Expr::Value(Value::SingleQuotedString("bar".to_string()))),
@ -5047,6 +5106,7 @@ fn arrow_cast_precedence() {
left: Box::new(Expr::Identifier(Ident {
value: "foo".to_string(),
quote_style: None,
span: Span::empty(),
})),
op: BinaryOperator::Arrow,
right: Box::new(Expr::Cast {