-
Notifications
You must be signed in to change notification settings - Fork 289
/
tuple_sketch_int64.sqlx
186 lines (163 loc) · 4.89 KB
/
tuple_sketch_int64.sqlx
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
config { hasOutput: true }
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
CREATE OR REPLACE AGGREGATE FUNCTION ${self()}(key_col INT64, value_col INT64, lg_k INT64 NOT AGGREGATE)
RETURNS BYTES
LANGUAGE js
OPTIONS (
library=["${JS_BUCKET}/tuple_sketch.mjs"],
description='''Aggregates id_col, value_col and log_k args and returns a tuple sketch.
For more details: https://datasketches.apache.org/docs/Tuple/TupleOverview.html'''
) AS '''
import ModuleFactory from "${JS_BUCKET}/tuple_sketch.mjs";
var Module = await ModuleFactory();
// Helper definitions
// shared buffer for serialization and deserialization
var BUFFER = {
ptr: 0,
size: 0,
};
function maxSize(lg_k) {
// see https://datasketches.apache.org/docs/Theta/ThetaSize.html
// 16 bytes per entry was calculated for theta sketch, since tuple sketch has an additional summary row of input datatype,
// doubling the size requirement as max bound
return 8 + 24 + 32 * (1 << lg_k);
}
function requireBuffer(size) {
if (BUFFER.size < size) {
releaseBuffer();
BUFFER.ptr = Module._malloc(size);
BUFFER.size = size;
}
return BUFFER;
}
function releaseBuffer() {
if (BUFFER.ptr) {
Module._free(BUFFER.ptr);
}
BUFFER.ptr = 0;
BUFFER.size = 0;
}
function destroyState(state) {
if (state.sketch) {
Module._update_sketch_destroy(state.sketch);
state.sketch = 0;
}
if (state.union) {
Module._tuple_union_destroy(state.union);
state.union = 0;
}
state.serialized = null;
}
function updateUnion(union, bytes) {
var buffer = requireBuffer(bytes.length);
Module.HEAPU8.subarray(buffer.ptr, buffer.ptr + buffer.size).set(bytes);
Module._tuple_union_update_buffer(union, buffer.ptr, bytes.length);
}
// UDAF interface
export function initialState(lg_k) {
lg_k = Module._clamp_lg_k(lg_k);
return {
sketch: Module._update_sketch_initialize(lg_k),
lg_k: lg_k,
serialized: null,
union: 0,
};
}
export function aggregate(state, key, value) {
if (!state.sketch) {
state.sketch = Module._update_sketch_initialize(state.lg_k);
}
Module._tuple_sketch_update_int64(state.sketch, key, value);
}
export function serialize(state) {
var buffer = requireBuffer(maxSize(state.lg_k));
var len = 0;
try {
if (state.sketch && state.serialized) {
// merge aggregated and serialized state
Module.HEAPU8
.subarray(buffer.ptr, buffer.ptr + buffer.size)
.set(state.serialized);
len = Module._tuple_combined_update_serialized(
buffer.ptr, buffer.size, state.serialized.length,
state.sketch, state.lg_k);
} else if (state.sketch) {
len = Module._update_sketch_serialize(
state.sketch, buffer.ptr, buffer.size);
} else if (state.union) {
len = Module._tuple_union_serialize_sketch(
state.union, buffer.ptr, buffer.size);
} else if (state.serialized) {
return {
lg_k: state.lg_k,
bytes: state.serialized,
}
} else {
throw new Error(
"Unexpected state in serialization " + JSON.stringify(state));
}
return {
lg_k: state.lg_k,
bytes: Module.HEAPU8.slice(buffer.ptr , buffer.ptr + len),
};
} finally {
destroyState(state);
}
}
export function deserialize(serialized) {
return {
sketch: 0,
union: 0,
serialized: serialized.bytes,
lg_k: serialized.lg_k,
};
}
export function merge(state, other_state) {
if (!state.union) {
state.union = Module._tuple_union_initialize(state.lg_k);
}
if (state.sketch) {
Module._tuple_union_update_sketch(state.union, state.sketch);
Module._update_sketch_destroy(state.sketch);
state.sketch = 0;
}
if (state.serialized) {
// consume it
updateUnion(state.union, state.serialized);
state.serialized = null;
}
if (other_state.union) {
throw new Error("other_state should not have union during merge()");
}
if (!other_state.sketch && !other_state.serialized) {
throw new Error("Expected sketch on other_state");
}
if (other_state.sketch) {
Module._tuple_union_update_sketch(state.union, other_state.sketch);
Module._update_sketch_destroy(other_state.sketch);
other_state.sketch = 0;
}
if (other_state.serialized) {
updateUnion(state.union, other_state.serialized);
other_state.serialized = null;
}
}
export function finalize(state) {
var result = serialize(state);
return result.bytes;
}
''';