* construct_md_array --- simple method for constructing an array object * with arbitrary dimensions and possible NULLs * * elems: array of Datum items to become the array contents * nulls: array of is-null flags (can be NULL if no nulls) * ndims: number of dimensions * dims: integer array with size of each dimension * lbs: integer array with lower bound of each dimension * elmtype, e
| 3407 | * to hard-wire values if the element type is hard-wired. |
| 3408 | */ |
| 3409 | ArrayType * |
| 3410 | construct_md_array(Datum *elems, |
| 3411 | bool *nulls, |
| 3412 | int ndims, |
| 3413 | int *dims, |
| 3414 | int *lbs, |
| 3415 | Oid elmtype, int elmlen, bool elmbyval, char elmalign) |
| 3416 | { |
| 3417 | ArrayType *result; |
| 3418 | bool hasnulls; |
| 3419 | int32 nbytes; |
| 3420 | int32 dataoffset; |
| 3421 | int i; |
| 3422 | int nelems; |
| 3423 | bool fixedwidthtype; |
| 3424 | |
| 3425 | if (ndims < 0) /* we do allow zero-dimension arrays */ |
| 3426 | ereport(ERROR, |
| 3427 | (errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 3428 | errmsg("invalid number of dimensions: %d", ndims))); |
| 3429 | if (ndims > MAXDIM) |
| 3430 | ereport(ERROR, |
| 3431 | (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), |
| 3432 | errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)", |
| 3433 | ndims, MAXDIM))); |
| 3434 | |
| 3435 | /* This checks for overflow of the array dimensions */ |
| 3436 | nelems = ArrayGetNItems(ndims, dims); |
| 3437 | ArrayCheckBounds(ndims, dims, lbs); |
| 3438 | |
| 3439 | /* if ndims <= 0 or any dims[i] == 0, return empty array */ |
| 3440 | if (nelems <= 0) |
| 3441 | return construct_empty_array(elmtype); |
| 3442 | |
| 3443 | /* compute required space */ |
| 3444 | nbytes = 0; |
| 3445 | |
| 3446 | /* fast path for fixed width types */ |
| 3447 | switch (elmtype) |
| 3448 | { |
| 3449 | case INT2OID: |
| 3450 | case INT4OID: |
| 3451 | case INT8OID: |
| 3452 | case FLOAT4OID: |
| 3453 | case FLOAT8OID: |
| 3454 | fixedwidthtype=true; |
| 3455 | break; |
| 3456 | default: |
| 3457 | fixedwidthtype=false; |
| 3458 | } |
| 3459 | hasnulls = false; |
| 3460 | if (fixedwidthtype) |
| 3461 | { |
| 3462 | nbytes = nelems * elmlen; |
| 3463 | |
| 3464 | /* Still need to handle the possibility of nulls */ |
| 3465 | if (nulls) |
| 3466 | { |