Package: aiotrino
IntroductionThis package provides a asyncio client interface to queryTrinoa distributed SQL engine. It supports Python 3.7, 3.8, 3.9, 3.10, 3.11, 3.12.Installation$ pip install aiotrinoQuick StartUse the DBAPI interface to query Trino:importaiotrinoconn=aiotrino.dbapi.connect(host='localhost',port=8080,user='the-user',catalog='the-catalog',schema='the-schema',)cur=awaitconn.cursor()awaitcur.execute('SELECT * FROM system.runtime.nodes')rows=awaitcur.fetchall()awaitconn.close()Or with context managerimportaiotrinoasyncwithaiotrino.dbapi.connect(host='localhost',port=8080,user='the-user',catalog='the-catalog',schema='the-schema',)asconn:cur=awaitconn.cursor()awaitcur.execute('SELECT * FROM system.runtime.nodes')rows=awaitcur.fetchall()This will query thesystem.runtime.nodessystem tables that shows the nodes
in the Trino cluster.The DBAPI implementation inaiotrino.dbapiprovides methods to retrieve fewer
rows for exampleCursorfetchone()orCursor.fetchmany(). By defaultCursor.fetchmany()fetches one row. Please settrino.dbapi.Cursor.arraysizeaccordingly.For backwards compatibility with PrestoSQL, override the headers at the start of your applicationimportaiotrinoaiotrino.constants.HEADERS=aiotrino.constants.PrestoHeadersBasic AuthenticationTheBasicAuthenticationclass can be used to connect to a LDAP-configured Trino
cluster:importaiotrinoconn=aiotrino.dbapi.connect(host='coordinator url',port=8443,user='the-user',catalog='the-catalog',schema='the-schema',http_scheme='https',auth=aiotrino.auth.BasicAuthentication("principal id","password"),)cur=awaitconn.cursor()awaitcur.execute('SELECT * FROM system.runtime.nodes')rows=awaitcur.fetchall()awaitconn.close()JWT Token AuthenticationTheJWTAuthenticationclass can be used to connect to a configured Trino cluster:importaiotrinoconn=aiotrino.dbapi.connect(host='coordinator url',port=8443,catalog='the-catalog',schema='the-schema',http_scheme='https',auth=aiotrino.auth.JWTAuthentication(token="jwt-token"),)cur=awaitconn.cursor()awaitcur.execute('SELECT * FROM system.runtime.nodes')rows=awaitcur.fetchall()awaitconn.close()TransactionsThe client runs by default inautocommitmode. To enable transactions, setisolation_levelto a value different thanIsolationLevel.AUTOCOMMIT:importaiotrinofromaiotrinoimporttransactionasyncwithaiotrino.dbapi.connect(host='localhost',port=8080,user='the-user',catalog='the-catalog',schema='the-schema',isolation_level=transaction.IsolationLevel.REPEATABLE_READ,)asconn:cur=awaitconn.cursor()awaitcur.execute('INSERT INTO sometable VALUES (1, 2, 3)')awaitcur.fetchone()awaitcur.execute('INSERT INTO sometable VALUES (4, 5, 6)')awaitcur.fetchone()The transaction is created when the first SQL statement is executed.trino.dbapi.Connection.commit()will be automatically called when the code
exits thewithcontext and the queries succeed, otherwise
`trino.dbapi.Connection.rollback()' will be called.DevelopmentGetting Started With DevelopmentStart by forking the repository and then modify the code in your fork.Clone the repository and go inside the code directory. Then you can get the
version with./setup.py --version.We recommend that you usevirtualenvfor development:$ virtualenv .venv
$ . .venv/bin/activate
# TODO add requirements.txt: pip install -r requirements.txt
$ pip install .For development purpose, pip can reference the code you are modifying in avirtualenv:$ pip install -e .[tests]That way, you do not need to runpip installagain to make your changes
applied to thevirtualenv.When the code is ready, submit a Pull Request.Code StyleFor Python code, adhere to PEP 8.Prefer code that is readable over one that is "clever".When writing a Git commit message, follow theseguidelines.Running TestsThere is a helper scripts,run, that provides commands to run tests.
Type./run teststo run both unit and integration tests.aiotrinousespytestfor its tests. To run
only unit tests, type:$ pytest testsThen you can pass options like--pdbor anything supported bypytest --help.To run the tests with different versions of Python in managedvirtualenvs,
usetox(see the configuration intox.ini):$ toxTo run integration tests:$ pytest integration_testsThey pull a Docker image and then run a container with a Trino server:the image is namedtrinodb/trino:${TRINO_VERSION}the container is namedaiotrino-python-client-tests-{uuid4()[:7]}Test setupSupported OS Ubuntu 22.04Installpyenvcurlhttps://pyenv.run|bashInstall required python versions# Install the latest of all supported versionspyenvinstall3.7,3.8,3.9,3.10,3.11,3.12Set the installed versions as default for the shell. This allowstoxto find them.List installed versions and update the following command as needed.pyenvversionspyenvshell3.12.13.11.33.10.113.9.163.8.163.7.16InstalltoxpipinstalltoxRuntoxtoxReleasingSet up your development environment.Change version inaiotrino/__init__.py.Commit and create an annotated tag (git tag -m '' current_version)Run the following:..venv/bin/activate&&pipinstalltwinewheelsetuptools&&rm-rfdist/&&./setup.pysdistbdist_wheel&&twineuploaddist/*&&openhttps://pypi.org/project/aiotrino/&&echo"Released!"Push the branch and the tag (git push upstream master current_version)Send release announcement.Need Help?Feel free to create an issue as it make your request visible to other users and contributors.If an interactive discussion would be better or if you just want to hangout and chat about
the Trino Python client, you can join us on the#python-clientchannel onTrino Slack.