-
Notifications
You must be signed in to change notification settings - Fork 3
/
cpc_sketch_get_estimate_and_bounds_seed.sqlx
52 lines (48 loc) · 2.29 KB
/
cpc_sketch_get_estimate_and_bounds_seed.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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
config { hasOutput: true }
CREATE OR REPLACE FUNCTION ${self()}(sketch BYTES, num_std_devs BYTEINT, seed INT64)
RETURNS STRUCT<estimate FLOAT64, lower_bound FLOAT64, upper_bound FLOAT64>
LANGUAGE js
OPTIONS (
library=["${JS_BUCKET}/cpc_sketch.js"],
js_parameter_encoding_mode='STANDARD',
description = '''Gets cardinality estimate and bounds from given sketch.
Param sketch: The given sketch to query as bytes.
Param num_std_devs: The returned bounds will be based on the statistical confidence interval determined by the given number of standard deviations
from the returned estimate. This number may be one of {1,2,3}, where 1 represents 68% confidence, 2 represents 95% confidence and 3 represents 99.7% confidence.
For example, if the given num_std_devs = 2 and the returned values are {1000, 990, 1010} that means that with 95% confidence, the true value lies within the range [990, 1010].
Param seed: This is used to confirm that the given sketch was configured with the correct seed.
Returns: a STRUCT with 3 FLOAT64 values as {estimate, lower_bound, upper_bound}.
For more information:
- https://datasketches.apache.org/docs/CPC/CpcSketches.html
'''
) AS R"""
try {
const result = Module.cpc_sketch.getEstimateAndBounds(sketch, Number(num_std_devs), seed ? BigInt(seed) : BigInt(Module.DEFAULT_SEED));
return {
estimate: result.get(0),
lower_bound: result.get(1),
upper_bound: result.get(2)
};
} catch (e) {
if (e.message != null) throw e;
throw new Error(Module.getExceptionMessage(e));
}
""";