Python SQL Query Engine
Project description
Opteryx
Query your data, where it lives.
A unified SQL interface to unlock insights across your diverse data sources, from blobs stores to databases - effortless cross-platform data analytics.
Resource | Location |
---|---|
Source Code | https://github.com/mabel-dev/opteryx |
Documentation | https://opteryx.dev/ |
Download | https://pypi.org/project/opteryx/ |
Install • Examples • Get Involved
What is Opteryx?
Opteryx champions the SQL-on-everything approach, streamlining cross-platform data analytics by federating SQL queries across diverse data sources, including database systems like Postgres and datalake file formats like Parquet. The goal is to enhance your data analytics process by offering a unified way to access data from across your organization.
Opteryx is a Python library that combines elements of in-process database engines like SQLite and DuckDB with federative features found in systems like Presto and Trino. The result is a versatile tool for querying data across multiple data sources in a seamless fashion.
Opteryx offers the following features:
- SQL queries on data files generated by other processes, such as logs
- A command-line tool for filtering, transforming, and combining files
- Integration with familiar tools like pandas and Polars
- Embeddable as a low-cost engine, enabling portability and allowing for hundreds of analysts to leverage ad hoc databases with ease
- Unified and federated access to data on disk, in the cloud, and in on-premises databases, not only through the same interface but in the same query
How Does it Work?
Opteryx processes queries by first determining the appropriate query language to interact with different downstream data platforms. It translates your query into SQL, CQL, or another suitable format for document stores like MongoDB, based on the data source. This enables Opteryx to efficiently retrieve the necessary data from systems such as MySQL or MongoDB to respond to your query.
Why Use Opteryx?
Familiar Interface
Opteryx supports key parts of the Python DBAPI and SQL92 standard standards which many analysts and engineers will already know how to use.
Consistent Syntax
Opteryx creates a common SQL-layer over multiple data platforms, allowing backend systems to be upgraded, migrated or consolidated without changing any Opteryx code.
Where possible, errors and warnings returned by Opteryx help the user to understand how to fix their statement to reduce time-to-success for even novice SQL users.
Consumption-Based Billing Friendly
Opteryx is well-suited for deployments to environments which are pay-as-you-use, like Google Cloud Run. Great for situations where you have low-volume usage, or multiple environments, where the costs of many traditional database deployment can quickly add up.
Python Ecosystem
Opteryx is Open Source Python, it quickly and easily integrates into Python code, including Jupyter Notebooks, so you can start querying your data within a few minutes. Opteryx integrates with many of your favorite Python data tools, you can use Opteryx to run SQL against pandas and Polars DataFrames, and even execute a JOIN
on an in-memory DataFrame and a remote SQL dataset.
Time Travel
Designed for data analytics in environments where decisions need to be replayable, Opteryx allows you to query data as at a point in time in the past to replay decision algorithms against facts as they were known in the past. You can even self-join tables historic data, great for finding deltas in datasets over time. (data must be structured to enable temporal queries)
Fast
Benchmarks on M2 Pro Mac running an ad hoc GROUP BY
over a 6 million row parquet file via the CLI in ~1/4th of a second from a cold start (no caching and predefined schema). (different systems will have different performance characteristics)
Instant Elasticity
Designed to run in Knative and similar environments like Google Cloud Run, Opteryx can scale down to zero, and scale up to respond to thousands of concurrent queries within seconds.
Bring your own Data
Opteryx supports multiple query engines, dataframe APIs and storage formats. You can mix-and-match sources in a single query. Opteryx can even JOIN
datasets stored in different formats and different platforms in the same query, such as Parquet and MySQL.
Opteryx allows you to query your data directly in the systems where they are stored, eliminating the need to duplicate data into a common store for analytics. This saves you the cost and effort of maintaining duplicates.
Opteryx can push parts of your query to the source query engine, allowing queries to run at the speed of the backend, rather than your local computer.
And if there's not a connector in the box for your data platform; feel free to submit a pull request to add one.
Install
Installing from PyPI is recommended.
pip install opteryx
To build Opteryx from source, refer to the contribution guides.
Opteryx installs with a small set of libraries it needs for core functionality, such as Numpy, PyArrow, and orjson. Some features require additional libraries to be installed, you are notified of these libraries as they are required.
Examples
Filter a Dataset on the Command Line
In this example, we are running Opteryx from the command line to filter one of the internal example datasets and display the results on the console.
python -m opteryx "SELECT * FROM \$astronauts WHERE 'Apollo 11' IN UNNEST(missions);"
this example is complete and should run as-is
Execute a Simple Query in Python
In this example, we are showing the basic usage of the Python API by executing a simple query that makes no references to any datasets.
# Import the Opteryx SQL query engine library.
import opteryx
# Execute a SQL query to evaluate the expression 4 * 7.
# The result is stored in the 'result' variable.
result = opteryx.query("SELECT 4 * 7;")
# Display the first row(s) of the result to verify the query executed correctly.
result.head()
ID | 4 * 7 |
---|---|
1 | 28 |
this example is complete and should run as-is
Execute SQL on a pandas DataFrame
In this example, we are running a SQL statement on a pandas DataFrame and returning the result as a new pandas DataFrame.
# Required imports
import opteryx
import pandas
# Read data from the exoplanets.csv file hosted on Google Cloud Storage
# The resulting DataFrame is stored in the variable `pandas_df`.
pandas_df = pandas.read_csv("https://storage.googleapis.com/opteryx/exoplanets/exoplanets.csv")
# Register the pandas DataFrame with Opteryx under the alias "exoplanets"
# This makes the DataFrame available for SQL-like queries.
opteryx.register_df("exoplanets", pandas_df)
# Perform an SQL query to group the data by `koi_disposition` and count the number
# of occurrences of each distinct `koi_disposition`.
# The result is stored in `aggregated_df`.
aggregated_df = opteryx.query("SELECT koi_disposition, COUNT(*) FROM exoplanets GROUP BY koi_disposition;").pandas()
# Display the aggregated DataFrame to get a preview of the result.
aggregated_df.head()
koi_disposition COUNT(*)
0 CONFIRMED 2293
1 FALSE POSITIVE 5023
2 CANDIDATE 2248
this example is complete and should run as-is
Query Data on Local Disk
In this example, we are querying and filtering a file directly. This example will not run as written because the file being queried does not exist.
# Import the Opteryx query engine.
import opteryx
# Execute a SQL query to select the first 5 rows from the 'space_missions.parquet' table.
# The result will be stored in the 'result' variable.
result = opteryx.query("SELECT * FROM 'space_missions.parquet' LIMIT 5;")
# Display the result.
# This is useful for quick inspection of the data.
result.head()
ID | Company | Location | Price | Launched_at | Rocket | Rocket_Status | Mission | Mission_Status |
---|---|---|---|---|---|---|---|---|
0 | RVSN USSR | Site 1/5, Baikonur Cosmodrome, | null | 1957-10-04 19:28:00 | Sputnik 8K71PS | Retired | Sputnik-1 | Success |
1 | RVSN USSR | Site 1/5, Baikonur Cosmodrome, | null | 1957-11-03 02:30:00 | Sputnik 8K71PS | Retired | Sputnik-2 | Success |
2 | US Navy | LC-18A, Cape Canaveral AFS, Fl | null | 1957-12-06 16:44:00 | Vanguard | Retired | Vanguard TV3 | Failure |
3 | AMBA | LC-26A, Cape Canaveral AFS, Fl | null | 1958-02-01 03:48:00 | Juno I | Retired | Explorer 1 | Success |
4 | US Navy | LC-18A, Cape Canaveral AFS, Fl | null | 1958-02-05 07:33:00 | Vanguard | Retired | Vanguard TV3BU | Failure |
this example requires a data file, space_missions.parquet.
Query Data in SQLite
In this example, we are querying a SQLite database via Opteryx. This example will not run as written because the file being queried does not exist.
# Import the Opteryx query engine and the SqlConnector from its connectors module.
import opteryx
from opteryx.connectors import SqlConnector
# Register a new data store with the prefix "sql", specifying the SQL Connector to handle it.
# This allows queries with the 'sql' prefix to be routed to the appropriate SQL database.
opteryx.register_store(
prefix="sql", # Prefix for distinguishing this particular store
connector=SqlConnector, # Specify the connector to handle queries for this store
remove_prefix=True, # Remove the prefix from the table name when querying SQLite
connection="sqlite:///database.db" # SQLAlchemy connection string for the SQLite database
)
# Execute a SQL query to select specified columns from the 'planets' table in the SQL store,
# limiting the output to 5 rows. The result is stored in the 'result' variable.
result = opteryx.query("SELECT name, mass, diameter, density FROM sql.planets LIMIT 5;")
# Display the result.
# This is useful for quickly verifying that the query executed correctly.
result.head()
ID | name | mass | diameter | density |
---|---|---|---|---|
1 | Mercury | 0.33 | 4879 | 5427 |
2 | Venus | 4.87 | 12104 | 5243 |
3 | Earth | 5.97 | 12756 | 5514 |
4 | Mars | 0.642 | 6792 | 3933 |
5 | Jupiter | 1898.0 | 142984 | 1326 |
this example requires a data file, database.db.
Query Data on GCS
In this example, we are to querying a dataset on GCS in a public bucket called 'opteryx'.
# Import the Opteryx query engine and the GcpCloudStorageConnector from its connectors module.
import opteryx
from opteryx.connectors import GcpCloudStorageConnector
# Register a new data store named 'opteryx', specifying the GcpCloudStorageConnector to handle it.
# This allows queries for this particular store to be routed to the appropriate GCP Cloud Storage bucket.
opteryx.register_store(
"opteryx", # Name of the store to register
GcpCloudStorageConnector # Connector to handle queries for this store
)
# Execute a SQL query to select all columns from the 'space_missions' table located in the 'opteryx' store,
# and limit the output to 5 rows. The result is stored in the 'result' variable.
result = opteryx.query("SELECT * FROM opteryx.space_missions LIMIT 5;")
# Display the result.
# This is useful for quickly verifying that the query executed correctly.
result.head()
ID | Company | Location | Price | Launched_at | Rocket | Rocket_Status | Mission | Mission_Status |
---|---|---|---|---|---|---|---|---|
0 | RVSN USSR | Site 1/5, Baikonur Cosmodrome, | null | 1957-10-04 19:28:00 | Sputnik 8K71PS | Retired | Sputnik-1 | Success |
1 | RVSN USSR | Site 1/5, Baikonur Cosmodrome, | null | 1957-11-03 02:30:00 | Sputnik 8K71PS | Retired | Sputnik-2 | Success |
2 | US Navy | LC-18A, Cape Canaveral AFS, Fl | null | 1957-12-06 16:44:00 | Vanguard | Retired | Vanguard TV3 | Failure |
3 | AMBA | LC-26A, Cape Canaveral AFS, Fl | null | 1958-02-01 03:48:00 | Juno I | Retired | Explorer 1 | Success |
4 | US Navy | LC-18A, Cape Canaveral AFS, Fl | null | 1958-02-05 07:33:00 | Vanguard | Retired | Vanguard TV3BU | Failure |
this example is complete and should run as-is
You can also try Opteryx right now using our interactive labs on Binder.
Community
Get Involved
- :star: Star this repo
- Contribute — join us in building Opteryx, through writing code, or inspiring others to use it.
- Let us know your ideas, how you are using Opteryx, or report a bug or feature request.
- See the contributor documentation for Opteryx. It's easy to get started, and we're really friendly if you need any help!
- If you're interested in contributing to the code now, check out GitHub issues. Feel free to ask questions or open a draft PR.
Security
See the project Security Policy for information about reporting vulnerabilities.
License
Opteryx is licensed under Apache 2.0 except where specific modules note otherwise.
Status
Opteryx is in beta. Beta means different things to different people, to us, being beta means:
- Core functionality has good regression test coverage to help ensure stability
- Some edge cases may have undetected bugs
- Performance tuning is incomplete
- Changes are focused on feature completion, bugs, performance, reducing debt, and security
- Code structure and APIs are not stable and may change
Related Projects
Project details
Release history Release notifications | RSS feed
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 opteryx-0.17.2a818-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2f594f34da4d97eb74e79a81a57e2119fbb7ab6d919a6fe558d94efa583a1506 |
|
MD5 | de3e63fd0ffa57c1e6664ca389460be7 |
|
BLAKE2b-256 | a29b0b32d73e449da87af768fc93d3ae5cba391a8c778719109058d167f95b54 |
Hashes for opteryx-0.17.2a818-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9485c10adfd3242435f123ba18d08b110f57403b3d7c8ca6dd287ba0db4c5287 |
|
MD5 | 0130362cb8109c59e28949f1b8145047 |
|
BLAKE2b-256 | b00e3c268289b445128004350fb38dc7ec4b4e75631c0e18cd130bccdafa6ffc |
Hashes for opteryx-0.17.2a818-cp312-cp312-macosx_10_15_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d4ae6a216531b2ca1d4b68f44a555c51b3d105df590a57f203115b853f221c31 |
|
MD5 | 1c9d30b6bd5e1c26945ce3d2cafe04b6 |
|
BLAKE2b-256 | b1d8b9f6dbd7d70f9df3bccc83694ef8b6c6c461b4a554f444231228cff37cf2 |
Hashes for opteryx-0.17.2a818-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88aa905c3057c2b3bd3d0359b7f960526aaea4fada3c2074d171cf3d202468fc |
|
MD5 | aa0cdef92d7c7d12bc97015977b8ed33 |
|
BLAKE2b-256 | 50ca1f8bb8694ce21b8177cd82c340381a6460a3b6a8e59aed03e27bbe7d685f |
Hashes for opteryx-0.17.2a818-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 262c480762aab6ee402e05d5148ba13384082430ebc740d73e92731d6d5d7ec0 |
|
MD5 | 5be1cebbcde967ab33a569adc9c7c514 |
|
BLAKE2b-256 | 377b16e6866b4d3e3e34bd1635544d9acd9632e6bb0db88115c904bf60caca2a |
Hashes for opteryx-0.17.2a818-cp311-cp311-macosx_10_15_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 23fcad2507097219742239c82664672156184d45d2510e93d33e4de39b21f9b6 |
|
MD5 | 48314f3cc1514e0a676c35e05383eb95 |
|
BLAKE2b-256 | a456a4840b007257c27d9f1270e8210b505be1d1ec71a9f34cb01726491c77ad |
Hashes for opteryx-0.17.2a818-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ca6332235d83ca8eed66c18c8cb56c1e3eb26f8c765178fad62cb99c3a806cc |
|
MD5 | 2a2a072ad33d8738d914c443ded8ee9d |
|
BLAKE2b-256 | 47fb64f056081142648de662181b035d757f2f4b6c84bd10efb73cd3d2f73e57 |
Hashes for opteryx-0.17.2a818-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 51e4324bb208ca88f805bd88b3e56f318acd3c71ff81b674a79178627fc8b7b1 |
|
MD5 | 2bad7efda1a08a337ee5a655afba8d58 |
|
BLAKE2b-256 | 7a6d3b20dcb4152568b54c110201a51877cb4d5e870726494d7e177e0767901a |
Hashes for opteryx-0.17.2a818-cp310-cp310-macosx_10_15_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 86e8da8de70e2b9b4e8a458aa231418b0368366b0f419dd496b764631c0999e7 |
|
MD5 | 912e07a32e945da4ad7b16f37b1569a2 |
|
BLAKE2b-256 | 747c713c995cc4d5f808e49745ffd52c5ec2e15bdc3f55cc40a7d134d5f66a60 |
Hashes for opteryx-0.17.2a818-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 66357a9cfc499dc939b80dbc408443697f054bea723684aa8e35028f5dd2ee3b |
|
MD5 | c0954f02766fe53f7647e8fd65780f20 |
|
BLAKE2b-256 | 63c6df78ebc47166c74c3be3a7dc746b2d915b1a8abbf012b551e7dced0ab018 |
Hashes for opteryx-0.17.2a818-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | df47a5bfd944a81eee3dca04e231cf1faf23cd0208e06e7a4372a3bd90f7ae03 |
|
MD5 | 32ab63d6df301b3a6b77d6b5b7986515 |
|
BLAKE2b-256 | b52dae98da1e9536787a2ca9f49a1bb73730c7bb8b1150a31bd8b9ce3c458ae7 |
Hashes for opteryx-0.17.2a818-cp39-cp39-macosx_10_15_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2c8fd0b16f05ceb238c5fc270b55a42f6be3dbd55d57180f396e6e07ef1bb1f5 |
|
MD5 | 767a70e1c61c695aa86c02dd361d10f7 |
|
BLAKE2b-256 | 2cf51fc663d9639695072b6d959960883fb0401b4f56c3009ac69867d22f77e5 |