dbt Analytics Engineering (50 Qs)•Part 3: Enterprise Scale, Performance & CI/CD Pipelines (Senior / Staff)
dbt Python Models: When and how to run Python scripts in Snowflake, Databricks, and BigQuery.
senior LevelMost AskedCode ExampleFormsPerformancePipes
In Simple Words: SQL is ideal for set-based data transformations; Python is ideal for complex statistical analysis, NLP parsing, and machine learning scoring:
python
# models/customer_churn_prediction.py
import statsmodels.api as sm
def model(dbt, session):
dbt.config(materialized="table", packages=["statsmodels", "scikit-learn"])
# Read upstream dbt SQL model as a DataFrame!
df = dbt.ref("fct_customer_activity").to_pandas()
# Perform Python ML transformations
df["predicted_churn"] = (df["inactive_days"] > 30).astype(int)
return session.create_dataframe(df)
- Executes serverlessly inside the data warehouse's secure Python runtime without moving data over the public internet.