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

Issue:
PG: Create Table with Array types
Using the `[]` notation in PostgresSQL, it is possible to create columns that store arrays and matrices.

```sql
CREATE table tab (
    arr_col INT[],
    mat_col INT[][]
);
```

This is currently not handled.



Discussion / hints:

Fix patch:
diff --git a/grammar.js b/grammar.js
index dff9beae..390bf34a 100644
--- a/grammar.js
+++ b/grammar.js
@@ -384,61 +384,77 @@ module.exports = grammar({
 
     keyword_array: _ => make_keyword("array"), // not included in _type since it's a constructor literal
 
-    _type: $ => choice(
-      $.keyword_boolean,
-      $.bit,
-      $.keyword_binary,
-
-      $.keyword_smallserial,
-      $.keyword_serial,
-      $.keyword_bigserial,
-
-      $.tinyint,
-      $.smallint,
-      $.mediumint,
-      $.int,
-      $.bigint,
-      $.decimal,
-      $.numeric,
-      $.double,
-      $.float,
-
-      $.keyword_money,
-
-      $.char,
-      $.varchar,
-      $.numeric,
-      $.keyword_string,
-      $.keyword_text,
-
-      $.keyword_uuid,
-
-      $.keyword_json,
-      $.keyword_jsonb,
-      $.keyword_xml,
-
-      $.keyword_bytea,
-      $.keyword_inet,
-
-      $.enum,
-
-      $.keyword_date,
-      $.keyword_datetime,
-      $.keyword_timestamp,
-      $.keyword_timestamptz,
-      $.keyword_interval,
-
-      $.keyword_geometry,
-      $.keyword_geography,
-      $.keyword_box2d,
-      $.keyword_box3d,
-
-      $.keyword_oid,
-      $.keyword_name,
-      $.keyword_regclass,
-      $.keyword_regnamespace,
-      $.keyword_regproc,
-      $.keyword_regtype,
+    _type: $ => seq(
+      choice(
+        $.keyword_boolean,
+        $.bit,
+        $.keyword_binary,
+
+        $.keyword_smallserial,
+        $.keyword_serial,
+        $.keyword_bigserial,
+
+        $.tinyint,
+        $.smallint,
+        $.mediumint,
+        $.int,
+        $.bigint,
+        $.decimal,
+        $.numeric,
+        $.double,
+        $.float,
+
+        $.keyword_money,
+
+        $.char,
+        $.varchar,
+        $.numeric,
+        $.keyword_string,
+        $.keyword_text,
+
+        $.keyword_uuid,
+
+        $.keyword_json,
+        $.keyword_jsonb,
+        $.keyword_xml,
+
+        $.keyword_bytea,
+        $.keyword_inet,
+
+        $.enum,
+
+        $.keyword_date,
+        $.keyword_datetime,
+        $.keyword_timestamp,
+        $.keyword_timestamptz,
+        $.keyword_interval,
+
+        $.keyword_geometry,
+        $.keyword_geography,
+        $.keyword_box2d,
+        $.keyword_box3d,
+
+        $.keyword_oid,
+        $.keyword_name,
+        $.keyword_regclass,
+        $.keyword_regnamespace,
+        $.keyword_regproc,
+        $.keyword_regtype,
+      ),
+      optional($.array_size_definition)
+    ),
+
+    array_size_definition: $ => seq(
+      choice(
+        seq($.keyword_array, optional($._array_size_definition)),
+        repeat1($._array_size_definition),
+      ),
+    ),
+
+    _array_size_definition: $ => seq(
+      '[',
+      optional(field("size", alias($._integer, $.literal))),
+      ']'
     ),
 
     tinyint: $ => unsigned_type($, parametric_type($, $.keyword_tinyint)),


Test patch:
diff --git a/test/corpus/create.txt b/test/corpus/create.txt
index b754f0fe..335936ff 100644
--- a/test/corpus/create.txt
+++ b/test/corpus/create.txt
@@ -1555,3 +1555,70 @@ CREATE TABLE "Session" (
           type: (keyword_inet)
           (keyword_not)
           (keyword_null))))))
+
+================================================================================
+CREATE TABLE with ARRAYS and MATRIX
+================================================================================
+
+CREATE TABLE tab (
+    name   text,
+    array2 integer[3],
+    matrix text[][],
+    square integer[3][3],
+    array4 integer ARRAY[4],
+    array_ integer ARRAY,
+    multid integer[3][3][3][3]
+);
+
+--------------------------------------------------------------------------------
+
+(program
+  (statement
+    (create_table
+      (keyword_create)
+      (keyword_table)
+      (object_reference
+        (identifier))
+      (column_definitions
+        (column_definition
+          (identifier)
+          (keyword_text))
+        (column_definition
+          (identifier)
+          (int
+            (keyword_int))
+          (array_size_definition
+            (literal)))
+        (column_definition
+          (identifier)
+          (keyword_text)
+          (array_size_definition))
+        (column_definition
+          (identifier)
+          (int
+            (keyword_int))
+          (array_size_definition
+            (literal)
+            (literal)))
+        (column_definition
+          (identifier)
+          (int
+            (keyword_int))
+          (array_size_definition
+            (keyword_array)
+            (literal)))
+        (column_definition
+          (identifier)
+          (int
+            (keyword_int))
+          (array_size_definition
+            (keyword_array)))
+        (column_definition
+          (identifier)
+          (int
+            (keyword_int))
+          (array_size_definition
+            (literal)
+            (literal)
+            (literal)
+            (literal)))))))