Open geo-martino opened 1 month ago
This is because our path for executing sql does not allow executing multiple statements separated by ;
. This is a Databricks limitation, rather than a dbt limitation. I believe that even places in the Databricks UI that allow to execute multiple statements separated by ; do so by splitting them up and executing them one by one. We can keep this bug open though, as I can see this being a limitation in the grants operation is painful enough that we should rewrite the code the split and execute one by one; however, when I did this for MV/STs, it was pretty challenging, so it will probably not be fixed in the short term.
The flip side though is that you can put them into hooks as separate entries:
"on-run-start": [
"<first grant>",
"<second grant>"
]
Understood. Yes it does quite limit our ability to logically group multiple related statements together for execution into one macro for ease-of-use within config files e.g. GRANT
or ALTER
statements. It would be excessive and make the hooks config quite messy to separate out the multiple grant statements into their own entries in the hooks config. And in many cases this is not possible as the exact queries that should be executed in a given hook are dynamically generated based on the properties of the model itself.
For now, we are able to get this to work using a workaround which executes the query at compile time, returning no statements to the hook itself like below.
{% macro grant_access(table_name) -%}
{%- set group_name = "group" -%}
{%- set catalog_identifier = "`catalog_name`" -%}
{%- set schema_identifier = catalog_identifier ~ "`schema_name`" -%}
{%- set schema_identifier = schema_identifier ~ table_name -%}
{%- if execute -%}
{%- set queries %}
GRANT USE CATALOG ON CATALOG {{ catalog_identifier }} TO `{{ group_name }}`;
GRANT USE SCHEMA ON SCHEMA {{ schema_identifier }} TO `{{ group_name }}`;
GRANT SELECT ON TABLE {{ table_identifier }} TO `{{ group_name }}`;
GRANT MODIFY ON TABLE {{ table_identifier }} TO `{{ group_name }}`;
{%- endset -%}
{{ run_queries(queries) }}
{%- endif -%}
{%- endmacro %}
{% macro run_queries(queries) -%}
{% for query in (queries | trim | trim(';')).split(';') -%}
{%- do run_query(query | trim) -%}
{%- endfor %}
{%- endmacro %}
The issue here being that, as the execution happens when the macro is compiled, the logs appear to hang before executing the hook. The hook then appears to execute instantaneously as no statement was returned by the macro to execute.
We will continue to use this workaround in the meantime, but it would be great to have this looked into in the future.
Describe the bug
I am attempting to execute multiple SQL statements on various hooks and seem to be unable to execute multiple statements separated by
;
within one entry.I am generating multiple
GRANT
statements with a macro. I've rewritten an abridged version of the macro and it's usage below showing how it is used in both theon-run-start
andon-run-end
hooks in thedbt_project
file on one entry.I find that, if a macro generates multiple separate SQL statements, then the command fails. However if a macro generates just one SQL statement, the command executes with no error. I see this exact behaviour when configuring
on-run-start
,on-run-end
,pre-hook
, andpost-hook
hooks.As a workaround, I have set the macros to execute each statement when called by using the
run_query
macro within thegrant_access
macro.Steps To Reproduce
A failing macro which generates multiple SQL statements:
A successful macro which generates only one SQL statement:
dbt_project.yml
:Whenever I try to execute
dbt run
, these commands fail.Expected behavior
The statements should execute successfully. The documentation for dbt shows that it is possible to build and execute multiple statements with jinja within one entry on these config keys.
Screenshots and log output
System information
The output of
dbt --version
:The operating system you're using: MacOS Sequoia 15.0.1 (24A348)
The output of
python --version
: Python 3.12.7Additional context
I am not sure if this is an issue with the
dbt-databricks
ordbt-core
. I am posting here first as it appears that, according to thedbt-core
documentation noted above, this should be supported. My guess therefore is that the issue stems from the adapter, but I will be happy to forward this issue todbt-core
if I am wrong.