DuckDB: Why the Same Query Reads More Groups
Outline
Transcript
0:00 Imagine Priya rewrites a Parquet export. Her DuckDB query and rows stay unchanged, but the reader now keeps four groups instead of one. A code-review trap: correct answer, lost skips. My first thought is a missing filter. Did it still reach the scan? It did. Parquet batches rows into row groups, with separate column chunks. Group zero's day minimum is zero, its maximum 15; we're asking for 20. 20's outside. Can you show how the reader skips it? Because 20 exceeds the upper bound, the statistics check returns always false.
0:34 The reader sets its offset, the next-row position, to the group's row count. That completes the group; the scan advances to another. We can retrieve that decision. Set enable logging to true and logging level to debug before the query, then query the function named DuckDB logs for read-row-group and skip-row-group messages. Ah, nice. Our synthetic run and source trace use DuckDB version 1.5.6. Source links are in the description. Then our four equally sized groups give us a concrete sequence: skips for groups zero, two and three, and a read for group one.
1:10 Right. For example, group one's day bounds are 16 through 31, so 20 is possible there. The other two groups start above 20. Our query counts matching records and totals their amounts. Yeah. We kept the comparison clean too: a fresh connection for each file, logging on before the query. What happens when we interleave the same rows? Then every group spans zero through 63. It's like dealing days into separate piles; we changed their arrangement while holding each record and the query fixed. 20 fits every range, so none earns that always-false result.
1:44 Do the events actually agree? Four reads, one event for each group. Both files return the same matching count and amount sum, but this arrangement loses all three skips. Ugh, I can see us waving that through. The totals match, everybody relaxes, and every pile still says, '20 could be here.' Those ranges belong beside the results. Yeah. The rows didn't change. The range evidence did. But keeping group one doesn't tell us which records matched. How much can its range summary prove? Imagine a box labelled 16 to 31, holding only two cards: 16 and 31.
2:22 20 fits the label, but isn't inside. Oh, so the label can send me away, but it can't hand me the right card. That leaves the actual values to check. Yeah. So how does the pinned scan code handle a smaller batch of rows inside a surviving group? It loads the filter columns first. Our plan's day equals 20 condition makes day the filter column, while amount is the output. Right. Those day values decide which amounts qualify. And it keeps, basically, the positions that passed as a selection, plus their count.
2:55 Amount's reader gets both next. But if that batch has no survivors, does amount still get read? It gets a Skip call for that batch. The group can survive while a batch contributes nothing. Ah, that moves the rejection inside the group. If some positions do pass, how does amount use them? Say the batch has days 19 and 20, with amounts five and seven. Only the second row passes. So amount's Select gets that position, with 1 survivor. Slicing leaves the row carrying seven. Do those messages show whether Select read both amounts before keeping seven?
3:28 No. They're group decisions. In the source, Select can read the whole batch before slicing it, while Skip records rows to skip later. So a group's read event doesn't mean every column was fully decoded, and hearing 'Skip' doesn't give us a byte count. Yeah. Our one-versus-four comparison counts groups kept. Both layouts matched 1,024 records; that doesn't establish a four-times speedup. True. I think we'd show Priya four versus one, then go quiet when she asks about bytes or elapsed time. We know where the skips went; we don't know their cost.
4:03 And before Priya approves the new file, let's look at its day bounds. DuckDB has a Parquet metadata function that shows the bounds for each group. Yep. I'd keep the original export as our baseline. But is ordering by day enough to make Priya's new export policy safe? But three skips for one filter wouldn't settle that policy. Suppose Priya says, “Let's make that our default export order.” I'd be tempted too. Let's try a filter that doesn't ask about day. We tested amount equals 80 on these same files, with their bytes unchanged.
4:37 What happened to the group decisions when we asked about amount instead of day? Both layouts kept all four. Every group's amount bounds ran from zero to 96, so 80 fell inside each range. The results still matched. Ah, the day ordering bought us nothing at this group check for the amount query. I'd bring representative filters to that review, plus measurements that account for sorting and rewriting. And I'd compare scan conditions, group bounds and observed decisions across our actual queries before approving Priya's policy. Those distinguish a filter that never arrived from ranges that can't reject a group.
5:14 Equal results can hide that change. Exactly. DuckDB needs evidence that a group cannot match before it takes the skip branch. Thanks for listening to Learning Podcasts.