You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datasketches.apache.org by al...@apache.org on 2024/10/03 02:32:32 UTC

(datasketches-bigquery) branch additional_functions created (now 6d05d4c)

This is an automated email from the ASF dual-hosted git repository.

alsay pushed a change to branch additional_functions
in repository https://gitbox.apache.org/repos/asf/datasketches-bigquery.git


      at 6d05d4c  added some convenience functions

This branch includes the following new commits:

     new 6d05d4c  added some convenience functions

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org


(datasketches-bigquery) 01/01: added some convenience functions

Posted by al...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

alsay pushed a commit to branch additional_functions
in repository https://gitbox.apache.org/repos/asf/datasketches-bigquery.git

commit 6d05d4cd5dfd38c0b84aee87c7c59f092950b9a2
Author: AlexanderSaydakov <Al...@users.noreply.github.com>
AuthorDate: Wed Oct 2 19:32:08 2024 -0700

    added some convenience functions
---
 kll/kll_sketch.cpp                                 | 18 +++++---
 kll/sqlx/kll_sketch_float_build.sqlx               |  6 +--
 kll/sqlx/kll_sketch_float_get_cdf.sqlx             |  7 +--
 .../sqlx/kll_sketch_float_get_max_value.sqlx       | 34 +++++++++------
 .../sqlx/kll_sketch_float_get_min_value.sqlx       | 34 +++++++++------
 ...kll_sketch_float_get_normalized_rank_error.sqlx | 50 ++++++++++++++++++++++
 .../sqlx/kll_sketch_float_get_num_retained.sqlx    | 36 ++++++++++------
 kll/sqlx/kll_sketch_float_get_pmf.sqlx             |  7 +--
 kll/sqlx/kll_sketch_float_merge.sqlx               |  8 ++--
 kll/test/kll_sketch_test.sql                       | 11 +++++
 .../sqlx/theta_sketch_get_num_retained.sqlx        | 15 +++----
 .../sqlx/theta_sketch_get_num_retained_seed.sqlx   | 32 ++++++++------
 .../sqlx/theta_sketch_get_theta.sqlx               | 13 +++---
 .../sqlx/theta_sketch_get_theta_seed.sqlx          | 30 +++++++------
 theta/test/theta_sketch_test.sql                   | 18 ++++++++
 theta/theta_sketch.cpp                             |  6 +++
 tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx    |  1 -
 ...lx => tuple_sketch_int64_get_num_retained.sqlx} |  9 ++--
 ... tuple_sketch_int64_get_num_retained_seed.sqlx} | 24 +++++++----
 ...mate.sqlx => tuple_sketch_int64_get_theta.sqlx} |  7 ++-
 ...sqlx => tuple_sketch_int64_get_theta_seed.sqlx} | 21 ++++++---
 tuple/test/tuple_sketch_int_test.sql               | 18 ++++++++
 tuple/tuple_sketch_int64.cpp                       |  6 +++
 23 files changed, 282 insertions(+), 129 deletions(-)

diff --git a/kll/kll_sketch.cpp b/kll/kll_sketch.cpp
index 06f3ea1..f68a167 100644
--- a/kll/kll_sketch.cpp
+++ b/kll/kll_sketch.cpp
@@ -32,7 +32,6 @@ EMSCRIPTEN_BINDINGS(kll_sketch_float) {
   }));
 
   emscripten::register_vector<float>("VectorFloat");
-  emscripten::register_vector<double>("VectorDouble");
 
   emscripten::constant("DEFAULT_K", datasketches::kll_constants::DEFAULT_K);
 
