Repository: sqlfluff/sqlfluff

Issue:
SQLFluff cannot parse queries that use BigQueryML functions
Input:
```
WITH
  confusion_matrix AS (
  SELECT
    expected_label,
    commerce,
    digital,
    traditional_services
  FROM
    ML.CONFUSION_MATRIX(MODEL model3,
      (
      SELECT
        *
      FROM
        table1
      WHERE
        training = 0 )))

SELECT
  *,
  commerce pct_commerce
FROM
  confusion_matrix
```

I ran `sqlfluff fix --rules L003 test.sql`. Output (note the parse error):
```
==== finding violations ====
== [test.sql] FAIL
L:   2 | P:   3 | L003 | Indentation not consistent with line #1
L:   3 | P:   3 | L003 | Indentation not hanging or a multiple of 4 spaces
L:   8 | P:   3 | L003 | Indentation not hanging or a multiple of 4 spaces
L:   9 | P:  24 |  PRS | Found unparsable segment @L009P024: '(MODEL model3,\n     ...'
L:  10 | P:   7 | L003 | Indentation not hanging or a multiple of 4 spaces
L:  11 | P:   7 | L003 | Indentation not hanging or a multiple of 4 spaces
L:  12 | P:   9 | L003 | Line over-indented compared to line #1
L:  13 | P:   7 | L003 | Indentation not hanging or a multiple of 4 spaces
L:  14 | P:   9 | L003 | Line over-indented compared to line #1
L:  15 | P:   7 | L003 | Indentation not hanging or a multiple of 4 spaces
L:  16 | P:   9 | L003 | Line over-indented compared to line #1
L:  19 | P:   3 | L003 | Indentation not hanging or a multiple of 4 spaces
L:  20 | P:   3 | L003 | Indentation not hanging or a multiple of 4 spaces
L:  22 | P:   3 | L003 | Indentation not hanging or a multiple of 4 spaces
==== fixing violations ====
13 linting violations found
Are you sure you wish to attempt to fix these? [Y/n] ...
Attempting fixes...
Persisting Changes...
== [test.sql] PASS
```

Discussion:

Patch:
diff --git a/src/sqlfluff/core/rules/std/L031.py b/src/sqlfluff/core/rules/std/L031.py
index 6fcf03777..046908f1b 100644
--- a/src/sqlfluff/core/rules/std/L031.py
+++ b/src/sqlfluff/core/rules/std/L031.py
@@ -59,6 +59,8 @@ class Rule_L031(BaseCrawler):
                 return None
 
             table_expression = from_clause_segment.get_child("table_expression")
+            if not table_expression:
+                return None
             table_expression = table_expression.get_child("main_table_expression")
 
             # Find base table


Test patch:
diff --git a/test/core/rules/test_cases/L031.yml b/test/core/rules/test_cases/L031.yml
index 7f1a69a5d..284a865ec 100644
--- a/test/core/rules/test_cases/L031.yml
+++ b/test/core/rules/test_cases/L031.yml
@@ -118,3 +118,32 @@ issue_635:
   configs:
     core:
       dialect: snowflake
+
+# This query was causing a runtime error in the rule.
+issue_239:
+  pass_str: |
+    WITH
+      confusion_matrix AS (
+      SELECT
+        expected_label,
+        commerce,
+        digital,
+        traditional_services
+      FROM
+        ML.CONFUSION_MATRIX(MODEL model3,
+          (
+          SELECT
+            *
+          FROM
+            table1
+          WHERE
+            training = 0 )))
+
+    SELECT
+      *,
+      commerce pct_commerce
+    FROM
+      confusion_matrix
+  configs:
+    core:
+      dialect: bigquery