1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use std::cmp::Ordering;
use crate::{
CmpFunc, DBData, OrdIndexedZSet, RootCircuit, Stream,
dynamic::{DowncastTrait, DynData},
operator::{dynamic::group::RankCustomOrdFactories, group::WithCustomOrd},
};
impl<K, V> Stream<RootCircuit, OrdIndexedZSet<K, V>>
where
K: DBData,
V: DBData,
{
/// Rank elements in the group.
///
/// This operator implements the behavior of the following SQL pattern:
/// ```text
/// SELECT
/// ...,
/// RANK() OVER (PARTITION BY .. ORDER BY ...) AS rank
/// FROM table
/// ```
///
/// The `CF` type, `projection_func`, and `rank_cmp_func` function together establish the
/// ranking of values in the group:
/// * `CF` establishes a _total_ ordering of elements such that `v1 <= v2 =>
/// rank(v1) <= rank(v2)`.
/// * `projection_func` projects the value to a value that is used for the ranking
/// (e.g., the ORDER BY columns).
/// * `rank_cmp_func` compares the rank of two elements, one specified by its full value, the other by its projected value.
/// Must respect the following equivalence:
/// `rank_cmp_func(v1, v2) <=> rank(v1).cmp(rank(v2))`.
///
/// The `output_func` closure takes a value and its rank and produces an
/// output value.
///
/// ## Correctness
///
/// * `CF` must establish a total order over `V`, consistent with `impl E
/// for V`, i.e., `CF::cmp(v1, v2) == Equal <=> v1.eq(v2)`.
/// * `CF` must be consistent with `rank_cmp_func`, i.e.,
/// - `CF::cmp(v1, v2) == Less => rank_cmp_func(v1, v2) != Greater`.
/// - `CF::cmp(v1, v2) == Equal => rank_cmp_func(v1, v2) == Equal`.
/// - `CF::cmp(v1, v2) == Greater => rank_cmp_func(v1, v2) != Less`.
pub fn rank_custom_order<CF, RV, PF, EF, OF, OV>(
&self,
projection_func: PF,
rank_cmp_func: EF,
output_func: OF,
) -> Stream<RootCircuit, OrdIndexedZSet<K, OV>>
where
CF: CmpFunc<V>,
OV: DBData,
RV: DBData,
PF: Fn(&V) -> RV + 'static,
EF: Fn(&V, &RV) -> Ordering + 'static,
OF: Fn(i64, &V) -> OV + 'static,
{
self.rank_custom_order_persistent::<CF, RV, PF, EF, OF, OV>(
None,
projection_func,
rank_cmp_func,
output_func,
)
}
pub fn rank_custom_order_persistent<CF, RV, PF, EF, OF, OV>(
&self,
persistent_id: Option<&str>,
projection_func: PF,
rank_cmp_func: EF,
output_func: OF,
) -> Stream<RootCircuit, OrdIndexedZSet<K, OV>>
where
CF: CmpFunc<V>,
RV: DBData,
OV: DBData,
PF: Fn(&V) -> RV + 'static,
EF: Fn(&V, &RV) -> Ordering + 'static,
OF: Fn(i64, &V) -> OV + 'static,
{
let factories = RankCustomOrdFactories::<DynData, DynData, DynData, DynData>::new::<
K,
WithCustomOrd<V, CF>,
RV,
OV,
>();
self.inner()
.dyn_rank_custom_order_mono(
persistent_id,
&factories,
Box::new(
move |v1, v2: &mut DynData /* <WithCustomOrd<V, CF>> */| unsafe {
*v2.downcast_mut::<WithCustomOrd<V, CF>>() =
WithCustomOrd::new(v1.downcast::<V>().clone())
},
),
Box::new(move |v, rv| unsafe {
*rv.downcast_mut::<RV>() =
(projection_func)(&v.downcast::<WithCustomOrd<V, CF>>().val)
}),
Box::new(move |x, y| unsafe {
rank_cmp_func(
&x.downcast::<WithCustomOrd<V, CF>>().val,
y.downcast::<RV>(),
)
}),
Box::new(move |rank, v2, ov| unsafe {
*ov.downcast_mut() =
output_func(rank, &v2.downcast::<WithCustomOrd<V, CF>>().val)
}),
)
.typed()
}
/// Rank elements in the group.
///
/// This operator implements the behavior of the following SQL pattern:
/// ```text
/// SELECT
/// ...,
/// DENSE_RANK() OVER (PARTITION BY .. ORDER BY ...) AS rank
/// FROM table
/// ```
///
/// The `CF` type, `projection_func`, and `rank_cmp_func` function together establish the
/// ranking of values in the group:
/// * `CF` establishes a _total_ ordering of elements such that `v1 < v2 =>
/// rank(v1) <= rank(v2)`.
/// * `projection_func` projects the value to a value that is used for the ranking
/// (e.g., the ORDER BY columns).
/// * `rank_cmp_func` compares the rank of two elements, one specified by its full value, the other by its projected value.
/// Must respect the following equivalence:
/// `rank_cmp_func(v1, v2) <=> rank(v1).cmp(rank(v2))`.
///
/// The `output_func` closure takes a value and its rank and produces an
/// output value.
///
/// ## Correctness
///
/// * `CF` must establish a total order over `V`, consistent with `impl Eq
/// for V`, i.e., `CF::cmp(v1, v2) == Equal <=> v1.eq(v2)`.
/// * `CF` must be consistent with `rank_cmp_func`, i.e.,
/// - `CF::cmp(v1, v2) == Less => rank_cmp_func(v1, v2) != Greater`.
/// - `CF::cmp(v1, v2) == Equal => rank_cmp_func(v1, v2) == Equal`.
/// - `CF::cmp(v1, v2) == Greater => rank_cmp_func(v1, v2) != Less`.
pub fn dense_rank_custom_order<CF, RV, PF, EF, OF, OV>(
&self,
projection_func: PF,
rank_cmp_func: EF,
output_func: OF,
) -> Stream<RootCircuit, OrdIndexedZSet<K, OV>>
where
CF: CmpFunc<V>,
OV: DBData,
RV: DBData,
PF: Fn(&V) -> RV + 'static,
EF: Fn(&V, &RV) -> Ordering + 'static,
OF: Fn(i64, &V) -> OV + 'static,
{
self.dense_rank_custom_order_persistent::<CF, RV, PF, EF, OF, OV>(
None,
projection_func,
rank_cmp_func,
output_func,
)
}
pub fn dense_rank_custom_order_persistent<CF, RV, PF, EF, OF, OV>(
&self,
persistent_id: Option<&str>,
projection_func: PF,
rank_cmp_func: EF,
output_func: OF,
) -> Stream<RootCircuit, OrdIndexedZSet<K, OV>>
where
CF: CmpFunc<V>,
OV: DBData,
RV: DBData,
PF: Fn(&V) -> RV + 'static,
EF: Fn(&V, &RV) -> Ordering + 'static,
OF: Fn(i64, &V) -> OV + 'static,
{
let factories = RankCustomOrdFactories::<DynData, DynData, DynData, DynData>::new::<
K,
WithCustomOrd<V, CF>,
RV,
OV,
>();
self.inner()
.dyn_dense_rank_custom_order_mono(
persistent_id,
&factories,
Box::new(
move |v1, v2: &mut DynData /* <WithCustomOrd<V, CF>> */| unsafe {
*v2.downcast_mut::<WithCustomOrd<V, CF>>() =
WithCustomOrd::new(v1.downcast::<V>().clone())
},
),
Box::new(move |v, rv| unsafe {
*rv.downcast_mut::<RV>() =
projection_func(&v.downcast::<WithCustomOrd<V, CF>>().val)
}),
Box::new(move |x, y| unsafe {
rank_cmp_func(
&x.downcast::<WithCustomOrd<V, CF>>().val,
y.downcast::<RV>(),
)
}),
Box::new(move |rank, v2, ov| unsafe {
*ov.downcast_mut() =
output_func(rank, &v2.downcast::<WithCustomOrd<V, CF>>().val)
}),
)
.typed()
}
}