Repository: sqlfluff/sqlfluff

Issue:
Extra space (indentation) added before "FROM" in CTE
-- If this is a parsing or linting issue, please include a minimal SQL example which reproduces the issue, along with the `sqlfluff parse` output, `sqlfluff lint` output and `sqlfluff fix` output when relevant. Finally, please add any labels that are related to the issue before submitting so that it can be addressed easily! --

Starting query:
```
WITH
correctly_substituted AS (
    SELECT
        COUNT(1) AS count_correctly_substituted
    FROM
        all_upstream_matches
)

SELECT
    count_correctly_substituted
FROM
    correctly_substituted
```

## Expected Behaviour
In a CTE, the `FROM` should be aligned with `SELECT`.

## Observed Behaviour
In this case, the `FROM` was  indented one extra space.

```
WITH
correctly_substituted AS (
    SELECT COUNT(1) AS count_correctly_substituted
     FROM
        all_upstream_matches
)

SELECT count_correctly_substituted
FROM
    correctly_substituted
```

## Steps to Reproduce

```
sqlfluff fix test.sql
```
## Version
Include the output of `sqlfluff --version` along with your Python version
SQLFluff 0.5.3
Python 3.7.3

## Configuration
```
[sqlfluff]
verbose = 0
nocolor = False
dialect = bigquery
templater = jinja
rules = None
exclude_rules = L025,L027,L029,L031,L032,L034
recurse = 0

[sqlfluff:templater:jinja:macros]
# Macros provided as builtins for dbt projects
dbt_ref = {% macro ref(model_ref) %}{{model_ref}}{% endmacro %}
dbt_config = {% macro config() %}{% for k in kwargs %}{% endfor %}{% endmacro %}

# Some rules can be configured directly from the config common to other rules.
[sqlfluff:rules]
tab_space_size = 4
max_line_length = 120

# Some rules have their own specific config.
[sqlfluff:rules:L010]
capitalisation_policy = consistent

[sqlfluff:rules:L016]
max_line_length = 100

[sqlfluff:rules:L014]
capitalisation_policy = consistent

[sqlfluff:rules:L030]
capitalisation_policy = consistent

[sqlfluff:rules:L047]
prefer_count_1 = True

# Settings in this section will be provided to SQL templates during the SQL
# formatting/linting process. Generally, the exact values don't matter as long
# as they result in valid SQL syntax when substituted in the file.
[sqlfluff:templater:jinja:context]
```


Discussion:

Patch:
diff --git a/src/sqlfluff/rules/L036.py b/src/sqlfluff/rules/L036.py
index d49de4d94..f9edbe4ad 100644
--- a/src/sqlfluff/rules/L036.py
+++ b/src/sqlfluff/rules/L036.py
@@ -157,6 +157,25 @@ class Rule_L036(BaseRule):
                     select_clause.segments[select_targets_info.first_select_target_idx],
                 ),
             ]
+            if (
+                select_targets_info.first_select_target_idx
+                - select_targets_info.first_new_line_idx
+                == 2
+                and select_clause.segments[
+                    select_targets_info.first_new_line_idx + 1
+                ].is_whitespace
+            ):
+                # If the select target is preceded by a single whitespace
+                # segment, delete that as well. This addresses the bug fix
+                # tested in L036.yml's "test_cte" scenario.
+                fixes.append(
+                    LintFix(
+                        "delete",
+                        select_clause.segments[
+                            select_targets_info.first_new_line_idx + 1
+                        ],
+                    ),
+                )
             if parent_stack and parent_stack[-1].type == "select_statement":
                 select_stmt = parent_stack[-1]
                 select_clause_idx = select_stmt.segments.index(select_clause)


Test patch:
diff --git a/test/fixtures/rules/std_rule_cases/L036.yml b/test/fixtures/rules/std_rule_cases/L036.yml
index 70e4676e0..dcd98982a 100644
--- a/test/fixtures/rules/std_rule_cases/L036.yml
+++ b/test/fixtures/rules/std_rule_cases/L036.yml
@@ -13,11 +13,9 @@ test_single_select_target_and_new_line_before_select_target:
       select
           a
       from x
-  # In a normal "sqlfluff fix" run, the extra spaces in fix_str would be removed
-  # by a different rule, L039.
   fix_str: |
     select a
-        from x
+    from x
 
 test_multiple_select_targets_on_new_lines_and_new_line_after_select:
   pass_str: |
@@ -101,3 +99,26 @@ test_multiple_select_targets_some_newlines_missing_2:
     g,
       h
     from x
+
+test_cte:
+  fail_str: |
+    WITH
+    cte1 AS (
+        SELECT
+            c1 AS c
+        FROM
+            t
+    )
+
+    SELECT 1
+    FROM cte1
+  fix_str: |
+    WITH
+    cte1 AS (
+        SELECT c1 AS c
+        FROM
+            t
+    )
+
+    SELECT 1
+    FROM cte1