-
Notifications
You must be signed in to change notification settings - Fork 93
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
chore(spans): Upgrade sqlparser #3057
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -383,7 +383,7 @@ mod tests { | |
scrub_sql_test!( | ||
strip_prefixes_mysql_generic, | ||
r#"SELECT `table`.`foo`, count(*) from `table` WHERE sku = %s"#, | ||
r#"SELECT foo, count(*) from table WHERE sku = %s"# | ||
r#"SELECT foo, count(*) FROM table WHERE sku = %s"# | ||
); | ||
|
||
scrub_sql_test_with_dialect!( | ||
|
@@ -581,7 +581,7 @@ mod tests { | |
scrub_sql_test!( | ||
values_multi, | ||
"INSERT INTO a (b, c, d, e) VALuES (%s, %s, %s, %s), (%s, %s, %s, %s), (%s, %s, %s, %s) ON CONFLICT DO NOTHING", | ||
"INSERT INTO a (..) VALUES (%s) ON CONFLICT DO NOTHING" | ||
"INSERT INTO a (..) VALUES (%s) ON CONFLICT DO NOTHING" | ||
); | ||
|
||
scrub_sql_test!( | ||
|
@@ -876,13 +876,10 @@ mod tests { | |
); | ||
|
||
scrub_sql_test_with_dialect!( | ||
dont_fallback_to_regex, | ||
replace_into, | ||
"mysql", | ||
// sqlparser cannot parse REPLACE INTO. If we know that | ||
// a query is MySQL, we should give up rather than try to scrub | ||
// with regex | ||
r#"REPLACE INTO `foo` (`a`) VALUES ("abcd1234")"#, | ||
"REPLACE INTO foo (a) VALUES (%s)" | ||
"REPLACE INTO foo (..) VALUES (%s)" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is now parsed successfully and no longer relies on the fallback scrubber. |
||
); | ||
|
||
scrub_sql_test!( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,8 @@ use itertools::Itertools; | |
use once_cell::sync::Lazy; | ||
use regex::Regex; | ||
use sqlparser::ast::{ | ||
AlterTableOperation, Assignment, BinaryOperator, CloseCursor, ColumnDef, Expr, Ident, | ||
ObjectName, Query, Select, SelectItem, SetExpr, Statement, TableAlias, TableConstraint, | ||
AlterTableOperation, Assignment, BinaryOperator, CloseCursor, ColumnDef, Expr, FunctionArg, | ||
Ident, ObjectName, Query, Select, SelectItem, SetExpr, Statement, TableAlias, TableConstraint, | ||
TableFactor, UnaryOperator, Value, VisitMut, VisitorMut, | ||
}; | ||
use sqlparser::dialect::{Dialect, GenericDialect}; | ||
|
@@ -198,12 +198,41 @@ impl VisitorMut for NormalizeVisitor { | |
Self::simplify_table_alias(alias); | ||
} | ||
TableFactor::Pivot { | ||
table_alias, | ||
pivot_alias, | ||
value_column, | ||
alias, | ||
.. | ||
} => { | ||
Self::simplify_compound_identifier(value_column); | ||
Self::simplify_table_alias(alias); | ||
} | ||
TableFactor::Function { | ||
name, args, alias, .. | ||
} => { | ||
Self::simplify_compound_identifier(&mut name.0); | ||
for arg in args { | ||
if let FunctionArg::Named { name, .. } = arg { | ||
Self::scrub_name(name); | ||
} | ||
} | ||
Self::simplify_table_alias(alias); | ||
} | ||
TableFactor::JsonTable { columns, alias, .. } => { | ||
for column in columns { | ||
Self::scrub_name(&mut column.name); | ||
} | ||
Self::simplify_table_alias(alias); | ||
} | ||
TableFactor::Unpivot { | ||
value, | ||
name, | ||
columns, | ||
alias, | ||
.. | ||
} => { | ||
Self::simplify_table_alias(table_alias); | ||
Self::simplify_table_alias(pivot_alias); | ||
Self::scrub_name(value); | ||
Self::scrub_name(name); | ||
Self::simplify_compound_identifier(columns); | ||
Self::simplify_table_alias(alias); | ||
} | ||
} | ||
ControlFlow::Continue(()) | ||
|
@@ -311,8 +340,10 @@ impl VisitorMut for NormalizeVisitor { | |
columns, source, .. | ||
} => { | ||
*columns = vec![Self::ellipsis()]; | ||
if let SetExpr::Values(v) = &mut *source.body { | ||
v.rows = vec![vec![Expr::Value(Self::placeholder())]] | ||
if let Some(source) = source.as_mut() { | ||
if let SetExpr::Values(v) = &mut *source.body { | ||
v.rows = vec![vec![Expr::Value(Self::placeholder())]] | ||
} | ||
} | ||
} | ||
// Simple lists of col = value assignments are collapsed to `..`. | ||
|
@@ -350,89 +381,93 @@ impl VisitorMut for NormalizeVisitor { | |
Statement::Close { | ||
cursor: CloseCursor::Specific { name }, | ||
} => Self::erase_name(name), | ||
Statement::AlterTable { name, operation } => { | ||
Statement::AlterTable { | ||
name, operations, .. | ||
} => { | ||
Self::simplify_compound_identifier(&mut name.0); | ||
match operation { | ||
AlterTableOperation::AddConstraint(c) => match c { | ||
TableConstraint::Unique { name, columns, .. } => { | ||
if let Some(name) = name { | ||
Self::scrub_name(name); | ||
for operation in operations { | ||
match operation { | ||
Comment on lines
+388
to
+389
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We now have to loop through multiple operations, hence the large diff. |
||
AlterTableOperation::AddConstraint(c) => match c { | ||
TableConstraint::Unique { name, columns, .. } => { | ||
if let Some(name) = name { | ||
Self::scrub_name(name); | ||
} | ||
for column in columns { | ||
Self::scrub_name(column); | ||
} | ||
} | ||
for column in columns { | ||
Self::scrub_name(column); | ||
TableConstraint::ForeignKey { | ||
name, | ||
columns, | ||
referred_columns, | ||
.. | ||
} => { | ||
if let Some(name) = name { | ||
Self::scrub_name(name); | ||
} | ||
for column in columns { | ||
Self::scrub_name(column); | ||
} | ||
for column in referred_columns { | ||
Self::scrub_name(column); | ||
} | ||
} | ||
} | ||
TableConstraint::ForeignKey { | ||
name, | ||
columns, | ||
referred_columns, | ||
.. | ||
} => { | ||
if let Some(name) = name { | ||
Self::scrub_name(name); | ||
TableConstraint::Check { name, .. } => { | ||
if let Some(name) = name { | ||
Self::scrub_name(name); | ||
} | ||
} | ||
for column in columns { | ||
Self::scrub_name(column); | ||
TableConstraint::Index { name, columns, .. } => { | ||
if let Some(name) = name { | ||
Self::scrub_name(name); | ||
} | ||
for column in columns { | ||
Self::scrub_name(column); | ||
} | ||
} | ||
for column in referred_columns { | ||
Self::scrub_name(column); | ||
TableConstraint::FulltextOrSpatial { | ||
opt_index_name, | ||
columns, | ||
.. | ||
} => { | ||
if let Some(name) = opt_index_name { | ||
Self::scrub_name(name); | ||
} | ||
for column in columns { | ||
Self::scrub_name(column); | ||
} | ||
} | ||
}, | ||
AlterTableOperation::AddColumn { column_def, .. } => { | ||
let ColumnDef { name, .. } = column_def; | ||
Self::scrub_name(name); | ||
} | ||
TableConstraint::Check { name, .. } => { | ||
if let Some(name) = name { | ||
Self::scrub_name(name); | ||
} | ||
AlterTableOperation::DropConstraint { name, .. } => Self::scrub_name(name), | ||
AlterTableOperation::DropColumn { column_name, .. } => { | ||
Self::scrub_name(column_name) | ||
} | ||
TableConstraint::Index { name, columns, .. } => { | ||
if let Some(name) = name { | ||
Self::scrub_name(name); | ||
} | ||
for column in columns { | ||
Self::scrub_name(column); | ||
} | ||
AlterTableOperation::RenameColumn { | ||
old_column_name, | ||
new_column_name, | ||
} => { | ||
Self::scrub_name(old_column_name); | ||
Self::scrub_name(new_column_name); | ||
} | ||
TableConstraint::FulltextOrSpatial { | ||
opt_index_name, | ||
columns, | ||
.. | ||
AlterTableOperation::ChangeColumn { | ||
old_name, new_name, .. | ||
} => { | ||
if let Some(name) = opt_index_name { | ||
Self::scrub_name(name); | ||
} | ||
for column in columns { | ||
Self::scrub_name(column); | ||
} | ||
Self::scrub_name(old_name); | ||
Self::scrub_name(new_name); | ||
} | ||
}, | ||
AlterTableOperation::AddColumn { column_def, .. } => { | ||
let ColumnDef { name, .. } = column_def; | ||
Self::scrub_name(name); | ||
} | ||
AlterTableOperation::DropConstraint { name, .. } => Self::scrub_name(name), | ||
AlterTableOperation::DropColumn { column_name, .. } => { | ||
Self::scrub_name(column_name) | ||
} | ||
AlterTableOperation::RenameColumn { | ||
old_column_name, | ||
new_column_name, | ||
} => { | ||
Self::scrub_name(old_column_name); | ||
Self::scrub_name(new_column_name); | ||
} | ||
AlterTableOperation::ChangeColumn { | ||
old_name, new_name, .. | ||
} => { | ||
Self::scrub_name(old_name); | ||
Self::scrub_name(new_name); | ||
} | ||
AlterTableOperation::RenameConstraint { old_name, new_name } => { | ||
Self::scrub_name(old_name); | ||
Self::scrub_name(new_name); | ||
} | ||
AlterTableOperation::AlterColumn { column_name, .. } => { | ||
Self::scrub_name(column_name); | ||
AlterTableOperation::RenameConstraint { old_name, new_name } => { | ||
Self::scrub_name(old_name); | ||
Self::scrub_name(new_name); | ||
} | ||
AlterTableOperation::AlterColumn { column_name, .. } => { | ||
Self::scrub_name(column_name); | ||
} | ||
_ => {} | ||
} | ||
_ => {} | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is now parsed successfully and no longer relies on the fallback scrubber.