SQL Interpolation
When using parameters inside SQL queries, hibase provides extended interpolation syntax beyond the standard {{ $param_name }} curly braces. This section covers all the ways parameters can be injected into SQL queries, including how NULL values are handled.
Regular Interpolation
The simplest form — reference a parameter by name inside double curly braces:
SELECT *
FROM my_table
WHERE created_at >= {{ $last_created }}
OR updated_at >= {{ $last_updated }}
Given these parameters:
{
"last_created": "2018-07-28 12:10:35",
"last_updated": "2018-07-29 18:42:21"
}
The query evaluates to:
SELECT *
FROM my_table
WHERE created_at >= '2018-07-28 12:10:35'
OR updated_at >= '2018-07-29 18:42:21'
Values are automatically quoted according to their type (strings get single quotes, numbers don't, etc.).
NULL Values and SQL-Aware Interpolation
Regular interpolation breaks when a parameter is NULL — the expression WHERE created_at >= NULL is not valid SQL.
To handle this, include the SQL operator inside the curly braces. This tells hibase to nullify the entire clause when the parameter is NULL:
SELECT *
FROM my_table
{{ WHERE created_at >= $last_created }}
{{ OR updated_at >= $last_updated }}
When parameters are NULL, this evaluates to:
SELECT *
FROM my_table
WHERE true
OR false
Each SQL operator is nullified differently to preserve the logical structure of your query:
| SQL Snippet | With NULL param evaluates to |
|---|---|
{{ WHERE [...] }} |
WHERE true |
{{ AND [...] }} |
AND true |
{{ OR [...] }} |
OR false |
{{ GROUP BY [...] }} |
(removed entirely) |
{{ ORDER BY [...] }} |
(removed entirely) |
NULL Values with Comparison Operators
When using regular interpolation (parameter outside the operator clause), hibase intelligently converts comparison operators for NULL values:
| 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 |
Choosing the Right Syntax
The two approaches have different meanings. Given "category_id": null:
SQL-aware syntax — nullifies the entire condition:
SELECT * FROM my_table
{{ WHERE category_id = $category_id }}
-- evaluates to: WHERE true (returns all rows)
Regular syntax — converts to IS NULL:
SELECT * FROM my_table
WHERE category_id = {{ $category_id }}
-- evaluates to: WHERE category_id IS NULL (returns only NULL rows)
Choose based on your intent:
- Use SQL-aware syntax (
{{ WHERE ... }}) when aNULLparameter means "skip this filter" — the most common case for incremental loading where the first run has no high-water mark yet - Use regular syntax when a
NULLparameter is a meaningful value you want to match
Raw Value Injection
By default, parameters are cast according to their type before injection (strings are quoted, etc.). Sometimes you need to inject a value without any casting — for example, when interpolating table names, column names, or schema names.
Use the raw_value() function to bypass type casting:
Given these parameters:
{
"schema": "public",
"table": "products",
"column": "category",
"value": "fashion"
}
And this query:
SELECT id, {{ raw_value($column) }}
FROM `{{ raw_value($schema) }}`.`{{ raw_value($table) }}`
{{ WHERE raw_value($column) = $value }}
The output is:
SELECT id, category
FROM `public`.`products`
WHERE category = 'fashion'
Notice that raw_value($column) injects category without quotes, while $value is quoted as 'fashion' because it uses regular interpolation.
raw_value() is only available in SQL query interpolation. Common uses:
- Dynamic table names:
FROM {{ raw_value($target_table) }} - Dynamic column names:
SELECT {{ raw_value($column) }} - Dynamic schema names:
FROM {{ raw_value($schema) }}.my_table - Dynamic SQL fragments:
ORDER BY {{ raw_value($sort_column) }} {{ raw_value($sort_direction) }}