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

Issue:
`ERROR` on `COUNT(DISTINCT tbl.field)`
![image](https://github.com/DerekStride/tree-sitter-sql/assets/22329650/ab715000-55c5-4f46-be63-8ab682768bd3)

Tree-sitter syntax error for `tbl` and `field` in:

```sql
SELECT
  COUNT(DISTINCT `tbl`.`field`)
FROM
  whatever;
```



Discussion / hints:
I think this issue may be resolved when #150 is merged. The issue is that the `term` can be a `field` and there we have issue when the `field` is like scm.tbl.col.


Just to recap the tree here:
`invocation` -> `_select_expression` -> `term` -> `_expression` -> `field` -> [schema: `_alias_identifier`.][table_alias: `_alias_identifier`.]`identifier`

`_alias_identifier` -> `identifier` -> \``_idenfier`\`

I am a bit surprised that is does not work at the moment.

EDIT: Ah now I get it. The `distinct` is not registered correctly at all. It is interpreted as a field and the column afterwards as alias of that column



Ah yes. I did not pay attention to `DISTINCT`, since it did not error out. My mistake in not pointing that out!

`DISTINCT` is indeed mis-identified as `"field", "identifier"`:

![image](https://github.com/DerekStride/tree-sitter-sql/assets/22329650/654411a1-26ec-4384-9320-b584ae246779)

I am preparing a fix right now

Fix patch:
diff --git a/grammar.js b/grammar.js
index a5c9f2b9..41c43266 100644
--- a/grammar.js
+++ b/grammar.js
@@ -1940,7 +1940,15 @@ module.exports = grammar({
 
     invocation: $ => seq(
       field('name', $.identifier),
-      paren_list(field('parameter', $._select_expression)),
+      paren_list(
+        seq(
+          optional($.keyword_distinct),
+          field(
+            'parameter',
+            $._select_expression,
+          ),
+          optional($.order_by)
+        )),
     ),
 
     exists: $ => seq(
@@ -2241,11 +2249,11 @@ module.exports = grammar({
       $._expression,
     ),
 
-    order_by: $ => seq(
+    order_by: $ => prec.right(seq(
       $.keyword_order,
       $.keyword_by,
       comma_list($.order_target, true),
-    ),
+    )),
 
     order_target: $ => seq(
       $._expression,


Test patch:
diff --git a/test/corpus/functions.txt b/test/corpus/functions.txt
index ac32a406..a0e05d07 100644
--- a/test/corpus/functions.txt
+++ b/test/corpus/functions.txt
@@ -168,12 +168,16 @@ FROM table_a;
         (term
           value: (invocation
             name: (identifier)
+            (keyword_distinct)
             parameter: (term
               value: (field
-                name: (identifier))
-              alias: (identifier))
-            (ERROR
-              (keyword_by))))))
+                name: (identifier)))
+            (order_by
+              (keyword_order)
+              (keyword_by)
+              (order_target
+                (field
+                  name: (identifier))))))))
     (from
       (keyword_from)
       (relation
diff --git a/test/corpus/select.txt b/test/corpus/select.txt
index a44f269f..843aae81 100644
--- a/test/corpus/select.txt
+++ b/test/corpus/select.txt
@@ -474,10 +474,10 @@ FROM my_table;
         (term
           value: (invocation
             name: (identifier)
+            (keyword_distinct)
             parameter: (term
               value: (field
-                name: (identifier))
-              alias: (identifier)))
+                name: (identifier))))
           (keyword_as)
           alias: (identifier))))
     (from
@@ -727,12 +727,10 @@ FROM my_table;
         (term
           value: (invocation
             name: (identifier)
+            (keyword_distinct)
             parameter: (term
-              value: (invocation
-                name: (identifier)
-                parameter: (term
-                  value: (field
-                    name: (identifier)))))))))
+              value: (field
+                name: (identifier)))))))
     (from
       (keyword_from)
       (relation
@@ -778,10 +776,10 @@ FROM my_table;
         (term
           value: (invocation
             name: (identifier)
+            (keyword_distinct)
             parameter: (term
               value: (field
-                name: (identifier))
-              alias: (identifier))))))
+                name: (identifier)))))))
     (from
       (keyword_from)
       (relation