1010
1111typedef enum
1212{
13- RBITMAP_OUTPUT_ARRAY , /* output as int array */
14- RBITMAP_OUTPUT_BYTEA /* output as bytea */
13+ RBITMAP_OUTPUT_ARRAY , /* output as int array */
14+ RBITMAP_OUTPUT_BYTEA /* output as bytea */
1515}RBITMAPOutputFormat ;
1616
1717static const struct config_enum_entry output_format_options [] =
@@ -21,7 +21,7 @@ static const struct config_enum_entry output_format_options[] =
2121 {NULL , 0 , false}
2222};
2323
24- static int rbitmap_output_format = RBITMAP_OUTPUT_BYTEA ; /* output format */
24+ static int rbitmap_output_format = RBITMAP_OUTPUT_BYTEA ; /* output format */
2525
2626static roaring_memory_t rb_memory_hook = {
2727 .malloc = palloc ,
@@ -32,25 +32,25 @@ static roaring_memory_t rb_memory_hook = {
3232 .aligned_free = pg_aligned_free ,
3333};
3434
35- void _PG_init (void );
35+ void _PG_init (void );
3636/*
3737 * Module load callback
3838 */
3939void
4040_PG_init (void )
4141{
42- /* Define custom GUC variables. */
43- DefineCustomEnumVariable ("roaringbitmap.output_format" ,
44- "Selects output format of roaringbitmap." ,
45- NULL ,
46- & rbitmap_output_format ,
47- RBITMAP_OUTPUT_BYTEA ,
48- output_format_options ,
49- PGC_USERSET ,
50- 0 ,
51- NULL ,
52- NULL ,
53- NULL );
42+ /* Define custom GUC variables. */
43+ DefineCustomEnumVariable ("roaringbitmap.output_format" ,
44+ "Selects output format of roaringbitmap." ,
45+ NULL ,
46+ & rbitmap_output_format ,
47+ RBITMAP_OUTPUT_BYTEA ,
48+ output_format_options ,
49+ PGC_USERSET ,
50+ 0 ,
51+ NULL ,
52+ NULL ,
53+ NULL );
5454 roaring_init_memory_hook (rb_memory_hook );
5555}
5656
@@ -99,15 +99,22 @@ Datum
9999rb_from_bytea (PG_FUNCTION_ARGS ) {
100100 bytea * serializedbytes = PG_GETARG_BYTEA_P (0 );
101101 roaring_bitmap_t * r1 ;
102+ size_t expectedsize ;
103+ bytea * serializedbytes2 ;
102104
103105 r1 = roaring_bitmap_portable_deserialize_safe (VARDATA (serializedbytes ), VARSIZE (serializedbytes ) - VARHDRSZ );
104106 if (!r1 )
105107 ereport (ERROR ,
106108 (errcode (ERRCODE_NULL_VALUE_NOT_ALLOWED ),
107109 errmsg ("bitmap format is error" )));
108110
111+ expectedsize = roaring_bitmap_portable_size_in_bytes (r1 );
112+ serializedbytes2 = (bytea * ) palloc (VARHDRSZ + expectedsize );
113+ roaring_bitmap_portable_serialize (r1 , VARDATA (serializedbytes2 ));
109114 roaring_bitmap_free (r1 );
110- PG_RETURN_BYTEA_P (serializedbytes );
115+
116+ SET_VARSIZE (serializedbytes2 , VARHDRSZ + expectedsize );
117+ PG_RETURN_BYTEA_P (serializedbytes2 );
111118}
112119
113120
@@ -136,12 +143,17 @@ roaringbitmap_in(PG_FUNCTION_ARGS) {
136143 (errcode (ERRCODE_NULL_VALUE_NOT_ALLOWED ),
137144 errmsg ("bitmap format is error" )));
138145
146+ expectedsize = roaring_bitmap_portable_size_in_bytes (r1 );
147+ serializedbytes = (bytea * ) palloc (VARHDRSZ + expectedsize );
148+ roaring_bitmap_portable_serialize (r1 , VARDATA (serializedbytes ));
139149 roaring_bitmap_free (r1 );
140- return dd ;
150+
151+ SET_VARSIZE (serializedbytes , VARHDRSZ + expectedsize );
152+ PG_RETURN_BYTEA_P (serializedbytes );
141153 }
142154 /* else int array input */
143155
144- /* Find the head char '{' */
156+ /* Find the head char '{' */
145157 while (* ptr && isspace ((unsigned char ) * ptr ))
146158 ptr ++ ;
147159 if (* ptr != '{' )
0 commit comments