-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathtest_uda.py
More file actions
347 lines (316 loc) · 8.88 KB
/
test_uda.py
File metadata and controls
347 lines (316 loc) · 8.88 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import unittest
from feldera import PipelineBuilder
from tests.platform.helper import gen_pipeline_name
from tests import TEST_CLIENT
from feldera.runtime_config import RuntimeConfig
from feldera.testutils import FELDERA_TEST_NUM_WORKERS, FELDERA_TEST_NUM_HOSTS
@gen_pipeline_name
def test_uda(pipeline_name):
sql = """
CREATE LINEAR AGGREGATE I128_SUM(s BINARY(16)) RETURNS BINARY(16);
CREATE TABLE T(x BINARY(16), y BINARY(16) NOT NULL);
CREATE MATERIALIZED VIEW V AS SELECT I128_SUM(x) AS S, I128_SUM(y) AS N, COUNT(*) AS C FROM T;
"""
toml = """
i256 = { version = "0.2.2", features = ["num-traits"] }
num-traits = "0.2.19"
"""
udfs = """
use i256::I256;
use feldera_sqllib::*;
use crate::{AddAssignByRef, AddByRef, HasZero, MulByRef, SizeOf, Tup3};
use derive_more::Add;
use num_traits::Zero;
use rkyv::Fallible;
use std::ops::AddAssign;
#[derive(Add, Clone, Debug, Default, PartialOrd, Ord, Eq, PartialEq, Hash)]
pub struct I256Wrapper {
pub data: I256,
}
impl SizeOf for I256Wrapper {
fn size_of_children(&self, context: &mut size_of::Context) {}
}
impl From<[u8; 32]> for I256Wrapper {
fn from(value: [u8; 32]) -> Self {
Self { data: I256::from_be_bytes(value) }
}
}
impl From<&[u8]> for I256Wrapper {
fn from(value: &[u8]) -> Self {
let mut padded = [0u8; 32];
// If original value is negative, pad with sign
if value[0] & 0x80 != 0 {
padded.fill(0xff);
}
let len = value.len();
if len > 32 {
panic!("Slice larger than target");
}
padded[32-len..].copy_from_slice(&value[..len]);
Self { data: I256::from_be_bytes(padded) }
}
}
impl MulByRef<Weight> for I256Wrapper {
type Output = Self;
fn mul_by_ref(&self, other: &Weight) -> Self::Output {
println!("Mul {:?} by {}", self, other);
Self {
data: self.data.checked_mul_i64(*other)
.expect("Overflow during multiplication"),
}
}
}
impl HasZero for I256Wrapper {
fn zero() -> Self {
Self { data: I256::zero() }
}
fn is_zero(&self) -> bool {
self.data.is_zero()
}
}
impl AddByRef for I256Wrapper {
fn add_by_ref(&self, other: &Self) -> Self {
Self { data: self.data.add(other.data) }
}
}
impl AddAssignByRef<Self> for I256Wrapper {
fn add_assign_by_ref(&mut self, other: &Self) {
self.data += other.data
}
}
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialOrd, Ord, Eq, PartialEq)]
pub struct ArchivedI256Wrapper {
pub bytes: [u8; 32],
}
impl rkyv::Archive for I256Wrapper {
type Archived = ArchivedI256Wrapper;
type Resolver = ();
#[inline]
unsafe fn resolve(&self, pos: usize, _: Self::Resolver, out: *mut Self::Archived) {
out.write(ArchivedI256Wrapper {
bytes: self.data.to_be_bytes(),
});
}
}
impl<S: Fallible + ?Sized> rkyv::Serialize<S> for I256Wrapper {
#[inline]
fn serialize(&self, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
Ok(())
}
}
impl<D: Fallible + ?Sized> rkyv::Deserialize<I256Wrapper, D> for ArchivedI256Wrapper {
#[inline]
fn deserialize(&self, _: &mut D) -> Result<I256Wrapper, D::Error> {
Ok(I256Wrapper::from(self.bytes))
}
}
pub type i128_sum_accumulator_type = I256Wrapper;
pub fn i128_sum_map(val: ByteArray) -> i128_sum_accumulator_type {
I256Wrapper::from(val.as_slice())
}
pub fn i128_sum_post(val: i128_sum_accumulator_type) -> ByteArray {
// Check for overflow
if val.data < I256::from(i128::MIN) || val.data > I256::from(i128::MAX) {
panic!("Result of aggregation {} does not fit in 128 bits", val.data);
}
ByteArray::new(&val.data.to_be_bytes()[16..])
}
"""
pipeline = PipelineBuilder(
TEST_CLIENT,
name=pipeline_name,
sql=sql,
udf_rust=udfs,
udf_toml=toml,
runtime_config=RuntimeConfig(
workers=FELDERA_TEST_NUM_WORKERS,
hosts=FELDERA_TEST_NUM_HOSTS,
),
).create_or_replace()
pipeline.start()
pipeline.input_json(
"t",
[
{
"insert": {
"x": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
"y": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
}
}
],
update_format="insert_delete",
)
pipeline.wait_for_idle()
output = list(pipeline.query("SELECT * FROM V;"))
assert output == [
{
"s": "00000000000000000000000000000001",
"n": "00000000000000000000000000000001",
"c": 1,
}
]
# Insert -1
pipeline.input_json(
"t",
[
{
"insert": {
"x": [
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
],
"y": [
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
],
}
}
],
update_format="insert_delete",
)
pipeline.wait_for_idle()
output = list(pipeline.query("SELECT * FROM V;"))
assert output == [
{
"s": "00000000000000000000000000000000",
"n": "00000000000000000000000000000000",
"c": 2,
}
]
pipeline.input_json(
"t",
[
{
"insert": {
"x": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2],
"y": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2],
}
}
],
update_format="insert_delete",
)
output = list(pipeline.query("SELECT * FROM V;"))
assert output == [
{
"s": "00000000000000000000000000000002",
"n": "00000000000000000000000000000002",
"c": 3,
}
]
pipeline.input_json(
"t",
[
{
"insert": {
"x": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3],
"y": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3],
}
}
],
update_format="insert_delete",
)
output = list(pipeline.query("SELECT * FROM V;"))
assert output == [
{
"s": "00000000000000000000000000000005",
"n": "00000000000000000000000000000005",
"c": 4,
}
]
pipeline.input_json(
"t",
[
{
"delete": {
"x": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
"y": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
}
},
{
"delete": {
"x": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2],
"y": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2],
}
},
{
"delete": {
"x": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3],
"y": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3],
}
},
{
"delete": {
"x": [
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
1,
],
"y": [
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
1,
],
}
},
],
update_format="insert_delete",
)
output = list(pipeline.query("SELECT * FROM V;"))
assert output == [{"s": None, "n": None, "c": 0}]
if __name__ == "__main__":
unittest.main()