You can define parameters (or params in short) at different levels in your ETL Workflows and use them to make your process more smart, automated and reusable.
A parameters set is a JSON object that is made available to your Data Source query, so you can inject values into it before it gets executed.
For example, you might define the following parameters set within one of your steps...
1 2 3 4{ "date_start": "{{ $today - 7 }}", "date_end": "{{ $today }}" }
... and use its values into an SQL query by writing something along the lines of:
1 2 3 4SELECT * FROM my_table WHERE timestamp >= {{ $date_start }} AND timestamp <= {{ $date_end }}
Values from your params will be injected into your SQL query before this is executed. In the example above, the resulting query will effectively be selecting only records which have a timestamp value within the past seven days.
Note how the params defined above (date_start and date_end) are injected into the SQL Query below, where they are referenced by their name.
There are a few gotchas you should be aware of when using parameters within ETL Workflows. These docs cover everything you need to know to get started on the right track and avoid common pitfalls.
The parameters set which is passed on to your queries is a JSON object which you can compose by defining params settings at different levels:
Furthermore, within steps you can define parameters both as a JSON string (JSON params) and by running an SQL Query onto one of your connected databases (Query params).
In addition to what you can define yourself, a set of Default params is automatically made available to you. Check out the Default Parameters docs to see what is included by default.
The final parameters set - which your queries will have access to - is composed by running through these settings sequentially, from Workflow params to Query params:
| Settings | Priority (4 = lowest, 1 = highest). |
|---|---|
| Default Params | 4 |
| Workflow Params | 3 |
| Step JSON Params | 2 |
| Step Query Params | 1 |
Params from all of these settings are merged together into a final parameters set, which will be available to your data source query.
NB: What comes later in the sequence can potentially override what came earlier, if any of the param keys overlap. For example, if you would write the following into your Workflow params...
1 2 3 4{ "key1": "workflow value 1", "key2": "workflow value 2" }
... and the following Step JSON params in a given Step within the same workflow:
1 2 3{ "key1": "step value 1" }
..., your final parameters set will be:
1 2 3 4{ "key1": "step value 1", "key2": "workflow value 2" }
As you can see, the Step took precedence over the Workflow - as one of the defined keys (key1) was conflicting. The resulting parameters set is a merge of Workflow and Step params, where Step params take precedence over Workflow params.
Given params are defined hierarchically (see above), params settings which rank higher (e.g. Step params) do have access to params defined within settings that rank lower (e.g. Workflow params).
You can therefore reference params across different settings levels, as long as they are defined at levels with a lower priority.
For example, if you would define the following within your Workflow params for a given step within the workflow...
1 2 3{ "category_id": 42 }
... you could then make use of this information within your Step Query params:
1 2 3SELECT max(created_at) AS last_created FROM products WHERE category_id = {{ $category_id }}
This will correctly evaluate to ... WHERE category_id = 42, because the category_id parameter was defined prior to your Step Query params.
To make it explicit, this matrix illustrates which parameter (vertical axis) have access to which (horizontal axis):
| Settings | Default Params | Workflow Params | Step JSON Params | Step Query Params |
|---|---|---|---|---|
| Default Params | - | No | No | No |
| Workflow Params | Yes | - | No | No |
| Step JSON Params | Yes | Yes | - | No |
| Step Query Params | Yes | Yes | Yes | - |
The difference between Workflow and Step parameters is not only in their relative priority:
Workflow parameters are calculated only once, when the entire workflow starts, and passed on to every single Step without ever being recalculated. Accordingly, Step parameters are only calculated when a step starts and they are never passed on to other steps.
This is important, for example, when working with timestamps. Defining a param as {{ $now }} within your Workflow params will ensure all steps will get the same value for that param. Moving that param definition to Step params, on the other hand, will recalculate the param for each and every one of your steps.
If it is important to your workflow's logic that a parameter is set once and shared among steps, define it at the Workflow level.
By default, the parameters set passed to your data source query will include the following data:
1 2 3 4 5{ "step_target": { "column_names": ... // An ordered list of column names of the target table in the step } }
Within your workflow's steps, you can fetch parameters through an SQL query which is run before the data source query.
This query must return a single row. Its result will be converted to JSON (where column names will be keys and values will be values) and added to the final parameters set that will be passed to your data source query.
For example, the following query...
1 2 3SELECT max(created_at) AS last_created, max(updated_at) AS last_updated FROM my_target_table
... will add the following params to your parameters set (values are provided as an example):
1 2 3 4{ "last_created": "2018-07-28 12:10:35", "last_updated": "2018-07-29 18:42:21" }
You will then be able to reference those parameters as you would do with any other within your data source query, for example:
1 2 3 4SELECT * FROM my_table WHERE created_at >= {{ $last_created }} OR updated_at >= {{ $last_updated }}
The query above illustrates the concept fine, but it does have some problems. Keep on reading to learn how to properly handle params within SQL queries.
The syntax for interpolating params within SQL queries is a slightly extended version of the ubiquitous hibase syntax with double curly braces.
Given a set of parameters...
1 2 3 4{ "last_created": "2018-07-28 12:10:35", "last_updated": "2018-07-29 18:42:21" }
..., your first option is to interpolate their values within an SQL Query as you would normally do elsewhere:
1 2 3 4SELECT * FROM my_table WHERE created_at >= {{ $last_created }} OR updated_at >= {{ $last_updated }}
This will interpolate the values of your params as you would expect:
1 2 3 4SELECT * FROM my_table WHERE created_at >= '2018-07-28 12:10:35' OR updated_at >= '2018-07-29 18:42:21'
So far so good. But what if any one of those params is null?
1 2 3 4{ "last_created": null, "last_updated": null }
These params would generate a broken SQL Query if injected directly (WHERE created_at >= NULL is not valid SQL).
This is why you have a second option when it comes to SQL Queries. By including the SQL operator (WHERE / AND / OR) into the double curly braces, you tell hibase that you want to nullify that condition in case the exposed parameter should be null:
1 2 3 4SELECT * FROM my_table {{ WHERE created_at >= $last_created }} {{ OR updated_at >= $last_updated }}
In case of null parameters, this new query will evaluate to the following:
1 2 3 4SELECT * FROM my_table WHERE true OR false
As you can see, different SQL operators are nullified in different ways, in order to keep the implicit logic of your SQL query intact:
| SQL Snippet | With NULL param evaluates to |
|---|---|
{{ WHERE [...] }} | "WHERE true" |
{{ AND [...] }} | "AND true" |
{{ OR [...] }} | "OR false" |
{{ GROUP BY [...] }} | "" (empty string) |
{{ ORDER BY [...] }} | "" (empty string) |
NB: You don't always want to decide which syntax to use on the base of whether a param can be NULL or not.
Sometimes, a NULL value for a parameter is perfectly valid. For example, given this parameters set and SQL Query...
1 2 3{ "category_id": null }
1 2 3SELECT * FROM my_table WHERE category_id = {{ $category_id }}
..., you might want the SQL statement to evaluate to WHERE column IS NULL, rather than WHERE column = NULL or WHERE true.
hibase takes care of replacing comparison operators for you in case the logic of the query can be inferred. Here is what you can expect hibase to do based on the syntax you use, when a given parameter is NULL:
| SQL Snippet | With NULL param evaluates to |
|---|---|
WHERE column = {{ $param }} | WHERE column IS NULL |
WHERE column <> {{ $param }} | WHERE column IS NOT NULL |
WHERE column != {{ $param }} | WHERE column IS NOT NULL |
WHERE column > {{ $param }} | WHERE column IS NOT NULL |
Ultimately, both approaches will be valid in the example above but they will have different meanings:
1 2 3SELECT * FROM my_table {{ WHERE category_id = $category_id }}
will evaluate to ... WHERE true, whereas:
1 2 3SELECT * FROM my_table WHERE category_id = {{ $category_id }}
will evaluate to ... WHERE category_id IS NULL.
Decide which syntax to use according to your final goal and to the logic of your query.
The primary goal of parameters within SQL Queries is to inject values into the query itself. A plain parameter will be therefore casted according to its type before being interpolated within the query.
Sometimes you might want to have better control over how a parameter should be casted. The most common case for this is wanting to interpolate table or column names from parameters themselves.
hibase lets you take this control back through the raw_value function, which is only available in the context of SQL queries interpolation.
By wrapping parameters into the raw_value function, no casting will be performed before interpolation. As always, an example is worth more than a thousand words. Given the following set of parameters:
1 2 3 4 5 6{ "schema": "public", "table": "products", "column": "category", "value": "fashion" }
and the following parametric SQL Query:
1 2 3SELECT id, {{ raw_value($column) }} FROM `{{ raw_value($schema) }}`.`{{ raw_value($table) }}` {{ WHERE raw_value($column) = $value }}
... you can expect this final query as an output:
1 2 3SELECT id, category FROM `public`.`products` WHERE category = 'fashion'