Skip to content

Commit

Permalink
feat(spans): Collapse list of SQL columns with aliases (#2367)
Browse files Browse the repository at this point in the history
We already scrubbed list of columns in SQL columns such as

```sql
SELECT a, b, c FROM table
-- becomes
SELECT .. FROM table
```

Also collapse if columns have an alias, e.g.

```sql
SELECT table.a AS table__a, table.b AS table__b, table.c AS table__c FROM table
-- becomes
SELECT .. FROM table
```
  • Loading branch information
jjbayer committed Aug 1, 2023
1 parent 46c46e7 commit f942199
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion relay-general/src/store/normalize/span/description.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ static SQL_COLLAPSE_PLACEHOLDERS: Lazy<Regex> = Lazy::new(|| {
});

/// Collapse simple lists of columns in select.
/// For example:
/// SELECT a, b FROM x -> SELECT .. FROM x
/// SELECT "a.b" AS a__b, "a.c" AS a__c FROM x -> SELECT .. FROM x
static SQL_COLLAPSE_SELECT: Lazy<Regex> = Lazy::new(|| {
Regex::new(r#"(?i)(?P<select>SELECT(\s+(DISTINCT|ALL))?)\s+(?P<columns>(\w+(?:\s*,\s*\w+)+))\s+(?<from>FROM|$)"#)
let col = r"\w+( AS \w+)?";
Regex::new(format!(r#"(?i)(?P<select>SELECT(\s+(DISTINCT|ALL))?)\s+(?P<columns>({col}(?:\s*,\s*{col})+))\s+(?<from>FROM|$)"#).as_str())
.unwrap()
});

Expand Down Expand Up @@ -701,6 +705,14 @@ mod tests {
"SELECT DISTINCT .. FROM table WHERE %s"
);

span_description_test!(
span_description_collapse_columns_with_as,
// Simple lists of columns will be collapsed.
r#"SELECT myfield1, "a"."b" AS a__b, another_field as bar FROM table WHERE %s"#,
"db.sql.query",
"SELECT .. FROM table WHERE %s"
);

span_description_test!(
span_description_scrub_values,
"INSERT INTO a (b, c, d, e) VALUES (%s, %s, %s, %s)",
Expand Down

0 comments on commit f942199

Please sign in to comment.