DerekStride / tree-sitter-sql

SQL grammar for tree-sitter
http://derek.stride.host/tree-sitter-sql/
MIT License
159 stars 54 forks source link

specific statement after a specific pg alter table is not parsed correctly #258

Closed alanoliveira closed 5 months ago

alanoliveira commented 5 months ago
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]
matthias-Q commented 5 months ago

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.

alanoliveira commented 5 months ago

I see, yes that is right the SET STATISTICS is not implemented yet.

matthias-Q commented 5 months ago

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

alanoliveira commented 5 months ago

omg, my bad sorry