SQLAlchemy Trino Dialect
sqlalchemy-trino was a Trino (f.k.a. PrestoSQL) dialect for SQLAlchemy. As of version 0.5.0, this project is officially deprecated. Its functionality has been merged into the upstream `trino-python-client` project, which now provides a built-in SQLAlchemy dialect. This library is no longer actively developed.
Warnings
- breaking The `sqlalchemy-trino` library is officially deprecated as of version 0.5.0. All active development has moved to the `trino-python-client` package, which now includes the SQLAlchemy dialect directly.
- gotcha When `trino-python-client` (PyPI package `trino`) version 0.307.0 or newer is installed, it includes a built-in SQLAlchemy dialect. Installing both `sqlalchemy-trino` and `trino-python-client` might lead to conflicts or unexpected behavior if `sqlalchemy-trino` is older than 0.4.1.
- breaking Version 0.4.0 introduced significant changes: `TrinoResultProxy` was removed in favor of eager-loading `cursor.description`, and metadata queries shifted from `SHOW` commands to `information_schema`.
- gotcha The default catalog was changed from `hive` to `system` in version 0.3.0.
Install
-
pip install sqlalchemy-trino -
pip install trino[sqlalchemy]
Imports
- create_engine
from sqlalchemy import create_engine
Quickstart
import os
from sqlalchemy import create_engine, text
# NOTE: This library is DEPRECATED. The recommended approach is to use trino-python-client directly.
# pip install trino[sqlalchemy]
# from sqlalchemy import create_engine
# engine = create_engine(f"trino://{os.environ.get('TRINO_USER', 'user')}@{os.environ.get('TRINO_HOST', 'localhost')}:{os.environ.get('TRINO_PORT', '8080')}/{os.environ.get('TRINO_CATALOG', 'system')}/{os.environ.get('TRINO_SCHEMA', 'runtime')}")
# Example using the deprecated sqlalchemy-trino library:
# Configure connection details via environment variables or replace directly
TRINO_USER = os.environ.get('TRINO_USER', 'test_user')
TRINO_HOST = os.environ.get('TRINO_HOST', 'localhost')
TRINO_PORT = os.environ.get('TRINO_PORT', '8080')
TRINO_CATALOG = os.environ.get('TRINO_CATALOG', 'system')
TRINO_SCHEMA = os.environ.get('TRINO_SCHEMA', 'runtime')
connection_string = (
f"trino://{TRINO_USER}@{TRINO_HOST}:{TRINO_PORT}/{TRINO_CATALOG}/{TRINO_SCHEMA}"
)
try:
engine = create_engine(connection_string)
with engine.connect() as connection:
result = connection.execute(text("SELECT 1"))
print("Successfully connected to Trino!")
for row in result:
print(row)
except Exception as e:
print(f"Error connecting to Trino: {e}")
print("Please ensure Trino is running and connection details are correct.")