Skip to main content

Python bindings for prqlc, the PRQL compiler

Project description

PRQL - Python Bindings

Python bindings for PRQL, the Pipelined Relational Query Language.

PRQL is a modern language for transforming data — a simple, powerful, pipelined SQL replacement. Like SQL, it's readable, explicit and declarative. Unlike SQL, it forms a logical pipeline of transformations, and supports abstractions such as variables and functions. It can be used with any database that uses SQL, since it compiles to SQL.

PRQL can be as simple as:

from tracks
filter artist == "Bob Marley"     # Each line transforms the previous result
aggregate {                       # `aggregate` reduces each column to a value
  plays    = sum plays,
  longest  = max length,
  shortest = min length,          # Trailing commas are allowed
}

Installation

pip install prqlc

Usage

Basic usage:

import prqlc

prql_query = """
    from employees
    join salaries (==emp_id)
    group {employees.dept_id, employees.gender} (
      aggregate {
        avg_salary = average salaries.salary
      }
    )
"""

options = prqlc.CompileOptions(
    format=True, signature_comment=True, target="sql.postgres"
)

sql = prqlc.compile(prql_query)
sql_postgres = prqlc.compile(prql_query, options)

The following functions and classes are exposed:

def compile(prql_query: str, options: Optional[CompileOptions] = None) -> str:
    """Compiles a PRQL query into SQL."""
    ...

def prql_to_pl(prql_query: str) -> str:
    """Converts a PRQL query to PL AST in JSON format."""
    ...

def pl_to_prql(pl_json: str) -> str:
    """Converts PL AST as a JSON string into a formatted PRQL string."""
    ...

def pl_to_rq(pl_json: str) -> str:
    """Resolves and lowers PL AST (JSON) into RQ AST (JSON)."""
    ...

def rq_to_sql(rq_json: str, options: Optional[CompileOptions] = None) -> str:
    """Converts RQ AST (JSON) into a SQL query."""
    ...

class CompileOptions:
    def __init__(
        self,
        *,
        format: bool = True,
        target: str = "sql.any",
        signature_comment: bool = True,
    ) -> None:
    """Compilation options for SQL backend of the compiler.

    Args:
        format (bool): Pass generated SQL string through a formatter that splits
            it into multiple lines and prettifies indentation and spacing.
            Defaults to True.
        target (str): Target dialect to compile to. Defaults to "sql.any", which
            uses the 'target' argument from the query header to determine the
            SQL dialect. Other targets are available by calling the `get_targets`
            function.
        signature_comment (bool): Emits the compiler signature as a comment after
            the generated SQL. Defaults to True.

    """
    ...

def get_targets() -> list[str]:
    """List available target dialects for compilation."""
    ...

Debugging functions

The following functions are available within the prqlc.debug module. They are for experimental purposes and may be unstable.

def prql_lineage(prql_query: str) -> str:
    """Computes a column-level lineage graph from a PRQL query.

    Returns JSON-formatted string. See the docs for the `prqlc debug lineage`
    CLI command for more details.
    """
    ...

def pl_to_lineage(pl_json: str) -> str:
    """Computes a column-level lineage graph from PL AST (JSON)."""
    ...

Notes

These bindings are in a crate named prqlc-python and published to a Python package on PyPI named prqlc, available at https://pypi.org/project/prqlc. This crate is not published to crates.io.

The package is consumed by pyprql & dbt-prql.

Relies on pyo3 for all the magic.

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

prqlc-0.13.4.tar.gz (501.5 kB view details)

Uploaded Source

Built Distributions

prqlc-0.13.4-cp37-abi3-win_amd64.whl (2.4 MB view details)

Uploaded CPython 3.7+Windows x86-64

prqlc-0.13.4-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.7+manylinux: glibc 2.17+ x86-64

prqlc-0.13.4-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.5 MB view details)

Uploaded CPython 3.7+manylinux: glibc 2.17+ ARM64

prqlc-0.13.4-cp37-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (5.5 MB view details)

Uploaded CPython 3.7+macOS 10.12+ universal2 (ARM64, x86-64)macOS 10.12+ x86-64macOS 11.0+ ARM64

File details

Details for the file prqlc-0.13.4.tar.gz.

File metadata

  • Download URL: prqlc-0.13.4.tar.gz
  • Upload date:
  • Size: 501.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.8.3

File hashes

Hashes for prqlc-0.13.4.tar.gz
Algorithm Hash digest
SHA256 6f91152a22c692d7ea471229fac981615e2b45ed97c148d17000a3b7875d8a22
MD5 23432a9336477433a5233fdcf92513bb
BLAKE2b-256 d95aca8f53dc079c8a2173de196b878ecbdf33e420b6666646dfa8aa52abbb6f

See more details on using hashes here.

File details

Details for the file prqlc-0.13.4-cp37-abi3-win_amd64.whl.

File metadata

  • Download URL: prqlc-0.13.4-cp37-abi3-win_amd64.whl
  • Upload date:
  • Size: 2.4 MB
  • Tags: CPython 3.7+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.8.3

File hashes

Hashes for prqlc-0.13.4-cp37-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 c7cb17fe068df453ab2f358ecba7e7f47c62dd44bf37efe809b14d574e70c772
MD5 e5c1f5a077e91cec66ddea6dbe1323c2
BLAKE2b-256 ddf0f3dfa86a0dbaea7b23bb338cb5eb5b8e867332719a70e413cf2557baf50b

See more details on using hashes here.

File details

Details for the file prqlc-0.13.4-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for prqlc-0.13.4-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d4a1dd3dee1efd49c47e9766f87f08b994b50171361966ce41e91a4abec8da25
MD5 7c2653ead15de1062f9d9cb6521d05b3
BLAKE2b-256 8976028cf03dec1aee9e33fac38c4e4686ce63637bcf5a181a5cb1a65790a5c2

See more details on using hashes here.

File details

Details for the file prqlc-0.13.4-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for prqlc-0.13.4-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 17d5408abf355a2765960f6a8e7b2214758911e0bbb2e9ea735cfd5236c2289f
MD5 f35363676b0832f09581d0d9cc063215
BLAKE2b-256 3a1105de14e69a40e5ed303424d523a706838b8282baeb550ce402cfc9e1ffae

See more details on using hashes here.

File details

Details for the file prqlc-0.13.4-cp37-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl.

File metadata

File hashes

Hashes for prqlc-0.13.4-cp37-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 e39080bf80b826c2963c55f66e78241e6675eee4b4f68a5302d689048a592946
MD5 3b36ba6dce3bd5bdba25a4751b383c8b
BLAKE2b-256 37715e8f78b3dd0a454a0dce2176b1f6c4e2fa0471947f08d4b1b2c143679c66

See more details on using hashes here.

Supported by

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