Path: redb/redb.Core/Query/Window/Frame.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Core/Query/Window/Frame.cs
----
namespace redb.Core.Query.Window;
///
/// Window boundary type
///
public enum FrameBoundType
{
UnboundedPreceding, // UNBOUNDED PRECEDING
Preceding, // N PRECEDING
CurrentRow, // CURRENT ROW
Following, // N FOLLOWING
UnboundedFollowing // UNBOUNDED FOLLOWING
}
///
/// Frame type: ROWS or RANGE
///
public enum FrameType
{
Rows, // ROWS BETWEEN
Range // RANGE BETWEEN
}
///
/// Frame boundary
///
public class FrameBound
{
public FrameBoundType Type { get; set; }
public int? Offset { get; set; } // For N PRECEDING / N FOLLOWING
public static FrameBound UnboundedPreceding() => new() { Type = FrameBoundType.UnboundedPreceding };
public static FrameBound Preceding(int n) => new() { Type = FrameBoundType.Preceding, Offset = n };
public static FrameBound CurrentRow() => new() { Type = FrameBoundType.CurrentRow };
public static FrameBound Following(int n) => new() { Type = FrameBoundType.Following, Offset = n };
public static FrameBound UnboundedFollowing() => new() { Type = FrameBoundType.UnboundedFollowing };
}
///
/// Frame specification for window functions
///
public class FrameSpec
{
public FrameType Type { get; set; } = FrameType.Rows;
public FrameBound Start { get; set; } = FrameBound.UnboundedPreceding();
public FrameBound End { get; set; } = FrameBound.CurrentRow();
}
///
/// Builder for creating FrameSpec
///
public class FrameBuilder
{
private readonly FrameSpec _spec = new();
public FrameBuilder(FrameType type = FrameType.Rows)
{
_spec.Type = type;
}
///
/// ROWS BETWEEN n PRECEDING AND CURRENT ROW
///
public FrameBuilder Preceding(int n)
{
_spec.Start = FrameBound.Preceding(n);
_spec.End = FrameBound.CurrentRow();
return this;
}
///
/// ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
///
public FrameBuilder UnboundedPreceding()
{
_spec.Start = FrameBound.UnboundedPreceding();
_spec.End = FrameBound.CurrentRow();
return this;
}
///
/// ... AND n FOLLOWING
///
public FrameBuilder AndFollowing(int n)
{
_spec.End = FrameBound.Following(n);
return this;
}
///
/// ... AND CURRENT ROW
///
public FrameBuilder AndCurrentRow()
{
_spec.End = FrameBound.CurrentRow();
return this;
}
///
/// ... AND UNBOUNDED FOLLOWING
///
public FrameBuilder AndUnboundedFollowing()
{
_spec.End = FrameBound.UnboundedFollowing();
return this;
}
public FrameSpec Build() => _spec;
public static implicit operator FrameSpec(FrameBuilder builder) => builder.Build();
}
///
/// Static helper for creating frames
///
public static class Frame
{
///
/// ROWS BETWEEN ...
///
public static FrameBuilder Rows() => new(FrameType.Rows);
///
/// ROWS BETWEEN n PRECEDING AND CURRENT ROW (sliding window)
///
public static FrameBuilder Rows(int preceding) => new FrameBuilder(FrameType.Rows).Preceding(preceding);
///
/// RANGE BETWEEN ...
///
public static FrameBuilder Range() => new(FrameType.Range);
}