Skip to content

Commit

Permalink
fix: delta generation for SEARCH INDEX as the ASC keyword is not supp…
Browse files Browse the repository at this point in the history
…orted (#174)

* fix: delta generation for SEARCH INDEX as the ASC keyword is not supported in columns of index
* fix: parser definitions for create_search_index

---------

Co-authored-by: Niel Markwick <[email protected]>
  • Loading branch information
gurminder71 and nielm authored Oct 5, 2024
1 parent c367a81 commit 897d4b0
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,18 +189,17 @@ private void generateAlterStatementsFor(
// Look for differences in tokenKeyList
// 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.
Map<String, ASTkey_part> originalKeyParts =
getChildByType(original.children, ASTtoken_key_list.class).getKeyParts().stream()
Map<String, ASTpath> originalKeyParts =
getChildByType(original.children, ASTtoken_key_list.class).getPaths().stream()
.collect(
Collectors.toMap(
ASTkey_part::toString, Function.identity(), (x, y) -> y, LinkedHashMap::new));
Map<String, ASTkey_part> newKeyParts =
getChildByType(other.children, ASTtoken_key_list.class).getKeyParts().stream()
ASTpath::toString, Function.identity(), (x, y) -> y, LinkedHashMap::new));
Map<String, ASTpath> newKeyParts =
getChildByType(other.children, ASTtoken_key_list.class).getPaths().stream()
.collect(
Collectors.toMap(
ASTkey_part::toString, Function.identity(), (x, y) -> y, LinkedHashMap::new));
MapDifference<String, ASTkey_part> keyPartsDiff =
Maps.difference(originalKeyParts, newKeyParts);
ASTpath::toString, Function.identity(), (x, y) -> y, LinkedHashMap::new));
MapDifference<String, ASTpath> keyPartsDiff = Maps.difference(originalKeyParts, newKeyParts);

// Look for differences in storedColumnList
// Easiest is to use Maps.difference, but first we need some maps, and we need to preserve order
Expand Down Expand Up @@ -234,17 +233,17 @@ private void generateAlterStatementsFor(
Maps.difference(originalStoredColumns, newStoredColumns);

if (allowDropColumnStatements) {
for (ASTkey_part droppedToken : keyPartsDiff.entriesOnlyOnLeft().values()) {
for (ASTpath droppedTokenCol : keyPartsDiff.entriesOnlyOnLeft().values()) {
LOG.info(
"Dropping token colum {} for search index: {}",
droppedToken.getKeyPath(),
droppedTokenCol.toString(),
original.getName());

dropStatements.add(
"ALTER SEARCH INDEX "
+ original.getName()
+ " DROP COLUMN "
+ droppedToken.getKeyPath());
+ droppedTokenCol.toString());
}
}

Expand All @@ -261,9 +260,9 @@ private void generateAlterStatementsFor(
+ droppedStoredCol.toString());
}

for (ASTkey_part newToken : keyPartsDiff.entriesOnlyOnRight().values()) {
for (ASTpath newToken : keyPartsDiff.entriesOnlyOnRight().values()) {
LOG.info(
"Adding token colum {} for search index: {}", newToken.getKeyPath(), original.getName());
"Adding token colum {} for search index: {}", newToken.toString(), original.getName());

createStatements.add(
"ALTER SEARCH INDEX " + original.getName() + " ADD COLUMN " + newToken.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ public ASTtoken_key_list(DdlParser p, int id) {
}

private void validateChildren() {
AstTreeUtils.validateChildrenClasses(children, ImmutableSet.of(ASTkey_part.class));
AstTreeUtils.validateChildrenClasses(children, ImmutableSet.of(ASTpath.class));
}

@Override
public String toString() {
validateChildren();
return "( " + Joiner.on(", ").join(getKeyParts()) + " )";
return "( " + Joiner.on(", ").join(getPaths()) + " )";
}

public List<ASTkey_part> getKeyParts() {
return AstTreeUtils.getChildrenAssertType(children, ASTkey_part.class);
public List<ASTpath> getPaths() {
return AstTreeUtils.getChildrenAssertType(children, ASTpath.class);
}

@Override
Expand Down
8 changes: 4 additions & 4 deletions src/main/jjtree-sources/ddl_parser.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ void alter_search_index_statement() :
<SEARCH><INDEX> (qualified_identifier() #name)
( LOOKAHEAD(2) <ADD> <STORED> <COLUMN> stored_column() #add_stored_column
| LOOKAHEAD(2) <DROP> <STORED> <COLUMN> identifier() #drop_stored_column_name
| LOOKAHEAD(2) <ADD> <COLUMN> key_part() #add_token_column
| LOOKAHEAD(2) <DROP> <COLUMN> key_part() #drop_token_column
| LOOKAHEAD(2) <ADD> <COLUMN> path() #add_token_column
| LOOKAHEAD(2) <DROP> <COLUMN> path() #drop_token_column
)
}

Expand Down Expand Up @@ -410,8 +410,8 @@ void token_key_list() :
{}
{
"("
[ key_part()
("," key_part() )*
[ path()
("," path() )*
]
")"
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/ddlParserValidation.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ CREATE CHANGE STREAM change_stream_name FOR table1, table2 ( ), table3 ( col1, c
== Test 13a create search index full

CREATE SEARCH INDEX AlbumsIndex
ON Albums ( AlbumTitle_Tokens ASC, Rating_Tokens ASC )
ON Albums ( AlbumTitle_Tokens, Rating_Tokens )
STORING ( Genre, albumName )
PARTITION BY SingerId ASC
ORDER BY ReleaseTimestamp DESC
Expand All @@ -148,7 +148,7 @@ OPTIONS (sort_order_sharding=TRUE)
== Test 13b create search index Simple

CREATE SEARCH INDEX AlbumsIndex
ON Albums ( AlbumTitle_Tokens ASC )
ON Albums ( AlbumTitle_Tokens )
OPTIONS (sort_order_sharding=TRUE)

==
6 changes: 3 additions & 3 deletions src/test/resources/expectedDdlDiff.txt
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ ALTER INDEX test4 ADD STORED COLUMN col5

CREATE TABLE test2 ( col1 INT64 ) PRIMARY KEY (col1 ASC)

CREATE SEARCH INDEX AlbumsIndex ON Albums ( AlbumTitle_Tokens ASC )
CREATE SEARCH INDEX AlbumsIndex ON Albums ( AlbumTitle_Tokens )

== TEST 56 Dropping search index - before table dropping

Expand All @@ -310,12 +310,12 @@ ALTER SEARCH INDEX AlbumsIndex DROP COLUMN col2
ALTER SEARCH INDEX AlbumsIndex DROP STORED COLUMN scol2
ALTER TABLE test1 DROP COLUMN col2
ALTER TABLE test1 ADD COLUMN col3 INT64
ALTER SEARCH INDEX AlbumsIndex ADD COLUMN col3 ASC
ALTER SEARCH INDEX AlbumsIndex ADD COLUMN col3
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
ALTER SEARCH INDEX AlbumsIndex ADD COLUMN col3

== TEST 59 Add stored col to search index

Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/originalDdl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ create table test1 ( col1 int64 ) primary key (col1);

create table test2 ( col1 int64 ) primary key (col1);

CREATE SEARCH INDEX AlbumsIndex ON Albums ( AlbumTitle_Tokens ASC, Rating_Tokens ASC )
CREATE SEARCH INDEX AlbumsIndex ON Albums ( AlbumTitle_Tokens, Rating_Tokens )

== TEST 57 Changing search index - before and after table changes

Expand Down

0 comments on commit 897d4b0

Please sign in to comment.