-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(spanner, sqlalchemy-spanner): fix reflection crashes, add native UUID support, and improve JsonObject #17822
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
sakthivelmanii
wants to merge
1
commit into
main
Choose a base branch
from
fix-sqlalchemy-spanner-reflection-uuid-batch-storing
base: main
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
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
83 changes: 83 additions & 0 deletions
83
packages/sqlalchemy-spanner/tests/mockserver_tests/test_dialect_integration.py
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,83 @@ | ||
| # Copyright 2026 Google LLC All rights reserved. | ||
| # | ||
| # Licensed 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. | ||
|
|
||
| from google.cloud.spanner_admin_database_v1 import UpdateDatabaseDdlRequest | ||
| from google.cloud.spanner_v1 import ResultSet | ||
| from sqlalchemy import Column, Index, MetaData, Table, Uuid, types | ||
| from sqlalchemy.testing import eq_, is_instance_of | ||
|
|
||
| from tests.mockserver_tests.mock_server_test_base import ( | ||
| MockServerTestBase, | ||
| add_result, | ||
| ) | ||
|
|
||
|
|
||
| class TestDialectIntegration(MockServerTestBase): | ||
| def test_create_table_with_native_uuid(self): | ||
| """Integration test verifying native UUID and TOKENLIST DDL generation.""" | ||
| add_result( | ||
| """SELECT true | ||
| FROM INFORMATION_SCHEMA.TABLES | ||
| WHERE TABLE_SCHEMA="" AND TABLE_NAME="products" | ||
| LIMIT 1 | ||
| """, | ||
| ResultSet(), | ||
| ) | ||
| engine = self.create_engine() | ||
| metadata = MetaData() | ||
| Table( | ||
| "products", | ||
| metadata, | ||
| Column("product_id", Uuid, primary_key=True), | ||
| Column("token_data", types.String()), | ||
| ) | ||
| metadata.create_all(engine) | ||
| requests = self.database_admin_service.requests | ||
| eq_(1, len(requests)) | ||
| is_instance_of(requests[0], UpdateDatabaseDdlRequest) | ||
| statement = requests[0].statements[0] | ||
| assert "product_id UUID NOT NULL" in statement | ||
|
|
||
| def test_create_index_with_storing_clause(self): | ||
| """Integration test verifying DDL generation for indexes with STORING clause.""" | ||
| add_result( | ||
| """SELECT true | ||
| FROM INFORMATION_SCHEMA.TABLES | ||
| WHERE TABLE_SCHEMA="" AND TABLE_NAME="items" | ||
| LIMIT 1 | ||
| """, | ||
| ResultSet(), | ||
| ) | ||
| engine = self.create_engine() | ||
| metadata = MetaData() | ||
| items = Table( | ||
| "items", | ||
| metadata, | ||
| Column("id", Uuid, primary_key=True), | ||
| Column("category", types.String(50)), | ||
| Column("name", types.String(100)), | ||
| Column("description", types.String(500)), | ||
| ) | ||
| Index( | ||
| "ix_items_category", | ||
| items.c.category, | ||
| spanner_storing=["name", "description"], | ||
| ) | ||
| metadata.create_all(engine) | ||
| requests = self.database_admin_service.requests | ||
| eq_(1, len(requests)) | ||
| is_instance_of(requests[0], UpdateDatabaseDdlRequest) | ||
| statements = requests[0].statements | ||
| create_index_statement = [s for s in statements if "CREATE INDEX" in s][0] | ||
| assert "STORING (name, description)" in create_index_statement |
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,104 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed 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. | ||
|
|
||
| from unittest.mock import MagicMock | ||
| from sqlalchemy import Column, Index, Table, MetaData, Uuid, types | ||
| from sqlalchemy.schema import CreateIndex | ||
| from sqlalchemy.testing import eq_ | ||
| from sqlalchemy.testing.plugin.plugin_base import fixtures | ||
| from google.cloud.sqlalchemy_spanner.sqlalchemy_spanner import ( | ||
| _type_map, | ||
| _type_map_inv, | ||
| SpannerDialect, | ||
| SpannerDDLCompiler, | ||
| ) | ||
|
|
||
|
|
||
| class TestSpannerDialect(fixtures.TestBase): | ||
| def test_tokenlist_in_type_map(self): | ||
| """Test that TOKENLIST is registered in _type_map to prevent KeyError during reflection.""" | ||
| assert "TOKENLIST" in _type_map | ||
| eq_(_type_map["TOKENLIST"], types.String) | ||
|
|
||
| def test_uuid_in_type_map(self): | ||
| """Test that native UUID is registered in _type_map.""" | ||
| assert "UUID" in _type_map | ||
| eq_(_type_map["UUID"], types.UUID) | ||
|
|
||
| def test_uuid_in_type_map_inv(self): | ||
| """Test that types.UUID maps to 'UUID' in _type_map_inv.""" | ||
| assert types.UUID in _type_map_inv | ||
| eq_(_type_map_inv[types.UUID], "UUID") | ||
|
|
||
| def test_visit_uuid_compilation(self): | ||
| """Test that SpannerTypeCompiler compiles types.UUID and Uuid to 'UUID'.""" | ||
| dialect = SpannerDialect() | ||
| eq_(dialect.type_compiler.process(types.UUID()), "UUID") | ||
| eq_(dialect.type_compiler.process(Uuid()), "UUID") | ||
|
|
||
| def test_string36_backward_compatibility(self): | ||
| """Test that existing String(36) compiles to STRING(36) without regression.""" | ||
| dialect = SpannerDialect() | ||
| processed = dialect.type_compiler.process(types.String(36)) | ||
| eq_(processed, "STRING(36)") | ||
| eq_(_type_map["STRING"], types.String) | ||
|
|
||
| def test_get_multi_indexes_excludes_search_indexes_sql(self): | ||
| """Test that get_multi_indexes SQL query excludes SEARCH indexes.""" | ||
| dialect = SpannerDialect() | ||
| connection = MagicMock() | ||
| mock_snapshot = MagicMock() | ||
| mock_snapshot.execute_sql.return_value = [] | ||
| connection.connection.database.snapshot.return_value.__enter__.return_value = mock_snapshot | ||
|
|
||
| dialect.get_multi_indexes(connection) | ||
|
|
||
| # Retrieve the SQL executed by snapshot | ||
| executed_sql = mock_snapshot.execute_sql.call_args[0][0] | ||
| assert "i.index_type != 'SEARCH'" in executed_sql | ||
|
|
||
| def test_get_multi_indexes_handles_none_column_ordering(self): | ||
| """Test that get_multi_indexes does not crash when column_ordering has None elements.""" | ||
| dialect = SpannerDialect() | ||
| connection = MagicMock() | ||
| mock_snapshot = MagicMock() | ||
| # Mock row: schema, table, index_name, columns, is_unique, column_orderings, storing_columns | ||
| mock_row = [ | ||
| "public", | ||
| "my_table", | ||
| "idx_search", | ||
| ["col1"], | ||
| False, | ||
| [None], # column_ordering is None | ||
| [], | ||
| ] | ||
| mock_snapshot.execute_sql.return_value = [mock_row] | ||
| connection.connection.database.snapshot.return_value.__enter__.return_value = mock_snapshot | ||
|
|
||
| res = dialect.get_multi_indexes(connection) | ||
| assert ("public", "my_table") in res | ||
| index_info = res[("public", "my_table")][0] | ||
| eq_(index_info["column_sorting"]["col1"], None) | ||
|
|
||
| def test_visit_create_index_storing_unbound_columns(self): | ||
| """Test creating index with spanner_storing when storing columns are string names or unbound objects.""" | ||
| compiler = SpannerDDLCompiler(SpannerDialect(), None) | ||
| metadata = MetaData() | ||
| t = Table("t", metadata, Column("col1", types.String(100))) | ||
| # In batch mode, storing columns may be string names not in t.c, or Column objects without t.c mapping | ||
| idx = Index("ix_test", t.c.col1, spanner_storing=["storing_col1", Column("storing_col2", types.String(50))]) | ||
|
|
||
| create_index_op = CreateIndex(idx) | ||
| ddl = compiler.visit_create_index(create_index_op) | ||
| assert "STORING (storing_col1, storing_col2)" in ddl |
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.
If
row[5](the column orderings array) isNone(which can happen if the database returns a null array for certain index types or configurations), attempting tozipit withrow[3]will raise aTypeError: zip argument #2 must support iteration. Guardingrow[5]with a fallback to an empty listrow[5] or []prevents this potential crash.