Integrating PRQL with dbt: A Step-by-Step Guide

Dec 21, 2023 | Programming

Welcome to the exciting world of transforming your SQL experience with dbt (data build tool) using PRQL (a new query language for analyzing data). This article will guide you on how to effectively integrate dbt-prql into your workflow, allowing you to write PRQL within dbt models, leveraging the best features of both languages while maintaining version control and testing capabilities.

What Is dbt-prql?

dbt-prql is a tool that enables the writing of PRQL queries in dbt models. This integration allows you to enjoy the power and simplicity of PRQL while benefiting from dbt’s version control and lineage testing across queries. With dbt-prql installed, dbt automatically compiles PRQL code embedded within specific tags into SQL during its compilation process.

Getting Started: Installation

To begin your journey, you’ll need to install dbt-prql. This is a straightforward process:

pip install dbt-prql

Once installed, there’s no additional configuration needed; the system is ready to transform your PRQL queries!

Writing Your First PRQL Query

Let’s dip our toes into PRQL with a simple example. Imagine you are an employee at a company, and you want to filter employees by age. Here’s how you would do it:

elm
% prql %
from employees
filter (age in 20..30)
% endprql %

This PRQL query translates to a SQL command as follows:

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

Think of it like trying to locate a specific book in a library. The PRQL query helps you filter through the vast array of options quickly, allowing you to find exactly what you need without flipping through every single page of every single book.

Complex Queries Made Easy

As you grow more comfortable with PRQL, you can create intricate queries. For example:

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

This snippet may produce a SQL query like the one below:

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

Here, think of PRQL as a skilled chef who can effortlessly prepare a complex dish with various ingredients, making it simpler and more efficient than writing out the long recipe (SQL) step by step.

Replacing Macros for Clean Code

dbt’s use of macros simplifies code significantly, but it can also lead to unnecessary complexity. Here’s how you might replace a common dbt macro approach with PRQL:

elm
% prql %
func filter_amount method =
    sum(case when payment_method = method then amount end) as method_amount
from ref(raw_payments)
group order_id (aggregate [
    filter_amount bank_transfer,
    filter_amount credit_card,
    filter_amount gift_card,
])
% endprql %

This PRQL model gives you an easier and cleaner way to achieve your goals, emphasizing simplicity in coding while also providing real-time feedback on any errors encountered as you type.

Troubleshooting Tips

While using dbt-prql, you may encounter some bumps along the way. Here are a few troubleshooting ideas:

  • If you notice the PRQL isn’t compiling after installation, ensure you’ve wrapped your PRQL code correctly within % prql % and % endprql % tags.
  • For any persistent issues, try setting the environment variable DBT_PRQL_DISABLE=1, which will bypass the library to check if it is causing the problem.
  • Remember, if you encounter any unresolved issues, you can always check the GitHub Issues page for support and advice.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox