Custom Libraries
Expand SDF Built-in Libraries with custom logic
SDF comes with built-in open-source libraries (learn more about our open source libs). To further increase SDF’s compatibility, it is possible to expand those built-in libs with custom logic.
The following guide will explain how to do so, step by step.
Setup
Open an SDF workspace
For the sake of the example, we will use our tests
sample workspace.
However, you can always use your existing SDF workspace to follow along.
To create the tests
workspace, run the following command:
There are slight syntax differences for expanding each library. To keep it simple, we will focus our example on tests and point out the implementation differences for other libs.
Create a New Libs Folder
Within your workspace, create a new folder. We will call it my_lib
,
but any name would work. This is where we will add our custom logic in
later steps.
If you are using the tests
example, your workspace should look like this:
Copy SDF's Built-in Lib
One option is to copy the libraries from GitHub:
Another option is to copy them from sdfTarget
.
If you don’t have an sdfTarget
directory, all you need to do is
to compile your workspace:
If your compilation was successful, you will see sdfTarget
in your
workspace:
SDF’s libraries will be located under sdfTarget/dbg/.lib
.
Let’s copy sdf_test
to our new custom libraries folder:
You can choose to keep the library name as is, sdf_test
,
or change it if there’s another name you’d prefer to use.
Add Custom Library to the Workspace YML
Now, we need to add our new library to the workspace configuration.
In our workspace.sdf.yml
file, let’s add the following section:
Notice, we are using SDF’s built-in library name, sdf_test
.
However, we can also choose a different name to our library. If
we decide to do so, we’ll need to indicate it in the defaults.
Let’s assume we call the new tests library new_tests
:
If we want to add another type of custom lib with a non-default name, we should add to the defaults section:
- We can have more than one custom lib added to a workspace. Each lib is independent of the rest.
- We can create an environment to include the custom libs rather than include it in the main workspace configuration. It’ll look like this:
Expand Your Lib
Let’s add some custom logic!
Open the lib file. It will be located under my_lib/sdf_test/macro/test.jinja
.
If you changed any folder names, make sure to apply the changes to locate
the relevant file.
Copy the following macro to the top of your custom tests lib:
We are keeping it simple and merely checking that the column value is below 1000, but of course, you can implement any custom and complex logic here.
Try It!
First, we need to add the test to our table yml file, just like for SDF’s built-in tests
(to learn more, visit our Tests Guide).
In the file src_metadata/raw_inapp_events.sdf.yml
, add our new custom test
to the column event_name
:
Let’s run the test! In your terminal, execute:
The test passed!
We can look at the compiled tests query located in the file
sdftarget/dbg/preprocessed/sdftarget/dbg/tests/tests_workspace/pub/test_raw_inapp_events.sql
.
Notice our custom logic in the compiled query: