table.sql 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. -- Copyright 2020 Josh Wills. All rights reserved.
  2. -- Copyright Materialize, Inc. and contributors. All rights reserved.
  3. --
  4. -- Licensed under the Apache License, Version 2.0 (the "License");
  5. -- you may not use this file except in compliance with the License.
  6. -- You may obtain a copy of the License in the LICENSE file at the
  7. -- root of this repository, or online at
  8. --
  9. -- http://www.apache.org/licenses/LICENSE-2.0
  10. --
  11. -- Unless required by applicable law or agreed to in writing, software
  12. -- distributed under the License is distributed on an "AS IS" BASIS,
  13. -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. -- See the License for the specific language governing permissions and
  15. -- limitations under the License.
  16. {% materialization table, adapter='materialize' %}
  17. {%- set identifier = model['alias'] -%}
  18. {%- set old_relation = adapter.get_relation(identifier=identifier,
  19. schema=schema,
  20. database=database) -%}
  21. {%- set target_relation = api.Relation.create(identifier=identifier,
  22. schema=schema,
  23. database=database,
  24. type='materialized_view') -%}
  25. {% if old_relation %}
  26. {{ adapter.drop_relation(old_relation) }}
  27. {% endif %}
  28. {{ run_hooks(pre_hooks, inside_transaction=False) }}
  29. {{ run_hooks(pre_hooks, inside_transaction=True) }}
  30. {% call statement('main') -%}
  31. -- Creates a materialized view, not a table, in Materialize
  32. {{ materialize__create_materialized_view_as(target_relation, sql) }}
  33. {%- endcall %}
  34. {{ create_indexes(target_relation) }}
  35. {% do persist_docs(target_relation, model) %}
  36. {{ run_hooks(post_hooks, inside_transaction=False) }}
  37. {{ run_hooks(post_hooks, inside_transaction=True) }}
  38. {{ return({'relations': [target_relation]}) }}
  39. {% endmaterialization %}