Comments (3)
Wanna give this a try?
macros/cowsay.sql
macros/cowsay.sql
{% macro cowsay(message="hi") %}
{% do log(" ________________________________________", info=True) %}
{% do log(" " ~ message, info=True) %}
{% do log(" ----------------------------------------", info=True) %}
{% do log(" \ ^__^", info=True) %}
{% do log(" \ (oo)\_______", info=True) %}
{% do log(" (__)\ )", info=True) %}
{% do log(" ||----w |", info=True) %}
{% do log(" || ||", info=True) %}
{% endmacro %}
dbt run-operation cowsay --args '{message: "Arg, I be a pirate"}'
16:08:08 ________________________________________
16:08:08 Arg, I be a pirate
16:08:08 ----------------------------------------
16:08:08 \ ^__^
16:08:08 \ (oo)\_______
16:08:08 (__)\ )
16:08:08 ||----w |
16:08:08 || ||
from dbt-core.
Since it looks like we already support this, I'm going to close this as "not planned". But just let me know if I overlooked something and we can re-open it.
from dbt-core.
@aranke I overlooked one key piece in your original message 😅
👉 the part where {{ some_macro() }}
is within the args you are passing.
I'm interpreting that the desired behavior is for the --vars
argument to accept Jinja that is rendered as a pre-step and passed to the macro after it is rendered. (I recognize that it could be interpreted the exact opposite, so please let me know if so.)
While we don't plan on adopting that behavior within run-operation
, there are a couple ways to achieve this today:
- Push the Jinja logic into the macro (recommended)
- Use interactive compilation to render your values as a pre-step
Option 1 - push the Jinja logic into the macro
Suppose you have a cowsay
macro like described in #9862 (comment).
We'd recommend refactoring the cowsay
macro so that it does not need --args
to contain any Jinja. That way, it can be called like this:
dbt run-operation cowsay
dbt run-operation cowsay --args "{suffix_type: 'standard'}"
dbt run-operation cowsay --args "{suffix_type: 'alternate'}"
Toggle for an example.
{% macro cowsay(message="hi", suffix_type=none) %}
{% set timestamp = get_timestamp_suffix(suffix_type) %}
{% do log(" ________________________________________", info=True) %}
{% do log(" " ~ timestamp ~ " " ~ message , info=True) %}
{% do log(" ----------------------------------------", info=True) %}
{% do log(" \ ^__^" , info=True) %}
{% do log(" \ (oo)\_______" , info=True) %}
{% do log(" (__)\ )" , info=True) %}
{% do log(" ||----w |" , info=True) %}
{% do log(" || ||" , info=True) %}
{% endmacro %}
{% macro get_timestamp_suffix(suffix_type=none) %}
{% if suffix_type == 'standard' %}
{% do return(run_started_at.strftime('%d-%m-%Y')) %}
{% elif suffix_type == 'alternate' %}
{% do return(run_started_at.strftime('%Y%m%d')) %}
{% else %}
{% do return("") %}
{% endif %}
{% endmacro %}
Option 2 - use interactive compilation
The following is a more complicated option. I'm not sure if there are valid use-cases for it or not, but providing it for the sake of completeness.
The general pattern is this:
- Use interactive compilation to render your Jinja
- Store the result in an environment variable
- Pass the value of the environment variable as an argument
It is a multi-step process that is dependent upon the capabilities of the shell it is operating within (bash, zsh, fsh, PowerShell, etc.), so your mileage may vary.
Here's an example that works for me in zsh
(and should work with bash
as well) as long as jq
is installed:
export message=$(dbt --log-format json compile --inline "{{ get_timestamp_suffix(suffix_type='standard') }}" --output text | jq -r 'select(.data.compiled != null) | .data.compiled')
dbt run-operation cowsay --args "{message: '$message'}"
unset message
If #9840 is implemented, then the command in the first step would be a bit more simple:
export message=$(dbt compile --inline "{{ get_timestamp_suffix(suffix_type='standard') }}" --quiet)
dbt run-operation cowsay --args "{message: '$message'}"
unset message
from dbt-core.
Related Issues (20)
- [Feature] Sync unit tests with model enabled/disabled config HOT 1
- [Feature] Support constraints independently from enforcing a full model contract HOT 12
- [Impl] Allow generic tests to accept arbitrary configs HOT 1
- [Impl] Call adapter.pre_model_hook + adapter.post_model_hook within TestTask
- Explicitly bind to localhost in docs serve
- [Bug] Tests with severity: error is not throwing error HOT 7
- [Feature] populate model['constraints'] even if the contract is not enforced
- [Bug] infinite loop in dbt core HOT 3
- Prefer existing project nodes to external nodes, even if the project node is disabled
- [Bug] Running `dbt build --resource-type saved_query` on 1.7 throws an uncaught exception HOT 1
- [Feature] Unit Tests Should Support ref & source statements when specifying rows with sql HOT 3
- Add `--host` flag to `dbt docs serve`, defaulting to '127.0.0.1' HOT 2
- [Bug] `dbt -v` doesn't know that adapters are decoupled from core HOT 1
- Warn if `updated_at` field for snapshot is not same datatype (or timezone) as what's returned in `snapshot_get_time()` HOT 2
- Improve capturing the history of records in a deleted state HOT 2
- New snapshot config to validate uniqueness before merge
- Support specifying types for recording
- [Bug] the run_query macro causes unit tests to fail with a SQL Compilation error HOT 1
- [Feature] No more jinja block for snapshots - new snapshot design
- [Feature] Emit a more specific error when there are duplicate columns in a model with an enforced contract HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dbt-core.