Skip to content

Commit ead7111

Browse files
authored
Merge pull request #338 from DerekStride/index-covering
Add support for covering indexes, hash sharding, & tablespace
2 parents e2181ab + c353f5d commit ead7111

File tree

3 files changed

+156
-2
lines changed

3 files changed

+156
-2
lines changed

grammar.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ module.exports = grammar({
1717

1818
conflicts: $ => [
1919
[$.object_reference, $._qualified_field],
20+
[$.field, $._qualified_field],
21+
[$._column, $._qualified_field],
2022
[$.object_reference],
2123
[$.between_expression, $.binary_expression],
2224
[$.time],
@@ -117,6 +119,8 @@ module.exports = grammar({
117119
keyword_columns: _ => make_keyword("columns"),
118120
keyword_materialized: _ => make_keyword("materialized"),
119121
keyword_tablespace: _ => make_keyword("tablespace"),
122+
keyword_split: _ => make_keyword("split"),
123+
keyword_tablets: _ => make_keyword("tablets"),
120124
keyword_sequence: _ => make_keyword("sequence"),
121125
keyword_increment: _ => make_keyword("increment"),
122126
keyword_minvalue: _ => make_keyword("minvalue"),
@@ -148,6 +152,7 @@ module.exports = grammar({
148152
keyword_using: _ => make_keyword("using"),
149153
keyword_use: _ => make_keyword("use"),
150154
keyword_index: _ => make_keyword("index"),
155+
keyword_include: _ => make_keyword("include"),
151156
keyword_for: _ => make_keyword("for"),
152157
keyword_if: _ => make_keyword("if"),
153158
keyword_exists: _ => make_keyword("exists"),
@@ -1422,6 +1427,16 @@ module.exports = grammar({
14221427
)
14231428
),
14241429

1430+
composite_field: $ => seq(
1431+
wrapped_in_parenthesis(
1432+
comma_list(
1433+
alias($._index_field, $.field),
1434+
true,
1435+
),
1436+
),
1437+
optional($.keyword_hash),
1438+
),
1439+
14251440
_index_field: $ => seq(
14261441
choice(
14271442
field("expression", wrapped_in_parenthesis($._expression)),
@@ -1430,7 +1445,7 @@ module.exports = grammar({
14301445
),
14311446
optional(seq($.keyword_collate, $.identifier)),
14321447
optional($._operator_class),
1433-
optional($.direction),
1448+
optional(choice($.keyword_hash, $.direction)),
14341449
optional(
14351450
seq(
14361451
$.keyword_nulls,
@@ -1442,7 +1457,22 @@ module.exports = grammar({
14421457
),
14431458
),
14441459

1445-
index_fields: $ => wrapped_in_parenthesis(comma_list(alias($._index_field, $.field))),
1460+
index_fields: $ => wrapped_in_parenthesis(
1461+
comma_list(
1462+
choice(
1463+
$.composite_field,
1464+
alias($._index_field, $.field),
1465+
),
1466+
true,
1467+
),
1468+
),
1469+
tablespace: $ => seq($.keyword_tablespace, $.identifier),
1470+
tablet_split: $ => seq($.keyword_split, $.keyword_into, $._natural_number, $.keyword_tablets),
1471+
1472+
covering_columns: $ => seq(
1473+
$.keyword_include,
1474+
$.index_fields,
1475+
),
14461476

14471477
create_index: $ => seq(
14481478
$.keyword_create,
@@ -1474,6 +1504,9 @@ module.exports = grammar({
14741504
),
14751505
$.index_fields
14761506
),
1507+
optional($.covering_columns),
1508+
optional($.tablespace),
1509+
optional($.tablet_split),
14771510
optional(
14781511
$.where,
14791512
),

queries/highlights.scm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@
147147
(keyword_constraint)
148148
(keyword_force)
149149
(keyword_use)
150+
(keyword_include)
150151
(keyword_for)
151152
(keyword_if)
152153
(keyword_exists)
@@ -258,6 +259,8 @@
258259
(keyword_start)
259260
(keyword_restart)
260261
(keyword_tablespace)
262+
(keyword_split)
263+
(keyword_tablets)
261264
(keyword_until)
262265
(keyword_user)
263266
(keyword_valid)

test/corpus/index.txt

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,121 @@ CREATE UNIQUE INDEX foo_index ON foo (
111111
left: (field
112112
name: (identifier))
113113
right: (literal)))))))
114+
115+
================================================================================
116+
hash sharding index
117+
================================================================================
118+
119+
CREATE INDEX idx_sharding_clustering
120+
ON tree.sitter (shard_1 HASH, foo, bar);
121+
122+
--------------------------------------------------------------------------------
123+
124+
(program
125+
(statement
126+
(create_index
127+
(keyword_create)
128+
(keyword_index)
129+
column: (identifier)
130+
(keyword_on)
131+
(object_reference
132+
schema: (identifier)
133+
name: (identifier))
134+
(index_fields
135+
(field
136+
column: (identifier)
137+
(keyword_hash))
138+
(field
139+
column: (identifier))
140+
(field
141+
column: (identifier))))))
142+
143+
================================================================================
144+
hash sharding composite index
145+
================================================================================
146+
147+
CREATE INDEX idx_sharding_clustering
148+
ON tree.sitter ((shard_1, shard_2) HASH, foo, bar);
149+
150+
--------------------------------------------------------------------------------
151+
152+
(program
153+
(statement
154+
(create_index
155+
(keyword_create)
156+
(keyword_index)
157+
column: (identifier)
158+
(keyword_on)
159+
(object_reference
160+
schema: (identifier)
161+
name: (identifier))
162+
(index_fields
163+
(composite_field
164+
(field
165+
column: (identifier))
166+
(field
167+
column: (identifier))
168+
(keyword_hash))
169+
(field
170+
column: (identifier))
171+
(field
172+
column: (identifier))))))
173+
174+
================================================================================
175+
Create covering index
176+
================================================================================
177+
178+
CREATE INDEX idx_covering ON tree.sitter (other_id, foo) INCLUDE (bar, baz);
179+
180+
--------------------------------------------------------------------------------
181+
182+
(program
183+
(statement
184+
(create_index
185+
(keyword_create)
186+
(keyword_index)
187+
column: (identifier)
188+
(keyword_on)
189+
(object_reference
190+
schema: (identifier)
191+
name: (identifier))
192+
(index_fields
193+
(field
194+
column: (identifier))
195+
(field
196+
column: (identifier)))
197+
(covering_columns
198+
(keyword_include)
199+
(index_fields
200+
(field
201+
column: (identifier))
202+
(field
203+
column: (identifier)))))))
204+
205+
================================================================================
206+
index with TABLESPACE & Tablet Split
207+
================================================================================
208+
209+
CREATE INDEX code_idx ON films (code) TABLESPACE indexspace SPLIT INTO 2 TABLETS;
210+
211+
--------------------------------------------------------------------------------
212+
213+
(program
214+
(statement
215+
(create_index
216+
(keyword_create)
217+
(keyword_index)
218+
column: (identifier)
219+
(keyword_on)
220+
(object_reference
221+
name: (identifier))
222+
(index_fields
223+
(field
224+
column: (identifier)))
225+
(tablespace
226+
(keyword_tablespace)
227+
(identifier))
228+
(tablet_split
229+
(keyword_split)
230+
(keyword_into)
231+
(keyword_tablets)))))

0 commit comments

Comments
 (0)