| 1027 | |
| 1028 | PG_FUNCTION_INFO_V1(array_normalize); |
| 1029 | Datum |
| 1030 | array_normalize(PG_FUNCTION_ARGS){ |
| 1031 | ArrayType * arg = PG_GETARG_ARRAYTYPE_P(0); |
| 1032 | |
| 1033 | if (ARR_NDIM(arg) != 1) { |
| 1034 | ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 1035 | errmsg("Input array with multiple dimensions is not allowed!"))); |
| 1036 | } |
| 1037 | |
| 1038 | if (ARR_HASNULL(arg)) { |
| 1039 | ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), |
| 1040 | errmsg("Input array with nulls is not allowed!"))); |
| 1041 | } |
| 1042 | |
| 1043 | ArrayType *v = array_to_float8_array(arg); |
| 1044 | |
| 1045 | Datum norm_sqr = General_Array_to_Element(v, Float8GetDatum(0), 0.0, |
| 1046 | element_sum_sqr, noop_finalize); |
| 1047 | |
| 1048 | if (DatumGetFloat8(norm_sqr) == 0.) { |
| 1049 | elog(WARNING, "No non-zero elements found, returning the input array."); |
| 1050 | PG_RETURN_ARRAYTYPE_P(arg); |
| 1051 | } |
| 1052 | Datum inverse_norm = Float8GetDatum(1.0/sqrt(DatumGetFloat8(norm_sqr))); |
| 1053 | ArrayType* res = General_Array_to_Array(v, inverse_norm, element_mult); |
| 1054 | |
| 1055 | if (v != arg) { pfree(v); } |
| 1056 | PG_FREE_IF_COPY(arg,0); |
| 1057 | |
| 1058 | PG_RETURN_ARRAYTYPE_P(res); |
| 1059 | |
| 1060 | } |
| 1061 | /* |
| 1062 | * This function checks if an array contains NULL values. |
| 1063 | */ |
nothing calls this directly
no test coverage detected