* numeric_avg_deserialize * Deserialize bytea into NumericAggState for numeric aggregates that * don't require sumX2. Deserializes bytea into NumericAggState using the * standard pq API. * * numeric_avg_serialize(numeric_avg_deserialize(bytea)) must result in a value * which matches the original bytea value. */
| 5089 | * which matches the original bytea value. |
| 5090 | */ |
| 5091 | Datum |
| 5092 | numeric_avg_deserialize(PG_FUNCTION_ARGS) |
| 5093 | { |
| 5094 | bytea *sstate; |
| 5095 | NumericAggState *result; |
| 5096 | Datum temp; |
| 5097 | NumericVar tmp_var; |
| 5098 | StringInfoData buf; |
| 5099 | |
| 5100 | if (!AggCheckCallContext(fcinfo, NULL)) |
| 5101 | elog(ERROR, "aggregate function called in non-aggregate context"); |
| 5102 | |
| 5103 | sstate = PG_GETARG_BYTEA_PP(0); |
| 5104 | |
| 5105 | /* |
| 5106 | * Copy the bytea into a StringInfo so that we can "receive" it using the |
| 5107 | * standard recv-function infrastructure. |
| 5108 | */ |
| 5109 | initStringInfo(&buf); |
| 5110 | appendBinaryStringInfo(&buf, |
| 5111 | VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate)); |
| 5112 | |
| 5113 | result = makeNumericAggStateCurrentContext(false); |
| 5114 | |
| 5115 | /* N */ |
| 5116 | result->N = pq_getmsgint64(&buf); |
| 5117 | |
| 5118 | /* sumX */ |
| 5119 | temp = DirectFunctionCall3(numeric_recv, |
| 5120 | PointerGetDatum(&buf), |
| 5121 | ObjectIdGetDatum(InvalidOid), |
| 5122 | Int32GetDatum(-1)); |
| 5123 | init_var_from_num(DatumGetNumeric(temp), &tmp_var); |
| 5124 | accum_sum_add(&(result->sumX), &tmp_var); |
| 5125 | |
| 5126 | /* maxScale */ |
| 5127 | result->maxScale = pq_getmsgint(&buf, 4); |
| 5128 | |
| 5129 | /* maxScaleCount */ |
| 5130 | result->maxScaleCount = pq_getmsgint64(&buf); |
| 5131 | |
| 5132 | /* NaNcount */ |
| 5133 | result->NaNcount = pq_getmsgint64(&buf); |
| 5134 | |
| 5135 | /* pInfcount */ |
| 5136 | result->pInfcount = pq_getmsgint64(&buf); |
| 5137 | |
| 5138 | /* nInfcount */ |
| 5139 | result->nInfcount = pq_getmsgint64(&buf); |
| 5140 | |
| 5141 | pq_getmsgend(&buf); |
| 5142 | pfree(buf.data); |
| 5143 | |
| 5144 | PG_RETURN_POINTER(result); |
| 5145 | } |
| 5146 | |
| 5147 | |
| 5148 | /* |
nothing calls this directly
no test coverage detected