-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Trino data reader #20127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
joshuaFordyce
wants to merge
10
commits into
run-llama:main
Choose a base branch
from
joshuaFordyce:trinoDataReader
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Trino data reader #20127
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c28fe59
added the TrinoReader stuff.
joshuaFordyce 6932023
Added framework for this contribution
joshuaFordyce b9ddb67
added comprehensive testing
joshuaFordyce a060bdb
add documentation to TrinoReader
joshuaFordyce 88762f5
feat: add TrinoReader with comprehensive linting fixes
joshuaFordyce a219c13
completed review notes
joshuaFordyce d2d5bab
corrected OOP issues
joshuaFordyce cc85b01
fixed package build breaking details
joshuaFordyce 0c598b8
fix: tests and package build (hopefully)
AstraBert 48a674b
changed requires-python=4.0 to <=3.13 to fix maturin build error
joshuaFordyce File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
llama-index-integrations/readers/llama-index-readers-trino/.gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # Python | ||
| __pycache__/ | ||
| *.pyc | ||
| *.pyo | ||
| *.pyd | ||
| .Python | ||
| .ipynb_checkpoints/ | ||
| venv/ | ||
| .env | ||
|
|
||
| # LlamaIndex/Poetry | ||
| .venv/ | ||
| .pytest_cache/ | ||
| .mypy_cache/ | ||
| .ruff_cache/ | ||
| dist/ | ||
| build/ | ||
| *.egg-info | ||
| htmlcov/ | ||
| tmp/ |
15 changes: 15 additions & 0 deletions
15
llama-index-integrations/readers/llama-index-readers-trino/CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # Changelog | ||
|
|
||
| All notable changes to this project will be documented in this file. | ||
|
|
||
| The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), | ||
| and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). | ||
|
|
||
| ## [0.1.0] - YYYY-MM-DD | ||
|
|
||
| ### Added | ||
|
|
||
| - Initial implementation of the `TrinoReader`. | ||
| - Basic connection, query execution, and document transformation logic. | ||
| - Setup files: `pyproject.toml`, `Makefile`, `README.md`, `LICENSE`, and `.gitignore`. | ||
| - Unit tests with mocking for Trino connection. |
26 changes: 26 additions & 0 deletions
26
llama-index-integrations/readers/llama-index-readers-trino/LICENSE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| ## 5. `LICENSE` (MIT License) | ||
|
|
||
| This is a permissive and common open-source license. | ||
|
|
||
| ```text | ||
| MIT License | ||
|
|
||
| Copyright (c) [Year] [Your Name] | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
26 changes: 26 additions & 0 deletions
26
llama-index-integrations/readers/llama-index-readers-trino/Makefile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| .PHONY: install setup lint format test mypy clean | ||
|
|
||
| # Setup and Dependencies | ||
| setup: | ||
| poetry install --with dev | ||
|
|
||
| # Linting and Formatting | ||
| lint: | ||
| ruff check . | ||
| format: | ||
| ruff check . --fix | ||
| ruff format . | ||
|
|
||
| # Testing and Typing | ||
| test: | ||
| pytest tests/ | ||
| mypy: | ||
| mypy . | ||
|
|
||
| # Clean up build artifacts | ||
| clean: | ||
| find . -name "__pycache__" -exec rm -rf {} + | ||
| rm -rf .pytest_cache | ||
| rm -rf .mypy_cache | ||
| rm -rf dist | ||
| rm -rf *.egg-info |
65 changes: 65 additions & 0 deletions
65
llama-index-integrations/readers/llama-index-readers-trino/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| TrinoReader is a custom Data Loader designed to solve the problem of robust data ingestion for LlamaiNdex RAG pipelines on a Trino Data lake | ||
|
|
||
| ## Quick Start Guide | ||
|
|
||
| ### Installation | ||
|
|
||
| Install the required Python packages, including the core reader and the native `trino` client: | ||
|
|
||
| ```bash | ||
| pip install llama-index-core trino pandas | ||
| ``` | ||
|
|
||
| ### Usage Example: Data Ingestion | ||
|
|
||
| The TrinoReader is instantiated with standard connection parameters and uses the load_data method to execute a query and retrieve documents ready for indexing. | ||
|
|
||
| ```Python | ||
| import logging | ||
| from llama_index.core.schema import Document | ||
| from your_module import TrinoReader # Assumed import | ||
|
|
||
| # 1. Define the SQL Query (Explicitly list columns for best practice) | ||
| query_to_index = """ | ||
| SELECT c_custkey, c_name, c_acctbal | ||
| FROM tpch.tiny.customer | ||
| WHERE c_nationkey = 1 | ||
| LIMIT 5; | ||
| """ | ||
|
|
||
| # 2. Instantiate the Reader | ||
| trino_data_loader = TrinoReader( | ||
| host="localhost", | ||
| port=8080, | ||
| user="rag_user", | ||
| catalog="tpch", | ||
| schema="tiny" | ||
| ) | ||
|
|
||
| # 3. Execute the Ingestion | ||
| print(f"Executing query on Trino...") | ||
| try: | ||
| documents: List[Document] = trino_data_loader.load_data(query=query_to_index) | ||
|
|
||
| # 4. Verification: Inspect the RAG-ready Document | ||
| if documents: | ||
| print(f"Successfully loaded {len(documents)} documents.") | ||
| print("\n--- Example Document (High-Density Context) ---") | ||
| print(f"Text Content: {documents[0].text}") | ||
| print(f"Metadata: {documents[0].metadata}") | ||
| print("------------------------------------------") | ||
|
|
||
| except Exception as e: | ||
| logging.error(f"FATAL: Data loading failed: {e}") | ||
|
|
||
| ``` | ||
|
|
||
| ### Contributing | ||
|
|
||
| This is an open-source project. If you have any suggestions for improvement, or would like to contribute a fix, please feel free to submit a pull request. | ||
|
|
||
| ### Focus Areas for Contribution: | ||
|
|
||
| Implementing the lazy_load_data generator for memory-efficient streaming of massive tables. | ||
|
|
||
| Adding support for advanced Trino authentication methods (Kerberos, JWT). |
152 changes: 152 additions & 0 deletions
152
llama-index-integrations/readers/llama-index-readers-trino/llama_index/readers/trino/base.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| from typing import Dict, List, Any, Tuple | ||
| from llama_index.core.readers.base import BaseReader | ||
| from llama_index.core.schema import Document | ||
| import trino | ||
| import logging | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class TrinoReader(BaseReader): | ||
| """ | ||
|
|
||
| Trino database reader. | ||
|
|
||
| Loads data from a Trino cluster into Document used by LlamaIndex. | ||
|
|
||
| Args: | ||
| host (str): server that's running Trino | ||
| schema (str): reside within a catalog and serve as a wa to organize tables and other database objects | ||
| port (int): network port number used for communication with a Trino cluster | ||
| catalog (str): A catalog in trino specifies a connector | ||
|
|
||
|
|
||
|
|
||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| user: str, | ||
| schema: str, | ||
| host: str, | ||
| port: int = 8080, | ||
| catalog: str = "hive", | ||
| **kwargs: Any, | ||
| ) -> None: | ||
| """Initialize with Trino connection parameters.""" | ||
| # Store connection parameters (self.host, self.port, etc.) | ||
| # self.conn_params = {...} | ||
|
|
||
| self.host = host | ||
| self.port = port | ||
| self.catalog = catalog | ||
| self.user = user | ||
| self.schema = schema | ||
| self._conn = None | ||
| self._cursor = None | ||
|
|
||
| self._conn_paramse = { | ||
| "host": host, | ||
| "port": port, | ||
| "catalog": catalog, | ||
| "user": user, | ||
| "schema": schema, | ||
| } | ||
|
|
||
| def configure_Connection(self) -> Tuple[trino.dbapi.Connection, trino.dbapi.Cursor]: | ||
| """ | ||
| Configure Connection for Trino Datawarehouse | ||
| """ | ||
| if self._conn is None or self._conn.closed: | ||
| try: | ||
| self._conn = trino.dbapi.connect( | ||
| host=self.host, | ||
| port=self.port, | ||
| catalog=self.catalog, | ||
| user=self.user, | ||
| schema=self.schema, | ||
| ) | ||
|
|
||
| self._cursor = self._conn.cursor() | ||
| except trino.dbapi.DatabaseError as e: | ||
| print(f"Trino connection failed:{e}") | ||
| raise | ||
| return self._conn, self._cursor | ||
|
|
||
| def execute_query( | ||
| self, query: str, conn: trino.dbapi.Connection, cur: trino.dbapi.Cursor | ||
| ): | ||
joshuaFordyce marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| Executes Query againg Trino instance | ||
|
|
||
| Args: | ||
| query (str): The SQL++ query to execute. | ||
| conn (trino.dbapi.Connection) The trino connection used to build the cursor | ||
| cursor (trino.dbapi.Cursor) an Object used to execute SQL queries against Trino | ||
|
|
||
| """ | ||
| try: | ||
| self._cursor.execute(query) | ||
|
|
||
| rows = self._cursor.fetchall() | ||
| return [rows, self._cursor.description] | ||
| except trino.dbapi.DatabaseError as e: | ||
| print(f"Trino connection failed: {e}") | ||
| raise | ||
|
|
||
| def load_data(self, query: str) -> List[Document]: | ||
| """ | ||
| Loads data from Trino by executing a single SQL query. | ||
|
|
||
| Args: | ||
| query: The SQL query to execute against the Trino cluster. | ||
|
|
||
| """ | ||
| all_documents = [] | ||
|
|
||
| conn = None | ||
| cur = None | ||
| try: | ||
| conn, cur = self.configure_Connection() | ||
| if not conn or not cur: | ||
| logger.warning("Could not establish connection; returning empty list") | ||
| return [] | ||
| # 1. Connect to Trino using self.conn_params | ||
|
|
||
| results = self.execute_query(query, conn, cur) | ||
|
|
||
| column_names = [desc[0] for desc in results[1]] | ||
|
|
||
| if results[0] is None: | ||
| return [] | ||
|
|
||
| # 3 Document Transformation | ||
| for row_index, row in enumerate(results[0]): | ||
| # Ensure all elements in row are cast to str before joining for content | ||
| content = ", ".join( | ||
| f"{name}: {value}" for name, value in zip(column_names, row) | ||
| ) | ||
|
|
||
| # Metadata must be a mapping (Dict[str, Any]) | ||
| metadata: Dict[str, Any] = dict(zip(column_names, row)) | ||
| metadata.update( | ||
| { | ||
| "source": "raw_data", | ||
| "catalog": self.catalog, | ||
| "schema": self.schema, | ||
| "row_id": row_index, | ||
| } | ||
| ) | ||
|
|
||
| all_documents.append(Document(text=content, metadata=metadata)) | ||
|
|
||
| return all_documents | ||
|
|
||
| except Exception as e: | ||
| logger.error(f"FATAL ERROR during data loading: {e}") | ||
| raise | ||
| finally: | ||
| if cur: | ||
| cur.close() | ||
| if conn: | ||
| conn.close() | ||
|
Empty file.
74 changes: 74 additions & 0 deletions
74
llama-index-integrations/readers/llama-index-readers-trino/pyproject.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| [build-system] | ||
| requires = ["hatchling"] | ||
| build-backend = "hatchling.build" | ||
|
|
||
| [dependency-groups] | ||
| dev = [ | ||
| "ipython==8.10.0", | ||
| "jupyter>=1.0.0,<2", | ||
| "mypy==0.991", | ||
| "pre-commit==3.2.0", | ||
| "pylint==2.15.10", | ||
| "pytest==7.2.1", | ||
| "pytest-mock==3.11.1", | ||
| "ruff==0.11.11", | ||
| "types-Deprecated>=0.1.0", | ||
| "types-PyYAML>=6.0.12.12,<7", | ||
| "types-protobuf>=4.24.0.4,<5", | ||
| "types-redis==4.5.5.0", | ||
| "types-requests==2.28.11.8", | ||
| "types-setuptools==67.1.0.0", | ||
| "black[jupyter]<=23.9.1,>=23.7.0", | ||
| "codespell[toml]>=v2.2.6", | ||
| "diff-cover>=9.2.0", | ||
| "pytest-cov>=6.1.1", | ||
| # Specific requirement for testing your Trino code: | ||
| "trino>=0.316.0", | ||
| "pandas>=2.0.0", | ||
| ] | ||
|
|
||
| [project] | ||
| name = "llama-index-readers-trino" | ||
| version = "0.1.0" # Starting version for a new integration | ||
| description = "LlamaIndex Data Loader for Trino distributed SQL query engine." | ||
| authors = [{name = "Joshua Fordyce", email = "[email protected]"}] | ||
| requires-python = ">=3.9,<4.0" | ||
| readme = "README.md" | ||
| license = "MIT" | ||
| maintainers = [{name = "Joshua Fordyce"}] | ||
| keywords = [ | ||
| "trino", | ||
| "distributed query", | ||
| "data lakehouse", | ||
| "data federation", | ||
| "sql", | ||
| ] | ||
| dependencies = ["llama-index-core>=0.13.0,<0.15", "trino>=0.316.0"] | ||
|
|
||
| [tool.codespell] | ||
| check-filenames = true | ||
| check-hidden = true | ||
| skip = "*.csv,*.html,*.json,*.jsonl,*.pdf,*.txt,*.ipynb" | ||
|
|
||
| [tool.hatch.build.targets.sdist] | ||
| include = ["llama_index/"] | ||
| exclude = ["**/BUILD"] | ||
|
|
||
| [tool.hatch.build.targets.wheel] | ||
| include = ["llama_index/"] | ||
| exclude = ["**/BUILD"] | ||
|
|
||
| [tool.llamahub] | ||
| # This path needs to reflect the final location of your Trino reader module | ||
| contains_example = false | ||
| import_path = "llama_index.readers.trino" | ||
|
|
||
| [tool.llamahub.class_authors] | ||
| # My new class name | ||
| TrinoReader = "Joshua Fordyce" | ||
|
|
||
| [tool.mypy] | ||
| disallow_untyped_defs = true | ||
| exclude = ["_static", "build", "examples", "notebooks", "venv"] | ||
| ignore_missing_imports = true | ||
| python_version = "3.10" |
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want the package to be published correctly, the scripts should be moved under
llama_index/readers/trino