arrow-odbc#
Sync Arrow-over-ODBC adapter built on arrow-odbc.
Streams pyarrow.RecordBatchReader results from any ODBC-compliant driver,
making it a good fit for read-heavy analytical transfer between SQL Server,
PostgreSQL, MySQL, or other ODBC sources and the Arrow ecosystem.
SQL Server coverage is exercised in CI against SQL Server 2022 through
pytest-databases and Microsoft ODBC Driver 18. The shared contract matrix
verifies native Arrow reads, Arrow reader/batch output, and Arrow bulk ingest
for this adapter. Row-oriented execute_many() is intentionally unsupported;
use load_from_arrow() for bulk writes.
Extension support is SQL Server-backed. The adapter exports a table-backed events queue store, a Litestar session store, and Google ADK session/event and memory stores for SQL Server connections through Microsoft ODBC Driver 18.
Configuration#
- class sqlspec.adapters.arrow_odbc.ArrowOdbcConfig[source]#
Bases:
NoPoolSyncConfig[Connection,ArrowOdbcDriver]Configuration for synchronous arrow-odbc connections.
- driver_type#
alias of
ArrowOdbcDriver
- connection_type#
alias of
Connection
- __init__(*, connection_config=None, connection_instance=None, migration_config=None, statement_config=None, driver_features=None, bind_key=None, extension_config=None, observability_config=None, **kwargs)[source]#
Initialize arrow-odbc configuration.
- provide_connection(*args, **kwargs)[source]#
Provide a connection context manager.
- Return type:
ArrowOdbcConnectionContext
- provide_session(*_args, statement_config=None, **_kwargs)[source]#
Provide a driver session context manager.
- Return type:
ArrowOdbcSessionContext
Driver#
- class sqlspec.adapters.arrow_odbc.ArrowOdbcDriver[source]#
Bases:
SyncDriverAdapterBaseSync driver for generic ODBC connections with Arrow-native transfer.
- __init__(connection, statement_config=None, driver_features=None)[source]#
Initialize driver adapter with connection and configuration.
- Parameters:
connection¶ (
Connection) -- Database connection instancestatement_config¶ (
StatementConfig|None) -- Statement configuration for the driverdriver_features¶ (
dict[str, typing.Any] |None) -- Driver-specific features like extensions, secrets, and connection callbacksobservability¶ -- Optional runtime handling lifecycle hooks, observers, and spans
- property data_dictionary: ArrowOdbcDataDictionary#
Get the data dictionary for this driver.
- Returns:
Data dictionary instance for metadata queries
- dispatch_execute(cursor, statement)[source]#
Execute a single SQL statement.
Must be implemented by each driver for database-specific execution logic.
- dispatch_execute_many(cursor, statement)[source]#
Execute SQL with multiple parameter sets (executemany).
Must be implemented by each driver for database-specific executemany logic.
- dispatch_execute_script(cursor, statement)[source]#
Execute a SQL script containing multiple statements.
Default implementation splits the script and executes statements individually. Drivers can override for database-specific script execution methods.
- dispatch_select_stream(statement, chunk_size)[source]#
Return a native Arrow ODBC row stream backed by record batches.
- collect_rows(cursor, fetched)[source]#
Collect rows from cursor after fetchall for the direct execution path.
Adapters should override this method to provide optimized row collection that bypasses full dispatch_execute overhead.
- Parameters:
- Return type:
- Returns:
Tuple of (data, column_names, row_count).
- Raises:
NotImplementedError -- If the adapter does not implement this method.
- resolve_rowcount(cursor)[source]#
Resolve the number of affected rows from cursor for the direct execution path.
Adapters should override this method to provide optimized rowcount resolution that bypasses full dispatch_execute overhead.
- Parameters:
cursor¶ (
Connection) -- Database cursor with rowcount metadata.- Return type:
- Returns:
Number of affected rows, or 0 when unknown.
- Raises:
NotImplementedError -- If the adapter does not implement this method.
- with_cursor(connection)[source]#
Create and return a context manager for cursor acquisition and cleanup.
Returns a context manager that yields a cursor for database operations. Concrete implementations handle database-specific cursor creation and cleanup.
- Return type:
ArrowOdbcCursor
- handle_database_exceptions()[source]#
Handle database-specific exceptions and wrap them appropriately.
- Return type:
ArrowOdbcExceptionHandler- Returns:
Exception handler with deferred exception pattern for mypyc compatibility. The handler stores mapped exceptions in pending_exception rather than raising from __exit__ to avoid ABI boundary violations.
- rollback_to_savepoint(name)[source]#
Roll back the current transaction to a previously created savepoint.
- Return type:
- select_to_arrow(statement, /, *parameters, statement_config=None, return_format='table', native_only=False, batch_size=None, arrow_schema=None, **kwargs)[source]#
Execute a query and return native Arrow results.
- bulk_insert_arrow(target_table, source, *, chunk_size=None)[source]#
Insert an Arrow table or reader into a database table.
- Return type:
- load_from_arrow(table, source, *, partitioner=None, overwrite=False, telemetry=None)[source]#
Load Arrow data into a table via arrow-odbc bulk insert.
- Return type:
Data Dictionary#
- class sqlspec.adapters.arrow_odbc.data_dictionary.ArrowOdbcDataDictionary[source]#
Bases:
SyncDataDictionaryBaseRuntime-dialect data dictionary for generic ODBC connections.
- dialect: ClassVar[str] = 'sqlite'#
Dialect identifier. Must be defined by subclasses as a class attribute.
- get_dialect_config()[source]#
Return the runtime dialect configuration for this data dictionary.
- Return type:
DialectConfig
- get_metadata_capabilities(driver, domains=None)[source]#
Report Arrow ODBC metadata capabilities without claiming raw catalog support.
- Return type:
MetadataCapabilityProfile
- resolve_identifier(identifier)[source]#
Return a runtime-dialect-normalized identifier.
- Return type:
- get_version(driver)[source]#
Get database version information when the runtime dialect provides a query.
- Return type:
VersionInfo|None
- get_feature_flag(driver, feature)[source]#
Check whether the runtime dialect supports a feature.
- Return type:
- get_optimal_type(driver, type_category)[source]#
Get the optimal runtime dialect type for a category.
- Return type:
- get_tables(driver, schema=None)[source]#
Get table metadata for dialects with bundled catalog queries.
- Return type:
list[TableMetadata]
- get_columns(driver, table=None, schema=None)[source]#
Get column metadata for dialects with bundled catalog queries.
- Return type:
list[ColumnMetadata]
- get_indexes(driver, table=None, schema=None)[source]#
Get index metadata for dialects with bundled catalog queries.
- Return type:
list[IndexMetadata]
Extensions#
- class sqlspec.adapters.arrow_odbc.events.ArrowOdbcEventQueueStore[source]#
Bases:
BaseEventQueueStore[ArrowOdbcConfig]Event queue DDL for arrow-odbc SQL Server configs.
- class sqlspec.adapters.arrow_odbc.litestar.ArrowOdbcStore[source]#
Bases:
BaseSQLSpecStore[ArrowOdbcConfig]SQL Server-backed session store using arrow-odbc sessions.
- __init__(config)[source]#
Initialize the session store.
- Parameters:
config¶ (
ArrowOdbcConfig) -- SQLSpec database configuration.
- class sqlspec.adapters.arrow_odbc.adk.ArrowOdbcADKStore[source]#
Bases:
BaseSyncADKStore[ArrowOdbcConfig]Synchronous SQL Server ADK session/event store using arrow-odbc.
- create_session(session_id, app_name, user_id, state, owner_id=None)[source]#
Create a new ADK session.
- Return type:
- get_session(app_name, user_id, session_id, *, renew_for=None)[source]#
Return a scoped session or
Noneif absent.- Return type:
- update_session_state(app_name, user_id, session_id, state)[source]#
Replace a session's durable state.
- Return type:
- list_sessions(app_name, user_id=None)[source]#
List ADK sessions for an application, optionally scoped to a user.
- Return type:
- delete_session(app_name, user_id, session_id)[source]#
Delete a session. Event rows cascade through the FK.
- Return type:
- append_event_and_update_state(event_record, app_name, user_id, session_id, state, *, app_state=None, user_state=None)[source]#
Atomically append an event and update durable session/scoped state.
- Return type:
- get_events(app_name, user_id, session_id, after_timestamp=None, limit=None)[source]#
Return events for a scoped session ordered by event timestamp.
- Return type:
- delete_idle_sessions(updated_before)[source]#
Delete sessions whose update_time is older than
updated_before.- Return type:
- class sqlspec.adapters.arrow_odbc.adk.ArrowOdbcADKMemoryStore[source]#
Bases:
BaseSyncADKMemoryStore[ArrowOdbcConfig]SQL Server ADK memory store using arrow-odbc.
- insert_memory_entries(entries, owner_id=None)[source]#
Insert memory entries, skipping duplicates by event_id.
- Return type:
- search_entries(query, app_name, user_id, limit=None)[source]#
Search memory entries with SQL Server LIKE matching.
- Return type:
Schema Discovery#
ArrowOdbcDataDictionary.get_columns first uses bundled dialect catalog
queries. When no query exists for the detected dialect (or it returns no
rows) and a table name is given, the driver issues a zero-row probe
(SELECT * FROM "schema"."table" WHERE 1=0) and derives column names,
ordering, nullability, and SQL type names from the Arrow reader schema.
Arrow-derived type names are approximations (for example VARCHAR for any
string column); mssql_python and other ODBC adapters without native
metadata APIs remain SQL-only.