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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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,173 @@ 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
Contributor
Author
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. Done — added |
||
|
|
||
| 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()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testInTransactionContextUpdateToScheduledPostgresEmptyTokens() { | ||
| 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(false); | ||
|
|
||
| ArgumentCaptor<Statement> statementCaptor = ArgumentCaptor.forClass(Statement.class); | ||
|
|
||
| assertNull(pgTransactionContext.updateToScheduled(Collections.emptyList())); | ||
|
|
||
| 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.emptyList(), captured.getParameters().get("p2").getStringArray()); | ||
| verify(transaction, times(0)).buffer(any(Iterable.class)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testInTransactionContextUpdateToScheduledPostgresMultipleTokens() { | ||
| 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(true).thenReturn(false); | ||
| when(resultSet.getString(PartitionMetadataAdminDao.COLUMN_PARTITION_TOKEN)) | ||
| .thenReturn("token1") | ||
| .thenReturn("token3"); | ||
|
|
||
| ArgumentCaptor<Statement> statementCaptor = ArgumentCaptor.forClass(Statement.class); | ||
| ArgumentCaptor<ImmutableList<Mutation>> mutations = | ||
| ArgumentCaptor.forClass(ImmutableList.class); | ||
| doNothing().when(transaction).buffer(mutations.capture()); | ||
|
|
||
| List<String> inputTokens = ImmutableList.of("token1", "token2", "token3"); | ||
| assertNull(pgTransactionContext.updateToScheduled(inputTokens)); | ||
|
|
||
| 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(inputTokens, captured.getParameters().get("p2").getStringArray()); | ||
|
|
||
| verify(transaction, times(2)).buffer(any(ImmutableList.class)); | ||
| List<ImmutableList<Mutation>> bufferedBatches = mutations.getAllValues(); | ||
| assertEquals(2, bufferedBatches.size()); | ||
| assertEquals( | ||
| "token1", | ||
| bufferedBatches | ||
| .get(0) | ||
| .get(0) | ||
| .asMap() | ||
| .get(PartitionMetadataAdminDao.COLUMN_PARTITION_TOKEN) | ||
| .getString()); | ||
| assertEquals( | ||
| State.SCHEDULED.toString(), | ||
| bufferedBatches | ||
| .get(0) | ||
| .get(0) | ||
| .asMap() | ||
| .get(PartitionMetadataAdminDao.COLUMN_STATE) | ||
| .getString()); | ||
| assertEquals( | ||
| "token3", | ||
| bufferedBatches | ||
| .get(1) | ||
| .get(0) | ||
| .asMap() | ||
| .get(PartitionMetadataAdminDao.COLUMN_PARTITION_TOKEN) | ||
| .getString()); | ||
| assertEquals( | ||
| State.SCHEDULED.toString(), | ||
| bufferedBatches | ||
| .get(1) | ||
| .get(0) | ||
| .asMap() | ||
| .get(PartitionMetadataAdminDao.COLUMN_STATE) | ||
| .getString()); | ||
| } | ||
| } | ||
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?
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.
Yeah, it seems change the behavior. However, this change may be safe
Here is why:
java
"SELECT * FROM " + metadataTableName
+ " WHERE " + COLUMN_PARTITION_TOKEN + " IN UNNEST(@composedPartitionTokens) AND "
+ COLUMN_STATE + " = @State"
In GoogleSQL, passing an empty list binds an empty ARRAY to @composedPartitionTokens, and IN UNNEST([]) evaluates to false (0 rows).
The only reason the original PostgreSQL code had if (!composedPartitionTokens.isEmpty()) was because it used raw SQL string concatenation (= ANY (Array[...])), where an untyped empty literal Array[] (without ::text[]) is a PostgreSQL syntax error (cannot determine type of empty array).
With a typed bound Spanner parameter $2 (Value.stringArray(...)), Spanner PostgreSQL resolves "PartitionToken" = ANY ($2) to the exact same $in_array(STRING, ARRAY) AST as GoogleSQL's IN UNNEST(@composedPartitionTokens) (verified in parameterized_queries.test), cleanly returning 0 rows when $2 is empty.