Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can't rename views in Azure Synapse Serverless #3

Open
dataders opened this issue Sep 8, 2021 · 1 comment
Open

can't rename views in Azure Synapse Serverless #3

dataders opened this issue Sep 8, 2021 · 1 comment

Comments

@dataders
Copy link
Contributor

dataders commented Sep 8, 2021

background

In Azure Synapse serverless you cannot:

  • materialize tables only external tables and views, and
  • rename views (because sp_rename and RENAME is not supported)

You can, however, do this for dedicated SQL pools, which is what most teams use.

question

@jtcohen6 is it possible to use a project-level boolean variable that will conditionally switch between using the default view materialization and the custom one below?

In my head I'm envisioning a custom materialization that would conditionally dispatch to either the dbt-core's default view materialization and this hacky, drop-and-recreate view materialization below based on a is_serverless variable that defaults to False.

-- override for view.sql

{% materialization view, adapter='synapse' %}
  {%- set identifier = model['alias'] -%}
  {%- set old_relation = adapter.get_relation(database=database,
                                              schema=schema,
                                              identifier=identifier) -%}
  {%- set target_relation = api.Relation.create(database=database,
                                                schema=schema,
                                                identifier=identifier,
                                                type='view') -%}
  {%- set exists_as_table = (old_relation is not none and old_relation.is_table) -%}
  {%- set exists_as_view = (old_relation is not none and old_relation.is_view) -%}

  {{ run_hooks(pre_hooks) }}

  {% if old_relation is not none %}
    {% do adapter.drop_relation(old_relation) %}
  {% endif %}

  -- build model
  {% call statement('main') -%}
    {{ create_view_as(target_relation, sql) }}
  {%- endcall %}
  {{ run_hooks(post_hooks) }}

  {% do persist_docs(target_relation, model) %}
  {{ return({'relations': [target_relation]}) }}
{% endmaterialization %}
@jtcohen6
Copy link

jtcohen6 commented Sep 9, 2021

@swanderz All properties of the currently active connection profile are available from the target context variable.

I also believe it's possible to "call" a materialization as a macro, and thereby avoid duplicating logic. dbt turns materializations into macros with names like materialization_view_default. That's not exactly documented, though, or guaranteed to continue working the same in the future.

For clarity, anyways, I'd lean toward including both logical paths, with clear branching logic:

{% materialization view, adapter='synapse' %}
     ... common setup ...
  {% if target.is_serverless %}
     ... hacky drop and recreate ...
  {% else %}
     ... standard rename-swap-drop ...
{% endmaterialization %}

@dataders dataders transferred this issue from microsoft/dbt-synapse Aug 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants