test.sql 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. -- Copyright Materialize, Inc. and contributors. All rights reserved.
  2. --
  3. -- Licensed under the Apache License, Version 2.0 (the "License");
  4. -- you may not use this file except in compliance with the License.
  5. -- You may obtain a copy of the License in the LICENSE file at the
  6. -- root of this repository, or online at
  7. --
  8. -- http://www.apache.org/licenses/LICENSE-2.0
  9. --
  10. -- Unless required by applicable law or agreed to in writing, software
  11. -- distributed under the License is distributed on an "AS IS" BASIS,
  12. -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. -- See the License for the specific language governing permissions and
  14. -- limitations under the License.
  15. {%- materialization test, adapter='materialize' -%}
  16. {% set relations = [] %}
  17. -- For an overview of the precedence logic behind store_failures and
  18. -- store_failures_at, see dbt-core #8653.
  19. {% if should_store_failures() %}
  20. {% set identifier = model['alias'] %}
  21. {% set old_relation = adapter.get_relation(database=database, schema=schema, identifier=identifier) %}
  22. {% set store_failures_as = config.get('store_failures_as') %}
  23. {% if store_failures_as == none %}{% set store_failures_as = 'table' %}{% endif %}
  24. {% if store_failures_as not in ['table', 'view', 'materialized_view'] %}
  25. {{ exceptions.raise_compiler_error(
  26. "'" ~ store_failures_as ~ "' is not a valid value for `store_failures_as`. "
  27. "Accepted values are: ['ephemeral', 'table', 'view', 'materialized_view']"
  28. ) }}
  29. {% endif %}
  30. {% set target_relation = api.Relation.create(
  31. identifier=identifier, schema=schema, database=database, type=store_failures_as) -%} %}
  32. {% if old_relation %}
  33. {% do adapter.drop_relation(old_relation) %}
  34. {% endif %}
  35. {% if store_failures_as == 'view' %}
  36. {% call statement(auto_begin=True) %}
  37. {{ materialize__create_view_as(target_relation, sql) }}
  38. {% endcall %}
  39. {% else %}
  40. {% call statement(auto_begin=True) %}
  41. {{ materialize__create_materialized_view_as(target_relation, sql) }}
  42. {% endcall %}
  43. {% endif %}
  44. {% do relations.append(target_relation) %}
  45. {% set main_sql %}
  46. select *
  47. from {{ target_relation }}
  48. {% endset %}
  49. {{ adapter.commit() }}
  50. {% else %}
  51. {% set main_sql = sql %}
  52. {% endif %}
  53. {% set limit = config.get('limit') %}
  54. {% set fail_calc = config.get('fail_calc') %}
  55. {% set warn_if = config.get('warn_if') %}
  56. {% set error_if = config.get('error_if') %}
  57. -- Tests compile to ad-hoc queries, which need a cluster to run against. If no
  58. -- cluster is configured for data tests, use the target cluster from
  59. -- `profiles.yml`. If none exists, fall back to the default cluster
  60. -- configured for the connected user.
  61. -- See: misc/dbt-materialize/dbt/adapters/materialize/connections.py
  62. {%- set cluster = adapter.generate_final_cluster_name(config.get('cluster', target.cluster)) %}
  63. {% call statement('main', fetch_result=True) -%}
  64. {{ get_test_sql(main_sql, fail_calc, warn_if, error_if, limit, cluster)}}
  65. {%- endcall %}
  66. {{ return({'relations': relations}) }}
  67. {%- endmaterialization -%}