Skip to main content

Write PRQL in dbt models

Project description

dbt-prql

dbt-prql allows writing PRQL in dbt models.

Trivial Example:

{% prql %}
from employees
filter (age | in 20..30)
{% endprql %}

...compiles to:

SELECT
  employees.*
FROM
  employees
WHERE
  age BETWEEN 20
  AND 30

More complex example:

{% prql %}
from {{ source('salesforce', 'in_process') }}
derive expected_sales = probability * value
join {{ ref('team', 'team_sales') }} [name]
group name (
    aggregate (expected_sales)
)
{% endprql %}

...compiles to:

SELECT
  name,
  {{ source('salesforce', 'in_process') }}.probability * {{ source('salesforce', 'in_process') }}.value AS expected_sales
FROM
  {{ source('salesforce', 'in_process') }}
  JOIN {{ ref('team', 'team_sales') }} USING(name)
GROUP BY
  name

Functionality

  • Any text between {% prql %} and {% endprql %} tags will be compiled from PRQL to SQL.
  • Any text within the PRQL query that's surrounded by {{...}} will be passed through to dbt parser without modification.

Installation

pip install dbt-prql

Current state

Currently this in an early state. But it's enthusiastically supported — if there are any problems, please open an issue.

Note that we need to release a new pyprql version for this to pass jinja expressions through, which we'll do in the next couple of days.

Is this magic?

It's much worse.

Unfortunately, it's not possible to add behavior to dbt beyond the database adapters (e.g. dbt-bigquery) or jinja-only plugins (e.g. dbt-utils). So this library hacks the python import system to monkeypatch dbt's jinja environment with an additional jinja extension, which avoids the need for any changes to dbt.

Thanks to mtkennerly/poetry-dynamic-versioning for the technique.

This isn't stable between dbt versions, since it relies on internal dbt APIs. The technique is also normatively bad — it runs a few lines of code every time the python interpreter starts — whose errors could lead to very confusing bugs beyond the domain of the problem (though in the case of this code, it's small and well-constructed™).

If there's ever any concern that the library might be causing a problem, just set an environment variable DBT_PRQL_DISABLE=1, and this library won't monkeypatch anything. It's also fully uninstallable with pip uninstall dbt-prql.

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

dbt-prql-0.1.0.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dbt_prql-0.1.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file dbt-prql-0.1.0.tar.gz.

File metadata

  • Download URL: dbt-prql-0.1.0.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.13

File hashes

Hashes for dbt-prql-0.1.0.tar.gz
Algorithm Hash digest
SHA256 59b8aa1ae5945995d28c37628f5b74d7173a3c756aaee12a436b8e76bb3ba08a
MD5 7f05b752383f25a019aa9874b98b9ba9
BLAKE2b-256 4af20f320b2a3d9f72c47219afa943292f7b07539fbe61abe7ff4d429762c195

See more details on using hashes here.

File details

Details for the file dbt_prql-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: dbt_prql-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.13

File hashes

Hashes for dbt_prql-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 11b2797b0bfb8875a2c6187e21816dd0c146de88afa5330533ee0d6c246f67e6
MD5 c7b5d29d1150988f2e9cfb79b0135b7b
BLAKE2b-256 70f84d0d77d3c72d75be043ec0031e8ace1b3abe91b742c00d0193962f5fafb2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page