Learning Objectives
By the end of this chapter, you will be able to:
- Write efficient Spark SQL and PySpark code to apply advanced data transformations, including window functions, joins, and aggregations, to manipulate and analyze large datasets.
- Develop a quarantining process for bad data with Lakeflow Spark Declarative Pipelines, or Auto Loader in classic jobs.
Executive Summary
- Window functions and aggregations are the two highest-cost shuffle patterns in Silver pipelines, and the choice of partition keys, sort keys, and frame boundaries decides whether the stage spills or finishes in memory.
- Spark SQL and the DataFrame API compile through the same Catalyst planner, so the production choice is rarely about performance and almost always about testability, parameterization, and version control under Databricks Asset Bundles.
- Joins are governed by Adaptive Query Execution: broadcast hints, skew handling, and dynamic partition coalescing each have a specific trigger condition you must recognize during query-profile review.
- Quarantine for bad data has two canonical surfaces:
EXPECT ... ON VIOLATION DROP ROW(or fail / warn) inside Lakeflow Spark Declarative Pipelines, andbadRecordsPathplus_rescued_datainside Auto Loader classic jobs. The right choice depends on whether the pipeline is declarative or imperative, and whether bad rows must be re-processable or only inspected.
Assumptions
- The reader has completed Chapters 1 and 2, has a Unity Catalog enabled workspace, and uses Databricks Runtime with Photon enabled unless explicitly stated.
- Code samples use a fictional Silver project rooted at catalog
prod_silverand schemasales, with Bronze sources atprod_bronze.sales. - Lakeflow Spark Declarative Pipelines is the canonical name for what older Databricks materials call DLT; the in-code Python decorators remain
@dlt.*and the SQL keywords remainLIVEandSTREAMING LIVE, but the product surface is Lakeflow SDP. - All commands assume a Unity Catalog three-part name (
catalog.schema.table).
