|
| 1 | +# compact_chunk |
| 2 | + |
| 3 | +Merges overlapping compressed batches within a chunk. Only touches batches |
| 4 | +that need fixing — correctly ordered batches are left as-is. |
| 5 | + |
| 6 | +Overlap detection reads the firstlast sparse metadata, which stores the exact |
| 7 | +orderby values of each batch's first and last rows. Two adjacent batches overlap |
| 8 | +when the current batch's first row sorts before the previous batch's last row. |
| 9 | +Because the metadata holds the real boundary rows for every orderby column, no |
| 10 | +decompression is needed, even for multi-column orderby. |
| 11 | + |
| 12 | +## How It Works |
| 13 | + |
| 14 | +``` |
| 15 | + Phase 1: FIND Phase 2: RECOMPRESS Phase 3: VERIFY |
| 16 | + ┌──────────────┐ ┌──────────────────┐ ┌────────────────┐ |
| 17 | + │ Index scan │──────▶│ Decompress+merge │─────▶│ Re-scan with │ |
| 18 | + │ Stop at │ │ overlapping │ │ fresh snapshot │ |
| 19 | + │ first issue │ │ batches, continue│ │ Clear UNORDERED│ |
| 20 | + └──────────────┘ │ scanning for more│ │ if clean │ |
| 21 | + └──────────────────┘ └────────────────┘ |
| 22 | +``` |
| 23 | + |
| 24 | +## Handling specific compression and batch configurations |
| 25 | + |
| 26 | +### 1. No overlaps (no-op) |
| 27 | + |
| 28 | +``` |
| 29 | + ┌────────┐ ┌────────┐ ┌────────┐ ┌────────┐ ┌────────┐ ┌────────┐ |
| 30 | + │ 1..100 │ │101..200│ │201..300│ ───▶ │ 1..100 │ │101..200│ │201..300│ |
| 31 | + └────────┘ └────────┘ └────────┘ └────────┘ └────────┘ └────────┘ |
| 32 | + (unchanged, UNORDERED cleared) |
| 33 | +``` |
| 34 | + |
| 35 | +### 2. Overlapping batches |
| 36 | + |
| 37 | +``` |
| 38 | + ┌──────────┐ ┌───────┐┌───────┐┌───────┐┌──┐┌────────┐ |
| 39 | + │ 1..100 │ │ 1..50 ││51..100││101 ││ ││201..300│ |
| 40 | + └──────────┘ │ ││ ││ ..150 ││… │└────────┘ |
| 41 | + ┌──────────┐ ───▶ └───────┘└───────┘└───────┘└──┘ |
| 42 | + │ 50..150 │ ◄────── merged + re-sorted ──► ◄ kept ► |
| 43 | + └──────────┘ |
| 44 | + ┌────────┐ |
| 45 | + │201..300│ |
| 46 | + └────────┘ |
| 47 | +``` |
| 48 | + |
| 49 | +### 3. Segmentby — independent per segment |
| 50 | + |
| 51 | +``` |
| 52 | + d1: ┌──────┐ ┌───────┐ d1: ┌──────────────┐ |
| 53 | + │1..100│ │50..200│ ──▶ │ 1..200 merged│ |
| 54 | + └──────┘ └───────┘ └──────────────┘ |
| 55 | + d2: ┌──────┐ ┌───────┐ d2: ┌──────────────┐ |
| 56 | + │1..100│ │50..200│ ──▶ │ 1..200 merged│ |
| 57 | + └──────┘ └───────┘ └──────────────┘ |
| 58 | +``` |
| 59 | + |
| 60 | +### 4. DESC orderby |
| 61 | + |
| 62 | +``` |
| 63 | + orderby='time DESC' max◄────────────────────►min |
| 64 | +
|
| 65 | + ┌──────────┐ ┌──────────────────┐ |
| 66 | + │ 200..100 │ │ 200..........100 │ |
| 67 | + └──────────┘ ──▶ │ merged │ |
| 68 | + ┌──────────┐ └──────────────────┘ |
| 69 | + │ 150..50 │ |
| 70 | + └──────────┘ |
| 71 | +``` |
| 72 | + |
| 73 | +### 5. Multi-column orderby — boundary tie resolution |
| 74 | + |
| 75 | +When col1 first/last tie, compare the secondary columns straight from the |
| 76 | +first/last metadata — no decompression. |
| 77 | + |
| 78 | +``` |
| 79 | + orderby='device,time' Both batches tie on device (first=d2, last=d2) |
| 80 | +
|
| 81 | + Batch 1 last row: (d2, 08:20) ◄─ from last metadata |
| 82 | + Batch 2 first row: (d2, 08:21) ◄─ from first metadata |
| 83 | + 08:20 < 08:21 → no overlap ✓ |
| 84 | +
|
| 85 | + Batch 1 last row: (d2, 08:20) |
| 86 | + Batch 2 first row: (d2, 08:11) |
| 87 | + 08:20 > 08:11 → OVERLAP → merge |
| 88 | +``` |
| 89 | + |
| 90 | +### 6. Mixed-null batch overlaps a neighbor |
| 91 | + |
| 92 | +A batch with both NULL and non-NULL values in the first orderby column has a |
| 93 | +NULL boundary row. With NULLS LAST its last row is NULL, which sorts after a |
| 94 | +following non-null batch — so the two batches overlap. The merge re-sorts the |
| 95 | +rows and the NULLs settle at the end (NULLS LAST). (A mixed-null batch with no |
| 96 | +neighbor to overlap is already ordered and is left as-is.) |
| 97 | + |
| 98 | +``` |
| 99 | + orderby='value NULLS LAST' last row of batch 1 is NULL, sorts after batch 2 |
| 100 | +
|
| 101 | + ┌─────────────────────┐ ┌──────────┐ ┌────────────────────────────────┐ |
| 102 | + │ 1001..1800, NULL×200│ │1801..2800│ ──▶ │ 1001..2800 re-sorted, NULL×200 │ |
| 103 | + └─────────────────────┘ └──────────┘ └────────────────────────────────┘ |
| 104 | + first=1001, last=NULL ──▶ overlap merged, NULLs at end (NULLS LAST) |
| 105 | +``` |
| 106 | + |
| 107 | +### 7. Overlap merge preserving NULLs |
| 108 | + |
| 109 | +When overlapping batches with nullable first orderby are merged, the re-sort |
| 110 | +keeps the NULL rows in their correct ordered position. |
| 111 | + |
| 112 | +``` |
| 113 | + orderby='value NULLS LAST' |
| 114 | +
|
| 115 | + ┌──────────────────┐ ┌──────────────────────────┐ |
| 116 | + │ 1..400, NULL×100 │ ──▶ │ 1..699 re-sorted, NULL×100│ |
| 117 | + └──────────────────┘ └──────────────────────────┘ |
| 118 | + ┌──────────┐ overlap NULLs kept at end (NULLS LAST) |
| 119 | + │ 200..699 │ on 200..400 |
| 120 | + └──────────┘ |
| 121 | +``` |
| 122 | + |
| 123 | +### 8. Secondary column NULLs at boundary tie |
| 124 | + |
| 125 | +The boundary comparison uses the column's sort order, which already places NULLs |
| 126 | +per its NULLS FIRST/LAST setting, so a NULL boundary value compares like any |
| 127 | +other value. |
| 128 | + |
| 129 | +``` |
| 130 | + orderby='time, value NULLS LAST' |
| 131 | +
|
| 132 | + Batch 1 last row: (08:20, NULL) CORRECT: NULL with NULLS LAST |
| 133 | + Batch 2 first row: (08:20, 1001) means NULL > 1001 → OVERLAP → merge |
| 134 | + |
| 135 | +
|
| 136 | + ┌──────────────────┐ ┌──────────────────┐ ┌──────────────────────────┐ |
| 137 | + │ ..., (08:20,NULL)│ │(08:20,1001), ... │ ──▶ │ merged + correctly sorted│ |
| 138 | + └──────────────────┘ └──────────────────┘ └──────────────────────────┘ |
| 139 | +``` |
0 commit comments