forked from GoogleCloudPlatform/bigquery-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeta_sketch_bytes.sqlx
More file actions
191 lines (168 loc) · 4.99 KB
/
theta_sketch_bytes.sqlx
File metadata and controls
191 lines (168 loc) · 4.99 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
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()}(x BYTES, lg_k INT64 NOT AGGREGATE)
RETURNS BYTES
LANGUAGE js
OPTIONS (
library=["${JS_BUCKET}/theta_sketch.mjs"],
description='''Aggregates bytes_col, log_k args and returns a theta sketch.
This function can also be used for initializing theta sketch for string cols.
Just CAST( string_col AS BYTES FORMAT \'UTF-8\') and pass to this function.
For more details: https://datasketches.apache.org/docs/Theta/ThetaSketchFramework.html'''
) AS '''
import ModuleFactory from "${JS_BUCKET}/theta_sketch.mjs";
var Module = await ModuleFactory();
// Helper definitions
// shared buffer for serialization and deserialization
var BUFFER = {
ptr: 0,
size: 0,
};
// allocate a shared buffer for passing strings/bytes
var STRMAX = 256;
var STRBUF = Module._malloc(STRMAX);
function maxSize(lg_k) {
// see https://datasketches.apache.org/docs/Theta/ThetaSize.html
return 8 + 24 + 16 * (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._theta_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._theta_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, arg) {
if (!state.sketch) {
state.sketch = Module._update_sketch_initialize(state.lg_k);
}
// make sure that the argument will fit into the string buffer
if (arg.length > STRMAX) {
// allocate a bigger buffer
Module._free(STRBUF);
while (arg.length >= STRMAX) {
STRMAX *= 2;
}
STRBUF = Module._malloc(STRMAX);
}
Module.HEAPU8.subarray(STRBUF, STRBUF + arg.length).set(arg);
Module._theta_sketch_update_bytes(state.sketch, STRBUF, arg.length);
}
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._theta_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._theta_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._theta_union_initialize(state.lg_k);
}
if (state.sketch || other_state.sketch) {
throw new Error("update_sketch not expected during merge()");
}
if (other_state.union) {
throw new Error("other_state should not have union during merge()");
}
if (state.serialized) {
// consume it
updateUnion(state.union, state.serialized);
state.serialized = null;
}
if (other_state.serialized) {
updateUnion(state.union, other_state.serialized);
other_state.serialized = null;
} else {
throw new Error("Expected serialized sketch on other_state");
}
}
export function finalize(state) {
var result = serialize(state);
return result.bytes;
}
''';