| 11 | |
| 12 | PG_FUNCTION_INFO_V1(generate_sparse_vector); |
| 13 | Datum generate_sparse_vector(PG_FUNCTION_ARGS) |
| 14 | { |
| 15 | SvecType *output_sfv; |
| 16 | int16_t typlen; |
| 17 | bool typbyval; |
| 18 | char typalign; |
| 19 | bool *nulls; |
| 20 | |
| 21 | if (PG_NARGS() != 3) |
| 22 | elog(ERROR, "Invalid number of arguments."); |
| 23 | |
| 24 | ArrayType *term_index = PG_GETARG_ARRAYTYPE_P(0); |
| 25 | ArrayType *term_count = PG_GETARG_ARRAYTYPE_P(1); |
| 26 | |
| 27 | int64_t dict_size = PG_GETARG_INT64(2); |
| 28 | |
| 29 | /* Check if arrays have null entries */ |
| 30 | if (ARR_HASNULL(term_index) || ARR_HASNULL(term_count)) |
| 31 | elog(ERROR, "One or both of the argument arrays has one or more null entries."); |
| 32 | |
| 33 | if (dict_size <= 0) |
| 34 | elog(ERROR, "Dictionary size cannot be zero or negative."); |
| 35 | |
| 36 | /* Check if any of the argument arrays is empty */ |
| 37 | if ((ARR_NDIM(term_index) == 0) || (ARR_NDIM(term_count) == 0)) |
| 38 | elog(ERROR, "One or more argument arrays is empty."); |
| 39 | |
| 40 | int term_index_nelems = ARR_DIMS(term_index)[0]; |
| 41 | int term_count_nelems = ARR_DIMS(term_count)[0]; |
| 42 | |
| 43 | |
| 44 | /* If no. of elements in the arrays are not equal, throw an error */ |
| 45 | if (term_index_nelems != term_count_nelems) |
| 46 | elog(ERROR, "No. of elements in the argument arrays are not equal."); |
| 47 | |
| 48 | Datum *term_index_data; |
| 49 | Datum *term_count_data; |
| 50 | |
| 51 | /* Deconstruct the arrays */ |
| 52 | get_typlenbyvalalign(INT8OID, &typlen, &typbyval, &typalign); |
| 53 | deconstruct_array(term_index, INT8OID, typlen, typbyval, typalign, |
| 54 | &term_index_data, &nulls, &term_index_nelems); |
| 55 | |
| 56 | get_typlenbyvalalign(FLOAT8OID, &typlen, &typbyval, &typalign); |
| 57 | deconstruct_array(term_count, FLOAT8OID, typlen, typbyval, typalign, |
| 58 | &term_count_data, &nulls, &term_count_nelems); |
| 59 | |
| 60 | /* Check if term index array has indexes in proper order or not */ |
| 61 | for(int i = 0; i < term_index_nelems; i++) |
| 62 | { |
| 63 | if (DatumGetInt64(term_index_data[i]) < 0 || |
| 64 | DatumGetInt64(term_index_data[i]) >= dict_size) |
| 65 | elog(ERROR, "Term indexes must range from 0 to total number of elements in the dictonary - 1."); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | float8 *histogram = (float8 *)palloc0(sizeof(float8) * dict_size); |
| 70 | for (int k = 0; k < dict_size; k++) |
nothing calls this directly
no test coverage detected