aws / aws/amazon-redshift-python-driver
support `Cursor` attribute to provide ANSI SQL State Code
- 主要語言
- Python
- 星號
- 220
- 分支
- 86
- PR 合併指標
- 30 天內沒有已合併 PR
描述
While a `Cursor` attribute providing SQL State Code is not officially a part of [PEP 249: Python DB API 2.0 spec](https://peps.python.org/pep-0249/), there is an ANSI-standardized "SQL state code".
Many database drivers provide this as a `Cursor` attribute, dbt was able to depend on these drivers to provide it for `ConnectionManager.get_response()` method, which will report to users after successful queries the kind of operation performed (`SELECT`, `INSERT`, `CREATE`) and the numbers of rows affected. Originally the Redshift adapter for dbt, was supported by the `psycopg2` driver, which provides this information in `statusmessage`.
As reported in https://github.com/dbt-labs/dbt-redshift/issues/785, after migrating the driver dependency to `redshift-connector`, users are in a degraded state and receive less information than previously due to the SQL state not being available.
### Support for SQL state amongst popular analytics database drivers
| Driver | `Cursor` attribute (docs) |
|--------|--------|
| psycopg2 | [`statusmessage`](https://www.psycopg.org/docs/cursor.html#cursor.statusmessage) |
| `snowflake-connector-python` | [`sqlstate`](https://docs.snowflake.com/en/developer-guide/python-connector/python-connector-api#id7) |
### Ideal implementation
[Postgres's `CommandComplete` message](https://www.postgresql.org/docs/current/protocol-message-formats.html#PROTOCOL-MESSAGE-FORMATS-COMMANDCOMPLETE)
| Command | Tag | `rows` indicates the number of rows |
|------------------------|---------------------|-----------------------------------------------------------------------------|
| `INSERT` | `INSERT 0 rows` | inserted |
| `DELETE` | `DELETE rows` | deleted |
| `UPDATE` | `UPDATE rows` | updated |
| `MERGE` | `MERGE rows` | inserted, updated, or deleted |
| `SELECT` / `CREATE TABLE AS` | `SELECT rows` | retrieved |
| `MOVE` | `MOVE rows` | ursor's position has been changed by |
| `FETCH` | `FETCH rows` | that have been retrieved from the cursor |
| `COPY` | `COPY rows` | copied, only in `PostgreSQL` 8.2 and later |
貢獻指南
研究方向
該 issue 提到了 dbt 中的 ConnectionManager.get_response() 和 PostgreSQL 的 CommandComplete 訊息,但沒有提供 repository 檔案或測試。首先定位 cursor 的實作及其對已完成 command 中繼資料的處理,然後將可用 driver 的行為與列出的 statusmessage 和 sqlstate 屬性進行比較。當 cursor 暴露 SQL 狀態資訊,並且對於受支援的 command 保留受影響 operation 的詳細資訊時,即可視為完成。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python, sql
- 領域
- databases
- Issue 類型
- 功能
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 38/100