Repository navigation
[Spanner Change Stream] Fix Change Streams PostgreSQL metadata queries #40755
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jiangzzhu
wants to merge
1
commit into
apache:master
Choose a base branch
from
jiangzzhu:fix-changestream-pg-and-schema-bugs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,8 +18,10 @@ | |
| package org.apache.beam.sdk.io.gcp.spanner.changestreams.dao; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assert.assertNotNull; | ||
| import static org.junit.Assert.assertNull; | ||
| import static org.junit.Assert.assertTrue; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.doNothing; | ||
| import static org.mockito.Mockito.mock; | ||
|
|
@@ -31,7 +33,9 @@ | |
| import com.google.cloud.spanner.DatabaseClient; | ||
| import com.google.cloud.spanner.Dialect; | ||
| import com.google.cloud.spanner.Mutation; | ||
| import com.google.cloud.spanner.ReadOnlyTransaction; | ||
| import com.google.cloud.spanner.ResultSet; | ||
| import com.google.cloud.spanner.Statement; | ||
| import com.google.cloud.spanner.Struct; | ||
| import com.google.cloud.spanner.TransactionContext; | ||
| import com.google.cloud.spanner.TransactionRunner; | ||
|
|
@@ -406,4 +410,81 @@ public void testExtractTvfName() { | |
| assertEquals("tvf", PartitionMetadataDao.extractTvfName("partitionToken123#tvf")); | ||
| assertEquals("", PartitionMetadataDao.extractTvfName("partitionToken123")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTableExistsGoogleSql() { | ||
| ReadOnlyTransaction readOnlyTx = mock(ReadOnlyTransaction.class); | ||
| ResultSet resultSet = mock(ResultSet.class); | ||
| when(databaseClient.singleUseReadOnlyTransaction()).thenReturn(readOnlyTx); | ||
| when(readOnlyTx.executeQuery(any(Statement.class), any())).thenReturn(resultSet); | ||
| when(resultSet.next()).thenReturn(true); | ||
|
|
||
| assertTrue(partitionMetadataDao.tableExists()); | ||
|
|
||
| ArgumentCaptor<Statement> statementCaptor = ArgumentCaptor.forClass(Statement.class); | ||
| verify(readOnlyTx).executeQuery(statementCaptor.capture(), any()); | ||
| assertEquals( | ||
| "SELECT t.table_name FROM information_schema.tables AS t " | ||
| + "WHERE t.table_catalog = '' AND " | ||
| + "t.table_schema = '' AND " | ||
| + "t.table_name = '" | ||
| + METADATA_TABLE_NAME | ||
| + "'", | ||
| statementCaptor.getValue().getSql()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTableExistsPostgres() { | ||
| ReadOnlyTransaction readOnlyTx = mock(ReadOnlyTransaction.class); | ||
| ResultSet resultSet = mock(ResultSet.class); | ||
| when(databaseClient.singleUseReadOnlyTransaction()).thenReturn(readOnlyTx); | ||
| when(readOnlyTx.executeQuery(any(Statement.class), any())).thenReturn(resultSet); | ||
| when(resultSet.next()).thenReturn(true).thenReturn(false); | ||
|
|
||
| PartitionMetadataDao pgDao = | ||
| new PartitionMetadataDao(METADATA_TABLE_NAME, databaseClient, Dialect.POSTGRESQL); | ||
| assertTrue(pgDao.tableExists()); | ||
| assertFalse(pgDao.tableExists()); | ||
|
|
||
| ArgumentCaptor<Statement> statementCaptor = ArgumentCaptor.forClass(Statement.class); | ||
| verify(readOnlyTx, times(2)).executeQuery(statementCaptor.capture(), any()); | ||
| assertEquals( | ||
| "SELECT t.table_name FROM information_schema.tables AS t " | ||
| + "WHERE t.table_schema = 'public' AND " | ||
| + "t.table_name = '" | ||
| + METADATA_TABLE_NAME | ||
| + "'", | ||
| statementCaptor.getValue().getSql()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testInTransactionContextUpdateToScheduledPostgres() { | ||
| PartitionMetadataDao.InTransactionContext pgTransactionContext = | ||
| new PartitionMetadataDao.InTransactionContext( | ||
| METADATA_TABLE_NAME, transaction, Dialect.POSTGRESQL); | ||
| ResultSet resultSet = mock(ResultSet.class); | ||
| when(transaction.executeQuery(any(), any())).thenReturn(resultSet); | ||
| when(resultSet.next()).thenReturn(true).thenReturn(false); | ||
| when(resultSet.getString(any())).thenReturn(PARTITION_TOKEN); | ||
|
|
||
| ArgumentCaptor<Statement> statementCaptor = ArgumentCaptor.forClass(Statement.class); | ||
| ArgumentCaptor<ImmutableList<Mutation>> mutations = | ||
| ArgumentCaptor.forClass(ImmutableList.class); | ||
| doNothing().when(transaction).buffer(mutations.capture()); | ||
|
|
||
| assertNull(pgTransactionContext.updateToScheduled(Collections.singletonList(PARTITION_TOKEN))); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add test coverage of 0 and multiple tokens as well |
||
|
|
||
| verify(transaction).executeQuery(statementCaptor.capture(), any()); | ||
| Statement captured = statementCaptor.getValue(); | ||
| assertEquals( | ||
| "SELECT * FROM \"" | ||
| + METADATA_TABLE_NAME | ||
| + "\" WHERE \"State\" = $1 AND \"PartitionToken\" = ANY ($2)", | ||
| captured.getSql()); | ||
| assertEquals(State.CREATED.toString(), captured.getParameters().get("p1").getString()); | ||
| assertEquals( | ||
| Collections.singletonList(PARTITION_TOKEN), | ||
| captured.getParameters().get("p2").getStringArray()); | ||
| assertEquals(1, mutations.getValue().size()); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to change the behavior for empty composedPartitionTokens array.
Previously it wouldn't check COLUMN_PARTITION_TOKEN but now I think that it will do ANY() which evaluates to false.
Maybe have a branch on if composedPartitionTokens is empty and have two statements you bind to?