DuckDB UNNEST can put spend on the wrong campaign when lists are out of order
Side-by-side UNNEST pairs values by position, not by key. Equal list lengths don’t prove that the pairs are right.
A query that asks for spend by campaign can return plausible totals for the wrong campaigns if it flattens two independently assembled lists. DuckDB’s UNNEST documentation says that multiple list expressions in the same SELECT are aligned by position. The shorter list is padded with NULL. Neither operation checks that a spend value belongs to the campaign it lands beside.
Equal lengths don’t catch the error
Suppose a campaign list is ordered spring, summer, but a separate spend export is ordered summer, spring, with amounts 90 and 30. A generated query that combines the arrays might look like this:
SELECT unnest(['spring', 'summer']) AS campaign,
unnest([90, 30]) AS spend;
By DuckDB’s documented rule, the first pair is spring, 90 and the second is summer, 30. The correct attribution in this example is the reverse. Both lists contain two entries and the spend still totals 120, so checking the lengths or the grand total would miss the error. If the amount list contained only [90], the same rule would put 90 beside spring and pad summer with NULL; the padding would not tell you which campaign the 90 actually belonged to.
Keep the key with the amount
If the source is nested, preserve the campaign key and spend in the same record. DuckDB documents that recursive UNNEST can flatten a list of structs into rows and columns:
SELECT unnest([
{'campaign': 'summer', 'spend': 90},
{'campaign': 'spring', 'spend': 30}
], recursive := true);
If you already have two keyed tables, join on the campaign ID at the intended reporting grain rather than relying on row order. If the spend list has lost its keys and the source never guaranteed alignment, recover the mapping upstream rather than guessing. For a text-to-SQL regression test, reverse one keyed input’s order and check the amount for each campaign, not just the total. A query that preserves the keys should still report 30 for spring and 90 for summer.
sources
comments · 0
- EKOS’s pending BIRD submission pairs a SQLite-tuned run with a separate hidden-test scoreDec 3, 2026
- MotherDuck rolls out DuckDB 2.0 and reports faster cold reads, not faster agent answersDec 3, 2026
- Oracle Select AI can use two definitions of the same metric. Source order won’t break the tieDec 3, 2026