|
8 | 8 |
|
9 | 9 | Snowflake SQLAlchemy runs on the top of the Snowflake Connector for Python as a [dialect](http://docs.sqlalchemy.org/en/latest/dialects/) to bridge a Snowflake database and SQLAlchemy applications. |
10 | 10 |
|
11 | | - |
| 11 | +Table of contents: |
| 12 | +<!-- TOC --> |
| 13 | +* [Snowflake SQLAlchemy](#snowflake-sqlalchemy) |
| 14 | + * [Prerequisites](#prerequisites) |
| 15 | + * [Snowflake Connector for Python](#snowflake-connector-for-python) |
| 16 | + * [Data Analytics and Web Application Frameworks (Optional)](#data-analytics-and-web-application-frameworks-optional) |
| 17 | + * [Installing Snowflake SQLAlchemy](#installing-snowflake-sqlalchemy) |
| 18 | + * [Verifying Your Installation](#verifying-your-installation) |
| 19 | + * [Parameters and Behavior](#parameters-and-behavior) |
| 20 | + * [Connection Parameters](#connection-parameters) |
| 21 | + * [Escaping Special Characters such as `%, @` signs in Passwords](#escaping-special-characters-such-as---signs-in-passwords) |
| 22 | + * [Using a proxy server](#using-a-proxy-server) |
| 23 | + * [Using session parameters](#using-session-parameters) |
| 24 | + * [Opening and Closing Connection](#opening-and-closing-connection) |
| 25 | + * [Auto-increment Behavior](#auto-increment-behavior) |
| 26 | + * [Object Name Case Handling](#object-name-case-handling) |
| 27 | + * [Index Support](#index-support) |
| 28 | + * [Single Column Index](#single-column-index) |
| 29 | + * [Multi-Column Index](#multi-column-index) |
| 30 | + * [Numpy Data Type Support](#numpy-data-type-support) |
| 31 | + * [DECFLOAT Data Type Support](#decfloat-data-type-support) |
| 32 | + * [DECFLOAT Precision](#decfloat-precision) |
| 33 | + * [VECTOR Data Type Support](#vector-data-type-support) |
| 34 | + * [Cache Column Metadata](#cache-column-metadata) |
| 35 | + * [VARIANT, ARRAY and OBJECT Support](#variant-array-and-object-support) |
| 36 | + * [Structured Data Types Support](#structured-data-types-support) |
| 37 | + * [MAP](#map) |
| 38 | + * [OBJECT](#object) |
| 39 | + * [ARRAY](#array) |
| 40 | + * [CLUSTER BY Support](#cluster-by-support) |
| 41 | + * [Alembic Support](#alembic-support) |
| 42 | + * [Key Pair Authentication Support](#key-pair-authentication-support) |
| 43 | + * [Merge Command Support](#merge-command-support) |
| 44 | + * [CopyIntoStorage Support](#copyintostorage-support) |
| 45 | + * [Iceberg Table with Snowflake Catalog support](#iceberg-table-with-snowflake-catalog-support) |
| 46 | + * [Hybrid Table support](#hybrid-table-support) |
| 47 | + * [Dynamic Tables support](#dynamic-tables-support) |
| 48 | + * [Notes](#notes) |
| 49 | + * [Verifying Package Signatures](#verifying-package-signatures) |
| 50 | + * [Support](#support) |
| 51 | +<!-- TOC --> |
12 | 52 |
|
13 | 53 | ## Prerequisites |
14 | 54 |
|
@@ -184,6 +224,44 @@ engine = create_engine(URL( |
184 | 224 |
|
185 | 225 | Use the supported environment variables, `HTTPS_PROXY`, `HTTP_PROXY` and `NO_PROXY` to configure a proxy server. |
186 | 226 |
|
| 227 | +#### Using session parameters |
| 228 | + |
| 229 | +Snowflake [session parameters](https://docs.snowflake.com/en/sql-reference/parameters#session-parameters) (such as [`QUERY_TAG`](https://docs.snowflake.com/en/sql-reference/parameters#query-tag)) cannot be set directly through the `URL` helper. |
| 230 | +Instead, pass them via the `connect_args` parameter of `create_engine`, using the `session_parameters` dict — the same way you would [through the Python connector](https://docs.snowflake.com/en/developer-guide/python-connector/python-connector-connect#setting-session-parameters): |
| 231 | + |
| 232 | +```python |
| 233 | +from snowflake.sqlalchemy import URL |
| 234 | +from sqlalchemy import create_engine |
| 235 | + |
| 236 | +engine = create_engine( |
| 237 | + URL( |
| 238 | + # CONNECTION_PARAMETERS |
| 239 | + ), |
| 240 | + connect_args={ |
| 241 | + "session_parameters": { |
| 242 | + "QUERY_TAG": "SOME_QUERY_TAGS", |
| 243 | + } |
| 244 | + }, |
| 245 | +) |
| 246 | +``` |
| 247 | + |
| 248 | +Session parameters set this way apply to all queries executed within the session. |
| 249 | +To change a session parameter for specific queries mid-session, use `ALTER SESSION`: |
| 250 | + |
| 251 | +```python |
| 252 | +from sqlalchemy import text |
| 253 | + |
| 254 | +with engine.connect() as conn: |
| 255 | + conn.execute(text("ALTER SESSION SET QUERY_TAG = 'batch_job_1'")) |
| 256 | + conn.execute(text("...")) # Uses 'batch_job_1' |
| 257 | + |
| 258 | + conn.execute(text("ALTER SESSION SET QUERY_TAG = 'batch_job_2'")) |
| 259 | + conn.execute(text("...")) # Uses 'batch_job_2' |
| 260 | + |
| 261 | + conn.execute(text("ALTER SESSION UNSET QUERY_TAG")) |
| 262 | + conn.execute(text("...")) # No tag |
| 263 | +``` |
| 264 | + |
187 | 265 | ### Opening and Closing Connection |
188 | 266 |
|
189 | 267 | Open a connection by executing `engine.connect()`; avoid using `engine.execute()`. Make certain to close the connection by executing `connection.close()` before |
|
0 commit comments