Datum alpine_plda_first(PG_FUNCTION_ARGS) { ArrayType *assign; ArrayType *topiccount; int32 * assign_array_data; int32 * topiccount_array_data; Datum values[2]; int32 column_size,topicnumber; int32 temptopic; int32 k; bool * isnulls ; TupleDesc tuple; HeapTuple ret; Datum * arr1; Datum * arr2; if (PG_ARGISNULL(0)){ PG_RETURN_NULL(); } column_size=PG_GETARG_INT32(0); topicnumber=PG_GETARG_INT32(1); arr1 = palloc0(column_size * sizeof(Datum));//Datum * assign = construct_array(arr1,column_size,INT4OID,4,true,'i'); assign_array_data = (int32 *)ARR_DATA_PTR(assign); arr2 = palloc0(topicnumber * sizeof(Datum));//Datum * topiccount = construct_array(arr2,topicnumber,INT4OID,4,true,'i'); topiccount_array_data = (int32 *)ARR_DATA_PTR(topiccount); for ( k = 0; k < column_size; k++){ temptopic = random() % topicnumber + 1; assign_array_data[k] = temptopic; topiccount_array_data[temptopic-1]++; } values[0] = PointerGetDatum(assign); values[1] = PointerGetDatum(topiccount); if (get_call_result_type(fcinfo, NULL, &tuple) != TYPEFUNC_COMPOSITE) ereport(ERROR, (errcode( ERRCODE_FEATURE_NOT_SUPPORTED ), errmsg( "function returning record called in context " "that cannot accept type record" ))); tuple = BlessTupleDesc(tuple); isnulls = palloc0(2 * sizeof(bool)); ret = heap_form_tuple(tuple, values, isnulls); if (isnulls[0] || isnulls[1]) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("function \"%s\" produced null results", format_procedure(fcinfo->flinfo->fn_oid)))); PG_RETURN_DATUM(HeapTupleGetDatum(ret)); }
Datum randomTopics(PG_FUNCTION_ARGS) { int32 doclen = PG_GETARG_INT32(0); int32 num_topics = PG_GETARG_INT32(1); ArrayType * ret_topics_arr, * ret_topic_d_arr; int32 * ret_topics, * ret_topic_d; Datum * arr1 = palloc0(doclen * sizeof(Datum)); ret_topics_arr = construct_array(arr1,doclen,INT4OID,4,true,'i'); ret_topics = (int32 *)ARR_DATA_PTR(ret_topics_arr); Datum * arr2 = palloc0(num_topics * sizeof(Datum)); ret_topic_d_arr = construct_array(arr2,num_topics,INT4OID,4,true,'i'); ret_topic_d = (int32 *)ARR_DATA_PTR(ret_topic_d_arr); /* Sample topics */ int i, rtopic; for (i=0; i!=doclen; i++) { rtopic = random() % num_topics + 1; ret_topics[i] = rtopic; ret_topic_d[rtopic-1]++; } /* Package up the return arrays */ Datum values[2]; values[0] = PointerGetDatum(ret_topics_arr); values[1] = PointerGetDatum(ret_topic_d_arr); TupleDesc tuple; if (get_call_result_type(fcinfo, NULL, &tuple) != TYPEFUNC_COMPOSITE) ereport(ERROR, (errcode( ERRCODE_FEATURE_NOT_SUPPORTED ), errmsg( "function returning record called in context " "that cannot accept type record" ))); tuple = BlessTupleDesc(tuple); bool * isnulls = palloc0(2 * sizeof(bool)); HeapTuple ret = heap_form_tuple(tuple, values, isnulls); if (isnulls[0] || isnulls[1]) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("function \"%s\" produced null results", format_procedure(fcinfo->flinfo->fn_oid),i))); PG_RETURN_DATUM(HeapTupleGetDatum(ret)); }
Datum lexize(PG_FUNCTION_ARGS) { text *in = PG_GETARG_TEXT_P(1); DictInfo *dict; TSLexeme *res, *ptr; Datum *da; ArrayType *a; SET_FUNCOID(); dict = finddict(PG_GETARG_OID(0)); ptr = res = (TSLexeme *) DatumGetPointer( FunctionCall3(&(dict->lexize_info), PointerGetDatum(dict->dictionary), PointerGetDatum(VARDATA(in)), Int32GetDatum(VARSIZE(in) - VARHDRSZ) ) ); PG_FREE_IF_COPY(in, 1); if (!res) { if (PG_NARGS() > 2) PG_RETURN_POINTER(NULL); else PG_RETURN_NULL(); } while (ptr->lexeme) ptr++; da = (Datum *) palloc(sizeof(Datum) * (ptr - res + 1)); ptr = res; while (ptr->lexeme) { da[ptr - res] = PointerGetDatum(char2text(ptr->lexeme)); ptr++; } a = construct_array( da, ptr - res, TEXTOID, -1, false, 'i' ); ptr = res; while (ptr->lexeme) { pfree(DatumGetPointer(da[ptr - res])); pfree(ptr->lexeme); ptr++; } pfree(res); pfree(da); PG_RETURN_POINTER(a); }
/* * Returns N-th range (in form of array) * * First argument is the parent relid. * Second argument is the index of the range (if it is negative then the last * range will be returned). */ Datum get_range_by_idx(PG_FUNCTION_ARGS) { int parent_oid = DatumGetInt32(PG_GETARG_DATUM(0)); int idx = DatumGetInt32(PG_GETARG_DATUM(1)); PartRelationInfo *prel; RangeRelation *rangerel; RangeEntry *ranges; RangeEntry *re; Datum *elems; TypeCacheEntry *tce; prel = get_pathman_relation_info(parent_oid, NULL); rangerel = get_pathman_range_relation(parent_oid, NULL); if (!prel || !rangerel || idx >= (int)rangerel->ranges.length) PG_RETURN_NULL(); tce = lookup_type_cache(prel->atttype, 0); ranges = dsm_array_get_pointer(&rangerel->ranges); if (idx >= 0) re = &ranges[idx]; else re = &ranges[rangerel->ranges.length - 1]; elems = palloc(2 * sizeof(Datum)); elems[0] = PATHMAN_GET_DATUM(re->min, rangerel->by_val); elems[1] = PATHMAN_GET_DATUM(re->max, rangerel->by_val); PG_RETURN_ARRAYTYPE_P( construct_array(elems, 2, prel->atttype, tce->typlen, tce->typbyval, tce->typalign)); }
Datum pgstrom_avg_int8_accum(PG_FUNCTION_ARGS) { ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0); int32 nrows = PG_GETARG_INT32(1); int64 psumX = PG_GETARG_INT64(2); int64 *transvalues; int64 newN; int64 newSumX; transvalues = check_int64_array(transarray, 2); newN = transvalues[0] + nrows; newSumX = transvalues[1] + psumX; if (AggCheckCallContext(fcinfo, NULL)) { transvalues[0] = newN; transvalues[1] = newSumX; PG_RETURN_ARRAYTYPE_P(transarray); } else { Datum transdatums[2]; ArrayType *result; transdatums[0] = Int64GetDatumFast(newN); transdatums[1] = Int64GetDatumFast(newSumX); result = construct_array(transdatums, 2, INT8OID, sizeof(int64), FLOAT8PASSBYVAL, 'd'); PG_RETURN_ARRAYTYPE_P(result); } }
Datum zero_array(PG_FUNCTION_ARGS) { int32 len = PG_GETARG_INT32(0); Datum * array = palloc0(len * sizeof(Datum)); ArrayType * pgarray = construct_array(array, len, INT4OID, 4, true, 'i'); PG_RETURN_ARRAYTYPE_P(pgarray); }
Datum stem_token_arr(PG_FUNCTION_ARGS) { if (PG_ARGISNULL(0)) { PG_RETURN_NULL(); } /* Prepare elements to receive input text[] */ ArrayType *arr = PG_GETARG_ARRAYTYPE_P(0); Datum *dtum; bool *nulls; int ndim; /* Deconstruct input text[] */ deconstruct_array(arr, TEXTOID, -1, false, 'i', &dtum, &nulls, &ndim); /* Prepare stemmer */ struct sb_stemmer *stemmer = sb_stemmer_new( "english" /* language */, NULL /* language encoding NULL for UTF-8 */); Assert(stemmer); /* Call stemming code */ text **result = (text **) palloc(ndim * sizeof(text * )); for(int i=0; i< ndim; i++) { text *token = dtum[i] == 0 ? NULL : DatumGetTextP(dtum[i]); char *empty; if(token == NULL) { empty = (char *)palloc(sizeof(char)); empty[0] = '\0'; } result[i] = (token == NULL ? cstring_to_text(empty) : cstring_to_text(stem_token_text(stemmer, token))); } ArrayType *res = construct_array((Datum*)result, ndim, TEXTOID, -1, false, 'i'); sb_stemmer_delete(stemmer); PG_RETURN_ARRAYTYPE_P(res); }
Datum postal_normalize(PG_FUNCTION_ARGS) { text *address = PG_GETARG_TEXT_P(0); size_t arr_nelems, i; libpostal_normalize_options_t options = libpostal_get_default_options(); char **expansions = libpostal_expand_address(text_to_cstring(address), options, &arr_nelems); ArrayType *arr; Datum *arr_elems = palloc(sizeof(Datum) * arr_nelems); Oid elem_type = TEXTOID; int16 elem_len; char elem_byval; char elem_align; get_typlenbyvalalign(elem_type, &elem_len, &elem_byval, &elem_align); for (i = 0; i < arr_nelems; i++) { arr_elems[i] = PointerGetDatum(cstring_to_text(expansions[i])); } /* Array construction takes a full copy of the input */ arr = construct_array(arr_elems, arr_nelems, elem_type, elem_len, elem_byval, elem_align); /* Clean up unmanaged memory */ libpostal_expansion_array_destroy(expansions, arr_nelems); PG_RETURN_ARRAYTYPE_P(arr); }
Datum tsvector2textarray(PG_FUNCTION_ARGS) { TSVector ts = PG_GETARG_TSVECTOR(0); ArrayType *a; Datum *words; int i; WordEntry *wptr = ARRPTR(ts); words = palloc( sizeof(Datum) * (ts->size+1) ); for(i=0; i<ts->size; i++) { text *t = palloc(VARHDRSZ + wptr->len); SET_VARSIZE(t, VARHDRSZ + wptr->len); memcpy( VARDATA(t), STRPTR(ts) + wptr->pos, wptr->len); words[i] = PointerGetDatum(t); wptr++; } a = construct_array( words, ts->size, TEXTOID, -1, false, 'i' ); PG_FREE_IF_COPY(ts, 0); PG_RETURN_ARRAYTYPE_P(a); }
Datum hstore_akeys(PG_FUNCTION_ARGS) { HStore *hs = PG_GETARG_HS(0); Datum *d; ArrayType *a; HEntry *entries = ARRPTR(hs); char *base = STRPTR(hs); int count = HS_COUNT(hs); int i; if (count == 0) { a = construct_empty_array(TEXTOID); PG_RETURN_POINTER(a); } d = (Datum *) palloc(sizeof(Datum) * count); for (i = 0; i < count; ++i) { text *item = cstring_to_text_with_len(HS_KEY(entries, base, i), HS_KEYLEN(entries, i)); d[i] = PointerGetDatum(item); } a = construct_array(d, count, TEXTOID, -1, false, 'i'); PG_RETURN_POINTER(a); }
static Datum build_array(element_set_t * eset, Oid element_type) { Datum * array_of_datums; int i; int16 typlen; bool typbyval; char typalign; /* do the compaction */ compact_set(eset, false); #if DEBUG_PROFILE print_set_stats(eset); #endif /* get detailed type information on the element type */ get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign); /* Copy data from compact array to array of Datums * A bit suboptimal way, spends excessive memory */ array_of_datums = palloc0(eset->nsorted * sizeof(Datum)); for (i = 0; i < eset->nsorted; i++) memcpy(array_of_datums + i, eset->data + (eset->item_size * i), eset->item_size); /* build and return the array */ PG_RETURN_DATUM(PointerGetDatum(construct_array( array_of_datums, eset->nsorted, element_type, typlen, typbyval, typalign ))); }
Datum float4_accum(PG_FUNCTION_ARGS) { ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0); float4 newval4 = PG_GETARG_FLOAT4(1); float8 *transvalues; float8 N, sumX, sumX2, newval; Datum transdatums[3]; ArrayType *result; transvalues = check_float8_array(transarray, "float4_accum"); N = transvalues[0]; sumX = transvalues[1]; sumX2 = transvalues[2]; /* Do arithmetic in float8 for best accuracy */ newval = newval4; N += 1.0; sumX += newval; sumX2 += newval * newval; transdatums[0] = Float8GetDatumFast(N); transdatums[1] = Float8GetDatumFast(sumX); transdatums[2] = Float8GetDatumFast(sumX2); result = construct_array(transdatums, 3, FLOAT8OID, sizeof(float8), false /* float8 byval */ , 'd'); PG_RETURN_ARRAYTYPE_P(result); }
Datum WeightedNoReplacement(PG_FUNCTION_ARGS) { int value1 = PG_GETARG_INT32(0); int value2 = PG_GETARG_INT32(1); int64 *result = (int64*)palloc(sizeof(int64)*value1); int32 i = 0; int32 k = 0; ArrayType *pgarray; float to_select = value1; for(;i < value2; i++){ if(rand()%100 < (to_select/(value2 - i))*100){ result[k] = i+1; k++; } if(to_select == 0) break; } pgarray = construct_array((Datum *)result, value1, INT8OID, sizeof(int64),true,'d'); PG_RETURN_ARRAYTYPE_P(pgarray); }
Datum rotation3d_explode(PG_FUNCTION_ARGS) { Rotation3D * rotation3d = (Rotation3D*)PG_GETARG_POINTER(0); ArrayType * result; Datum * result_data; result_data = (Datum*)palloc(sizeof(float8)*9); if (!result_data) { PG_RETURN_NULL(); } result_data[0] = Float8GetDatum(rotation3d->r11); result_data[1] = Float8GetDatum(rotation3d->r12); result_data[2] = Float8GetDatum(rotation3d->r13); result_data[3] = Float8GetDatum(rotation3d->r21); result_data[4] = Float8GetDatum(rotation3d->r22); result_data[5] = Float8GetDatum(rotation3d->r23); result_data[6] = Float8GetDatum(rotation3d->r31); result_data[7] = Float8GetDatum(rotation3d->r32); result_data[8] = Float8GetDatum(rotation3d->r33); result = construct_array(result_data, 9, FLOAT8OID, sizeof(float8), true, 'd'); pfree(result_data); PG_RETURN_ARRAYTYPE_P(result); }
/* * covariance - mathmatical compatible result can be lead using * nrows, psum(X), psum(X*X), psum(Y), psum(Y*Y), psum(X*Y) */ Datum pgstrom_covariance_float8_accum(PG_FUNCTION_ARGS) { ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0); int32 nrows = PG_GETARG_INT32(1); float8 psumX = PG_GETARG_FLOAT8(2); float8 psumX2 = PG_GETARG_FLOAT8(3); float8 psumY = PG_GETARG_FLOAT8(4); float8 psumY2 = PG_GETARG_FLOAT8(5); float8 psumXY = PG_GETARG_FLOAT8(6); float8 *transvalues; float8 newN; float8 newSumX; float8 newSumX2; float8 newSumY; float8 newSumY2; float8 newSumXY; transvalues = check_float8_array(transarray, 6); newN = transvalues[0] + (float8) nrows; newSumX = transvalues[1] + psumX; check_float8_valid(newSumX, isinf(transvalues[1]) || isinf(psumX), true); newSumX2 = transvalues[2] + psumX2; check_float8_valid(newSumX2, isinf(transvalues[2]) || isinf(psumX2), true); newSumY = transvalues[3] + psumY; check_float8_valid(newSumY, isinf(transvalues[3]) || isinf(psumY), true); newSumY2 = transvalues[4] + psumY2; check_float8_valid(newSumY2, isinf(transvalues[4]) || isinf(psumY2), true); newSumXY = transvalues[5] + psumXY; check_float8_valid(newSumXY, isinf(transvalues[5]) || isinf(psumXY), true); if (AggCheckCallContext(fcinfo, NULL)) { transvalues[0] = newN; transvalues[1] = newSumX; transvalues[2] = newSumX2; transvalues[3] = newSumY; transvalues[4] = newSumY2; transvalues[5] = newSumXY; PG_RETURN_ARRAYTYPE_P(transarray); } else { Datum transdatums[6]; ArrayType *result; transdatums[0] = Float8GetDatumFast(newN); transdatums[1] = Float8GetDatumFast(newSumX); transdatums[2] = Float8GetDatumFast(newSumX2); transdatums[3] = Float8GetDatumFast(newSumY); transdatums[4] = Float8GetDatumFast(newSumY2); transdatums[5] = Float8GetDatumFast(newSumXY); result = construct_array(transdatums, 6, FLOAT8OID, sizeof(float8), FLOAT8PASSBYVAL, 'd'); PG_RETURN_ARRAYTYPE_P(result); } }
/* * actually does the work for array(), and array_accum() if it is given a null * input array. * * numelems and elem_start allow the function to be shared given the differing * arguments accepted by array() and array_accum(). With array(), all function * arguments are used for array construction -- therefore elem_start is 0 and * numelems is the number of function arguments. With array_accum(), we are * always initializing the array with a single element given to us as argument * number 1 (i.e. the second argument). * */ static ArrayType * plr_array_create(FunctionCallInfo fcinfo, int numelems, int elem_start) { Oid funcid = fcinfo->flinfo->fn_oid; Datum *dvalues = (Datum *) palloc(numelems * sizeof(Datum)); int16 typlen; bool typbyval; Oid typinput; Oid element_type; char typalign; int i; HeapTuple tp; Oid functypeid; Oid *funcargtypes; ArrayType *result; if (numelems == 0) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("at least one value required to construct an array"))); /* * Get the type metadata for the array return type and its elements */ tp = SearchSysCache(PROCOID, ObjectIdGetDatum(funcid), 0, 0, 0); if (!HeapTupleIsValid(tp)) /* internal error */ elog(ERROR, "function OID %u does not exist", funcid); functypeid = ((Form_pg_proc) GETSTRUCT(tp))->prorettype; getTypeInputInfo(functypeid, &typinput, &element_type); get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign); funcargtypes = FUNCARGTYPES(tp); /* * the first function argument(s) may not be one of our array elements, * but the caller is responsible to ensure we get nothing but array * elements once they start coming */ for (i = elem_start; i < elem_start + numelems; i++) if (funcargtypes[i] != element_type) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("argument %d datatype not " \ "compatible with return data type", i + 1))); ReleaseSysCache(tp); for (i = 0; i < numelems; i++) dvalues[i] = PG_GETARG_DATUM(elem_start + i); result = construct_array(dvalues, numelems, element_type, typlen, typbyval, typalign); return result; }
/* * The final function for aggregating the class counts for REP. It takes the class * count array produced by the sfunc and produces a two-element array. The first * element is the ID of the class that has the maximum number of cases represented by * the root node of the subtree being processed. The second element is the number of * reduced misclassified cases if the leave nodes of the subtree are pruned. * * Parameters: * class_count_data: The array containing all the information for the * calculation of Reduced-Error pruning. * Return: * A two element array. */ Datum rep_aggr_class_count_ffunc(PG_FUNCTION_ARGS) { ArrayType *class_count_array = PG_GETARG_ARRAYTYPE_P(0); int array_dim = ARR_NDIM(class_count_array); check_error_value ( array_dim == 1, "invalid array dimension: %d. The dimension of class count array must be equal to 1", array_dim ); int *p_array_dim = ARR_DIMS(class_count_array); int array_length = ArrayGetNItems(array_dim,p_array_dim); int64 *class_count_data = (int64 *)ARR_DATA_PTR(class_count_array); int64 *result = palloc(sizeof(int64)*2); check_error ( result, "memory allocation failure" ); int64 max = class_count_data[1]; int64 sum = max; int maxid = 1; for(int i = 2; i < array_length; ++i) { if(max < class_count_data[i]) { max = class_count_data[i]; maxid = i; } sum += class_count_data[i]; } /* maxid is the id of the class, which has the most cases */ result[0] = maxid; /* * (sum - max) is the number of mis-classified cases represented by * the root node of the subtree being processed * class_count_data[0] the total number of mis-classified cases */ result[1] = class_count_data[0] - (sum - max); ArrayType* result_array = construct_array( (Datum *)result, 2, INT8OID, sizeof(int64), true, 'd' ); PG_RETURN_ARRAYTYPE_P(result_array); }
/* * Sets the policy of a table into the gp_distribution_policy table * from a GpPolicy structure. */ void GpPolicyStore(Oid tbloid, const GpPolicy *policy) { Relation gp_policy_rel; HeapTuple gp_policy_tuple = NULL; ArrayType *attrnums; bool nulls[2]; Datum values[2]; Insist(policy->ptype == POLICYTYPE_PARTITIONED); /* * Open and lock the gp_distribution_policy catalog. */ gp_policy_rel = heap_open(GpPolicyRelationId, RowExclusiveLock); /* * Convert C arrays into Postgres arrays. */ if (policy->nattrs > 0) { int i; Datum *akey; akey = (Datum *) palloc(policy->nattrs * sizeof(Datum)); for (i = 0; i < policy->nattrs; i++) akey[i] = Int16GetDatum(policy->attrs[i]); attrnums = construct_array(akey, policy->nattrs, INT2OID, 2, true, 's'); } else { attrnums = NULL; } nulls[0] = false; nulls[1] = false; values[0] = ObjectIdGetDatum(tbloid); if (attrnums) values[1] = PointerGetDatum(attrnums); else nulls[1] = true; gp_policy_tuple = heap_form_tuple(RelationGetDescr(gp_policy_rel), values, nulls); /* Insert tuple into the relation */ simple_heap_insert(gp_policy_rel, gp_policy_tuple); CatalogUpdateIndexes(gp_policy_rel, gp_policy_tuple); /* * Close the gp_distribution_policy relcache entry without unlocking. * We have updated the catalog: consequently the lock must be held until * end of transaction. */ heap_close(gp_policy_rel, NoLock); } /* GpPolicyStore */
Datum spherepoint_xyz(PG_FUNCTION_ARGS) { SPoint * p = ( SPoint * ) PG_GETARG_POINTER ( 0 ) ; Datum dret[3]; ArrayType *result; static Vector3D v ; spoint_vector3d ( &v , p ); dret[0] = Float8GetDatumFast(v.x); dret[1] = Float8GetDatumFast(v.y); dret[2] = Float8GetDatumFast(v.z); #ifdef FLOAT8PASSBYVAL result = construct_array ( dret , 3, FLOAT8OID, sizeof(float8), FLOAT8PASSBYVAL, 'd'); #else result = construct_array ( dret , 3, FLOAT8OID, sizeof(float8), false /* float8 byval */ , 'd' ); #endif PG_RETURN_ARRAYTYPE_P(result); }
/** * Given the oid of a relation, this method calculates reltuples, relpages. This only looks up * local information (on master or segments). It produces meaningful values for AO and * heap tables and returns [0.0,0.0] for all other relations. * Input: * relationoid * Output: * array of two values [reltuples,relpages] */ Datum gp_statistics_estimate_reltuples_relpages_oid(PG_FUNCTION_ARGS) { float4 relpages = 0.0; float4 reltuples = 0.0; Oid relOid = PG_GETARG_OID(0); Datum values[2]; ArrayType *result; Relation rel = try_relation_open(relOid, AccessShareLock, false); if (rel != NULL) { if (rel->rd_rel->relkind == RELKIND_RELATION) { if (RelationIsHeap(rel)) { gp_statistics_estimate_reltuples_relpages_heap(rel, &reltuples, &relpages); } else if (RelationIsAoRows(rel)) { gp_statistics_estimate_reltuples_relpages_ao_rows(rel, &reltuples, &relpages); } else if (RelationIsAoCols(rel)) { gp_statistics_estimate_reltuples_relpages_ao_cs(rel, &reltuples, &relpages); } } else if (rel->rd_rel->relkind == RELKIND_INDEX) { reltuples = 1.0; relpages = RelationGetNumberOfBlocks(rel); } else { /** * Should we silently return [0.0,0.0] or error out? Currently, we choose option 1. */ } relation_close(rel, AccessShareLock); } else { /** * Should we silently return [0.0,0.0] or error out? Currently, we choose option 1. */ } values[0] = Float4GetDatum(reltuples); values[1] = Float4GetDatum(relpages); result = construct_array(values, 2, FLOAT4OID, sizeof(float4), true, 'i'); PG_RETURN_ARRAYTYPE_P(result); }
Sen_board *construct_board(int len) { Sen_board *ret = malloc(sizeof(Sen_board)); ret->len = len; ret->bound = false; ret->data = construct_array(len); ret->print_sep = ' '; printf("hi\n"); return ret; }
Datum xmlnode_children(PG_FUNCTION_ARGS) { xmlnode nodeRaw = (xmlnode) PG_GETARG_VARLENA_P(0); char *data = (char *) VARDATA(nodeRaw); XMLNodeOffset rootNdOff = XNODE_ROOT_OFFSET(nodeRaw); XMLNodeHdr node = (XMLNodeHdr) (data + rootNdOff); TypeInfo nodeType; ArrayType *result; /* * Unfortunately the type info has to be retrieved every time again. If * the backend used global variable to remember the values, all backends * would have to invalidate the values whenever the extension gets dropped * / created. */ Assert(fcinfo->flinfo != NULL); initXNodeTypeInfo(fcinfo->flinfo->fn_oid, 0, &nodeType); if (node->kind == XMLNODE_DOC || node->kind == XMLNODE_ELEMENT || node->kind == XMLNODE_DOC_FRAGMENT) { XMLCompNodeHdr root = (XMLCompNodeHdr) node; unsigned short children = root->children; Datum *elems; char *childOffPtr; unsigned short i; if (children == 0) { result = construct_empty_array(nodeType.oid); PG_RETURN_POINTER(result); } elems = (Datum *) palloc(children * sizeof(Datum)); childOffPtr = XNODE_FIRST_REF(root); for (i = 0; i < children; i++) { XMLNodeOffset childOff = readXMLNodeOffset(&childOffPtr, XNODE_GET_REF_BWIDTH(root), true); XMLNodeHdr childNode = (XMLNodeHdr) (data + rootNdOff - childOff); char *childNodeCopy = copyXMLNode(childNode, NULL, true, NULL); elems[i] = PointerGetDatum(childNodeCopy); } result = construct_array(elems, children, nodeType.oid, nodeType.elmlen, nodeType.elmbyval, nodeType.elmalign); PG_RETURN_POINTER(result); } else { result = construct_empty_array(nodeType.oid); PG_RETURN_POINTER(result); } }
char * svec_out_internal(SvecType *svec) { char *ix_string,*vals_string,*result; int ixlen,vslen; SparseData sdata=sdata_from_svec(svec); int64 *array_ix =sdata_index_to_int64arr(sdata); ArrayType *pgarray_ix,*pgarray_vals; pgarray_ix = construct_array((Datum *)array_ix, sdata->unique_value_count,INT8OID, sizeof(int64),true,'d'); ix_string = DatumGetPointer(OidFunctionCall1(F_ARRAY_OUT, PointerGetDatum(pgarray_ix))); ixlen = strlen(ix_string); pgarray_vals = construct_array((Datum *)sdata->vals->data, sdata->unique_value_count,FLOAT8OID, sizeof(float8),true,'d'); vals_string = DatumGetPointer(OidFunctionCall1(F_ARRAY_OUT, PointerGetDatum(pgarray_vals))); vslen = strlen(vals_string); result = (char *)palloc(sizeof(char)*(vslen+ixlen+1+1)); /* NULLs are represented as NaN internally; see svec_in(); * Here we print each NaN as an NVP. */ for (int i=0; i!=vslen; i++) if (vals_string[i] == 'N') { vals_string[i+1] = 'V'; vals_string[i+2] = 'P'; i = i+2; } sprintf(result,"%s:%s",ix_string,vals_string); pfree(ix_string); pfree(vals_string); pfree(array_ix); return(result); }
Datum cword_count(PG_FUNCTION_ARGS) { ArrayType * count_arr, * doc_arr, * topics_arr; int32 * count, * doc, * topics; int32 doclen, num_topics, dsize, i; Datum * array; int32 idx; if (!(fcinfo->context && IsA(fcinfo->context, AggState))) elog(ERROR, "cword_count not used as part of an aggregate"); doclen = PG_GETARG_INT32(3); num_topics = PG_GETARG_INT32(4); dsize = PG_GETARG_INT32(5); /* Construct a zero'd array at the first call of this function */ if (PG_ARGISNULL(0)) { array = palloc0(dsize*num_topics*sizeof(Datum)); count_arr = construct_array(array,dsize*num_topics,INT4OID,4,true,'i'); } else { count_arr = PG_GETARG_ARRAYTYPE_P(0); } doc_arr = PG_GETARG_ARRAYTYPE_P(1); topics_arr = PG_GETARG_ARRAYTYPE_P(2); /* Check that the input arrays are of the right dimension and type */ if (ARR_NDIM(count_arr) != 1 || ARR_ELEMTYPE(count_arr) != INT4OID || ARR_NDIM(doc_arr) != 1 || ARR_ELEMTYPE(doc_arr) != INT4OID || ARR_NDIM(topics_arr) != 1 || ARR_ELEMTYPE(topics_arr) != INT4OID) ereport (ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("transition function \"%s\" called with invalid parameters", format_procedure(fcinfo->flinfo->fn_oid)))); count = (int32 *)ARR_DATA_PTR(count_arr); doc = (int32 *)ARR_DATA_PTR(doc_arr); topics = (int32 *)ARR_DATA_PTR(topics_arr); /* Update the word-topic count */ for (i=0; i!=doclen; i++) { idx = (doc[i]-1) * num_topics + (topics[i]-1); if (idx < 0 || idx >= dsize*num_topics) ereport (ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("function \"%s\" called with invalid parameters", format_procedure(fcinfo->flinfo->fn_oid)))); count[idx]++; } PG_RETURN_BYTEA_P(count_arr); }
/* * Return permissive policies to be added */ List * test_rls_hooks_permissive(CmdType cmdtype, Relation relation) { List *policies = NIL; RowSecurityPolicy *policy = palloc0(sizeof(RowSecurityPolicy)); Datum role; FuncCall *n; Node *e; ColumnRef *c; ParseState *qual_pstate; RangeTblEntry *rte; if (strcmp(RelationGetRelationName(relation), "rls_test_permissive") && strcmp(RelationGetRelationName(relation), "rls_test_both")) return NIL; qual_pstate = make_parsestate(NULL); rte = addRangeTableEntryForRelation(qual_pstate, relation, NULL, false, false); addRTEtoQuery(qual_pstate, rte, false, true, true); role = ObjectIdGetDatum(ACL_ID_PUBLIC); policy->policy_name = pstrdup("extension policy"); policy->policy_id = InvalidOid; policy->polcmd = '*'; policy->roles = construct_array(&role, 1, OIDOID, sizeof(Oid), true, 'i'); /* * policy->qual = (Expr *) makeConst(BOOLOID, -1, InvalidOid, * sizeof(bool), BoolGetDatum(true), false, true); */ n = makeFuncCall(list_make2(makeString("pg_catalog"), makeString("current_user")), NIL, 0); c = makeNode(ColumnRef); c->fields = list_make1(makeString("username")); c->location = 0; e = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", (Node *) n, (Node *) c, 0); policy->qual = (Expr *) transformWhereClause(qual_pstate, copyObject(e), EXPR_KIND_POLICY, "POLICY"); policy->with_check_qual = copyObject(policy->qual); policy->hassublinks = false; policies = list_make1(policy); return policies; }
ArrayType * create_guc_array(List *guc_list, int elems) { ArrayType *array; Datum *darray; darray = create_guc_datum_array(guc_list, elems); array = construct_array(darray, elems, TEXTOID, TEXT_TYPLEN, TEXT_TYPBYVAL, TEXT_TYPALIGN); pfree(darray); return array; }
Datum pgstrom_partial_avg_int8(PG_FUNCTION_ARGS) { ArrayType *result; Datum items[2]; items[0] = PG_GETARG_DATUM(0); /* nrows(int8) */ items[1] = PG_GETARG_DATUM(1); /* p_sum(int8) */ result = construct_array(items, 2, INT8OID, sizeof(int64), FLOAT8PASSBYVAL, 'd'); PG_RETURN_ARRAYTYPE_P(result); }
/** * @return An array of float8s obtained by converting a given sparse vector */ ArrayType *svec_return_array_internal(SvecType *svec) { SparseData sdata = sdata_from_svec(svec); double *array = sdata_to_float8arr(sdata); ArrayType *pgarray = construct_array((Datum *)array, sdata->total_value_count,FLOAT8OID, sizeof(float8),true,'d'); pfree(array); return(pgarray); }
Datum compute_InfoGain(PG_FUNCTION_ARGS) { ArrayType *state = PG_GETARG_ARRAYTYPE_P(0); float8 *vals_state=(float8 *)ARR_DATA_PTR(state); int32 posclasses = PG_GETARG_INT32(1); int32 posvalues = PG_GETARG_INT32(2); float8 *result = palloc(sizeof(float8)*4); int i = 1; int max = 1; float8 tot = entropyWeightedFloat(vals_state, 1, posclasses, vals_state[0], vals_state[0]); ArrayType *pgarray; /*ereport(NOTICE, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg(">>>>>>>R1: %f %f %f %f %f %f %f %f %f", vals_state[0], vals_state[1], vals_state[2], vals_state[3], vals_state[4], vals_state[5], vals_state[6], vals_state[7], vals_state[8])));*/ for(; i < (posvalues+1); ++i){ tot -= entropyWeightedFloat(vals_state, (i*(posclasses+1)+1), posclasses, vals_state[i*(posclasses+1)], vals_state[0]); } result[0] = tot; i = 1; tot = 0; for(; i < (posvalues+1); ++i){ tot += ChiSquareStatistic(vals_state, (i*(posclasses+1)+1), posclasses, vals_state[i*(posclasses+1)], vals_state[0]); } result[1] = tot; i = 1; for(; i < (posclasses+1); ++i){ if(vals_state[max] < vals_state[i]){ max = i; } } if(vals_state[0] == 0){ result[2] = 0; }else{ result[2] = vals_state[max]/vals_state[0]; } result[3] = max; /*ereport(NOTICE, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg(">>>>>>>R2: %f %f %f %f %f %f %f %f", result[0], result[1], result[2] ,result[3], vals_state[0], vals_state[1], vals_state[2], vals_state[3])));*/ pgarray = construct_array((Datum *)result, 4,FLOAT8OID, sizeof(float8),true,'d'); PG_RETURN_ARRAYTYPE_P(pgarray); }
Datum pgstrom_partial_avg_float8(PG_FUNCTION_ARGS) { int64 nrows = PG_GETARG_INT64(0); ArrayType *result; Datum items[2]; items[0] = Float8GetDatum((float8)nrows); items[1] = PG_GETARG_DATUM(1); /* p_sum(float8) */ result = construct_array(items, 2, FLOAT8OID, sizeof(float8), FLOAT8PASSBYVAL, 'd'); PG_RETURN_ARRAYTYPE_P(result); }