Skip to content

Commit 18dd81c

Browse files
committed
display RangeFrameBounds
Signed-off-by: Richard Chien <[email protected]>
1 parent 6e693fe commit 18dd81c

File tree

2 files changed

+52
-47
lines changed

2 files changed

+52
-47
lines changed

src/expr/core/src/window_function/call.rs

+51-47
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
use std::fmt::Display;
1616

1717
use enum_as_inner::EnumAsInner;
18+
use parse_display::Display;
1819
use risingwave_common::bail;
1920
use risingwave_common::types::{
20-
DataType, Datum, ScalarImpl, ScalarRefImpl, Sentinelled, ToDatumRef, ToOwnedDatum,
21+
DataType, Datum, ScalarImpl, ScalarRefImpl, Sentinelled, ToDatumRef, ToOwnedDatum, ToText,
2122
};
2223
use risingwave_common::util::sort_util::{Direction, OrderType};
2324
use risingwave_pb::expr::window_frame::{PbBound, PbExclusion};
2425
use risingwave_pb::expr::{PbWindowFrame, PbWindowFunction};
26+
use FrameBound::{CurrentRow, Following, Preceding, UnboundedFollowing, UnboundedPreceding};
2527

2628
use super::WindowFuncKind;
2729
use crate::aggregate::AggArgs;
@@ -185,7 +187,12 @@ pub struct RangeFrameBounds {
185187

186188
impl Display for RangeFrameBounds {
187189
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
188-
write!(f, "RANGE BETWEEN {:?} AND {:?}", self.start, self.end)?; // TODO(): display
190+
write!(
191+
f,
192+
"RANGE BETWEEN {} AND {}",
193+
self.start.for_display(),
194+
self.end.for_display()
195+
)?;
189196
Ok(())
190197
}
191198
}
@@ -311,22 +318,23 @@ impl RangeFrameBounds {
311318
}
312319
}
313320

314-
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, EnumAsInner)]
321+
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, EnumAsInner, Display)]
322+
#[display(style = "TITLE CASE")]
315323
pub enum FrameBound<T> {
316-
UnboundedPreceding, /* TODO(rc): maybe we can merge `UnboundedPreceding` and `UnboundedFollowing` */
324+
UnboundedPreceding,
325+
#[display("{0} PRECEDING")]
317326
Preceding(T),
318327
CurrentRow,
328+
#[display("{0} FOLLOWING")]
319329
Following(T),
320330
UnboundedFollowing,
321331
}
322332

