Sub topic 16
Migrating and mixing tools in one pipeline
This chapter assumes you have read the chapters on interoperability and the decision framework. You should know how Arrow-backed conversion works and have a checklist for choosing a tool. Here you decide whether to move an existing pipeline to a new tool or let several tools share it.
Count the cost of a migration
A migration replaces working code with new code. That work is only worth doing when a measured problem justifies it. Typical reasons are:
- The job no longer fits in memory, or it runs too slowly for its schedule.
- Memory spikes cause out-of-memory failures.
- The team spends real time maintaining workarounds, such as manual chunking.
Typical costs are rewriting the logic, re-testing the output, and teaching the team a new API. A common mistake is migrating because a tool is popular. If the pipeline meets its deadlines and fits on one machine, the best migration is often none.
Mix tools instead
A pipeline has stages, and each stage can use the tool that suits it. This avoids a full rewrite. A common split is:
- DuckDB or Polars reads Parquet files, filters them, and aggregates them down to a small table.
- pandas takes that small table for plotting or for a library that expects a pandas DataFrame.
The heavy step runs in an engine built for it. The result is small enough that pandas handles it easily.
A worked example
This script keeps the heavy aggregation in DuckDB and hands only the result to pandas. It creates a small sample file first, so it runs as shown.
import duckdb
import pandas as pd
pd.DataFrame(
{"region": ["eu", "us", "eu", "us"], "revenue": [10, 20, 5, 15]}
).to_parquet("sales.parquet")
summary = duckdb.sql(
"SELECT region, sum(revenue) AS revenue "
"FROM 'sales.parquet' "
"WHERE revenue > 0 "
"GROUP BY region"
).df()
summary["share"] = summary["revenue"] / summary["revenue"].sum()
print(summary.sort_values("region"))DuckDB scans only the two columns it needs. pandas then does the small final calculation. Writing a Parquet file between stages, rather than passing objects in memory, also lets each stage run as a separate job.
Migrate in steps
If you do migrate, reduce the risk by working in small pieces.
- Pick one stage, ideally the slowest or the most memory-hungry.
- Run the old and new versions on the same input.
- Compare the outputs, allowing for float rounding and row order. Row order is often not guaranteed after a group-by.
- Switch that stage over, then move to the next.
Costs of mixing
Mixing is not free. Each extra tool is another dependency to install, version, and debug. Conversions can copy data, which costs memory for large tables. Keep the number of tools small, and convert only at stage boundaries, on data that has already been reduced.
Exercise
A nightly job reads 40 GB of Parquet on a 64 GB machine, aggregates it, and then draws charts with a pandas-based library. It runs for six hours and sometimes runs out of memory. Should you rewrite the whole job in Spark? What would you do instead?
Show answer
Not necessarily. Spark adds cluster costs, and the data fits on one machine if it is read lazily. Move only the read, filter, and aggregate stage to DuckDB or Polars, which read just the needed columns and can work out of core. Pass the small aggregated result to pandas for the charts. Compare the new output with the old one, and measure the runtime and memory use before deciding whether anything more is needed.