Query template rendering and execution library
Project description
qtrex
Query template rendering and execution library written in Python.
The goal of qtrex
is to provide a simple API that supports loading .sql
files that can be templated with jinja
, and provide extensible configuration
options to either compile the files, and execute the rendered templates against
various databases.
Getting Started
qtrex
is installable at https://pypi.org/project/qtrex/ via pip
using:
We currently only support bigquery
, but plan on adding other DB support as
optional dependencies.
pip install 'qtrex[bigquery]==0.0.5'
Examples
Here is a brief example usage of qtrex
.
Assuming you have query templates in a directory on a local filesystem, using our test suite as an example:
|tests
|--test_*.py
|--testdata
|--mytemplate.sql
|--ingest
|--another_file_ext.j2
|--another_query.sql
Where ./tests/testdata/mytemplate.sql
has the following contents:
SELECT SUM(x)
FROM UNNEST({{ params.test_array }}) AS x
and ./tests/testdata/ingest/another_query.sql
has:
SELECT
*
FROM
`{{ params.my_project_id }}.{{ params.my_dataset }}.{{ params.my_table }}`
and lastly, ./tests/testdata/nested_params.sql
has:
SELECT
{{ params.test_dict_key.one }} + {{ params.test_dict_key.two }}
Next, we want to have our .yaml
config (or extend qtrex.config.BaseConfig
)
to implement your own config mechanism.
Our ./tests/example.yaml
will look like:
params:
- key: test_string_key
value: "string_value"
- key: test_array_key
value: [1, 2, 3]
- key: test_dict_key
value:
one: 1
two: 2
three: 3
We can now run the following script (./tests/example.py
) after changing
into the ./tests
directory
from qtrex.executor import BigQueryExecutor
from qtrex.store import Store
from qtrex.config import YAMLConfig
def main():
with open("./example.yaml", "r") as f:
cfg = YAMLConfig(f)
store = Store.from_path(cfg, "./testdata")
ex = BigQueryExecutor()
for query_ref in store:
print(f"{query_ref.name}: {query_ref.template}\n")
res = ex.execute(query_ref, dry_run=True)
print(f"results: {res}")
if __name__ == "__main__":
main()
When we run this script:
cd ./tests
python example.py
we should see the following in stdout
mytemplate.sql: SELECT SUM(x)
FROM UNNEST([1, 2, 3]) AS x
results: QueryResult(query_ref=QueryRef(filename='./testdata\\mytemplate.sql', template='SELECT SUM(x)\nFROM UNNEST([1, 2, 3]) AS x', name='mytemplate.sql'), df=None, error=None)
nested_params.sql: SELECT
1 + 2
results: QueryResult(query_ref=QueryRef(filename='./testdata\\nested_params.sql', template='SELECT\n 1 + 2', name='nested_params.sql'), df=None, error=None)
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 Distribution
File details
Details for the file qtrex-0.1.0.tar.gz
.
File metadata
- Download URL: qtrex-0.1.0.tar.gz
- Upload date:
- Size: 28.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 806fb0973344f067716c25b2018b289158396cdb14cbac7a5064d57508b8ffd8 |
|
MD5 | 8a3590b352295ba55eb044405dee8f1f |
|
BLAKE2b-256 | 3f15280d692a8852bac3f485004afa537d8f837df18f3e7fc89665b3b2e530de |
File details
Details for the file qtrex-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: qtrex-0.1.0-py3-none-any.whl
- Upload date:
- Size: 14.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 39fcba022f67c39f549eddf874f059554a3495bcd289760aeacae11e4b368ecf |
|
MD5 | 95903f40e51f734b77863b8d86946aaf |
|
BLAKE2b-256 | abad73a9f5446299663ecb1669d92e19716228ceb21b957c1c9b9cda5342f3ee |