-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added user defined functions * added description for readme.md * fixed as per PR comment * formatting * fixed readme
- Loading branch information
1 parent
b2712b9
commit 615465a
Showing
4 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
macros/user_defined_functions/snowflake__user_defined_function.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
This materialization is used for creating user defined function objects. | ||
The idea behind this materialization is for ability to define CREATE user defined function statements and have dbt use the necessary logic | ||
of deploying the user defined function in a consistent manner and logic. | ||
*/ | ||
{%- materialization user_defined_function, adapter='snowflake' -%} | ||
{%- set preferred_language = config.get('preferred_language', default=SQL) -%} | ||
{%- set parameters = config.get('parameters', default='') -%} | ||
{%- set is_secure = config.get('is_secure', default=false) -%} | ||
{%- set immutable = config.get('immutable', default=false) -%} | ||
|
||
{%- set sdk_version = config.get('sdk_version', default=null) -%} | ||
{%- set import_Path = config.get('import_Path', default=null) -%} | ||
{%- set packages = config.get('packages', default=null) -%} | ||
{%- set handler_name = config.get('handler_name', default=null) -%} | ||
{%- set imports = config.get('imports', default=null) -%} | ||
{%- set target_path = config.get('target_path', default=null) -%} | ||
{%- set runtime_version = config.get('runtime_version', default=null) -%} | ||
|
||
{%- set identifier = config.get('override_name', default=model['alias'] ) -%} | ||
{%- set return_type = config.get('return_type', default='varchar' ) -%} | ||
|
||
|
||
{%- set target_relation = api.Relation.create( identifier=identifier, schema=schema, database=database) -%} | ||
|
||
{%- set has_transactional_hooks = (hooks | selectattr('transaction', 'equalto', True) | list | length) > 0 %} | ||
|
||
-- setup | ||
{{ run_hooks(pre_hooks, inside_transaction=False) }} | ||
|
||
-- BEGIN happens here: | ||
{{ run_hooks(pre_hooks, inside_transaction=True) }} | ||
|
||
-------------------------------------------------------------------------------------------------------------------- | ||
-- build model | ||
|
||
{% call statement('main') -%} | ||
{{ dbt_dataengineers_materilizations.snowflake_create_user_defined_functions_statement(target_relation, is_secure, preferred_language, immutable, parameters, return_type, sdk_version, import_Path, packages, handler_name, imports, target_path, runtime_version, sql) }} | ||
|
||
{%- endcall %} | ||
|
||
-------------------------------------------------------------------------------------------------------------------- | ||
-- build model | ||
{{ run_hooks(post_hooks, inside_transaction=True) }} | ||
|
||
-- `COMMIT` happens here | ||
{{ adapter.commit() }} | ||
{{ run_hooks(post_hooks, inside_transaction=False) }} | ||
|
||
-- return | ||
{{ return({'relations': [target_relation]}) }} | ||
|
||
{%- endmaterialization -%} |
31 changes: 31 additions & 0 deletions
31
macros/user_defined_functions/snowflake_create_user_defined_functions_statement.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{%- macro snowflake_create_user_defined_functions_statement(relation, is_secure, preferred_language, immutable, parameters, return_type, sdk_version, import_Path, packages, handler_name, imports, target_path,runtime_version, sql) -%} | ||
|
||
{% if is_secure %} | ||
CREATE OR REPLACE SECURE FUNCTION {{ relation.include(database=(not temporary), schema=(not temporary)) }}({{ parameters }}) | ||
{% else %} | ||
CREATE OR REPLACE FUNCTION {{ relation.include(database=(not temporary), schema=(not temporary)) }}({{ parameters }}) | ||
{% endif %} | ||
RETURNS {{ return_type }} | ||
{% if preferred_language != 'sql' %} | ||
LANGUAGE {{ preferred_language }} | ||
{% endif %} | ||
|
||
{% if preferred_language == 'python' %} | ||
RUNTIME_VERSION = '{{ runtime_version }}' | ||
HANDLER = '{{ handler_name }}' | ||
PACKAGES = {{ packages }} | ||
|
||
{% elif preferred_language == 'java' %} | ||
handler = {{ handler_name }} | ||
target_path = {{ target_path }} | ||
{% endif %} | ||
|
||
AS | ||
|
||
{% if preferred_language != 'python' %} | ||
'{{ sql }}' | ||
{% else %} | ||
{{ sql }} | ||
{% endif %} | ||
; | ||
{%- endmacro -%} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
version: 2 | ||
|
||
macros: | ||
|
||
- name: snowflake_user_defined_functions | ||
docs: | ||
show: false | ||
|