Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:MaterializeInc Materialize MaterializeAdapter Relations

From Leeroopedia


Knowledge Sources misc/dbt-materialize/dbt/adapters/materialize/impl.py, misc/dbt-materialize/dbt/adapters/materialize/relation.py
Domains Declarative Data Modeling, Relation Type System, Catalog Introspection
Last Updated 2026-02-08

Overview

Concrete adapter and relation classes for Materialize provided by dbt-materialize, implementing relation type discrimination, catalog listing, constraint rendering, and column schema introspection.

Description

The MaterializeAdapter class extends both PostgresAdapter and SQLAdapter to provide Materialize-specific behavior for managing data models and relations. It binds together the MaterializeConnectionManager and MaterializeRelation classes, and overrides several PostgreSQL-specific methods.

MaterializeRelation is a frozen dataclass extending PostgresRelation that:

  • Carries a type field of type MaterializeRelationType, an enum with values: Table, View, CTE, External, MaterializedView, Source, SourceTable, Sink, and the legacy MaterializedViewLegacy.
  • Overrides the maximum identifier length to 255 characters (PostgreSQL limits to 63).
  • Provides boolean properties: is_materialized_view, is_source, is_source_table, is_sink.

MaterializeAdapter provides:

  • list_relations_without_caching(): Queries the Materialize catalog to list all relations in a schema, mapping each to its MaterializeRelationType.
  • render_column_constraint(): Renders NOT NULL constraints as ASSERT NOT NULL (Materialize-specific syntax).
  • render_raw_columns_constraints(): Processes raw column constraint definitions from model configurations.
  • get_column_schema_from_query(): Creates a temporary view to introspect column names and types without executing the full query (avoids needing a valid cluster).
  • CONSTRAINT_SUPPORT: Only not_null is enforced; check, unique, primary_key, and foreign_key are not supported.

Usage

Use this implementation when:

  • Working with Materialize-specific relation types (sources, sinks, source tables) in dbt models.
  • Understanding how dbt resolves and caches Materialize catalog metadata.
  • Debugging constraint rendering for materialized views with ASSERT NOT NULL.
  • Investigating how column type introspection works without requiring a valid cluster.

Code Reference

Source Location

File Lines Description
misc/dbt-materialize/dbt/adapters/materialize/impl.py L110-358 MaterializeAdapter class
misc/dbt-materialize/dbt/adapters/materialize/relation.py L27-43 MaterializeRelationType enum
misc/dbt-materialize/dbt/adapters/materialize/relation.py L45-78 MaterializeRelation dataclass
misc/dbt-materialize/dbt/adapters/materialize/impl.py L194-219 list_relations_without_caching()
misc/dbt-materialize/dbt/adapters/materialize/impl.py L275-339 get_column_schema_from_query()

Signature

class MaterializeRelationType(StrEnum):
    Table = "table"
    View = "view"
    CTE = "cte"
    External = "external"
    MaterializedView = "materialized_view"
    Source = "source"
    SourceTable = "source_table"
    Sink = "sink"
    MaterializedViewLegacy = "materializedview"


@dataclass(frozen=True, eq=False, repr=False)
class MaterializeRelation(PostgresRelation):
    type: Optional[MaterializeRelationType] = None
    require_alias: bool = False

    def relation_max_name_length(self) -> int: ...

    @classproperty
    def get_relation_type(cls) -> Type[MaterializeRelationType]: ...

    @property
    def is_materialized_view(self) -> bool: ...

    @property
    def is_source(self) -> bool: ...

    @property
    def is_source_table(self) -> bool: ...

    @property
    def is_sink(self) -> bool: ...


class MaterializeAdapter(PostgresAdapter, SQLAdapter):
    ConnectionManager = MaterializeConnectionManager
    Relation = MaterializeRelation
    AdapterSpecificConfigs = MaterializeConfig

    def list_relations_without_caching(
        self, schema_relation: MaterializeRelation
    ) -> List[MaterializeRelation]: ...

    @classmethod
    def render_column_constraint(
        cls, constraint: ColumnLevelConstraint
    ) -> str: ...

    def get_column_schema_from_query(
        self, sql: str
    ) -> List[PostgresColumn]: ...

Import

from dbt.adapters.materialize import MaterializeAdapter
from dbt.adapters.materialize.relation import MaterializeRelation, MaterializeRelationType

I/O Contract

Inputs

Parameter Type Description
schema_relation (for list_relations_without_caching) MaterializeRelation A relation object identifying the target schema to list relations from.
constraint (for render_column_constraint) ColumnLevelConstraint A parsed column constraint object. Only ConstraintType.not_null produces output.
sql (for get_column_schema_from_query) str A SQL query whose result column names and types are to be introspected.

Outputs

Return Type Description
(from list_relations_without_caching) List[MaterializeRelation] All relations in the specified schema, each with its correct MaterializeRelationType.
(from render_column_constraint) str The string "assert not null" for NOT NULL constraints, or "" for unsupported types.
(from get_column_schema_from_query) List[PostgresColumn] Column objects with name and type information derived from a temporary view.

Usage Examples

# Checking if a relation is a materialized view
from dbt.adapters.materialize.relation import MaterializeRelation, MaterializeRelationType

rel = MaterializeRelation.create(
    database="materialize",
    schema="public",
    identifier="my_model",
    type=MaterializeRelationType.MaterializedView,
)

assert rel.is_materialized_view is True
assert rel.is_source is False
assert rel.relation_max_name_length() == 255
-- Example dbt model (materialized view) referencing a source
{{ config(materialized='materialized_view', cluster='quickstart') }}

SELECT
    id,
    name,
    updated_at
FROM {{ source('my_source', 'events') }}
WHERE updated_at > NOW() - INTERVAL '7 days'

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment