Repository: sqlfluff/sqlfluff

Issue:
noqa: PRS emits ignored parsing erros on stdout
### Search before asking

- [X] I searched the [issues](https://github.com/sqlfluff/sqlfluff/issues) and found no similar issues.


### What Happened

Using `sqlfluff` as auto formatter, requires `-` to be clean. Right now, a log line is included with or without colored output and no verbosity. 

### Expected Behaviour

Log line is emitted on stderr.

### Observed Behaviour

Observe "  [0 templating/parsing errors remaining after "ignore" & "warning"]" on the first line of `formatted-noqa-psr.sql`:

```sql
  [0 templating/parsing errors remaining after "ignore" & "warning"]
select
  create_time as last,
  (
    count(
      *) over
    (
      order by create_time desc -- noqa: PRS
      range between current row and '10 seconds' following  -- noqa: PRS
    )
  ) * 0.1
  as count
from foo_bar
where create_time > '2023-01-12T00:00:00'
order by create_time
limit 1000
```

### How to reproduce

`noqa-psr.sql`:
```sql
select
  create_time as last,
  (
    count(
      *) over
    (
      order by create_time desc -- noqa: PRS
      range between current row and '10 seconds' following  -- noqa: PRS
    )
  ) * 0.1
  as count
from foo_bar
where create_time > '2023-01-12T00:00:00'
order by create_time
limit 1000
```

Format from stdin:
```
cat noqa-psr.sql | sqlfluff format --ignore-local-config -d postgres - > formatted-noqa-psr.sql
```

### Dialect

postgres

### Version

2.3.3 (same on 2.3.2)

### Configuration

default

### 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:
coveralls: 
[![Coverage Status](https://coveralls.io/builds/63606786/badge)](https://coveralls.io/builds/63606786)

coverage: 100.0%. remained the same when pulling **0cae285141dd60e1e790544b678b8dbb759398a7 on ac/logline** into **472f9b89984da6ee31069c023c2433ec0c5d0080 on main**.

github-actions[bot]: # Coverage Results ✅
```
Name    Stmts   Miss  Cover   Missing
-------------------------------------
TOTAL   16839      0   100%

221 files skipped due to complete coverage.

```


Patch:
diff --git a/src/sqlfluff/cli/commands.py b/src/sqlfluff/cli/commands.py
index 7a2fa3e38..cff65daa5 100644
--- a/src/sqlfluff/cli/commands.py
+++ b/src/sqlfluff/cli/commands.py
@@ -754,7 +754,9 @@ def _stdin_fix(linter: Linter, formatter, fix_even_unparsable: bool) -> None:
     templater_error = result.num_violations(types=SQLTemplaterError) > 0
     unfixable_error = result.num_violations(types=SQLLintError, fixable=False) > 0
     if not fix_even_unparsable:
-        exit_code = formatter.handle_files_with_tmp_or_prs_errors(result)
+        exit_code = formatter.handle_files_with_tmp_or_prs_errors(
+            result, force_stderr=True
+        )
 
     if result.num_violations(types=SQLLintError, fixable=True) > 0:
         stdout = result.paths[0].files[0].fix_string()[0]
diff --git a/src/sqlfluff/cli/formatters.py b/src/sqlfluff/cli/formatters.py
index af5d449fd..3eb3f5a8e 100644
--- a/src/sqlfluff/cli/formatters.py
+++ b/src/sqlfluff/cli/formatters.py
@@ -583,7 +583,9 @@ class OutputStreamFormatter:
             Color.lightgrey,
         )
 
-    def handle_files_with_tmp_or_prs_errors(self, lint_result: LintingResult) -> int:
+    def handle_files_with_tmp_or_prs_errors(
+        self, lint_result: LintingResult, force_stderr=False
+    ) -> int:
         """Discard lint fixes for files with templating or parse errors.
 
         Returns 1 if there are any files with templating or parse errors after
@@ -608,7 +610,7 @@ class OutputStreamFormatter:
                         color=color,
                     ),
                     color=not self.plain_output,
-                    err=num_filtered_errors > 0,
+                    err=force_stderr or num_filtered_errors > 0,
                 )
         return EXIT_FAIL if num_filtered_errors else EXIT_SUCCESS
 


Test patch:
diff --git a/test/cli/commands_test.py b/test/cli/commands_test.py
index a76a58444..efe41ceaa 100644
--- a/test/cli/commands_test.py
+++ b/test/cli/commands_test.py
@@ -1133,6 +1133,24 @@ def test__cli__command_fix_stdin(stdin, rules, stdout):
             "   select    *    FRoM     t    ",
             "select * from t\n",
         ),
+        (
+            # Check that warnings related to parsing errors on input don't
+            # go to stdout. This query shouldn't change, but stdout should
+            # remain clean.
+            # https://github.com/sqlfluff/sqlfluff/issues/5327
+            "select\n"
+            "    count(*) over (\n"
+            "        order by a desc \n"
+            "        range between b row and '10 seconds' following  -- noqa: PRS\n"
+            "    ) as c\n"
+            "from d\n",
+            "select\n"
+            "    count(*) over (\n"
+            "        order by a desc \n"
+            "        range between b row and '10 seconds' following  -- noqa: PRS\n"
+            "    ) as c\n"
+            "from d\n",
+        ),
     ],
 )
 def test__cli__command_format_stdin(stdin, stdout):
@@ -1143,8 +1161,9 @@ def test__cli__command_format_stdin(stdin, stdout):
             ("-", "--disable-progress-bar", "--dialect=ansi"),
         ],
         cli_input=stdin,
+        mix_stderr=False,
     )
-    assert result.output == stdout
+    assert result.stdout == stdout
 
 
 def test__cli__command_fix_stdin_logging_to_stderr(monkeypatch):