Skip to content

Commit 0a05536

Browse files
committed
get_user_creation_query: include metadata permissions for dremio
1 parent 75dd8a8 commit 0a05536

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

macros/utils/cross_db_utils/get_user_creation_query.sql

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,57 @@ grant create table on {{ parameters["schema"] }}.* to {{ parameters["user"] }}
107107

108108

109109
{% macro dremio__get_user_creation_query(parameters) %}
110+
{% set dremio_dbs = elementary.get_dremio_databases() %}
111+
112+
-- Create dremio user
110113
CREATE USER "{{ parameters["user"] }}";
111114

115+
-- General usage permissions
112116
GRANT USAGE ON PROJECT TO USER "{{ parameters["user"] }}";
117+
118+
-- Read permissions on elementary schema
113119
GRANT SELECT ON ALL DATASETS IN FOLDER {% for part in (parameters["object_storage"] ~ "." ~ parameters["object_storage_path"]).split(".") %}"{{ part }}"{% if not loop.last %}.{% endif %}{% endfor %} TO USER "{{ parameters["user"] }}";
120+
121+
-- Metadata permissions on all catalogs and sources (no read access)
122+
{% for db_name, db_type in dremio_dbs.items() -%}
123+
GRANT VIEW REFLECTION ON {{ db_type }} "{{ db_name }}" TO USER "{{ parameters["user"] }}";
124+
{% endfor %}
114125
{% endmacro %}
115126

116127

117128
{# Databricks, BigQuery, Spark #}
118129
{% macro default__get_user_creation_query(parameters) %}
119130
{% do exceptions.raise_compiler_error('User creation not supported through sql using ' ~ target.type) %}
120131
{% endmacro %}
132+
133+
{% macro get_dremio_databases() %}
134+
{% set dremio_databases_query %}
135+
select distinct
136+
case when lower(table_type) = 'view' then 'CATALOG' else 'SOURCE' end database_type,
137+
split_part(table_schema, '.', 1) as database_name
138+
from information_schema."TABLES"
139+
where split_part(table_schema, '.', 1) not in ('$scratch', 'INFORMATION_SCHEMA', 'sys')
140+
{% endset %}
141+
{% set configured_dbs = elementary.get_configured_databases_from_graph() | map('lower') | list %}
142+
143+
{% set db_name_to_type = {} %}
144+
{% for row in elementary.agate_to_dicts(elementary.run_query(dremio_databases_query)) %}
145+
{% set db_name_lower = row["database_name"] | lower %}
146+
147+
{# Only include dbs configured in the dbt project #}
148+
{% if db_name_lower not in configured_dbs %}
149+
{% continue %}
150+
{% endif %}
151+
152+
{# It seems that in some cases there can be tables in catalogs (spaces), even though the docs clain they
153+
should only contain views.
154+
So anyway, to be safe - if we see at least one view on the db we'll categorize it as a catalog. #}
155+
{% if db_name_lower in db_name_to_type and row["database_type"] != "CATALOG" %}
156+
{% continue %}
157+
{% endif %}
158+
159+
{% do db_name_to_type.update({row["database_name"]: row["database_type"]}) %}
160+
{% endfor %}
161+
162+
{% do return(db_name_to_type) %}
163+
{% endmacro %}

0 commit comments

Comments
 (0)