Skip to content

Commit

Permalink
simple python model (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
sungchun12 authored Aug 13, 2024
1 parent f56c1e0 commit d97a122
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions models/orders_returned.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

import typing as t
from datetime import datetime

import pandas as pd
from sqlmesh import ExecutionContext, model

@model(
"tcloud_demo.orders_returned",
owner="sung",
cron="@daily",
columns={
"order_id": "int",
"customer_id": "int",
"order_date": "date",
"status": "text",
},
column_descriptions={
"order_id": "Unique ID",
"customer_id": "Customer ID",
"order_date": "Order Date",
"status": "Order Status",
},
audits=[
("not_null", {"columns": ["order_id", "customer_id"]}),
],
)
def execute(
context: ExecutionContext,
start: datetime,
end: datetime,
execution_time: datetime,
**kwargs: t.Any,
) -> pd.DataFrame:
# Fetch data from the stg_orders model, automatically captures the model's dependencies
table = context.table("tcloud_demo.stg_orders")
df = context.fetchdf(f"SELECT * FROM {table}")

# Filter only where status equals "returned"
df_returned = df[df['status'] == 'returned']

# Select only the columns specified in the model definition
result = df_returned[['order_id', 'customer_id', 'order_date', 'status']]

return result

0 comments on commit d97a122

Please sign in to comment.