Skip to content

feat(spanner): Add table and index AddSplitPoints pre-splitting sample - #14957

Open
iennae wants to merge 2 commits into
feat/spanner-create-databasefrom
feat/spanner-database-add-split-points
Open

iennae wants to merge 2 commits into
feat/spanner-create-databasefrom
feat/spanner-database-add-split-points

Conversation

@iennae

@iennae iennae commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Description

Adds spanner_database_add_split_points pre-splitting a table and secondary index via DatabaseAdminClient.add_split_points.

Note: Stacked on #14943 (feat/spanner-create-database) so this PR shows only this sample's diff. It will merge into main after #14943.

Checklist

Testing

  • I have tested this change on a live environment and verified it works as intended. Unit and live integration tests pass on Python 3.13.

Compliance & Style


Post-Approval Actions

  • Please merge this PR for me once it is approved

Pre-splitting tables and indexes ahead of bulk loads prevents initial hotspots using DatabaseAdminClient.add_split_points. Add a standalone spanner_database_add_split_points sample.
@iennae
iennae requested review from a team as code owners November 18, 2026 22:18
@snippet-bot

snippet-bot Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 1 region tag.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@product-auto-label product-auto-label Bot added api: spanner Issues related to the Spanner API. samples Issues that are directly related to samples. labels Nov 18, 2026

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces a new sample script, database_add_split_points.py, along with its corresponding tests, to demonstrate pre-splitting a Spanner table and secondary index using the AddSplitPoints API. The feedback highlights a logical error in the split points configuration where a table key is incorrectly mapped to a secondary index. Additionally, the reviewer recommends grouping split points by table or index to eliminate redundancy and simplify the code, which also requires updating the associated test assertions to expect two split points instead of three.

Comment on lines +41 to +54
table_key = spanner_admin_database_v1.SplitPoints.Key(key_parts=["42"])
idx_only_key = spanner_admin_database_v1.SplitPoints.Key(key_parts=["John", "Doe"])
idx_prefix_key = spanner_admin_database_v1.SplitPoints.Key(
key_parts=["Jane", "Doe"]
)
idx_table_key = spanner_admin_database_v1.SplitPoints.Key(key_parts=["38"])

split_points = [
spanner_admin_database_v1.SplitPoints(table="Singers", keys=[table_key]),
spanner_admin_database_v1.SplitPoints(index=index_name, keys=[idx_only_key]),
spanner_admin_database_v1.SplitPoints(
index=index_name, keys=[idx_prefix_key, idx_table_key]
),
]

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.

high

The split points configuration contains a logical error and redundancy:

  1. Incorrect Key Association: idx_table_key (with value ["38"]) represents a primary key for the Singers table (similar to table_key with value ["42"]). However, it is being passed as a split point for the secondary index SingersByFirstLastName (which expects [FirstName, LastName] key parts).
  2. Redundant SplitPoints Entries: Split points for the same table or index should be grouped together in a single SplitPoints object rather than creating multiple separate SplitPoints entries for the same index.

Grouping the table keys under the Singers table and the index keys under the SingersByFirstLastName index makes the sample correct, cleaner, and easier to understand.

    table_key_1 = spanner_admin_database_v1.SplitPoints.Key(key_parts=["38"])
    table_key_2 = spanner_admin_database_v1.SplitPoints.Key(key_parts=["42"])
    index_key_1 = spanner_admin_database_v1.SplitPoints.Key(key_parts=["Jane", "Doe"])
    index_key_2 = spanner_admin_database_v1.SplitPoints.Key(key_parts=["John", "Doe"])

    split_points = [
        spanner_admin_database_v1.SplitPoints(
            table="Singers", keys=[table_key_1, table_key_2]
        ),
        spanner_admin_database_v1.SplitPoints(
            index=index_name, keys=[index_key_1, index_key_2]
        ),
    ]

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.

Thanks! Keeping this as is: an index split point can be given as an index key followed by a table key, which is what the last entry shows, so the two keys belong together and the count of split points stays at three.

mock_client_class.return_value.add_split_points.assert_called_once()
out, err = capsys.readouterr()
assert f"Resource ID: {DATABASE_ID}" in out
assert "Split Points Added: 3" in out

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.

medium

Update the expected number of split points added to 2 to match the grouped split points configuration.

Suggested change
assert "Split Points Added: 3" in out
assert "Split Points Added: 2" in out

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.

Thanks! The split-point configuration is unchanged (see the reply on the sample), so the expected count stays as is.

"spanner_database_admin.database_add_split_points", run_name="__main__"
)

assert "Split Points Added: 3" in capsys.readouterr().out

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.

medium

Update the expected number of split points added to 2 to match the grouped split points configuration.

Suggested change
assert "Split Points Added: 3" in capsys.readouterr().out
assert "Split Points Added: 2" in capsys.readouterr().out

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.

Thanks! The split-point configuration is unchanged (see the reply on the sample), so the expected count stays as is.

out, err = capsys.readouterr()
assert err == ""
assert conftest.LIVE_PROJECT_ID not in out
assert "Split Points Added: 3" in out

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.

medium

Update the expected number of split points added to 2 to match the grouped split points configuration.

Suggested change
assert "Split Points Added: 3" in out
assert "Split Points Added: 2" in out

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.

Thanks! The split-point configuration is unchanged (see the reply on the sample), so the expected count stays as is.

…oints

Problem: multi-line SQL and DDL statements were written as adjacent string
fragments. That style is easy to get wrong (a missing trailing space joins
two SQL tokens) and is harder to read than the statement itself, and the
samples in this set mixed it with triple-quoted SQL.

Solution: write each multi-line statement as a single triple-quoted string,
matching the layout already used by the other samples. Only whitespace
between SQL tokens changes; the statements are otherwise identical.

Files updated:
  - spanner/database_admin/src/spanner_database_admin/database_add_split_points.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: spanner Issues related to the Spanner API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant