Repository: DerekStride/tree-sitter-sql (language: python)

Issue:
specific statement after a specific pg alter table is not parsed correctly
```sql
ALTER TABLE users ALTER COLUMN email SET STATISTICS = 100;

SELECT attname FROM pg_stats;
```

The above snippet is generating the following tree
```
(program ; [0, 0] - [3, 0]
  (ERROR ; [0, 0] - [2, 29]
    (keyword_alter) ; [0, 0] - [0, 5]
    (keyword_table) ; [0, 6] - [0, 11]
    (object_reference ; [0, 12] - [0, 17]
      name: (identifier)) ; [0, 12] - [0, 17]
    (keyword_alter) ; [0, 18] - [0, 23]
    (keyword_column) ; [0, 24] - [0, 30]
    (identifier) ; [0, 31] - [0, 36]
    (keyword_set) ; [0, 37] - [0, 40]
    (keyword_select) ; [2, 0] - [2, 6]
    (keyword_from))) ; [2, 15] - [2, 19]
```

The expected is after the `;` 'closes' the error statement and start a new one.
If we replace the column name on select statement to `*` it works fine:

```
(program ; [0, 0] - [3, 0]
  (ERROR ; [0, 0] - [0, 58]
    (keyword_alter) ; [0, 0] - [0, 5]
    (keyword_table) ; [0, 6] - [0, 11]
    (object_reference ; [0, 12] - [0, 17]
      name: (identifier)) ; [0, 12] - [0, 17]
    (keyword_alter) ; [0, 18] - [0, 23]
    (keyword_column) ; [0, 24] - [0, 30]
    (identifier) ; [0, 31] - [0, 36]
    (keyword_set)) ; [0, 37] - [0, 40]
  (statement ; [2, 0] - [2, 22]
    (select ; [2, 0] - [2, 8]
      (keyword_select) ; [2, 0] - [2, 6]
      (select_expression ; [2, 7] - [2, 8]
        (term ; [2, 7] - [2, 8]
          value: (all_fields)))) ; [2, 7] - [2, 8]
    (from ; [2, 9] - [2, 22]
      (keyword_from) ; [2, 9] - [2, 13]
      (relation ; [2, 14] - [2, 22]
        (object_reference ; [2, 14] - [2, 22]
          name: (identifier)))))) ; [2, 14] - [2, 22]
```


Discussion / hints:
I think `SET STATISTICS` is not implemented yet. I can take a deeper look this evening. See https://github.com/DerekStride/tree-sitter-sql/blob/70ba8a5dd00c1229069684ea80d56035e75691a5/grammar.js#L1700


In your second tree, you still have an `ERROR` at the beginning.
I see, yes that is right the `SET STATISTICS` is not implemented yet.

Are you sure that the syntax in your first query is correct?
The documentation says, that there is no `=` between `STATISTICS` and the integer.
https://www.postgresql.org/docs/current/sql-altertable.html

Fix patch:
diff --git a/grammar.js b/grammar.js
index 8ce538e7..99f57229 100644
--- a/grammar.js
+++ b/grammar.js
@@ -261,6 +261,11 @@ module.exports = grammar({
     keyword_header: _ => make_keyword("header"),
     keyword_match: _ => make_keyword("match"),
     keyword_program: _ => make_keyword("program"),
+    keyword_plain: _ => make_keyword("plain"),
+    keyword_extended: _ => make_keyword("extended"),
+    keyword_main: _ => make_keyword("main"),
+    keyword_storage: _ => make_keyword("storage"),
+    keyword_compression: _ => make_keyword("compression"),
 
     keyword_trigger: _ => make_keyword('trigger'),
     keyword_function: _ => make_keyword("function"),
@@ -1725,8 +1730,33 @@ module.exports = grammar({
         ),
         seq(
           $.keyword_set,
-          $.keyword_default,
-          $._expression,
+          choice(
+            seq(
+              $.keyword_statistics,
+              field('statistics', $._integer)
+            ),
+            seq(
+              $.keyword_storage,
+              choice(
+                $.keyword_plain,
+                $.keyword_external,
+                $.keyword_extended,
+                $.keyword_main,
+                $.keyword_default,
+              ),
+            ),
+            seq(
+              $.keyword_compression,
+              field('compression_method', $._identifier)
+            ),
+            seq(
+              paren_list($._key_value_pair, true),
+            ),
+            seq(
+              $.keyword_default,
+              $._expression,
+            ),
+          )
         ),
         seq(
           $.keyword_drop,
diff --git a/queries/highlights.scm b/queries/highlights.scm
index 2ed3bdd0..c152913b 100644
--- a/queries/highlights.scm
+++ b/queries/highlights.scm
@@ -317,6 +317,11 @@
   (keyword_program)
   (keyword_quote)
   (keyword_stdin)
+  (keyword_extended)
+  (keyword_main)
+  (keyword_plain)
+  (keyword_storage)
+  (keyword_compression)
 ] @keyword
 
 [


Test patch:
diff --git a/test/corpus/alter.txt b/test/corpus/alter.txt
index 6ee401ed..05f7ccbd 100644
--- a/test/corpus/alter.txt
+++ b/test/corpus/alter.txt
@@ -1199,3 +1199,134 @@ ALTER TABLE table_name
               (identifier))
             (column
               (identifier))))))))
+
+
+================================================================================
+Alter Column Set Storage
+================================================================================
+
+ALTER TABLE table_name
+    ALTER COLUMN col1 SET STORAGE PLAIN;
+ALTER TABLE table_name
+    ALTER COLUMN col1 SET STORAGE EXTERNAL;
+ALTER TABLE table_name
+    ALTER COLUMN col1 SET STORAGE EXTENDED;
+ALTER TABLE table_name
+    ALTER COLUMN col1 SET STORAGE MAIN;
+ALTER TABLE table_name
+    ALTER COLUMN col1 SET STORAGE DEFAULT;
+
+--------------------------------------------------------------------------------
+
+(program
+  (statement
+    (alter_table
+      (keyword_alter)
+      (keyword_table)
+      (object_reference
+        (identifier))
+      (alter_column
+        (keyword_alter)
+        (keyword_column)
+        (identifier)
+        (keyword_set)
+        (keyword_storage)
+        (keyword_plain))))
+  (statement
+    (alter_table
+      (keyword_alter)
+      (keyword_table)
+      (object_reference
+        (identifier))
+      (alter_column
+        (keyword_alter)
+        (keyword_column)
+        (identifier)
+        (keyword_set)
+        (keyword_storage)
+        (keyword_external))))
+  (statement
+    (alter_table
+      (keyword_alter)
+      (keyword_table)
+      (object_reference
+        (identifier))
+      (alter_column
+        (keyword_alter)
+        (keyword_column)
+        (identifier)
+        (keyword_set)
+        (keyword_storage)
+        (keyword_extended))))
+  (statement
+    (alter_table
+      (keyword_alter)
+      (keyword_table)
+      (object_reference
+        (identifier))
+      (alter_column
+        (keyword_alter)
+        (keyword_column)
+        (identifier)
+        (keyword_set)
+        (keyword_storage)
+        (keyword_main))))
+  (statement
+    (alter_table
+      (keyword_alter)
+      (keyword_table)
+      (object_reference
+        (identifier))
+      (alter_column
+        (keyword_alter)
+        (keyword_column)
+        (identifier)
+        (keyword_set)
+        (keyword_storage)
+        (keyword_default)))))
+
+================================================================================
+Alter Column Set Compression
+================================================================================
+
+ALTER TABLE table_name
+    ALTER COLUMN col1 SET COMPRESSION lz4;
+
+--------------------------------------------------------------------------------
+
+(program
+  (statement
+    (alter_table
+      (keyword_alter)
+      (keyword_table)
+      (object_reference
+        (identifier))
+      (alter_column
+        (keyword_alter)
+        (keyword_column)
+        (identifier)
+        (keyword_set)
+        (keyword_compression)))))
+
+================================================================================
+Alter Column Set Statistics
+================================================================================
+
+ALTER TABLE table_name
+    ALTER COLUMN col1 SET STATISTICS 100;
+
+--------------------------------------------------------------------------------
+
+(program
+  (statement
+    (alter_table
+      (keyword_alter)
+      (keyword_table)
+      (object_reference
+        (identifier))
+      (alter_column
+        (keyword_alter)
+        (keyword_column)
+        (identifier)
+        (keyword_set)
+        (keyword_statistics)))))