Repository: sqlfluff/sqlfluff

Issue:
Update warning for parsing errors found on the ansi dialect
### Search before asking

- [X] I searched the [issues](https://github.com/sqlfluff/sqlfluff/issues) and found no similar issues.


### Description

In the past specifying a dialect was **optional**. If unspecified, the dialect defaulted to `ansi`. Because of this there is a warning presented when sqlfluff runs in parse mode and the dialect is set to ansi and parsing errors are encountered.

`WARNING: Parsing errors found and dialect is set to 'ansi'. Have you configured your dialect?`

Currently, specifying a dialect is **mandatory**. Therefore this warning is perhaps not needed... and certainly not needed in its current form.

I opened this issue to document the idea and solicit feedback. 
1. The simplest improvement to make the message more appropriate is to just change it to this:

`WARNING: Parsing errors found and dialect is set to 'ansi'. Is 'ansi' the correct dialect?`

2. On the other hand, we know that the user explicitly set the dialect to `ansi`. So why bother asking if it was intentional? We don't ask if you meant postgres or tsql. There's an argument to simply remove the warning altogether.

3. Finally, we could potentially differentiate between `--dialect ansi` passed on the command line vs the dialect being picked up from a `.sqlfluff` config file. Perhaps the warning should be displayed only the in the case where the dialect was picked up implicitly from the config file.

### Use case

_No response_

### Dialect

ansi

### Are you willing to work on and submit a PR to address the issue?

- [X] Yes I am willing to submit a PR!

### Code of Conduct

- [X] I agree to follow this project's [Code of Conduct](https://github.com/sqlfluff/sqlfluff/blob/main/CODE_OF_CONDUCT.md)


Discussion:
codecov[bot]: # [Codecov](https://codecov.io/gh/sqlfluff/sqlfluff/pull/3411?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff) Report
> Merging [#3411](https://codecov.io/gh/sqlfluff/sqlfluff/pull/3411?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff) (0ba4516) into [main](https://codecov.io/gh/sqlfluff/sqlfluff/commit/f15bf0b33dae503546426c597a506a78ead4cb51?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff) (f15bf0b) will **not change** coverage.
> The diff coverage is `100.00%`.

```diff
@@            Coverage Diff            @@
##              main     #3411   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          170       170           
  Lines        12852     12852           
=========================================
  Hits         12852     12852           
```


| [Impacted Files](https://codecov.io/gh/sqlfluff/sqlfluff/pull/3411?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff) | Coverage Δ | |
|---|---|---|
| [src/sqlfluff/cli/commands.py](https://codecov.io/gh/sqlfluff/sqlfluff/pull/3411/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff#diff-c3JjL3NxbGZsdWZmL2NsaS9jb21tYW5kcy5weQ==) | `100.00% <100.00%> (ø)` | |
| [src/sqlfluff/cli/formatters.py](https://codecov.io/gh/sqlfluff/sqlfluff/pull/3411/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff#diff-c3JjL3NxbGZsdWZmL2NsaS9mb3JtYXR0ZXJzLnB5) | `100.00% <100.00%> (ø)` | |
| [src/sqlfluff/core/linter/linter.py](https://codecov.io/gh/sqlfluff/sqlfluff/pull/3411/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff#diff-c3JjL3NxbGZsdWZmL2NvcmUvbGludGVyL2xpbnRlci5weQ==) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/sqlfluff/sqlfluff/pull/3411?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/sqlfluff/sqlfluff/pull/3411?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff). Last update [f15bf0b...0ba4516](https://codecov.io/gh/sqlfluff/sqlfluff/pull/3411?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff).

mdahlman: Yes! In fact, I was going to mention this explicitly. 
I didn't see where to create a test. Looking now, I suppose it would be in `commands_test.py`.
But I didn't see this discussed in the Wiki, so I as hesitant to create a new test.
Do you think `test__cli__command_dialect` would be a reasonably close test to use as a model?

I will investigate, but any guidance will be welcome.
mdahlman: Here's a test case. Feedback requested.
mdahlman: I'll see your additional test and raise you a variation.
From the initial enhancement request I had been thinking of differentiating between explicitly specifying the dialect and implicitly specifying it. I did not give different warnings for the two situations, but now I included separate tests for the two situations. I like this better.

For the `# pragma: no cover`s, I removed only one, because it seems that that's the only one being tested. Did you have more than just the 1 in mind?

Patch:
diff --git a/src/sqlfluff/cli/commands.py b/src/sqlfluff/cli/commands.py
index 7c390a43e..e2b7766c8 100644
--- a/src/sqlfluff/cli/commands.py
+++ b/src/sqlfluff/cli/commands.py
@@ -1079,8 +1079,10 @@ def _print_out_violations_and_timing(
             output_stream.write("==== parsing violations ====")  # pragma: no cover
         for v in parsed_string.violations:
             output_stream.write(format_violation(v))  # pragma: no cover
-        if parsed_string.violations and parsed_string.config.get("dialect") == "ansi":
-            output_stream.write(format_dialect_warning())  # pragma: no cover
+        if parsed_string.violations:
+            output_stream.write(
+                format_dialect_warning(parsed_string.config.get("dialect"))
+            )
 
         if verbose >= 2:
             output_stream.write("==== timings ====")
diff --git a/src/sqlfluff/cli/formatters.py b/src/sqlfluff/cli/formatters.py
index 7efbd2418..e72ae84df 100644
--- a/src/sqlfluff/cli/formatters.py
+++ b/src/sqlfluff/cli/formatters.py
@@ -187,12 +187,12 @@ def format_dialects(dialect_readout, verbose=0):
     return text_buffer.getvalue()
 
 
-def format_dialect_warning():  # pragma: no cover
-    """Output a warning for parsing errors found on the ansi dialect."""
+def format_dialect_warning(dialect):
+    """Output a warning for parsing errors."""
     return colorize(
         (
             "WARNING: Parsing errors found and dialect is set to "
-            "'ansi'. Have you configured your dialect?"
+            f"'{dialect}'. Have you configured your dialect correctly?"
         ),
         Color.lightgrey,
     )
@@ -321,9 +321,9 @@ class OutputStreamFormatter:
             f"=== [{colorize(templater, Color.lightgrey)}] {message}"
         )  # pragma: no cover
 
-    def dispatch_dialect_warning(self) -> None:
+    def dispatch_dialect_warning(self, dialect) -> None:
         """Dispatch a warning for dialects."""
-        self._dispatch(format_dialect_warning())  # pragma: no cover
+        self._dispatch(format_dialect_warning(dialect))  # pragma: no cover
 
     def _format_file_violations(
         self, fname: str, violations: List[SQLBaseError]
diff --git a/src/sqlfluff/core/linter/linter.py b/src/sqlfluff/core/linter/linter.py
index c897609cb..bef4a06b9 100644
--- a/src/sqlfluff/core/linter/linter.py
+++ b/src/sqlfluff/core/linter/linter.py
@@ -723,11 +723,11 @@ class Linter:
             )
 
         # Safety flag for unset dialects
-        if parsed.config.get("dialect") == "ansi" and linted_file.get_violations(
+        if linted_file.get_violations(
             fixable=True if fix else None, types=SQLParseError
         ):
             if formatter:  # pragma: no cover TODO?
-                formatter.dispatch_dialect_warning()
+                formatter.dispatch_dialect_warning(parsed.config.get("dialect"))
 
         return linted_file
 


Test patch:
diff --git a/test/cli/commands_test.py b/test/cli/commands_test.py
index 63d6fca84..c15bd9d93 100644
--- a/test/cli/commands_test.py
+++ b/test/cli/commands_test.py
@@ -100,6 +100,53 @@ def test__cli__command_dialect():
     )
 
 
+def test__cli__command_parse_error_dialect_explicit_warning():
+    """Check parsing error raises the right warning."""
+    # For any parsing error there should be a non-zero exit code
+    # and a human-readable warning should be dislayed.
+    # Dialect specified as commandline option.
+    result = invoke_assert_code(
+        ret_code=66,
+        args=[
+            parse,
+            [
+                "-n",
+                "--dialect",
+                "postgres",
+                "test/fixtures/cli/fail_many.sql",
+            ],
+        ],
+    )
+    assert (
+        "WARNING: Parsing errors found and dialect is set to 'postgres'. "
+        "Have you configured your dialect correctly?" in result.stdout
+    )
+
+
+def test__cli__command_parse_error_dialect_implicit_warning():
+    """Check parsing error raises the right warning."""
+    # For any parsing error there should be a non-zero exit code
+    # and a human-readable warning should be dislayed.
+    # Dialect specified in .sqlfluff config.
+    result = invoke_assert_code(
+        ret_code=66,
+        args=[
+            # Config sets dialect to tsql
+            parse,
+            [
+                "-n",
+                "--config",
+                "test/fixtures/cli/extra_configs/.sqlfluff",
+                "test/fixtures/cli/fail_many.sql",
+            ],
+        ],
+    )
+    assert (
+        "WARNING: Parsing errors found and dialect is set to 'tsql'. "
+        "Have you configured your dialect correctly?" in result.stdout
+    )
+
+
 def test__cli__command_dialect_legacy():
     """Check the script raises the right exception on a legacy dialect."""
     result = invoke_assert_code(