FrameEndIdx returns the index of the first row after the frame.
(ctx context.Context, evalCtx *EvalContext)
| 273 | |
| 274 | // FrameEndIdx returns the index of the first row after the frame. |
| 275 | func (wfr *WindowFrameRun) FrameEndIdx(ctx context.Context, evalCtx *EvalContext) (int, error) { |
| 276 | if wfr.Frame == nil { |
| 277 | return wfr.DefaultFrameSize(), nil |
| 278 | } |
| 279 | switch wfr.Frame.Mode { |
| 280 | case RANGE: |
| 281 | if wfr.Frame.Bounds.EndBound == nil { |
| 282 | // We're using default value of CURRENT ROW when EndBound is omitted. |
| 283 | // Spec: in RANGE mode CURRENT ROW means that the frame ends with the current row's last peer. |
| 284 | return wfr.DefaultFrameSize(), nil |
| 285 | } |
| 286 | switch wfr.Frame.Bounds.EndBound.BoundType { |
| 287 | case OffsetPreceding: |
| 288 | value, err := wfr.getValueByOffset(ctx, evalCtx, wfr.EndBoundOffset, true /* negative */) |
| 289 | if err != nil { |
| 290 | return 0, err |
| 291 | } |
| 292 | if wfr.OrdDirection == encoding.Descending { |
| 293 | // We use binary search on [0, wfr.PartitionSize()) interval to find |
| 294 | // the first row whose value is smaller than 'value'. If such row is |
| 295 | // not found, then Search will correctly return wfr.PartitionSize(). |
| 296 | // Note that searching up to wfr.RowIdx is not correct in case of a |
| 297 | // zero offset (we need to include all peers of the current row). |
| 298 | return sort.Search(wfr.PartitionSize(), func(i int) bool { |
| 299 | if wfr.err != nil { |
| 300 | return false |
| 301 | } |
| 302 | valueAt, err := wfr.valueAt(ctx, i) |
| 303 | if err != nil { |
| 304 | wfr.err = err |
| 305 | return false |
| 306 | } |
| 307 | return valueAt.Compare(evalCtx, value) < 0 |
| 308 | }), wfr.err |
| 309 | } |
| 310 | // We use binary search on [0, wfr.PartitionSize()) interval to find |
| 311 | // the first row whose value is smaller than 'value'. If such row is |
| 312 | // not found, then Search will correctly return wfr.PartitionSize(). |
| 313 | // Note that searching up to wfr.RowIdx is not correct in case of a |
| 314 | // zero offset (we need to include all peers of the current row). |
| 315 | return sort.Search(wfr.PartitionSize(), func(i int) bool { |
| 316 | if wfr.err != nil { |
| 317 | return false |
| 318 | } |
| 319 | valueAt, err := wfr.valueAt(ctx, i) |
| 320 | if err != nil { |
| 321 | wfr.err = err |
| 322 | return false |
| 323 | } |
| 324 | return valueAt.Compare(evalCtx, value) > 0 |
| 325 | }), wfr.err |
| 326 | case CurrentRow: |
| 327 | // Spec: in RANGE mode CURRENT ROW means that the frame end with the current row's last peer. |
| 328 | return wfr.DefaultFrameSize(), nil |
| 329 | case OffsetFollowing: |
| 330 | value, err := wfr.getValueByOffset(ctx, evalCtx, wfr.EndBoundOffset, false /* negative */) |
| 331 | if err != nil { |
| 332 | return 0, err |
no test coverage detected