Search code examples
snowflake-cloud-data-platformdbt

How do I refer to a dbt source in Snowpark?


I have defined a dbt source like so:

version: 2

sources:
  - name: mysource
    database: MY_DATABASE
    schema: myschema
    tables:
      - name: table1

and am referencing it in a dbt model like so:

{%- set source_table = source('mysource', 'table1') -%}

SELECT * 
FROM {{ source_table }} source

(in reality its more complicated than this but for the purposes of demonstration, this will do.

I am now exploring Snowpark and I'd like to rewrite this model in Snowpark. The basic skeleton for a Snowpark model is:

def model(dbt, session):
    # Must be either table or incremental (view is not currently supported)
    dbt.config(materialized = "table")

    # DataFrame representing an upstream model
    df = dbt.ref("my_first_dbt_model")
 
    return df

(Create a simple python model)

In this example the source table is referred to using dbt.ref(). How do I refer to the source defined above?


Solution

  • ok, turns out its pretty easy:

    def model(dbt, session):
        # Must be either table or incremental (view is not currently supported)
        dbt.config(materialized = "table")
    
        # DataFrame representing an upstream model
        df = dbt.source('mysource', 'table1')
     
        return df