Skip to content

Update ChangeStreamDao to query different TVF for postgresSQL based on the change stream partition mode#36667

Merged
Abacn merged 1 commit into
apache:masterfrom
chenxuesdu:master
Jan 26, 2026
Merged

Update ChangeStreamDao to query different TVF for postgresSQL based on the change stream partition mode#36667
Abacn merged 1 commit into
apache:masterfrom
chenxuesdu:master

Conversation

@chenxuesdu

Copy link
Copy Markdown
Contributor

Add a private variable isMutableKeyRange, which will be initialized during ChangeStreamDao constructor by reading from internal spanner table.

In function changeStreamQuery, when constructing change stream query, based on the value of isMutableKeyRange, call different TVFs when the Dialect is PostgreSQL(The googleSQL part is completed).

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @chenxuesdu, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the Spanner change stream connector by introducing logic to adapt query behavior for PostgreSQL dialects based on the change stream's partition mode. Specifically, it allows the system to correctly query change streams that use either MUTABLE_KEY_RANGE or IMMUTABLE_KEY_RANGE partitioning by selecting the appropriate internal Spanner Table-Valued Function, ensuring proper data retrieval and compatibility.

Highlights

  • Dynamic TVF Selection for PostgreSQL: The ChangeStreamDao now dynamically selects between 'read_proto_bytes_' and 'read_json_' Table-Valued Functions (TVFs) for PostgreSQL change stream queries, based on the change stream's partition mode.
  • isMutableKeyRange Field: A new private boolean field, 'isMutableKeyRange', has been introduced in ChangeStreamDao to store the detected partition mode.
  • Constructor Initialization: The 'isMutableKeyRange' field is initialized during the ChangeStreamDao constructor by querying the 'information_schema.change_stream_options' table to determine the change stream's partition mode.
  • New Test File: A new test file, ChangeStreamDaoTest.java, has been added to validate the logic for determining and using the 'isMutableKeyRange' property.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@chenxuesdu chenxuesdu changed the title Update ChangeStreamDao to query differnet TVF for postgresSQL based on the change stream partition mode Update ChangeStreamDao to query different TVF for postgresSQL based on the change stream partition mode Oct 29, 2025
@chenxuesdu chenxuesdu force-pushed the master branch 8 times, most recently from 473321f to 88ea37b Compare October 29, 2025 22:28
@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @m-trieu for label java.
R: @nielm for label spanner.

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

Statement.newBuilder(
"select option_name, option_value\n"
+ "from information_schema.change_stream_options\n"
+ "where change_stream_name = $1")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add option_name = 'partition_mode' here so we can get the parititon mode directly and no need to filter later?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

Statement.newBuilder(
"select option_name, option_value\n"
+ "from information_schema.change_stream_options\n"
+ "where change_stream_name = @changeStreamName")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

query =
"SELECT * FROM \"spanner\".\"read_json_" + changeStreamName + "\"($1, $2, $3, $4, null)";
// Ensure we have determined whether change stream uses mutable key range
boolean isMutable = isMutableKeyRangeChangeStream();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this called each time we issue the query? As our query will be constantly checkpointed and canceled and re-issued, this can be called many times.

I think we just need to query the partition mode once for the whole pipeline.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the logic to SpannerIO so it will only be executed once when connector starts. Please take a look. Thanks.

@chenxuesdu chenxuesdu force-pushed the master branch 5 times, most recently from d5f8b77 to 9209310 Compare October 31, 2025 17:17
@github-actions

github-actions Bot commented Nov 8, 2025

Copy link
Copy Markdown
Contributor

Reminder, please take a look at this pr: @m-trieu @nielm

@derrickaw

Copy link
Copy Markdown
Collaborator

waiting on author

}

@Test
public void testFetchPartitionModeGoogleSql() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we parameterized the tests to have different combination among googlesql/postgresql vs immuatable/mutable/empty string ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do it in a separate PR.

public void testWithDefaultCredential() {
// Get the default credential, without setting any credentials in the pipeline options or
// Get the default credential, without setting any credentials in the pipeline
// options or

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following 2 lines of comments can be combined.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. Thanks.


@Test
public void testFetchPartitionModeGoogleSql() {
DatabaseClient databaseClient = Mockito.mock(DatabaseClient.class);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines are the same for all tests below, can they be refactored to avoid repeatition? Thanks.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored. Please take a look.

ResultSet resultSet = Mockito.mock(ResultSet.class);

when(databaseClient.readOnlyTransaction()).thenReturn(transaction);
when(transaction.executeQuery(any(Statement.class))).thenReturn(resultSet);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If through mock, how the fetch option sql statement syntax in SpannerIO.java is tested to be correct?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the test. please take a look.

SpannerIO.isMutableChangeStream(
databaseClient, Dialect.GOOGLE_STANDARD_SQL, TEST_CHANGE_STREAM));
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also should the the failure or throw exception case of fetch option sql statement be tested as well?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the test. Please take a look.

the change stream partition mode

For MUTABLE_KEY_RANGE change stream, use read_proto_bytes_, else use read_json_
@github-actions

Copy link
Copy Markdown
Contributor

Reminder, please take a look at this pr: @m-trieu @nielm

@github-actions

Copy link
Copy Markdown
Contributor

Assigning new set of reviewers because Pr has gone too long without review. If you would like to opt out of this review, comment assign to next reviewer:

R: @Abacn for label java.
R: @nielm for label spanner.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

import org.mockito.ArgumentCaptor;

@RunWith(JUnit4.class)
@RunWith(Enclosed.class)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this one is Enclosed.class? How is it different from JUnit4.class?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enclosed is used to support parameterized test.

@tianz101 tianz101 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Abacn Abacn merged commit 6a399df into apache:master Jan 26, 2026
16 checks passed
aIbrahiim pushed a commit to aIbrahiim/beam that referenced this pull request Jan 28, 2026
apache#36667)

the change stream partition mode

For MUTABLE_KEY_RANGE change stream, use read_proto_bytes_, else use read_json_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants