Why decimal(38,2) becomes decimal(38,6), and how to stop it
A partner's Snowflake table expects money as NUMBER(38,2). One day it arrives as NUMBER(38,6). Nobody changed the column. Somebody changed a calculation.
Where the extra decimals come from
Spark, and so Databricks, decides the type of every decimal expression from the types of its inputs. Those rules are sensible, but they are easy to forget. Here is what Apache Spark 3.5 returns when the inputs are decimal(38,2):
| Expression | Result type |
|---|---|
SUM(amount) | decimal(38,2) |
amount * quantity (integer) | decimal(38,2) |
amount * rate (both decimal(38,2)) | decimal(38,4) |
AVG(amount) | decimal(38,6) |
amount / other_amount | decimal(38,6) |
ROUND(AVG(amount), 2) | decimal(35,2) |
The last row surprises people. Rounding to two places fixes the scale, but the precision changes to 35. If your contract says decimal(38,2), a rounded average still breaks it.
Now add one more habit: building Gold tables with CREATE OR REPLACE TABLE ... AS SELECT. That statement takes its column types from the query, so when someone adds an average to a mart, the table's schema changes with it. No error, no warning. Whatever reads that table next, whether a Snowflake share, a partner feed or a BI model, inherits the new type.
Why it matters downstream
- Reconciliations fail. Values that should match to the cent now differ in the fourth decimal place.
- Partner loads break or silently widen. A strict target rejects the file; a loose one changes its own column.
- Reports change format. Currency columns suddenly show six decimals.
The fix: declare the contract, cast to it, test it
1. Write the contract down as data
Keep the agreed schema of every shared table in a table of its own: table, column, type. It doubles as documentation your partners can read.
table_name column_name data_type
gold.sales_daily sale_date date
gold.sales_daily units decimal(18,0)
gold.sales_daily net_amount decimal(38,2)
2. Cast every Gold column explicitly
Never let the query decide. Wrap each output column in a CAST to its contract type, including computed ones:
CAST(SUM(quantity * unit_price) AS decimal(38,2)) AS net_amount
3. Test the contract before you publish
After building Gold and before sharing it, compare the actual schema with the contract. Fail the job on any missing column, extra column or type change:
expected = {c.column_name: c.data_type for c in contracts}
actual = {f.name: f.dataType.simpleString() for f in spark.table(t).schema}
drift = {k for k in expected.keys() | actual.keys()
if expected.get(k) != actual.get(k)}
if drift:
raise Exception(f"Data contract broken, not publishing: {sorted(drift)}")
On the Snowflake side, the same check can read INFORMATION_SCHEMA.COLUMNS, which exposes DATA_TYPE, NUMERIC_PRECISION and NUMERIC_SCALE for every column.
What it looks like in practice
In our reference pipeline on Databricks, the contract check is its own step, between building Gold and reconciling against legacy output. When we changed one contract entry to decimal(38,6) in testing, the publish step stopped with:
Data contract broken, not publishing:
sales_daily.net_amount: type drift (decimal(38,6) vs decimal(38,2))
That is the whole idea. The cheapest place to catch a type change is the moment before it leaves your platform.
Sharing data from Databricks to Snowflake or partners? Our cross-platform data sharing service sets up the contract register, the casts and the tests for every shared table. Or book a free 30-minute call.