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
PyAthena provides comprehensive support for Amazon Athena's ARRAY data types, enabling you to work with ordered collections of data in your Python applications.
614
+
615
+
Basic Usage
616
+
^^^^^^^^^^^
617
+
618
+
.. code:: python
619
+
620
+
from sqlalchemy import Column, String, Integer, Table, MetaData
621
+
from pyathena.sqlalchemy.types import AthenaArray
622
+
623
+
# Define a table with ARRAY columns
624
+
orders = Table('orders', metadata,
625
+
Column('id', Integer),
626
+
Column('item_ids', AthenaArray(Integer)),
627
+
Column('tags', AthenaArray(String)),
628
+
Column('categories', AthenaArray(String))
629
+
)
630
+
631
+
This creates a table definition equivalent to:
632
+
633
+
.. code:: sql
634
+
635
+
CREATE TABLE orders (
636
+
id INTEGER,
637
+
item_ids ARRAY<INTEGER>,
638
+
tags ARRAY<STRING>,
639
+
categories ARRAY<STRING>
640
+
)
641
+
642
+
Querying ARRAY Data
643
+
^^^^^^^^^^^^^^^^^^^
644
+
645
+
PyAthena automatically converts ARRAY data between different formats:
646
+
647
+
.. code:: python
648
+
649
+
from sqlalchemy import create_engine, select
650
+
651
+
# Query ARRAY data using ARRAY constructor
652
+
result = connection.execute(
653
+
select().from_statement(
654
+
text("SELECT ARRAY[1, 2, 3, 4, 5] as item_ids")
655
+
)
656
+
).fetchone()
657
+
658
+
# Access ARRAY data as Python list
659
+
item_ids = result.item_ids # [1, 2, 3, 4, 5]
660
+
661
+
Complex ARRAY Operations
662
+
^^^^^^^^^^^^^^^^^^^^^^^^
663
+
664
+
For arrays containing complex data types:
665
+
666
+
.. code:: python
667
+
668
+
# Arrays with STRUCT elements
669
+
result = connection.execute(
670
+
select().from_statement(
671
+
text("SELECT ARRAY[ROW('Alice', 25), ROW('Bob', 30)] as users")
0 commit comments