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

Issue:
Support for check constraints
The parser does not currently seem to support check constraints, for example:

```sql
CREATE TABLE products (
    product_no integer,
    name text,
    price numeric CHECK (price > 0)
);
```

For reference: https://www.postgresql.org/docs/current/ddl-constraints.html#DDL-CONSTRAINTS-CHECK-CONSTRAINTS


Discussion / hints:
If I have time I will take a look.

Fix patch:
diff --git a/grammar.js b/grammar.js
index aa2bbfad..46014146 100644
--- a/grammar.js
+++ b/grammar.js
@@ -2544,6 +2544,7 @@ module.exports = grammar({
       $.keyword_auto_increment,
       $.direction,
       $._column_comment,
+      $._check_constraint,
       seq(
         optional(seq($.keyword_generated, $.keyword_always)),
         $.keyword_as,
@@ -2555,6 +2556,17 @@ module.exports = grammar({
       )
     )),
 
+    _check_constraint: $ => seq(
+      optional(
+        seq(
+          $.keyword_constraint,
+          $.literal
+        )
+      ),
+      $.keyword_check,
+      wrapped_in_parenthesis($.binary_expression)
+    ),
+
     _default_expression: $ => seq(
       $.keyword_default,
       optional_parenthesis($._inner_default_expression),
@@ -2583,13 +2595,21 @@ module.exports = grammar({
       $._constraint_literal,
       $._key_constraint,
       $._primary_key_constraint,
+      $._check_constraint
     ),
 
     _constraint_literal: $ => seq(
       $.keyword_constraint,
       field('name', $.identifier),
-      $._primary_key,
-      $.ordered_columns,
+      choice(
+        seq(
+          $._primary_key,
+          $.ordered_columns,
+        ),
+        seq(
+          $._check_constraint
+        )
+      )
     ),
 
     _primary_key_constraint: $ => seq(


Test patch:
diff --git a/test/corpus/create.txt b/test/corpus/create.txt
index cd3c3766..2e66be84 100644
--- a/test/corpus/create.txt
+++ b/test/corpus/create.txt
@@ -215,6 +215,62 @@ CREATE TABLE my_table (
               (column
                 name: (identifier)))))))))
 
+================================================================================
+Create table with constraint checks
+================================================================================
+
+CREATE TABLE products (
+    product_no integer,
+    name text,
+    price numeric CHECK (price > 0),
+    discounted_price numeric CHECK (discounted_price > 0),
+    CHECK (price > discounted_price)
+);
+
+--------------------------------------------------------------------------------
+
+(program
+  (statement
+    (create_table
+      (keyword_create)
+      (keyword_table)
+      (object_reference
+        (identifier))
+      (column_definitions
+        (column_definition
+          (identifier)
+          (int
+            (keyword_int)))
+        (column_definition
+          (identifier)
+          (keyword_text))
+        (column_definition
+          (identifier)
+          (numeric
+            (keyword_numeric))
+          (keyword_check)
+          (binary_expression
+            (field
+              (identifier))
+            (literal)))
+        (column_definition
+          (identifier)
+          (numeric
+            (keyword_numeric))
+          (keyword_check)
+          (binary_expression
+            (field
+              (identifier))
+            (literal)))
+        (constraints
+          (constraint
+            (keyword_check)
+            (binary_expression
+              (field
+                (identifier))
+              (field
+                (identifier)))))))))
+
 ================================================================================
 Create table if not exists
 ================================================================================