risingwave_expr/window_function/
states.rs1use std::ops::{Deref, DerefMut};
16
17use itertools::Itertools;
18use risingwave_common::types::Datum;
19
20use super::state::{Readiness, StateEvictHint, StateKey, WindowState};
21use crate::Result;
22
23pub struct WindowStates(Vec<Box<dyn WindowState + Send + Sync>>);
24
25impl WindowStates {
26 pub fn new(states: Vec<Box<dyn WindowState + Send + Sync>>) -> Self {
27 assert!(!states.is_empty());
28 Self(states)
29 }
30
31 pub fn are_aligned(&self) -> bool {
33 self.0
34 .iter()
35 .map(|state| state.curr_window().key)
36 .all_equal()
37 }
38
39 pub fn curr_key(&self) -> Option<&StateKey> {
41 debug_assert!(self.are_aligned());
42 self.0.first().and_then(|state| state.curr_window().key)
43 }
44
45 pub fn are_ready(&self) -> bool {
47 debug_assert!(self.are_aligned());
48 self.0.iter().all(|state| state.curr_window().is_ready)
49 }
50
51 pub fn readiness(&self) -> Readiness {
54 debug_assert!(self.are_aligned());
55 let mut readiness = Readiness::Ready;
56 for state in &self.0 {
57 readiness = readiness.and(state.readiness());
58 if readiness == Readiness::WaitForInput {
59 break;
60 }
61 }
62 readiness
63 }
64
65 pub fn is_empty(&self) -> bool {
67 self.0.iter().all(|state| state.is_empty())
68 }
69
70 pub fn slide(&mut self) -> Result<(Vec<Datum>, StateEvictHint)> {
72 debug_assert!(self.are_aligned());
73 let mut output = Vec::with_capacity(self.0.len());
74 let mut evict_hint: Option<StateEvictHint> = None;
75 for state in &mut self.0 {
76 let (x_output, x_evict) = state.slide()?;
77 output.push(x_output);
78 evict_hint = match evict_hint {
79 Some(evict_hint) => Some(evict_hint.merge(x_evict)),
80 None => Some(x_evict),
81 };
82 }
83 Ok((
84 output,
85 evict_hint.expect("# of evict hints = # of window states"),
86 ))
87 }
88
89 pub fn slide_no_output(&mut self) -> Result<StateEvictHint> {
91 debug_assert!(self.are_aligned());
92 let mut evict_hint: Option<StateEvictHint> = None;
93 for state in &mut self.0 {
94 let x_evict = state.slide_no_output()?;
95 evict_hint = match evict_hint {
96 Some(evict_hint) => Some(evict_hint.merge(x_evict)),
97 None => Some(x_evict),
98 };
99 }
100 Ok(evict_hint.expect("# of evict hints = # of window states"))
101 }
102
103 pub fn slide_no_evict_hint(&mut self) -> Result<Vec<Datum>> {
105 debug_assert!(self.are_aligned());
106 let mut output = Vec::with_capacity(self.0.len());
107 for state in &mut self.0 {
108 let (x_output, _) = state.slide()?;
109 output.push(x_output);
110 }
111 Ok(output)
112 }
113
114 pub fn just_slide(&mut self) -> Result<()> {
116 debug_assert!(self.are_aligned());
117 for state in &mut self.0 {
118 state.slide_no_output()?;
119 }
120 Ok(())
121 }
122
123 pub fn just_slide_to(&mut self, curr_key: &StateKey) -> Result<()> {
127 while self.curr_key() != Some(curr_key) {
129 self.just_slide()?;
130 }
131 Ok(())
132 }
133}
134
135impl Deref for WindowStates {
136 type Target = Vec<Box<dyn WindowState + Send + Sync>>;
137
138 fn deref(&self) -> &Self::Target {
139 &self.0
140 }
141}
142
143impl DerefMut for WindowStates {
144 fn deref_mut(&mut self) -> &mut Self::Target {
145 &mut self.0
146 }
147}