@@ -44,12 +43,9 @@ EMSCRIPTEN_BINDINGS(kll_sketch_float) {
     .function("update", emscripten::optional_override([](kll_sketch_float& self, float value) {
       self.update(value);
     }))
-    .function("mergeBytes", emscripten::optional_override([](kll_sketch_float& self, const std::string& bytes) {
+    .function("merge", emscripten::optional_override([](kll_sketch_float& self, const std::string& bytes) {
       self.merge(kll_sketch_float::deserialize(bytes.data(), bytes.size()));
     }))
-    .function("mergeBuffer", emscripten::optional_override([](kll_sketch_float& self, intptr_t bytes, size_t size) {
-      self.merge(kll_sketch_float::deserialize(reinterpret_cast<void*>(bytes), size));
-    }))
     .function("serializeAsUint8Array", emscripten::optional_override([](const kll_sketch_float& self) {
       auto bytes = self.serialize();
       return Uint8Array.new_(emscripten::typed_memory_view(bytes.size(), bytes.data()));
@@ -58,17 +54,25 @@ EMSCRIPTEN_BINDINGS(kll_sketch_float) {
       return new kll_sketch_float(kll_sketch_float::deserialize(bytes.data(), bytes.size()));
     }), emscripten::allow_raw_pointers())
     .function("getN", &kll_sketch_float::get_n)
+    .function("getNumRetained", &kll_sketch_float::get_num_retained)
+    .function("getMinValue", &kll_sketch_float::get_min_item)
+    .function("getMaxValue", &kll_sketch_float::get_max_item)
     .function("getRank", &kll_sketch_float::get_rank)
     .function("getQuantile", &kll_sketch_float::get_quantile)
     .function("getPMF", emscripten::optional_override([](const kll_sketch_float& self, const std::vector<float>& split_points, bool inclusive) {
-      return self.get_PMF(split_points.data(), split_points.size(), inclusive);
+      const auto pmf = self.get_PMF(split_points.data(), split_points.size(), inclusive);
+      return emscripten::val::array(pmf.begin(), pmf.end());
     }))
     .function("getCDF", emscripten::optional_override([](const kll_sketch_float& self, const std::vector<float>& split_points, bool inclusive) {
-      return self.get_CDF(split_points.data(), split_points.size(), inclusive);
+      const auto cdf = self.get_CDF(split_points.data(), split_points.size(), inclusive);
+      return emscripten::val::array(cdf.begin(), cdf.end());
     }))
     .function("toString", emscripten::optional_override([](const kll_sketch_float& self) {
       return self.to_string();
     }))
+    .function("getNormalizedRankError", emscripten::optional_override([](const kll_sketch_float& self, bool pmf) {
+      return self.get_normalized_rank_error(pmf);
+    }))
     ;
 
   emscripten::function("kolmogorovSmirnovTest", emscripten::optional_override([](const std::string& sketch_bytes1, const std::string& sketch_bytes2, double pvalue) {
diff --git a/kll/sqlx/kll_sketch_float_build.sqlx b/kll/sqlx/kll_sketch_float_build.sqlx
index b285be0..e8eae78 100644
--- a/kll/sqlx/kll_sketch_float_build.sqlx
+++ b/kll/sqlx/kll_sketch_float_build.sqlx
@@ -70,7 +70,7 @@ export function serialize(state) {
     // for prior transition deserialize-aggregate
     // merge aggregated and serialized state
     if (state.sketch != null && state.serialized != null) {
-      sketch.mergeBytes(state.serialized);
+      sketch.merge(state.serialized);
     }
     return {
       k: state.k,
@@ -94,11 +94,11 @@ export function merge(state, other_state) {
       state.sketch = new Module.kll_sketch_float(state.k);
     }
     if (state.serialized != null) {
-      state.sketch.mergeBytes(state.serialized);
+      state.sketch.merge(state.serialized);
       state.serialized = null;
     }
     if (other_state.serialized != null) {
-      state.sketch.mergeBytes(other_state.serialized);
+      state.sketch.merge(other_state.serialized);
       other_state.serialized = null;
     }
   } catch (e) {
diff --git a/kll/sqlx/kll_sketch_float_get_cdf.sqlx b/kll/sqlx/kll_sketch_float_get_cdf.sqlx
index e631cfe..35eb6e5 100644
--- a/kll/sqlx/kll_sketch_float_get_cdf.sqlx
+++ b/kll/sqlx/kll_sketch_float_get_cdf.sqlx
@@ -57,13 +57,10 @@ try {
   var sketchObject = null;
   try {
     sketchObject = Module.kll_sketch_float.deserialize(sketch);
+    if (sketchObject.isEmpty()) return null;
     var vectorFloat = new Module.VectorFloat();
     split_points.forEach(value => vectorFloat.push_back(value));
-    if (sketchObject.isEmpty()) return null;
-    const cdf = sketchObject.getCDF(vectorFloat, inclusive);
-    var array = [];
-    for (let i = 0; i < cdf.size(); i++) array.push(cdf.get(i));
-    return array;
+    return sketchObject.getCDF(vectorFloat, inclusive);
   } finally {
     if (sketchObject != null) sketchObject.delete();
   }
diff --git a/tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx b/kll/sqlx/kll_sketch_float_get_max_value.sqlx
similarity index 60%
copy from tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx
copy to kll/sqlx/kll_sketch_float_get_max_value.sqlx
index 8827d19..1bf4d48 100644
--- a/tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx
+++ b/kll/sqlx/kll_sketch_float_get_max_value.sqlx
@@ -21,19 +21,29 @@ config { hasOutput: true }
 
 CREATE OR REPLACE FUNCTION ${self()}(sketch BYTES)
 RETURNS FLOAT64
+LANGUAGE js
 OPTIONS (
-  library=["gs://$GCS_BUCKET/tuple_sketch_int64.js"],
-  description = '''Returns the cardinality estimate of the given Tuple Sketch.
-Note that cardinality estimation accuracy, plots, and error tables are the same as the Theta Sketch.
-This function only applies to Tuple Sketches with an INT64 summary column.
-  
-Param sketch: the given Tuple Sketch. This may not be NULL.
-Defaults: seed = 9001.
-Returns: the cardinality estimate of the given Tuple Sketch
+  library=["gs://$GCS_BUCKET/kll_sketch.js"],
+  js_parameter_encoding_mode='STANDARD',
+  description = '''Returns the maximum value of the input stream.
+
+Param sketch: the given sketch as BYTES.
+Returns: max value as FLOAT64
 
 For more information:
- - https://datasketches.apache.org/docs/Tuple/TupleSketches.html
+ - https://datasketches.apache.org/docs/KLL/KLLSketch.html
 '''
-) AS (
-  $BQ_DATASET.tuple_sketch_int64_get_estimate_seed(sketch, NULL)
-);
+) AS R"""
+try {
+  var sketchObject = null;
+  try {
+    sketchObject = Module.kll_sketch_float.deserialize(sketch);
+    return sketchObject.getMaxValue();
+  } finally {
+    if (sketchObject != null) sketchObject.delete();
+  }
+} catch (e) {
+  if (e.message != null) throw e;
+  throw new Error(Module.getExceptionMessage(e));
+}
+""";
diff --git a/tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx b/kll/sqlx/kll_sketch_float_get_min_value.sqlx
similarity index 60%
copy from tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx
copy to kll/sqlx/kll_sketch_float_get_min_value.sqlx
index 8827d19..9e4aa62 100644
--- a/tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx
+++ b/kll/sqlx/kll_sketch_float_get_min_value.sqlx
@@ -21,19 +21,29 @@ config { hasOutput: true }
 
 CREATE OR REPLACE FUNCTION ${self()}(sketch BYTES)
 RETURNS FLOAT64
+LANGUAGE js
 OPTIONS (
-  library=["gs://$GCS_BUCKET/tuple_sketch_int64.js"],
-  description = '''Returns the cardinality estimate of the given Tuple Sketch.
-Note that cardinality estimation accuracy, plots, and error tables are the same as the Theta Sketch.
-This function only applies to Tuple Sketches with an INT64 summary column.
-  
-Param sketch: the given Tuple Sketch. This may not be NULL.
-Defaults: seed = 9001.
-Returns: the cardinality estimate of the given Tuple Sketch
+  library=["gs://$GCS_BUCKET/kll_sketch.js"],
+  js_parameter_encoding_mode='STANDARD',
+  description = '''Returns the minimum value of the input stream.
+
+Param sketch: the given sketch as BYTES.
+Returns: min value as FLOAT64
 
 For more information:
- - https://datasketches.apache.org/docs/Tuple/TupleSketches.html
+ - https://datasketches.apache.org/docs/KLL/KLLSketch.html
 '''
-) AS (
-  $BQ_DATASET.tuple_sketch_int64_get_estimate_seed(sketch, NULL)
-);
+) AS R"""
+try {
+  var sketchObject = null;
+  try {
+    sketchObject = Module.kll_sketch_float.deserialize(sketch);
+    return sketchObject.getMinValue();
+  } finally {
+    if (sketchObject != null) sketchObject.delete();
+  }
+} catch (e) {
+  if (e.message != null) throw e;
+  throw new Error(Module.getExceptionMessage(e));
+}
+""";
diff --git a/kll/sqlx/kll_sketch_float_get_normalized_rank_error.sqlx b/kll/sqlx/kll_sketch_float_get_normalized_rank_error.sqlx
new file mode 100644
index 0000000..2d276df
--- /dev/null
+++ b/kll/sqlx/kll_sketch_float_get_normalized_rank_error.sqlx
@@ -0,0 +1,50 @@
+/*
+ * 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, pmf BOOL)
+RETURNS FLOAT64
+LANGUAGE js
+OPTIONS (
+  library=["gs://$GCS_BUCKET/kll_sketch.js"],
+  js_parameter_encoding_mode='STANDARD',
+  description = '''Returns the approximate rank error of the given sketch normalized as a fraction between zero and one.
+Param sketch: the given sketch as BYTES.
+Param pmf: if true, returns the "double-sided" normalized rank error for the get_PMF() function.
+Otherwise, it is the "single-sided" normalized rank error for all the other queries.
+Returns: normalized rank error as FLOAT64
+
+For more information:
+ - https://datasketches.apache.org/docs/KLL/KLLSketch.html
+'''
+) AS R"""
+try {
+  var sketchObject = null;
+  try {
+    sketchObject = Module.kll_sketch_float.deserialize(sketch);
+    return sketchObject.getNormalizedRankError(pmf);
+  } finally {
+    if (sketchObject != null) sketchObject.delete();
+  }
+} catch (e) {
+  if (e.message != null) throw e;
+  throw new Error(Module.getExceptionMessage(e));
+}
+""";
diff --git a/tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx b/kll/sqlx/kll_sketch_float_get_num_retained.sqlx
similarity index 58%
copy from tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx
copy to kll/sqlx/kll_sketch_float_get_num_retained.sqlx
index 8827d19..b9c83f8 100644
--- a/tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx
+++ b/kll/sqlx/kll_sketch_float_get_num_retained.sqlx
@@ -20,20 +20,30 @@
 config { hasOutput: true }
 
 CREATE OR REPLACE FUNCTION ${self()}(sketch BYTES)
-RETURNS FLOAT64
+RETURNS INT64
+LANGUAGE js
 OPTIONS (
-  library=["gs://$GCS_BUCKET/tuple_sketch_int64.js"],
-  description = '''Returns the cardinality estimate of the given Tuple Sketch.
-Note that cardinality estimation accuracy, plots, and error tables are the same as the Theta Sketch.
-This function only applies to Tuple Sketches with an INT64 summary column.
-  
-Param sketch: the given Tuple Sketch. This may not be NULL.
-Defaults: seed = 9001.
-Returns: the cardinality estimate of the given Tuple Sketch
+  library=["gs://$GCS_BUCKET/kll_sketch.js"],
+  js_parameter_encoding_mode='STANDARD',
+  description = '''Returns the number of retained items (samples) in the sketch.
+
+Param sketch: the given sketch as BYTES.
+Returns: number of retained items as INT64
 
 For more information:
- - https://datasketches.apache.org/docs/Tuple/TupleSketches.html
+ - https://datasketches.apache.org/docs/KLL/KLLSketch.html
 '''
-) AS (
-  $BQ_DATASET.tuple_sketch_int64_get_estimate_seed(sketch, NULL)
-);
+) AS R"""
+try {
+  var sketchObject = null;
+  try {
+    sketchObject = Module.kll_sketch_float.deserialize(sketch);
+    return sketchObject.getNumRetained();
+  } finally {
+    if (sketchObject != null) sketchObject.delete();
+  }
+} catch (e) {
+  if (e.message != null) throw e;
+  throw new Error(Module.getExceptionMessage(e));
+}
+""";
diff --git a/kll/sqlx/kll_sketch_float_get_pmf.sqlx b/kll/sqlx/kll_sketch_float_get_pmf.sqlx
index 9f53f16..ee050fc 100644
--- a/kll/sqlx/kll_sketch_float_get_pmf.sqlx
+++ b/kll/sqlx/kll_sketch_float_get_pmf.sqlx
@@ -59,13 +59,10 @@ try {
   var sketchObject = null;
   try {
     sketchObject = Module.kll_sketch_float.deserialize(sketch);
+    if (sketchObject.isEmpty()) return null;
     var vectorFloat = new Module.VectorFloat();
     split_points.forEach(value => vectorFloat.push_back(value));
-    if (sketchObject.isEmpty()) return null;
-    var pmf = sketchObject.getPMF(vectorFloat, inclusive);
-    var array = [];
-    for (let i = 0; i < pmf.size(); i++) array.push(pmf.get(i));
-    return array;
+    return sketchObject.getPMF(vectorFloat, inclusive);
   } finally {
     if (sketchObject != null) sketchObject.delete();
   }
diff --git a/kll/sqlx/kll_sketch_float_merge.sqlx b/kll/sqlx/kll_sketch_float_merge.sqlx
index 4c0ece6..8b7ecb3 100644
--- a/kll/sqlx/kll_sketch_float_merge.sqlx
+++ b/kll/sqlx/kll_sketch_float_merge.sqlx
@@ -57,7 +57,7 @@ export function aggregate(state, sketch) {
     if (state.sketch == null) { // for transition deserialize-aggregate
       state.sketch = new Module.kll_sketch_float(state.k);
     }
-    state.sketch.mergeBytes(sketch);
+    state.sketch.merge(sketch);
   } catch (e) {
     if (e.message != null) throw e;
     throw new Error(Module.getExceptionMessage(e));
@@ -70,7 +70,7 @@ export function serialize(state) {
     // for prior transition deserialize-aggregate
     // merge aggregated and serialized state
     if (state.sketch != null && state.serialized != null) {
-      sketch.mergeBytes(state.serialized);
+      sketch.merge(state.serialized);
     }
     return {
       k: state.k,
@@ -94,11 +94,11 @@ export function merge(state, other_state) {
       state.sketch = new Module.kll_sketch_float(state.k);
     }
     if (state.serialized != null) {
-      state.sketch.mergeBytes(state.serialized);
+      state.sketch.merge(state.serialized);
       state.serialized = null;
     }
     if (other_state.serialized != null) {
-      state.sketch.mergeBytes(other_state.serialized);
+      state.sketch.merge(other_state.serialized);
       other_state.serialized = null;
     }
   } catch (e) {
diff --git a/kll/test/kll_sketch_test.sql b/kll/test/kll_sketch_test.sql
index ece9d4f..341cb07 100644
--- a/kll/test/kll_sketch_test.sql
+++ b/kll/test/kll_sketch_test.sql
@@ -41,8 +41,19 @@ select $BQ_DATASET.kll_sketch_float_get_pmf($BQ_DATASET.kll_sketch_float_merge(s
 # expected 0.5, 1
 select $BQ_DATASET.kll_sketch_float_get_cdf($BQ_DATASET.kll_sketch_float_merge(sketch, null), [10.0], true) from $BQ_DATASET.kll_sketch;
 
+# expected 1
+select $BQ_DATASET.kll_sketch_float_get_min_value($BQ_DATASET.kll_sketch_float_merge(sketch, null)) from $BQ_DATASET.kll_sketch;
+
+# expected 20
+select $BQ_DATASET.kll_sketch_float_get_max_value($BQ_DATASET.kll_sketch_float_merge(sketch, null)) from $BQ_DATASET.kll_sketch;
+
 drop table $BQ_DATASET.kll_sketch;
 
+# expected about 1.3%
+select $BQ_DATASET.kll_sketch_float_get_normalized_rank_error($BQ_DATASET.kll_sketch_float_build(value, null), false) from unnest(generate_array(1, 10000)) as value;
+
+select $BQ_DATASET.kll_sketch_float_get_num_retained($BQ_DATASET.kll_sketch_float_build(value, null)) from unnest(generate_array(1, 10000)) as value;
+
 # expected false
 select $BQ_DATASET.kll_sketch_float_kolmogorov_smirnov(
   (select $BQ_DATASET.kll_sketch_float_build(value, null) from unnest([1,2,3,4,5,6,7,8,9,10]) as value),
diff --git a/tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx b/theta/sqlx/theta_sketch_get_num_retained.sqlx
similarity index 62%
copy from tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx
copy to theta/sqlx/theta_sketch_get_num_retained.sqlx
index 8827d19..1626f39 100644
--- a/tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx
+++ b/theta/sqlx/theta_sketch_get_num_retained.sqlx
@@ -20,20 +20,17 @@
 config { hasOutput: true }
 
 CREATE OR REPLACE FUNCTION ${self()}(sketch BYTES)
-RETURNS FLOAT64
+RETURNS INT
 OPTIONS (
-  library=["gs://$GCS_BUCKET/tuple_sketch_int64.js"],
-  description = '''Returns the cardinality estimate of the given Tuple Sketch.
-Note that cardinality estimation accuracy, plots, and error tables are the same as the Theta Sketch.
-This function only applies to Tuple Sketches with an INT64 summary column.
+  description = '''Returns the number of retained entries in the given sketch.
   
-Param sketch: the given Tuple Sketch. This may not be NULL.
+Param sketch: The given sketch to query as BYTES.
 Defaults: seed = 9001.
-Returns: the cardinality estimate of the given Tuple Sketch
+Returns: number of retained entries as INT.
 
 For more information:
- - https://datasketches.apache.org/docs/Tuple/TupleSketches.html
+ - https://datasketches.apache.org/docs/Theta/ThetaSketches.html
 '''
 ) AS (
-  $BQ_DATASET.tuple_sketch_int64_get_estimate_seed(sketch, NULL)
+  $BQ_DATASET.theta_sketch_get_num_retained_seed(sketch, NULL)
 );
diff --git a/tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx b/theta/sqlx/theta_sketch_get_num_retained_seed.sqlx
similarity index 52%
copy from tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx
copy to theta/sqlx/theta_sketch_get_num_retained_seed.sqlx
index 8827d19..3337aa2 100644
--- a/tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx
+++ b/theta/sqlx/theta_sketch_get_num_retained_seed.sqlx
@@ -19,21 +19,27 @@
 
 config { hasOutput: true }
 
-CREATE OR REPLACE FUNCTION ${self()}(sketch BYTES)
-RETURNS FLOAT64
+CREATE OR REPLACE FUNCTION ${self()}(sketch BYTES, seed INT64)
+RETURNS INT
+LANGUAGE js
 OPTIONS (
-  library=["gs://$GCS_BUCKET/tuple_sketch_int64.js"],
-  description = '''Returns the cardinality estimate of the given Tuple Sketch.
-Note that cardinality estimation accuracy, plots, and error tables are the same as the Theta Sketch.
-This function only applies to Tuple Sketches with an INT64 summary column.
+  library=["gs://$GCS_BUCKET/theta_sketch.js"],
+  js_parameter_encoding_mode='STANDARD',
+  description = '''Returns the number of retained entries in the given sketch.
   
-Param sketch: the given Tuple Sketch. This may not be NULL.
-Defaults: seed = 9001.
-Returns: the cardinality estimate of the given Tuple Sketch
+Param sketch: The given sketch to query as BYTES.
+Param seed: This is used to confirm that the given sketch was configured with the correct seed.
+Returns: number of retained entries as INT.
 
 For more information:
- - https://datasketches.apache.org/docs/Tuple/TupleSketches.html
+ - https://datasketches.apache.org/docs/Theta/ThetaSketches.html
 '''
-) AS (
-  $BQ_DATASET.tuple_sketch_int64_get_estimate_seed(sketch, NULL)
-);
+) AS R"""
+const default_seed = BigInt(Module.DEFAULT_SEED);
+try {
+  return Module.compact_theta_sketch.getNumRetainedFromBytes(sketch, seed ? BigInt(seed) : default_seed);
+} catch (e) {
+  if (e.message != null) throw e;
+  throw new Error(Module.getExceptionMessage(e));
+}
+""";
diff --git a/tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx b/theta/sqlx/theta_sketch_get_theta.sqlx
similarity index 63%
copy from tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx
copy to theta/sqlx/theta_sketch_get_theta.sqlx
index 8827d19..2665c55 100644
--- a/tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx
+++ b/theta/sqlx/theta_sketch_get_theta.sqlx
@@ -22,18 +22,15 @@ config { hasOutput: true }
 CREATE OR REPLACE FUNCTION ${self()}(sketch BYTES)
 RETURNS FLOAT64
 OPTIONS (
-  library=["gs://$GCS_BUCKET/tuple_sketch_int64.js"],
-  description = '''Returns the cardinality estimate of the given Tuple Sketch.
-Note that cardinality estimation accuracy, plots, and error tables are the same as the Theta Sketch.
-This function only applies to Tuple Sketches with an INT64 summary column.
+  description = '''Returns theta (effective sampling rate) as a fraction from 0 to 1.
   
-Param sketch: the given Tuple Sketch. This may not be NULL.
+Param sketch: The given sketch to query as BYTES.
 Defaults: seed = 9001.
-Returns: the cardinality estimate of the given Tuple Sketch
+Returns: theta as FLOAT64.
 
 For more information:
- - https://datasketches.apache.org/docs/Tuple/TupleSketches.html
+ - https://datasketches.apache.org/docs/Theta/ThetaSketches.html
 '''
 ) AS (
-  $BQ_DATASET.tuple_sketch_int64_get_estimate_seed(sketch, NULL)
+  $BQ_DATASET.theta_sketch_get_theta_seed(sketch, NULL)
 );
diff --git a/tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx b/theta/sqlx/theta_sketch_get_theta_seed.sqlx
similarity index 54%
copy from tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx
copy to theta/sqlx/theta_sketch_get_theta_seed.sqlx
index 8827d19..b219801 100644
--- a/tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx
+++ b/theta/sqlx/theta_sketch_get_theta_seed.sqlx
@@ -19,21 +19,27 @@
 
 config { hasOutput: true }
 
-CREATE OR REPLACE FUNCTION ${self()}(sketch BYTES)
+CREATE OR REPLACE FUNCTION ${self()}(sketch BYTES, seed INT64)
 RETURNS FLOAT64
+LANGUAGE js
 OPTIONS (
-  library=["gs://$GCS_BUCKET/tuple_sketch_int64.js"],
-  description = '''Returns the cardinality estimate of the given Tuple Sketch.
-Note that cardinality estimation accuracy, plots, and error tables are the same as the Theta Sketch.
-This function only applies to Tuple Sketches with an INT64 summary column.
+  library=["gs://$GCS_BUCKET/theta_sketch.js"],
+  js_parameter_encoding_mode='STANDARD',
+  description = '''Returns theta (effective sampling rate) as a fraction from 0 to 1.
   
-Param sketch: the given Tuple Sketch. This may not be NULL.
-Defaults: seed = 9001.
-Returns: the cardinality estimate of the given Tuple Sketch
+Param sketch: The given sketch to query as BYTES.
+Param seed: This is used to confirm that the given sketch was configured with the correct seed.
+Returns: theta as FLOAT64.
 
 For more information:
- - https://datasketches.apache.org/docs/Tuple/TupleSketches.html
+ - https://datasketches.apache.org/docs/Theta/ThetaSketches.html
 '''
-) AS (
-  $BQ_DATASET.tuple_sketch_int64_get_estimate_seed(sketch, NULL)
-);
+) AS R"""
+const default_seed = BigInt(Module.DEFAULT_SEED);
+try {
+  return Module.compact_theta_sketch.getThetaFromBytes(sketch, seed ? BigInt(seed) : default_seed);
+} catch (e) {
+  if (e.message != null) throw e;
+  throw new Error(Module.getExceptionMessage(e));
+}
+""";
diff --git a/theta/test/theta_sketch_test.sql b/theta/test/theta_sketch_test.sql
index e9f55b2..f7ba8c6 100644
--- a/theta/test/theta_sketch_test.sql
+++ b/theta/test/theta_sketch_test.sql
@@ -36,6 +36,14 @@ select $BQ_DATASET.theta_sketch_to_string(
   $BQ_DATASET.theta_sketch_agg_union(sketch)
 ) from $BQ_DATASET.theta_sketch;
 
+select $BQ_DATASET.theta_sketch_get_theta(
+  $BQ_DATASET.theta_sketch_agg_union(sketch)
+) from $BQ_DATASET.theta_sketch;
+
+select $BQ_DATASET.theta_sketch_get_num_retained(
+  $BQ_DATASET.theta_sketch_agg_union(sketch)
+) from $BQ_DATASET.theta_sketch;
+
 drop table $BQ_DATASET.theta_sketch;
 
 # using full signatures
@@ -59,6 +67,16 @@ select $BQ_DATASET.theta_sketch_to_string_seed(
   111
 ) from $BQ_DATASET.theta_sketch;
 
+select $BQ_DATASET.theta_sketch_get_theta_seed(
+  $BQ_DATASET.theta_sketch_agg_union_lgk_seed(sketch, struct<int, int>(10, 111)),
+  111
+) from $BQ_DATASET.theta_sketch;
+
+select $BQ_DATASET.theta_sketch_get_num_retained_seed(
+  $BQ_DATASET.theta_sketch_agg_union_lgk_seed(sketch, struct<int, int>(10, 111)),
+  111
+) from $BQ_DATASET.theta_sketch;
+
 drop table $BQ_DATASET.theta_sketch;
 
 # using defaults
diff --git a/theta/theta_sketch.cpp b/theta/theta_sketch.cpp
index 1cb1529..00cf7e7 100644
--- a/theta/theta_sketch.cpp
+++ b/theta/theta_sketch.cpp
@@ -68,6 +68,12 @@ EMSCRIPTEN_BINDINGS(theta_sketch) {
     .class_function("toStringFromBytes", emscripten::optional_override([](const std::string& bytes, uint64_t seed) {
       return wrapped_compact_theta_sketch::wrap(bytes.data(), bytes.size(), seed).to_string();
     }))
+    .class_function("getThetaFromBytes", emscripten::optional_override([](const std::string& bytes, uint64_t seed) {
+      return wrapped_compact_theta_sketch::wrap(bytes.data(), bytes.size(), seed).get_theta();
+    }))
+    .class_function("getNumRetainedFromBytes", emscripten::optional_override([](const std::string& bytes, uint64_t seed) {
+      return wrapped_compact_theta_sketch::wrap(bytes.data(), bytes.size(), seed).get_num_retained();
+    }))
     .class_function("getMaxSerializedSizeBytes", &compact_theta_sketch::get_max_serialized_size_bytes)
     ;
 
diff --git a/tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx b/tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx
index 8827d19..0e5386a 100644
--- a/tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx
+++ b/tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx
@@ -22,7 +22,6 @@ config { hasOutput: true }
 CREATE OR REPLACE FUNCTION ${self()}(sketch BYTES)
 RETURNS FLOAT64
 OPTIONS (
-  library=["gs://$GCS_BUCKET/tuple_sketch_int64.js"],
   description = '''Returns the cardinality estimate of the given Tuple Sketch.
 Note that cardinality estimation accuracy, plots, and error tables are the same as the Theta Sketch.
 This function only applies to Tuple Sketches with an INT64 summary column.
diff --git a/tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx b/tuple/sqlx/tuple_sketch_int64_get_num_retained.sqlx
similarity index 82%
copy from tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx
copy to tuple/sqlx/tuple_sketch_int64_get_num_retained.sqlx
index 8827d19..8a7bd1b 100644
--- a/tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx
+++ b/tuple/sqlx/tuple_sketch_int64_get_num_retained.sqlx
@@ -20,20 +20,19 @@
 config { hasOutput: true }
 
 CREATE OR REPLACE FUNCTION ${self()}(sketch BYTES)
-RETURNS FLOAT64
+RETURNS INT
 OPTIONS (
-  library=["gs://$GCS_BUCKET/tuple_sketch_int64.js"],
-  description = '''Returns the cardinality estimate of the given Tuple Sketch.
+  description = '''Returns the number of retained entries in the given sketch.
 Note that cardinality estimation accuracy, plots, and error tables are the same as the Theta Sketch.
 This function only applies to Tuple Sketches with an INT64 summary column.
   
 Param sketch: the given Tuple Sketch. This may not be NULL.
 Defaults: seed = 9001.
-Returns: the cardinality estimate of the given Tuple Sketch
+Returns: number of retained entries as INT.
 
 For more information:
  - https://datasketches.apache.org/docs/Tuple/TupleSketches.html
 '''
 ) AS (
-  $BQ_DATASET.tuple_sketch_int64_get_estimate_seed(sketch, NULL)
+  $BQ_DATASET.tuple_sketch_int64_get_num_retained_seed(sketch, NULL)
 );
diff --git a/tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx b/tuple/sqlx/tuple_sketch_int64_get_num_retained_seed.sqlx
similarity index 66%
copy from tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx
copy to tuple/sqlx/tuple_sketch_int64_get_num_retained_seed.sqlx
index 8827d19..313c369 100644
--- a/tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx
+++ b/tuple/sqlx/tuple_sketch_int64_get_num_retained_seed.sqlx
@@ -19,21 +19,27 @@
 
 config { hasOutput: true }
 
-CREATE OR REPLACE FUNCTION ${self()}(sketch BYTES)
-RETURNS FLOAT64
+CREATE OR REPLACE FUNCTION ${self()}(sketch BYTES, seed INT64)
+RETURNS INT
+LANGUAGE js
 OPTIONS (
   library=["gs://$GCS_BUCKET/tuple_sketch_int64.js"],
-  description = '''Returns the cardinality estimate of the given Tuple Sketch.
+  js_parameter_encoding_mode='STANDARD',
+  description = '''Returns the number of retained entries in the given sketch.
 Note that cardinality estimation accuracy, plots, and error tables are the same as the Theta Sketch.
 This function only applies to Tuple Sketches with an INT64 summary column.
   
 Param sketch: the given Tuple Sketch. This may not be NULL.
-Defaults: seed = 9001.
-Returns: the cardinality estimate of the given Tuple Sketch
-
+Param seed: This is used to confirm that the given sketches were configured with the correct seed. A NULL specifies the default seed = 9001.
+Returns: number of retained entries as INT.
 For more information:
  - https://datasketches.apache.org/docs/Tuple/TupleSketches.html
 '''
-) AS (
-  $BQ_DATASET.tuple_sketch_int64_get_estimate_seed(sketch, NULL)
-);
+) AS R"""
+try {
+  return Module.compact_tuple_sketch_int64.getNumRetained(sketch, seed ? BigInt(seed) : BigInt(Module.DEFAULT_SEED));
+} catch (e) {
+  if (e.message != null) throw e;
+  throw new Error(Module.getExceptionMessage(e));
+}
+""";
diff --git a/tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx b/tuple/sqlx/tuple_sketch_int64_get_theta.sqlx
similarity index 83%
copy from tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx
copy to tuple/sqlx/tuple_sketch_int64_get_theta.sqlx
index 8827d19..4fa9528 100644
--- a/tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx
+++ b/tuple/sqlx/tuple_sketch_int64_get_theta.sqlx
@@ -22,18 +22,17 @@ config { hasOutput: true }
 CREATE OR REPLACE FUNCTION ${self()}(sketch BYTES)
 RETURNS FLOAT64
 OPTIONS (
-  library=["gs://$GCS_BUCKET/tuple_sketch_int64.js"],
-  description = '''Returns the cardinality estimate of the given Tuple Sketch.
+  description = '''Returns theta (effective sampling rate) as a fraction from 0 to 1.
 Note that cardinality estimation accuracy, plots, and error tables are the same as the Theta Sketch.
 This function only applies to Tuple Sketches with an INT64 summary column.
   
 Param sketch: the given Tuple Sketch. This may not be NULL.
 Defaults: seed = 9001.
-Returns: the cardinality estimate of the given Tuple Sketch
+Returns: theta as FLOAT64.
 
 For more information:
  - https://datasketches.apache.org/docs/Tuple/TupleSketches.html
 '''
 ) AS (
-  $BQ_DATASET.tuple_sketch_int64_get_estimate_seed(sketch, NULL)
+  $BQ_DATASET.tuple_sketch_int64_get_theta_seed(sketch, NULL)
 );
diff --git a/tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx b/tuple/sqlx/tuple_sketch_int64_get_theta_seed.sqlx
similarity index 67%
copy from tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx
copy to tuple/sqlx/tuple_sketch_int64_get_theta_seed.sqlx
index 8827d19..9f33fec 100644
--- a/tuple/sqlx/tuple_sketch_int64_get_estimate.sqlx
+++ b/tuple/sqlx/tuple_sketch_int64_get_theta_seed.sqlx
@@ -19,21 +19,28 @@
 
 config { hasOutput: true }
 
-CREATE OR REPLACE FUNCTION ${self()}(sketch BYTES)
+CREATE OR REPLACE FUNCTION ${self()}(sketch BYTES, seed INT64)
 RETURNS FLOAT64
+LANGUAGE js
 OPTIONS (
   library=["gs://$GCS_BUCKET/tuple_sketch_int64.js"],
-  description = '''Returns the cardinality estimate of the given Tuple Sketch.
+  js_parameter_encoding_mode='STANDARD',
+  description = '''Returns theta (effective sampling rate) as a fraction from 0 to 1.
 Note that cardinality estimation accuracy, plots, and error tables are the same as the Theta Sketch.
 This function only applies to Tuple Sketches with an INT64 summary column.
   
 Param sketch: the given Tuple Sketch. This may not be NULL.
-Defaults: seed = 9001.
-Returns: the cardinality estimate of the given Tuple Sketch
+Param seed: This is used to confirm that the given sketches were configured with the correct seed. A NULL specifies the default seed = 9001.
+Returns: theta as FLOAT64.
 
 For more information:
  - https://datasketches.apache.org/docs/Tuple/TupleSketches.html
 '''
-) AS (
-  $BQ_DATASET.tuple_sketch_int64_get_estimate_seed(sketch, NULL)
-);
+) AS R"""
+try {
+  return Module.compact_tuple_sketch_int64.getTheta(sketch, seed ? BigInt(seed) : BigInt(Module.DEFAULT_SEED));
+} catch (e) {
+  if (e.message != null) throw e;
+  throw new Error(Module.getExceptionMessage(e));
+}
+""";
diff --git a/tuple/test/tuple_sketch_int_test.sql b/tuple/test/tuple_sketch_int_test.sql
index 01917dd..681c8d4 100644
--- a/tuple/test/tuple_sketch_int_test.sql
+++ b/tuple/test/tuple_sketch_int_test.sql
@@ -35,6 +35,14 @@ select $BQ_DATASET.tuple_sketch_int64_to_string(
   $BQ_DATASET.tuple_sketch_int64_agg_union(sketch)
 ) from $BQ_DATASET.tuple_sketch;
 
+select $BQ_DATASET.tuple_sketch_int64_get_theta(
+  $BQ_DATASET.tuple_sketch_int64_agg_union(sketch)
+) from $BQ_DATASET.tuple_sketch;
+
+select $BQ_DATASET.tuple_sketch_int64_get_num_retained(
+  $BQ_DATASET.tuple_sketch_int64_agg_union(sketch)
+) from $BQ_DATASET.tuple_sketch;
+
 drop table $BQ_DATASET.tuple_sketch;
 
 # using full signatures
@@ -65,6 +73,16 @@ select $BQ_DATASET.tuple_sketch_int64_to_string_seed(
   111
 ) from $BQ_DATASET.tuple_sketch;
 
+select $BQ_DATASET.tuple_sketch_int64_get_theta_seed(
+  $BQ_DATASET.tuple_sketch_int64_agg_union_lgk_seed_mode(sketch, STRUCT<BYTEINT, INT64, STRING>(10, 111, "NOP")),
+  111
+) from $BQ_DATASET.tuple_sketch;
+
+select $BQ_DATASET.tuple_sketch_int64_get_num_retained_seed(
+  $BQ_DATASET.tuple_sketch_int64_agg_union_lgk_seed_mode(sketch, STRUCT<BYTEINT, INT64, STRING>(10, 111, "NOP")),
+  111
+) from $BQ_DATASET.tuple_sketch;
+
 drop table $BQ_DATASET.tuple_sketch;
 
 
diff --git a/tuple/tuple_sketch_int64.cpp b/tuple/tuple_sketch_int64.cpp
index f33bcd6..34cdfb7 100644
--- a/tuple/tuple_sketch_int64.cpp
+++ b/tuple/tuple_sketch_int64.cpp
@@ -125,6 +125,12 @@ EMSCRIPTEN_BINDINGS(tuple_sketch_int64) {
     .class_function("getEstimate", emscripten::optional_override([](const std::string& sketch_bytes, uint64_t seed) {
       return compact_tuple_sketch_int64::deserialize(sketch_bytes.data(), sketch_bytes.size(), seed).get_estimate();
     }))
+    .class_function("getTheta", emscripten::optional_override([](const std::string& sketch_bytes, uint64_t seed) {
+      return compact_tuple_sketch_int64::deserialize(sketch_bytes.data(), sketch_bytes.size(), seed).get_theta();
+    }))
+    .class_function("getNumRetained", emscripten::optional_override([](const std::string& sketch_bytes, uint64_t seed) {
+      return compact_tuple_sketch_int64::deserialize(sketch_bytes.data(), sketch_bytes.size(), seed).get_num_retained();
+    }))
     .class_function("toString", emscripten::optional_override([](const std::string& sketch_bytes, uint64_t seed) {
       return compact_tuple_sketch_int64::deserialize(sketch_bytes.data(), sketch_bytes.size(), seed).to_string();
     }))


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org