Type Casting: Advanced
Storage Types and Query Performance
Spot questions where type casting is the hidden requirement: string dates, numeric strings, decimal precision.
Three platform concerns
- ▸"design the type alignment from Postgres to Iceberg to Snowflake to Arrow"
- ▸"how do we enforce money handling across the platform"
- ▸"a column's type changed; how do we catch it before downstream breaks"
- ▸"the precision is lost between the source and the warehouse"
- ▸"the Arrow IPC deserialization fails on some rows"
Implicit Casts That Defeat an Index
Write explicit type conversions and use TRY_CAST to handle dirty data that fails conversion.
Type system mappings
Where alignment breaks
Arrow as the lingua franca
Iceberg's type evolution rules
Numeric Overflow and Precision Loss at Scale
Avoid the integer division trap (5/2=2) by casting to DECIMAL before dividing, and control precision in financial calculations.
Money handling
Identifier safety
Percentage and ratio types
Cast-Heavy Predicates and Plan Quality
Parse dates from strings using TO_DATE/PARSE with format strings, and format dates back to strings for output.
Schema contracts with type declarations
Type-aware lineage
Cross-system type registries
Fixing Type Bugs Upstream vs in Query
Discuss how column types affect storage size, join performance (int vs string keys), and predicate pushdown eligibility.
Type-quality metrics
Type-migration runbook
The closing thought
> You are in a data engineering interview at a financial platform. The interviewer asks: 'We have data flowing from Postgres source databases through a Kafka stream to an Iceberg lake to a Snowflake warehouse, with consumers reading from Arrow IPC. Walk me through the type system across the path.'
NUMERIC(20,4) to an Avro decimal logical type, into Iceberg DECIMAL(20,4), into Snowflake NUMBER(20,4), into Arrow Decimal128. Misalignment at any boundary loses data.NUMERIC into a string, and a default Iceberg-to-Snowflake mapping to NUMBER(38,9) silently narrows a wider source.NUMERIC with explicit precision plus a paired ISO 4217 currency column, never a float, and cross-currency aggregates require an explicit conversion step. Stripe's standard after three money-as-float incidents made a float in any column named amount, price, or cost a CI failure.SUM(customer_id) is nonsense but legal, and a ratio column that stores 0 to 100 where consumers expect 0 to 1 produces a 100x error. Postgres CREATE DOMAIN and dbt accepted-range tests are where those contracts live.INT to LONG, FLOAT to DOUBLE, a decimal to a wider decimal) and the catalog rejects narrowing writes, which makes the storage layer itself the governance boundary for type changes.NULL, how often contract tests fail, and use column-level lineage to identify every downstream consumer before a migration. The runbook is announce, add or alter, backfill with explicit casts, update contracts, test downstream, monitor, then retire the old column.Implicit casts hide bugs that only show up in production at 2 AM
- Category
- SQL
- Difficulty
- advanced
- Duration
- 25 minutes
- Challenges
- 0 hands-on challenges
Topics covered: Storage Types and Query Performance, Implicit Casts That Defeat an Index, Numeric Overflow and Precision Loss at Scale, Cast-Heavy Predicates and Plan Quality, Fixing Type Bugs Upstream vs in Query
Lesson Sections
- Storage Types and Query Performance (concepts: sqlCast)
The question that opens the platform-scale type conversation: 'we have data flowing from Postgres source databases through a Kafka stream to an Iceberg lake to a Snowflake warehouse, with consumers reading from Arrow IPC for ML serving. Walk me through the type system across the path.' This is not a CAST question. The platform-grade answer walks the type alignment at each boundary: Postgres NUMERIC to Avro decimal logical type, into Iceberg DECIMAL, into Snowflake NUMBER, into Arrow Decimal128.
- Implicit Casts That Defeat an Index (concepts: sqlCast)
The first platform concern is codec alignment. Data leaves a source database in one type system, traverses a serialization format, lands in a storage layer, and is read by a consumer with its own type system. Each step's type system has to align with the next; misalignment is data loss. Type system mappings Postgres NUMERIC(20,4) maps to Avro {type: bytes, logicalType: decimal, precision: 20, scale: 4}. Iceberg's DECIMAL(20, 4) reads that Avro and stores it natively. Snowflake's NUMBER(20,4) rea
- Numeric Overflow and Precision Loss at Scale (concepts: sqlDecimalType)
Domain types are custom types that encode business invariants. Money is not a float; it's a precise decimal with a currency. An identifier is not an integer; it's a typed token that should not accidentally be added to another integer. Most warehouses don't support custom types directly, but the platform pattern is to enforce the invariants through wrapper functions, validation rules, and code conventions. Money handling The discipline: money columns are NUMERIC with explicit precision (not FLOAT
- Cast-Heavy Predicates and Plan Quality (concepts: sqlDateFormat)
At platform scale, type changes ripple. When a column's type changes from NUMERIC(10,2) to NUMERIC(20,4), every downstream pipeline that reads it has to handle the change. The platform's lineage and contract testing surface the change before consumers break. Schema contracts with type declarations dbt contracts allow per-column type declarations on model outputs. The contract specifies the type and any constraints (NOT NULL, accepted ranges). When the model produces output that violates the cont
- Fixing Type Bugs Upstream vs in Query (concepts: sqlStorageOptimization)
The last platform concern is the operational layer: the mechanisms that catch type violations in production, the runbooks for type migrations, the dashboards that surface type-related metrics. Type-quality metrics The platform tracks metrics about types: how often safe-cast returns NULL (signals upstream drift); how often a column's type changes (signals migration cadence); how often dbt contract tests fail (signals breaking changes that almost shipped). These metrics surface type discipline as