relation.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. from dataclasses import dataclass
  17. from typing import Optional, Type
  18. from dbt_common.dataclass_schema import StrEnum
  19. from dbt.adapters.postgres.relation import PostgresRelation
  20. from dbt.adapters.utils import classproperty
  21. # types in ./misc/dbt-materialize need to import generic types from typing
  22. class MaterializeRelationType(StrEnum):
  23. # Built-in materialization types.
  24. Table = "table"
  25. View = "view"
  26. CTE = "cte"
  27. External = "external"
  28. MaterializedView = "materialized_view"
  29. # Materialize-specific materialization types.
  30. Source = "source"
  31. SourceTable = "source_table"
  32. Sink = "sink"
  33. # NOTE(morsapaes): dbt supports materialized views as a built-in
  34. # materialization since v1.6.0, so we deprecate the legacy materialization
  35. # name but keep it around for backwards compatibility.
  36. MaterializedViewLegacy = "materializedview"
  37. @dataclass(frozen=True, eq=False, repr=False)
  38. class MaterializeRelation(PostgresRelation):
  39. type: Optional[MaterializeRelationType] = None
  40. require_alias: bool = False
  41. # Materialize does not have a 63-character limit for relation names, unlike
  42. # PostgreSQL (see dbt-core #2727). Instead, we set 255 as the maximum
  43. # identifier length (see database-issues#6303).
  44. def relation_max_name_length(self):
  45. return 255
  46. @classproperty
  47. def get_relation_type(cls) -> Type[MaterializeRelationType]:
  48. return MaterializeRelationType
  49. @property
  50. def is_materialized_view(self) -> bool:
  51. return self.type in [
  52. MaterializeRelationType.MaterializedView,
  53. MaterializeRelationType.MaterializedViewLegacy,
  54. ]
  55. @property
  56. def is_source(self) -> bool:
  57. return self.type == MaterializeRelationType.Source
  58. @property
  59. def is_source_table(self) -> bool:
  60. return self.type == MaterializeRelationType.SourceTable
  61. @property
  62. def is_sink(self) -> bool:
  63. return self.type == MaterializeRelationType.Sink