You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/sqlalchemy.md
+47-92Lines changed: 47 additions & 92 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1084,79 +1084,66 @@ CREATE TABLE orders (
1084
1084
)
1085
1085
```
1086
1086
1087
-
#### Querying ARRAY data
1087
+
#### Querying ARRAY data with typed SQLAlchemy expressions
1088
1088
1089
-
PyAthena automatically converts ARRAY data between different formats:
1089
+
Use `select()` with `ARRAY` or `AthenaArray` columns whose element types are known, either declared explicitly or reflected from Athena, to receive typed Python collections.
1090
+
For example, the `events` table above returns `numbers` as a list of integers and `labels` as nested lists of strings.
1091
+
SQLAlchemy serializes these result columns as JSON in Athena and converts the returned values according to the column types.
1092
+
You do not need to call `json.loads()` on these results.
1093
+
This preserves strings such as `"a,b"`, `"001"`, and `"null"` as strings, including within nested arrays.
1090
1094
1091
-
```python
1092
-
from sqlalchemy import text
1093
-
1094
-
# Query ARRAY data using ARRAY constructor
1095
-
result = connection.execute(
1096
-
text("SELECT ARRAY[1, 2, 3, 4, 5] as item_ids")
1097
-
).fetchone()
1098
-
1099
-
# Access ARRAY data as Python list
1100
-
item_ids = result.item_ids # [1, 2, 3, 4, 5]
1101
-
```
1095
+
#### Direct cursor and textual SQL results
1102
1096
1103
-
#### Complex ARRAY operations
1097
+
Direct `cursor.execute()` calls and untyped SQLAlchemy `text()` queries use the cursor's existing conversion behavior.
1098
+
The examples in this section use the standard REST cursor with its default converter and no result type hints or custom converters.
1099
+
Other cursor implementations have their own conversion behavior.
1104
1100
1105
-
For arrays containing complex data types:
1101
+
The standard converter already converts simple ARRAY values to Python lists:
1106
1102
1107
1103
```python
1108
-
# Arrays with STRUCT elements
1109
-
result = connection.execute(
1110
-
text("SELECT ARRAY[ROW('Alice', 25), ROW('Bob', 30)] as users")
1111
-
).fetchone()
1112
-
1113
-
users = result.users
1114
-
# Use typed SQLAlchemy expressions when you need declared ROW field types.
1115
-
1116
-
# Using CAST AS JSON for complex ARRAY operations
1117
-
result = connection.execute(
1118
-
text("SELECT CAST(ARRAY[1, 2, 3] AS JSON) as data")
1119
-
).fetchone()
1120
-
1121
-
# Parse JSON result
1122
-
import json
1123
-
ifisinstance(result.data, str):
1124
-
array_data = json.loads(result.data) # [1, 2, 3]
1125
-
else:
1126
-
array_data = result.data # Already converted to list
1104
+
result = cursor.execute("SELECT ARRAY[1, 2, 3] AS numbers").fetchone()
1105
+
numbers = result[0] # [1, 2, 3]
1127
1106
```
1128
1107
1129
-
#### Data format support
1130
-
1131
-
PyAthena supports multiple ARRAY data formats:
1132
-
1133
-
**Athena Native Format:**
1108
+
This conversion predates the typed SQLAlchemy ARRAY support described above.
1109
+
Typed SQLAlchemy ARRAY support does not change the behavior of direct cursor queries or untyped `text()` queries.
1134
1110
1135
-
```python
1136
-
# Input: '[1, 2, 3]'
1137
-
# Output: [1, 2, 3]
1111
+
The standard ARRAY converter first tries JSON parsing, then a limited parser for Athena's native text representation.
1112
+
The following examples show its behavior for strings received as ARRAY values:
"SELECT CAST(ARRAY[ARRAY['one', 'two'], ARRAY['three']] AS JSON) AS labels"
1139
+
).fetchone()
1140
+
labels = result[0] # [["one", "two"], ["three"]]
1158
1141
```
1159
1142
1143
+
The same applies to an untyped SQLAlchemy `text()` query using `awsathena+rest` with the default converter.
1144
+
No additional `json.loads()` call is needed in these examples.
1145
+
This path returns JSON-derived Python values; use typed SQLAlchemy ARRAY columns when you need conversion according to declared element types such as `Numeric` or `Date`.
PyAthena provides support for Amazon Athena's JSON data type, enabling you to work with JSON data in your SQLAlchemy applications. The JSON type is primarily used with Data Manipulation Language (DML) operations in Athena.
0 commit comments