Repository: sqlfluff/sqlfluff (Python)

Issue:
In 0.5.6, setting `indented_joins` to `True` breaks parsing of table aliases
-- 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! --

## Steps to Reproduce

`test.sql`
```sql
select
    field_1
from my_table as alias_1
```

```
$ sqlfluff lint test.sql
== [test.sql] FAIL
L:   3 | P:   1 |  PRS | Found unparsable section: 'from my_table as alias_1'
```

## Version
Include the output of `sqlfluff --version` along with your Python version
`sqlfluff, version 0.5.6`
## Configuration
```
[sqlfluff]
# Need to ignore templating until we can use the dbt templater
ignore = templating
exclude_rules = L003,L004,L046,L013,L022,L027,L031,L033,L034,L035,L036,L037,L039

[sqlfluff:templater:jinja:context]
target = 'dev'

[sqlfluff:indentation]
indented_joins = True

[sqlfluff:rules]
max_line_length = 115

[sqlfluff:rules:L010]
capitalisation_policy = lower

```



Discussion:
Fascinating bug! I did some tinkering to try and narrow down the issue. This setting in `.sqlfluff` is the key: if you remove this, the error does not occur:
```
[sqlfluff:indentation]
indented_joins = True
```

@alanmcruickshank: Could this be related to the big segment refactoring? I know that it involved some deep changes to the `Indent` and `Dedent` segments.
I'll take a look - I think your intuition is probably right.
Oh man 🤦 . It's a stupid typo. PR coming shortly, just working out how to get a test case around it.



Patch:
diff --git a/src/sqlfluff/core/parser/grammar/sequence.py b/src/sqlfluff/core/parser/grammar/sequence.py
index 675e2f43d1b..51ab6e7942b 100644
--- a/src/sqlfluff/core/parser/grammar/sequence.py
+++ b/src/sqlfluff/core/parser/grammar/sequence.py
@@ -96,7 +96,7 @@ def match(self, segments, parse_context):
                     # We've run our of sequence without matching everything.
                     # Do only optional or meta elements remain?
                     if all(
-                        e.is_optional() or e.is_meta or isinstance(elem, Conditional)
+                        e.is_optional() or e.is_meta or isinstance(e, Conditional)
                         for e in self._elements[idx:]
                     ):
                         # then it's ok, and we can return what we've got so far.


Test patch:
diff --git a/test/dialects/ansi_test.py b/test/dialects/ansi_test.py
index 3669f11399b..2b041d4c81c 100644
--- a/test/dialects/ansi_test.py
+++ b/test/dialects/ansi_test.py
@@ -179,3 +179,35 @@ def test__dialect__ansi_is_whitespace():
     for raw_seg in parsed.tree.iter_raw_seg():
         if raw_seg.is_type("whitespace", "newline"):
             assert raw_seg.is_whitespace
+
+
+@pytest.mark.parametrize(
+    "sql_string, indented_joins,meta_loc",
+    [
+        ("select field_1 from my_table as alias_1", True, (1, 5, 8, 14)),
+        ("select field_1 from my_table as alias_1", False, (1, 5, 8, 14)),
+        (
+            "select field_1 from my_table as alias_1 join foo using (field_1)",
+            True,
+            (1, 5, 8, 16, 21, 24, 26, 28, 29, 30),
+        ),
+        (
+            "select field_1 from my_table as alias_1 join foo using (field_1)",
+            False,
+            (1, 5, 8, 15, 17, 22, 25, 27, 29, 30),
+        ),
+    ],
+)
+def test__dialect__ansi_parse_indented_joins(sql_string, indented_joins, meta_loc):
+    """Test parsing of meta segments using Conditional works with indented_joins."""
+    lnt = Linter(
+        config=FluffConfig(configs={"indentation": {"indented_joins": indented_joins}})
+    )
+    parsed = lnt.parse_string(sql_string)
+    # Check that there's nothing unparsable
+    assert "unparsable" not in parsed.tree.type_set()
+    # Check all the segments that *should* be whitespace, ARE
+    res_meta_locs = tuple(
+        idx for idx, raw_seg in enumerate(parsed.tree.iter_raw_seg()) if raw_seg.is_meta
+    )
+    assert res_meta_locs == meta_loc