| 42 | var pool = newRowPool(1_000_000) |
| 43 | |
| 44 | func (c *Compactor) Compact(ctx context.Context, l log.Logger, r backend.Reader, w backend.Writer, inputs []*backend.BlockMeta) (newCompactedBlocks []*backend.BlockMeta, err error) { |
| 45 | var ( |
| 46 | compactionLevel uint32 |
| 47 | totalRecords int64 |
| 48 | minBlockStart time.Time |
| 49 | maxBlockEnd time.Time |
| 50 | bookmarks = make([]*bookmark[parquet.Row], 0, len(inputs)) |
| 51 | ) |
| 52 | for _, blockMeta := range inputs { |
| 53 | totalRecords += blockMeta.TotalObjects |
| 54 | |
| 55 | if blockMeta.CompactionLevel > compactionLevel { |
| 56 | compactionLevel = blockMeta.CompactionLevel |
| 57 | } |
| 58 | |
| 59 | if blockMeta.StartTime.Before(minBlockStart) || minBlockStart.IsZero() { |
| 60 | minBlockStart = blockMeta.StartTime |
| 61 | } |
| 62 | if blockMeta.EndTime.After(maxBlockEnd) { |
| 63 | maxBlockEnd = blockMeta.EndTime |
| 64 | } |
| 65 | |
| 66 | block := newBackendBlock(blockMeta, r) |
| 67 | |
| 68 | derivedCtx, span := tracer.Start(ctx, "vparquet.compactor.iterator") |
| 69 | defer span.End() |
| 70 | |
| 71 | iter, err := block.rawIter(derivedCtx, pool) |
| 72 | if err != nil { |
| 73 | return nil, fmt.Errorf("error creating iterator for block %s: %w", blockMeta.BlockID.String(), err) |
| 74 | } |
| 75 | |
| 76 | bookmarks = append(bookmarks, newBookmark(iter)) |
| 77 | } |
| 78 | |
| 79 | var ( |
| 80 | replicationFactor = inputs[0].ReplicationFactor |
| 81 | nextCompactionLevel = compactionLevel + 1 |
| 82 | sch, _, _ = SchemaWithDynamicChanges(inputs[0].DedicatedColumns) |
| 83 | ) |
| 84 | |
| 85 | // Dedupe rows and also call the metrics callback. |
| 86 | combine := func(rows []parquet.Row) (parquet.Row, error) { |
| 87 | if len(rows) == 0 { |
| 88 | return nil, nil |
| 89 | } |
| 90 | |
| 91 | if len(rows) == 1 { |
| 92 | return rows[0], nil |
| 93 | } |
| 94 | |
| 95 | isEqual := true |
| 96 | for i := 1; i < len(rows) && isEqual; i++ { |
| 97 | isEqual = rows[0].Equal(rows[i]) |
| 98 | } |
| 99 | if isEqual { |
| 100 | for i := 1; i < len(rows); i++ { |
| 101 | pool.Put(rows[i]) |