Puzzleshot #028 - Zero Duck Thirty

raw data→duckdb→result

Same kind of data. Same query. One file loses its zeros.

The scenario: a dealer file arrives with a ZIP code for every dealer. You load it with DuckDB, and the Northeast dealers’ ZIP codes look wrong.

No dealer file handy? This makes one with 25,005 rows:

awk 'BEGIN { print "dealer_id,zip"; for (i = 1; i <= 25000; i++) printf "%d,%d\n", i, 50000 + i % 9000; for (i = 25001; i <= 25005; i++) printf "%d,0%d\n", i, 2000 + i % 900 }' > dealers.csv

The last three lines of the file:

25003,02703
25004,02704
25005,02705

The challenge:

Round 1: Load dealers.csv in DuckDB and look at how it typed the columns. Then look at the last few rows. What’s different from what’s in the file?

Round 2: Fix it. How many ways can you find?

Bonus: You inherit a file that was already loaded this way, and the zeros are gone. How do you check how many rows were damaged, and how do you put the zeros back?

Things to consider:

Reveal solution

The short version: DuckDB works out each column’s type by reading a sample of the file, not the whole file. If the sample has no ZIP code with a leading zero, it decides the column is a number, and every zero-prefixed ZIP after the sample silently loses its zero.

Round 1 - spot it:

duckdb -c "DESCRIBE SELECT * FROM 'dealers.csv'"

The zip column comes back as BIGINT, not text. The last rows of the file read back as 2701, 2702 and so on, instead of 02701, 02702.

Why the small test file was fine: when DuckDB sees a value with a leading zero in its sample, it keeps the column as text, because turning it into a number would change the value. In my test on DuckDB 2.0.0, a zero-prefixed ZIP at row 20,000 was caught, and one at row 20,480 was missed. In this file the zero-prefixed ZIPs only show up at the very end, after the sample.

Round 2 - three fixes:

1. Tell it the type:

duckdb -c "SELECT * FROM read_csv('dealers.csv', types={'zip': 'VARCHAR'})"

2. Read everything as text, then cast the columns you actually need:

duckdb -c "SELECT * FROM read_csv('dealers.csv', all_varchar=true)"

3. Make it read the whole file to decide the types:

duckdb -c "SELECT * FROM read_csv('dealers.csv', sample_size=-1)"

All three keep 02701 as 02701. I’d choose the first for identifiers like ZIP codes, phone numbers and account numbers. The third reads the whole file just to detect types, so expect it to take longer on a big one.

Bonus - check and repair:

To count the damage, remember that every ZIP should have five digits:

duckdb -c "SELECT count(*) FROM 'dealers.csv' WHERE length(zip::VARCHAR) < 5"

That returns 5 here, one for each lost zero. Run the same check on a correctly typed read and it returns 0.

To put the zeros back:

duckdb -c "SELECT dealer_id, lpad(zip::VARCHAR, 5, '0') AS zip FROM 'dealers.csv'"

lpad pads each ZIP on the left with zeros up to five characters. That’s only safe because ZIP codes are always five digits. For columns with variable lengths, you can’t tell which zeros were lost.

The things to consider:

Why did the small file work? The sample contained a zero-prefixed value, so DuckDB kept the column as text.

What would you check on every new file? DESCRIBE it, and look at identifier columns for the wrong type or the wrong length.

Does pandas behave the same way? Worse, in my test. pandas read an all-digit zip column as integers on a small file and returned 3301, 2134 and 5401. It has no leading-zero guard. The fix is dtype={‘zip’: str} in read_csv.

The takeaway:

Identifiers aren’t numbers. If you’ll never do math on a column, make it text.