EX-1: Delta Lake DDL & Partitioning Strategy
Track: Architecture & Modeling • Difficulty: Beginner • Mastery Points: 100 PTS
Business Problem Scenario
Global DynaDatum Bank's monthly deposit reconciliation requires storing 240,000 month-end balance snapshots across 20,000 retail accounts. A junior engineer previously attempted to store raw text dumps without schema controls, leading to corrupted balance calculations. You must create the authoritative Delta Lake table definition for silver.core_balances. Note on Lakehouse architecture: while partitioning small tables (< 100 GB) is often an antipattern, in monthly financial ledger archives, partitioning by month-end snapshot date (balance_date) enables deterministic partition pruning during audit reconciliations (scanning 1 month out of 12 prunes 91.7% of partition files). Crucially, Databricks primary key constraints on Delta tables are declarative and informational (annotated with RELY); they are not enforced at write time. Therefore, data engineers declare primary keys to guide Catalyst query optimization while implementing separate pipeline uniqueness assertions to guarantee no duplicate (account_id, balance_date) rows exist.
Engineering Objective
Write the CREATE OR REPLACE TABLE silver.core_balances DDL statement in Delta Lake. • Target Schema: - account_id STRING NOT NULL - balance_date DATE NOT NULL - ledger_balance DECIMAL(18,2) NOT NULL - available_balance DECIMAL(18,2) NOT NULL - currency STRING NOT NULL • Integrity Constraints: Primary key constraint on (account_id, balance_date) RELY. • Storage Strategy: USING DELTA, PARTITIONED BY (balance_date). • Uniqueness Criterion: The DDL must declare decimal precision DECIMAL(18,2) for financial balances, declare the composite primary key constraint on (account_id, balance_date) with RELY, and partition by (balance_date). In addition, verify that no duplicate (account_id, balance_date) combinations exist in the target dataset. Key Concepts: - Declarative Constraints (RELY): Informational metadata informing the query planner of primary key uniqueness without write-time locking overhead. - Partition Pruning: Physical directory segregation allowing monthly audit queries to prune 91.7% of annual ledger data.
Target Table: silver.core_balances (240k rows: account_id STRING, balance_date DATE, ledger_balance DECIMAL(18,2), available_balance DECIMAL(18,2), currency STRING)
Verification & Architecture Insights
Partitioning by balance_date enables deterministic monthly partition pruning (skipping 91.7% of annual files during audit queries), while declarative composite primary keys guide Catalyst query optimization (paired with pipeline uniqueness assertions to prevent duplicate ledger snapshots).