323333
impl<T> FrameBound<T> {
324334
fn offset_value(&self) -> Option<&T> {
325335
match self {
326-
FrameBound::UnboundedPreceding
327-
| FrameBound::UnboundedFollowing
328-
| FrameBound::CurrentRow => None,
329-
FrameBound::Preceding(offset) | FrameBound::Following(offset) => Some(offset),
336+
UnboundedPreceding | UnboundedFollowing | CurrentRow => None,
337+
Preceding(offset) | Following(offset) => Some(offset),
330338
}
331339
}
332340

@@ -336,15 +344,14 @@ impl<T> FrameBound<T> {
336344
offset_checker: impl Fn(&T) -> Result<()>,
337345
) -> Result<()> {
338346
match (start, end) {
339-
(_, FrameBound::UnboundedPreceding) => bail!("frame end cannot be UNBOUNDED PRECEDING"),
340-
(FrameBound::UnboundedFollowing, _) => {
347+
(_, UnboundedPreceding) => bail!("frame end cannot be UNBOUNDED PRECEDING"),
348+
(UnboundedFollowing, _) => {
341349
bail!("frame start cannot be UNBOUNDED FOLLOWING")
342350
}
343-
(FrameBound::Following(_), FrameBound::CurrentRow)
344-
| (FrameBound::Following(_), FrameBound::Preceding(_)) => {
351+
(Following(_), CurrentRow) | (Following(_), Preceding(_)) => {
345352
bail!("frame starting from following row cannot have preceding rows")
346353
}
347-
(FrameBound::CurrentRow, FrameBound::Preceding(_)) => {
354+
(CurrentRow, Preceding(_)) => {
348355
bail!("frame starting from current row cannot have preceding rows")
349356
}
350357
_ => {}
@@ -366,11 +373,11 @@ where
366373
{
367374
fn reverse(self) -> FrameBound<T> {
368375
match self {
369-
FrameBound::UnboundedPreceding => FrameBound::UnboundedFollowing,
370-
FrameBound::Preceding(offset) => FrameBound::Following(offset),
371-
FrameBound::CurrentRow => FrameBound::CurrentRow,
372-
FrameBound::Following(offset) => FrameBound::Preceding(offset),
373-
FrameBound::UnboundedFollowing => FrameBound::UnboundedPreceding,
376+
UnboundedPreceding => UnboundedFollowing,
377+
Preceding(offset) => Following(offset),
378+
CurrentRow => CurrentRow,
379+
Following(offset) => Preceding(offset),
380+
UnboundedFollowing => UnboundedPreceding,
374381
}
375382
}
376383
}
@@ -413,27 +420,14 @@ impl FrameBound<usize> {
413420
}
414421
}
415422

416-
impl Display for FrameBound<usize> {
417-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
418-
match self {
419-
FrameBound::UnboundedPreceding => write!(f, "UNBOUNDED PRECEDING")?,
420-
FrameBound::Preceding(n) => write!(f, "{} PRECEDING", n)?,
421-
FrameBound::CurrentRow => write!(f, "CURRENT ROW")?,
422-
FrameBound::Following(n) => write!(f, "{} FOLLOWING", n)?,
423-
FrameBound::UnboundedFollowing => write!(f, "UNBOUNDED FOLLOWING")?,
424-
}
425-
Ok(())
426-
}
427-
}
428-
429423
impl FrameBound<usize> {
430424
/// Convert the bound to sized offset from current row. `None` if the bound is unbounded.
431425
pub fn to_offset(&self) -> Option<isize> {
432426
match self {
433-
FrameBound::UnboundedPreceding | FrameBound::UnboundedFollowing => None,
434-
FrameBound::CurrentRow => Some(0),
435-
FrameBound::Preceding(n) => Some(-(*n as isize)),
436-
FrameBound::Following(n) => Some(*n as isize),
427+
UnboundedPreceding | UnboundedFollowing => None,
428+
CurrentRow => Some(0),
429+
Preceding(n) => Some(-(*n as isize)),
430+
Following(n) => Some(*n as isize),
437431
}
438432
}
439433

@@ -451,11 +445,21 @@ impl FrameBound<usize> {
451445
impl FrameBound<ScalarImpl> {
452446
fn as_ref(&self) -> FrameBound<ScalarRefImpl<'_>> {
453447
match self {
454-
FrameBound::UnboundedPreceding => FrameBound::UnboundedPreceding,
455-
FrameBound::Preceding(offset) => FrameBound::Preceding(offset.as_scalar_ref_impl()),
456-
FrameBound::CurrentRow => FrameBound::CurrentRow,
457-
FrameBound::Following(offset) => FrameBound::Following(offset.as_scalar_ref_impl()),
458-
FrameBound::UnboundedFollowing => FrameBound::UnboundedFollowing,
448+
UnboundedPreceding => UnboundedPreceding,
449+
Preceding(offset) => Preceding(offset.as_scalar_ref_impl()),
450+
CurrentRow => CurrentRow,
451+
Following(offset) => Following(offset.as_scalar_ref_impl()),
452+
UnboundedFollowing => UnboundedFollowing,
453+
}
454+
}
455+
456+
fn for_display(&self) -> FrameBound<String> {
457+
match self {
458+
UnboundedPreceding => UnboundedPreceding,
459+
Preceding(offset) => Preceding(offset.as_scalar_ref_impl().to_text()),
460+
CurrentRow => CurrentRow,
461+
Following(offset) => Following(offset.as_scalar_ref_impl().to_text()),
462+
UnboundedFollowing => UnboundedFollowing,
459463
}
460464
}
461465
}
@@ -464,11 +468,11 @@ impl FrameBound<ScalarRefImpl<'_>> {
464468
fn bound_of(self, order_value: impl ToDatumRef, order_type: OrderType) -> Sentinelled<Datum> {
465469
let order_value = order_value.to_datum_ref();
466470
match (self, order_type.direction()) {
467-
(FrameBound::UnboundedPreceding, _) => Sentinelled::Smallest,
468-
(FrameBound::UnboundedFollowing, _) => Sentinelled::Largest,
469-
(FrameBound::CurrentRow, _) => Sentinelled::Normal(order_value.to_owned_datum()),
470-
(FrameBound::Preceding(offset), Direction::Ascending)
471-
| (FrameBound::Following(offset), Direction::Descending) => {
471+
(UnboundedPreceding, _) => Sentinelled::Smallest,
472+
(UnboundedFollowing, _) => Sentinelled::Largest,
473+
(CurrentRow, _) => Sentinelled::Normal(order_value.to_owned_datum()),
474+
(Preceding(offset), Direction::Ascending)
475+
| (Following(offset), Direction::Descending) => {
472476
// should SUBTRACT the offset
473477
if let Some(value) = order_value {
474478
let res = match (value, offset) {
@@ -490,8 +494,8 @@ impl FrameBound<ScalarRefImpl<'_>> {
490494
Sentinelled::Normal(None)
491495
}
492496
}
493-
(FrameBound::Following(offset), Direction::Ascending)
494-
| (FrameBound::Preceding(offset), Direction::Descending) => {
497+
(Following(offset), Direction::Ascending)
498+
| (Preceding(offset), Direction::Descending) => {
495499
// should ADD the offset
496500
if let Some(value) = order_value {
497501
let res = match (value, offset) {

src/stream/src/executor/over_window/general.rs

+1
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ impl<S: StateStore> OverWindowExecutor<S> {
246246
bounds @ risingwave_expr::window_function::FrameBounds::Range(..) => bounds,
247247
};
248248
call.frame.bounds = test_frame_bounds;
249+
println!("[rc] new frame: {}", call.frame);
249250
call
250251
});
251252

0 commit comments

Comments
 (0)