Custom materializations
Write a custom materialization in Python when built-in model kinds don't fit, using the CustomMaterialization and CustomKind classes, then package and share it across projects.
Vulcan ships with model kinds that cover the most common ways to evaluate and materialize transformations. Custom materializations cover the rest.
If a built-in kind doesn't fit your use case, write a custom materialization in Python to control how your models materialize.
What is a materialization?
A materialization is the "how" behind model execution. When Vulcan runs a model, it has to get that data into your database. The materialization is the set of methods that execute your transformation logic and manage the resulting data.
Some materializations are straightforward. A FULL model kind replaces the table each time it runs, so its materialization is essentially CREATE OR REPLACE TABLE [name] AS [your query].
Others are more complex. An INCREMENTAL_BY_TIME_RANGE model figures out which time intervals to process, queries only that data, and merges it into the existing table.
The materialization logic also varies by SQL engine. PostgreSQL doesn't support CREATE OR REPLACE TABLE, so FULL models on Postgres use DROP then CREATE instead. Vulcan handles these engine-specific details for built-in model kinds; with custom materializations, you control them.
How custom materializations work
A custom materialization is like creating your own model kind. Define it in Python, name it, and reference that name in your model's MODEL block. It can accept configuration arguments from your model definition.
Every custom materialization needs:
Python code: written as a Python class.
Base class: must inherit from Vulcan's
CustomMaterializationclass.Insert method: implement the
insertmethod at minimum.Auto-loading: Vulcan discovers materializations in your
materializations/directory.
You can also:
Override other methods from
MaterializableStrategyorEngineAdapterclasses.Execute arbitrary SQL through the engine adapter.
Run Python processing with Pandas or other libraries. For most cases, put that logic in a Python model instead.
Vulcan loads any Python files in your project's materializations/ directory. Or, package your materialization as a Python package and install it like any other dependency.
Creating a custom materialization
Add a .py file to your project's materializations/ folder. Vulcan imports all Python modules in this folder when your project loads, so your materializations are ready to use.
Your materialization class inherits from CustomMaterialization and implements at least the insert method.
Simple example
A complete example with custom insert logic and logging:
Breakdown:
NAME
The identifier you use in your model definition (for example, simple_custom).
table_name
The target table where data is inserted.
query_or_df
A SQL query string or a DataFrame (Pandas, PySpark, Snowpark).
model
The full model definition object, with access to all model properties.
is_first_insert
True if this is the first time inserting data for this model version.
render_kwargs
Dictionary of arguments used to render the model query.
self.adapter
The engine adapter: your interface to execute SQL and interact with the database.
Minimal example
A simple full-refresh materialization:
This replaces the table contents each time the model runs, like a FULL model kind.
Controlling table creation and deletion
Customize how tables and views are created and deleted by overriding the create and delete methods:
This gives you control over the lifecycle of your data objects.
Using a custom materialization
To use the materialization, set the model's kind to CUSTOM and pass the class NAME as materialization:
Passing properties to the materialization
Pass configuration to your materialization with materialization_properties to customize behavior per model:
Then access these properties in your materialization code via model.custom_materialization_properties:
This lets you build flexible materializations that adapt to different use cases.
Extending CustomKind
Subclass CustomKind when you need to validate or coerce custom properties before Vulcan opens any database connection, or you need a property to be present (and the right type) at parse time rather than at runtime.
Create a subclass of CustomKind and Vulcan detects and uses it instead of the default when your project loads.
Creating a custom kind
A custom kind that validates a primary_key property:
Using the custom kind in a model
Use it in your model:
Linking to your materialization
Connect your custom kind to your materialization with a generic type parameter:
When Vulcan loads your materialization, it inspects the type signature for generic parameters that subclass CustomKind. If it finds one, it uses your subclass when building model.kind instead of the default.
Two benefits:
Early validation:
primary_keyvalidation runs at load time, not evaluation time. Vulcan catches issues before you create a plan.Type safety:
model.kindresolves to your custom kind object, giving you access to extra properties without additional validation.
Sharing custom materializations
Two ways to share a materialization across projects:
Copying files
Copy the materialization code into each project's materializations/ directory. It works, but you have to update each copy manually when you change anything.
If you go this route, keep the materialization code in version control and set up a way to notify users when updates are available.
Python packaging
Packaging the materialization as a Python package solves the copy-paste problem and covers the case where the scheduler (Airflow and others) runs on machines that don't have access to the project's materializations/ directory.
Package your materialization with setuptools entrypoints:
Once the package is installed, Vulcan discovers and loads your materialization from the entrypoint list. No manual configuration needed.
Last updated
Was this helpful?