Write a Python transform as collection code
Create a Python Code asset and connect it to a pipeline's input and output datasets.
Use a Python or SQL Code asset when a built-in transform cannot express the logic clearly. A code node references a reusable Code asset in the selected collection; it is not a pipeline-local script.
In this lesson, you keep the lesson 3 result unchanged and build a separate pipeline that filters high-value orders.
What the code does
The code keeps orders whose amount is at least 10,000 and renames the source date column for the output schema.
| Stage | Data |
|---|---|
| Input | src_postgres_orders with order_id, ord_dt_str, and amount |
| Process | Rename ord_dt_str to order_dt_str and filter with amount >= 10000 |
| Output | Two rows in mart_orders_filtered with order_id, order_dt_str, and amount |
For this single condition, the built-in Select/Filter transform would be simpler. The exercise uses Python so you can learn the code node's input and output contract.
Create the output dataset
- Open the collection that contains
src_postgres_orders. - Select Add item, point to Dataset, and select Table.
- Enter
mart_orders_filteredfor Name and select Next. - Configure these attributes under Schema.
| Column | Data type |
|---|---|
order_id | Text |
order_dt_str | Text |
amount | Integer |
- Select Create and confirm that
mart_orders_filteredappears in the collection.
Use an existing dataset only when its schema is identical. Changing the schema of a dataset that already contains data can make later writes fail validation.
Open a new pipeline
- Select Data → Pipelines in the sidebar.
- Select Create in the upper-right.
- In Select Pipeline Collection, choose the collection containing the input and output datasets.
Create a new pipeline instead of modifying lesson 3 so both results remain available.
Create the Python Code asset
- Drag Quick Add → Code → Python onto the canvas.
- Name it
filter_high_value_orders. - Enter this code.
import polars as pl
def run(src_postgres_orders, *, options=None, contexts=None):
output = (
src_postgres_orders
.with_columns(
pl.col("amount").cast(pl.Int64),
pl.col("ord_dt_str").alias("order_dt_str"),
)
.filter(pl.col("amount") >= 10000)
.select(["order_id", "order_dt_str", "amount"])
)
return {"mart_orders_filtered": output}
The runtime passes connected inputs as keyword arguments. Therefore, the function argument src_postgres_orders must exactly match the input port name. The returned key mart_orders_filtered must exactly match the output port name. A mismatch causes an unexpected keyword argument or output-key error.
Connect the pipeline
- Drag
src_postgres_ordersfrom the collection section onto the canvas. - Connect it to the left handle of
filter_high_value_orders. - Drag
mart_orders_filteredonto the canvas. - Connect the code node's right handle to
mart_orders_filtered. - In the code node's Options tab, confirm that the input is
src_postgres_orders, exactly matches the firstrunargument, and uses incremental read mode. - Confirm the
mart_orders_filteredoutput and append write mode.
src_postgres_orders → filter_high_value_orders → mart_orders_filtered
Save, run, and verify
- Select Save and enter
mart_orders_filtered_pipeline. - Select Run now.
- Confirm a successful run in the run history.
- Open
mart_orders_filteredand select Data.
The result should contain these two rows. The 8,900 order is excluded.
| order_id | order_dt_str | amount |
|---|---|---|
| order_001 | 2026-07-01 | 12500 |
| order_003 | 2026-07-03 | 17300 |
Self-check
- Is
filter_high_value_orderssaved as a Code asset in the selected collection? - Are the nodes connected dataset → code → dataset?
- Do the function argument and returned key match the pipeline ports?
- Does the successful output contain only the two orders of at least 10,000?
Next lesson
Next, you configure a schedule for the batch pipeline.