From 3dfb44395a4e1538c566d1863c7633eb22d898e6 Mon Sep 17 00:00:00 2001 From: Niel Markwick Date: Wed, 19 Jun 2024 17:09:29 +0200 Subject: [PATCH] fix: handle cases where there are no stored columns in search indexes Fixes #118 --- .../ASTcreate_search_index_statement.java | 37 ++++++++++++------- src/test/resources/ddlParserValidation.txt | 1 + src/test/resources/expectedDdlDiff.txt | 14 +++++++ src/test/resources/newDdl.txt | 17 +++++++++ src/test/resources/originalDdl.txt | 19 ++++++++++ 5 files changed, 74 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTcreate_search_index_statement.java b/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTcreate_search_index_statement.java index 24ce51b..31c2df9 100644 --- a/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTcreate_search_index_statement.java +++ b/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTcreate_search_index_statement.java @@ -205,22 +205,31 @@ private void generateAlterStatementsFor( // Look for differences in storedColumnList // Easiest is to use Maps.difference, but first we need some maps, and we need to preserve order // so convert the keyParts to String, and then add to a LinkedHashMap. + ASTstored_column_list origStoredColList = + getOptionalChildByType(original.children, ASTstored_column_list.class); Map originalStoredColumns = - getChildByType(original.children, ASTstored_column_list.class).getStoredColumns().stream() - .collect( - Collectors.toMap( - ASTstored_column::toString, - Function.identity(), - (x, y) -> y, - LinkedHashMap::new)); + origStoredColList == null + ? Map.of() + : origStoredColList.getStoredColumns().stream() + .collect( + Collectors.toMap( + ASTstored_column::toString, + Function.identity(), + (x, y) -> y, + LinkedHashMap::new)); + + ASTstored_column_list newStoredColList = + getOptionalChildByType(other.children, ASTstored_column_list.class); Map newStoredColumns = - getChildByType(other.children, ASTstored_column_list.class).getStoredColumns().stream() - .collect( - Collectors.toMap( - ASTstored_column::toString, - Function.identity(), - (x, y) -> y, - LinkedHashMap::new)); + newStoredColList == null + ? Map.of() + : newStoredColList.getStoredColumns().stream() + .collect( + Collectors.toMap( + ASTstored_column::toString, + Function.identity(), + (x, y) -> y, + LinkedHashMap::new)); MapDifference storedColDiff = Maps.difference(originalStoredColumns, newStoredColumns); diff --git a/src/test/resources/ddlParserValidation.txt b/src/test/resources/ddlParserValidation.txt index 904cfdf..a89e36d 100644 --- a/src/test/resources/ddlParserValidation.txt +++ b/src/test/resources/ddlParserValidation.txt @@ -149,5 +149,6 @@ OPTIONS (sort_order_sharding=TRUE) CREATE SEARCH INDEX AlbumsIndex ON Albums ( AlbumTitle_Tokens ASC ) +OPTIONS (sort_order_sharding=TRUE) == diff --git a/src/test/resources/expectedDdlDiff.txt b/src/test/resources/expectedDdlDiff.txt index a8e29cb..78e4c31 100644 --- a/src/test/resources/expectedDdlDiff.txt +++ b/src/test/resources/expectedDdlDiff.txt @@ -313,4 +313,18 @@ ALTER TABLE test1 ADD COLUMN col3 INT64 ALTER SEARCH INDEX AlbumsIndex ADD COLUMN col3 ASC ALTER SEARCH INDEX AlbumsIndex ADD STORED COLUMN scol3 +== TEST 58 Add col to search index - no stored columns + +ALTER SEARCH INDEX AlbumsIndex ADD COLUMN col3 ASC + +== TEST 59 Add stored col to search index + +ALTER SEARCH INDEX AlbumsIndex ADD STORED COLUMN scol1 + +== TEST 60 Remove stored cols from search index + +ALTER SEARCH INDEX AlbumsIndex DROP STORED COLUMN scol1 + == + + diff --git a/src/test/resources/newDdl.txt b/src/test/resources/newDdl.txt index 6f32467..d340021 100644 --- a/src/test/resources/newDdl.txt +++ b/src/test/resources/newDdl.txt @@ -489,4 +489,21 @@ CREATE SEARCH INDEX AlbumsIndex ON Albums (col1, col3) STORING (scol1, scol3); +== TEST 58 Add col to search index - no stored columns + +CREATE SEARCH INDEX AlbumsIndex +ON Albums (col1, col2, col3) + +== TEST 59 Add stored col to search index + +CREATE SEARCH INDEX AlbumsIndex +ON Albums (col1, col2) +STORING (scol1); + +== TEST 60 Remove stored cols from search index + +CREATE SEARCH INDEX AlbumsIndex +ON Albums (col1, col2) + == + diff --git a/src/test/resources/originalDdl.txt b/src/test/resources/originalDdl.txt index 768f093..6a6c827 100644 --- a/src/test/resources/originalDdl.txt +++ b/src/test/resources/originalDdl.txt @@ -487,6 +487,25 @@ CREATE SEARCH INDEX AlbumsIndex ON Albums (col1, col2) STORING (scol1, scol2); +== TEST 58 Add col to search index - no stored columns + +CREATE SEARCH INDEX AlbumsIndex +ON Albums (col1, col2) + +== TEST 59 Add stored col to search index + +CREATE SEARCH INDEX AlbumsIndex +ON Albums (col1, col2) + +== TEST 60 Remove stored cols from search index + +CREATE SEARCH INDEX AlbumsIndex +ON Albums (col1, col2) +STORING (scol1) + == + + +