Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ module.exports = grammar({

conflicts: $ => [
[$.object_reference, $._qualified_field],
[$.field, $._qualified_field],
[$._column, $._qualified_field],
[$.object_reference],
[$.between_expression, $.binary_expression],
[$.time],
Expand Down Expand Up @@ -117,6 +119,8 @@ module.exports = grammar({
keyword_columns: _ => make_keyword("columns"),
keyword_materialized: _ => make_keyword("materialized"),
keyword_tablespace: _ => make_keyword("tablespace"),
keyword_split: _ => make_keyword("split"),
keyword_tablets: _ => make_keyword("tablets"),
keyword_sequence: _ => make_keyword("sequence"),
keyword_increment: _ => make_keyword("increment"),
keyword_minvalue: _ => make_keyword("minvalue"),
Expand Down Expand Up @@ -148,6 +152,7 @@ module.exports = grammar({
keyword_using: _ => make_keyword("using"),
keyword_use: _ => make_keyword("use"),
keyword_index: _ => make_keyword("index"),
keyword_include: _ => make_keyword("include"),
keyword_for: _ => make_keyword("for"),
keyword_if: _ => make_keyword("if"),
keyword_exists: _ => make_keyword("exists"),
Expand Down Expand Up @@ -1422,6 +1427,16 @@ module.exports = grammar({
)
),

composite_field: $ => seq(
wrapped_in_parenthesis(
comma_list(
alias($._index_field, $.field),
true,
),
),
optional($.keyword_hash),
),

_index_field: $ => seq(
choice(
field("expression", wrapped_in_parenthesis($._expression)),
Expand All @@ -1430,7 +1445,7 @@ module.exports = grammar({
),
optional(seq($.keyword_collate, $.identifier)),
optional($._operator_class),
optional($.direction),
optional(choice($.keyword_hash, $.direction)),
optional(
seq(
$.keyword_nulls,
Expand All @@ -1442,7 +1457,22 @@ module.exports = grammar({
),
),

index_fields: $ => wrapped_in_parenthesis(comma_list(alias($._index_field, $.field))),
index_fields: $ => wrapped_in_parenthesis(
comma_list(
choice(
$.composite_field,
alias($._index_field, $.field),
),
true,
),
),
tablespace: $ => seq($.keyword_tablespace, $.identifier),
tablet_split: $ => seq($.keyword_split, $.keyword_into, $._natural_number, $.keyword_tablets),

covering_columns: $ => seq(
$.keyword_include,
$.index_fields,
),

create_index: $ => seq(
$.keyword_create,
Expand Down Expand Up @@ -1474,6 +1504,9 @@ module.exports = grammar({
),
$.index_fields
),
optional($.covering_columns),
optional($.tablespace),
optional($.tablet_split),
optional(
$.where,
),
Expand Down
3 changes: 3 additions & 0 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
(keyword_constraint)
(keyword_force)
(keyword_use)
(keyword_include)
(keyword_for)
(keyword_if)
(keyword_exists)
Expand Down Expand Up @@ -258,6 +259,8 @@
(keyword_start)
(keyword_restart)
(keyword_tablespace)
(keyword_split)
(keyword_tablets)
(keyword_until)
(keyword_user)
(keyword_valid)
Expand Down
118 changes: 118 additions & 0 deletions test/corpus/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,121 @@ CREATE UNIQUE INDEX foo_index ON foo (
left: (field
name: (identifier))
right: (literal)))))))

================================================================================
hash sharding index
================================================================================

CREATE INDEX idx_sharding_clustering
ON tree.sitter (shard_1 HASH, foo, bar);

--------------------------------------------------------------------------------

(program
(statement
(create_index
(keyword_create)
(keyword_index)
column: (identifier)
(keyword_on)
(object_reference
schema: (identifier)
name: (identifier))
(index_fields
(field
column: (identifier)
(keyword_hash))
(field
column: (identifier))
(field
column: (identifier))))))

================================================================================
hash sharding composite index
================================================================================

CREATE INDEX idx_sharding_clustering
ON tree.sitter ((shard_1, shard_2) HASH, foo, bar);

--------------------------------------------------------------------------------

(program
(statement
(create_index
(keyword_create)
(keyword_index)
column: (identifier)
(keyword_on)
(object_reference
schema: (identifier)
name: (identifier))
(index_fields
(composite_field
(field
column: (identifier))
(field
column: (identifier))
(keyword_hash))
(field
column: (identifier))
(field
column: (identifier))))))

================================================================================
Create covering index
================================================================================

CREATE INDEX idx_covering ON tree.sitter (other_id, foo) INCLUDE (bar, baz);

--------------------------------------------------------------------------------

(program
(statement
(create_index
(keyword_create)
(keyword_index)
column: (identifier)
(keyword_on)
(object_reference
schema: (identifier)
name: (identifier))
(index_fields
(field
column: (identifier))
(field
column: (identifier)))
(covering_columns
(keyword_include)
(index_fields
(field
column: (identifier))
(field
column: (identifier)))))))

================================================================================
index with TABLESPACE & Tablet Split
================================================================================

CREATE INDEX code_idx ON films (code) TABLESPACE indexspace SPLIT INTO 2 TABLETS;

--------------------------------------------------------------------------------

(program
(statement
(create_index
(keyword_create)
(keyword_index)
column: (identifier)
(keyword_on)
(object_reference
name: (identifier))
(index_fields
(field
column: (identifier)))
(tablespace
(keyword_tablespace)
(identifier))
(tablet_split
(keyword_split)
(keyword_into)
(keyword_tablets)))))
Loading