Jinja and Macros Flashcards

(27 cards)

1
Q

What is Jinja in the context of dbt?

A

Jinja is a templating language used within dbt to add logic and reusability to SQL. It allows for control structures, macros, and dynamic expressions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do Jinja expressions, statements, and comments differ?

A

Expressions {{ ... }} output strings or values, statements {% ... %} control flow without output, and comments {# ... #} are ignored in compiled output.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Where can Jinja be used in a dbt project?

A

In models, analyses, tests, hooks, and macros — essentially anywhere SQL is used.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How can you check what SQL Jinja compiles to in dbt?

A

Use the ‘Compile’ button in dbt Cloud, or run dbt compile in dbt Core and view the compiled SQL in the target/compiled/ directory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the ref() function represent in dbt and why is it significant?

A

It’s a Jinja macro that resolves to the name of a model or seed, enabling dependency tracking and environment-agnostic SQL.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a macro in dbt?

A

A macro is a reusable piece of Jinja code (like a function) defined in .sql files, typically in the macros/ directory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Give an example use-case for a macro in dbt.

A

A macro like cents_to_dollars(column_name) converts cents to dollars and can be reused across multiple models.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you define a macro in dbt?

A

Use {% macro macro_name(args) %} and {% endmacro %} inside a .sql file, usually located in the macros/ directory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the following macro do?\n{% macro cents_to_dollars(column_name, scale=2) %} ({{ column_name }} / 100)::numeric(16, {{ scale }}) {% endmacro %}

A

It converts a value in cents to dollars, with a customizable numeric scale for precision.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is whitespace control in Jinja and why is it useful?

A

Whitespace control (using - in delimiters like {%-) helps produce cleaner, more readable compiled SQL by removing unnecessary whitespace.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the benefit of using environment variables in Jinja within dbt?

A

They allow configuration changes (e.g., credentials or target environments) without modifying source code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the following Jinja statement do in a model?\n{% for payment_method in payment_methods %}

A

It loops over each item in payment_methods to dynamically generate SQL code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can Jinja be used to dynamically generate pivoted columns in SQL?

A

By looping through a list of values and writing conditional aggregations with different column aliases.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Can Jinja be used to modify the way a dbt project builds based on the environment?

A

Yes, by referencing target.name or other context variables, Jinja can modify SQL or logic depending on the environment.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you use a macro from an installed dbt package?

A

Prefix the macro with the package name, e.g., {{ dbt_utils.dimensions(5) }}.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How can you qualify a macro within your own dbt project?

A

Prefix the macro with your project name if needed, mostly useful for package authors.

17
Q

What are the benefits of using Jinja macros for repeated logic in models?

A

They reduce code duplication, improve consistency, and simplify maintenance by centralizing logic.

18
Q

How do you use a macro from an external dbt package like dbt-utils?

A

You call the macro using the package name as a prefix, e.g., {{ dbt_utils.dimensions(5) }}.

19
Q

What is the purpose of qualifying a macro with a package name?

A

It helps avoid naming collisions and clarifies which package the macro originates from.

20
Q

When might you qualify a macro with your own project name?

A

Mostly useful for package authors or advanced use cases to disambiguate macro origins.

21
Q

What is ‘dbtonic’ Jinja code?

A

It refers to well-structured and readable dbt code that uses Jinja appropriately — balancing abstraction with clarity.

22
Q

What is a key principle of writing dbtonic code?

A

Favor readability over strict DRY-ness. Not all repeated lines need to be turned into macros.

23
Q

What is the recommended way to set variables in Jinja for dbt models?

A

Set them at the top of the model using {% set ... %} for better readability and reusability.

24
Q

Why should you avoid setting Jinja variables inline in loops or SQL?

A

It reduces readability and makes code harder to maintain, especially as complexity grows.

25
What should you check before writing your own macro?
Look in existing packages like dbt-utils to avoid duplicating effort with existing, tested solutions.
26
What does `{{ dbt_utils.dimensions(5) }}` do?
It expands to a list of five `field_` columns (field_1, field_2, etc.) in a SELECT clause.
27
How can you reduce whitespace in compiled SQL when using Jinja?
Use Jinja's whitespace control syntax, like `{%- ... -%}`, to strip extra spaces and lines.