-
Notifications
You must be signed in to change notification settings - Fork 494
dbo11y: remove comments from normalized sql text in postgres #5005
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
Open
cristiangreco
wants to merge
1
commit into
main
Choose a base branch
from
cristian/dbo11y-pg-remove-comments
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,26 +49,26 @@ func TestPgSqlParser_Redact(t *testing.T) { | |
| sql: `WITH active_users AS ( | ||
| SELECT * FROM users WHERE last_login > '2024-01-01' | ||
| ), recent_orders AS ( | ||
| SELECT o.* FROM orders o | ||
| JOIN active_users u ON u.id = o.user_id | ||
| SELECT o.* FROM orders o | ||
| JOIN active_users u ON u.id = o.user_id | ||
| WHERE o.created_at > '2024-03-01' | ||
| ) | ||
| SELECT au.name, COUNT(ro.id) as order_count | ||
| FROM active_users au | ||
| LEFT JOIN recent_orders ro ON ro.user_id = au.id | ||
| GROUP BY au.name | ||
| SELECT au.name, COUNT(ro.id) as order_count | ||
| FROM active_users au | ||
| LEFT JOIN recent_orders ro ON ro.user_id = au.id | ||
| GROUP BY au.name | ||
| HAVING COUNT(ro.id) > 5`, | ||
| want: `WITH active_users AS ( | ||
| SELECT * FROM users WHERE last_login > ? | ||
| ), recent_orders AS ( | ||
| SELECT o.* FROM orders o | ||
| JOIN active_users u ON u.id = o.user_id | ||
| SELECT o.* FROM orders o | ||
| JOIN active_users u ON u.id = o.user_id | ||
| WHERE o.created_at > ? | ||
| ) | ||
| SELECT au.name, COUNT(ro.id) as order_count | ||
| FROM active_users au | ||
| LEFT JOIN recent_orders ro ON ro.user_id = au.id | ||
| GROUP BY au.name | ||
| SELECT au.name, COUNT(ro.id) as order_count | ||
| FROM active_users au | ||
| LEFT JOIN recent_orders ro ON ro.user_id = au.id | ||
| GROUP BY au.name | ||
| HAVING COUNT(ro.id) > ?`, | ||
| }, | ||
| { | ||
|
|
@@ -106,13 +106,13 @@ func TestPgSqlParser_Redact(t *testing.T) { | |
| { | ||
| name: "WITH statement with UPDATE", | ||
| sql: `WITH inactive_users AS ( | ||
| SELECT id FROM users | ||
| SELECT id FROM users | ||
| WHERE last_login < '2023-01-01' AND status = 'active' | ||
| ) | ||
| UPDATE users SET status = 'inactive', updated_at = '2024-03-20' | ||
| WHERE id IN (SELECT id FROM inactive_users)`, | ||
| want: `WITH inactive_users AS ( | ||
| SELECT id FROM users | ||
| SELECT id FROM users | ||
| WHERE last_login < ? AND status = ? | ||
| ) | ||
| UPDATE users SET status = ?, updated_at = ? | ||
|
|
@@ -121,16 +121,16 @@ func TestPgSqlParser_Redact(t *testing.T) { | |
| { | ||
| name: "WITH statement with DELETE", | ||
| sql: `WITH old_orders AS ( | ||
| SELECT id FROM orders | ||
| SELECT id FROM orders | ||
| WHERE created_at < '2023-01-01' AND status = 'completed' | ||
| ) | ||
| DELETE FROM order_items | ||
| DELETE FROM order_items | ||
| WHERE order_id IN (SELECT id FROM old_orders)`, | ||
| want: `WITH old_orders AS ( | ||
| SELECT id FROM orders | ||
| SELECT id FROM orders | ||
| WHERE created_at < ? AND status = ? | ||
| ) | ||
| DELETE FROM order_items | ||
| DELETE FROM order_items | ||
| WHERE order_id IN (SELECT id FROM old_orders)`, | ||
| }, | ||
| { | ||
|
|
@@ -185,134 +185,6 @@ func TestPgSqlParser_Redact(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| func TestPgSqlParser_ExtractTableNames(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| sql string | ||
| want []string | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "simple select", | ||
| sql: "SELECT * FROM users", | ||
| want: []string{"users"}, | ||
| }, | ||
| { | ||
| name: "select with join", | ||
| sql: "SELECT * FROM users u JOIN orders o ON u.id = o.user_id", | ||
| want: []string{"orders", "users"}, | ||
| }, | ||
| { | ||
| name: "select with schema qualified tables", | ||
| sql: "SELECT * FROM public.users JOIN sales.orders ON users.id = orders.user_id", | ||
| want: []string{"public.users", "sales.orders"}, | ||
| }, | ||
| { | ||
| name: "insert statement", | ||
| sql: "INSERT INTO users (name, email) VALUES ('John', '[email protected]')", | ||
| want: []string{"users"}, | ||
| }, | ||
| { | ||
| name: "update statement", | ||
| sql: "UPDATE users SET last_login = NOW() WHERE id = 1", | ||
| want: []string{"users"}, | ||
| }, | ||
| { | ||
| name: "delete statement", | ||
| sql: "DELETE FROM users WHERE id = 1", | ||
| want: []string{"users"}, | ||
| }, | ||
| { | ||
| name: "with clause", | ||
| sql: `WITH active_users AS ( | ||
| SELECT * FROM users WHERE status = 'active' | ||
| ) | ||
| SELECT * FROM active_users au | ||
| JOIN orders o ON o.user_id = au.id`, | ||
| want: []string{"orders", "users"}, | ||
| }, | ||
| { | ||
| name: "subquery in where clause", | ||
| sql: `SELECT * FROM orders | ||
| WHERE user_id IN (SELECT id FROM users WHERE status = 'active')`, | ||
| want: []string{"orders", "users"}, | ||
| }, | ||
| { | ||
| name: "multiple schema qualified tables with aliases", | ||
| sql: `SELECT u.name, o.total, p.status | ||
| FROM public.users u | ||
| JOIN sales.orders o ON u.id = o.user_id | ||
| LEFT JOIN shipping.packages p ON o.id = p.order_id`, | ||
| want: []string{"public.users", "sales.orders", "shipping.packages"}, | ||
| }, | ||
| { | ||
| name: "truncated query with ...", | ||
| sql: "SELECT * FROM users JOIN orders ON users.id = orders.user_id AND...", | ||
| want: []string{"users", "orders"}, | ||
| }, | ||
| { | ||
| name: "truncated query with incomplete comment", | ||
| sql: "SELECT * FROM users JOIN orders ON users.id = orders.user_id /* some comment that gets truncated...", | ||
| want: []string{"users", "orders"}, | ||
| }, | ||
| { | ||
| name: "truncated query mid-table name", | ||
| sql: "SELECT * FROM users JOIN ord...", | ||
| want: []string{"users", "ord..."}, | ||
| }, | ||
| { | ||
| name: "truncated query with schema qualified tables", | ||
| sql: "SELECT * FROM public.users JOIN sales.orders ON users.id = orders.user_id AND...", | ||
| want: []string{"public.users", "sales.orders"}, | ||
| }, | ||
| { | ||
| name: "query with table.* expression", | ||
| sql: "SELECT u.*, o.* FROM users u JOIN orders o ON u.id = o.user_id", | ||
| want: []string{"users", "orders"}, | ||
| }, | ||
| { | ||
| name: "query with type cast", | ||
| sql: "SELECT u.id, '2024-03-20'::timestamp FROM users u", | ||
| want: []string{"users"}, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got, err := ExtractTableNames(tt.sql) | ||
| if (err != nil) != tt.wantErr { | ||
| t.Errorf("ExtractTableNames() error = %v, wantErr %v", err, tt.wantErr) | ||
| return | ||
| } | ||
| if !tt.wantErr { | ||
| if len(got) != len(tt.want) { | ||
| t.Errorf("ExtractTableNames()\nGOT = %v\nWANT = %v", got, tt.want) | ||
| return | ||
| } | ||
| // Compare slices ignoring order since table names might come in different order | ||
| gotMap := make(map[string]bool) | ||
| wantMap := make(map[string]bool) | ||
| for _, table := range got { | ||
| gotMap[table] = true | ||
| } | ||
| for _, table := range tt.want { | ||
| wantMap[table] = true | ||
| } | ||
| for table := range gotMap { | ||
| if !wantMap[table] { | ||
| t.Errorf("ExtractTableNames() got unexpected table = %v", table) | ||
| } | ||
| } | ||
| for table := range wantMap { | ||
| if !gotMap[table] { | ||
| t.Errorf("ExtractTableNames() missing expected table = %v", table) | ||
| } | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestContainsReservedKeywords(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is there a reason we're removing the db engine? I understand that postgres is uniquely identified by the other values, I.E. queryid and datname. That said, having the engine listed doesn't increase cardinality, and isn't a huge increase in log line size either.
I don't have a strong opinion either way, just curious what the motivation is here.
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.
Yes that's correct, it doesn't really change the cardinality. It was added there at early development stage though and we've removed it from various places over time (this is likely the last place where it appears). Not a big deal, doing it just for consistency at this point.