* expand_array: convert an array Datum into an expanded array * * The expanded object will be a child of parentcontext. * * Some callers can provide cache space to avoid repeated lookups of element * type data across calls; if so, pass a metacache pointer, making sure that * metacache->element_type is initialized to InvalidOid before first call. * If no cross-call caching is required, pass
| 47 | * If no cross-call caching is required, pass NULL for metacache. |
| 48 | */ |
| 49 | Datum |
| 50 | expand_array(Datum arraydatum, MemoryContext parentcontext, |
| 51 | ArrayMetaState *metacache) |
| 52 | { |
| 53 | ArrayType *array; |
| 54 | ExpandedArrayHeader *eah; |
| 55 | MemoryContext objcxt; |
| 56 | MemoryContext oldcxt; |
| 57 | ArrayMetaState fakecache; |
| 58 | |
| 59 | /* |
| 60 | * Allocate private context for expanded object. We start by assuming |
| 61 | * that the array won't be very large; but if it does grow a lot, don't |
| 62 | * constrain aset.c's large-context behavior. |
| 63 | */ |
| 64 | objcxt = AllocSetContextCreate(parentcontext, |
| 65 | "expanded array", |
| 66 | ALLOCSET_START_SMALL_SIZES); |
| 67 | |
| 68 | /* Set up expanded array header */ |
| 69 | eah = (ExpandedArrayHeader *) |
| 70 | MemoryContextAlloc(objcxt, sizeof(ExpandedArrayHeader)); |
| 71 | |
| 72 | EOH_init_header(&eah->hdr, &EA_methods, objcxt); |
| 73 | eah->ea_magic = EA_MAGIC; |
| 74 | |
| 75 | /* If the source is an expanded array, we may be able to optimize */ |
| 76 | if (VARATT_IS_EXTERNAL_EXPANDED(DatumGetPointer(arraydatum))) |
| 77 | { |
| 78 | ExpandedArrayHeader *oldeah = (ExpandedArrayHeader *) DatumGetEOHP(arraydatum); |
| 79 | |
| 80 | Assert(oldeah->ea_magic == EA_MAGIC); |
| 81 | |
| 82 | /* |
| 83 | * Update caller's cache if provided; we don't need it this time, but |
| 84 | * next call might be for a non-expanded source array. Furthermore, |
| 85 | * if the caller didn't provide a cache area, use some local storage |
| 86 | * to cache anyway, thereby avoiding a catalog lookup in the case |
| 87 | * where we fall through to the flat-copy code path. |
| 88 | */ |
| 89 | if (metacache == NULL) |
| 90 | metacache = &fakecache; |
| 91 | metacache->element_type = oldeah->element_type; |
| 92 | metacache->typlen = oldeah->typlen; |
| 93 | metacache->typbyval = oldeah->typbyval; |
| 94 | metacache->typalign = oldeah->typalign; |
| 95 | |
| 96 | /* |
| 97 | * If element type is pass-by-value and we have a Datum-array |
| 98 | * representation, just copy the source's metadata and Datum/isnull |
| 99 | * arrays. The original flat array, if present at all, adds no |
| 100 | * additional information so we need not copy it. |
| 101 | */ |
| 102 | if (oldeah->typbyval && oldeah->dvalues != NULL) |
| 103 | { |
| 104 | copy_byval_expanded_array(eah, oldeah); |
| 105 | /* return a R/W pointer to the expanded array */ |
| 106 | return EOHPGetRWDatum(&eah->hdr); |
no test coverage detected