Skip to content

Commit f96b326

Browse files
Merge pull request #715 from pyathena-dev/docs/federated-passthrough-queries
docs: add federated passthrough queries section
2 parents f41404c + 41dd842 commit f96b326

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

docs/usage.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,34 @@ and the query was a DML statement (the assumption being that you always want to
212212

213213
The S3 staging directory is not checked, so it's possible that the location of the results is not in your provided `s3_staging_dir`.
214214

215+
## Federated passthrough queries
216+
217+
Athena's [federated query](https://docs.aws.amazon.com/athena/latest/ug/connect-to-a-data-source.html) feature lets you query data in external sources (PostgreSQL, MySQL, Snowflake, etc.) through Lambda-based connectors. By default Athena's planner translates your SQL into the source dialect, but for complex queries — or when you need a feature only the source supports — you can use **passthrough queries** to push native SQL directly to the source.
218+
219+
Passthrough queries use the `TABLE(<connector>.system.query(query => '...'))` syntax. Because PyAthena passes the SQL string through to Athena unchanged, **no client-side changes are required** — these queries work out of the box.
220+
221+
```python
222+
from pyathena import connect
223+
224+
cursor = connect(s3_staging_dir="s3://YOUR_S3_BUCKET/path/to/",
225+
region_name="us-west-2").cursor()
226+
227+
cursor.execute("""
228+
SELECT * FROM TABLE(
229+
my_postgres_connector.system.query(
230+
query => 'SELECT id, name FROM public.users WHERE created_at > NOW() - INTERVAL ''7 days'''
231+
)
232+
)
233+
""")
234+
print(cursor.fetchall())
235+
```
236+
237+
The connector (`my_postgres_connector` above) must be registered in Athena as a data source connector first; see the [Athena documentation](https://docs.aws.amazon.com/athena/latest/ug/connectors-passthrough-queries.html) for the connector setup steps and the list of connectors that support passthrough.
238+
239+
```{note}
240+
Result columns from a passthrough query come back with the source system's types, which Athena maps to its own type system. When using `PandasCursor` or `ArrowCursor`, verify that the resulting dtypes match your expectations — connector-specific types (e.g., PostgreSQL `json`, MySQL `geometry`) may be returned as strings.
241+
```
242+
215243
(query-execution-callback)=
216244

217245
## Query execution callback

0 commit comments

Comments
 (0)