sqlspec 0.72.0__cp315-cp315-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 6b29784d6b1cafc66758__mypyc.cp315-win_amd64.pyd +0 -0
- sqlspec/__init__.py +381 -0
- sqlspec/__main__.py +14 -0
- sqlspec/__metadata__.py +14 -0
- sqlspec/_typing.py +775 -0
- sqlspec/adapters/__init__.py +3 -0
- sqlspec/adapters/adbc/__init__.py +16 -0
- sqlspec/adapters/adbc/_typing.py +221 -0
- sqlspec/adapters/adbc/adk/__init__.py +11 -0
- sqlspec/adapters/adbc/adk/store.py +4101 -0
- sqlspec/adapters/adbc/config.py +843 -0
- sqlspec/adapters/adbc/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/adbc/core.py +3209 -0
- sqlspec/adapters/adbc/data_dictionary.py +1448 -0
- sqlspec/adapters/adbc/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/adbc/driver.py +1400 -0
- sqlspec/adapters/adbc/events/__init__.py +5 -0
- sqlspec/adapters/adbc/events/store.py +384 -0
- sqlspec/adapters/adbc/litestar/__init__.py +5 -0
- sqlspec/adapters/adbc/litestar/store.py +962 -0
- sqlspec/adapters/adbc/pool.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/adbc/pool.py +242 -0
- sqlspec/adapters/adbc/type_converter.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/adbc/type_converter.py +117 -0
- sqlspec/adapters/aiomysql/__init__.py +25 -0
- sqlspec/adapters/aiomysql/_typing.py +134 -0
- sqlspec/adapters/aiomysql/adk/__init__.py +5 -0
- sqlspec/adapters/aiomysql/adk/store.py +1542 -0
- sqlspec/adapters/aiomysql/config.py +634 -0
- sqlspec/adapters/aiomysql/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/aiomysql/core.py +922 -0
- sqlspec/adapters/aiomysql/data_dictionary.py +16 -0
- sqlspec/adapters/aiomysql/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/aiomysql/driver.py +494 -0
- sqlspec/adapters/aiomysql/events/__init__.py +5 -0
- sqlspec/adapters/aiomysql/events/store.py +72 -0
- sqlspec/adapters/aiomysql/litestar/__init__.py +5 -0
- sqlspec/adapters/aiomysql/litestar/store.py +345 -0
- sqlspec/adapters/aiosqlite/__init__.py +43 -0
- sqlspec/adapters/aiosqlite/_typing.py +75 -0
- sqlspec/adapters/aiosqlite/adk/__init__.py +5 -0
- sqlspec/adapters/aiosqlite/adk/store.py +1546 -0
- sqlspec/adapters/aiosqlite/config.py +607 -0
- sqlspec/adapters/aiosqlite/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/aiosqlite/core.py +1155 -0
- sqlspec/adapters/aiosqlite/data_dictionary.py +343 -0
- sqlspec/adapters/aiosqlite/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/aiosqlite/driver.py +559 -0
- sqlspec/adapters/aiosqlite/events/__init__.py +5 -0
- sqlspec/adapters/aiosqlite/events/store.py +54 -0
- sqlspec/adapters/aiosqlite/litestar/__init__.py +5 -0
- sqlspec/adapters/aiosqlite/litestar/store.py +315 -0
- sqlspec/adapters/aiosqlite/pool.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/aiosqlite/pool.py +1323 -0
- sqlspec/adapters/aiosqlite/type_converter.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/aiosqlite/type_converter.py +114 -0
- sqlspec/adapters/arrow_odbc/__init__.py +32 -0
- sqlspec/adapters/arrow_odbc/_typing.py +131 -0
- sqlspec/adapters/arrow_odbc/adk/__init__.py +5 -0
- sqlspec/adapters/arrow_odbc/adk/store.py +1671 -0
- sqlspec/adapters/arrow_odbc/config.py +446 -0
- sqlspec/adapters/arrow_odbc/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/arrow_odbc/core.py +795 -0
- sqlspec/adapters/arrow_odbc/data_dictionary.py +345 -0
- sqlspec/adapters/arrow_odbc/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/arrow_odbc/driver.py +885 -0
- sqlspec/adapters/arrow_odbc/events/__init__.py +5 -0
- sqlspec/adapters/arrow_odbc/events/store.py +114 -0
- sqlspec/adapters/arrow_odbc/litestar/__init__.py +5 -0
- sqlspec/adapters/arrow_odbc/litestar/store.py +682 -0
- sqlspec/adapters/arrow_odbc/type_converter.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/arrow_odbc/type_converter.py +95 -0
- sqlspec/adapters/asyncmy/__init__.py +27 -0
- sqlspec/adapters/asyncmy/_typing.py +215 -0
- sqlspec/adapters/asyncmy/adk/__init__.py +5 -0
- sqlspec/adapters/asyncmy/adk/store.py +1544 -0
- sqlspec/adapters/asyncmy/config.py +707 -0
- sqlspec/adapters/asyncmy/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/asyncmy/core.py +917 -0
- sqlspec/adapters/asyncmy/data_dictionary.py +16 -0
- sqlspec/adapters/asyncmy/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/asyncmy/driver.py +529 -0
- sqlspec/adapters/asyncmy/events/__init__.py +5 -0
- sqlspec/adapters/asyncmy/events/store.py +72 -0
- sqlspec/adapters/asyncmy/litestar/__init__.py +5 -0
- sqlspec/adapters/asyncmy/litestar/store.py +348 -0
- sqlspec/adapters/asyncpg/__init__.py +28 -0
- sqlspec/adapters/asyncpg/_typing.py +113 -0
- sqlspec/adapters/asyncpg/adk/__init__.py +5 -0
- sqlspec/adapters/asyncpg/adk/store.py +1145 -0
- sqlspec/adapters/asyncpg/config.py +903 -0
- sqlspec/adapters/asyncpg/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/asyncpg/core.py +797 -0
- sqlspec/adapters/asyncpg/data_dictionary.py +417 -0
- sqlspec/adapters/asyncpg/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/asyncpg/driver.py +927 -0
- sqlspec/adapters/asyncpg/events/__init__.py +6 -0
- sqlspec/adapters/asyncpg/events/_hub.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/asyncpg/events/_hub.py +279 -0
- sqlspec/adapters/asyncpg/events/backend.py +410 -0
- sqlspec/adapters/asyncpg/events/store.py +57 -0
- sqlspec/adapters/asyncpg/litestar/__init__.py +5 -0
- sqlspec/adapters/asyncpg/litestar/store.py +278 -0
- sqlspec/adapters/asyncpg/tenant.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/asyncpg/tenant.py +140 -0
- sqlspec/adapters/bigquery/__init__.py +23 -0
- sqlspec/adapters/bigquery/_typing.py +184 -0
- sqlspec/adapters/bigquery/adk/__init__.py +5 -0
- sqlspec/adapters/bigquery/adk/store.py +920 -0
- sqlspec/adapters/bigquery/config.py +631 -0
- sqlspec/adapters/bigquery/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/bigquery/core.py +1433 -0
- sqlspec/adapters/bigquery/data_dictionary.py +502 -0
- sqlspec/adapters/bigquery/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/bigquery/driver.py +1266 -0
- sqlspec/adapters/bigquery/events/__init__.py +5 -0
- sqlspec/adapters/bigquery/events/store.py +158 -0
- sqlspec/adapters/bigquery/litestar/__init__.py +5 -0
- sqlspec/adapters/bigquery/litestar/store.py +359 -0
- sqlspec/adapters/cockroach_asyncpg/__init__.py +32 -0
- sqlspec/adapters/cockroach_asyncpg/_typing.py +54 -0
- sqlspec/adapters/cockroach_asyncpg/adk/__init__.py +9 -0
- sqlspec/adapters/cockroach_asyncpg/adk/store.py +1096 -0
- sqlspec/adapters/cockroach_asyncpg/config.py +653 -0
- sqlspec/adapters/cockroach_asyncpg/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/cockroach_asyncpg/core.py +835 -0
- sqlspec/adapters/cockroach_asyncpg/data_dictionary.py +371 -0
- sqlspec/adapters/cockroach_asyncpg/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/cockroach_asyncpg/driver.py +438 -0
- sqlspec/adapters/cockroach_asyncpg/events/__init__.py +5 -0
- sqlspec/adapters/cockroach_asyncpg/events/store.py +20 -0
- sqlspec/adapters/cockroach_asyncpg/litestar/__init__.py +5 -0
- sqlspec/adapters/cockroach_asyncpg/litestar/store.py +203 -0
- sqlspec/adapters/cockroach_asyncpg/tenant.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/cockroach_asyncpg/tenant.py +143 -0
- sqlspec/adapters/cockroach_psycopg/__init__.py +46 -0
- sqlspec/adapters/cockroach_psycopg/_typing.py +271 -0
- sqlspec/adapters/cockroach_psycopg/adk/__init__.py +17 -0
- sqlspec/adapters/cockroach_psycopg/adk/store.py +2274 -0
- sqlspec/adapters/cockroach_psycopg/config.py +1285 -0
- sqlspec/adapters/cockroach_psycopg/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/cockroach_psycopg/core.py +667 -0
- sqlspec/adapters/cockroach_psycopg/data_dictionary.py +622 -0
- sqlspec/adapters/cockroach_psycopg/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/cockroach_psycopg/driver.py +864 -0
- sqlspec/adapters/cockroach_psycopg/events/__init__.py +8 -0
- sqlspec/adapters/cockroach_psycopg/events/store.py +34 -0
- sqlspec/adapters/cockroach_psycopg/litestar/__init__.py +9 -0
- sqlspec/adapters/cockroach_psycopg/litestar/store.py +413 -0
- sqlspec/adapters/cockroach_psycopg/tenant.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/cockroach_psycopg/tenant.py +222 -0
- sqlspec/adapters/db2/__init__.py +34 -0
- sqlspec/adapters/db2/_typing.py +357 -0
- sqlspec/adapters/db2/adk/__init__.py +10 -0
- sqlspec/adapters/db2/adk/store.py +1950 -0
- sqlspec/adapters/db2/config.py +550 -0
- sqlspec/adapters/db2/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/db2/core.py +1021 -0
- sqlspec/adapters/db2/data_dictionary.py +493 -0
- sqlspec/adapters/db2/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/db2/driver.py +898 -0
- sqlspec/adapters/db2/events/__init__.py +5 -0
- sqlspec/adapters/db2/events/store.py +77 -0
- sqlspec/adapters/db2/litestar/__init__.py +5 -0
- sqlspec/adapters/db2/litestar/store.py +519 -0
- sqlspec/adapters/db2/migrations.py +221 -0
- sqlspec/adapters/db2/pool.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/db2/pool.py +876 -0
- sqlspec/adapters/db2/tenant.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/db2/tenant.py +292 -0
- sqlspec/adapters/duckdb/__init__.py +30 -0
- sqlspec/adapters/duckdb/_typing.py +182 -0
- sqlspec/adapters/duckdb/adk/__init__.py +10 -0
- sqlspec/adapters/duckdb/adk/store.py +1672 -0
- sqlspec/adapters/duckdb/config.py +506 -0
- sqlspec/adapters/duckdb/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/duckdb/core.py +1299 -0
- sqlspec/adapters/duckdb/data_dictionary.py +553 -0
- sqlspec/adapters/duckdb/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/duckdb/driver.py +1214 -0
- sqlspec/adapters/duckdb/events/__init__.py +5 -0
- sqlspec/adapters/duckdb/events/store.py +38 -0
- sqlspec/adapters/duckdb/litestar/__init__.py +5 -0
- sqlspec/adapters/duckdb/litestar/store.py +313 -0
- sqlspec/adapters/duckdb/pool.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/duckdb/pool.py +1121 -0
- sqlspec/adapters/mssql_python/__init__.py +42 -0
- sqlspec/adapters/mssql_python/_typing.py +135 -0
- sqlspec/adapters/mssql_python/adk/__init__.py +5 -0
- sqlspec/adapters/mssql_python/adk/store.py +1278 -0
- sqlspec/adapters/mssql_python/config.py +455 -0
- sqlspec/adapters/mssql_python/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/mssql_python/core.py +862 -0
- sqlspec/adapters/mssql_python/data_dictionary.py +417 -0
- sqlspec/adapters/mssql_python/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/mssql_python/driver.py +772 -0
- sqlspec/adapters/mssql_python/events/__init__.py +5 -0
- sqlspec/adapters/mssql_python/events/store.py +46 -0
- sqlspec/adapters/mssql_python/litestar/__init__.py +5 -0
- sqlspec/adapters/mssql_python/litestar/store.py +355 -0
- sqlspec/adapters/mssql_python/migrations.py +162 -0
- sqlspec/adapters/mssql_python/pool.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/mssql_python/pool.py +165 -0
- sqlspec/adapters/mssql_python/tenant.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/mssql_python/tenant.py +74 -0
- sqlspec/adapters/mssql_python/type_converter.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/mssql_python/type_converter.py +98 -0
- sqlspec/adapters/mysqlconnector/__init__.py +47 -0
- sqlspec/adapters/mysqlconnector/_typing.py +205 -0
- sqlspec/adapters/mysqlconnector/adk/__init__.py +17 -0
- sqlspec/adapters/mysqlconnector/adk/store.py +2503 -0
- sqlspec/adapters/mysqlconnector/config.py +925 -0
- sqlspec/adapters/mysqlconnector/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/mysqlconnector/core.py +1075 -0
- sqlspec/adapters/mysqlconnector/data_dictionary.py +23 -0
- sqlspec/adapters/mysqlconnector/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/mysqlconnector/driver.py +702 -0
- sqlspec/adapters/mysqlconnector/events/__init__.py +8 -0
- sqlspec/adapters/mysqlconnector/events/store.py +89 -0
- sqlspec/adapters/mysqlconnector/litestar/__init__.py +9 -0
- sqlspec/adapters/mysqlconnector/litestar/store.py +524 -0
- sqlspec/adapters/oracledb/__init__.py +159 -0
- sqlspec/adapters/oracledb/_json_handlers.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/oracledb/_json_handlers.py +208 -0
- sqlspec/adapters/oracledb/_param_types.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/oracledb/_param_types.py +65 -0
- sqlspec/adapters/oracledb/_plugins.py +137 -0
- sqlspec/adapters/oracledb/_storage.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/oracledb/_storage.py +138 -0
- sqlspec/adapters/oracledb/_typing.py +468 -0
- sqlspec/adapters/oracledb/_uuid_handlers.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/oracledb/_uuid_handlers.py +143 -0
- sqlspec/adapters/oracledb/_vector_handlers.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/oracledb/_vector_handlers.py +252 -0
- sqlspec/adapters/oracledb/adk/__init__.py +27 -0
- sqlspec/adapters/oracledb/adk/store.py +4031 -0
- sqlspec/adapters/oracledb/config.py +1785 -0
- sqlspec/adapters/oracledb/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/oracledb/core.py +5416 -0
- sqlspec/adapters/oracledb/data_dictionary.py +1264 -0
- sqlspec/adapters/oracledb/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/oracledb/driver.py +3273 -0
- sqlspec/adapters/oracledb/events/__init__.py +29 -0
- sqlspec/adapters/oracledb/events/_hub.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/oracledb/events/_hub.py +394 -0
- sqlspec/adapters/oracledb/events/backend.py +311 -0
- sqlspec/adapters/oracledb/events/store.py +842 -0
- sqlspec/adapters/oracledb/litestar/__init__.py +17 -0
- sqlspec/adapters/oracledb/litestar/store.py +862 -0
- sqlspec/adapters/oracledb/migrations.py +650 -0
- sqlspec/adapters/oracledb/tenant.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/oracledb/tenant.py +990 -0
- sqlspec/adapters/oracledb/type_converter.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/oracledb/type_converter.py +93 -0
- sqlspec/adapters/psqlpy/__init__.py +21 -0
- sqlspec/adapters/psqlpy/_typing.py +180 -0
- sqlspec/adapters/psqlpy/adk/__init__.py +5 -0
- sqlspec/adapters/psqlpy/adk/store.py +1514 -0
- sqlspec/adapters/psqlpy/config.py +828 -0
- sqlspec/adapters/psqlpy/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/psqlpy/core.py +1040 -0
- sqlspec/adapters/psqlpy/data_dictionary.py +399 -0
- sqlspec/adapters/psqlpy/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/psqlpy/driver.py +631 -0
- sqlspec/adapters/psqlpy/events/__init__.py +12 -0
- sqlspec/adapters/psqlpy/events/_hub.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/psqlpy/events/_hub.py +367 -0
- sqlspec/adapters/psqlpy/events/backend.py +415 -0
- sqlspec/adapters/psqlpy/events/store.py +46 -0
- sqlspec/adapters/psqlpy/litestar/__init__.py +5 -0
- sqlspec/adapters/psqlpy/litestar/store.py +270 -0
- sqlspec/adapters/psqlpy/tenant.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/psqlpy/tenant.py +137 -0
- sqlspec/adapters/psqlpy/type_converter.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/psqlpy/type_converter.py +21 -0
- sqlspec/adapters/psycopg/__init__.py +44 -0
- sqlspec/adapters/psycopg/_typing.py +408 -0
- sqlspec/adapters/psycopg/adk/__init__.py +17 -0
- sqlspec/adapters/psycopg/adk/store.py +2761 -0
- sqlspec/adapters/psycopg/config.py +1522 -0
- sqlspec/adapters/psycopg/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/psycopg/core.py +1001 -0
- sqlspec/adapters/psycopg/data_dictionary.py +701 -0
- sqlspec/adapters/psycopg/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/psycopg/driver.py +1629 -0
- sqlspec/adapters/psycopg/events/__init__.py +25 -0
- sqlspec/adapters/psycopg/events/_hub.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/psycopg/events/_hub.py +646 -0
- sqlspec/adapters/psycopg/events/backend.py +739 -0
- sqlspec/adapters/psycopg/events/store.py +82 -0
- sqlspec/adapters/psycopg/litestar/__init__.py +5 -0
- sqlspec/adapters/psycopg/litestar/store.py +595 -0
- sqlspec/adapters/psycopg/tenant.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/psycopg/tenant.py +226 -0
- sqlspec/adapters/psycopg/type_converter.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/psycopg/type_converter.py +94 -0
- sqlspec/adapters/pymssql/__init__.py +29 -0
- sqlspec/adapters/pymssql/_typing.py +156 -0
- sqlspec/adapters/pymssql/adk/__init__.py +5 -0
- sqlspec/adapters/pymssql/adk/store.py +1270 -0
- sqlspec/adapters/pymssql/config.py +299 -0
- sqlspec/adapters/pymssql/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/pymssql/core.py +527 -0
- sqlspec/adapters/pymssql/data_dictionary.py +417 -0
- sqlspec/adapters/pymssql/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/pymssql/driver.py +527 -0
- sqlspec/adapters/pymssql/events/__init__.py +5 -0
- sqlspec/adapters/pymssql/events/store.py +46 -0
- sqlspec/adapters/pymssql/litestar/__init__.py +5 -0
- sqlspec/adapters/pymssql/litestar/store.py +350 -0
- sqlspec/adapters/pymssql/migrations.py +162 -0
- sqlspec/adapters/pymssql/pool.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/pymssql/pool.py +417 -0
- sqlspec/adapters/pymssql/tenant.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/pymssql/tenant.py +73 -0
- sqlspec/adapters/pymysql/__init__.py +27 -0
- sqlspec/adapters/pymysql/_typing.py +105 -0
- sqlspec/adapters/pymysql/adk/__init__.py +5 -0
- sqlspec/adapters/pymysql/adk/store.py +1646 -0
- sqlspec/adapters/pymysql/config.py +545 -0
- sqlspec/adapters/pymysql/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/pymysql/core.py +909 -0
- sqlspec/adapters/pymysql/data_dictionary.py +16 -0
- sqlspec/adapters/pymysql/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/pymysql/driver.py +370 -0
- sqlspec/adapters/pymysql/events/__init__.py +5 -0
- sqlspec/adapters/pymysql/events/store.py +42 -0
- sqlspec/adapters/pymysql/litestar/__init__.py +5 -0
- sqlspec/adapters/pymysql/litestar/store.py +299 -0
- sqlspec/adapters/pymysql/pool.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/pymysql/pool.py +480 -0
- sqlspec/adapters/snowflake/__init__.py +36 -0
- sqlspec/adapters/snowflake/_typing.py +62 -0
- sqlspec/adapters/snowflake/adk/__init__.py +5 -0
- sqlspec/adapters/snowflake/adk/store.py +778 -0
- sqlspec/adapters/snowflake/config.py +333 -0
- sqlspec/adapters/snowflake/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/snowflake/core.py +683 -0
- sqlspec/adapters/snowflake/data_dictionary.py +355 -0
- sqlspec/adapters/snowflake/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/snowflake/driver.py +872 -0
- sqlspec/adapters/snowflake/events/__init__.py +5 -0
- sqlspec/adapters/snowflake/events/store.py +28 -0
- sqlspec/adapters/snowflake/litestar/__init__.py +5 -0
- sqlspec/adapters/snowflake/litestar/store.py +162 -0
- sqlspec/adapters/snowflake/migrations.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/snowflake/migrations.py +76 -0
- sqlspec/adapters/snowflake/pool.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/snowflake/pool.py +385 -0
- sqlspec/adapters/snowflake/tenant.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/snowflake/tenant.py +200 -0
- sqlspec/adapters/snowflake/type_converter.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/snowflake/type_converter.py +33 -0
- sqlspec/adapters/spanner/__init__.py +57 -0
- sqlspec/adapters/spanner/_typing.py +300 -0
- sqlspec/adapters/spanner/adk/__init__.py +22 -0
- sqlspec/adapters/spanner/adk/artifact_store.py +693 -0
- sqlspec/adapters/spanner/adk/store.py +3053 -0
- sqlspec/adapters/spanner/config.py +1578 -0
- sqlspec/adapters/spanner/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/spanner/core.py +1244 -0
- sqlspec/adapters/spanner/data_dictionary.py +706 -0
- sqlspec/adapters/spanner/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/spanner/driver.py +1673 -0
- sqlspec/adapters/spanner/events/__init__.py +5 -0
- sqlspec/adapters/spanner/events/store.py +357 -0
- sqlspec/adapters/spanner/litestar/__init__.py +5 -0
- sqlspec/adapters/spanner/litestar/store.py +570 -0
- sqlspec/adapters/spanner/type_converter.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/spanner/type_converter.py +466 -0
- sqlspec/adapters/sqlanywhere/__init__.py +40 -0
- sqlspec/adapters/sqlanywhere/_typing.py +139 -0
- sqlspec/adapters/sqlanywhere/adk/__init__.py +5 -0
- sqlspec/adapters/sqlanywhere/adk/store.py +1136 -0
- sqlspec/adapters/sqlanywhere/config.py +359 -0
- sqlspec/adapters/sqlanywhere/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/sqlanywhere/core.py +583 -0
- sqlspec/adapters/sqlanywhere/data_dictionary.py +282 -0
- sqlspec/adapters/sqlanywhere/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/sqlanywhere/driver.py +480 -0
- sqlspec/adapters/sqlanywhere/events/__init__.py +5 -0
- sqlspec/adapters/sqlanywhere/events/store.py +28 -0
- sqlspec/adapters/sqlanywhere/litestar/__init__.py +5 -0
- sqlspec/adapters/sqlanywhere/litestar/store.py +237 -0
- sqlspec/adapters/sqlanywhere/migrations.py +178 -0
- sqlspec/adapters/sqlanywhere/pool.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/sqlanywhere/pool.py +397 -0
- sqlspec/adapters/sqlite/__init__.py +32 -0
- sqlspec/adapters/sqlite/_typing.py +91 -0
- sqlspec/adapters/sqlite/adk/__init__.py +5 -0
- sqlspec/adapters/sqlite/adk/store.py +1628 -0
- sqlspec/adapters/sqlite/config.py +520 -0
- sqlspec/adapters/sqlite/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/sqlite/core.py +1035 -0
- sqlspec/adapters/sqlite/data_dictionary.py +327 -0
- sqlspec/adapters/sqlite/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/sqlite/driver.py +432 -0
- sqlspec/adapters/sqlite/events/__init__.py +5 -0
- sqlspec/adapters/sqlite/events/store.py +54 -0
- sqlspec/adapters/sqlite/litestar/__init__.py +5 -0
- sqlspec/adapters/sqlite/litestar/store.py +367 -0
- sqlspec/adapters/sqlite/pool.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/sqlite/pool.py +935 -0
- sqlspec/adapters/sqlite/type_converter.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/sqlite/type_converter.py +114 -0
- sqlspec/adapters/sybase/__init__.py +37 -0
- sqlspec/adapters/sybase/_typing.py +150 -0
- sqlspec/adapters/sybase/adk/__init__.py +5 -0
- sqlspec/adapters/sybase/adk/store.py +1370 -0
- sqlspec/adapters/sybase/config.py +276 -0
- sqlspec/adapters/sybase/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/sybase/core.py +578 -0
- sqlspec/adapters/sybase/data_dictionary.py +358 -0
- sqlspec/adapters/sybase/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/sybase/driver.py +497 -0
- sqlspec/adapters/sybase/events/__init__.py +5 -0
- sqlspec/adapters/sybase/events/store.py +88 -0
- sqlspec/adapters/sybase/litestar/__init__.py +5 -0
- sqlspec/adapters/sybase/litestar/store.py +328 -0
- sqlspec/adapters/sybase/migrations.py +178 -0
- sqlspec/adapters/sybase/pool.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/sybase/pool.py +435 -0
- sqlspec/adapters/teradata/__init__.py +59 -0
- sqlspec/adapters/teradata/_typing.py +145 -0
- sqlspec/adapters/teradata/adk/__init__.py +5 -0
- sqlspec/adapters/teradata/adk/store.py +1300 -0
- sqlspec/adapters/teradata/config.py +324 -0
- sqlspec/adapters/teradata/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/teradata/core.py +437 -0
- sqlspec/adapters/teradata/data_dictionary.py +316 -0
- sqlspec/adapters/teradata/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/teradata/driver.py +721 -0
- sqlspec/adapters/teradata/events/__init__.py +5 -0
- sqlspec/adapters/teradata/events/store.py +64 -0
- sqlspec/adapters/teradata/litestar/__init__.py +5 -0
- sqlspec/adapters/teradata/litestar/store.py +264 -0
- sqlspec/adapters/teradata/migrations.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/teradata/migrations.py +80 -0
- sqlspec/adapters/teradata/pool.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/teradata/pool.py +377 -0
- sqlspec/adapters/teradata/type_converter.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/teradata/type_converter.py +95 -0
- sqlspec/base.cp315-win_amd64.pyd +0 -0
- sqlspec/base.py +1275 -0
- sqlspec/builder/__init__.py +330 -0
- sqlspec/builder/_base.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_base.py +1752 -0
- sqlspec/builder/_column.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_column.py +508 -0
- sqlspec/builder/_ddl.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_ddl.py +4503 -0
- sqlspec/builder/_delete.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_delete.py +75 -0
- sqlspec/builder/_dml.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_dml.py +639 -0
- sqlspec/builder/_explain.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_explain.py +650 -0
- sqlspec/builder/_expression_wrappers.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_expression_wrappers.py +77 -0
- sqlspec/builder/_factory.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_factory.py +1834 -0
- sqlspec/builder/_generation.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_generation.py +55 -0
- sqlspec/builder/_insert.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_insert.py +308 -0
- sqlspec/builder/_join.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_join.py +501 -0
- sqlspec/builder/_locking.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_locking.py +77 -0
- sqlspec/builder/_merge.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_merge.py +1047 -0
- sqlspec/builder/_parsing_utils.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_parsing_utils.py +1197 -0
- sqlspec/builder/_select.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_select.py +1514 -0
- sqlspec/builder/_temporal.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_temporal.py +183 -0
- sqlspec/builder/_update.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_update.py +139 -0
- sqlspec/builder/_values.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_values.py +271 -0
- sqlspec/builder/_vector_distance.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_vector_distance.py +763 -0
- sqlspec/cli.py +1656 -0
- sqlspec/config.py +3402 -0
- sqlspec/core/__init__.py +542 -0
- sqlspec/core/_cursor.cp315-win_amd64.pyd +0 -0
- sqlspec/core/_cursor.py +564 -0
- sqlspec/core/_operators.cp315-win_amd64.pyd +0 -0
- sqlspec/core/_operators.py +167 -0
- sqlspec/core/_ordering.cp315-win_amd64.pyd +0 -0
- sqlspec/core/_ordering.py +151 -0
- sqlspec/core/_pagination.py +66 -0
- sqlspec/core/_pipeline.cp315-win_amd64.pyd +0 -0
- sqlspec/core/_pipeline.py +343 -0
- sqlspec/core/_pool.cp315-win_amd64.pyd +0 -0
- sqlspec/core/_pool.py +72 -0
- sqlspec/core/cache.cp315-win_amd64.pyd +0 -0
- sqlspec/core/cache.py +1503 -0
- sqlspec/core/capabilities.cp315-win_amd64.pyd +0 -0
- sqlspec/core/capabilities.py +67 -0
- sqlspec/core/compiler.cp315-win_amd64.pyd +0 -0
- sqlspec/core/compiler.py +1667 -0
- sqlspec/core/config_runtime.cp315-win_amd64.pyd +0 -0
- sqlspec/core/config_runtime.py +219 -0
- sqlspec/core/correlation.cp315-win_amd64.pyd +0 -0
- sqlspec/core/correlation.py +135 -0
- sqlspec/core/explain.cp315-win_amd64.pyd +0 -0
- sqlspec/core/explain.py +284 -0
- sqlspec/core/filters.cp315-win_amd64.pyd +0 -0
- sqlspec/core/filters.py +1442 -0
- sqlspec/core/hashing.cp315-win_amd64.pyd +0 -0
- sqlspec/core/hashing.py +394 -0
- sqlspec/core/metrics.cp315-win_amd64.pyd +0 -0
- sqlspec/core/metrics.py +86 -0
- sqlspec/core/parameters/__init__.py +174 -0
- sqlspec/core/parameters/_alignment.cp315-win_amd64.pyd +0 -0
- sqlspec/core/parameters/_alignment.py +321 -0
- sqlspec/core/parameters/_converter.cp315-win_amd64.pyd +0 -0
- sqlspec/core/parameters/_converter.py +956 -0
- sqlspec/core/parameters/_declared.cp315-win_amd64.pyd +0 -0
- sqlspec/core/parameters/_declared.py +154 -0
- sqlspec/core/parameters/_processor.cp315-win_amd64.pyd +0 -0
- sqlspec/core/parameters/_processor.py +1646 -0
- sqlspec/core/parameters/_registry.cp315-win_amd64.pyd +0 -0
- sqlspec/core/parameters/_registry.py +207 -0
- sqlspec/core/parameters/_transformers.cp315-win_amd64.pyd +0 -0
- sqlspec/core/parameters/_transformers.py +318 -0
- sqlspec/core/parameters/_types.cp315-win_amd64.pyd +0 -0
- sqlspec/core/parameters/_types.py +658 -0
- sqlspec/core/parameters/_validator.cp315-win_amd64.pyd +0 -0
- sqlspec/core/parameters/_validator.py +699 -0
- sqlspec/core/query_modifiers.cp315-win_amd64.pyd +0 -0
- sqlspec/core/query_modifiers.py +559 -0
- sqlspec/core/result/__init__.py +67 -0
- sqlspec/core/result/_base.cp315-win_amd64.pyd +0 -0
- sqlspec/core/result/_base.py +1772 -0
- sqlspec/core/routing.cp315-win_amd64.pyd +0 -0
- sqlspec/core/routing.py +270 -0
- sqlspec/core/splitter.cp315-win_amd64.pyd +0 -0
- sqlspec/core/splitter.py +1126 -0
- sqlspec/core/sqlcommenter.cp315-win_amd64.pyd +0 -0
- sqlspec/core/sqlcommenter.py +509 -0
- sqlspec/core/stack.cp315-win_amd64.pyd +0 -0
- sqlspec/core/stack.py +167 -0
- sqlspec/core/statement.cp315-win_amd64.pyd +0 -0
- sqlspec/core/statement.py +2253 -0
- sqlspec/core/tenant/__init__.py +64 -0
- sqlspec/core/tenant/base.cp315-win_amd64.pyd +0 -0
- sqlspec/core/tenant/base.py +123 -0
- sqlspec/core/type_converter.cp315-win_amd64.pyd +0 -0
- sqlspec/core/type_converter.py +147 -0
- sqlspec/data_dictionary/__init__.py +237 -0
- sqlspec/data_dictionary/_capabilities.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/_capabilities.py +142 -0
- sqlspec/data_dictionary/_dependencies.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/_dependencies.py +602 -0
- sqlspec/data_dictionary/_index_ddl.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/_index_ddl.py +562 -0
- sqlspec/data_dictionary/_loader.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/_loader.py +344 -0
- sqlspec/data_dictionary/_registry.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/_registry.py +152 -0
- sqlspec/data_dictionary/_types.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/_types.py +1852 -0
- sqlspec/data_dictionary/dialects/__init__.py +34 -0
- sqlspec/data_dictionary/dialects/bigquery/__init__.py +15 -0
- sqlspec/data_dictionary/dialects/bigquery/config.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/dialects/bigquery/config.py +128 -0
- sqlspec/data_dictionary/dialects/bigquery/sql/columns.sql +63 -0
- sqlspec/data_dictionary/dialects/bigquery/sql/constraints.sql +37 -0
- sqlspec/data_dictionary/dialects/bigquery/sql/datasets.sql +14 -0
- sqlspec/data_dictionary/dialects/bigquery/sql/ddl.sql +51 -0
- sqlspec/data_dictionary/dialects/bigquery/sql/dependencies.sql +22 -0
- sqlspec/data_dictionary/dialects/bigquery/sql/foreign_keys.sql +34 -0
- sqlspec/data_dictionary/dialects/bigquery/sql/indexes.sql +59 -0
- sqlspec/data_dictionary/dialects/bigquery/sql/objects.sql +13 -0
- sqlspec/data_dictionary/dialects/bigquery/sql/privileges.sql +13 -0
- sqlspec/data_dictionary/dialects/bigquery/sql/routines.sql +50 -0
- sqlspec/data_dictionary/dialects/bigquery/sql/system.sql +59 -0
- sqlspec/data_dictionary/dialects/bigquery/sql/tables.sql +111 -0
- sqlspec/data_dictionary/dialects/bigquery/sql/version.sql +3 -0
- sqlspec/data_dictionary/dialects/bigquery/sql/views.sql +31 -0
- sqlspec/data_dictionary/dialects/cockroachdb/__init__.py +17 -0
- sqlspec/data_dictionary/dialects/cockroachdb/config.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/dialects/cockroachdb/config.py +95 -0
- sqlspec/data_dictionary/dialects/cockroachdb/sql/columns.sql +163 -0
- sqlspec/data_dictionary/dialects/cockroachdb/sql/comments.sql +10 -0
- sqlspec/data_dictionary/dialects/cockroachdb/sql/constraints.sql +67 -0
- sqlspec/data_dictionary/dialects/cockroachdb/sql/crdb_internal.sql +11 -0
- sqlspec/data_dictionary/dialects/cockroachdb/sql/ddl.sql +10 -0
- sqlspec/data_dictionary/dialects/cockroachdb/sql/dependencies.sql +20 -0
- sqlspec/data_dictionary/dialects/cockroachdb/sql/foreign_keys.sql +62 -0
- sqlspec/data_dictionary/dialects/cockroachdb/sql/indexes.sql +32 -0
- sqlspec/data_dictionary/dialects/cockroachdb/sql/objects.sql +15 -0
- sqlspec/data_dictionary/dialects/cockroachdb/sql/privileges.sql +13 -0
- sqlspec/data_dictionary/dialects/cockroachdb/sql/schemas.sql +10 -0
- sqlspec/data_dictionary/dialects/cockroachdb/sql/sequences.sql +14 -0
- sqlspec/data_dictionary/dialects/cockroachdb/sql/tables.sql +15 -0
- sqlspec/data_dictionary/dialects/cockroachdb/sql/version.sql +3 -0
- sqlspec/data_dictionary/dialects/cockroachdb/sql/views.sql +11 -0
- sqlspec/data_dictionary/dialects/db2/__init__.py +31 -0
- sqlspec/data_dictionary/dialects/db2/config.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/dialects/db2/config.py +172 -0
- sqlspec/data_dictionary/dialects/db2/sql/columns.sql +98 -0
- sqlspec/data_dictionary/dialects/db2/sql/constraints.sql +49 -0
- sqlspec/data_dictionary/dialects/db2/sql/foreign_keys.sql +43 -0
- sqlspec/data_dictionary/dialects/db2/sql/indexes.sql +39 -0
- sqlspec/data_dictionary/dialects/db2/sql/objects.sql +53 -0
- sqlspec/data_dictionary/dialects/db2/sql/schemas.sql +13 -0
- sqlspec/data_dictionary/dialects/db2/sql/tables.sql +77 -0
- sqlspec/data_dictionary/dialects/db2/sql/version.sql +8 -0
- sqlspec/data_dictionary/dialects/db2/sql/views.sql +15 -0
- sqlspec/data_dictionary/dialects/duckdb/__init__.py +5 -0
- sqlspec/data_dictionary/dialects/duckdb/config.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/dialects/duckdb/config.py +66 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/columns.sql +150 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/constraints.sql +34 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/databases.sql +11 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/ddl.sql +221 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/dependencies.sql +50 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/extensions.sql +14 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/foreign_keys.sql +46 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/functions.sql +18 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/indexes.sql +34 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/objects.sql +30 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/schemas.sql +10 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/sequences.sql +19 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/system.sql +21 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/tables.sql +59 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/types.sql +12 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/version.sql +3 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/views.sql +15 -0
- sqlspec/data_dictionary/dialects/mariadb/__init__.py +5 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/columns.sql +45 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/constraints.sql +30 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/ddl.sql +27 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/events.sql +29 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/foreign_keys.sql +42 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/indexes.sql +33 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/objects.sql +50 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/partitions.sql +27 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/plugins.sql +16 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/privileges.sql +24 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/routines.sql +21 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/schemas.sql +10 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/system.sql +12 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/tables.sql +64 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/triggers.sql +21 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/version.sql +7 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/views.sql +15 -0
- sqlspec/data_dictionary/dialects/mssql/__init__.py +51 -0
- sqlspec/data_dictionary/dialects/mssql/config.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/dialects/mssql/config.py +558 -0
- sqlspec/data_dictionary/dialects/mssql/sql/columns.sql +120 -0
- sqlspec/data_dictionary/dialects/mssql/sql/constraints.sql +106 -0
- sqlspec/data_dictionary/dialects/mssql/sql/ddl.sql +89 -0
- sqlspec/data_dictionary/dialects/mssql/sql/dependencies.sql +17 -0
- sqlspec/data_dictionary/dialects/mssql/sql/extended_properties.sql +16 -0
- sqlspec/data_dictionary/dialects/mssql/sql/foreign_keys.sql +40 -0
- sqlspec/data_dictionary/dialects/mssql/sql/indexes.sql +86 -0
- sqlspec/data_dictionary/dialects/mssql/sql/modules.sql +19 -0
- sqlspec/data_dictionary/dialects/mssql/sql/objects.sql +16 -0
- sqlspec/data_dictionary/dialects/mssql/sql/permissions.sql +31 -0
- sqlspec/data_dictionary/dialects/mssql/sql/routines.sql +39 -0
- sqlspec/data_dictionary/dialects/mssql/sql/schemas.sql +13 -0
- sqlspec/data_dictionary/dialects/mssql/sql/sequences.sql +18 -0
- sqlspec/data_dictionary/dialects/mssql/sql/storage.sql +22 -0
- sqlspec/data_dictionary/dialects/mssql/sql/system.sql +38 -0
- sqlspec/data_dictionary/dialects/mssql/sql/tables.sql +97 -0
- sqlspec/data_dictionary/dialects/mssql/sql/triggers.sql +21 -0
- sqlspec/data_dictionary/dialects/mssql/sql/version.sql +7 -0
- sqlspec/data_dictionary/dialects/mssql/sql/views.sql +18 -0
- sqlspec/data_dictionary/dialects/mysql/__init__.py +34 -0
- sqlspec/data_dictionary/dialects/mysql/config.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/dialects/mysql/config.py +363 -0
- sqlspec/data_dictionary/dialects/mysql/dictionary.py +650 -0
- sqlspec/data_dictionary/dialects/mysql/sql/columns.sql +45 -0
- sqlspec/data_dictionary/dialects/mysql/sql/components.sql +8 -0
- sqlspec/data_dictionary/dialects/mysql/sql/constraints.sql +30 -0
- sqlspec/data_dictionary/dialects/mysql/sql/ddl.sql +23 -0
- sqlspec/data_dictionary/dialects/mysql/sql/events.sql +29 -0
- sqlspec/data_dictionary/dialects/mysql/sql/foreign_keys.sql +42 -0
- sqlspec/data_dictionary/dialects/mysql/sql/indexes.sql +33 -0
- sqlspec/data_dictionary/dialects/mysql/sql/objects.sql +38 -0
- sqlspec/data_dictionary/dialects/mysql/sql/partitions.sql +27 -0
- sqlspec/data_dictionary/dialects/mysql/sql/plugins.sql +16 -0
- sqlspec/data_dictionary/dialects/mysql/sql/privileges.sql +26 -0
- sqlspec/data_dictionary/dialects/mysql/sql/routines.sql +21 -0
- sqlspec/data_dictionary/dialects/mysql/sql/schemas.sql +10 -0
- sqlspec/data_dictionary/dialects/mysql/sql/system.sql +25 -0
- sqlspec/data_dictionary/dialects/mysql/sql/tables.sql +64 -0
- sqlspec/data_dictionary/dialects/mysql/sql/triggers.sql +21 -0
- sqlspec/data_dictionary/dialects/mysql/sql/version.sql +3 -0
- sqlspec/data_dictionary/dialects/mysql/sql/views.sql +15 -0
- sqlspec/data_dictionary/dialects/oracle/__init__.py +39 -0
- sqlspec/data_dictionary/dialects/oracle/config.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/dialects/oracle/config.py +266 -0
- sqlspec/data_dictionary/dialects/oracle/sql/arguments.sql +29 -0
- sqlspec/data_dictionary/dialects/oracle/sql/columns.sql +103 -0
- sqlspec/data_dictionary/dialects/oracle/sql/comments.sql +22 -0
- sqlspec/data_dictionary/dialects/oracle/sql/constraints.sql +49 -0
- sqlspec/data_dictionary/dialects/oracle/sql/ddl.sql +4 -0
- sqlspec/data_dictionary/dialects/oracle/sql/dependencies.sql +16 -0
- sqlspec/data_dictionary/dialects/oracle/sql/foreign_keys.sql +48 -0
- sqlspec/data_dictionary/dialects/oracle/sql/grants.sql +34 -0
- sqlspec/data_dictionary/dialects/oracle/sql/indexes.sql +87 -0
- sqlspec/data_dictionary/dialects/oracle/sql/lob_storage.sql +28 -0
- sqlspec/data_dictionary/dialects/oracle/sql/materialized_views.sql +27 -0
- sqlspec/data_dictionary/dialects/oracle/sql/objects.sql +26 -0
- sqlspec/data_dictionary/dialects/oracle/sql/partitions.sql +44 -0
- sqlspec/data_dictionary/dialects/oracle/sql/routines.sql +21 -0
- sqlspec/data_dictionary/dialects/oracle/sql/schemas.sql +13 -0
- sqlspec/data_dictionary/dialects/oracle/sql/sequences.sql +21 -0
- sqlspec/data_dictionary/dialects/oracle/sql/source.sql +13 -0
- sqlspec/data_dictionary/dialects/oracle/sql/system.sql +37 -0
- sqlspec/data_dictionary/dialects/oracle/sql/tables.sql +48 -0
- sqlspec/data_dictionary/dialects/oracle/sql/triggers.sql +29 -0
- sqlspec/data_dictionary/dialects/oracle/sql/version.sql +25 -0
- sqlspec/data_dictionary/dialects/oracle/sql/views.sql +49 -0
- sqlspec/data_dictionary/dialects/postgres/__init__.py +19 -0
- sqlspec/data_dictionary/dialects/postgres/config.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/dialects/postgres/config.py +119 -0
- sqlspec/data_dictionary/dialects/postgres/sql/columns.sql +130 -0
- sqlspec/data_dictionary/dialects/postgres/sql/comments.sql +27 -0
- sqlspec/data_dictionary/dialects/postgres/sql/constraints.sql +53 -0
- sqlspec/data_dictionary/dialects/postgres/sql/ddl.sql +55 -0
- sqlspec/data_dictionary/dialects/postgres/sql/dependencies.sql +42 -0
- sqlspec/data_dictionary/dialects/postgres/sql/extensions.sql +11 -0
- sqlspec/data_dictionary/dialects/postgres/sql/foreign_keys.sql +72 -0
- sqlspec/data_dictionary/dialects/postgres/sql/indexes.sql +84 -0
- sqlspec/data_dictionary/dialects/postgres/sql/materialized_views.sql +17 -0
- sqlspec/data_dictionary/dialects/postgres/sql/objects.sql +25 -0
- sqlspec/data_dictionary/dialects/postgres/sql/partitions.sql +19 -0
- sqlspec/data_dictionary/dialects/postgres/sql/privileges.sql +29 -0
- sqlspec/data_dictionary/dialects/postgres/sql/routines.sql +25 -0
- sqlspec/data_dictionary/dialects/postgres/sql/schemas.sql +13 -0
- sqlspec/data_dictionary/dialects/postgres/sql/sequences.sql +32 -0
- sqlspec/data_dictionary/dialects/postgres/sql/system.sql +53 -0
- sqlspec/data_dictionary/dialects/postgres/sql/tables.sql +67 -0
- sqlspec/data_dictionary/dialects/postgres/sql/triggers.sql +20 -0
- sqlspec/data_dictionary/dialects/postgres/sql/types.sql +21 -0
- sqlspec/data_dictionary/dialects/postgres/sql/version.sql +3 -0
- sqlspec/data_dictionary/dialects/postgres/sql/views.sql +17 -0
- sqlspec/data_dictionary/dialects/snowflake/__init__.py +31 -0
- sqlspec/data_dictionary/dialects/snowflake/config.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/dialects/snowflake/config.py +321 -0
- sqlspec/data_dictionary/dialects/snowflake/sql/columns.sql +48 -0
- sqlspec/data_dictionary/dialects/snowflake/sql/constraints.sql +64 -0
- sqlspec/data_dictionary/dialects/snowflake/sql/ddl.sql +3 -0
- sqlspec/data_dictionary/dialects/snowflake/sql/foreign_keys.sql +7 -0
- sqlspec/data_dictionary/dialects/snowflake/sql/objects.sql +34 -0
- sqlspec/data_dictionary/dialects/snowflake/sql/routines.sql +79 -0
- sqlspec/data_dictionary/dialects/snowflake/sql/schemas.sql +19 -0
- sqlspec/data_dictionary/dialects/snowflake/sql/sequences.sql +45 -0
- sqlspec/data_dictionary/dialects/snowflake/sql/tables.sql +29 -0
- sqlspec/data_dictionary/dialects/snowflake/sql/version.sql +3 -0
- sqlspec/data_dictionary/dialects/snowflake/sql/views.sql +37 -0
- sqlspec/data_dictionary/dialects/spanner/__init__.py +5 -0
- sqlspec/data_dictionary/dialects/spanner/config.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/dialects/spanner/config.py +67 -0
- sqlspec/data_dictionary/dialects/spanner/sql/columns.sql +58 -0
- sqlspec/data_dictionary/dialects/spanner/sql/foreign_keys.sql +91 -0
- sqlspec/data_dictionary/dialects/spanner/sql/googlesql/change_streams.sql +28 -0
- sqlspec/data_dictionary/dialects/spanner/sql/googlesql/columns.sql +46 -0
- sqlspec/data_dictionary/dialects/spanner/sql/googlesql/constraints.sql +63 -0
- sqlspec/data_dictionary/dialects/spanner/sql/googlesql/database.sql +9 -0
- sqlspec/data_dictionary/dialects/spanner/sql/googlesql/foreign_keys.sql +91 -0
- sqlspec/data_dictionary/dialects/spanner/sql/googlesql/indexes.sql +66 -0
- sqlspec/data_dictionary/dialects/spanner/sql/googlesql/objects.sql +10 -0
- sqlspec/data_dictionary/dialects/spanner/sql/googlesql/placements.sql +8 -0
- sqlspec/data_dictionary/dialects/spanner/sql/googlesql/privileges.sql +13 -0
- sqlspec/data_dictionary/dialects/spanner/sql/googlesql/property_graphs.sql +11 -0
- sqlspec/data_dictionary/dialects/spanner/sql/googlesql/routines.sql +33 -0
- sqlspec/data_dictionary/dialects/spanner/sql/googlesql/sequences.sql +14 -0
- sqlspec/data_dictionary/dialects/spanner/sql/googlesql/system.sql +28 -0
- sqlspec/data_dictionary/dialects/spanner/sql/googlesql/tables.sql +30 -0
- sqlspec/data_dictionary/dialects/spanner/sql/googlesql/views.sql +13 -0
- sqlspec/data_dictionary/dialects/spanner/sql/indexes.sql +30 -0
- sqlspec/data_dictionary/dialects/spanner/sql/postgresql/change_streams.sql +28 -0
- sqlspec/data_dictionary/dialects/spanner/sql/postgresql/columns.sql +34 -0
- sqlspec/data_dictionary/dialects/spanner/sql/postgresql/constraints.sql +63 -0
- sqlspec/data_dictionary/dialects/spanner/sql/postgresql/foreign_keys.sql +91 -0
- sqlspec/data_dictionary/dialects/spanner/sql/postgresql/indexes.sql +52 -0
- sqlspec/data_dictionary/dialects/spanner/sql/postgresql/objects.sql +10 -0
- sqlspec/data_dictionary/dialects/spanner/sql/postgresql/sequences.sql +14 -0
- sqlspec/data_dictionary/dialects/spanner/sql/postgresql/system.sql +28 -0
- sqlspec/data_dictionary/dialects/spanner/sql/postgresql/tables.sql +22 -0
- sqlspec/data_dictionary/dialects/spanner/sql/tables.sql +9 -0
- sqlspec/data_dictionary/dialects/spanner/sql/version.sql +3 -0
- sqlspec/data_dictionary/dialects/sqlanywhere/__init__.py +31 -0
- sqlspec/data_dictionary/dialects/sqlanywhere/config.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/dialects/sqlanywhere/config.py +191 -0
- sqlspec/data_dictionary/dialects/sqlanywhere/sql/columns.sql +97 -0
- sqlspec/data_dictionary/dialects/sqlanywhere/sql/constraints.sql +43 -0
- sqlspec/data_dictionary/dialects/sqlanywhere/sql/foreign_keys.sql +45 -0
- sqlspec/data_dictionary/dialects/sqlanywhere/sql/indexes.sql +39 -0
- sqlspec/data_dictionary/dialects/sqlanywhere/sql/objects.sql +18 -0
- sqlspec/data_dictionary/dialects/sqlanywhere/sql/schemas.sql +15 -0
- sqlspec/data_dictionary/dialects/sqlanywhere/sql/tables.sql +86 -0
- sqlspec/data_dictionary/dialects/sqlanywhere/sql/version.sql +3 -0
- sqlspec/data_dictionary/dialects/sqlanywhere/sql/views.sql +15 -0
- sqlspec/data_dictionary/dialects/sqlite/__init__.py +19 -0
- sqlspec/data_dictionary/dialects/sqlite/config.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/dialects/sqlite/config.py +412 -0
- sqlspec/data_dictionary/dialects/sqlite/sql/columns.sql +59 -0
- sqlspec/data_dictionary/dialects/sqlite/sql/compile_options.sql +3 -0
- sqlspec/data_dictionary/dialects/sqlite/sql/constraints.sql +67 -0
- sqlspec/data_dictionary/dialects/sqlite/sql/ddl.sql +37 -0
- sqlspec/data_dictionary/dialects/sqlite/sql/foreign_keys.sql +45 -0
- sqlspec/data_dictionary/dialects/sqlite/sql/functions.sql +3 -0
- sqlspec/data_dictionary/dialects/sqlite/sql/indexes.sql +76 -0
- sqlspec/data_dictionary/dialects/sqlite/sql/modules.sql +3 -0
- sqlspec/data_dictionary/dialects/sqlite/sql/objects.sql +13 -0
- sqlspec/data_dictionary/dialects/sqlite/sql/schemas.sql +3 -0
- sqlspec/data_dictionary/dialects/sqlite/sql/system.sql +7 -0
- sqlspec/data_dictionary/dialects/sqlite/sql/tables.sql +64 -0
- sqlspec/data_dictionary/dialects/sqlite/sql/triggers.sql +10 -0
- sqlspec/data_dictionary/dialects/sqlite/sql/version.sql +3 -0
- sqlspec/data_dictionary/dialects/sqlite/sql/views.sql +10 -0
- sqlspec/data_dictionary/dialects/sybase/__init__.py +31 -0
- sqlspec/data_dictionary/dialects/sybase/config.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/dialects/sybase/config.py +187 -0
- sqlspec/data_dictionary/dialects/sybase/sql/columns.sql +137 -0
- sqlspec/data_dictionary/dialects/sybase/sql/constraints.sql +85 -0
- sqlspec/data_dictionary/dialects/sybase/sql/foreign_keys.sql +81 -0
- sqlspec/data_dictionary/dialects/sybase/sql/indexes.sql +51 -0
- sqlspec/data_dictionary/dialects/sybase/sql/objects.sql +21 -0
- sqlspec/data_dictionary/dialects/sybase/sql/schemas.sql +16 -0
- sqlspec/data_dictionary/dialects/sybase/sql/tables.sql +46 -0
- sqlspec/data_dictionary/dialects/sybase/sql/version.sql +3 -0
- sqlspec/data_dictionary/dialects/sybase/sql/views.sql +19 -0
- sqlspec/data_dictionary/dialects/teradata/__init__.py +33 -0
- sqlspec/data_dictionary/dialects/teradata/config.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/dialects/teradata/config.py +228 -0
- sqlspec/data_dictionary/dialects/teradata/sql/columns.sql +22 -0
- sqlspec/data_dictionary/dialects/teradata/sql/constraints.sql +19 -0
- sqlspec/data_dictionary/dialects/teradata/sql/foreign_keys.sql +9 -0
- sqlspec/data_dictionary/dialects/teradata/sql/indexes.sql +9 -0
- sqlspec/data_dictionary/dialects/teradata/sql/objects.sql +4 -0
- sqlspec/data_dictionary/dialects/teradata/sql/schemas.sql +9 -0
- sqlspec/data_dictionary/dialects/teradata/sql/tables.sql +22 -0
- sqlspec/data_dictionary/dialects/teradata/sql/version.sql +4 -0
- sqlspec/data_dictionary/dialects/teradata/sql/views.sql +4 -0
- sqlspec/dialects/__init__.py +44 -0
- sqlspec/dialects/db2/__init__.py +111 -0
- sqlspec/dialects/db2/_generators.cp315-win_amd64.pyd +0 -0
- sqlspec/dialects/db2/_generators.py +376 -0
- sqlspec/dialects/db2/_parsers.cp315-win_amd64.pyd +0 -0
- sqlspec/dialects/db2/_parsers.py +657 -0
- sqlspec/dialects/db2/_transforms.cp315-win_amd64.pyd +0 -0
- sqlspec/dialects/db2/_transforms.py +175 -0
- sqlspec/dialects/postgres/__init__.py +10 -0
- sqlspec/dialects/postgres/_generators.cp315-win_amd64.pyd +0 -0
- sqlspec/dialects/postgres/_generators.py +81 -0
- sqlspec/dialects/postgres/_operators.cp315-win_amd64.pyd +0 -0
- sqlspec/dialects/postgres/_operators.py +95 -0
- sqlspec/dialects/postgres/_paradedb.py +44 -0
- sqlspec/dialects/postgres/_pg_textsearch.py +38 -0
- sqlspec/dialects/postgres/_pgvector.py +36 -0
- sqlspec/dialects/spanner/__init__.py +69 -0
- sqlspec/dialects/spanner/_expressions.py +247 -0
- sqlspec/dialects/spanner/_generators.cp315-win_amd64.pyd +0 -0
- sqlspec/dialects/spanner/_generators.py +961 -0
- sqlspec/dialects/spanner/_parsers.cp315-win_amd64.pyd +0 -0
- sqlspec/dialects/spanner/_parsers.py +871 -0
- sqlspec/dialects/spanner/_spangres.py +43 -0
- sqlspec/dialects/spanner/_spanner.py +57 -0
- sqlspec/dialects/sybase/__init__.py +115 -0
- sqlspec/dialects/sybase/_generators.cp315-win_amd64.pyd +0 -0
- sqlspec/dialects/sybase/_generators.py +379 -0
- sqlspec/dialects/sybase/_parsers.cp315-win_amd64.pyd +0 -0
- sqlspec/dialects/sybase/_parsers.py +225 -0
- sqlspec/dialects/sybase/_transforms.cp315-win_amd64.pyd +0 -0
- sqlspec/dialects/sybase/_transforms.py +68 -0
- sqlspec/dialects/teradata/__init__.py +42 -0
- sqlspec/dialects/teradata/_generators.cp315-win_amd64.pyd +0 -0
- sqlspec/dialects/teradata/_generators.py +203 -0
- sqlspec/driver/__init__.py +151 -0
- sqlspec/driver/_async.cp315-win_amd64.pyd +0 -0
- sqlspec/driver/_async.py +3244 -0
- sqlspec/driver/_common.cp315-win_amd64.pyd +0 -0
- sqlspec/driver/_common.py +4765 -0
- sqlspec/driver/_deferred.cp315-win_amd64.pyd +0 -0
- sqlspec/driver/_deferred.py +232 -0
- sqlspec/driver/_exception_handler.cp315-win_amd64.pyd +0 -0
- sqlspec/driver/_exception_handler.py +224 -0
- sqlspec/driver/_routing.cp315-win_amd64.pyd +0 -0
- sqlspec/driver/_routing.py +2891 -0
- sqlspec/driver/_session.cp315-win_amd64.pyd +0 -0
- sqlspec/driver/_session.py +180 -0
- sqlspec/driver/_sql_helpers.cp315-win_amd64.pyd +0 -0
- sqlspec/driver/_sql_helpers.py +140 -0
- sqlspec/driver/_stream.cp315-win_amd64.pyd +0 -0
- sqlspec/driver/_stream.py +392 -0
- sqlspec/driver/_sync.cp315-win_amd64.pyd +0 -0
- sqlspec/driver/_sync.py +3107 -0
- sqlspec/driver/_tenant.cp315-win_amd64.pyd +0 -0
- sqlspec/driver/_tenant.py +326 -0
- sqlspec/exceptions.cp315-win_amd64.pyd +0 -0
- sqlspec/exceptions.py +568 -0
- sqlspec/extensions/__init__.py +3 -0
- sqlspec/extensions/adk/__init__.py +136 -0
- sqlspec/extensions/adk/_config_utils.py +306 -0
- sqlspec/extensions/adk/_table_utils.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/adk/_table_utils.py +126 -0
- sqlspec/extensions/adk/_types.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/adk/_types.py +59 -0
- sqlspec/extensions/adk/artifact/__init__.py +18 -0
- sqlspec/extensions/adk/artifact/_types.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/adk/artifact/_types.py +41 -0
- sqlspec/extensions/adk/artifact/service.py +657 -0
- sqlspec/extensions/adk/artifact/store.py +444 -0
- sqlspec/extensions/adk/converters.py +230 -0
- sqlspec/extensions/adk/maintenance.py +262 -0
- sqlspec/extensions/adk/memory/__init__.py +36 -0
- sqlspec/extensions/adk/memory/_types.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/adk/memory/_types.py +48 -0
- sqlspec/extensions/adk/memory/converters.py +271 -0
- sqlspec/extensions/adk/memory/service.py +474 -0
- sqlspec/extensions/adk/memory/store.py +586 -0
- sqlspec/extensions/adk/migrations/0001_create_adk_tables.py +181 -0
- sqlspec/extensions/adk/migrations/0002_create_mssql_python_memory.py +51 -0
- sqlspec/extensions/adk/migrations/__init__.py +3 -0
- sqlspec/extensions/adk/migrations/reconcile.py +282 -0
- sqlspec/extensions/adk/service.py +463 -0
- sqlspec/extensions/adk/store.py +1239 -0
- sqlspec/extensions/events/__init__.py +132 -0
- sqlspec/extensions/events/_buffer.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/events/_buffer.py +38 -0
- sqlspec/extensions/events/_channel.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/events/_channel.py +1144 -0
- sqlspec/extensions/events/_hints.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/events/_hints.py +38 -0
- sqlspec/extensions/events/_models.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/events/_models.py +22 -0
- sqlspec/extensions/events/_names.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/events/_names.py +27 -0
- sqlspec/extensions/events/_payload.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/events/_payload.py +139 -0
- sqlspec/extensions/events/_protocols.py +154 -0
- sqlspec/extensions/events/_queue.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/events/_queue.py +1147 -0
- sqlspec/extensions/events/_store.py +318 -0
- sqlspec/extensions/events/migrations/0001_create_event_queue.py +106 -0
- sqlspec/extensions/events/migrations/__init__.py +3 -0
- sqlspec/extensions/events/migrations/reconcile.py +145 -0
- sqlspec/extensions/events/primitives.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/events/primitives.py +91 -0
- sqlspec/extensions/fastapi/__init__.py +26 -0
- sqlspec/extensions/fastapi/extension.py +360 -0
- sqlspec/extensions/fastapi/providers.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/fastapi/providers.py +1091 -0
- sqlspec/extensions/flask/__init__.py +20 -0
- sqlspec/extensions/flask/_portal.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/flask/_portal.py +212 -0
- sqlspec/extensions/flask/_state.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/flask/_state.py +135 -0
- sqlspec/extensions/flask/_utils.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/flask/_utils.py +171 -0
- sqlspec/extensions/flask/extension.py +1234 -0
- sqlspec/extensions/litestar/__init__.py +52 -0
- sqlspec/extensions/litestar/_tenant_resolution.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/litestar/_tenant_resolution.py +314 -0
- sqlspec/extensions/litestar/_utils.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/litestar/_utils.py +222 -0
- sqlspec/extensions/litestar/channels.py +249 -0
- sqlspec/extensions/litestar/cli.py +100 -0
- sqlspec/extensions/litestar/config.py +5 -0
- sqlspec/extensions/litestar/handlers.py +796 -0
- sqlspec/extensions/litestar/migrations/0001_create_session_table.py +138 -0
- sqlspec/extensions/litestar/migrations/__init__.py +3 -0
- sqlspec/extensions/litestar/migrations/reconcile.py +132 -0
- sqlspec/extensions/litestar/plugin.py +2172 -0
- sqlspec/extensions/litestar/providers.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/litestar/providers.py +1239 -0
- sqlspec/extensions/litestar/store.py +613 -0
- sqlspec/extensions/litestar/tenant.py +292 -0
- sqlspec/extensions/otel/__init__.py +58 -0
- sqlspec/extensions/prometheus/__init__.py +5 -0
- sqlspec/extensions/prometheus/_observer.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/prometheus/_observer.py +152 -0
- sqlspec/extensions/sanic/__init__.py +28 -0
- sqlspec/extensions/sanic/_state.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/sanic/_state.py +102 -0
- sqlspec/extensions/sanic/_utils.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/sanic/_utils.py +234 -0
- sqlspec/extensions/sanic/extension.py +1467 -0
- sqlspec/extensions/starlette/__init__.py +31 -0
- sqlspec/extensions/starlette/_state.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/starlette/_state.py +103 -0
- sqlspec/extensions/starlette/_utils.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/starlette/_utils.py +267 -0
- sqlspec/extensions/starlette/extension.py +691 -0
- sqlspec/extensions/starlette/middleware.py +845 -0
- sqlspec/loader.cp315-win_amd64.pyd +0 -0
- sqlspec/loader.py +1733 -0
- sqlspec/migrations/__init__.py +104 -0
- sqlspec/migrations/_backup.cp315-win_amd64.pyd +0 -0
- sqlspec/migrations/_backup.py +36 -0
- sqlspec/migrations/base.cp315-win_amd64.pyd +0 -0
- sqlspec/migrations/base.py +1688 -0
- sqlspec/migrations/commands.py +4002 -0
- sqlspec/migrations/context.cp315-win_amd64.pyd +0 -0
- sqlspec/migrations/context.py +158 -0
- sqlspec/migrations/fix.cp315-win_amd64.pyd +0 -0
- sqlspec/migrations/fix.py +193 -0
- sqlspec/migrations/loaders.cp315-win_amd64.pyd +0 -0
- sqlspec/migrations/loaders.py +835 -0
- sqlspec/migrations/runner.cp315-win_amd64.pyd +0 -0
- sqlspec/migrations/runner.py +2144 -0
- sqlspec/migrations/schema.cp315-win_amd64.pyd +0 -0
- sqlspec/migrations/schema.py +1266 -0
- sqlspec/migrations/squash.cp315-win_amd64.pyd +0 -0
- sqlspec/migrations/squash.py +2452 -0
- sqlspec/migrations/templates.cp315-win_amd64.pyd +0 -0
- sqlspec/migrations/templates.py +310 -0
- sqlspec/migrations/tracker.cp315-win_amd64.pyd +0 -0
- sqlspec/migrations/tracker.py +1529 -0
- sqlspec/migrations/utils.cp315-win_amd64.pyd +0 -0
- sqlspec/migrations/utils.py +534 -0
- sqlspec/migrations/validation.cp315-win_amd64.pyd +0 -0
- sqlspec/migrations/validation.py +355 -0
- sqlspec/migrations/version.cp315-win_amd64.pyd +0 -0
- sqlspec/migrations/version.py +455 -0
- sqlspec/observability/__init__.py +122 -0
- sqlspec/observability/_common.cp315-win_amd64.pyd +0 -0
- sqlspec/observability/_common.py +307 -0
- sqlspec/observability/_config.cp315-win_amd64.pyd +0 -0
- sqlspec/observability/_config.py +362 -0
- sqlspec/observability/_diagnostics.cp315-win_amd64.pyd +0 -0
- sqlspec/observability/_diagnostics.py +75 -0
- sqlspec/observability/_dispatcher.cp315-win_amd64.pyd +0 -0
- sqlspec/observability/_dispatcher.py +320 -0
- sqlspec/observability/_formatters/__init__.py +13 -0
- sqlspec/observability/_formatters/_aws.cp315-win_amd64.pyd +0 -0
- sqlspec/observability/_formatters/_aws.py +90 -0
- sqlspec/observability/_formatters/_azure.cp315-win_amd64.pyd +0 -0
- sqlspec/observability/_formatters/_azure.py +84 -0
- sqlspec/observability/_formatters/_base.cp315-win_amd64.pyd +0 -0
- sqlspec/observability/_formatters/_base.py +40 -0
- sqlspec/observability/_formatters/_gcp.cp315-win_amd64.pyd +0 -0
- sqlspec/observability/_formatters/_gcp.py +119 -0
- sqlspec/observability/_formatting.cp315-win_amd64.pyd +0 -0
- sqlspec/observability/_formatting.py +76 -0
- sqlspec/observability/_observer.cp315-win_amd64.pyd +0 -0
- sqlspec/observability/_observer.py +445 -0
- sqlspec/observability/_runtime.cp315-win_amd64.pyd +0 -0
- sqlspec/observability/_runtime.py +740 -0
- sqlspec/observability/_sampling.cp315-win_amd64.pyd +0 -0
- sqlspec/observability/_sampling.py +169 -0
- sqlspec/observability/_spans.cp315-win_amd64.pyd +0 -0
- sqlspec/observability/_spans.py +212 -0
- sqlspec/protocols.py +1235 -0
- sqlspec/py.typed +0 -0
- sqlspec/service/__init__.py +5 -0
- sqlspec/service/_base.py +1051 -0
- sqlspec/service/_core.cp315-win_amd64.pyd +0 -0
- sqlspec/service/_core.py +625 -0
- sqlspec/storage/__init__.py +111 -0
- sqlspec/storage/_arrow_payload.py +802 -0
- sqlspec/storage/_arrow_stream.cp315-win_amd64.pyd +0 -0
- sqlspec/storage/_arrow_stream.py +134 -0
- sqlspec/storage/_ingest.cp315-win_amd64.pyd +0 -0
- sqlspec/storage/_ingest.py +810 -0
- sqlspec/storage/_paths.cp315-win_amd64.pyd +0 -0
- sqlspec/storage/_paths.py +313 -0
- sqlspec/storage/_staging_sql.cp315-win_amd64.pyd +0 -0
- sqlspec/storage/_staging_sql.py +695 -0
- sqlspec/storage/_utils.cp315-win_amd64.pyd +0 -0
- sqlspec/storage/_utils.py +174 -0
- sqlspec/storage/backends/__init__.py +43 -0
- sqlspec/storage/backends/base.cp315-win_amd64.pyd +0 -0
- sqlspec/storage/backends/base.py +399 -0
- sqlspec/storage/backends/fsspec.cp315-win_amd64.pyd +0 -0
- sqlspec/storage/backends/fsspec.py +1390 -0
- sqlspec/storage/backends/local.cp315-win_amd64.pyd +0 -0
- sqlspec/storage/backends/local.py +968 -0
- sqlspec/storage/backends/obstore.cp315-win_amd64.pyd +0 -0
- sqlspec/storage/backends/obstore.py +1974 -0
- sqlspec/storage/errors.cp315-win_amd64.pyd +0 -0
- sqlspec/storage/errors.py +127 -0
- sqlspec/storage/pipeline.cp315-win_amd64.pyd +0 -0
- sqlspec/storage/pipeline.py +1270 -0
- sqlspec/storage/registry.cp315-win_amd64.pyd +0 -0
- sqlspec/storage/registry.py +318 -0
- sqlspec/typing.py +261 -0
- sqlspec/utils/__init__.py +9 -0
- sqlspec/utils/adapters.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/adapters.py +186 -0
- sqlspec/utils/arrow_helpers.py +2157 -0
- sqlspec/utils/config_tools.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/config_tools.py +712 -0
- sqlspec/utils/correlation.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/correlation.py +137 -0
- sqlspec/utils/deprecation.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/deprecation.py +222 -0
- sqlspec/utils/dispatch.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/dispatch.py +104 -0
- sqlspec/utils/env.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/env.py +437 -0
- sqlspec/utils/fixtures.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/fixtures.py +2470 -0
- sqlspec/utils/logging.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/logging.py +267 -0
- sqlspec/utils/module_loader.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/module_loader.py +399 -0
- sqlspec/utils/portal.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/portal.py +411 -0
- sqlspec/utils/routing.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/routing.py +645 -0
- sqlspec/utils/schema.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/schema.py +1050 -0
- sqlspec/utils/serializers/__init__.py +69 -0
- sqlspec/utils/serializers/_json.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/serializers/_json.py +525 -0
- sqlspec/utils/serializers/_numpy.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/serializers/_numpy.py +65 -0
- sqlspec/utils/serializers/_schema.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/serializers/_schema.py +340 -0
- sqlspec/utils/sync_tools.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/sync_tools.py +502 -0
- sqlspec/utils/tenant/__init__.py +71 -0
- sqlspec/utils/tenant/_context.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/tenant/_context.py +550 -0
- sqlspec/utils/tenant/_cursor.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/tenant/_cursor.py +43 -0
- sqlspec/utils/tenant/_mssql.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/tenant/_mssql.py +189 -0
- sqlspec/utils/tenant/_postgres.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/tenant/_postgres.py +142 -0
- sqlspec/utils/text.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/text.py +317 -0
- sqlspec/utils/type_converters.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/type_converters.py +220 -0
- sqlspec/utils/type_guards.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/type_guards.py +1505 -0
- sqlspec/utils/uuids.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/uuids.py +263 -0
- sqlspec-0.72.0.dist-info/METADATA +228 -0
- sqlspec-0.72.0.dist-info/RECORD +1144 -0
- sqlspec-0.72.0.dist-info/WHEEL +4 -0
- sqlspec-0.72.0.dist-info/entry_points.txt +13 -0
- sqlspec-0.72.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,2157 @@
|
|
|
1
|
+
"""Arrow conversion helpers for dict-to-Arrow transformations.
|
|
2
|
+
|
|
3
|
+
This module provides utilities for converting Python dictionaries to Apache Arrow
|
|
4
|
+
format, handling empty results, NULL values, and type inference.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import contextlib
|
|
8
|
+
import logging
|
|
9
|
+
import re
|
|
10
|
+
import struct
|
|
11
|
+
from collections.abc import AsyncIterator, Awaitable, Callable, Iterable, Iterator, Mapping, Sequence
|
|
12
|
+
from functools import lru_cache
|
|
13
|
+
from itertools import islice
|
|
14
|
+
from typing import TYPE_CHECKING, Any, Final, Literal, cast, overload
|
|
15
|
+
|
|
16
|
+
from sqlspec.exceptions import ImproperConfigurationError, SQLBuilderError
|
|
17
|
+
from sqlspec.utils.dispatch import TypeDispatcher
|
|
18
|
+
from sqlspec.utils.logging import get_logger, log_with_context
|
|
19
|
+
from sqlspec.utils.module_loader import (
|
|
20
|
+
ensure_pandas,
|
|
21
|
+
ensure_polars,
|
|
22
|
+
ensure_pyarrow,
|
|
23
|
+
import_optional,
|
|
24
|
+
import_optional_attr,
|
|
25
|
+
)
|
|
26
|
+
from sqlspec.utils.type_guards import has_arrow_table_stats, has_get_data
|
|
27
|
+
from sqlspec.utils.uuids import uuid_from_bytes
|
|
28
|
+
|
|
29
|
+
if TYPE_CHECKING:
|
|
30
|
+
from sqlspec.core.result import ArrowResult
|
|
31
|
+
from sqlspec.data_dictionary import DialectConfig
|
|
32
|
+
from sqlspec.storage import StorageTelemetry
|
|
33
|
+
from sqlspec.typing import ArrowRecordBatch, ArrowRecordBatchReader, ArrowTable, PandasDataFrame, PolarsDataFrame
|
|
34
|
+
|
|
35
|
+
__all__ = (
|
|
36
|
+
"PG_BINARY_COPY_HEADER",
|
|
37
|
+
"PG_BINARY_COPY_TRAILER",
|
|
38
|
+
"arrow_batch_to_rows",
|
|
39
|
+
"arrow_batches_from_async_rows",
|
|
40
|
+
"arrow_batches_from_rows",
|
|
41
|
+
"arrow_reader_to_return_format",
|
|
42
|
+
"arrow_reader_with_deferred_close",
|
|
43
|
+
"arrow_table_column_names",
|
|
44
|
+
"arrow_table_needs_parameter_preparation",
|
|
45
|
+
"arrow_table_num_columns",
|
|
46
|
+
"arrow_table_num_rows",
|
|
47
|
+
"arrow_table_to_pandas",
|
|
48
|
+
"arrow_table_to_polars",
|
|
49
|
+
"arrow_table_to_pylist",
|
|
50
|
+
"arrow_table_to_return_format",
|
|
51
|
+
"arrow_table_to_rows",
|
|
52
|
+
"arrow_type_from_token",
|
|
53
|
+
"build_ingest_telemetry",
|
|
54
|
+
"cast_arrow_table_schema",
|
|
55
|
+
"coerce_arrow_batches",
|
|
56
|
+
"coerce_arrow_table",
|
|
57
|
+
"coerce_oracle_df_to_arrow_batches",
|
|
58
|
+
"coerce_oracle_df_to_arrow_table",
|
|
59
|
+
"convert_dict_to_arrow",
|
|
60
|
+
"convert_dict_to_arrow_with_schema",
|
|
61
|
+
"encode_arrow_batch_pg_binary",
|
|
62
|
+
"ensure_arrow_table",
|
|
63
|
+
"iter_pgcopy_binary_chunks",
|
|
64
|
+
"iter_pgcopy_binary_chunks_async",
|
|
65
|
+
"oracle_df_batches_to_arrow_reader",
|
|
66
|
+
"oracle_df_to_arrow_table",
|
|
67
|
+
"records_to_arrow_table",
|
|
68
|
+
"sql_type_for_category",
|
|
69
|
+
"sql_type_from_arrow_type",
|
|
70
|
+
)
|
|
71
|
+
logger = get_logger("sqlspec.utils.arrow_helpers")
|
|
72
|
+
PG_BINARY_COPY_HEADER: Final[bytes] = b"PGCOPY\n\xff\r\n\x00\x00\x00\x00\x00\x00\x00\x00\x00"
|
|
73
|
+
PG_BINARY_COPY_TRAILER: Final[bytes] = b"\xff\xff"
|
|
74
|
+
_PG_DATE_EPOCH_DAYS: Final[int] = 10957
|
|
75
|
+
_PG_EPOCH_OFFSET_US: Final[int] = 946_684_800_000_000
|
|
76
|
+
_PG_NULL_MARKER: Final[bytes] = b"\xff\xff\xff\xff"
|
|
77
|
+
_ARROW_TABLE_COERCER: "TypeDispatcher[Any] | None" = None
|
|
78
|
+
_DEFERRED_CLOSE_READER_CLS: "type[Any] | None" = None
|
|
79
|
+
_ARROW_SCHEMA_DECISION_CACHE_SIZE = 512
|
|
80
|
+
_CONFIG_CATEGORY_DEFAULTS: "dict[str, str]" = {
|
|
81
|
+
"uuid": "UUID",
|
|
82
|
+
"boolean": "BOOLEAN",
|
|
83
|
+
"blob": "BLOB",
|
|
84
|
+
"json": "JSON",
|
|
85
|
+
"text": "TEXT",
|
|
86
|
+
}
|
|
87
|
+
_PLAIN_CATEGORY_TYPES: "dict[str, str]" = {
|
|
88
|
+
"smallint": "SMALLINT",
|
|
89
|
+
"integer": "INTEGER",
|
|
90
|
+
"bigint": "BIGINT",
|
|
91
|
+
"real": "REAL",
|
|
92
|
+
"date": "DATE",
|
|
93
|
+
"time": "TIME",
|
|
94
|
+
"interval": "INTERVAL",
|
|
95
|
+
}
|
|
96
|
+
_NAIVE_TIMESTAMP_TYPES: "dict[str, str]" = {"mssql": "DATETIME2(6)", "bigquery": "DATETIME", "sybase": "BIGDATETIME"}
|
|
97
|
+
_AWARE_TIMESTAMP_TYPES: "dict[str, str]" = {
|
|
98
|
+
"postgres": "TIMESTAMP WITH TIME ZONE",
|
|
99
|
+
"cockroachdb": "TIMESTAMPTZ",
|
|
100
|
+
"duckdb": "TIMESTAMP WITH TIME ZONE",
|
|
101
|
+
"oracle": "TIMESTAMP WITH TIME ZONE",
|
|
102
|
+
"mssql": "DATETIMEOFFSET(6)",
|
|
103
|
+
"sybase": "BIGDATETIME",
|
|
104
|
+
"sqlanywhere": "TIMESTAMP WITH TIME ZONE",
|
|
105
|
+
"snowflake": "TIMESTAMP_TZ",
|
|
106
|
+
"teradata": "TIMESTAMP(6) WITH TIME ZONE",
|
|
107
|
+
}
|
|
108
|
+
_BOUNDED_KEY_TEXT_DIALECTS: "frozenset[str]" = frozenset({
|
|
109
|
+
"mysql",
|
|
110
|
+
"mariadb",
|
|
111
|
+
"oracle",
|
|
112
|
+
"mssql",
|
|
113
|
+
"db2",
|
|
114
|
+
"sybase",
|
|
115
|
+
"sqlanywhere",
|
|
116
|
+
"teradata",
|
|
117
|
+
})
|
|
118
|
+
_KEY_TEXT_LENGTH = 255
|
|
119
|
+
_DEFAULT_DECIMAL_PRECISION = 38
|
|
120
|
+
_DEFAULT_DECIMAL_SCALE = 9
|
|
121
|
+
_DECIMAL_MAX_PRECISION: "dict[str, int]" = {
|
|
122
|
+
"snowflake": 38,
|
|
123
|
+
"sybase": 38,
|
|
124
|
+
"sqlanywhere": 127,
|
|
125
|
+
"duckdb": 38,
|
|
126
|
+
"mssql": 38,
|
|
127
|
+
"oracle": 38,
|
|
128
|
+
"db2": 31,
|
|
129
|
+
"mysql": 65,
|
|
130
|
+
"mariadb": 65,
|
|
131
|
+
"postgres": 1000,
|
|
132
|
+
"cockroachdb": 1000,
|
|
133
|
+
"bigquery": 76,
|
|
134
|
+
"teradata": 38,
|
|
135
|
+
}
|
|
136
|
+
_SPANNER_NUMERIC_SCALE = 9
|
|
137
|
+
_SPANNER_NUMERIC_INTEGER_DIGITS = 29
|
|
138
|
+
_BIGQUERY_NUMERIC_MAX_PRECISION = 38
|
|
139
|
+
_BIGQUERY_NUMERIC_MAX_SCALE = 9
|
|
140
|
+
_BIGQUERY_BIGNUMERIC_MAX_PRECISION = 76
|
|
141
|
+
_ARROW_TOKEN_FACTORIES: "dict[str, Callable[[Any], Any]]" = {
|
|
142
|
+
"bool": lambda pa: pa.bool_(),
|
|
143
|
+
"int16": lambda pa: pa.int16(),
|
|
144
|
+
"int32": lambda pa: pa.int32(),
|
|
145
|
+
"int64": lambda pa: pa.int64(),
|
|
146
|
+
"float32": lambda pa: pa.float32(),
|
|
147
|
+
"float64": lambda pa: pa.float64(),
|
|
148
|
+
"decimal": lambda pa: pa.decimal128(38, 9),
|
|
149
|
+
"string": lambda pa: pa.string(),
|
|
150
|
+
"binary": lambda pa: pa.binary(),
|
|
151
|
+
"date": lambda pa: pa.date32(),
|
|
152
|
+
"time": lambda pa: pa.time64("us"),
|
|
153
|
+
"timestamp": lambda pa: pa.timestamp("us"),
|
|
154
|
+
"timestamptz": lambda pa: pa.timestamp("us", tz="UTC"),
|
|
155
|
+
"duration": lambda pa: pa.duration("us"),
|
|
156
|
+
}
|
|
157
|
+
_DECIMAL128_MAX_PRECISION = 38
|
|
158
|
+
_DECIMAL256_MAX_PRECISION = 76
|
|
159
|
+
_DECIMAL_TOKEN_PATTERN: Final[re.Pattern[str]] = re.compile(r"^decimal\((\d+),(\d+)\)$")
|
|
160
|
+
_PINNABLE_NULL_TOKENS: Final[frozenset[str]] = frozenset({
|
|
161
|
+
"bool",
|
|
162
|
+
"int16",
|
|
163
|
+
"int32",
|
|
164
|
+
"int64",
|
|
165
|
+
"float32",
|
|
166
|
+
"float64",
|
|
167
|
+
"date",
|
|
168
|
+
"timestamp",
|
|
169
|
+
"timestamptz",
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
@overload
|
|
174
|
+
def convert_dict_to_arrow(
|
|
175
|
+
data: "list[dict[str, Any]]",
|
|
176
|
+
return_format: Literal["table"] = "table",
|
|
177
|
+
batch_size: int | None = None,
|
|
178
|
+
column_types: "Mapping[str, str] | None" = None,
|
|
179
|
+
) -> "ArrowTable": ...
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
@overload
|
|
183
|
+
def convert_dict_to_arrow(
|
|
184
|
+
data: "list[dict[str, Any]]",
|
|
185
|
+
return_format: Literal["reader"],
|
|
186
|
+
batch_size: int | None = None,
|
|
187
|
+
column_types: "Mapping[str, str] | None" = None,
|
|
188
|
+
) -> "ArrowRecordBatchReader": ...
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
@overload
|
|
192
|
+
def convert_dict_to_arrow(
|
|
193
|
+
data: "list[dict[str, Any]]",
|
|
194
|
+
return_format: Literal["batch"],
|
|
195
|
+
batch_size: int | None = None,
|
|
196
|
+
column_types: "Mapping[str, str] | None" = None,
|
|
197
|
+
) -> "ArrowRecordBatch": ...
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
@overload
|
|
201
|
+
def convert_dict_to_arrow(
|
|
202
|
+
data: "list[dict[str, Any]]",
|
|
203
|
+
return_format: Literal["batches"],
|
|
204
|
+
batch_size: int | None = None,
|
|
205
|
+
column_types: "Mapping[str, str] | None" = None,
|
|
206
|
+
) -> "list[ArrowRecordBatch]": ...
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def convert_dict_to_arrow(
|
|
210
|
+
data: "list[dict[str, Any]]",
|
|
211
|
+
return_format: Literal["table", "reader", "batch", "batches"] = "table",
|
|
212
|
+
batch_size: int | None = None,
|
|
213
|
+
column_types: "Mapping[str, str] | None" = None,
|
|
214
|
+
) -> "ArrowTable | ArrowRecordBatch | ArrowRecordBatchReader | list[ArrowRecordBatch]":
|
|
215
|
+
"""Convert list of dictionaries to Arrow Table or RecordBatch.
|
|
216
|
+
|
|
217
|
+
Handles empty results, NULL values, and automatic type inference.
|
|
218
|
+
Used by adapters that don't have native Arrow support to convert
|
|
219
|
+
dict-based results to Arrow format.
|
|
220
|
+
|
|
221
|
+
Args:
|
|
222
|
+
data: List of dictionaries (one per row).
|
|
223
|
+
return_format: Output format - "table" for Table, "batch"/"batches" for RecordBatch.
|
|
224
|
+
"reader" returns a RecordBatchReader.
|
|
225
|
+
batch_size: Chunk size for batching (used when return_format="batch"/"batches").
|
|
226
|
+
column_types: Optional column name to neutral type-token map reported by the
|
|
227
|
+
adapter. Used to type columns that are NULL in every row and would
|
|
228
|
+
otherwise infer as Arrow ``null``.
|
|
229
|
+
|
|
230
|
+
Returns:
|
|
231
|
+
ArrowTable or ArrowRecordBatch depending on return_format.
|
|
232
|
+
"""
|
|
233
|
+
ensure_pyarrow()
|
|
234
|
+
import pyarrow as pa
|
|
235
|
+
|
|
236
|
+
if not data:
|
|
237
|
+
empty_schema = pa.schema([])
|
|
238
|
+
empty_table = pa.Table.from_pydict({}, schema=empty_schema)
|
|
239
|
+
|
|
240
|
+
if return_format == "reader":
|
|
241
|
+
return pa.RecordBatchReader.from_batches(empty_table.schema, empty_table.to_batches())
|
|
242
|
+
|
|
243
|
+
if return_format == "batches":
|
|
244
|
+
return []
|
|
245
|
+
|
|
246
|
+
if return_format == "batch":
|
|
247
|
+
batches = empty_table.to_batches(max_chunksize=batch_size)
|
|
248
|
+
return batches[0] if batches else pa.RecordBatch.from_pydict({})
|
|
249
|
+
|
|
250
|
+
return empty_table
|
|
251
|
+
arrow_table = _resolve_null_columns(pa.Table.from_pylist(data), column_types)
|
|
252
|
+
|
|
253
|
+
if return_format == "reader":
|
|
254
|
+
batches = arrow_table.to_batches(max_chunksize=batch_size)
|
|
255
|
+
return pa.RecordBatchReader.from_batches(arrow_table.schema, batches)
|
|
256
|
+
|
|
257
|
+
if return_format == "batches":
|
|
258
|
+
return arrow_table.to_batches(max_chunksize=batch_size)
|
|
259
|
+
|
|
260
|
+
if return_format == "batch":
|
|
261
|
+
batches = arrow_table.to_batches(max_chunksize=batch_size)
|
|
262
|
+
return batches[0] if batches else pa.RecordBatch.from_pydict({})
|
|
263
|
+
|
|
264
|
+
return arrow_table
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
def arrow_type_from_token(token: str) -> Any:
|
|
268
|
+
"""Return the pyarrow type for a neutral column-type token.
|
|
269
|
+
|
|
270
|
+
Unknown tokens resolve to ``string`` so an adapter reporting a type this
|
|
271
|
+
module does not model degrades to the safe fallback rather than raising.
|
|
272
|
+
Exact ``decimal(P,S)`` tokens resolve to ``decimal128`` for ``P <= 38``
|
|
273
|
+
and ``decimal256`` for ``38 < P <= 76``.
|
|
274
|
+
"""
|
|
275
|
+
import pyarrow as pa
|
|
276
|
+
|
|
277
|
+
factory = _ARROW_TOKEN_FACTORIES.get(token)
|
|
278
|
+
if factory is not None:
|
|
279
|
+
return factory(pa)
|
|
280
|
+
parsed_decimal = _parse_decimal_token(token)
|
|
281
|
+
if parsed_decimal is not None:
|
|
282
|
+
precision, scale = parsed_decimal
|
|
283
|
+
if precision <= _DECIMAL128_MAX_PRECISION:
|
|
284
|
+
return pa.decimal128(precision, scale)
|
|
285
|
+
return pa.decimal256(precision, scale)
|
|
286
|
+
return pa.string()
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
def sql_type_from_arrow_type(arrow_type: Any, dialect: str | None = None) -> str:
|
|
290
|
+
"""Return a SQL column type string for a PyArrow DataType.
|
|
291
|
+
|
|
292
|
+
Args:
|
|
293
|
+
arrow_type: PyArrow DataType instance.
|
|
294
|
+
dialect: Optional target SQL dialect name.
|
|
295
|
+
|
|
296
|
+
Returns:
|
|
297
|
+
Dialect-optimal or standard SQL type string.
|
|
298
|
+
"""
|
|
299
|
+
ensure_pyarrow()
|
|
300
|
+
import pyarrow as pa
|
|
301
|
+
|
|
302
|
+
if _arrow_type_is_uuid(arrow_type):
|
|
303
|
+
return sql_type_for_category("uuid", dialect)
|
|
304
|
+
|
|
305
|
+
unwrapped = arrow_type
|
|
306
|
+
if pa.types.is_dictionary(unwrapped):
|
|
307
|
+
unwrapped = unwrapped.value_type
|
|
308
|
+
is_extension = getattr(pa.types, "is_extension", None)
|
|
309
|
+
if is_extension is not None and is_extension(unwrapped) and not _arrow_type_is_uuid(unwrapped):
|
|
310
|
+
unwrapped = unwrapped.storage_type
|
|
311
|
+
|
|
312
|
+
if _arrow_type_is_uuid(unwrapped):
|
|
313
|
+
return sql_type_for_category("uuid", dialect)
|
|
314
|
+
if pa.types.is_decimal(unwrapped):
|
|
315
|
+
return sql_type_for_category("decimal", dialect, precision=unwrapped.precision, scale=unwrapped.scale)
|
|
316
|
+
if pa.types.is_timestamp(unwrapped):
|
|
317
|
+
return sql_type_for_category("timestamptz" if getattr(unwrapped, "tz", None) else "timestamp", dialect)
|
|
318
|
+
return sql_type_for_category(_arrow_type_category(pa, unwrapped), dialect)
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
def sql_type_for_category(
|
|
322
|
+
category: str, dialect: str | None = None, *, precision: int | None = None, scale: int | None = None
|
|
323
|
+
) -> str:
|
|
324
|
+
"""Return a dialect-aware SQL column type for a neutral value category.
|
|
325
|
+
|
|
326
|
+
This is the single mapping shared by Arrow schema conversion and Python value
|
|
327
|
+
inference. Categories are ``uuid``, ``boolean``, ``smallint``, ``integer``,
|
|
328
|
+
``bigint``, ``real``, ``double``, ``decimal``, ``date``, ``time``,
|
|
329
|
+
``timestamp`` (naive), ``timestamptz`` (timezone-aware), ``interval``,
|
|
330
|
+
``blob``, ``json``, ``text``, and ``key_text`` (text used in a primary key,
|
|
331
|
+
which must be bounded on engines that cannot index unbounded text).
|
|
332
|
+
|
|
333
|
+
Args:
|
|
334
|
+
category: Neutral value category.
|
|
335
|
+
dialect: Optional target SQL dialect name.
|
|
336
|
+
precision: Decimal precision for the ``decimal`` category.
|
|
337
|
+
scale: Decimal scale for the ``decimal`` category.
|
|
338
|
+
|
|
339
|
+
Returns:
|
|
340
|
+
Dialect-specific SQL type string.
|
|
341
|
+
|
|
342
|
+
Raises:
|
|
343
|
+
SQLBuilderError: When a decimal precision exceeds what the dialect can store exactly.
|
|
344
|
+
"""
|
|
345
|
+
config = _resolve_dialect_config(dialect)
|
|
346
|
+
name = config.name if config is not None else None
|
|
347
|
+
if category in _CONFIG_CATEGORY_DEFAULTS:
|
|
348
|
+
return config.get_optimal_type(category) if config is not None else _CONFIG_CATEGORY_DEFAULTS[category]
|
|
349
|
+
if category == "key_text":
|
|
350
|
+
if name in _BOUNDED_KEY_TEXT_DIALECTS and config is not None:
|
|
351
|
+
return config.get_optimal_type("varchar", length=_KEY_TEXT_LENGTH)
|
|
352
|
+
return config.get_optimal_type("text") if config is not None else "TEXT"
|
|
353
|
+
if category == "double":
|
|
354
|
+
if name in {"duckdb", "mysql", "mariadb"}:
|
|
355
|
+
return "DOUBLE"
|
|
356
|
+
if name in {"bigquery", "spanner"} and config is not None:
|
|
357
|
+
return config.get_optimal_type("float")
|
|
358
|
+
return "DOUBLE PRECISION"
|
|
359
|
+
if category == "decimal":
|
|
360
|
+
return _decimal_sql_type(name, precision, scale)
|
|
361
|
+
if category == "timestamp":
|
|
362
|
+
return _NAIVE_TIMESTAMP_TYPES.get(name or "", "TIMESTAMP")
|
|
363
|
+
if category == "timestamptz":
|
|
364
|
+
if name is None:
|
|
365
|
+
return "TIMESTAMP WITH TIME ZONE"
|
|
366
|
+
return _AWARE_TIMESTAMP_TYPES.get(name, "TIMESTAMP")
|
|
367
|
+
if category in _PLAIN_CATEGORY_TYPES:
|
|
368
|
+
return _PLAIN_CATEGORY_TYPES[category]
|
|
369
|
+
msg = f"Unknown SQL type category: {category!r}"
|
|
370
|
+
raise SQLBuilderError(msg)
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
def convert_dict_to_arrow_with_schema(
|
|
374
|
+
data: "list[dict[str, Any]]",
|
|
375
|
+
return_format: Literal["table", "reader", "batch", "batches"] = "table",
|
|
376
|
+
batch_size: int | None = None,
|
|
377
|
+
arrow_schema: Any = None,
|
|
378
|
+
column_types: "Mapping[str, str] | None" = None,
|
|
379
|
+
column_names: "Sequence[str] | None" = None,
|
|
380
|
+
) -> "ArrowTable | ArrowRecordBatch | ArrowRecordBatchReader | list[ArrowRecordBatch]":
|
|
381
|
+
"""Convert dict rows to Arrow, optionally casting to an explicit schema.
|
|
382
|
+
|
|
383
|
+
An explicit ``arrow_schema`` always wins; ``column_types`` only fills in
|
|
384
|
+
columns that would otherwise infer as Arrow ``null``. Empty results retain
|
|
385
|
+
``column_names`` with reported types or Arrow ``null`` when types are unknown.
|
|
386
|
+
"""
|
|
387
|
+
table = convert_dict_to_arrow(data, return_format="table", batch_size=batch_size, column_types=column_types)
|
|
388
|
+
if arrow_schema is not None or (not data and column_names):
|
|
389
|
+
import pyarrow as pa
|
|
390
|
+
|
|
391
|
+
if arrow_schema is not None:
|
|
392
|
+
if not isinstance(arrow_schema, pa.Schema):
|
|
393
|
+
msg = f"arrow_schema must be a pyarrow.Schema, got {type(arrow_schema).__name__}"
|
|
394
|
+
raise TypeError(msg)
|
|
395
|
+
if not data:
|
|
396
|
+
table = pa.Table.from_pydict({name: [] for name in arrow_schema.names}, schema=arrow_schema)
|
|
397
|
+
else:
|
|
398
|
+
table = table.cast(arrow_schema)
|
|
399
|
+
else:
|
|
400
|
+
hints = column_types or {}
|
|
401
|
+
schema = pa.schema([
|
|
402
|
+
(name, arrow_type_from_token(hints[name]) if name in hints else pa.null())
|
|
403
|
+
for name in column_names or ()
|
|
404
|
+
])
|
|
405
|
+
table = pa.Table.from_pydict({name: [] for name in schema.names}, schema=schema)
|
|
406
|
+
return arrow_table_to_return_format(table, return_format=return_format, batch_size=batch_size)
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
def arrow_batches_from_rows(
|
|
410
|
+
rows: "Iterator[dict[str, Any]]",
|
|
411
|
+
*,
|
|
412
|
+
batch_size: int,
|
|
413
|
+
arrow_schema: Any = None,
|
|
414
|
+
integer_may_widen: bool = False,
|
|
415
|
+
on_read_ahead: "Callable[[tuple[str, ...]], None] | None" = None,
|
|
416
|
+
) -> "ArrowRecordBatchReader | None":
|
|
417
|
+
"""Convert a synchronous row iterator into a lazy Arrow RecordBatchReader.
|
|
418
|
+
|
|
419
|
+
Args:
|
|
420
|
+
rows: Synchronous iterator yielding row dictionaries.
|
|
421
|
+
batch_size: Maximum number of rows to convert per batch.
|
|
422
|
+
arrow_schema: Optional explicit PyArrow schema overriding inference.
|
|
423
|
+
integer_may_widen: Whether top-level integer columns may widen to floats in later chunks.
|
|
424
|
+
on_read_ahead: Optional callback receiving unsettled column names when read-ahead occurs.
|
|
425
|
+
|
|
426
|
+
Returns:
|
|
427
|
+
A ``pyarrow.RecordBatchReader`` streaming converted batches, or ``None`` when ``rows`` is empty.
|
|
428
|
+
|
|
429
|
+
Raises:
|
|
430
|
+
ValueError: When ``batch_size < 1``.
|
|
431
|
+
TypeError: When ``arrow_schema`` is provided and is not a ``pyarrow.Schema``.
|
|
432
|
+
"""
|
|
433
|
+
if batch_size < 1:
|
|
434
|
+
msg = "batch_size must be greater than or equal to 1"
|
|
435
|
+
raise ValueError(msg)
|
|
436
|
+
|
|
437
|
+
ensure_pyarrow()
|
|
438
|
+
import pyarrow as pa
|
|
439
|
+
|
|
440
|
+
if arrow_schema is not None and not isinstance(arrow_schema, pa.Schema):
|
|
441
|
+
_close_sync_rows(rows)
|
|
442
|
+
msg = f"arrow_schema must be a pyarrow.Schema, got {type(arrow_schema).__name__}"
|
|
443
|
+
raise TypeError(msg)
|
|
444
|
+
|
|
445
|
+
try:
|
|
446
|
+
iterator = iter(rows)
|
|
447
|
+
first_chunk = list(islice(iterator, batch_size))
|
|
448
|
+
if not first_chunk:
|
|
449
|
+
_close_sync_rows(rows)
|
|
450
|
+
return None
|
|
451
|
+
|
|
452
|
+
raw_types = getattr(rows, "column_types", None)
|
|
453
|
+
column_types: Mapping[str, str] | None = raw_types if isinstance(raw_types, Mapping) else None
|
|
454
|
+
probe = pa.Table.from_pylist(first_chunk)
|
|
455
|
+
|
|
456
|
+
if arrow_schema is not None:
|
|
457
|
+
_reject_lost_struct_fields(probe.schema, arrow_schema)
|
|
458
|
+
first_table = cast(
|
|
459
|
+
"ArrowTable",
|
|
460
|
+
convert_dict_to_arrow_with_schema(
|
|
461
|
+
first_chunk, return_format="table", arrow_schema=arrow_schema, column_types=column_types
|
|
462
|
+
),
|
|
463
|
+
)
|
|
464
|
+
schema = first_table.schema
|
|
465
|
+
else:
|
|
466
|
+
declared = _declared_types(probe.schema, column_types)
|
|
467
|
+
unsettled = tuple(
|
|
468
|
+
field.name
|
|
469
|
+
for field in probe.schema
|
|
470
|
+
if field.name not in declared and _type_is_unsettled(field.type, integer_may_widen=integer_may_widen)
|
|
471
|
+
)
|
|
472
|
+
if unsettled:
|
|
473
|
+
log_with_context(
|
|
474
|
+
logger, logging.INFO, "storage.export.read_ahead", columns=list(unsettled), mode="sync"
|
|
475
|
+
)
|
|
476
|
+
if on_read_ahead is not None:
|
|
477
|
+
on_read_ahead(unsettled)
|
|
478
|
+
tables = [probe]
|
|
479
|
+
while True:
|
|
480
|
+
next_chunk = list(islice(iterator, batch_size))
|
|
481
|
+
if not next_chunk:
|
|
482
|
+
break
|
|
483
|
+
tables.append(pa.Table.from_pylist(next_chunk))
|
|
484
|
+
_close_sync_rows(rows)
|
|
485
|
+
combined = _resolve_null_columns(pa.concat_tables(tables, promote_options="permissive"), column_types)
|
|
486
|
+
if declared:
|
|
487
|
+
declared_schema = pa.schema([
|
|
488
|
+
field.with_type(declared[field.name]) if field.name in declared else field
|
|
489
|
+
for field in combined.schema
|
|
490
|
+
])
|
|
491
|
+
combined = combined.cast(declared_schema)
|
|
492
|
+
return pa.RecordBatchReader.from_batches(combined.schema, combined.to_batches(max_chunksize=batch_size))
|
|
493
|
+
|
|
494
|
+
if declared:
|
|
495
|
+
pinned_schema = pa.schema([
|
|
496
|
+
field.with_type(declared[field.name]) if field.name in declared else field for field in probe.schema
|
|
497
|
+
])
|
|
498
|
+
first_table = probe.cast(pinned_schema)
|
|
499
|
+
else:
|
|
500
|
+
first_table = probe
|
|
501
|
+
schema = first_table.schema
|
|
502
|
+
except BaseException:
|
|
503
|
+
_close_sync_rows(rows)
|
|
504
|
+
raise
|
|
505
|
+
|
|
506
|
+
explicit = arrow_schema is not None
|
|
507
|
+
rows_closed = False
|
|
508
|
+
|
|
509
|
+
def _close_rows_once() -> None:
|
|
510
|
+
nonlocal rows_closed
|
|
511
|
+
if not rows_closed:
|
|
512
|
+
rows_closed = True
|
|
513
|
+
_close_sync_rows(rows)
|
|
514
|
+
|
|
515
|
+
def _iter_batches() -> "Iterator[ArrowRecordBatch]":
|
|
516
|
+
try:
|
|
517
|
+
yield from first_table.to_batches(max_chunksize=batch_size)
|
|
518
|
+
while True:
|
|
519
|
+
later_chunk = list(islice(iterator, batch_size))
|
|
520
|
+
if not later_chunk:
|
|
521
|
+
break
|
|
522
|
+
later_table = _convert_later_chunk(later_chunk, schema, explicit=explicit)
|
|
523
|
+
yield from later_table.to_batches(max_chunksize=batch_size)
|
|
524
|
+
finally:
|
|
525
|
+
_close_rows_once()
|
|
526
|
+
|
|
527
|
+
return arrow_reader_with_deferred_close(
|
|
528
|
+
pa.RecordBatchReader.from_batches(schema, _iter_batches()), _close_rows_once
|
|
529
|
+
)
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
async def arrow_batches_from_async_rows(
|
|
533
|
+
rows: "AsyncIterator[dict[str, Any]]",
|
|
534
|
+
*,
|
|
535
|
+
batch_size: int,
|
|
536
|
+
arrow_schema: Any = None,
|
|
537
|
+
integer_may_widen: bool = False,
|
|
538
|
+
on_read_ahead: "Callable[[tuple[str, ...]], None] | None" = None,
|
|
539
|
+
) -> "AsyncIterator[ArrowRecordBatch] | None":
|
|
540
|
+
"""Convert an asynchronous row iterator into an async Arrow RecordBatch iterator.
|
|
541
|
+
|
|
542
|
+
Args:
|
|
543
|
+
rows: Asynchronous iterator yielding row dictionaries.
|
|
544
|
+
batch_size: Maximum number of rows to convert per batch.
|
|
545
|
+
arrow_schema: Optional explicit PyArrow schema overriding inference.
|
|
546
|
+
integer_may_widen: Whether top-level integer columns may widen to floats in later chunks.
|
|
547
|
+
on_read_ahead: Optional callback receiving unsettled column names when read-ahead occurs.
|
|
548
|
+
|
|
549
|
+
Returns:
|
|
550
|
+
An async iterator yielding ``pyarrow.RecordBatch`` objects, or ``None`` when ``rows`` is empty.
|
|
551
|
+
|
|
552
|
+
Raises:
|
|
553
|
+
ValueError: When ``batch_size < 1``.
|
|
554
|
+
TypeError: When ``arrow_schema`` is provided and is not a ``pyarrow.Schema``.
|
|
555
|
+
"""
|
|
556
|
+
if batch_size < 1:
|
|
557
|
+
msg = "batch_size must be greater than or equal to 1"
|
|
558
|
+
raise ValueError(msg)
|
|
559
|
+
|
|
560
|
+
ensure_pyarrow()
|
|
561
|
+
import pyarrow as pa
|
|
562
|
+
|
|
563
|
+
if arrow_schema is not None and not isinstance(arrow_schema, pa.Schema):
|
|
564
|
+
await _aclose_async_rows(rows)
|
|
565
|
+
msg = f"arrow_schema must be a pyarrow.Schema, got {type(arrow_schema).__name__}"
|
|
566
|
+
raise TypeError(msg)
|
|
567
|
+
|
|
568
|
+
try:
|
|
569
|
+
iterator = aiter(rows)
|
|
570
|
+
first_chunk = await _read_async_chunk(iterator, batch_size)
|
|
571
|
+
if not first_chunk:
|
|
572
|
+
await _aclose_async_rows(rows)
|
|
573
|
+
return None
|
|
574
|
+
|
|
575
|
+
raw_types = getattr(rows, "column_types", None)
|
|
576
|
+
column_types: Mapping[str, str] | None = raw_types if isinstance(raw_types, Mapping) else None
|
|
577
|
+
probe = pa.Table.from_pylist(first_chunk)
|
|
578
|
+
|
|
579
|
+
if arrow_schema is not None:
|
|
580
|
+
_reject_lost_struct_fields(probe.schema, arrow_schema)
|
|
581
|
+
first_table = cast(
|
|
582
|
+
"ArrowTable",
|
|
583
|
+
convert_dict_to_arrow_with_schema(
|
|
584
|
+
first_chunk, return_format="table", arrow_schema=arrow_schema, column_types=column_types
|
|
585
|
+
),
|
|
586
|
+
)
|
|
587
|
+
schema = first_table.schema
|
|
588
|
+
else:
|
|
589
|
+
declared = _declared_types(probe.schema, column_types)
|
|
590
|
+
unsettled = tuple(
|
|
591
|
+
field.name
|
|
592
|
+
for field in probe.schema
|
|
593
|
+
if field.name not in declared and _type_is_unsettled(field.type, integer_may_widen=integer_may_widen)
|
|
594
|
+
)
|
|
595
|
+
if unsettled:
|
|
596
|
+
log_with_context(
|
|
597
|
+
logger, logging.INFO, "storage.export.read_ahead", columns=list(unsettled), mode="async"
|
|
598
|
+
)
|
|
599
|
+
if on_read_ahead is not None:
|
|
600
|
+
on_read_ahead(unsettled)
|
|
601
|
+
tables = [probe]
|
|
602
|
+
while True:
|
|
603
|
+
next_chunk = await _read_async_chunk(iterator, batch_size)
|
|
604
|
+
if not next_chunk:
|
|
605
|
+
break
|
|
606
|
+
tables.append(pa.Table.from_pylist(next_chunk))
|
|
607
|
+
await _aclose_async_rows(rows)
|
|
608
|
+
combined = _resolve_null_columns(pa.concat_tables(tables, promote_options="permissive"), column_types)
|
|
609
|
+
if declared:
|
|
610
|
+
declared_schema = pa.schema([
|
|
611
|
+
field.with_type(declared[field.name]) if field.name in declared else field
|
|
612
|
+
for field in combined.schema
|
|
613
|
+
])
|
|
614
|
+
combined = combined.cast(declared_schema)
|
|
615
|
+
read_ahead_batches = combined.to_batches(max_chunksize=batch_size)
|
|
616
|
+
|
|
617
|
+
async def _iter_read_ahead() -> "AsyncIterator[ArrowRecordBatch]":
|
|
618
|
+
for batch in read_ahead_batches:
|
|
619
|
+
yield batch
|
|
620
|
+
|
|
621
|
+
return _AsyncRowBatchIterator(_iter_read_ahead(), None)
|
|
622
|
+
|
|
623
|
+
if declared:
|
|
624
|
+
pinned_schema = pa.schema([
|
|
625
|
+
field.with_type(declared[field.name]) if field.name in declared else field for field in probe.schema
|
|
626
|
+
])
|
|
627
|
+
first_table = probe.cast(pinned_schema)
|
|
628
|
+
else:
|
|
629
|
+
first_table = probe
|
|
630
|
+
schema = first_table.schema
|
|
631
|
+
except BaseException:
|
|
632
|
+
await _aclose_async_rows(rows)
|
|
633
|
+
raise
|
|
634
|
+
|
|
635
|
+
explicit = arrow_schema is not None
|
|
636
|
+
|
|
637
|
+
async def _iter_pinned() -> "AsyncIterator[ArrowRecordBatch]":
|
|
638
|
+
for batch in first_table.to_batches(max_chunksize=batch_size):
|
|
639
|
+
yield batch
|
|
640
|
+
while True:
|
|
641
|
+
later_chunk = await _read_async_chunk(iterator, batch_size)
|
|
642
|
+
if not later_chunk:
|
|
643
|
+
break
|
|
644
|
+
later_table = _convert_later_chunk(later_chunk, schema, explicit=explicit)
|
|
645
|
+
for batch in later_table.to_batches(max_chunksize=batch_size):
|
|
646
|
+
yield batch
|
|
647
|
+
|
|
648
|
+
return _AsyncRowBatchIterator(_iter_pinned(), rows)
|
|
649
|
+
|
|
650
|
+
|
|
651
|
+
def records_to_arrow_table(
|
|
652
|
+
records: "Iterable[Mapping[str, Any]] | Iterable[Iterable[Any]]", columns: "list[str] | None"
|
|
653
|
+
) -> "ArrowTable":
|
|
654
|
+
"""Normalize mapping or positional records into a PyArrow table for ingest."""
|
|
655
|
+
ensure_pyarrow()
|
|
656
|
+
import pyarrow as pa
|
|
657
|
+
|
|
658
|
+
materialized = list(records)
|
|
659
|
+
if not materialized:
|
|
660
|
+
msg = "load_from_records requires at least one record."
|
|
661
|
+
raise ImproperConfigurationError(msg)
|
|
662
|
+
|
|
663
|
+
first = materialized[0]
|
|
664
|
+
if isinstance(first, Mapping):
|
|
665
|
+
resolved = columns if columns is not None else list(first.keys())
|
|
666
|
+
expected = set(resolved)
|
|
667
|
+
for record in materialized:
|
|
668
|
+
if not isinstance(record, Mapping):
|
|
669
|
+
msg = "load_from_records mapping records must all be mappings."
|
|
670
|
+
raise ImproperConfigurationError(msg)
|
|
671
|
+
if record.keys() != expected:
|
|
672
|
+
msg = "load_from_records mapping records must all share the same keys."
|
|
673
|
+
raise ImproperConfigurationError(msg)
|
|
674
|
+
mapping_records = cast("list[Mapping[str, Any]]", materialized)
|
|
675
|
+
table = pa.Table.from_pylist(mapping_records)
|
|
676
|
+
return table if columns is None else table.select(resolved)
|
|
677
|
+
|
|
678
|
+
if columns is None:
|
|
679
|
+
msg = "load_from_records requires columns when records are positional sequences."
|
|
680
|
+
raise ImproperConfigurationError(msg)
|
|
681
|
+
|
|
682
|
+
row_values = [tuple(record) for record in materialized]
|
|
683
|
+
if any(len(row) != len(columns) for row in row_values):
|
|
684
|
+
msg = "load_from_records positional records must match the number of columns."
|
|
685
|
+
raise ImproperConfigurationError(msg)
|
|
686
|
+
try:
|
|
687
|
+
arrays = [pa.array(values) for values in zip(*row_values, strict=True)]
|
|
688
|
+
except ValueError as exc:
|
|
689
|
+
msg = "load_from_records positional records must match the number of columns."
|
|
690
|
+
raise ImproperConfigurationError(msg) from exc
|
|
691
|
+
return pa.Table.from_arrays(arrays, names=columns)
|
|
692
|
+
|
|
693
|
+
|
|
694
|
+
def coerce_arrow_table(source: "ArrowResult | Any") -> "ArrowTable":
|
|
695
|
+
"""Coerce various sources to a PyArrow Table."""
|
|
696
|
+
ensure_pyarrow()
|
|
697
|
+
|
|
698
|
+
if has_get_data(source):
|
|
699
|
+
table = source.get_data()
|
|
700
|
+
coercer = _get_arrow_table_coercer().get(table)
|
|
701
|
+
if coercer is not None:
|
|
702
|
+
return cast("ArrowTable", coercer(table))
|
|
703
|
+
msg = "ArrowResult did not return a pyarrow.Table instance"
|
|
704
|
+
raise TypeError(msg)
|
|
705
|
+
coercer = _get_arrow_table_coercer().get(source)
|
|
706
|
+
if coercer is not None:
|
|
707
|
+
return cast("ArrowTable", coercer(source))
|
|
708
|
+
if hasattr(source, "__arrow_c_stream__"):
|
|
709
|
+
import pyarrow as pa
|
|
710
|
+
|
|
711
|
+
return pa.RecordBatchReader.from_stream(cast("Any", source)).read_all()
|
|
712
|
+
if isinstance(source, Mapping):
|
|
713
|
+
import pyarrow as pa
|
|
714
|
+
|
|
715
|
+
return pa.Table.from_pydict(source)
|
|
716
|
+
if isinstance(source, Iterable):
|
|
717
|
+
import pyarrow as pa
|
|
718
|
+
|
|
719
|
+
return pa.Table.from_pylist(list(source))
|
|
720
|
+
msg = f"Unsupported Arrow source type: {type(source).__name__}"
|
|
721
|
+
raise TypeError(msg)
|
|
722
|
+
|
|
723
|
+
|
|
724
|
+
def coerce_arrow_batches(
|
|
725
|
+
source: "ArrowResult | Any", *, batch_size: int = 65_536
|
|
726
|
+
) -> "tuple[list[str], Iterator[ArrowRecordBatch]]":
|
|
727
|
+
"""Coerce an Arrow source into column names and a lazy RecordBatch iterator.
|
|
728
|
+
|
|
729
|
+
Streams ``pyarrow.RecordBatchReader`` and ``__arrow_c_stream__`` sources one
|
|
730
|
+
batch at a time without materializing the full table into memory.
|
|
731
|
+
|
|
732
|
+
Args:
|
|
733
|
+
source: ArrowResult, RecordBatchReader, RecordBatch, Table, C stream, or dict/row iterable.
|
|
734
|
+
batch_size: Maximum rows per batch when splitting an in-memory Table.
|
|
735
|
+
|
|
736
|
+
Returns:
|
|
737
|
+
Tuple of ``(column_names, batch_iterator)``.
|
|
738
|
+
"""
|
|
739
|
+
ensure_pyarrow()
|
|
740
|
+
import pyarrow as pa
|
|
741
|
+
|
|
742
|
+
unwrapped = source.get_data() if has_get_data(source) else source
|
|
743
|
+
if isinstance(unwrapped, pa.RecordBatchReader):
|
|
744
|
+
return list(unwrapped.schema.names), iter(unwrapped)
|
|
745
|
+
if isinstance(unwrapped, pa.RecordBatch):
|
|
746
|
+
return list(unwrapped.schema.names), iter((unwrapped,))
|
|
747
|
+
if isinstance(unwrapped, pa.Table):
|
|
748
|
+
return list(unwrapped.schema.names), iter(unwrapped.to_batches(max_chunksize=batch_size))
|
|
749
|
+
if hasattr(unwrapped, "__arrow_c_stream__"):
|
|
750
|
+
reader = pa.RecordBatchReader.from_stream(cast("Any", unwrapped))
|
|
751
|
+
return list(reader.schema.names), iter(reader)
|
|
752
|
+
table = coerce_arrow_table(unwrapped)
|
|
753
|
+
return list(table.schema.names), iter(table.to_batches(max_chunksize=batch_size))
|
|
754
|
+
|
|
755
|
+
|
|
756
|
+
def arrow_batch_to_rows(batch: "ArrowRecordBatch", columns: "list[str] | None" = None) -> "list[tuple[Any, ...]]":
|
|
757
|
+
"""Convert a single Arrow RecordBatch to a list of row tuples.
|
|
758
|
+
|
|
759
|
+
Args:
|
|
760
|
+
batch: Arrow RecordBatch to extract rows from.
|
|
761
|
+
columns: Optional subset or ordering of column names.
|
|
762
|
+
|
|
763
|
+
Returns:
|
|
764
|
+
List of row tuples for the batch.
|
|
765
|
+
"""
|
|
766
|
+
ensure_pyarrow()
|
|
767
|
+
resolved_columns = columns or list(batch.schema.names)
|
|
768
|
+
if not resolved_columns or batch.num_rows == 0:
|
|
769
|
+
return []
|
|
770
|
+
col_data = [batch.column(col).to_pylist() for col in resolved_columns]
|
|
771
|
+
if not col_data or not col_data[0]:
|
|
772
|
+
return []
|
|
773
|
+
return [tuple(row) for row in zip(*col_data, strict=False)]
|
|
774
|
+
|
|
775
|
+
|
|
776
|
+
def ensure_arrow_table(data: Any) -> "ArrowTable":
|
|
777
|
+
"""Ensure data is a PyArrow Table."""
|
|
778
|
+
ensure_pyarrow()
|
|
779
|
+
import pyarrow as pa
|
|
780
|
+
|
|
781
|
+
if isinstance(data, pa.Table):
|
|
782
|
+
return data
|
|
783
|
+
msg = f"Expected an Arrow Table, but got {type(data).__name__}"
|
|
784
|
+
raise TypeError(msg)
|
|
785
|
+
|
|
786
|
+
|
|
787
|
+
def cast_arrow_table_schema(table: "ArrowTable", arrow_schema: Any) -> "ArrowTable":
|
|
788
|
+
"""Cast an Arrow table to a provided schema.
|
|
789
|
+
|
|
790
|
+
Args:
|
|
791
|
+
table: Arrow table to cast.
|
|
792
|
+
arrow_schema: Optional pyarrow.Schema for casting.
|
|
793
|
+
|
|
794
|
+
Returns:
|
|
795
|
+
Arrow table with updated schema.
|
|
796
|
+
|
|
797
|
+
Raises:
|
|
798
|
+
TypeError: If arrow_schema is not a pyarrow.Schema instance.
|
|
799
|
+
"""
|
|
800
|
+
if arrow_schema is None:
|
|
801
|
+
return table
|
|
802
|
+
|
|
803
|
+
ensure_pyarrow()
|
|
804
|
+
import pyarrow as pa
|
|
805
|
+
|
|
806
|
+
if not isinstance(arrow_schema, pa.Schema):
|
|
807
|
+
msg = f"arrow_schema must be a pyarrow.Schema, got {type(arrow_schema).__name__}"
|
|
808
|
+
raise TypeError(msg)
|
|
809
|
+
return table.cast(arrow_schema)
|
|
810
|
+
|
|
811
|
+
|
|
812
|
+
def arrow_table_to_return_format(
|
|
813
|
+
table: "ArrowTable",
|
|
814
|
+
return_format: Literal["table", "reader", "batch", "batches"] = "table",
|
|
815
|
+
batch_size: int | None = None,
|
|
816
|
+
) -> "ArrowTable | ArrowRecordBatch | ArrowRecordBatchReader | list[ArrowRecordBatch]":
|
|
817
|
+
"""Convert an Arrow table into the requested return format.
|
|
818
|
+
|
|
819
|
+
Args:
|
|
820
|
+
table: Arrow table to convert.
|
|
821
|
+
return_format: Output format (table, reader, batch, batches).
|
|
822
|
+
batch_size: Batch size for reader/batch outputs.
|
|
823
|
+
|
|
824
|
+
Returns:
|
|
825
|
+
Converted Arrow data in the requested format.
|
|
826
|
+
"""
|
|
827
|
+
ensure_pyarrow()
|
|
828
|
+
import pyarrow as pa
|
|
829
|
+
|
|
830
|
+
if return_format == "table":
|
|
831
|
+
return table
|
|
832
|
+
|
|
833
|
+
batches = table.to_batches(max_chunksize=batch_size)
|
|
834
|
+
if return_format == "reader":
|
|
835
|
+
return pa.RecordBatchReader.from_batches(table.schema, batches)
|
|
836
|
+
if return_format == "batches":
|
|
837
|
+
return batches
|
|
838
|
+
return batches[0] if batches else pa.RecordBatch.from_pylist([], schema=table.schema)
|
|
839
|
+
|
|
840
|
+
|
|
841
|
+
def arrow_table_to_rows(
|
|
842
|
+
table: "ArrowTable", columns: "list[str] | None" = None
|
|
843
|
+
) -> "tuple[list[str], list[tuple[Any, ...]]]":
|
|
844
|
+
"""Convert Arrow table to column names and row tuples."""
|
|
845
|
+
ensure_pyarrow()
|
|
846
|
+
resolved_columns = columns or list(table.column_names)
|
|
847
|
+
if not resolved_columns:
|
|
848
|
+
msg = "Arrow table has no columns to import"
|
|
849
|
+
raise ValueError(msg)
|
|
850
|
+
|
|
851
|
+
col_data = [table.column(col).to_pylist() for col in resolved_columns]
|
|
852
|
+
if not col_data or not col_data[0]:
|
|
853
|
+
return resolved_columns, []
|
|
854
|
+
|
|
855
|
+
records: list[tuple[Any, ...]] = [tuple(row) for row in zip(*col_data, strict=False)]
|
|
856
|
+
return resolved_columns, records
|
|
857
|
+
|
|
858
|
+
|
|
859
|
+
def arrow_table_needs_parameter_preparation(table: "ArrowTable | ArrowRecordBatch") -> bool:
|
|
860
|
+
"""Return whether Arrow rows may emit nested values needing driver preparation."""
|
|
861
|
+
return _arrow_schema_needs_preparation(table.schema)
|
|
862
|
+
|
|
863
|
+
|
|
864
|
+
def arrow_table_to_pylist(
|
|
865
|
+
table: "ArrowTable | ArrowRecordBatch", *, decode_arrow_extension_types: bool = False
|
|
866
|
+
) -> "list[dict[str, Any]]":
|
|
867
|
+
"""Convert Arrow data to dictionaries and optionally decode opaque UUID fields."""
|
|
868
|
+
if not decode_arrow_extension_types or not _arrow_schema_has_opaque_uuid(table.schema):
|
|
869
|
+
return table.to_pylist()
|
|
870
|
+
|
|
871
|
+
column_values = [
|
|
872
|
+
_arrow_uuid_column_to_pylist(table.column(index), field.type) for index, field in enumerate(table.schema)
|
|
873
|
+
]
|
|
874
|
+
return [dict(zip(table.column_names, row, strict=False)) for row in zip(*column_values, strict=False)]
|
|
875
|
+
|
|
876
|
+
|
|
877
|
+
def arrow_table_column_names(table: "ArrowTable") -> "list[str]":
|
|
878
|
+
"""Return Arrow table column names."""
|
|
879
|
+
return list(table.column_names)
|
|
880
|
+
|
|
881
|
+
|
|
882
|
+
def arrow_table_num_rows(table: "ArrowTable") -> int:
|
|
883
|
+
"""Return Arrow table row count."""
|
|
884
|
+
return int(table.num_rows)
|
|
885
|
+
|
|
886
|
+
|
|
887
|
+
def arrow_table_num_columns(table: "ArrowTable") -> int:
|
|
888
|
+
"""Return Arrow table column count."""
|
|
889
|
+
return int(table.num_columns)
|
|
890
|
+
|
|
891
|
+
|
|
892
|
+
def arrow_table_to_pandas(table: "ArrowTable") -> "PandasDataFrame":
|
|
893
|
+
"""Convert Arrow table to pandas DataFrame."""
|
|
894
|
+
ensure_pandas()
|
|
895
|
+
import pandas as pd
|
|
896
|
+
|
|
897
|
+
result = table.to_pandas()
|
|
898
|
+
if not isinstance(result, pd.DataFrame):
|
|
899
|
+
msg = f"Expected a pandas DataFrame, but got {type(result).__name__}"
|
|
900
|
+
raise TypeError(msg)
|
|
901
|
+
return result
|
|
902
|
+
|
|
903
|
+
|
|
904
|
+
def arrow_table_to_polars(table: "ArrowTable") -> "PolarsDataFrame":
|
|
905
|
+
"""Convert Arrow table to Polars DataFrame."""
|
|
906
|
+
ensure_polars()
|
|
907
|
+
import polars as pl
|
|
908
|
+
|
|
909
|
+
result = pl.from_arrow(table)
|
|
910
|
+
if not isinstance(result, pl.DataFrame):
|
|
911
|
+
msg = f"Expected a Polars DataFrame, but got {type(result).__name__}"
|
|
912
|
+
raise TypeError(msg)
|
|
913
|
+
return result
|
|
914
|
+
|
|
915
|
+
|
|
916
|
+
def build_ingest_telemetry(table: "ArrowTable", *, format_label: str = "arrow") -> "StorageTelemetry":
|
|
917
|
+
"""Build telemetry dict from Arrow table statistics."""
|
|
918
|
+
if has_arrow_table_stats(table):
|
|
919
|
+
rows = int(table.num_rows)
|
|
920
|
+
bytes_processed = int(table.nbytes)
|
|
921
|
+
else:
|
|
922
|
+
rows = 0
|
|
923
|
+
bytes_processed = 0
|
|
924
|
+
return {"rows_processed": rows, "bytes_processed": bytes_processed, "format": format_label}
|
|
925
|
+
|
|
926
|
+
|
|
927
|
+
def arrow_reader_with_deferred_close(reader: Any, close_callback: "Callable[[], None]") -> "ArrowRecordBatchReader":
|
|
928
|
+
"""Wrap a RecordBatchReader so close_callback fires on close, exhaustion, or error."""
|
|
929
|
+
ensure_pyarrow()
|
|
930
|
+
import pyarrow as pa
|
|
931
|
+
|
|
932
|
+
iterator = _DeferredCloseBatchIterator(reader, close_callback)
|
|
933
|
+
base_reader = pa.RecordBatchReader.from_batches(reader.schema, iterator)
|
|
934
|
+
reader_cls = _get_deferred_close_reader_cls()
|
|
935
|
+
return cast("ArrowRecordBatchReader", reader_cls(base_reader, iterator.close))
|
|
936
|
+
|
|
937
|
+
|
|
938
|
+
def arrow_reader_to_return_format(
|
|
939
|
+
reader: Any,
|
|
940
|
+
return_format: Literal["table", "reader", "batch", "batches"] = "reader",
|
|
941
|
+
batch_size: "int | None" = None,
|
|
942
|
+
arrow_schema: Any = None,
|
|
943
|
+
) -> "tuple[Any, int]":
|
|
944
|
+
"""Shape a RecordBatchReader into the requested Arrow payload and row count.
|
|
945
|
+
|
|
946
|
+
Returns -1 as the row count for lazily-streamed reader payloads.
|
|
947
|
+
"""
|
|
948
|
+
ensure_pyarrow()
|
|
949
|
+
import pyarrow as pa
|
|
950
|
+
|
|
951
|
+
if not isinstance(reader, pa.RecordBatchReader):
|
|
952
|
+
msg = f"Expected a pyarrow.RecordBatchReader, got {type(reader).__name__}"
|
|
953
|
+
raise TypeError(msg)
|
|
954
|
+
if arrow_schema is not None:
|
|
955
|
+
if not isinstance(arrow_schema, pa.Schema):
|
|
956
|
+
reader_close = getattr(reader, "close", None)
|
|
957
|
+
if callable(reader_close):
|
|
958
|
+
with contextlib.suppress(Exception):
|
|
959
|
+
reader_close()
|
|
960
|
+
msg = f"arrow_schema must be a pyarrow.Schema, got {type(arrow_schema).__name__}"
|
|
961
|
+
raise TypeError(msg)
|
|
962
|
+
try:
|
|
963
|
+
reader = reader.cast(arrow_schema)
|
|
964
|
+
except BaseException:
|
|
965
|
+
reader_close = getattr(reader, "close", None)
|
|
966
|
+
if callable(reader_close):
|
|
967
|
+
with contextlib.suppress(Exception):
|
|
968
|
+
reader_close()
|
|
969
|
+
raise
|
|
970
|
+
if return_format == "reader":
|
|
971
|
+
return reader, -1
|
|
972
|
+
try:
|
|
973
|
+
if return_format == "batches":
|
|
974
|
+
batches = list(reader)
|
|
975
|
+
return batches, sum(batch.num_rows for batch in batches)
|
|
976
|
+
table = reader.read_all()
|
|
977
|
+
shaped = arrow_table_to_return_format(table, return_format=return_format, batch_size=batch_size)
|
|
978
|
+
return shaped, int(table.num_rows)
|
|
979
|
+
finally:
|
|
980
|
+
reader_close = getattr(reader, "close", None)
|
|
981
|
+
if callable(reader_close):
|
|
982
|
+
with contextlib.suppress(Exception):
|
|
983
|
+
reader_close()
|
|
984
|
+
|
|
985
|
+
|
|
986
|
+
def encode_arrow_batch_pg_binary(
|
|
987
|
+
batch: "ArrowRecordBatch",
|
|
988
|
+
*,
|
|
989
|
+
columns: "Sequence[str] | None" = None,
|
|
990
|
+
vector_columns: frozenset[str] = frozenset(),
|
|
991
|
+
jsonb_columns: frozenset[str] = frozenset(),
|
|
992
|
+
include_header: bool = False,
|
|
993
|
+
include_trailer: bool = False,
|
|
994
|
+
) -> bytes:
|
|
995
|
+
"""Encode an Arrow RecordBatch into PostgreSQL COPY BINARY format.
|
|
996
|
+
|
|
997
|
+
Uses zero-loop structured NumPy dtype packing when all columns have fixed
|
|
998
|
+
wire widths and no nulls, falling back to direct Arrow buffer slicing for
|
|
999
|
+
variable-width and nullable columns.
|
|
1000
|
+
|
|
1001
|
+
Args:
|
|
1002
|
+
batch: Arrow RecordBatch to encode.
|
|
1003
|
+
columns: Optional subset or order of columns to encode.
|
|
1004
|
+
vector_columns: Column names using pgvector binary wire format.
|
|
1005
|
+
jsonb_columns: Column names requiring PostgreSQL jsonb version byte prefix.
|
|
1006
|
+
include_header: Whether to prepend ``PG_BINARY_COPY_HEADER``.
|
|
1007
|
+
include_trailer: Whether to append ``PG_BINARY_COPY_TRAILER``.
|
|
1008
|
+
|
|
1009
|
+
Returns:
|
|
1010
|
+
PostgreSQL ``COPY ... WITH (FORMAT BINARY)`` byte payload.
|
|
1011
|
+
"""
|
|
1012
|
+
ensure_pyarrow()
|
|
1013
|
+
resolved_columns = list(columns) if columns is not None else list(batch.schema.names)
|
|
1014
|
+
body = b""
|
|
1015
|
+
if resolved_columns and batch.num_rows > 0:
|
|
1016
|
+
fast_body = _encode_pg_binary_fast_path_1(batch, resolved_columns, jsonb_columns)
|
|
1017
|
+
if fast_body is not None:
|
|
1018
|
+
body = fast_body
|
|
1019
|
+
else:
|
|
1020
|
+
body = _encode_pg_binary_fast_path_2(batch, resolved_columns, vector_columns, jsonb_columns)
|
|
1021
|
+
parts: list[bytes] = []
|
|
1022
|
+
if include_header:
|
|
1023
|
+
parts.append(PG_BINARY_COPY_HEADER)
|
|
1024
|
+
if body:
|
|
1025
|
+
parts.append(body)
|
|
1026
|
+
if include_trailer:
|
|
1027
|
+
parts.append(PG_BINARY_COPY_TRAILER)
|
|
1028
|
+
return b"".join(parts)
|
|
1029
|
+
|
|
1030
|
+
|
|
1031
|
+
def iter_pgcopy_binary_chunks(
|
|
1032
|
+
source: "ArrowResult | Any",
|
|
1033
|
+
*,
|
|
1034
|
+
columns: "Sequence[str] | None" = None,
|
|
1035
|
+
batch_size: int = 65_536,
|
|
1036
|
+
chunk_bytes: int = 8_388_608,
|
|
1037
|
+
vector_columns: frozenset[str] = frozenset(),
|
|
1038
|
+
jsonb_columns: frozenset[str] = frozenset(),
|
|
1039
|
+
metrics: "dict[str, int] | None" = None,
|
|
1040
|
+
) -> "Iterator[bytes]":
|
|
1041
|
+
"""Yield PostgreSQL COPY BINARY chunks from an Arrow source in bounded memory.
|
|
1042
|
+
|
|
1043
|
+
Args:
|
|
1044
|
+
source: Arrow Table, RecordBatch, RecordBatchReader, ArrowResult, or RecordBatch iterator.
|
|
1045
|
+
columns: Optional explicit column order.
|
|
1046
|
+
batch_size: Maximum rows per batch when splitting a Table.
|
|
1047
|
+
chunk_bytes: Target byte size for yielded chunks (default 8 MiB).
|
|
1048
|
+
vector_columns: Column names using pgvector binary wire format.
|
|
1049
|
+
jsonb_columns: Column names requiring PostgreSQL jsonb version byte prefix.
|
|
1050
|
+
metrics: Optional mutable dict updated in-place with ``rows_processed`` and ``bytes_processed``.
|
|
1051
|
+
|
|
1052
|
+
Yields:
|
|
1053
|
+
Byte chunks forming a complete ``PGCOPY`` binary stream.
|
|
1054
|
+
"""
|
|
1055
|
+
ensure_pyarrow()
|
|
1056
|
+
import pyarrow as pa
|
|
1057
|
+
|
|
1058
|
+
if columns is not None and isinstance(source, Iterator) and not isinstance(source, pa.RecordBatchReader):
|
|
1059
|
+
resolved_columns = list(columns)
|
|
1060
|
+
batches: Iterator[ArrowRecordBatch] = source
|
|
1061
|
+
else:
|
|
1062
|
+
inferred_columns, batches = coerce_arrow_batches(source, batch_size=batch_size)
|
|
1063
|
+
resolved_columns = list(columns) if columns is not None else inferred_columns
|
|
1064
|
+
|
|
1065
|
+
buffer = bytearray(PG_BINARY_COPY_HEADER)
|
|
1066
|
+
threshold = max(1, int(chunk_bytes))
|
|
1067
|
+
total_rows = 0
|
|
1068
|
+
total_bytes = 0
|
|
1069
|
+
for batch in batches:
|
|
1070
|
+
if batch.num_rows == 0:
|
|
1071
|
+
continue
|
|
1072
|
+
total_rows += int(batch.num_rows)
|
|
1073
|
+
total_bytes += int(batch.nbytes)
|
|
1074
|
+
encoded = encode_arrow_batch_pg_binary(
|
|
1075
|
+
batch,
|
|
1076
|
+
columns=resolved_columns,
|
|
1077
|
+
vector_columns=vector_columns,
|
|
1078
|
+
jsonb_columns=jsonb_columns,
|
|
1079
|
+
include_header=False,
|
|
1080
|
+
include_trailer=False,
|
|
1081
|
+
)
|
|
1082
|
+
buffer.extend(encoded)
|
|
1083
|
+
if len(buffer) >= threshold:
|
|
1084
|
+
yield bytes(buffer)
|
|
1085
|
+
buffer.clear()
|
|
1086
|
+
if metrics is not None:
|
|
1087
|
+
metrics["rows_processed"] = total_rows
|
|
1088
|
+
metrics["bytes_processed"] = total_bytes
|
|
1089
|
+
buffer.extend(PG_BINARY_COPY_TRAILER)
|
|
1090
|
+
yield bytes(buffer)
|
|
1091
|
+
|
|
1092
|
+
|
|
1093
|
+
def iter_pgcopy_binary_chunks_async(
|
|
1094
|
+
source: "ArrowResult | Any",
|
|
1095
|
+
*,
|
|
1096
|
+
columns: "Sequence[str] | None" = None,
|
|
1097
|
+
batch_size: int = 65_536,
|
|
1098
|
+
chunk_bytes: int = 8_388_608,
|
|
1099
|
+
vector_columns: frozenset[str] = frozenset(),
|
|
1100
|
+
jsonb_columns: frozenset[str] = frozenset(),
|
|
1101
|
+
metrics: "dict[str, int] | None" = None,
|
|
1102
|
+
) -> Any:
|
|
1103
|
+
"""Return an async byte-chunk iterator of PostgreSQL COPY BINARY payloads."""
|
|
1104
|
+
return _AsyncBytesIterator(
|
|
1105
|
+
iter_pgcopy_binary_chunks(
|
|
1106
|
+
source,
|
|
1107
|
+
columns=columns,
|
|
1108
|
+
batch_size=batch_size,
|
|
1109
|
+
chunk_bytes=chunk_bytes,
|
|
1110
|
+
vector_columns=vector_columns,
|
|
1111
|
+
jsonb_columns=jsonb_columns,
|
|
1112
|
+
metrics=metrics,
|
|
1113
|
+
)
|
|
1114
|
+
)
|
|
1115
|
+
|
|
1116
|
+
|
|
1117
|
+
def _extract_pg_fixed_column_array(col: Any, dtype: Any, num_rows: int) -> "tuple[int, str, Any] | None":
|
|
1118
|
+
"""Return ``(byte_width, numpy_dtype_str, numpy_array)`` for fixed-width scalar columns."""
|
|
1119
|
+
import numpy as np
|
|
1120
|
+
import pyarrow as pa
|
|
1121
|
+
|
|
1122
|
+
is_extension = getattr(pa.types, "is_extension", None)
|
|
1123
|
+
if is_extension is not None and is_extension(dtype):
|
|
1124
|
+
dtype = dtype.storage_type
|
|
1125
|
+
col = col.storage if hasattr(col, "storage") else col
|
|
1126
|
+
|
|
1127
|
+
if pa.types.is_boolean(dtype):
|
|
1128
|
+
filled = col.fill_null(False) if col.null_count > 0 else col
|
|
1129
|
+
arr = filled.to_numpy(zero_copy_only=False).astype("|u1", copy=False)
|
|
1130
|
+
return 1, "|u1", arr
|
|
1131
|
+
if pa.types.is_int8(dtype):
|
|
1132
|
+
return (
|
|
1133
|
+
2,
|
|
1134
|
+
">i2",
|
|
1135
|
+
np.frombuffer(col.buffers()[1], dtype=np.int8, count=col.offset + num_rows)[col.offset :].astype(">i2"),
|
|
1136
|
+
)
|
|
1137
|
+
if pa.types.is_uint8(dtype):
|
|
1138
|
+
return (
|
|
1139
|
+
2,
|
|
1140
|
+
">i2",
|
|
1141
|
+
np.frombuffer(col.buffers()[1], dtype=np.uint8, count=col.offset + num_rows)[col.offset :].astype(">i2"),
|
|
1142
|
+
)
|
|
1143
|
+
if pa.types.is_int16(dtype):
|
|
1144
|
+
return (
|
|
1145
|
+
2,
|
|
1146
|
+
">i2",
|
|
1147
|
+
np.frombuffer(col.buffers()[1], dtype=np.int16, count=col.offset + num_rows)[col.offset :].astype(
|
|
1148
|
+
">i2", copy=False
|
|
1149
|
+
),
|
|
1150
|
+
)
|
|
1151
|
+
if pa.types.is_uint16(dtype):
|
|
1152
|
+
return (
|
|
1153
|
+
4,
|
|
1154
|
+
">i4",
|
|
1155
|
+
np.frombuffer(col.buffers()[1], dtype=np.uint16, count=col.offset + num_rows)[col.offset :].astype(">i4"),
|
|
1156
|
+
)
|
|
1157
|
+
if pa.types.is_int32(dtype):
|
|
1158
|
+
return (
|
|
1159
|
+
4,
|
|
1160
|
+
">i4",
|
|
1161
|
+
np.frombuffer(col.buffers()[1], dtype=np.int32, count=col.offset + num_rows)[col.offset :].astype(
|
|
1162
|
+
">i4", copy=False
|
|
1163
|
+
),
|
|
1164
|
+
)
|
|
1165
|
+
if pa.types.is_uint32(dtype):
|
|
1166
|
+
return (
|
|
1167
|
+
8,
|
|
1168
|
+
">i8",
|
|
1169
|
+
np.frombuffer(col.buffers()[1], dtype=np.uint32, count=col.offset + num_rows)[col.offset :].astype(">i8"),
|
|
1170
|
+
)
|
|
1171
|
+
if pa.types.is_int64(dtype) or pa.types.is_uint64(dtype):
|
|
1172
|
+
return (
|
|
1173
|
+
8,
|
|
1174
|
+
">i8",
|
|
1175
|
+
np.frombuffer(col.buffers()[1], dtype=np.int64, count=col.offset + num_rows)[col.offset :].astype(
|
|
1176
|
+
">i8", copy=False
|
|
1177
|
+
),
|
|
1178
|
+
)
|
|
1179
|
+
if pa.types.is_float16(dtype):
|
|
1180
|
+
return (
|
|
1181
|
+
4,
|
|
1182
|
+
">f4",
|
|
1183
|
+
np.frombuffer(col.buffers()[1], dtype=np.float16, count=col.offset + num_rows)[col.offset :].astype(">f4"),
|
|
1184
|
+
)
|
|
1185
|
+
if pa.types.is_float32(dtype):
|
|
1186
|
+
return (
|
|
1187
|
+
4,
|
|
1188
|
+
">f4",
|
|
1189
|
+
np.frombuffer(col.buffers()[1], dtype=np.float32, count=col.offset + num_rows)[col.offset :].astype(
|
|
1190
|
+
">f4", copy=False
|
|
1191
|
+
),
|
|
1192
|
+
)
|
|
1193
|
+
if pa.types.is_float64(dtype):
|
|
1194
|
+
return (
|
|
1195
|
+
8,
|
|
1196
|
+
">f8",
|
|
1197
|
+
np.frombuffer(col.buffers()[1], dtype=np.float64, count=col.offset + num_rows)[col.offset :].astype(
|
|
1198
|
+
">f8", copy=False
|
|
1199
|
+
),
|
|
1200
|
+
)
|
|
1201
|
+
if pa.types.is_date32(dtype):
|
|
1202
|
+
buf = col.buffers()[1]
|
|
1203
|
+
raw_date32 = np.frombuffer(buf, dtype=np.int32, count=col.offset + num_rows)[col.offset :]
|
|
1204
|
+
return 4, ">i4", (raw_date32 - _PG_DATE_EPOCH_DAYS).astype(">i4")
|
|
1205
|
+
if pa.types.is_date64(dtype):
|
|
1206
|
+
buf = col.buffers()[1]
|
|
1207
|
+
raw_date64 = np.frombuffer(buf, dtype=np.int64, count=col.offset + num_rows)[col.offset :]
|
|
1208
|
+
return 4, ">i4", ((raw_date64 // 86_400_000) - _PG_DATE_EPOCH_DAYS).astype(">i4")
|
|
1209
|
+
if pa.types.is_timestamp(dtype):
|
|
1210
|
+
buf = col.buffers()[1]
|
|
1211
|
+
raw_ts = np.frombuffer(buf, dtype=np.int64, count=col.offset + num_rows)[col.offset :]
|
|
1212
|
+
unit = dtype.unit
|
|
1213
|
+
if unit == "us":
|
|
1214
|
+
shifted = raw_ts - _PG_EPOCH_OFFSET_US
|
|
1215
|
+
elif unit == "ms":
|
|
1216
|
+
shifted = raw_ts * 1_000 - _PG_EPOCH_OFFSET_US
|
|
1217
|
+
elif unit == "s":
|
|
1218
|
+
shifted = raw_ts * 1_000_000 - _PG_EPOCH_OFFSET_US
|
|
1219
|
+
else:
|
|
1220
|
+
shifted = (raw_ts // 1_000) - _PG_EPOCH_OFFSET_US
|
|
1221
|
+
return 8, ">i8", shifted.astype(">i8")
|
|
1222
|
+
if pa.types.is_fixed_size_binary(dtype):
|
|
1223
|
+
width = int(dtype.byte_width)
|
|
1224
|
+
buf = col.buffers()[1]
|
|
1225
|
+
arr = np.frombuffer(buf, dtype=f"|V{width}", count=col.offset + num_rows)[col.offset :]
|
|
1226
|
+
return width, f"|V{width}", arr
|
|
1227
|
+
return None
|
|
1228
|
+
|
|
1229
|
+
|
|
1230
|
+
def _encode_pg_binary_fast_path_1(
|
|
1231
|
+
batch: "ArrowRecordBatch", columns: "list[str]", jsonb_columns: frozenset[str]
|
|
1232
|
+
) -> bytes | None:
|
|
1233
|
+
"""Pack non-null fixed-width and FixedSizeList float vector columns via structured NumPy dtype."""
|
|
1234
|
+
import numpy as np
|
|
1235
|
+
import pyarrow as pa
|
|
1236
|
+
|
|
1237
|
+
num_rows = int(batch.num_rows)
|
|
1238
|
+
dtype_fields: list[Any] = [("ncols", ">i2")]
|
|
1239
|
+
assignments: list[tuple[str, Any]] = [("ncols", len(columns))]
|
|
1240
|
+
|
|
1241
|
+
for idx, col_name in enumerate(columns):
|
|
1242
|
+
if col_name in jsonb_columns:
|
|
1243
|
+
return None
|
|
1244
|
+
col = batch.column(col_name)
|
|
1245
|
+
if col.null_count > 0:
|
|
1246
|
+
return None
|
|
1247
|
+
if isinstance(col, pa.ChunkedArray):
|
|
1248
|
+
col = col.combine_chunks()
|
|
1249
|
+
field_type = col.type
|
|
1250
|
+
fixed_info = _extract_pg_fixed_column_array(col, field_type, num_rows)
|
|
1251
|
+
if fixed_info is not None:
|
|
1252
|
+
byte_width, np_dtype, arr = fixed_info
|
|
1253
|
+
len_key = f"c{idx}_len"
|
|
1254
|
+
val_key = f"c{idx}_val"
|
|
1255
|
+
dtype_fields.append((len_key, ">i4"))
|
|
1256
|
+
dtype_fields.append((val_key, np_dtype))
|
|
1257
|
+
assignments.append((len_key, byte_width))
|
|
1258
|
+
assignments.append((val_key, arr))
|
|
1259
|
+
continue
|
|
1260
|
+
|
|
1261
|
+
if pa.types.is_fixed_size_list(field_type) and pa.types.is_floating(field_type.value_type):
|
|
1262
|
+
dim = int(field_type.list_size)
|
|
1263
|
+
flat_values = cast("Any", col).flatten()
|
|
1264
|
+
if flat_values.null_count > 0:
|
|
1265
|
+
return None
|
|
1266
|
+
is_f16 = pa.types.is_float16(field_type.value_type)
|
|
1267
|
+
elem_dtype = ">f2" if is_f16 else ">f4"
|
|
1268
|
+
elem_size = 2 if is_f16 else 4
|
|
1269
|
+
arr_2d = flat_values.to_numpy(zero_copy_only=False).astype(elem_dtype, copy=False).reshape(num_rows, dim)
|
|
1270
|
+
len_key = f"c{idx}_len"
|
|
1271
|
+
dim_key = f"c{idx}_dim"
|
|
1272
|
+
unused_key = f"c{idx}_unused"
|
|
1273
|
+
val_key = f"c{idx}_val"
|
|
1274
|
+
dtype_fields.extend([
|
|
1275
|
+
(len_key, ">i4"),
|
|
1276
|
+
(dim_key, ">u2"),
|
|
1277
|
+
(unused_key, ">u2"),
|
|
1278
|
+
(val_key, elem_dtype, (dim,)),
|
|
1279
|
+
])
|
|
1280
|
+
assignments.extend([(len_key, 4 + elem_size * dim), (dim_key, dim), (unused_key, 0), (val_key, arr_2d)])
|
|
1281
|
+
continue
|
|
1282
|
+
|
|
1283
|
+
return None
|
|
1284
|
+
|
|
1285
|
+
row_dtype = np.dtype(dtype_fields)
|
|
1286
|
+
out = np.empty(num_rows, dtype=row_dtype)
|
|
1287
|
+
for field_key, value in assignments:
|
|
1288
|
+
out[field_key] = value
|
|
1289
|
+
return out.tobytes()
|
|
1290
|
+
|
|
1291
|
+
|
|
1292
|
+
def _create_pg_fixed_column_encoder(
|
|
1293
|
+
col: Any, field_type: Any, num_rows: int, null_mask: Any
|
|
1294
|
+
) -> "Callable[[int, bytearray], None] | None":
|
|
1295
|
+
"""Create an encoder for fixed-width columns or fixed-size list vectors."""
|
|
1296
|
+
import numpy as np
|
|
1297
|
+
import pyarrow as pa
|
|
1298
|
+
|
|
1299
|
+
fixed_info = _extract_pg_fixed_column_array(col, field_type, num_rows)
|
|
1300
|
+
if fixed_info is not None:
|
|
1301
|
+
byte_width, np_dtype, arr = fixed_info
|
|
1302
|
+
packed = np.empty(num_rows, dtype=np.dtype([("len", ">i4"), ("val", np_dtype)]))
|
|
1303
|
+
packed["len"] = byte_width
|
|
1304
|
+
packed["val"] = arr
|
|
1305
|
+
packed_mv = memoryview(packed.tobytes())
|
|
1306
|
+
stride = 4 + byte_width
|
|
1307
|
+
|
|
1308
|
+
def _emit_fixed(
|
|
1309
|
+
row_idx: int,
|
|
1310
|
+
out_buf: bytearray,
|
|
1311
|
+
_mv: memoryview = packed_mv,
|
|
1312
|
+
_stride: int = stride,
|
|
1313
|
+
_nulls: Any = null_mask,
|
|
1314
|
+
) -> None:
|
|
1315
|
+
if _nulls is not None and _nulls[row_idx]:
|
|
1316
|
+
out_buf.extend(_PG_NULL_MARKER)
|
|
1317
|
+
else:
|
|
1318
|
+
start = row_idx * _stride
|
|
1319
|
+
out_buf.extend(_mv[start : start + _stride])
|
|
1320
|
+
|
|
1321
|
+
return _emit_fixed
|
|
1322
|
+
|
|
1323
|
+
if pa.types.is_fixed_size_list(field_type) and pa.types.is_floating(field_type.value_type):
|
|
1324
|
+
dim = int(field_type.list_size)
|
|
1325
|
+
if null_mask is not None and np.any(null_mask):
|
|
1326
|
+
col_filled = col.fill_null([0.0] * dim)
|
|
1327
|
+
flat_values = col_filled.flatten()
|
|
1328
|
+
else:
|
|
1329
|
+
flat_values = col.flatten()
|
|
1330
|
+
is_f16 = pa.types.is_float16(field_type.value_type)
|
|
1331
|
+
elem_dtype = ">f2" if is_f16 else ">f4"
|
|
1332
|
+
elem_size = 2 if is_f16 else 4
|
|
1333
|
+
arr_2d = flat_values.to_numpy(zero_copy_only=False).astype(elem_dtype, copy=False).reshape(num_rows, dim)
|
|
1334
|
+
packed = np.empty(
|
|
1335
|
+
num_rows, dtype=np.dtype([("len", ">i4"), ("dim", ">u2"), ("unused", ">u2"), ("val", elem_dtype, (dim,))])
|
|
1336
|
+
)
|
|
1337
|
+
packed["len"] = 4 + elem_size * dim
|
|
1338
|
+
packed["dim"] = dim
|
|
1339
|
+
packed["unused"] = 0
|
|
1340
|
+
packed["val"] = arr_2d
|
|
1341
|
+
packed_mv = memoryview(packed.tobytes())
|
|
1342
|
+
stride = 8 + elem_size * dim
|
|
1343
|
+
|
|
1344
|
+
def _emit_fixed_vec(
|
|
1345
|
+
row_idx: int,
|
|
1346
|
+
out_buf: bytearray,
|
|
1347
|
+
_mv: memoryview = packed_mv,
|
|
1348
|
+
_stride: int = stride,
|
|
1349
|
+
_nulls: Any = null_mask,
|
|
1350
|
+
) -> None:
|
|
1351
|
+
if _nulls is not None and _nulls[row_idx]:
|
|
1352
|
+
out_buf.extend(_PG_NULL_MARKER)
|
|
1353
|
+
else:
|
|
1354
|
+
start = row_idx * _stride
|
|
1355
|
+
out_buf.extend(_mv[start : start + _stride])
|
|
1356
|
+
|
|
1357
|
+
return _emit_fixed_vec
|
|
1358
|
+
|
|
1359
|
+
return None
|
|
1360
|
+
|
|
1361
|
+
|
|
1362
|
+
def _create_pg_column_encoder(
|
|
1363
|
+
col: Any, col_name: str, num_rows: int, vector_columns: frozenset[str], jsonb_columns: frozenset[str]
|
|
1364
|
+
) -> Callable[[int, bytearray], None]:
|
|
1365
|
+
"""Create an encoder function for a single column."""
|
|
1366
|
+
import numpy as np
|
|
1367
|
+
import pyarrow as pa
|
|
1368
|
+
|
|
1369
|
+
if isinstance(col, pa.ChunkedArray):
|
|
1370
|
+
col = col.combine_chunks()
|
|
1371
|
+
field_type = col.type
|
|
1372
|
+
has_nulls = col.null_count > 0
|
|
1373
|
+
null_mask = col.is_null().to_numpy(zero_copy_only=False) if has_nulls else None
|
|
1374
|
+
|
|
1375
|
+
if col_name not in jsonb_columns:
|
|
1376
|
+
fixed_encoder = _create_pg_fixed_column_encoder(col, field_type, num_rows, null_mask)
|
|
1377
|
+
if fixed_encoder is not None:
|
|
1378
|
+
return fixed_encoder
|
|
1379
|
+
|
|
1380
|
+
if (
|
|
1381
|
+
pa.types.is_string(field_type)
|
|
1382
|
+
or pa.types.is_large_string(field_type)
|
|
1383
|
+
or pa.types.is_binary(field_type)
|
|
1384
|
+
or pa.types.is_large_binary(field_type)
|
|
1385
|
+
):
|
|
1386
|
+
offset_dtype = (
|
|
1387
|
+
np.int64 if (pa.types.is_large_string(field_type) or pa.types.is_large_binary(field_type)) else np.int32
|
|
1388
|
+
)
|
|
1389
|
+
offsets = np.frombuffer(col.buffers()[1], dtype=offset_dtype, count=col.offset + num_rows + 1)[col.offset :]
|
|
1390
|
+
data_buf = col.buffers()[2]
|
|
1391
|
+
data_mv = memoryview(data_buf) if data_buf is not None else memoryview(b"")
|
|
1392
|
+
is_jsonb = col_name in jsonb_columns
|
|
1393
|
+
diffs = np.diff(offsets)
|
|
1394
|
+
lengths_mv = memoryview((diffs + (1 if is_jsonb else 0)).astype(">i4").tobytes())
|
|
1395
|
+
|
|
1396
|
+
def _emit_varlen(
|
|
1397
|
+
row_idx: int,
|
|
1398
|
+
out_buf: bytearray,
|
|
1399
|
+
_offsets: Any = offsets,
|
|
1400
|
+
_data_mv: memoryview = data_mv,
|
|
1401
|
+
_len_mv: memoryview = lengths_mv,
|
|
1402
|
+
_nulls: Any = null_mask,
|
|
1403
|
+
_is_jsonb: bool = is_jsonb,
|
|
1404
|
+
) -> None:
|
|
1405
|
+
if _nulls is not None and _nulls[row_idx]:
|
|
1406
|
+
out_buf.extend(_PG_NULL_MARKER)
|
|
1407
|
+
return
|
|
1408
|
+
out_buf.extend(_len_mv[row_idx * 4 : (row_idx + 1) * 4])
|
|
1409
|
+
if _is_jsonb:
|
|
1410
|
+
out_buf.append(1)
|
|
1411
|
+
start = int(_offsets[row_idx])
|
|
1412
|
+
end = int(_offsets[row_idx + 1])
|
|
1413
|
+
if end > start:
|
|
1414
|
+
out_buf.extend(_data_mv[start:end])
|
|
1415
|
+
|
|
1416
|
+
return _emit_varlen
|
|
1417
|
+
|
|
1418
|
+
if (pa.types.is_list(field_type) or pa.types.is_large_list(field_type)) and (
|
|
1419
|
+
col_name in vector_columns or pa.types.is_floating(field_type.value_type)
|
|
1420
|
+
):
|
|
1421
|
+
offset_dtype = np.int64 if pa.types.is_large_list(field_type) else np.int32
|
|
1422
|
+
offsets = np.frombuffer(col.buffers()[1], dtype=offset_dtype, count=col.offset + num_rows + 1)[col.offset :]
|
|
1423
|
+
is_f16 = pa.types.is_float16(field_type.value_type)
|
|
1424
|
+
elem_dtype = ">f2" if is_f16 else ">f4"
|
|
1425
|
+
elem_size = 2 if is_f16 else 4
|
|
1426
|
+
values_attr = "values"
|
|
1427
|
+
flat_bytes = getattr(col, values_attr).to_numpy(zero_copy_only=False).astype(elem_dtype, copy=False).tobytes()
|
|
1428
|
+
flat_mv = memoryview(flat_bytes)
|
|
1429
|
+
dims = np.diff(offsets)
|
|
1430
|
+
headers = np.empty(num_rows, dtype=np.dtype([("len", ">i4"), ("dim", ">u2"), ("unused", ">u2")]))
|
|
1431
|
+
headers["len"] = 4 + elem_size * dims
|
|
1432
|
+
headers["dim"] = dims
|
|
1433
|
+
headers["unused"] = 0
|
|
1434
|
+
headers_mv = memoryview(headers.tobytes())
|
|
1435
|
+
|
|
1436
|
+
def _emit_var_vec(
|
|
1437
|
+
row_idx: int,
|
|
1438
|
+
out_buf: bytearray,
|
|
1439
|
+
_offsets: Any = offsets,
|
|
1440
|
+
_flat_mv: memoryview = flat_mv,
|
|
1441
|
+
_hdr_mv: memoryview = headers_mv,
|
|
1442
|
+
_elem_size: int = elem_size,
|
|
1443
|
+
_nulls: Any = null_mask,
|
|
1444
|
+
) -> None:
|
|
1445
|
+
if _nulls is not None and _nulls[row_idx]:
|
|
1446
|
+
out_buf.extend(_PG_NULL_MARKER)
|
|
1447
|
+
return
|
|
1448
|
+
out_buf.extend(_hdr_mv[row_idx * 8 : (row_idx + 1) * 8])
|
|
1449
|
+
start = int(_offsets[row_idx]) * _elem_size
|
|
1450
|
+
end = int(_offsets[row_idx + 1]) * _elem_size
|
|
1451
|
+
if end > start:
|
|
1452
|
+
out_buf.extend(_flat_mv[start:end])
|
|
1453
|
+
|
|
1454
|
+
return _emit_var_vec
|
|
1455
|
+
|
|
1456
|
+
py_values = col.to_pylist()
|
|
1457
|
+
is_jsonb = col_name in jsonb_columns
|
|
1458
|
+
|
|
1459
|
+
def _emit_fallback(
|
|
1460
|
+
row_idx: int, out_buf: bytearray, _vals: list[Any] = py_values, _is_jsonb: bool = is_jsonb
|
|
1461
|
+
) -> None:
|
|
1462
|
+
val = _vals[row_idx]
|
|
1463
|
+
if val is None:
|
|
1464
|
+
out_buf.extend(_PG_NULL_MARKER)
|
|
1465
|
+
return
|
|
1466
|
+
if isinstance(val, (bytes, bytearray, memoryview)):
|
|
1467
|
+
raw = bytes(val)
|
|
1468
|
+
elif isinstance(val, str):
|
|
1469
|
+
raw = val.encode("utf-8")
|
|
1470
|
+
elif isinstance(val, (dict, list, tuple)):
|
|
1471
|
+
raw = _encode_json_bytes(val)
|
|
1472
|
+
else:
|
|
1473
|
+
raw = str(val).encode("utf-8")
|
|
1474
|
+
if _is_jsonb:
|
|
1475
|
+
out_buf.extend(struct.pack(">iB", len(raw) + 1, 1))
|
|
1476
|
+
else:
|
|
1477
|
+
out_buf.extend(struct.pack(">i", len(raw)))
|
|
1478
|
+
out_buf.extend(raw)
|
|
1479
|
+
|
|
1480
|
+
return _emit_fallback
|
|
1481
|
+
|
|
1482
|
+
|
|
1483
|
+
def _encode_json_bytes(val: Any) -> bytes:
|
|
1484
|
+
"""Encode value to JSON bytes using msgspec or stdlib json without circular imports."""
|
|
1485
|
+
encoder = import_optional_attr("sqlspec.utils.serializers._json", "to_json")
|
|
1486
|
+
if encoder is not None:
|
|
1487
|
+
return cast("bytes", encoder(val, as_bytes=True))
|
|
1488
|
+
import json
|
|
1489
|
+
|
|
1490
|
+
return json.dumps(val).encode("utf-8")
|
|
1491
|
+
|
|
1492
|
+
|
|
1493
|
+
def _encode_pg_binary_fast_path_2(
|
|
1494
|
+
batch: "ArrowRecordBatch", columns: "list[str]", vector_columns: frozenset[str], jsonb_columns: frozenset[str]
|
|
1495
|
+
) -> bytes:
|
|
1496
|
+
"""Encode variable-width and nullable columns by slicing Arrow buffers directly."""
|
|
1497
|
+
num_rows = int(batch.num_rows)
|
|
1498
|
+
row_header = struct.pack(">h", len(columns))
|
|
1499
|
+
col_encoders = [
|
|
1500
|
+
_create_pg_column_encoder(batch.column(col_name), col_name, num_rows, vector_columns, jsonb_columns)
|
|
1501
|
+
for col_name in columns
|
|
1502
|
+
]
|
|
1503
|
+
|
|
1504
|
+
out_buf = bytearray()
|
|
1505
|
+
for row_idx in range(num_rows):
|
|
1506
|
+
out_buf.extend(row_header)
|
|
1507
|
+
for encoder in col_encoders:
|
|
1508
|
+
encoder(row_idx, out_buf)
|
|
1509
|
+
return bytes(out_buf)
|
|
1510
|
+
|
|
1511
|
+
|
|
1512
|
+
@lru_cache(maxsize=_ARROW_SCHEMA_DECISION_CACHE_SIZE)
|
|
1513
|
+
def _arrow_schema_has_opaque_uuid(schema: Any) -> bool:
|
|
1514
|
+
return any(
|
|
1515
|
+
_arrow_type_is_opaque_uuid(field.type) or _arrow_type_is_list_of_opaque_uuid(field.type) for field in schema
|
|
1516
|
+
)
|
|
1517
|
+
|
|
1518
|
+
|
|
1519
|
+
def _resolve_dialect_config(dialect: str | None) -> "DialectConfig | None":
|
|
1520
|
+
if not dialect:
|
|
1521
|
+
return None
|
|
1522
|
+
get_dialect_config = import_optional_attr("sqlspec.data_dictionary._registry", "get_dialect_config")
|
|
1523
|
+
with contextlib.suppress(ValueError):
|
|
1524
|
+
return cast("DialectConfig", get_dialect_config(str(dialect).strip()))
|
|
1525
|
+
return None
|
|
1526
|
+
|
|
1527
|
+
|
|
1528
|
+
def _arrow_type_category(pa: Any, data_type: Any) -> str:
|
|
1529
|
+
"""Map a non-decimal, non-timestamp PyArrow DataType to a neutral SQL type category."""
|
|
1530
|
+
types = pa.types
|
|
1531
|
+
if types.is_boolean(data_type):
|
|
1532
|
+
return "boolean"
|
|
1533
|
+
if types.is_int8(data_type) or types.is_int16(data_type) or types.is_uint8(data_type):
|
|
1534
|
+
return "smallint"
|
|
1535
|
+
if types.is_int32(data_type) or types.is_uint16(data_type):
|
|
1536
|
+
return "integer"
|
|
1537
|
+
if types.is_int64(data_type) or types.is_uint32(data_type) or types.is_uint64(data_type):
|
|
1538
|
+
return "bigint"
|
|
1539
|
+
if types.is_float16(data_type) or types.is_float32(data_type):
|
|
1540
|
+
return "real"
|
|
1541
|
+
if types.is_float64(data_type):
|
|
1542
|
+
return "double"
|
|
1543
|
+
if types.is_date(data_type):
|
|
1544
|
+
return "date"
|
|
1545
|
+
if types.is_time(data_type):
|
|
1546
|
+
return "time"
|
|
1547
|
+
if types.is_duration(data_type) or types.is_interval(data_type):
|
|
1548
|
+
return "interval"
|
|
1549
|
+
if (
|
|
1550
|
+
types.is_binary(data_type)
|
|
1551
|
+
or types.is_large_binary(data_type)
|
|
1552
|
+
or types.is_fixed_size_binary(data_type)
|
|
1553
|
+
or getattr(types, "is_binary_view", lambda _: False)(data_type)
|
|
1554
|
+
):
|
|
1555
|
+
return "blob"
|
|
1556
|
+
if (
|
|
1557
|
+
types.is_list(data_type)
|
|
1558
|
+
or types.is_large_list(data_type)
|
|
1559
|
+
or types.is_fixed_size_list(data_type)
|
|
1560
|
+
or types.is_struct(data_type)
|
|
1561
|
+
or types.is_map(data_type)
|
|
1562
|
+
or getattr(types, "is_list_view", lambda _: False)(data_type)
|
|
1563
|
+
or getattr(types, "is_large_list_view", lambda _: False)(data_type)
|
|
1564
|
+
):
|
|
1565
|
+
return "json"
|
|
1566
|
+
return "text"
|
|
1567
|
+
|
|
1568
|
+
|
|
1569
|
+
def _decimal_sql_type(dialect_name: str | None, precision: int | None, scale: int | None) -> str:
|
|
1570
|
+
"""Render an exact decimal type, refusing precisions the dialect cannot store exactly."""
|
|
1571
|
+
resolved_precision = _DEFAULT_DECIMAL_PRECISION if precision is None else int(precision)
|
|
1572
|
+
resolved_scale = _DEFAULT_DECIMAL_SCALE if scale is None else int(scale)
|
|
1573
|
+
if dialect_name == "spanner":
|
|
1574
|
+
if (
|
|
1575
|
+
resolved_scale > _SPANNER_NUMERIC_SCALE
|
|
1576
|
+
or resolved_precision - resolved_scale > _SPANNER_NUMERIC_INTEGER_DIGITS
|
|
1577
|
+
):
|
|
1578
|
+
msg = (
|
|
1579
|
+
f"DECIMAL({resolved_precision}, {resolved_scale}) does not fit Spanner NUMERIC "
|
|
1580
|
+
f"(at most {_SPANNER_NUMERIC_INTEGER_DIGITS} integer digits and {_SPANNER_NUMERIC_SCALE} fractional digits)"
|
|
1581
|
+
)
|
|
1582
|
+
raise SQLBuilderError(msg)
|
|
1583
|
+
return "NUMERIC"
|
|
1584
|
+
if dialect_name == "bigquery":
|
|
1585
|
+
if resolved_precision <= _BIGQUERY_NUMERIC_MAX_PRECISION and resolved_scale <= _BIGQUERY_NUMERIC_MAX_SCALE:
|
|
1586
|
+
return f"NUMERIC({resolved_precision}, {resolved_scale})"
|
|
1587
|
+
if resolved_precision <= _BIGQUERY_BIGNUMERIC_MAX_PRECISION:
|
|
1588
|
+
return f"BIGNUMERIC({resolved_precision}, {resolved_scale})"
|
|
1589
|
+
max_precision = _DECIMAL_MAX_PRECISION.get(dialect_name or "")
|
|
1590
|
+
if max_precision is not None and resolved_precision > max_precision:
|
|
1591
|
+
msg = (
|
|
1592
|
+
f"DECIMAL({resolved_precision}, {resolved_scale}) exceeds the maximum precision {max_precision} "
|
|
1593
|
+
f"supported by {dialect_name}; cast the column to a narrower decimal or a text type explicitly"
|
|
1594
|
+
)
|
|
1595
|
+
raise SQLBuilderError(msg)
|
|
1596
|
+
return f"DECIMAL({resolved_precision}, {resolved_scale})"
|
|
1597
|
+
|
|
1598
|
+
|
|
1599
|
+
def _arrow_type_is_uuid(data_type: Any) -> bool:
|
|
1600
|
+
if _arrow_type_is_opaque_uuid(data_type):
|
|
1601
|
+
return True
|
|
1602
|
+
ext_name = getattr(data_type, "extension_name", None)
|
|
1603
|
+
return isinstance(ext_name, str) and "uuid" in ext_name.casefold()
|
|
1604
|
+
|
|
1605
|
+
|
|
1606
|
+
def _arrow_type_is_opaque_uuid(data_type: Any) -> bool:
|
|
1607
|
+
return (
|
|
1608
|
+
getattr(data_type, "extension_name", None) == "arrow.opaque"
|
|
1609
|
+
and getattr(data_type, "type_name", "").casefold() == "uuid"
|
|
1610
|
+
)
|
|
1611
|
+
|
|
1612
|
+
|
|
1613
|
+
def _arrow_type_is_list_of_opaque_uuid(data_type: Any) -> bool:
|
|
1614
|
+
ensure_pyarrow()
|
|
1615
|
+
import pyarrow as pa
|
|
1616
|
+
|
|
1617
|
+
return pa.types.is_list(data_type) and _arrow_type_is_opaque_uuid(data_type.value_type)
|
|
1618
|
+
|
|
1619
|
+
|
|
1620
|
+
def _arrow_uuid_column_to_pylist(column: Any, data_type: Any) -> "list[Any]":
|
|
1621
|
+
if _arrow_type_is_opaque_uuid(data_type):
|
|
1622
|
+
ensure_pyarrow()
|
|
1623
|
+
import pyarrow as pa
|
|
1624
|
+
|
|
1625
|
+
array = cast("Any", column.combine_chunks() if isinstance(column, pa.ChunkedArray) else column)
|
|
1626
|
+
storage_values = cast("list[Any]", array.storage.to_pylist())
|
|
1627
|
+
return [uuid_from_bytes(value) if value is not None else None for value in storage_values]
|
|
1628
|
+
if _arrow_type_is_list_of_opaque_uuid(data_type):
|
|
1629
|
+
nested_values = cast("list[Any]", column.to_pylist())
|
|
1630
|
+
return [
|
|
1631
|
+
[uuid_from_bytes(item) if item is not None else None for item in value] if value is not None else None
|
|
1632
|
+
for value in nested_values
|
|
1633
|
+
]
|
|
1634
|
+
return cast("list[Any]", column.to_pylist())
|
|
1635
|
+
|
|
1636
|
+
|
|
1637
|
+
def _resolve_null_columns(table: "ArrowTable", column_types: "Mapping[str, str] | None" = None) -> "ArrowTable":
|
|
1638
|
+
"""Give value-inferred Arrow ``null`` columns their real type.
|
|
1639
|
+
|
|
1640
|
+
``pa.Table.from_pylist`` infers column types from values alone, so a column
|
|
1641
|
+
that is ``NULL`` in every row arrives as Arrow ``null`` and loses the type
|
|
1642
|
+
the database declared. When the adapter reported that column's type, use it.
|
|
1643
|
+
Otherwise fall back to ``string``, which holds the nulls without breaking
|
|
1644
|
+
string operations downstream.
|
|
1645
|
+
"""
|
|
1646
|
+
import pyarrow as pa
|
|
1647
|
+
|
|
1648
|
+
if not any(pa.types.is_null(field.type) for field in table.schema):
|
|
1649
|
+
return table
|
|
1650
|
+
|
|
1651
|
+
hints = column_types or {}
|
|
1652
|
+
fields = [
|
|
1653
|
+
field.with_type(arrow_type_from_token(hints[field.name]))
|
|
1654
|
+
if pa.types.is_null(field.type) and field.name in hints
|
|
1655
|
+
else (field.with_type(pa.string()) if pa.types.is_null(field.type) else field)
|
|
1656
|
+
for field in table.schema
|
|
1657
|
+
]
|
|
1658
|
+
return table.cast(pa.schema(fields))
|
|
1659
|
+
|
|
1660
|
+
|
|
1661
|
+
def _parse_decimal_token(token: str) -> "tuple[int, int] | None":
|
|
1662
|
+
match = _DECIMAL_TOKEN_PATTERN.match(token)
|
|
1663
|
+
if match is None:
|
|
1664
|
+
return None
|
|
1665
|
+
precision = int(match.group(1))
|
|
1666
|
+
scale = int(match.group(2))
|
|
1667
|
+
if precision < 1 or precision > _DECIMAL256_MAX_PRECISION or scale > precision:
|
|
1668
|
+
return None
|
|
1669
|
+
return precision, scale
|
|
1670
|
+
|
|
1671
|
+
|
|
1672
|
+
def _declared_types(probe_schema: Any, column_types: "Mapping[str, str] | None") -> "dict[str, Any]":
|
|
1673
|
+
if not column_types:
|
|
1674
|
+
return {}
|
|
1675
|
+
import pyarrow as pa
|
|
1676
|
+
|
|
1677
|
+
declared: dict[str, Any] = {}
|
|
1678
|
+
for field in probe_schema:
|
|
1679
|
+
token = column_types.get(field.name)
|
|
1680
|
+
if token is None:
|
|
1681
|
+
continue
|
|
1682
|
+
if pa.types.is_decimal(field.type):
|
|
1683
|
+
if _parse_decimal_token(token) is not None:
|
|
1684
|
+
declared[field.name] = arrow_type_from_token(token)
|
|
1685
|
+
elif pa.types.is_null(field.type) and (
|
|
1686
|
+
token in _PINNABLE_NULL_TOKENS or _parse_decimal_token(token) is not None
|
|
1687
|
+
):
|
|
1688
|
+
declared[field.name] = arrow_type_from_token(token)
|
|
1689
|
+
return declared
|
|
1690
|
+
|
|
1691
|
+
|
|
1692
|
+
def _is_any_list_type(pa: Any, data_type: Any) -> bool:
|
|
1693
|
+
types = pa.types
|
|
1694
|
+
return bool(
|
|
1695
|
+
types.is_list(data_type)
|
|
1696
|
+
or types.is_large_list(data_type)
|
|
1697
|
+
or types.is_fixed_size_list(data_type)
|
|
1698
|
+
or getattr(types, "is_list_view", lambda _: False)(data_type)
|
|
1699
|
+
or getattr(types, "is_large_list_view", lambda _: False)(data_type)
|
|
1700
|
+
)
|
|
1701
|
+
|
|
1702
|
+
|
|
1703
|
+
def _type_is_unsettled(data_type: Any, *, integer_may_widen: bool = False, _in_list: bool = False) -> bool:
|
|
1704
|
+
import pyarrow as pa
|
|
1705
|
+
|
|
1706
|
+
types = pa.types
|
|
1707
|
+
if types.is_null(data_type) or types.is_decimal(data_type) or types.is_struct(data_type) or types.is_map(data_type):
|
|
1708
|
+
return True
|
|
1709
|
+
if types.is_integer(data_type):
|
|
1710
|
+
return _in_list or integer_may_widen
|
|
1711
|
+
if _is_any_list_type(pa, data_type):
|
|
1712
|
+
return _type_is_unsettled(data_type.value_type, integer_may_widen=integer_may_widen, _in_list=True)
|
|
1713
|
+
return False
|
|
1714
|
+
|
|
1715
|
+
|
|
1716
|
+
def _struct_loses_fields(pa: Any, found_type: Any, target_type: Any) -> bool:
|
|
1717
|
+
types = pa.types
|
|
1718
|
+
if types.is_struct(found_type) and types.is_struct(target_type):
|
|
1719
|
+
target_fields = {child.name: child.type for child in target_type}
|
|
1720
|
+
for child in found_type:
|
|
1721
|
+
if child.name not in target_fields:
|
|
1722
|
+
return True
|
|
1723
|
+
if _struct_loses_fields(pa, child.type, target_fields[child.name]):
|
|
1724
|
+
return True
|
|
1725
|
+
return False
|
|
1726
|
+
if _is_any_list_type(pa, found_type) and _is_any_list_type(pa, target_type):
|
|
1727
|
+
return _struct_loses_fields(pa, found_type.value_type, target_type.value_type)
|
|
1728
|
+
if types.is_map(found_type) and types.is_map(target_type):
|
|
1729
|
+
return _struct_loses_fields(pa, found_type.key_type, target_type.key_type) or _struct_loses_fields(
|
|
1730
|
+
pa, found_type.item_type, target_type.item_type
|
|
1731
|
+
)
|
|
1732
|
+
return False
|
|
1733
|
+
|
|
1734
|
+
|
|
1735
|
+
def _reject_lost_struct_fields(inferred_schema: Any, target_schema: Any) -> None:
|
|
1736
|
+
import pyarrow as pa
|
|
1737
|
+
|
|
1738
|
+
target_fields = {field.name: field.type for field in target_schema}
|
|
1739
|
+
for field in inferred_schema:
|
|
1740
|
+
target_type = target_fields.get(field.name)
|
|
1741
|
+
if target_type is not None and _struct_loses_fields(pa, field.type, target_type):
|
|
1742
|
+
msg = f"Column {field.name!r} changed type from {target_type} to {field.type} during streaming"
|
|
1743
|
+
raise pa.ArrowTypeError(msg)
|
|
1744
|
+
|
|
1745
|
+
|
|
1746
|
+
def _locked_type_family(pa: Any, data_type: Any) -> str | None:
|
|
1747
|
+
types = pa.types
|
|
1748
|
+
if types.is_integer(data_type):
|
|
1749
|
+
return "integer"
|
|
1750
|
+
if types.is_floating(data_type):
|
|
1751
|
+
return "floating"
|
|
1752
|
+
if (
|
|
1753
|
+
types.is_string(data_type)
|
|
1754
|
+
or types.is_large_string(data_type)
|
|
1755
|
+
or getattr(types, "is_string_view", lambda _: False)(data_type)
|
|
1756
|
+
):
|
|
1757
|
+
return "string"
|
|
1758
|
+
if (
|
|
1759
|
+
types.is_binary(data_type)
|
|
1760
|
+
or types.is_large_binary(data_type)
|
|
1761
|
+
or types.is_fixed_size_binary(data_type)
|
|
1762
|
+
or getattr(types, "is_binary_view", lambda _: False)(data_type)
|
|
1763
|
+
):
|
|
1764
|
+
return "binary"
|
|
1765
|
+
if types.is_boolean(data_type):
|
|
1766
|
+
return "boolean"
|
|
1767
|
+
if types.is_temporal(data_type):
|
|
1768
|
+
return "temporal"
|
|
1769
|
+
if types.is_decimal(data_type):
|
|
1770
|
+
return "decimal"
|
|
1771
|
+
return None
|
|
1772
|
+
|
|
1773
|
+
|
|
1774
|
+
def _locked_type_accepts(pa: Any, target_type: Any, found_type: Any) -> bool:
|
|
1775
|
+
if pa.types.is_null(found_type) or target_type == found_type:
|
|
1776
|
+
return True
|
|
1777
|
+
if _is_any_list_type(pa, target_type) and _is_any_list_type(pa, found_type):
|
|
1778
|
+
return _locked_type_accepts(pa, target_type.value_type, found_type.value_type)
|
|
1779
|
+
target_family = _locked_type_family(pa, target_type)
|
|
1780
|
+
found_family = _locked_type_family(pa, found_type)
|
|
1781
|
+
if target_family is not None and target_family == found_family:
|
|
1782
|
+
return True
|
|
1783
|
+
return target_family == "floating" and found_family == "integer"
|
|
1784
|
+
|
|
1785
|
+
|
|
1786
|
+
def _convert_later_chunk(chunk: "list[dict[str, Any]]", schema: Any, *, explicit: bool) -> "ArrowTable":
|
|
1787
|
+
import pyarrow as pa
|
|
1788
|
+
|
|
1789
|
+
inferred = pa.Table.from_pylist(chunk)
|
|
1790
|
+
_reject_lost_struct_fields(inferred.schema, schema)
|
|
1791
|
+
if not explicit:
|
|
1792
|
+
target_fields = {field.name: field.type for field in schema}
|
|
1793
|
+
for field in inferred.schema:
|
|
1794
|
+
if field.name not in target_fields:
|
|
1795
|
+
msg = f"Column {field.name!r} appeared after schema was pinned during streaming"
|
|
1796
|
+
raise pa.ArrowTypeError(msg)
|
|
1797
|
+
target_type = target_fields[field.name]
|
|
1798
|
+
if not _locked_type_accepts(pa, target_type, field.type):
|
|
1799
|
+
msg = f"Column {field.name!r} changed type from {target_type} to {field.type} during streaming"
|
|
1800
|
+
raise pa.ArrowTypeError(msg)
|
|
1801
|
+
if inferred.column_names != schema.names and set(inferred.column_names) <= set(schema.names):
|
|
1802
|
+
present = set(inferred.column_names)
|
|
1803
|
+
columns = [
|
|
1804
|
+
inferred.column(field.name) if field.name in present else pa.nulls(inferred.num_rows, type=field.type)
|
|
1805
|
+
for field in schema
|
|
1806
|
+
]
|
|
1807
|
+
inferred = pa.Table.from_arrays(
|
|
1808
|
+
columns,
|
|
1809
|
+
schema=pa.schema([
|
|
1810
|
+
inferred.schema.field(field.name) if field.name in present else field for field in schema
|
|
1811
|
+
]),
|
|
1812
|
+
)
|
|
1813
|
+
return inferred.cast(schema)
|
|
1814
|
+
|
|
1815
|
+
|
|
1816
|
+
def _close_sync_rows(rows: Any) -> None:
|
|
1817
|
+
close = getattr(rows, "close", None)
|
|
1818
|
+
if callable(close):
|
|
1819
|
+
close()
|
|
1820
|
+
|
|
1821
|
+
|
|
1822
|
+
async def _aclose_async_rows(rows: Any) -> None:
|
|
1823
|
+
aclose = getattr(rows, "aclose", None)
|
|
1824
|
+
if callable(aclose):
|
|
1825
|
+
await cast("Callable[[], Awaitable[Any]]", aclose)()
|
|
1826
|
+
|
|
1827
|
+
|
|
1828
|
+
async def _read_async_chunk(iterator: "AsyncIterator[dict[str, Any]]", batch_size: int) -> "list[dict[str, Any]]":
|
|
1829
|
+
chunk: list[dict[str, Any]] = []
|
|
1830
|
+
for _ in range(batch_size):
|
|
1831
|
+
try:
|
|
1832
|
+
chunk.append(await anext(iterator))
|
|
1833
|
+
except StopAsyncIteration:
|
|
1834
|
+
break
|
|
1835
|
+
return chunk
|
|
1836
|
+
|
|
1837
|
+
|
|
1838
|
+
class _AsyncRowBatchIterator:
|
|
1839
|
+
"""Async RecordBatch iterator ensuring the underlying row source closes on exhaustion or aclose."""
|
|
1840
|
+
|
|
1841
|
+
__slots__ = ("_closed", "_gen", "_rows")
|
|
1842
|
+
|
|
1843
|
+
def __init__(self, gen: "AsyncIterator[ArrowRecordBatch]", rows: Any) -> None:
|
|
1844
|
+
self._gen = gen
|
|
1845
|
+
self._rows = rows
|
|
1846
|
+
self._closed = False
|
|
1847
|
+
|
|
1848
|
+
def __aiter__(self) -> "_AsyncRowBatchIterator":
|
|
1849
|
+
return self
|
|
1850
|
+
|
|
1851
|
+
async def __anext__(self) -> "ArrowRecordBatch":
|
|
1852
|
+
try:
|
|
1853
|
+
return await self._gen.__anext__()
|
|
1854
|
+
except StopAsyncIteration:
|
|
1855
|
+
await self._finalize()
|
|
1856
|
+
raise
|
|
1857
|
+
except BaseException:
|
|
1858
|
+
await self._finalize()
|
|
1859
|
+
raise
|
|
1860
|
+
|
|
1861
|
+
async def aclose(self) -> None:
|
|
1862
|
+
gen_aclose = getattr(self._gen, "aclose", None)
|
|
1863
|
+
try:
|
|
1864
|
+
if callable(gen_aclose):
|
|
1865
|
+
await cast("Callable[[], Awaitable[Any]]", gen_aclose)()
|
|
1866
|
+
finally:
|
|
1867
|
+
await self._finalize()
|
|
1868
|
+
|
|
1869
|
+
async def _finalize(self) -> None:
|
|
1870
|
+
if not self._closed:
|
|
1871
|
+
self._closed = True
|
|
1872
|
+
await _aclose_async_rows(self._rows)
|
|
1873
|
+
|
|
1874
|
+
|
|
1875
|
+
def _coerce_arrow_table_identity(source: Any) -> Any:
|
|
1876
|
+
return source
|
|
1877
|
+
|
|
1878
|
+
|
|
1879
|
+
def _coerce_arrow_record_batch(source: Any) -> Any:
|
|
1880
|
+
import pyarrow as pa
|
|
1881
|
+
|
|
1882
|
+
return pa.Table.from_batches([source])
|
|
1883
|
+
|
|
1884
|
+
|
|
1885
|
+
def _coerce_arrow_record_batch_reader(source: Any) -> Any:
|
|
1886
|
+
return source.read_all()
|
|
1887
|
+
|
|
1888
|
+
|
|
1889
|
+
def _get_arrow_table_coercer() -> "TypeDispatcher[Any]":
|
|
1890
|
+
global _ARROW_TABLE_COERCER
|
|
1891
|
+
if _ARROW_TABLE_COERCER is None:
|
|
1892
|
+
ensure_pyarrow()
|
|
1893
|
+
import pyarrow as pa
|
|
1894
|
+
|
|
1895
|
+
dispatcher = TypeDispatcher[Any]()
|
|
1896
|
+
dispatcher.register(pa.Table, _coerce_arrow_table_identity)
|
|
1897
|
+
dispatcher.register(pa.RecordBatch, _coerce_arrow_record_batch)
|
|
1898
|
+
dispatcher.register(pa.RecordBatchReader, _coerce_arrow_record_batch_reader)
|
|
1899
|
+
_ARROW_TABLE_COERCER = dispatcher
|
|
1900
|
+
return _ARROW_TABLE_COERCER
|
|
1901
|
+
|
|
1902
|
+
|
|
1903
|
+
def _arrow_type_needs_preparation(data_type: Any) -> bool:
|
|
1904
|
+
ensure_pyarrow()
|
|
1905
|
+
import pyarrow as pa
|
|
1906
|
+
|
|
1907
|
+
if pa.types.is_dictionary(data_type):
|
|
1908
|
+
return _arrow_type_needs_preparation(data_type.value_type)
|
|
1909
|
+
|
|
1910
|
+
is_extension = getattr(pa.types, "is_extension", None)
|
|
1911
|
+
if is_extension is not None and is_extension(data_type):
|
|
1912
|
+
return _arrow_type_needs_preparation(data_type.storage_type)
|
|
1913
|
+
|
|
1914
|
+
nested_type_checks = (
|
|
1915
|
+
"is_struct",
|
|
1916
|
+
"is_list",
|
|
1917
|
+
"is_large_list",
|
|
1918
|
+
"is_fixed_size_list",
|
|
1919
|
+
"is_map",
|
|
1920
|
+
"is_union",
|
|
1921
|
+
"is_list_view",
|
|
1922
|
+
"is_large_list_view",
|
|
1923
|
+
)
|
|
1924
|
+
return any(getattr(pa.types, check, lambda _: False)(data_type) for check in nested_type_checks)
|
|
1925
|
+
|
|
1926
|
+
|
|
1927
|
+
@lru_cache(maxsize=_ARROW_SCHEMA_DECISION_CACHE_SIZE)
|
|
1928
|
+
def _arrow_schema_needs_preparation(schema: Any) -> bool:
|
|
1929
|
+
return any(_arrow_type_needs_preparation(field.type) for field in schema)
|
|
1930
|
+
|
|
1931
|
+
|
|
1932
|
+
class _DeferredCloseBatchIterator:
|
|
1933
|
+
"""Iterate RecordBatches from a reader and invoke a callback on exhaustion or error."""
|
|
1934
|
+
|
|
1935
|
+
__slots__ = ("_close_callback", "_closed", "_reader")
|
|
1936
|
+
|
|
1937
|
+
def __init__(self, reader: Any, close_callback: "Callable[[], None]") -> None:
|
|
1938
|
+
self._reader = reader
|
|
1939
|
+
self._close_callback = close_callback
|
|
1940
|
+
self._closed = False
|
|
1941
|
+
|
|
1942
|
+
def __del__(self) -> None:
|
|
1943
|
+
self._finalize()
|
|
1944
|
+
|
|
1945
|
+
def __iter__(self) -> "_DeferredCloseBatchIterator":
|
|
1946
|
+
return self
|
|
1947
|
+
|
|
1948
|
+
def __next__(self) -> Any:
|
|
1949
|
+
try:
|
|
1950
|
+
return self._reader.read_next_batch()
|
|
1951
|
+
except StopIteration:
|
|
1952
|
+
self._finalize()
|
|
1953
|
+
raise
|
|
1954
|
+
except Exception:
|
|
1955
|
+
self._finalize()
|
|
1956
|
+
raise
|
|
1957
|
+
|
|
1958
|
+
def close(self) -> None:
|
|
1959
|
+
self._finalize()
|
|
1960
|
+
|
|
1961
|
+
def _finalize(self) -> None:
|
|
1962
|
+
if not self._closed:
|
|
1963
|
+
self._closed = True
|
|
1964
|
+
with contextlib.suppress(Exception):
|
|
1965
|
+
self._close_callback()
|
|
1966
|
+
|
|
1967
|
+
|
|
1968
|
+
def _deferred_reader_init(self: Any, reader: Any, close_fn: "Callable[[], None] | None") -> None:
|
|
1969
|
+
self._reader = reader
|
|
1970
|
+
self._close_fn = close_fn
|
|
1971
|
+
|
|
1972
|
+
|
|
1973
|
+
def _deferred_reader_enter(self: Any) -> Any:
|
|
1974
|
+
return self
|
|
1975
|
+
|
|
1976
|
+
|
|
1977
|
+
def _deferred_reader_exit(self: Any, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
|
|
1978
|
+
_ = (exc_type, exc_val, exc_tb)
|
|
1979
|
+
self.close()
|
|
1980
|
+
|
|
1981
|
+
|
|
1982
|
+
def _deferred_reader_iter(self: Any) -> Any:
|
|
1983
|
+
return self
|
|
1984
|
+
|
|
1985
|
+
|
|
1986
|
+
def _deferred_reader_next(self: Any) -> Any:
|
|
1987
|
+
return self._reader.read_next_batch()
|
|
1988
|
+
|
|
1989
|
+
|
|
1990
|
+
def _deferred_reader_read_next_batch(self: Any) -> Any:
|
|
1991
|
+
return self._reader.read_next_batch()
|
|
1992
|
+
|
|
1993
|
+
|
|
1994
|
+
def _deferred_reader_read_next_batch_with_custom_metadata(self: Any) -> Any:
|
|
1995
|
+
return self._reader.read_next_batch_with_custom_metadata()
|
|
1996
|
+
|
|
1997
|
+
|
|
1998
|
+
def _deferred_reader_iter_batches_with_custom_metadata(self: Any) -> Any:
|
|
1999
|
+
return self._reader.iter_batches_with_custom_metadata()
|
|
2000
|
+
|
|
2001
|
+
|
|
2002
|
+
def _deferred_reader_read_all(self: Any) -> Any:
|
|
2003
|
+
try:
|
|
2004
|
+
return self._reader.read_all()
|
|
2005
|
+
finally:
|
|
2006
|
+
self.close()
|
|
2007
|
+
|
|
2008
|
+
|
|
2009
|
+
def _deferred_reader_read_pandas(self: Any, **options: Any) -> Any:
|
|
2010
|
+
try:
|
|
2011
|
+
return self._reader.read_pandas(**options)
|
|
2012
|
+
finally:
|
|
2013
|
+
self.close()
|
|
2014
|
+
|
|
2015
|
+
|
|
2016
|
+
def _deferred_reader_export_to_c(self: Any, out_ptr: int) -> None:
|
|
2017
|
+
self._reader._export_to_c(out_ptr)
|
|
2018
|
+
|
|
2019
|
+
|
|
2020
|
+
def _deferred_reader_arrow_c_stream(self: Any, requested_schema: Any = None) -> Any:
|
|
2021
|
+
return self._reader.__arrow_c_stream__(requested_schema=requested_schema)
|
|
2022
|
+
|
|
2023
|
+
|
|
2024
|
+
def _deferred_reader_cast(self: Any, target_schema: Any) -> Any:
|
|
2025
|
+
fn = self._close_fn
|
|
2026
|
+
casted = self._reader.cast(target_schema)
|
|
2027
|
+
self._close_fn = None
|
|
2028
|
+
return type(self)(casted, fn)
|
|
2029
|
+
|
|
2030
|
+
|
|
2031
|
+
def _deferred_reader_close(self: Any) -> None:
|
|
2032
|
+
reader = getattr(self, "_reader", None)
|
|
2033
|
+
try:
|
|
2034
|
+
reader_close = getattr(reader, "close", None)
|
|
2035
|
+
if callable(reader_close):
|
|
2036
|
+
with contextlib.suppress(Exception):
|
|
2037
|
+
reader_close()
|
|
2038
|
+
finally:
|
|
2039
|
+
fn = getattr(self, "_close_fn", None)
|
|
2040
|
+
if fn is not None:
|
|
2041
|
+
self._close_fn = None
|
|
2042
|
+
if callable(fn):
|
|
2043
|
+
with contextlib.suppress(Exception):
|
|
2044
|
+
fn()
|
|
2045
|
+
|
|
2046
|
+
|
|
2047
|
+
def _get_deferred_close_reader_cls() -> "type[Any]":
|
|
2048
|
+
global _DEFERRED_CLOSE_READER_CLS
|
|
2049
|
+
if _DEFERRED_CLOSE_READER_CLS is None:
|
|
2050
|
+
ensure_pyarrow()
|
|
2051
|
+
import pyarrow as pa
|
|
2052
|
+
|
|
2053
|
+
namespace: dict[str, Any] = {
|
|
2054
|
+
"__module__": __name__,
|
|
2055
|
+
"__slots__": ("_close_fn", "_reader"),
|
|
2056
|
+
"__init__": _deferred_reader_init,
|
|
2057
|
+
"__del__": _deferred_reader_close,
|
|
2058
|
+
"__enter__": _deferred_reader_enter,
|
|
2059
|
+
"__exit__": _deferred_reader_exit,
|
|
2060
|
+
"__iter__": _deferred_reader_iter,
|
|
2061
|
+
"__next__": _deferred_reader_next,
|
|
2062
|
+
"schema": property(lambda self: self._reader.schema),
|
|
2063
|
+
"read_next_batch": _deferred_reader_read_next_batch,
|
|
2064
|
+
"read_next_batch_with_custom_metadata": _deferred_reader_read_next_batch_with_custom_metadata,
|
|
2065
|
+
"iter_batches_with_custom_metadata": _deferred_reader_iter_batches_with_custom_metadata,
|
|
2066
|
+
"read_all": _deferred_reader_read_all,
|
|
2067
|
+
"read_pandas": _deferred_reader_read_pandas,
|
|
2068
|
+
"_export_to_c": _deferred_reader_export_to_c,
|
|
2069
|
+
"__arrow_c_stream__": _deferred_reader_arrow_c_stream,
|
|
2070
|
+
"cast": _deferred_reader_cast,
|
|
2071
|
+
"close": _deferred_reader_close,
|
|
2072
|
+
}
|
|
2073
|
+
_DEFERRED_CLOSE_READER_CLS = type("_DeferredCloseReader", (pa.RecordBatchReader,), namespace)
|
|
2074
|
+
return _DEFERRED_CLOSE_READER_CLS
|
|
2075
|
+
|
|
2076
|
+
|
|
2077
|
+
class _AsyncBytesIterator:
|
|
2078
|
+
"""Wrap a synchronous byte-chunk iterator as an async iterator."""
|
|
2079
|
+
|
|
2080
|
+
__slots__ = ("_iterator",)
|
|
2081
|
+
|
|
2082
|
+
def __init__(self, iterator: "Iterator[bytes]") -> None:
|
|
2083
|
+
self._iterator = iterator
|
|
2084
|
+
|
|
2085
|
+
def __aiter__(self) -> "_AsyncBytesIterator":
|
|
2086
|
+
return self
|
|
2087
|
+
|
|
2088
|
+
async def __anext__(self) -> bytes:
|
|
2089
|
+
try:
|
|
2090
|
+
return next(self._iterator)
|
|
2091
|
+
except StopIteration as exc:
|
|
2092
|
+
raise StopAsyncIteration from exc
|
|
2093
|
+
|
|
2094
|
+
|
|
2095
|
+
def oracle_df_to_arrow_table(oracle_df: Any, pa_module: Any = None, arrow_schema: Any = None) -> "ArrowTable":
|
|
2096
|
+
"""Convert an Oracle DataFrame to a PyArrow Table using the zero-copy Arrow C Stream interface when supported."""
|
|
2097
|
+
if pa_module is None:
|
|
2098
|
+
ensure_pyarrow()
|
|
2099
|
+
pa_module = import_optional("pyarrow")
|
|
2100
|
+
|
|
2101
|
+
if hasattr(oracle_df, "__arrow_c_stream__"):
|
|
2102
|
+
reader = (
|
|
2103
|
+
pa_module.RecordBatchReader.from_stream(oracle_df, schema=arrow_schema)
|
|
2104
|
+
if arrow_schema is not None
|
|
2105
|
+
else pa_module.RecordBatchReader.from_stream(oracle_df)
|
|
2106
|
+
)
|
|
2107
|
+
return cast("ArrowTable", reader.read_all())
|
|
2108
|
+
table = pa_module.table(oracle_df)
|
|
2109
|
+
if arrow_schema is not None:
|
|
2110
|
+
table = table.cast(arrow_schema)
|
|
2111
|
+
return cast("ArrowTable", table)
|
|
2112
|
+
|
|
2113
|
+
|
|
2114
|
+
def coerce_oracle_df_to_arrow_table(oracle_df: Any, pa_module: Any = None, arrow_schema: Any = None) -> "ArrowTable":
|
|
2115
|
+
"""Alias for :func:`oracle_df_to_arrow_table`."""
|
|
2116
|
+
return oracle_df_to_arrow_table(oracle_df, pa_module=pa_module, arrow_schema=arrow_schema)
|
|
2117
|
+
|
|
2118
|
+
|
|
2119
|
+
def coerce_oracle_df_to_arrow_batches(
|
|
2120
|
+
oracle_df: Any, pa_module: Any = None, arrow_schema: Any = None
|
|
2121
|
+
) -> "list[ArrowRecordBatch]":
|
|
2122
|
+
"""Convert an Oracle DataFrame batch into PyArrow RecordBatches via the Arrow C Stream interface when supported."""
|
|
2123
|
+
if pa_module is None:
|
|
2124
|
+
ensure_pyarrow()
|
|
2125
|
+
pa_module = import_optional("pyarrow")
|
|
2126
|
+
|
|
2127
|
+
if hasattr(oracle_df, "__arrow_c_stream__"):
|
|
2128
|
+
reader = (
|
|
2129
|
+
pa_module.RecordBatchReader.from_stream(oracle_df, schema=arrow_schema)
|
|
2130
|
+
if arrow_schema is not None
|
|
2131
|
+
else pa_module.RecordBatchReader.from_stream(oracle_df)
|
|
2132
|
+
)
|
|
2133
|
+
return cast("list[ArrowRecordBatch]", list(reader))
|
|
2134
|
+
table = pa_module.table(oracle_df)
|
|
2135
|
+
if arrow_schema is not None:
|
|
2136
|
+
table = table.cast(arrow_schema)
|
|
2137
|
+
return cast("list[ArrowRecordBatch]", table.to_batches())
|
|
2138
|
+
|
|
2139
|
+
|
|
2140
|
+
def oracle_df_batches_to_arrow_reader(
|
|
2141
|
+
batches: "Iterable[Any]", schema: Any = None, pa_module: Any = None
|
|
2142
|
+
) -> "ArrowRecordBatchReader":
|
|
2143
|
+
"""Convert an iterable of Oracle DataFrame batches into a PyArrow RecordBatchReader."""
|
|
2144
|
+
if pa_module is None:
|
|
2145
|
+
ensure_pyarrow()
|
|
2146
|
+
pa_module = import_optional("pyarrow")
|
|
2147
|
+
|
|
2148
|
+
collected_batches: list[Any] = []
|
|
2149
|
+
for df_batch in batches:
|
|
2150
|
+
collected_batches.extend(coerce_oracle_df_to_arrow_batches(df_batch, pa_module=pa_module, arrow_schema=schema))
|
|
2151
|
+
if collected_batches:
|
|
2152
|
+
resolved_schema = schema if schema is not None else collected_batches[0].schema
|
|
2153
|
+
return cast(
|
|
2154
|
+
"ArrowRecordBatchReader", pa_module.RecordBatchReader.from_batches(resolved_schema, collected_batches)
|
|
2155
|
+
)
|
|
2156
|
+
resolved_schema = schema if schema is not None else pa_module.schema([])
|
|
2157
|
+
return cast("ArrowRecordBatchReader", pa_module.RecordBatchReader.from_batches(resolved_schema, []))
|