|
| 1 | +namespace HydraDB.Core.Exec; |
| 2 | + |
| 3 | +/// <summary> |
| 4 | +/// Positions inside a chunk that survived a filter. Filters never move rows: they only |
| 5 | +/// narrow this vector, so a chunk can be reused without copying row data. |
| 6 | +/// </summary> |
| 7 | +public sealed class SelectionVector |
| 8 | +{ |
| 9 | + public SelectionVector(int capacity) => Indices = new int[capacity]; |
| 10 | + |
| 11 | + public int[] Indices { get; } |
| 12 | + |
| 13 | + public int Count { get; private set; } |
| 14 | + |
| 15 | + public void Add(int index) => Indices[Count++] = index; |
| 16 | + |
| 17 | + public void Clear() => Count = 0; |
| 18 | + |
| 19 | + /// <summary>Selects the first <paramref name="count"/> rows in order.</summary> |
| 20 | + public void SelectAll(int count) |
| 21 | + { |
| 22 | + for (int i = 0; i < count; i++) Indices[i] = i; |
| 23 | + Count = count; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +/// <summary> |
| 28 | +/// A batch of rows in columnar-friendly form. Reference type on purpose: one instance is |
| 29 | +/// filled, filtered, and yielded repeatedly, so the scan allocates once per query rather |
| 30 | +/// than once per row. <see cref="Keys"/> holds the filter column already unboxed so |
| 31 | +/// <see cref="VectorFilter"/> can load it straight into a Vector<long>. |
| 32 | +/// </summary> |
| 33 | +public sealed class DataChunk |
| 34 | +{ |
| 35 | + public const int Capacity = 1024; |
| 36 | + |
| 37 | + public DataChunk(int columnCount) |
| 38 | + { |
| 39 | + ColumnCount = columnCount; |
| 40 | + Rows = new object?[Capacity][]; |
| 41 | + RowIds = new long[Capacity]; |
| 42 | + Keys = new long[Capacity]; |
| 43 | + KeyIsNull = new bool[Capacity]; |
| 44 | + Selection = new SelectionVector(Capacity); |
| 45 | + } |
| 46 | + |
| 47 | + public int ColumnCount { get; } |
| 48 | + |
| 49 | + /// <summary>References to the live row arrays. Never copies of them.</summary> |
| 50 | + public object?[][] Rows { get; } |
| 51 | + |
| 52 | + public long[] RowIds { get; } |
| 53 | + |
| 54 | + /// <summary>Unboxed filter column, laid out contiguously for SIMD loads.</summary> |
| 55 | + public long[] Keys { get; } |
| 56 | + |
| 57 | + public bool[] KeyIsNull { get; } |
| 58 | + |
| 59 | + public SelectionVector Selection { get; } |
| 60 | + |
| 61 | + public int Count { get; private set; } |
| 62 | + |
| 63 | + public bool IsFull => Count == Capacity; |
| 64 | + |
| 65 | + public void Reset() |
| 66 | + { |
| 67 | + Count = 0; |
| 68 | + Selection.Clear(); |
| 69 | + } |
| 70 | + |
| 71 | + public void Append(long rowId, object?[] values, int keyColumn) |
| 72 | + { |
| 73 | + int slot = Count++; |
| 74 | + Rows[slot] = values; |
| 75 | + RowIds[slot] = rowId; |
| 76 | + |
| 77 | + object? key = keyColumn >= 0 && keyColumn < values.Length ? values[keyColumn] : null; |
| 78 | + |
| 79 | + if (key is long unboxed) |
| 80 | + { |
| 81 | + Keys[slot] = unboxed; |
| 82 | + KeyIsNull[slot] = false; |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + // NULL never matches a comparison, so the payload value is irrelevant. |
| 87 | + Keys[slot] = 0; |
| 88 | + KeyIsNull[slot] = true; |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments