Trino dialect for SQLAlchemy
Project description
sqlalchemy-trino
Trino (f.k.a PrestoSQL) dialect for SQLAlchemy.
The primary purpose of this is provide a dialect for Trino that can be used with Apache Superset. But other use-cases should works as well.
Supported Trino version
Trino version 352 and higher
Installation
The driver can either be installed through PyPi or from the source code.
Through Python Package Index
pip install sqlalchemy-trino
Latest from Source Code
pip install git+https://github.com/dungdm93/sqlalchemy-trino
Usage
To connect from SQLAlchemy to Trino, use connection string (URL) following this pattern:
trino://<username>:<password>@<host>:<port>/catalog/[schema]
JWT authentication
You can pass the JWT token via either connect_args
or the query string
parameter accessToken
:
from sqlalchemy.engine import create_engine
from trino.auth import JWTAuthentication
# pass access token via connect_args
engine = create_engine(
'trino://<username>@<host>:<port>/',
connect_args={'auth': JWTAuthentication('a-jwt-token')},
)
# pass access token via the query string param accessToken
engine = create_engine(
'trino://<username>@<host>:<port>/?accessToken=a-jwt-token',
)
Notice: When using username and password, it will connect to Trino over TLS connection automatically.
User impersonation
It supports user impersonation with username and password based authentication only.
You can pass the session user (a.k.a., the user that will be impersonated) via
either connect_args
or the query string parameter sessionUser
:
from sqlalchemy.engine import create_engine
# pass session user via connect_args
engine = create_engine(
'trino://<username>:<password>@<host>:<port>/',
connect_args={'user': 'user-to-be-impersonated'},
)
# pass session user via a query string parameter
engine = create_engine(
'trino://<username>:<password>@<host>:<port>/?sessionUser=user-to-be-impersonated',
)
Pandas support
import pandas as pd
from pandas import DataFrame
import sqlalchemy_trino
from sqlalchemy.engine import Engine, Connection
def trino_pandas_write(engine: Engine):
df: DataFrame = pd.read_csv("tests/data/population.csv")
df.to_sql(con=engine, schema="default", name="abcxyz", method="multi", index=False)
print(df)
def trino_pandas_read(engine: Engine):
connection: Connection = engine.connect()
df = pd.read_sql("SELECT * FROM public.foobar", connection)
print(df)
Note: in df.to_sql
following params is required:
index=False
because index is not supported in Trino.method="multi"
: currentlymethod=None
(default) is not working because Trino dbapi is not supportexecutemany
yet
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for sqlalchemy_trino-0.4.1-py3.8.egg
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3c4ab1aa6a6476c1c92961087fa85283fd9179df24ceb1de77bc130102df527b |
|
MD5 | 0ae4a09233404e6c771298e7b226c81c |
|
BLAKE2b-256 | bae60e1ad2bed8ea1a8532de13b168a8c55706d1ce15bec8db0e6ba5379e1cfe |
Hashes for sqlalchemy_trino-0.4.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b9040e8a428d2d16714389f1306dd2ab92f615580c7e57a7189f911c4d1b9422 |
|
MD5 | 62fa0c20f2e435b82ba11211d937fab0 |
|
BLAKE2b-256 | 4b91d9b8c1538543fb4586a52f28e0f93ce09ba1d72d08951d1bc8eefd8bde17 |