From ee79d34010b8bebc3844b1bf4a733f4bb4152dbe Mon Sep 17 00:00:00 2001 From: Artur Zakirov Date: Fri, 9 Aug 2024 13:01:53 +0200 Subject: [PATCH] Copy jsonb value within populate_slot() into the allocator buffer --- src/reader.cpp | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/reader.cpp b/src/reader.cpp index 1b1e99c..01dc85a 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -1003,10 +1003,21 @@ class DefaultParquetReader : public ParquetReader } case arrow::Type::MAP: { - arrow::MapArray* maparray = (arrow::MapArray*) array; + arrow::MapArray *maparray = (arrow::MapArray*) array; + Datum jsonb = this->map_to_datum(maparray, chunkInfo.pos, typinfo); - slot->tts_values[attr] = - this->map_to_datum(maparray, chunkInfo.pos, typinfo); + /* + * Copy jsonb into memory block allocated by + * FastAllocator to prevent its destruction though + * to be able to recycle it once it fulfilled its + * purpose. + */ + void *jsonb_val = allocator->fast_alloc(VARSIZE_ANY(jsonb)); + + memcpy(jsonb_val, DatumGetPointer(jsonb), VARSIZE_ANY(jsonb)); + pfree(DatumGetPointer(jsonb)); + + slot->tts_values[attr] = PointerGetDatum(jsonb_val); break; } default: @@ -1255,9 +1266,7 @@ class CachingParquetReader : public ParquetReader case arrow::Type::MAP: { arrow::MapArray* maparray = (arrow::MapArray*) array; - - Datum jsonb = - this->map_to_datum(maparray, j, typinfo); + Datum jsonb = this->map_to_datum(maparray, j, typinfo); /* * Copy jsonb into memory block allocated by @@ -1265,10 +1274,13 @@ class CachingParquetReader : public ParquetReader * to be able to recycle it once it fulfilled its * purpose. */ - void *res = allocator->fast_alloc(VARSIZE_ANY(jsonb)); - memcpy(res, (Jsonb *) jsonb, VARSIZE_ANY(jsonb)); - ((Datum *) data)[row] = (Datum) res; - pfree((Jsonb *) jsonb); + void *jsonb_val = allocator->fast_alloc(VARSIZE_ANY(jsonb)); + + memcpy(jsonb_val, DatumGetPointer(jsonb), VARSIZE_ANY(jsonb)); + pfree(DatumGetPointer(jsonb)); + + ((Datum *) data)[row] = PointerGetDatum(jsonb_val); + break; } default: