Skip to content

Add Google Cloud Spanner database support#137

Open
fredrikaverpil wants to merge 2 commits intoMaxteabag:mainfrom
fredrikaverpil:feat/spanner
Open

Add Google Cloud Spanner database support#137
fredrikaverpil wants to merge 2 commits intoMaxteabag:mainfrom
fredrikaverpil:feat/spanner

Conversation

@fredrikaverpil
Copy link

@fredrikaverpil fredrikaverpil commented Feb 5, 2026

Why (this change)?

Spanner is a fully-managed, scalable, relational database from Google Cloud. Adding support allows sqlit users to connect to and query their Spanner databases!

What (was changed)?

  • Add new Spanner provider with adapter, schema, and registration
  • Support Application Default Credentials and service account JSON authentication
  • Support Spanner emulator for local development
  • Schema introspection via INFORMATION_SCHEMA (tables, views, columns, indexes)
  • Primary key detection via INDEX_COLUMNS
  • Handle Spanner-specific quirks:
    • Use autocommit=True for INFORMATION_SCHEMA queries (read-only mode required)
    • Set GRPC_DNS_RESOLVER=native to avoid c-ares DNS issues on some networks
  • Add dialect detection (GoogleSQL vs PostgreSQL) on connect
  • Add dialect-aware identifier quoting helpers
  • Add DatabaseType.SPANNER to enum and display order
  • Add spanner optional dependency in pyproject.toml
  • Add integration test fixtures and tests
  • Update README with Spanner in supported databases list and driver reference table

Notes

  • Spanner doesn't support stored procedures, triggers, or sequences
  • Each connection is scoped to a single database (no multi-database support)
  • SSH tunneling not applicable (cloud-native service using HTTPS/gRPC)
  • Dialect support:
    • GoogleSQL dialect: fully tested and working
    • PostgreSQL dialect: detection implemented but untested (I have no PostgreSQL dialect database available for testing). The dialect is detected on connect and quoting helpers are dialect-aware, but quote_identifier() and build_select_query() default to GoogleSQL syntax due to base class design limitations.

Add new database provider for Google Cloud Spanner using the official
google-cloud-spanner library's PEP-249 DB-API interface.

Features:
- Connection via Application Default Credentials or service account JSON
- Spanner emulator support for local development
- Schema introspection via INFORMATION_SCHEMA
- Primary key detection via INDEX_COLUMNS
- Index listing and definition retrieval
- GoogleSQL dialect with backtick identifier quoting

Spanner-specific limitations handled:
- Single database per connection (no multi-database support)
- No stored procedures, triggers, or sequences
- No SSH tunneling (cloud-native service)
- INFORMATION_SCHEMA queries require read-only mode (autocommit=True)
- gRPC DNS resolver set to native for network compatibility

Note: This implementation only supports GoogleSQL dialect. Spanner databases
using PostgreSQL dialect are not supported (would require different identifier
quoting and potentially different query syntax).
Spanner databases can use either GoogleSQL or PostgreSQL dialect. This
commit adds:

- Dialect detection on connect via INFORMATION_SCHEMA.DATABASE_OPTIONS
- Dialect stored on connection object (_sqlit_spanner_dialect)
- Dialect-aware identifier quoting:
  - GoogleSQL: `identifier` (backticks)
  - PostgreSQL: "identifier" (double quotes)
- Helper methods for connection-aware quoting

Limitations:
- quote_identifier() and build_select_query() always use GoogleSQL syntax
  because they don't have access to the connection object (base class design)
- Connection-aware alternatives are provided:
  - _quote_identifier_for_conn(conn, name)
  - build_select_query_for_conn(conn, table, limit, ...)

Note: PostgreSQL dialect support is experimental and untested. The dialect
detection can be verified with GoogleSQL databases to ensure the mechanism
works correctly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant