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 2021/01/09 00:27:58 UTC

[datasketches-postgresql] 01/01: better handling of optional args

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

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

commit 77b8dd9b82fe04e5918ca5979547bdb3bf2d8fb4
Author: AlexanderSaydakov <Al...@users.noreply.github.com>
AuthorDate: Fri Jan 8 16:27:24 2021 -0800

    better handling of optional args
---
 src/aod_sketch_pg_functions.c              | 17 +++++++----------
 src/cpc_sketch_pg_functions.c              |  8 ++++----
 src/frequent_strings_sketch_pg_functions.c | 18 +++---------------
 src/hll_sketch_pg_functions.c              | 17 ++++++++---------
 src/kll_float_sketch_pg_functions.c        |  8 ++++----
 src/theta_sketch_pg_functions.c            |  8 ++++----
 6 files changed, 30 insertions(+), 46 deletions(-)

diff --git a/src/aod_sketch_pg_functions.c b/src/aod_sketch_pg_functions.c
index 98766ef..a1f0673 100644
--- a/src/aod_sketch_pg_functions.c
+++ b/src/aod_sketch_pg_functions.c
@@ -118,8 +118,8 @@ Datum pg_aod_sketch_add_item(PG_FUNCTION_ARGS) {
   }
 
   if (PG_ARGISNULL(0)) {
-    lg_k = PG_GETARG_INT32(3);
-    p = PG_GETARG_FLOAT4(4);
+    lg_k = PG_NARGS() > 3 ? PG_GETARG_INT32(3) : 0;
+    p = PG_NARGS() > 4 ? PG_GETARG_FLOAT4(4) : 1;
     if (lg_k) {
       sketchptr = p ? aod_sketch_new_lgk_p(arr_len, lg_k, p) : aod_sketch_new_lgk(arr_len, lg_k);
     } else {
@@ -129,7 +129,7 @@ Datum pg_aod_sketch_add_item(PG_FUNCTION_ARGS) {
     sketchptr = PG_GETARG_POINTER(0);
   }
 
-    element_type = get_fn_expr_argtype(fcinfo->flinfo, 1);
+  element_type = get_fn_expr_argtype(fcinfo->flinfo, 1);
   element = PG_GETARG_DATUM(1);
   get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign);
   if (typlen == -1) {
@@ -222,8 +222,7 @@ Datum pg_aod_sketch_intersection_agg(PG_FUNCTION_ARGS) {
   oldcontext = MemoryContextSwitchTo(aggcontext);
 
   if (PG_ARGISNULL(0)) {
-    num_values = PG_GETARG_INT32(2);
-    if (num_values == 0) num_values = 1;
+    num_values = PG_NARGS() > 2 ? PG_GETARG_INT32(2) : 1;
     interptr = aod_intersection_new(num_values);
   } else {
     interptr = PG_GETARG_POINTER(0);
@@ -261,9 +260,8 @@ Datum pg_aod_sketch_union_agg(PG_FUNCTION_ARGS) {
   oldcontext = MemoryContextSwitchTo(aggcontext);
 
   if (PG_ARGISNULL(0)) {
-    num_values = PG_GETARG_INT32(2);
-    if (num_values == 0) num_values = 1;
-    lg_k = PG_GETARG_INT32(3);
+    num_values = PG_NARGS() > 2 ? PG_GETARG_INT32(2) : 1;
+    lg_k = PG_NARGS() > 3 ? PG_GETARG_INT32(3) : 0;
     unionptr = lg_k ? aod_union_new_lgk(num_values, lg_k) : aod_union_new(num_values);
   } else {
     unionptr = PG_GETARG_POINTER(0);
@@ -485,8 +483,7 @@ Datum pg_aod_sketch_to_kll_float_sketch(PG_FUNCTION_ARGS) {
   bytes_in = PG_GETARG_BYTEA_P(0);
   aodptr = aod_sketch_deserialize(VARDATA(bytes_in), VARSIZE(bytes_in) - VARHDRSZ);
   column_index = PG_GETARG_INT32(1);
-  k = PG_GETARG_INT32(2);
-  if (k == 0) k = DEFAULT_K;
+  k = PG_NARGS() > 2 ? PG_GETARG_INT32(2) : DEFAULT_K;
   kllptr = aod_sketch_to_kll_float_sketch(aodptr, column_index, k);
   bytes_out = kll_float_sketch_serialize(kllptr, VARHDRSZ);
   kll_float_sketch_delete(kllptr);
diff --git a/src/cpc_sketch_pg_functions.c b/src/cpc_sketch_pg_functions.c
index 2e5d3d4..00a521e 100644
--- a/src/cpc_sketch_pg_functions.c
+++ b/src/cpc_sketch_pg_functions.c
@@ -79,8 +79,8 @@ Datum pg_cpc_sketch_add_item(PG_FUNCTION_ARGS) {
   oldcontext = MemoryContextSwitchTo(aggcontext);
 
   if (PG_ARGISNULL(0)) {
-    lg_k = PG_GETARG_INT32(2);
-    sketchptr = cpc_sketch_new(lg_k ? lg_k : CPC_DEFAULT_LG_K);
+    lg_k = PG_NARGS() > 2 ? PG_GETARG_INT32(2) : CPC_DEFAULT_LG_K;
+    sketchptr = cpc_sketch_new(lg_k);
   } else {
     sketchptr = PG_GETARG_POINTER(0);
   }
@@ -172,8 +172,8 @@ Datum pg_cpc_sketch_union_agg(PG_FUNCTION_ARGS) {
   oldcontext = MemoryContextSwitchTo(aggcontext);
 
   if (PG_ARGISNULL(0)) {
-    lg_k = PG_GETARG_INT32(2);
-    unionptr = cpc_union_new(lg_k ? lg_k : CPC_DEFAULT_LG_K);
+    lg_k = PG_NARGS() > 2 ? PG_GETARG_INT32(2) : CPC_DEFAULT_LG_K;
+    unionptr = cpc_union_new(lg_k);
   } else {
     unionptr = PG_GETARG_POINTER(0);
   }
diff --git a/src/frequent_strings_sketch_pg_functions.c b/src/frequent_strings_sketch_pg_functions.c
index dced821..5c63c15 100644
--- a/src/frequent_strings_sketch_pg_functions.c
+++ b/src/frequent_strings_sketch_pg_functions.c
@@ -79,11 +79,7 @@ Datum pg_frequent_strings_sketch_add_item(PG_FUNCTION_ARGS) {
   str = PG_GETARG_VARCHAR_P(2);
 
   // optional weight
-  if (PG_NARGS() == 3) {
-    weight = 1;
-  } else {
-    weight = PG_GETARG_INT64(3);
-  }
+  weight = PG_NARGS() > 3 ? PG_GETARG_INT64(3) : 1;
 
   frequent_strings_sketch_update(sketchptr, VARDATA(str), VARSIZE(str) - VARHDRSZ, weight);
 
@@ -150,11 +146,7 @@ Datum pg_frequent_strings_sketch_to_string(PG_FUNCTION_ARGS) {
   bool print_items;
   char* str;
   bytes_in = PG_GETARG_BYTEA_P(0);
-  if (PG_NARGS() > 1) {
-    print_items = PG_GETARG_BOOL(1);
-  } else {
-    print_items = false;
-  }
+  print_items = PG_NARGS() > 1 ? PG_GETARG_BOOL(1) : false;
   sketchptr = frequent_strings_sketch_deserialize(VARDATA(bytes_in), VARSIZE(bytes_in) - VARHDRSZ);
   str = frequent_strings_sketch_to_string(sketchptr, print_items);
   frequent_strings_sketch_delete(sketchptr);
@@ -182,11 +174,7 @@ Datum frequent_strings_sketch_get_result(PG_FUNCTION_ARGS, bool no_false_positiv
     if (PG_ARGISNULL(0)) SRF_RETURN_DONE(funcctx);
     bytes_in = PG_GETARG_BYTEA_P(0);
     sketchptr = frequent_strings_sketch_deserialize(VARDATA(bytes_in), VARSIZE(bytes_in) - VARHDRSZ);
-    if (PG_ARGISNULL(1)) {
-      threshold = 0;
-    } else {
-      threshold = PG_GETARG_INT64(1);
-    }
+    threshold = PG_NARGS() > 1 ? PG_GETARG_INT64(1) : 0;
 
     funcctx->user_fctx = frequent_strings_sketch_get_frequent_items(sketchptr, no_false_positives, threshold);
     funcctx->max_calls = ((struct frequent_strings_sketch_result*) funcctx->user_fctx)->num;
diff --git a/src/hll_sketch_pg_functions.c b/src/hll_sketch_pg_functions.c
index 2454e66..4de4b68 100644
--- a/src/hll_sketch_pg_functions.c
+++ b/src/hll_sketch_pg_functions.c
@@ -80,9 +80,8 @@ Datum pg_hll_sketch_add_item(PG_FUNCTION_ARGS) {
   oldcontext = MemoryContextSwitchTo(aggcontext);
 
   if (PG_ARGISNULL(0)) {
-    lg_k = PG_GETARG_INT32(2);
-    if (lg_k == 0) lg_k = HLL_DEFAULT_LG_K;
-    tgt_type = PG_GETARG_INT32(3);
+    lg_k = PG_NARGS() > 2 ? PG_GETARG_INT32(2) : HLL_DEFAULT_LG_K;
+    tgt_type = PG_NARGS() > 3 ? PG_GETARG_INT32(3) : 0;
     if (tgt_type) {
       if ((tgt_type != 4) && (tgt_type != 6) && (tgt_type != 8)) {
         elog(ERROR, "hll_sketch_add_item: unsupported target type, must be 4, 6 or 8");
@@ -187,10 +186,10 @@ Datum pg_hll_sketch_union_agg(PG_FUNCTION_ARGS) {
   oldcontext = MemoryContextSwitchTo(aggcontext);
 
   if (PG_ARGISNULL(0)) {
-    lg_k = PG_GETARG_INT32(2);
+    lg_k = PG_NARGS() > 2 ? PG_GETARG_INT32(2) : HLL_DEFAULT_LG_K;
     stateptr = palloc(sizeof(struct hll_union_state));
-    stateptr->unionptr = hll_union_new(lg_k ? lg_k : HLL_DEFAULT_LG_K);
-    stateptr->tgt_type = PG_GETARG_INT32(3);
+    stateptr->unionptr = hll_union_new(lg_k);
+    stateptr->tgt_type = PG_NARGS() > 3 ? PG_GETARG_INT32(3) : 0;
     if (stateptr->tgt_type) {
       if ((stateptr->tgt_type != 4) && (stateptr->tgt_type != 6) && (stateptr->tgt_type != 8)) {
         elog(ERROR, "hll_sketch_union_agg: unsupported target type, must be 4, 6 or 8");
@@ -300,14 +299,14 @@ Datum pg_hll_sketch_union(PG_FUNCTION_ARGS) {
   unsigned lg_k;
   unsigned tgt_type;
 
-  lg_k = PG_GETARG_INT32(2);
-  tgt_type = PG_GETARG_INT32(3);
+  lg_k = PG_NARGS() > 2 ? PG_GETARG_INT32(2) : HLL_DEFAULT_LG_K;
+  tgt_type = PG_NARGS() > 3 ? PG_GETARG_INT32(3) : 0;
   if (tgt_type) {
     if ((tgt_type != 4) && (tgt_type != 6) && (tgt_type != 8)) {
       elog(ERROR, "hll_sketch_union: unsupported target type, must be 4, 6 or 8");
     }
   }
-  unionptr = hll_union_new(lg_k ? lg_k : HLL_DEFAULT_LG_K);
+  unionptr = hll_union_new(lg_k);
   if (!PG_ARGISNULL(0)) {
     bytes_in1 = PG_GETARG_BYTEA_P(0);
     sketchptr1 = hll_sketch_deserialize(VARDATA(bytes_in1), VARSIZE(bytes_in1) - VARHDRSZ);
diff --git a/src/kll_float_sketch_pg_functions.c b/src/kll_float_sketch_pg_functions.c
index 87426c8..3e31159 100644
--- a/src/kll_float_sketch_pg_functions.c
+++ b/src/kll_float_sketch_pg_functions.c
@@ -77,8 +77,8 @@ Datum pg_kll_float_sketch_add_item(PG_FUNCTION_ARGS) {
   oldcontext = MemoryContextSwitchTo(aggcontext);
 
   if (PG_ARGISNULL(0)) {
-    k = PG_GETARG_INT32(2);
-    sketchptr = kll_float_sketch_new(k ? k : DEFAULT_K);
+    k = PG_NARGS() > 2 ? PG_GETARG_INT32(2) : DEFAULT_K;
+    sketchptr = kll_float_sketch_new(k);
   } else {
     sketchptr = PG_GETARG_POINTER(0);
   }
@@ -160,8 +160,8 @@ Datum pg_kll_float_sketch_merge(PG_FUNCTION_ARGS) {
   oldcontext = MemoryContextSwitchTo(aggcontext);
 
   if (PG_ARGISNULL(0)) {
-    k = PG_GETARG_INT32(2);
-    unionptr = kll_float_sketch_new(k ? k : DEFAULT_K);
+    k = PG_NARGS() > 2 ? PG_GETARG_INT32(2) : DEFAULT_K;
+    unionptr = kll_float_sketch_new(k);
   } else {
     unionptr = PG_GETARG_POINTER(0);
   }
diff --git a/src/theta_sketch_pg_functions.c b/src/theta_sketch_pg_functions.c
index 1e9f077..04f64bf 100644
--- a/src/theta_sketch_pg_functions.c
+++ b/src/theta_sketch_pg_functions.c
@@ -86,8 +86,8 @@ Datum pg_theta_sketch_add_item(PG_FUNCTION_ARGS) {
   oldcontext = MemoryContextSwitchTo(aggcontext);
 
   if (PG_ARGISNULL(0)) {
-    lg_k = PG_GETARG_INT32(2);
-    p = PG_GETARG_FLOAT4(3);
+    lg_k = PG_NARGS() > 2 ? PG_GETARG_INT32(2) : 0;
+    p = PG_NARGS() > 3 ? PG_GETARG_FLOAT4(3) : 1;
     if (lg_k) {
       sketchptr = p ? theta_sketch_new_lgk_p(lg_k, p) : theta_sketch_new_lgk(lg_k);
     } else {
@@ -219,7 +219,7 @@ Datum pg_theta_sketch_union_agg(PG_FUNCTION_ARGS) {
   oldcontext = MemoryContextSwitchTo(aggcontext);
 
   if (PG_ARGISNULL(0)) {
-    lg_k = PG_GETARG_INT32(2);
+    lg_k = PG_NARGS() > 2 ? PG_GETARG_INT32(2) : 0;
     unionptr = lg_k ? theta_union_new(lg_k) : theta_union_new_default();
   } else {
     unionptr = PG_GETARG_POINTER(0);
@@ -347,7 +347,7 @@ Datum pg_theta_sketch_union(PG_FUNCTION_ARGS) {
   struct ptr_with_size bytes_out;
   int lg_k;
   
-  lg_k = PG_GETARG_INT32(2);
+  lg_k = PG_NARGS() > 2 ? PG_GETARG_INT32(2) : 0;
   unionptr = lg_k ? theta_union_new(lg_k) : theta_union_new_default();
   if (!PG_ARGISNULL(0)) {
     bytes_in1 = PG_GETARG_BYTEA_P(0);


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