-
Notifications
You must be signed in to change notification settings - Fork 4.5k
[Spanner Change Streams] Parse proto bytes change stream record in ChangeStreamRecordMapper #37427
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
chenxuesdu
wants to merge
1
commit into
apache:master
Choose a base branch
from
chenxuesdu:master
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.
+486
−7
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
Some comments aren't visible on the classic Files Changed page.
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
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 |
|---|---|---|
|
|
@@ -218,18 +218,28 @@ public class ChangeStreamRecordMapper { | |
| * @param resultSet the change stream result set | ||
| * @param resultSetMetadata the metadata generated when reading the change stream row | ||
| * @return a {@link List} of {@link ChangeStreamRecord} subclasses | ||
| * @throws InvalidProtocolBufferException | ||
| */ | ||
| public List<ChangeStreamRecord> toChangeStreamRecords( | ||
| PartitionMetadata partition, | ||
| ChangeStreamResultSet resultSet, | ||
| ChangeStreamResultSetMetadata resultSetMetadata) { | ||
| if (this.isPostgres()) { | ||
| // In PostgresQL, change stream records are returned as JsonB. | ||
| // For `MUTABLE_KEY_RANGE` option, change stream records are returned as protos. | ||
| if (resultSet.isProtoBytesChangeRecord()) { | ||
|
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. Maybe we have the getBytes(0) reflected here to match up getPgJsonb(0) so that it is very clear |
||
| return Arrays.asList( | ||
| toChangeStreamRecord( | ||
| partition, resultSet.getProtoChangeStreamRecordFromBytes(), resultSetMetadata)); | ||
| } | ||
|
|
||
| // For `IMMUTABLE_KEY_RANGE` option, change stream records are returned as | ||
| // JsonB. | ||
| return Collections.singletonList( | ||
| toChangeStreamRecordJson(partition, resultSet.getPgJsonb(0), resultSetMetadata)); | ||
| } | ||
|
|
||
| // In GoogleSQL, for `MUTABLE_KEY_RANGE` option, change stream records are returned as Protos. | ||
| // In GoogleSQL, for `MUTABLE_KEY_RANGE` option, change stream records are | ||
| // returned as Protos. | ||
| if (resultSet.isProtoChangeRecord()) { | ||
| return Arrays.asList( | ||
| toChangeStreamRecord( | ||
|
|
||
61 changes: 61 additions & 0 deletions
61
.../java/org/apache/beam/sdk/io/gcp/spanner/changestreams/dao/ChangeStreamResultSetTest.java
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 |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.beam.sdk.io.gcp.spanner.changestreams.dao; | ||
|
|
||
| import static org.apache.beam.sdk.io.gcp.spanner.changestreams.util.TestProtoMapper.recordToProto; | ||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertNotNull; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import com.google.cloud.ByteArray; | ||
| import com.google.cloud.Timestamp; | ||
| import com.google.cloud.spanner.ResultSet; | ||
| import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.HeartbeatRecord; | ||
| import org.junit.Test; | ||
|
|
||
| public class ChangeStreamResultSetTest { | ||
|
|
||
| @Test | ||
| public void testGetProtoChangeStreamRecordFromBytes() throws Exception { | ||
| // 1. Create an expected ChangeStreamRecord proto | ||
| Timestamp now = Timestamp.now(); | ||
| final HeartbeatRecord heartbeatRecord = | ||
| new HeartbeatRecord(Timestamp.ofTimeSecondsAndNanos(10L, 20), null); | ||
| com.google.spanner.v1.ChangeStreamRecord expectedRecord = recordToProto(heartbeatRecord); | ||
| assertNotNull(expectedRecord); | ||
|
|
||
| // 2. Convert it to bytes (simulating how Spanner PostgreSQL returns it) | ||
| byte[] protoBytes = expectedRecord.toByteArray(); | ||
|
|
||
| // 3. Mock the underlying Spanner ResultSet | ||
| ResultSet mockResultSet = mock(ResultSet.class); | ||
| // Simulate column 0 containing the BYTES representation of the proto | ||
| when(mockResultSet.getBytes(0)).thenReturn(ByteArray.copyFrom(protoBytes)); | ||
|
|
||
| // 4. Initialize ChangeStreamResultSet with the mock | ||
| ChangeStreamResultSet changeStreamResultSet = new ChangeStreamResultSet(mockResultSet); | ||
|
|
||
| // 5. Call the new method and assert it parses correctly | ||
| // (Note: This assumes you have added getProtoChangeStreamRecordFromBytes to the class) | ||
| com.google.spanner.v1.ChangeStreamRecord actualRecord = | ||
| changeStreamResultSet.getProtoChangeStreamRecordFromBytes(); | ||
|
|
||
| assertEquals(expectedRecord, actualRecord); | ||
| } | ||
| } |
Oops, something went wrong.
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.
Can we add accurate comments for this function, following similar convention for other functions? Thanks.
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.
Added comments. Please take a look.