Skip to content

Commit caf211a

Browse files
authored
fix: handle cases where there are no stored columns in search indexes (#136)
Fixes #118
1 parent 9b8bb81 commit caf211a

5 files changed

Lines changed: 74 additions & 14 deletions

File tree

src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTcreate_search_index_statement.java

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -205,22 +205,31 @@ private void generateAlterStatementsFor(
205205
// Look for differences in storedColumnList
206206
// Easiest is to use Maps.difference, but first we need some maps, and we need to preserve order
207207
// so convert the keyParts to String, and then add to a LinkedHashMap.
208+
ASTstored_column_list origStoredColList =
209+
getOptionalChildByType(original.children, ASTstored_column_list.class);
208210
Map<String, ASTstored_column> originalStoredColumns =
209-
getChildByType(original.children, ASTstored_column_list.class).getStoredColumns().stream()
210-
.collect(
211-
Collectors.toMap(
212-
ASTstored_column::toString,
213-
Function.identity(),
214-
(x, y) -> y,
215-
LinkedHashMap::new));
211+
origStoredColList == null
212+
? Map.of()
213+
: origStoredColList.getStoredColumns().stream()
214+
.collect(
215+
Collectors.toMap(
216+
ASTstored_column::toString,
217+
Function.identity(),
218+
(x, y) -> y,
219+
LinkedHashMap::new));
220+
221+
ASTstored_column_list newStoredColList =
222+
getOptionalChildByType(other.children, ASTstored_column_list.class);
216223
Map<String, ASTstored_column> newStoredColumns =
217-
getChildByType(other.children, ASTstored_column_list.class).getStoredColumns().stream()
218-
.collect(
219-
Collectors.toMap(
220-
ASTstored_column::toString,
221-
Function.identity(),
222-
(x, y) -> y,
223-
LinkedHashMap::new));
224+
newStoredColList == null
225+
? Map.of()
226+
: newStoredColList.getStoredColumns().stream()
227+
.collect(
228+
Collectors.toMap(
229+
ASTstored_column::toString,
230+
Function.identity(),
231+
(x, y) -> y,
232+
LinkedHashMap::new));
224233
MapDifference<String, ASTstored_column> storedColDiff =
225234
Maps.difference(originalStoredColumns, newStoredColumns);
226235

src/test/resources/ddlParserValidation.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,5 +149,6 @@ OPTIONS (sort_order_sharding=TRUE)
149149

150150
CREATE SEARCH INDEX AlbumsIndex
151151
ON Albums ( AlbumTitle_Tokens ASC )
152+
OPTIONS (sort_order_sharding=TRUE)
152153

153154
==

src/test/resources/expectedDdlDiff.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,4 +313,18 @@ ALTER TABLE test1 ADD COLUMN col3 INT64
313313
ALTER SEARCH INDEX AlbumsIndex ADD COLUMN col3 ASC
314314
ALTER SEARCH INDEX AlbumsIndex ADD STORED COLUMN scol3
315315

316+
== TEST 58 Add col to search index - no stored columns
317+
318+
ALTER SEARCH INDEX AlbumsIndex ADD COLUMN col3 ASC
319+
320+
== TEST 59 Add stored col to search index
321+
322+
ALTER SEARCH INDEX AlbumsIndex ADD STORED COLUMN scol1
323+
324+
== TEST 60 Remove stored cols from search index
325+
326+
ALTER SEARCH INDEX AlbumsIndex DROP STORED COLUMN scol1
327+
316328
==
329+
330+

src/test/resources/newDdl.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,4 +489,21 @@ CREATE SEARCH INDEX AlbumsIndex
489489
ON Albums (col1, col3)
490490
STORING (scol1, scol3);
491491

492+
== TEST 58 Add col to search index - no stored columns
493+
494+
CREATE SEARCH INDEX AlbumsIndex
495+
ON Albums (col1, col2, col3)
496+
497+
== TEST 59 Add stored col to search index
498+
499+
CREATE SEARCH INDEX AlbumsIndex
500+
ON Albums (col1, col2)
501+
STORING (scol1);
502+
503+
== TEST 60 Remove stored cols from search index
504+
505+
CREATE SEARCH INDEX AlbumsIndex
506+
ON Albums (col1, col2)
507+
492508
==
509+

src/test/resources/originalDdl.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,25 @@ CREATE SEARCH INDEX AlbumsIndex
487487
ON Albums (col1, col2)
488488
STORING (scol1, scol2);
489489

490+
== TEST 58 Add col to search index - no stored columns
491+
492+
CREATE SEARCH INDEX AlbumsIndex
493+
ON Albums (col1, col2)
494+
495+
== TEST 59 Add stored col to search index
496+
497+
CREATE SEARCH INDEX AlbumsIndex
498+
ON Albums (col1, col2)
499+
500+
== TEST 60 Remove stored cols from search index
501+
502+
CREATE SEARCH INDEX AlbumsIndex
503+
ON Albums (col1, col2)
504+
STORING (scol1)
505+
490506
==
491507

492508

509+
510+
511+

0 commit comments

Comments
 (0)