|
| 1 | +// Licensed under the Apache License, Version 2.0 (the "License") |
| 2 | +// you may not use this file except in compliance with the License. |
| 3 | +// You may obtain a copy of the License at |
| 4 | +// |
| 5 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +// |
| 7 | +// Unless required by applicable law or agreed to in writing, software |
| 8 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +// See the License for the specific language governing permissions and |
| 11 | +// limitations under the License. |
| 12 | + |
| 13 | +using FlowtideDotNet.Core.ColumnStore; |
| 14 | +using FlowtideDotNet.Core.ColumnStore.DataValues; |
| 15 | +using FlowtideDotNet.Core.ColumnStore.TreeStorage; |
| 16 | +using FlowtideDotNet.Core.Operators.Window; |
| 17 | +using FlowtideDotNet.Storage.Memory; |
| 18 | +using FlowtideDotNet.Storage.StateManager; |
| 19 | +using FlowtideDotNet.Storage.Tree; |
| 20 | +using FlowtideDotNet.Substrait.Expressions; |
| 21 | +using System; |
| 22 | +using System.Collections.Generic; |
| 23 | +using System.Diagnostics; |
| 24 | +using System.Linq; |
| 25 | +using System.Text; |
| 26 | +using System.Threading.Tasks; |
| 27 | + |
| 28 | +namespace FlowtideDotNet.Core.Compute.Columnar.Functions.WindowFunctions |
| 29 | +{ |
| 30 | + internal class LeadWindowFunctionDefinition : WindowFunctionDefinition |
| 31 | + { |
| 32 | + public override IWindowFunction Create(WindowFunction aggregateFunction, IFunctionsRegister functionsRegister) |
| 33 | + { |
| 34 | + if (aggregateFunction.Arguments.Count < 1) |
| 35 | + { |
| 36 | + throw new ArgumentException("Lead function requires at least one argument"); |
| 37 | + } |
| 38 | + |
| 39 | + var leadValueFunc = ColumnProjectCompiler.CompileToValue(aggregateFunction.Arguments[0], functionsRegister); |
| 40 | + |
| 41 | + Func<EventBatchData, int, IDataValue>? leadOffsetFunc = default; |
| 42 | + if (aggregateFunction.Arguments.Count > 1) |
| 43 | + { |
| 44 | + leadOffsetFunc = ColumnProjectCompiler.CompileToValue(aggregateFunction.Arguments[1], functionsRegister); |
| 45 | + } |
| 46 | + |
| 47 | + Func<EventBatchData, int, IDataValue> ? defaultFunc = default; |
| 48 | + if (aggregateFunction.Arguments.Count > 2) |
| 49 | + { |
| 50 | + defaultFunc = ColumnProjectCompiler.CompileToValue(aggregateFunction.Arguments[2], functionsRegister); |
| 51 | + } |
| 52 | + |
| 53 | + return new LeadWindowFunction(leadValueFunc, leadOffsetFunc, defaultFunc); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + internal class LeadWindowFunction : IWindowFunction |
| 58 | + { |
| 59 | + private IWindowAddOutputRow? _addOutputRow; |
| 60 | + private IBPlusTreeIterator<ColumnRowReference, WindowValue, ColumnKeyStorageContainer, WindowValueContainer>? _updateIterator; |
| 61 | + private IBPlusTreeIterator<ColumnRowReference, WindowValue, ColumnKeyStorageContainer, WindowValueContainer>? _windowIterator; |
| 62 | + private PartitionIterator? _updatePartitionIterator; |
| 63 | + private PartitionIterator? _windowPartitionIterator; |
| 64 | + |
| 65 | + private readonly Func<EventBatchData, int, IDataValue> _leadValueFunc; |
| 66 | + private readonly Func<EventBatchData, int, IDataValue>? _leadOffsetFunc; |
| 67 | + private readonly Func<EventBatchData, int, IDataValue>? _defaultValueFunc; |
| 68 | + |
| 69 | + public LeadWindowFunction( |
| 70 | + Func<EventBatchData, int, IDataValue> leadValueFunc, |
| 71 | + Func<EventBatchData, int, IDataValue>? leadOffsetFunc, |
| 72 | + Func<EventBatchData, int, IDataValue>? defaultValueFunc) |
| 73 | + { |
| 74 | + _leadValueFunc = leadValueFunc; |
| 75 | + _leadOffsetFunc = leadOffsetFunc; |
| 76 | + _defaultValueFunc = defaultValueFunc; |
| 77 | + } |
| 78 | + |
| 79 | + public async IAsyncEnumerable<EventBatchWeighted> ComputePartition(ColumnRowReference partitionValues) |
| 80 | + { |
| 81 | + Debug.Assert(_addOutputRow != null); |
| 82 | + Debug.Assert(_windowPartitionIterator != null); |
| 83 | + Debug.Assert(_updatePartitionIterator != null); |
| 84 | + |
| 85 | + await _windowPartitionIterator.Reset(partitionValues); |
| 86 | + _updatePartitionIterator.ResetCopyFrom(_windowPartitionIterator); |
| 87 | + |
| 88 | + var windowEnumerator = _windowPartitionIterator.GetAsyncEnumerator(); |
| 89 | + var updateEnumerator = _updatePartitionIterator.GetAsyncEnumerator(); |
| 90 | + |
| 91 | + long updateRowIndex = 0; |
| 92 | + long windowRowIndex = 0; |
| 93 | + |
| 94 | + var currentValue = new DataValueContainer(); |
| 95 | + currentValue._type = ArrowTypeId.Null; |
| 96 | + |
| 97 | + while (await updateEnumerator.MoveNextAsync()) |
| 98 | + { |
| 99 | + int rowOffset = 1; |
| 100 | + if (_leadOffsetFunc != null) |
| 101 | + { |
| 102 | + var offsetValue = _leadOffsetFunc(updateEnumerator.Current.Key.referenceBatch, updateEnumerator.Current.Key.RowIndex); |
| 103 | + if (offsetValue is Int64Value int64Value) |
| 104 | + { |
| 105 | + rowOffset = (int)int64Value.AsLong; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | + if (windowRowIndex > (updateRowIndex + rowOffset)) |
| 111 | + { |
| 112 | + // Must reset the window enumerator to the beginning, this can be done faster, but at this |
| 113 | + // time it is an edge case since it requires dynamic row offset |
| 114 | + _windowPartitionIterator.ResetCopyFrom(_updatePartitionIterator); |
| 115 | + windowEnumerator = _windowPartitionIterator.GetAsyncEnumerator(); |
| 116 | + windowRowIndex = 0; |
| 117 | + } |
| 118 | + |
| 119 | + bool movedNext = false; |
| 120 | + while (windowRowIndex <= (updateRowIndex + rowOffset)) |
| 121 | + { |
| 122 | + movedNext = await windowEnumerator.MoveNextAsync(); |
| 123 | + if (!movedNext) |
| 124 | + { |
| 125 | + break; |
| 126 | + } |
| 127 | + windowRowIndex++; |
| 128 | + } |
| 129 | + |
| 130 | + IDataValue? val; |
| 131 | + if (!movedNext) |
| 132 | + { |
| 133 | + if (_defaultValueFunc != null) |
| 134 | + { |
| 135 | + val = _defaultValueFunc(updateEnumerator.Current.Key.referenceBatch, updateEnumerator.Current.Key.RowIndex); |
| 136 | + } |
| 137 | + else |
| 138 | + { |
| 139 | + val = NullValue.Instance; |
| 140 | + } |
| 141 | + } |
| 142 | + else |
| 143 | + { |
| 144 | + val = _leadValueFunc(windowEnumerator.Current.Key.referenceBatch, windowEnumerator.Current.Key.RowIndex); |
| 145 | + } |
| 146 | + |
| 147 | + updateRowIndex++; |
| 148 | + |
| 149 | + updateEnumerator.Current.Value.UpdateStateValue(val); |
| 150 | + |
| 151 | + if (_addOutputRow.Count >= 100) |
| 152 | + { |
| 153 | + yield return _addOutputRow.GetCurrentBatch(); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + if (_addOutputRow.Count > 0) |
| 158 | + { |
| 159 | + yield return _addOutputRow.GetCurrentBatch(); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + public Task Initialize(IBPlusTree<ColumnRowReference, WindowValue, ColumnKeyStorageContainer, WindowValueContainer> persistentTree, List<int> partitionColumns, IMemoryAllocator memoryAllocator, IStateManagerClient stateManagerClient, IWindowAddOutputRow addOutputRow) |
| 164 | + { |
| 165 | + _addOutputRow = addOutputRow; |
| 166 | + _windowIterator = persistentTree.CreateIterator(); |
| 167 | + _updateIterator = persistentTree.CreateIterator(); |
| 168 | + |
| 169 | + _updatePartitionIterator = new PartitionIterator(_updateIterator, partitionColumns, addOutputRow); |
| 170 | + _windowPartitionIterator = new PartitionIterator(_windowIterator, partitionColumns); |
| 171 | + |
| 172 | + return Task.CompletedTask; |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments