Skip to content

Commit

Permalink
feat: add long description to ambigiuous
Browse files Browse the repository at this point in the history
  • Loading branch information
benfdking committed Jun 25, 2024
1 parent b1f65b1 commit 6f5211e
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 0 deletions.
27 changes: 27 additions & 0 deletions crates/lib/src/rules/ambiguous/AM01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,33 @@ impl Rule for RuleAM01 {
"ambiguous.distinct"
}

fn long_description(&self) -> Option<&'static str> {
r#"
**Anti-pattern**
`DISTINCT` and `GROUP BY are conflicting.
```sql
SELECT DISTINCT
a
FROM foo
GROUP BY a
```
**Best practice**
Remove `DISTINCT` or `GROUP BY`. In our case, removing `GROUP BY` is better.
```sql
SELECT DISTINCT
a
FROM foo
```
"#
.into()
}

fn description(&self) -> &'static str {
"Ambiguous use of 'DISTINCT' in a 'SELECT' statement with 'GROUP BY'."
}
Expand Down
25 changes: 25 additions & 0 deletions crates/lib/src/rules/ambiguous/AM02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,31 @@ impl Rule for RuleAM02 {
fn description(&self) -> &'static str {
"Look for UNION keyword not immediately followed by DISTINCT or ALL"
}
fn long_description(&self) -> Option<&'static str> {
r#"
**Anti-pattern**
In this example, `UNION DISTINCT` should be preferred over `UNION`, because explicit is better than implicit.
```sql
SELECT a, b FROM table_1
UNION
SELECT a, b FROM table_2
```
**Best practice**
Specify `DISTINCT` or `ALL` after `UNION` (note that `DISTINCT` is the default behavior).
```sql
SELECT a, b FROM table_1
UNION DISTINCT
SELECT a, b FROM table_2
```
"#
.into()
}

fn crawl_behaviour(&self) -> Crawler {
SegmentSeekerCrawler::new(["set_operator"].into()).into()
Expand Down
26 changes: 26 additions & 0 deletions crates/lib/src/rules/ambiguous/AM06.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,32 @@ impl Rule for RuleAM06 {
fn description(&self) -> &'static str {
"Inconsistent column references in 'GROUP BY/ORDER BY' clauses."
}
fn long_description(&self) -> Option<&'static str> {
r#"
**Anti-pattern**
In this example, the ORRDER BY clause mixes explicit and implicit order by column references.
```sql
SELECT
a, b
FROM foo
ORDER BY a, b DESC
```
**Best practice**
If any columns in the ORDER BY clause specify ASC or DESC, they should all do so.
```sql
SELECT
a, b
FROM foo
ORDER BY a ASC, b DESC
```
"#
.into()
}

fn eval(&self, context: RuleContext) -> Vec<LintResult> {
let skip = FunctionalContext::new(context.clone()).parent_stack().any(Some(|it| {
Expand Down
65 changes: 65 additions & 0 deletions docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,28 @@ Ambiguous use of 'DISTINCT' in a 'SELECT' statement with 'GROUP BY'.
**Fixable:** No


**Anti-pattern**

`DISTINCT` and `GROUP BY are conflicting.

```sql
SELECT DISTINCT
a
FROM foo
GROUP BY a
```

**Best practice**

Remove `DISTINCT` or `GROUP BY`. In our case, removing `GROUP BY` is better.


```sql
SELECT DISTINCT
a
FROM foo
```


### ambiguous.union

Expand All @@ -385,6 +407,27 @@ Look for UNION keyword not immediately followed by DISTINCT or ALL
**Fixable:** Yes


**Anti-pattern**

In this example, `UNION DISTINCT` should be preferred over `UNION`, because explicit is better than implicit.


```sql
SELECT a, b FROM table_1
UNION
SELECT a, b FROM table_2
```

**Best practice**

Specify `DISTINCT` or `ALL` after `UNION` (note that `DISTINCT` is the default behavior).

```sql
SELECT a, b FROM table_1
UNION DISTINCT
SELECT a, b FROM table_2
```


### ambiguous.column_references

Expand All @@ -395,6 +438,28 @@ Inconsistent column references in 'GROUP BY/ORDER BY' clauses.
**Fixable:** No


**Anti-pattern**

In this example, the ORRDER BY clause mixes explicit and implicit order by column references.

```sql
SELECT
a, b
FROM foo
ORDER BY a, b DESC
```

**Best practice**

If any columns in the ORDER BY clause specify ASC or DESC, they should all do so.

```sql
SELECT
a, b
FROM foo
ORDER BY a ASC, b DESC
```


### capitalisation.keywords

Expand Down

0 comments on commit 6f5211e

Please sign in to comment.