relations.sql 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. -- Due to a PostgreSQL compatibility issue in processing aliases in the GROUP BY
  17. -- clause, we must override this macro.
  18. -- See: https://github.com/dbt-labs/dbt-postgres/blob/main/dbt/include/postgres/macros/relations.sql
  19. {% macro materialize__get_relations() -%}
  20. {%- call statement('relations', fetch_result=True) -%}
  21. with relation as (
  22. select
  23. pg_rewrite.ev_class as class,
  24. pg_rewrite.oid as id
  25. from pg_rewrite
  26. ),
  27. class as (
  28. select
  29. oid as id,
  30. relname as name,
  31. relnamespace as schema,
  32. relkind as kind
  33. from pg_class
  34. ),
  35. dependency as (
  36. select distinct
  37. objid as id,
  38. refobjid as ref
  39. from pg_depend
  40. ),
  41. schema as (
  42. select
  43. oid as id,
  44. nspname as name
  45. from pg_namespace
  46. where nspname != 'information_schema' and nspname not like 'pg\_%'
  47. ),
  48. referenced as (
  49. select
  50. relation.id AS id,
  51. referenced_class.name,
  52. referenced_class.schema,
  53. referenced_class.kind
  54. from relation
  55. join class as referenced_class on relation.class=referenced_class.id
  56. where referenced_class.kind in ('r', 'v', 'm')
  57. ),
  58. relationships as (
  59. select
  60. referenced.name as referenced_name,
  61. referenced.schema as referenced_schema_id,
  62. dependent_class.name as dependent_name,
  63. dependent_class.schema as dependent_schema_id,
  64. referenced.kind as kind
  65. from referenced
  66. join dependency on referenced.id=dependency.id
  67. join class as dependent_class on dependency.ref=dependent_class.id
  68. where
  69. (referenced.name != dependent_class.name or
  70. referenced.schema != dependent_class.schema)
  71. )
  72. select
  73. referenced_schema.name as referenced_schema,
  74. relationships.referenced_name as referenced_name,
  75. dependent_schema.name as dependent_schema,
  76. relationships.dependent_name as dependent_name
  77. from relationships
  78. join schema as dependent_schema on relationships.dependent_schema_id=dependent_schema.id
  79. join schema as referenced_schema on relationships.referenced_schema_id=referenced_schema.id
  80. group by referenced_schema.name, referenced_name, dependent_schema.name, dependent_name
  81. order by referenced_schema, referenced_name, dependent_schema, dependent_name;
  82. {%- endcall -%}
  83. {{ return(load_result('relations').table) }}
  84. {% endmacro %}
  85. {% macro materialize_get_relations() %}
  86. {{ return(materialize__get_relations()) }}
  87. {% endmacro %}