From 93ed118c806501d1fced0296d40b2e85ba2bf698 Mon Sep 17 00:00:00 2001 From: smmathews-bw-boston Date: Tue, 10 Oct 2023 18:19:58 -0400 Subject: [PATCH 1/4] there's no reason why we should be constantly serializing and deserializing the portable version of roaring bitmap (that allows it to work in java and go) when we can just handle that case in roaringbitmap_in/roaringbitmap_out. TODO: fix up tests --- roaringbitmap.c | 128 +++++++++++++++++++++++++----------------------- 1 file changed, 67 insertions(+), 61 deletions(-) diff --git a/roaringbitmap.c b/roaringbitmap.c index 9729b76..18694f0 100644 --- a/roaringbitmap.c +++ b/roaringbitmap.c @@ -142,7 +142,7 @@ rb_from_bytea(PG_FUNCTION_ARGS) { bytea *serializedbytes = PG_GETARG_BYTEA_P(0); roaring_bitmap_t *r1; - r1 = roaring_bitmap_portable_deserialize(VARDATA(serializedbytes)); + r1 = roaring_bitmap_deserialize(VARDATA(serializedbytes)); if (!r1) ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), @@ -261,9 +261,9 @@ roaringbitmap_in(PG_FUNCTION_ARGS) { errmsg("malformed bitmap literal"))); } - expectedsize = roaring_bitmap_portable_size_in_bytes(r1); + expectedsize = roaring_bitmap_size_in_bytes(r1); serializedbytes = (bytea *) palloc(VARHDRSZ + expectedsize); - roaring_bitmap_portable_serialize(r1, VARDATA(serializedbytes)); + roaring_bitmap_serialize(r1, VARDATA(serializedbytes)); roaring_bitmap_free(r1); SET_VARSIZE(serializedbytes, VARHDRSZ + expectedsize); @@ -280,18 +280,23 @@ roaringbitmap_out(PG_FUNCTION_ARGS) { roaring_uint32_iterator_t iterator; StringInfoData buf; roaring_bitmap_t *r1; - - if(rbitmap_output_format == RBITMAP_OUTPUT_BYTEA){ - return DirectFunctionCall1(byteaout, PG_GETARG_DATUM(0)); - } - + size_t expectedsize; + serializedbytes = PG_GETARG_BYTEA_P(0); - r1 = roaring_bitmap_portable_deserialize(VARDATA(serializedbytes)); + r1 = roaring_bitmap_deserialize(VARDATA(serializedbytes)); if (!r1) ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), errmsg("bitmap format is error"))); + if(rbitmap_output_format == RBITMAP_OUTPUT_BYTEA){ + expectedsize = roaring_bitmap_portable_size_in_bytes(r1); + serializedbytes = (bytea *) palloc(VARHDRSZ + expectedsize); + roaring_bitmap_portable_serialize(r1, VARDATA(serializedbytes)); + roaring_bitmap_free(r1); + return DirectFunctionCall1(byteaout, PointerGetDatum(serializedbytes)); + } + initStringInfo(&buf); appendStringInfoChar(&buf, '{'); @@ -309,6 +314,7 @@ roaringbitmap_out(PG_FUNCTION_ARGS) { appendStringInfoChar(&buf, '}'); + roaring_bitmap_free(r1); PG_RETURN_CSTRING(buf.data); } @@ -350,13 +356,13 @@ rb_or(PG_FUNCTION_ARGS) { size_t expectedsize; bytea *serializedbytes; - r1 = roaring_bitmap_portable_deserialize(VARDATA(serializedbytes1)); + r1 = roaring_bitmap_deserialize(VARDATA(serializedbytes1)); if (!r1) ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), errmsg("bitmap format is error"))); - r2 = roaring_bitmap_portable_deserialize(VARDATA(serializedbytes2)); + r2 = roaring_bitmap_deserialize(VARDATA(serializedbytes2)); if (!r2) { roaring_bitmap_free(r1); ereport(ERROR, @@ -365,9 +371,9 @@ rb_or(PG_FUNCTION_ARGS) { } roaring_bitmap_or_inplace(r1, r2); roaring_bitmap_free(r2); - expectedsize = roaring_bitmap_portable_size_in_bytes(r1); + expectedsize = roaring_bitmap_size_in_bytes(r1); serializedbytes = (bytea *) palloc(VARHDRSZ + expectedsize); - roaring_bitmap_portable_serialize(r1, VARDATA(serializedbytes)); + roaring_bitmap_serialize(r1, VARDATA(serializedbytes)); roaring_bitmap_free(r1); SET_VARSIZE(serializedbytes, VARHDRSZ + expectedsize); @@ -454,9 +460,9 @@ rb_and(PG_FUNCTION_ARGS) { errmsg("bitmap format is error"))); } - expectedsize = roaring_bitmap_portable_size_in_bytes(r); + expectedsize = roaring_bitmap_size_in_bytes(r); serializedbytes = (bytea *) palloc(VARHDRSZ + expectedsize); - roaring_bitmap_portable_serialize(r, VARDATA(serializedbytes)); + roaring_bitmap_serialize(r, VARDATA(serializedbytes)); roaring_bitmap_free(r); SET_VARSIZE(serializedbytes, VARHDRSZ + expectedsize); @@ -544,9 +550,9 @@ rb_andnot(PG_FUNCTION_ARGS) { errmsg("bitmap format is error"))); } - expectedsize = roaring_bitmap_portable_size_in_bytes(r); + expectedsize = roaring_bitmap_size_in_bytes(r); serializedbytes = (bytea *) palloc(VARHDRSZ + expectedsize); - roaring_bitmap_portable_serialize(r, VARDATA(serializedbytes)); + roaring_bitmap_serialize(r, VARDATA(serializedbytes)); roaring_bitmap_free(r); SET_VARSIZE(serializedbytes, VARHDRSZ + expectedsize); @@ -608,13 +614,13 @@ rb_xor(PG_FUNCTION_ARGS) { size_t expectedsize; bytea *serializedbytes; - r1 = roaring_bitmap_portable_deserialize(VARDATA(serializedbytes1)); + r1 = roaring_bitmap_deserialize(VARDATA(serializedbytes1)); if (!r1) ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), errmsg("bitmap format is error"))); - r2 = roaring_bitmap_portable_deserialize(VARDATA(serializedbytes2)); + r2 = roaring_bitmap_deserialize(VARDATA(serializedbytes2)); if (!r2) { roaring_bitmap_free(r1); ereport(ERROR, @@ -624,9 +630,9 @@ rb_xor(PG_FUNCTION_ARGS) { roaring_bitmap_xor_inplace(r1, r2); roaring_bitmap_free(r2); - expectedsize = roaring_bitmap_portable_size_in_bytes(r1); + expectedsize = roaring_bitmap_size_in_bytes(r1); serializedbytes = (bytea *) palloc(VARHDRSZ + expectedsize); - roaring_bitmap_portable_serialize(r1, VARDATA(serializedbytes)); + roaring_bitmap_serialize(r1, VARDATA(serializedbytes)); roaring_bitmap_free(r1); SET_VARSIZE(serializedbytes, VARHDRSZ + expectedsize); @@ -1002,7 +1008,7 @@ rb_add(PG_FUNCTION_ARGS) { size_t expectedsize; bytea *serializedbytes; - roaring_bitmap_t *r1 = roaring_bitmap_portable_deserialize(VARDATA(serializedbytes1)); + roaring_bitmap_t *r1 = roaring_bitmap_deserialize(VARDATA(serializedbytes1)); if (!r1) ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), @@ -1010,9 +1016,9 @@ rb_add(PG_FUNCTION_ARGS) { roaring_bitmap_add(r1, value); - expectedsize = roaring_bitmap_portable_size_in_bytes(r1); + expectedsize = roaring_bitmap_size_in_bytes(r1); serializedbytes = (bytea *) palloc(VARHDRSZ + expectedsize); - roaring_bitmap_portable_serialize(r1, VARDATA(serializedbytes)); + roaring_bitmap_serialize(r1, VARDATA(serializedbytes)); roaring_bitmap_free(r1); SET_VARSIZE(serializedbytes, VARHDRSZ + expectedsize); @@ -1030,7 +1036,7 @@ rb_remove(PG_FUNCTION_ARGS) { size_t expectedsize; bytea *serializedbytes; - roaring_bitmap_t *r1 = roaring_bitmap_portable_deserialize(VARDATA(serializedbytes1)); + roaring_bitmap_t *r1 = roaring_bitmap_deserialize(VARDATA(serializedbytes1)); if (!r1) ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), @@ -1038,9 +1044,9 @@ rb_remove(PG_FUNCTION_ARGS) { roaring_bitmap_remove(r1, value); - expectedsize = roaring_bitmap_portable_size_in_bytes(r1); + expectedsize = roaring_bitmap_size_in_bytes(r1); serializedbytes = (bytea *) palloc(VARHDRSZ + expectedsize); - roaring_bitmap_portable_serialize(r1, VARDATA(serializedbytes)); + roaring_bitmap_serialize(r1, VARDATA(serializedbytes)); roaring_bitmap_free(r1); SET_VARSIZE(serializedbytes, VARHDRSZ + expectedsize); @@ -1212,7 +1218,7 @@ rb_fill(PG_FUNCTION_ARGS) { rangeend = MAX_BITMAP_RANGE_END; } - r1 = roaring_bitmap_portable_deserialize(VARDATA(serializedbytes1)); + r1 = roaring_bitmap_deserialize(VARDATA(serializedbytes1)); if (!r1) ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), @@ -1230,9 +1236,9 @@ rb_fill(PG_FUNCTION_ARGS) { roaring_bitmap_free(r2); } - expectedsize = roaring_bitmap_portable_size_in_bytes(r1); + expectedsize = roaring_bitmap_size_in_bytes(r1); serializedbytes = (bytea *) palloc(VARHDRSZ + expectedsize); - roaring_bitmap_portable_serialize(r1, VARDATA(serializedbytes)); + roaring_bitmap_serialize(r1, VARDATA(serializedbytes)); roaring_bitmap_free(r1); SET_VARSIZE(serializedbytes, VARHDRSZ + expectedsize); @@ -1261,7 +1267,7 @@ rb_clear(PG_FUNCTION_ARGS) { rangeend = MAX_BITMAP_RANGE_END; } - r1 = roaring_bitmap_portable_deserialize(VARDATA(serializedbytes1)); + r1 = roaring_bitmap_deserialize(VARDATA(serializedbytes1)); if (!r1) ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), @@ -1280,9 +1286,9 @@ rb_clear(PG_FUNCTION_ARGS) { roaring_bitmap_free(r2); } - expectedsize = roaring_bitmap_portable_size_in_bytes(r1); + expectedsize = roaring_bitmap_size_in_bytes(r1); serializedbytes = (bytea *) palloc(VARHDRSZ + expectedsize); - roaring_bitmap_portable_serialize(r1, VARDATA(serializedbytes)); + roaring_bitmap_serialize(r1, VARDATA(serializedbytes)); roaring_bitmap_free(r1); SET_VARSIZE(serializedbytes, VARHDRSZ + expectedsize); @@ -1310,7 +1316,7 @@ rb_flip(PG_FUNCTION_ARGS) { rangeend = MAX_BITMAP_RANGE_END; } - r1 = roaring_bitmap_portable_deserialize(VARDATA(serializedbytes1)); + r1 = roaring_bitmap_deserialize(VARDATA(serializedbytes1)); if (!r1) ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), @@ -1320,9 +1326,9 @@ rb_flip(PG_FUNCTION_ARGS) { roaring_bitmap_flip_inplace(r1, rangestart, rangeend); } - expectedsize = roaring_bitmap_portable_size_in_bytes(r1); + expectedsize = roaring_bitmap_size_in_bytes(r1); serializedbytes = (bytea *) palloc(VARHDRSZ + expectedsize); - roaring_bitmap_portable_serialize(r1, VARDATA(serializedbytes)); + roaring_bitmap_serialize(r1, VARDATA(serializedbytes)); roaring_bitmap_free(r1); SET_VARSIZE(serializedbytes, VARHDRSZ + expectedsize); @@ -1344,7 +1350,7 @@ rb_shiftright(PG_FUNCTION_ARGS) { size_t expectedsize; bytea *serializedbytes; - r1 = roaring_bitmap_portable_deserialize(VARDATA(serializedbytes1)); + r1 = roaring_bitmap_deserialize(VARDATA(serializedbytes1)); if (!r1) ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), @@ -1383,9 +1389,9 @@ rb_shiftright(PG_FUNCTION_ARGS) { r1 = r2; } - expectedsize = roaring_bitmap_portable_size_in_bytes(r1); + expectedsize = roaring_bitmap_size_in_bytes(r1); serializedbytes = (bytea *) palloc(VARHDRSZ + expectedsize); - roaring_bitmap_portable_serialize(r1, VARDATA(serializedbytes)); + roaring_bitmap_serialize(r1, VARDATA(serializedbytes)); roaring_bitmap_free(r1); SET_VARSIZE(serializedbytes, VARHDRSZ + expectedsize); @@ -1415,12 +1421,12 @@ rb_range(PG_FUNCTION_ARGS) { rangeend = MAX_BITMAP_RANGE_END; } - r1 = roaring_bitmap_portable_deserialize(VARDATA(serializedbytes1)); + r1 = roaring_bitmap_deserialize(VARDATA(serializedbytes1)); if (!r1) ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), errmsg("bitmap format is error"))); - + r2 = roaring_bitmap_create(); if (!r2) { roaring_bitmap_free(r1); @@ -1438,9 +1444,9 @@ rb_range(PG_FUNCTION_ARGS) { roaring_advance_uint32_iterator(&iterator); } - expectedsize = roaring_bitmap_portable_size_in_bytes(r2); + expectedsize = roaring_bitmap_size_in_bytes(r2); serializedbytes = (bytea *) palloc(VARHDRSZ + expectedsize); - roaring_bitmap_portable_serialize(r2, VARDATA(serializedbytes)); + roaring_bitmap_serialize(r2, VARDATA(serializedbytes)); roaring_bitmap_free(r1); roaring_bitmap_free(r2); @@ -1469,7 +1475,7 @@ rb_range_cardinality(PG_FUNCTION_ARGS) { rangeend = MAX_BITMAP_RANGE_END; } - r1 = roaring_bitmap_portable_deserialize(VARDATA(serializedbytes1)); + r1 = roaring_bitmap_deserialize(VARDATA(serializedbytes1)); if (!r1) ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), @@ -1517,7 +1523,7 @@ rb_select(PG_FUNCTION_ARGS) { rangeend = MAX_BITMAP_RANGE_END; } - r1 = roaring_bitmap_portable_deserialize(VARDATA(serializedbytes1)); + r1 = roaring_bitmap_deserialize(VARDATA(serializedbytes1)); if (!r1) ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), @@ -1575,9 +1581,9 @@ rb_select(PG_FUNCTION_ARGS) { } } - expectedsize = roaring_bitmap_portable_size_in_bytes(r2); + expectedsize = roaring_bitmap_size_in_bytes(r2); serializedbytes = (bytea *) palloc(VARHDRSZ + expectedsize); - roaring_bitmap_portable_serialize(r2, VARDATA(serializedbytes)); + roaring_bitmap_serialize(r2, VARDATA(serializedbytes)); roaring_bitmap_free(r1); roaring_bitmap_free(r2); @@ -1609,10 +1615,10 @@ rb_build(PG_FUNCTION_ARGS) { roaring_bitmap_add(r1, da[n]); } - expectedsize = roaring_bitmap_portable_size_in_bytes(r1); + expectedsize = roaring_bitmap_size_in_bytes(r1); serializedbytes = (bytea *) palloc(VARHDRSZ + expectedsize); - roaring_bitmap_portable_serialize(r1, VARDATA(serializedbytes)); + roaring_bitmap_serialize(r1, VARDATA(serializedbytes)); roaring_bitmap_free(r1); SET_VARSIZE(serializedbytes, VARHDRSZ + expectedsize); @@ -1634,7 +1640,7 @@ rb_to_array(PG_FUNCTION_ARGS) uint64_t card1; uint32_t counter = 0; - r1 = roaring_bitmap_portable_deserialize(VARDATA(serializedbytes)); + r1 = roaring_bitmap_deserialize(VARDATA(serializedbytes)); if (!r1) ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), @@ -1686,7 +1692,7 @@ rb_iterate(PG_FUNCTION_ARGS) { oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - r1 = roaring_bitmap_portable_deserialize(VARDATA(data)); + r1 = roaring_bitmap_deserialize(VARDATA(data)); if (!r1) ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), @@ -1742,7 +1748,7 @@ rb_or_trans(PG_FUNCTION_ARGS) { oldcontext = MemoryContextSwitchTo(aggctx); - r2 = roaring_bitmap_portable_deserialize(VARDATA(bb)); + r2 = roaring_bitmap_deserialize(VARDATA(bb)); if (PG_ARGISNULL(0)) { r1 = r2; @@ -1827,14 +1833,14 @@ rb_and_trans(PG_FUNCTION_ARGS) { bb = PG_GETARG_BYTEA_P(1); oldcontext = MemoryContextSwitchTo(aggctx); - r2 = roaring_bitmap_portable_deserialize(VARDATA(bb)); + r2 = roaring_bitmap_deserialize(VARDATA(bb)); MemoryContextSwitchTo(oldcontext); r1 = r2; } else { r1 = (roaring_bitmap_t *) PG_GETARG_POINTER(0); if (!roaring_bitmap_is_empty(r1)) { bb = PG_GETARG_BYTEA_P(1); - r2 = roaring_bitmap_portable_deserialize(VARDATA(bb)); + r2 = roaring_bitmap_deserialize(VARDATA(bb)); oldcontext = MemoryContextSwitchTo(aggctx); roaring_bitmap_and_inplace(r1, r2); @@ -1916,7 +1922,7 @@ rb_xor_trans(PG_FUNCTION_ARGS) { oldcontext = MemoryContextSwitchTo(aggctx); - r2 = roaring_bitmap_portable_deserialize(VARDATA(bb)); + r2 = roaring_bitmap_deserialize(VARDATA(bb)); if (PG_ARGISNULL(0)) { r1 = r2; @@ -2036,9 +2042,9 @@ rb_serialize(PG_FUNCTION_ARGS) { } else { r1 = (roaring_bitmap_t *) PG_GETARG_POINTER(0); - expectedsize = roaring_bitmap_portable_size_in_bytes(r1); + expectedsize = roaring_bitmap_size_in_bytes(r1); serializedbytes = (bytea *) palloc(VARHDRSZ + expectedsize); - roaring_bitmap_portable_serialize(r1, VARDATA(serializedbytes)); + roaring_bitmap_serialize(r1, VARDATA(serializedbytes)); SET_VARSIZE(serializedbytes, VARHDRSZ + expectedsize); PG_RETURN_BYTEA_P(serializedbytes); @@ -2066,7 +2072,7 @@ rb_deserialize(PG_FUNCTION_ARGS) { PG_RETURN_NULL(); } else { serializedbytes = PG_GETARG_BYTEA_P(0); - r1 = roaring_bitmap_portable_deserialize(VARDATA(serializedbytes)); + r1 = roaring_bitmap_deserialize(VARDATA(serializedbytes)); if (!r1) ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), @@ -2116,7 +2122,7 @@ rb_runoptimize(PG_FUNCTION_ARGS) { roaring_bitmap_t *r; size_t expectedsize; - r = roaring_bitmap_portable_deserialize(VARDATA(serializedbytes)); + r = roaring_bitmap_deserialize(VARDATA(serializedbytes)); if (!r) ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), @@ -2124,9 +2130,9 @@ rb_runoptimize(PG_FUNCTION_ARGS) { roaring_bitmap_run_optimize(r); - expectedsize = roaring_bitmap_portable_size_in_bytes(r); + expectedsize = roaring_bitmap_size_in_bytes(r); serializedbytes = (bytea *) palloc(VARHDRSZ + expectedsize); - roaring_bitmap_portable_serialize(r, VARDATA(serializedbytes)); + roaring_bitmap_serialize(r, VARDATA(serializedbytes)); roaring_bitmap_free(r); SET_VARSIZE(serializedbytes, VARHDRSZ + expectedsize); From 72124954ad5b66121a2b616b47a0ae77194f8d66 Mon Sep 17 00:00:00 2001 From: smmathews-bw-boston Date: Tue, 10 Oct 2023 18:25:35 -0400 Subject: [PATCH 2/4] serialize non-portable after deserializing from portable --- roaringbitmap.c | 118 ++++++++++++++++++++++++------------------------ 1 file changed, 58 insertions(+), 60 deletions(-) diff --git a/roaringbitmap.c b/roaringbitmap.c index 18694f0..9ce1901 100644 --- a/roaringbitmap.c +++ b/roaringbitmap.c @@ -177,90 +177,88 @@ roaringbitmap_in(PG_FUNCTION_ARGS) { ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), errmsg("bitmap format is error"))); + } else { + /* int array input */ - roaring_bitmap_free(r1); - return dd; - } - /* else int array input */ - - /* Find the head char '{' */ - while (*ptr && isspace((unsigned char) *ptr)) + /* Find the head char '{' */ + while (*ptr && isspace((unsigned char) *ptr)) ptr++; - if (*ptr !='{') - ereport(ERROR, - (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("malformed bitmap literal"))); - ptr++; + if (*ptr != '{') + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("malformed bitmap literal"))); + ptr++; - r1 = roaring_bitmap_create(); + r1 = roaring_bitmap_create(); - while (*ptr && isspace((unsigned char) *ptr)) - ptr++; + while (*ptr && isspace((unsigned char) *ptr)) + ptr++; - if (*ptr != '}') { - while (*ptr) { - /* Parse int element */ - errno = 0; - l = strtol(ptr, &badp, 10); + if (*ptr != '}') { + while (*ptr) { + /* Parse int element */ + errno = 0; + l = strtol(ptr, &badp, 10); - /* We made no progress parsing the string, so bail out */ - if (ptr == badp){ - roaring_bitmap_free(r1); - ereport(ERROR, - (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), - errmsg("invalid input syntax for %s: \"%s\"", - "integer", ptr))); - } + /* We made no progress parsing the string, so bail out */ + if (ptr == badp) { + roaring_bitmap_free(r1); + ereport(ERROR, + (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), + errmsg("invalid input syntax for %s: \"%s\"", + "integer", ptr))); + } - if (errno == ERANGE - || l < INT4_MIN || l > INT4_MAX - ){ + if (errno == ERANGE + || l < INT4_MIN || l > INT4_MAX + ) { roaring_bitmap_free(r1); ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), - errmsg("value \"%s\" is out of range for type %s", ptr, - "integer"))); + errmsg("value \"%s\" is out of range for type %s", ptr, + "integer"))); } - /* Add int element to bitmap */ - roaring_bitmap_add(r1, l); + /* Add int element to bitmap */ + roaring_bitmap_add(r1, l); - /* Skip any trailing whitespace after the int element */ - ptr = badp; - while (*ptr && isspace((unsigned char) *ptr)) + /* Skip any trailing whitespace after the int element */ + ptr = badp; + while (*ptr && isspace((unsigned char) *ptr)) + ptr++; + + /* Find the element terminator ',' */ + if (*ptr != ',') + break; ptr++; - /* Find the element terminator ',' */ - if (*ptr != ',') - break; - ptr++; + /* Skip any trailing whitespace after the terminator */ + while (*ptr && isspace((unsigned char) *ptr)) + ptr++; + } - /* Skip any trailing whitespace after the terminator */ - while (*ptr && isspace((unsigned char) *ptr)) - ptr++; + /* Find the tail char '{' */ + if (*ptr != '}') { + roaring_bitmap_free(r1); + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("malformed bitmap literal"))); + } } - /* Find the tail char '{' */ - if (*ptr !='}'){ + /* Check if input end */ + ptr++; + while (*ptr && isspace((unsigned char) *ptr)) + ptr++; + + if (*ptr != '\0') { roaring_bitmap_free(r1); ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("malformed bitmap literal"))); + errmsg("malformed bitmap literal"))); } } - /* Check if input end */ - ptr++; - while (*ptr && isspace((unsigned char) *ptr)) - ptr++; - - if (*ptr !='\0'){ - roaring_bitmap_free(r1); - ereport(ERROR, - (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("malformed bitmap literal"))); - } - expectedsize = roaring_bitmap_size_in_bytes(r1); serializedbytes = (bytea *) palloc(VARHDRSZ + expectedsize); roaring_bitmap_serialize(r1, VARDATA(serializedbytes)); From 83fd071e1eebb0ad898f9b5d98b78d4490d80fcd Mon Sep 17 00:00:00 2001 From: smmathews-bw-boston Date: Tue, 10 Oct 2023 19:10:53 -0400 Subject: [PATCH 3/4] have roaring_buffer_create use the non-portable version --- roaring_buffer_reader.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/roaring_buffer_reader.c b/roaring_buffer_reader.c index 4456f22..ef03047 100644 --- a/roaring_buffer_reader.c +++ b/roaring_buffer_reader.c @@ -239,7 +239,7 @@ static bool rb_append_copy_range(roaring_array_t *ra, const roaring_buffer_t *rb /** - * Creates a new roaring buffer (from a partable serialized roaringbitmap buffer). + * Creates a new roaring buffer (from a serialized roaringbitmap buffer). * Returns NULL if error occurred. */ roaring_buffer_t *roaring_buffer_create(const char *buf, size_t buf_len){ @@ -612,7 +612,7 @@ roaring_bitmap_t *roaring_buffer_andnot(const roaring_buffer_t *x1, return empty_bitmap; } if (0 == length2) { - return roaring_bitmap_portable_deserialize(x1->buf); + return roaring_bitmap_deserialize(x1->buf); } roaring_bitmap_t *answer = roaring_bitmap_create_with_capacity(length1); if(answer == NULL) From 993a5c302f286a5abfc152186af441df196bc7cf Mon Sep 17 00:00:00 2001 From: smmathews-bw-boston Date: Tue, 10 Oct 2023 19:27:26 -0400 Subject: [PATCH 4/4] set the var size --- roaringbitmap.c | 1 + 1 file changed, 1 insertion(+) diff --git a/roaringbitmap.c b/roaringbitmap.c index 9ce1901..18e046b 100644 --- a/roaringbitmap.c +++ b/roaringbitmap.c @@ -292,6 +292,7 @@ roaringbitmap_out(PG_FUNCTION_ARGS) { serializedbytes = (bytea *) palloc(VARHDRSZ + expectedsize); roaring_bitmap_portable_serialize(r1, VARDATA(serializedbytes)); roaring_bitmap_free(r1); + SET_VARSIZE(serializedbytes, VARHDRSZ + expectedsize); return DirectFunctionCall1(byteaout, PointerGetDatum(serializedbytes)); }