diff --git a/grammar.js b/grammar.js index 88ea0ce..ffc82c5 100644 --- a/grammar.js +++ b/grammar.js @@ -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], @@ -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"), @@ -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"), @@ -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)), @@ -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, @@ -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, @@ -1474,6 +1504,9 @@ module.exports = grammar({ ), $.index_fields ), + optional($.covering_columns), + optional($.tablespace), + optional($.tablet_split), optional( $.where, ), diff --git a/queries/highlights.scm b/queries/highlights.scm index d64bd9d..adc1867 100644 --- a/queries/highlights.scm +++ b/queries/highlights.scm @@ -147,6 +147,7 @@ (keyword_constraint) (keyword_force) (keyword_use) + (keyword_include) (keyword_for) (keyword_if) (keyword_exists) @@ -258,6 +259,8 @@ (keyword_start) (keyword_restart) (keyword_tablespace) + (keyword_split) + (keyword_tablets) (keyword_until) (keyword_user) (keyword_valid) diff --git a/test/corpus/index.txt b/test/corpus/index.txt index ed108ff..80f6819 100644 --- a/test/corpus/index.txt +++ b/test/corpus/index.txt @@ -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)))))