-
Notifications
You must be signed in to change notification settings - Fork 94
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
feat(spans): De-duplicate SQL conditions #2929
Changes from 6 commits
714d832
9896079
07210fc
6a595fd
779f406
7f863c1
f8a009e
adc1729
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -211,8 +211,8 @@ mod tests { | |
|
||
scrub_sql_test!( | ||
various_parameterized_ins_percentage, | ||
"SELECT count() FROM table1 WHERE id IN (%s, %s) AND id IN (%s, %s, %s)", | ||
"SELECT count() FROM table1 WHERE id IN (%s) AND id IN (%s)" | ||
"SELECT count() FROM table1 WHERE id1 IN (%s, %s) AND id2 IN (%s, %s, %s)", | ||
"SELECT count() FROM table1 WHERE id1 IN (%s) AND id2 IN (%s)" | ||
); | ||
|
||
scrub_sql_test!( | ||
|
@@ -698,6 +698,46 @@ mod tests { | |
"UPDATE tbl SET foo = CASE WHEN .. THEN .. END" | ||
); | ||
|
||
scrub_sql_test!( | ||
duplicate_conditions, | ||
r#"SELECT * | ||
FROM a | ||
WHERE a.status = %s | ||
AND ( | ||
(a.id = %s AND a.org = %s) | ||
OR (a.id = %s AND a.org = %s) | ||
OR (a.id = %s AND a.org = %s) | ||
OR (a.id = %s AND a.org = %s) | ||
)"#, | ||
"SELECT * FROM a WHERE status = %s AND ((id = %s AND org = %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. are the double parentheses 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. I did not intend to leave them initially, but they might help indicate to the user that something was scrubbed here. They should be easy to remove though, if we want to @gggritso any opinion? 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. I'd personally remove them. I think it's easy to notice queries are getting scrubbed so it's no surprise. 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. Done. 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. No strong opinion here 👍🏻 |
||
); | ||
|
||
scrub_sql_test!( | ||
duplicate_conditions_or, | ||
r#"SELECT * | ||
FROM a | ||
WHERE a.status = %s | ||
OR ( | ||
(a.id = %s OR a.org = %s) | ||
OR (a.id = %s OR a.org = %s) | ||
OR (a.id = %s OR a.org = %s) | ||
OR (a.id = %s OR a.org = %s) | ||
)"#, | ||
"SELECT * FROM a WHERE status = %s OR ((id = %s OR org = %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. @gggritso this is not exactly the placeholder you suggested, but it does keep the implementation simple. Let me know if it's good enough. 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. Works for me 👍🏻 going to defer to @Zylphrex on this, since he found the original issue 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 looks fine to me as it covers the case I originally raised. |
||
); | ||
|
||
scrub_sql_test!( | ||
non_duplicate_conditions, | ||
r#"SELECT * | ||
FROM a | ||
WHERE a.status = %s | ||
AND ( | ||
(a.id = %s AND a.org2 = %s) | ||
OR (a.id = %s AND a.org = %s) | ||
)"#, | ||
"SELECT * FROM a WHERE status = %s AND ((id = %s AND org2 = %s) OR (id = %s AND org = %s))" | ||
); | ||
|
||
scrub_sql_test!( | ||
unique_alias, | ||
"SELECT pg_advisory_unlock(%s, %s) AS t0123456789abcdef", | ||
|
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.
I think this raises an edge case how queries like this one where we have the same condition twice joined by a
AND
operator.Likely an uncommon use case, but the first list may be a subset of the second so it may be valid to transform it into just
id IN (%s)
.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.
I think it's OK to pretend that they are the same for grouping purposes, with the same reasoning as for the
OR
clause: If the left operand (id
) and the operatorIN
repeat multiple times, it is likely a dynamically generated query that we can simplify.