Skip to content

Commit

Permalink
feat: add long description for reference
Browse files Browse the repository at this point in the history
  • Loading branch information
benfdking committed Jun 25, 2024
1 parent 5b54c81 commit 514608c
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 2 deletions.
25 changes: 25 additions & 0 deletions crates/lib/src/rules/references/RF01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,31 @@ impl Rule for RuleRF01 {
"References cannot reference objects not present in 'FROM' clause."
}

fn long_description(&self) -> Option<&'static str> {
r#"
**Anti-pattern**
In this example, the reference `vee` has not been declared.
```sql
SELECT
vee.a
FROM foo
```
**Best practice**
Remove the reference.
```sql
SELECT
a
FROM foo
```
"#
.into()
}

fn eval(&self, context: RuleContext) -> Vec<LintResult> {
let dialects_disabled_by_default =
["bigquery", "databricks", "hive", "redshift", "soql", "sparksql"];
Expand Down
38 changes: 36 additions & 2 deletions crates/lib/src/rules/references/RF03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,38 @@ impl Rule for RuleRF03 {
"References should be consistent in statements with a single table."
}

fn is_fix_compatible(&self) -> bool {
true
fn long_description(&self) -> Option<&'static str> {
r#"
**Anti-pattern**
In this example, only the field b is referenced.
```sql
SELECT
a,
foo.b
FROM foo
```
**Best practice**
Add or remove references to all fields.
```sql
SELECT
a,
b
FROM foo
-- Also good
SELECT
foo.a,
foo.b
FROM foo
```
"#
.into()
}

fn eval(&self, context: RuleContext) -> Vec<LintResult> {
Expand All @@ -302,6 +332,10 @@ impl Rule for RuleRF03 {
self.visit_queries(query, &mut visited)
}

fn is_fix_compatible(&self) -> bool {
true
}

fn crawl_behaviour(&self) -> Crawler {
SegmentSeekerCrawler::new(
["select_statement", "set_expression", "with_compound_statement"].into(),
Expand Down
49 changes: 49 additions & 0 deletions docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,26 @@ References cannot reference objects not present in 'FROM' clause.
**Fixable:** No


**Anti-pattern**

In this example, the reference `vee` has not been declared.

```sql
SELECT
vee.a
FROM foo
```

**Best practice**

Remove the reference.

```sql
SELECT
a
FROM foo
```


### references.consistent

Expand All @@ -1233,6 +1253,35 @@ References should be consistent in statements with a single table.
**Fixable:** Yes


**Anti-pattern**

In this example, only the field b is referenced.

```sql
SELECT
a,
foo.b
FROM foo
```

**Best practice**

Add or remove references to all fields.

```sql
SELECT
a,
b
FROM foo
-- Also good
SELECT
foo.a,
foo.b
FROM foo
```


### structure.else_null

Expand Down

0 comments on commit 514608c

Please sign in to comment.