本文整理汇总了C++中PG_GETARG_ARRAYTYPE_P函数的典型用法代码示例。如果您正苦于以下问题:C++ PG_GETARG_ARRAYTYPE_P函数的具体用法?C++ PG_GETARG_ARRAYTYPE_P怎么用?C++ PG_GETARG_ARRAYTYPE_P使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PG_GETARG_ARRAYTYPE_P函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: array_positions
/*-----------------------------------------------------------------------------
* array_positions :
* return an array of positions of a value in an array.
*
* IS NOT DISTINCT FROM semantics are used for comparisons. Returns NULL when
* the input array is NULL. When the value is not found in the array, returns
* an empty array.
*
* This is not strict so we have to test for null inputs explicitly.
*-----------------------------------------------------------------------------
*/
Datum
array_positions(PG_FUNCTION_ARGS)
{
ArrayType *array;
Oid collation = PG_GET_COLLATION();
Oid element_type;
Datum searched_element,
value;
bool isnull;
int position;
TypeCacheEntry *typentry;
ArrayMetaState *my_extra;
bool null_search;
ArrayIterator array_iterator;
ArrayBuildState *astate = NULL;
if (PG_ARGISNULL(0))
PG_RETURN_NULL();
array = PG_GETARG_ARRAYTYPE_P(0);
element_type = ARR_ELEMTYPE(array);
position = (ARR_LBOUND(array))[0] - 1;
/*
* We refuse to search for elements in multi-dimensional arrays, since we
* have no good way to report the element's location in the array.
*/
if (ARR_NDIM(array) > 1)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("searching for elements in multidimensional arrays is not supported")));
astate = initArrayResult(INT4OID, CurrentMemoryContext, false);
if (PG_ARGISNULL(1))
{
/* fast return when the array doesn't have nulls */
if (!array_contains_nulls(array))
PG_RETURN_DATUM(makeArrayResult(astate, CurrentMemoryContext));
searched_element = (Datum) 0;
null_search = true;
}
else
{
searched_element = PG_GETARG_DATUM(1);
null_search = false;
}
/*
* We arrange to look up type info for array_create_iterator only once per
* series of calls, assuming the element type doesn't change underneath
* us.
*/
my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
if (my_extra == NULL)
{
fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
sizeof(ArrayMetaState));
my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
my_extra->element_type = ~element_type;
}
if (my_extra->element_type != element_type)
{
get_typlenbyvalalign(element_type,
&my_extra->typlen,
&my_extra->typbyval,
&my_extra->typalign);
typentry = lookup_type_cache(element_type, TYPECACHE_EQ_OPR_FINFO);
if (!OidIsValid(typentry->eq_opr_finfo.fn_oid))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_FUNCTION),
errmsg("could not identify an equality operator for type %s",
format_type_be(element_type))));
my_extra->element_type = element_type;
fmgr_info_cxt(typentry->eq_opr_finfo.fn_oid, &my_extra->proc,
fcinfo->flinfo->fn_mcxt);
}
/*
* Accumulate each array position iff the element matches the given
* element.
*/
array_iterator = array_create_iterator(array, 0, my_extra);
while (array_iterate(array_iterator, &value, &isnull))
//.........这里部分代码省略.........
示例2: array_push
/*-----------------------------------------------------------------------------
* array_push :
* push an element onto either end of a one-dimensional array
*----------------------------------------------------------------------------
*/
Datum
array_push(PG_FUNCTION_ARGS)
{
ArrayType *v;
Datum newelem;
bool isNull;
int *dimv,
*lb;
ArrayType *result;
int indx;
Oid element_type;
int16 typlen;
bool typbyval;
char typalign;
Oid arg0_typeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
Oid arg1_typeid = get_fn_expr_argtype(fcinfo->flinfo, 1);
Oid arg0_elemid;
Oid arg1_elemid;
ArrayMetaState *my_extra;
if (arg0_typeid == InvalidOid || arg1_typeid == InvalidOid)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("could not determine input data types")));
arg0_elemid = get_element_type(arg0_typeid);
arg1_elemid = get_element_type(arg1_typeid);
if (arg0_elemid != InvalidOid)
{
if (PG_ARGISNULL(0))
v = construct_empty_array(arg0_elemid);
else
v = PG_GETARG_ARRAYTYPE_P(0);
isNull = PG_ARGISNULL(1);
if (isNull)
newelem = (Datum) 0;
else
newelem = PG_GETARG_DATUM(1);
}
else if (arg1_elemid != InvalidOid)
{
if (PG_ARGISNULL(1))
v = construct_empty_array(arg1_elemid);
else
v = PG_GETARG_ARRAYTYPE_P(1);
isNull = PG_ARGISNULL(0);
if (isNull)
newelem = (Datum) 0;
else
newelem = PG_GETARG_DATUM(0);
}
else
{
/* Shouldn't get here given proper type checking in parser */
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
errmsg("neither input type is an array")));
PG_RETURN_NULL(); /* keep compiler quiet */
}
element_type = ARR_ELEMTYPE(v);
if (ARR_NDIM(v) == 1)
{
lb = ARR_LBOUND(v);
dimv = ARR_DIMS(v);
if (arg0_elemid != InvalidOid)
{
/* append newelem */
int ub = dimv[0] + lb[0] - 1;
indx = ub + 1;
/* overflow? */
if (indx < ub)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("integer out of range")));
}
else
{
/* prepend newelem */
indx = lb[0] - 1;
/* overflow? */
if (indx > lb[0])
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("integer out of range")));
}
}
else if (ARR_NDIM(v) == 0)
indx = 1;
else
ereport(ERROR,
//.........这里部分代码省略.........
示例3: alpine_miner_lr_ca_he_de_accum
Datum
alpine_miner_lr_ca_he_de_accum(PG_FUNCTION_ARGS)
{
ArrayType *beta_arg, *columns_arg, *result;
float8 *beta_data, *columns_data, *result_data;
int beta_count, columns_count, result_count;
int size;
bool add_intercept_arg;
double weight_arg;
int y_arg;
double fitness = 0.0;
double gx = 0.0;
double pi = 0.0;
if (PG_ARGISNULL(0) || PG_ARGISNULL(1) || PG_ARGISNULL(2) || PG_ARGISNULL(3) || PG_ARGISNULL(4) || PG_ARGISNULL(5)){
PG_RETURN_NULL();
}
result = PG_GETARG_ARRAYTYPE_P(0);
beta_arg = PG_GETARG_ARRAYTYPE_P(1);
columns_arg = PG_GETARG_ARRAYTYPE_P(2);
add_intercept_arg = PG_GETARG_BOOL(3);
weight_arg = PG_GETARG_FLOAT8(4);
y_arg = PG_GETARG_INT32(5);
result_data = (float8*) ARR_DATA_PTR(result);
beta_data = (float8*) ARR_DATA_PTR(beta_arg);
columns_data = (float8*) ARR_DATA_PTR(columns_arg);
result_count = ARR_DIMS(result)[0];
beta_count = ARR_DIMS(beta_arg)[0];
columns_count = ARR_DIMS(columns_arg)[0];
// float8 * column_array_data = (float8*) ARR_DATA_PTR(column_array);
if (result_count == 1){
result_count = beta_count *(beta_count+1)/2 + beta_count + 1;
size = result_count * sizeof(float8) + ARR_OVERHEAD_NONULLS(1);
result = (ArrayType *) palloc(size);
SET_VARSIZE(result, size);
result->ndim = 1;
result->dataoffset = 0;
result->elemtype = FLOAT8OID;
ARR_DIMS(result)[0] = result_count;
ARR_LBOUND(result)[0] = 1;
result_data = (float8*) ARR_DATA_PTR(result);
memset(result_data, 0, result_count * sizeof(float8));
}
pi = alpine_miner_compute_pi(beta_data, beta_count, columns_data, columns_count, add_intercept_arg);
alpine_miner_compute_hessian(beta_count,beta_data,columns_data, result_data
,weight_arg, add_intercept_arg, pi);
alpine_miner_compute_derivative(columns_count,columns_data, &result_data[beta_count *(beta_count+1)/2]
,weight_arg, y_arg, add_intercept_arg, pi);
if (y_arg == 1)
{
fitness = log(pi);
}
else
{
fitness = log(1.0 - pi);
}
fitness *= weight_arg;
result_data[result_count - 1] = fitness + result_data[result_count - 1];
PG_RETURN_ARRAYTYPE_P(result);
}
示例4: sampleNewTopics
Datum sampleNewTopics(PG_FUNCTION_ARGS)
{
int32 i, widx, wtopic, rtopic;
ArrayType * doc_arr = PG_GETARG_ARRAYTYPE_P(0);
ArrayType * topics_arr = PG_GETARG_ARRAYTYPE_P(1);
ArrayType * topic_d_arr = PG_GETARG_ARRAYTYPE_P(2);
ArrayType * global_count_arr = PG_GETARG_ARRAYTYPE_P(3);
ArrayType * topic_counts_arr = PG_GETARG_ARRAYTYPE_P(4);
int32 num_topics = PG_GETARG_INT32(5);
int32 dsize = PG_GETARG_INT32(6);
float8 alpha = PG_GETARG_FLOAT8(7);
float8 eta = PG_GETARG_FLOAT8(8);
if (ARR_NULLBITMAP(doc_arr) || ARR_NDIM(doc_arr) != 1 ||
ARR_ELEMTYPE(doc_arr) != INT4OID ||
ARR_NDIM(topics_arr) != 1 || ARR_ELEMTYPE(topics_arr) != INT4OID ||
ARR_NULLBITMAP(topic_d_arr) || ARR_NDIM(topic_d_arr) != 1 ||
ARR_ELEMTYPE(topic_d_arr) != INT4OID ||
ARR_NULLBITMAP(global_count_arr) || ARR_NDIM(global_count_arr) != 1
|| ARR_ELEMTYPE(global_count_arr) != INT4OID ||
ARR_NULLBITMAP(topic_counts_arr) || ARR_NDIM(topic_counts_arr) != 1
|| ARR_ELEMTYPE(topic_counts_arr) != INT4OID)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("function \"%s\" called with invalid parameters",
format_procedure(fcinfo->flinfo->fn_oid))));
// the document array
int32 * doc = (int32 *)ARR_DATA_PTR(doc_arr);
int32 len = ARR_DIMS(doc_arr)[0];
// array giving topic assignment to each word in document
int32 * topics = (int32 *)ARR_DATA_PTR(topics_arr);
// distribution of topics in document
int32 * topic_d = (int32 *)ARR_DATA_PTR(topic_d_arr);
// the word-topic count matrix
int32 * global_count = (int32 *)ARR_DATA_PTR(global_count_arr);
// total number of words assigned to each topic in the whole corpus
int32 * topic_counts = (int32 *)ARR_DATA_PTR(topic_counts_arr);
ArrayType * ret_topics_arr, * ret_topic_d_arr;
int32 * ret_topics, * ret_topic_d;
Datum * arr1 = palloc0(len * sizeof(Datum));
ret_topics_arr = construct_array(arr1,len,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);
for (i=0; i!=len; i++) {
widx = doc[i];
if (widx < 1 || widx > dsize)
ereport
(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("function \"%s\" called with invalid parameters",
format_procedure(fcinfo->flinfo->fn_oid))));
wtopic = topics[i];
rtopic = sampleTopic(num_topics,widx,wtopic,global_count,
topic_d,topic_counts,alpha,eta);
// <sampleNewTopics error checking>
ret_topics[i] = rtopic;
ret_topic_d[rtopic-1]++;
}
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));
}
示例5: generate_sparse_vector
Datum generate_sparse_vector(PG_FUNCTION_ARGS)
{
SvecType *output_sfv;
int16_t typlen;
bool typbyval;
char typalign;
bool *nulls;
if (PG_NARGS() != 3)
elog(ERROR, "Invalid number of arguments.");
ArrayType *term_index = PG_GETARG_ARRAYTYPE_P(0);
ArrayType *term_count = PG_GETARG_ARRAYTYPE_P(1);
int64_t dict_size = PG_GETARG_INT64(2);
/* Check if arrays have null entries */
if (ARR_HASNULL(term_index) || ARR_HASNULL(term_count))
elog(ERROR, "One or both of the argument arrays has one or more null entries.");
if (dict_size <= 0)
elog(ERROR, "Dictionary size cannot be zero or negative.");
/* Check if any of the argument arrays is empty */
if ((ARR_NDIM(term_index) == 0) || (ARR_NDIM(term_count) == 0))
elog(ERROR, "One or more argument arrays is empty.");
int term_index_nelems = ARR_DIMS(term_index)[0];
int term_count_nelems = ARR_DIMS(term_count)[0];
/* If no. of elements in the arrays are not equal, throw an error */
if (term_index_nelems != term_count_nelems)
elog(ERROR, "No. of elements in the argument arrays are not equal.");
Datum *term_index_data;
Datum *term_count_data;
/* Deconstruct the arrays */
get_typlenbyvalalign(INT8OID, &typlen, &typbyval, &typalign);
deconstruct_array(term_index, INT8OID, typlen, typbyval, typalign,
&term_index_data, &nulls, &term_index_nelems);
get_typlenbyvalalign(FLOAT8OID, &typlen, &typbyval, &typalign);
deconstruct_array(term_count, FLOAT8OID, typlen, typbyval, typalign,
&term_count_data, &nulls, &term_count_nelems);
/* Check if term index array has indexes in proper order or not */
for(int i = 0; i < term_index_nelems; i++)
{
if (DatumGetInt64(term_index_data[i]) < 0 ||
DatumGetInt64(term_index_data[i]) >= dict_size)
elog(ERROR, "Term indexes must range from 0 to total number of elements in the dictonary - 1.");
}
float8 *histogram = (float8 *)palloc0(sizeof(float8) * dict_size);
for (int k = 0; k < dict_size; k++)
{
histogram[k] = 0;
}
for (int i = 0; i < term_index_nelems; i++)
{
uint64_t idx = DatumGetInt64(term_index_data[i]);
histogram[idx] += DatumGetFloat8(term_count_data[i]);
}
output_sfv = svec_from_float8arr(histogram, dict_size);
pfree(histogram);
PG_RETURN_POINTER(output_sfv);
}
示例6: oid_pivot_accum
/*
* pivot_accum() - Pivot and accumulate
*/
static Datum oid_pivot_accum(FunctionCallInfo fcinfo, Oid type)
{
ArrayType *data;
ArrayType *labels;
text *attr;
int i;
/* Simple argument validation */
if (PG_NARGS() != 4)
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
errmsg("pivot_accum called with %d input arguments",
PG_NARGS())));
if (PG_ARGISNULL(1) || PG_ARGISNULL(2) || PG_ARGISNULL(3))
{
if (PG_ARGISNULL(0))
PG_RETURN_NULL();
PG_RETURN_ARRAYTYPE_P(PG_GETARG_ARRAYTYPE_P(0));
}
labels = PG_GETARG_ARRAYTYPE_P(1);
attr = PG_GETARG_TEXT_P(2);
/* Do nothing if the attr isn't in the labels array. */
if ((i = pivot_find(labels, attr)) < 0)
{
if (PG_ARGISNULL(0))
PG_RETURN_NULL();
PG_RETURN_ARRAYTYPE_P(PG_GETARG_ARRAYTYPE_P(0));
}
/* Get the data array, or make it if null */
if (!PG_ARGISNULL(0))
{
data = PG_GETARG_ARRAYTYPE_P(0);
Assert(ARR_DIMS(labels)[0] == ARR_DIMS(data)[0]);
}
else
{
int elsize, size, nelem;
switch (type) {
case INT4OID:
elsize = 4;
break;
case INT8OID:
case FLOAT8OID:
elsize = 8;
break;
default:
elsize = 0; /* Fixes complier warnings */
Assert(false);
}
nelem = ARR_DIMS(labels)[0];
size = nelem * elsize + ARR_OVERHEAD_NONULLS(1);
data = (ArrayType *) palloc(size);
SET_VARSIZE(data, size);
data->ndim = 1;
data->dataoffset = 0;
data->elemtype = type;
ARR_DIMS(data)[0] = nelem;
ARR_LBOUND(data)[0] = 1;
memset(ARR_DATA_PTR(data), 0, nelem * elsize);
}
/*
* Should we think about upconverting the arrays? Or is the assumption that
* the pivot isn't usually doing much aggregation?
*/
switch (type) {
case INT4OID:
{
int32 *datap = (int32*) ARR_DATA_PTR(data);
int32 value = PG_GETARG_INT32(3);
datap[i] += value;
break;
}
case INT8OID:
{
int64 *datap = (int64*) ARR_DATA_PTR(data);
int64 value = PG_GETARG_INT64(3);
datap[i] += value;
break;
}
case FLOAT8OID:
{
float8 *datap = (float8*) ARR_DATA_PTR(data);
float8 value = PG_GETARG_FLOAT8(3);
datap[i] += value;
break;
}
default:
Assert(false);
}
PG_RETURN_ARRAYTYPE_P(data);
}
示例7: __vcrf_max_top1_array
Datum
__vcrf_max_top1_array(PG_FUNCTION_ARGS)
{
ArrayType *v1 ;
// ,
// *v2;
ArrayType *result;
// int *dims,
// *lbs,
int ndims,
// nitems,
ndatabytes,
nbytes;
int nitems1;
// int *dims1,
// *lbs1,
// ndims1,
// nitems1,
// ndatabytes1;
// int *dims2,
// *lbs2,
// ndims2,
// nitems2,
// ndatabytes2;
int i;
// char *dat1,
// *dat2;
// bits8 *bitmap1,
// *bitmap2;
Oid element_type;
// Oid element_type1;
// Oid element_type2;
int32 dataoffset;
if (PG_ARGISNULL(0))
abort();
v1 = PG_GETARG_ARRAYTYPE_P(0);
element_type = ARR_ELEMTYPE(v1);
nitems1 = ARR_DIMS(v1)[1];
if (nitems1 == 0)
abort();
/* new array is the same as v1 for top 1 only !! */
ndims = 2;
ndatabytes = 3*sizeof(int);
dataoffset = 0; /* marker for no null bitmap */
nbytes = ndatabytes + ARR_OVERHEAD_NONULLS(ndims);
result = (ArrayType *) palloc(nbytes);
SET_VARSIZE(result, nbytes);
result->ndim = ndims;
result->dataoffset = dataoffset;
result->elemtype = element_type;
ARR_DIMS(result)[0]=3; /* num elements in first dim */
ARR_DIMS(result)[1]=1; /* num elements in second dim */
/* postgres arrays can be index from any start position eg:
* int[-5:14]. this is unlike c, where index always starts at 0 */
ARR_LBOUND(result)[0]=1; /* index of the first dim starts at .. */
ARR_LBOUND(result)[1]=1; /* index of second dim starts at ... */
// do top1 calculation here
for(i = 0; i < nitems1; i++) {
if (i == 0 || ((int*)ARR_DATA_PTR(v1))[0*nitems1+i] >
((int*)ARR_DATA_PTR(v1))[0*nitems1+((int*)ARR_DATA_PTR(result))[0*1+0]]) {
((int*)ARR_DATA_PTR(result))[0*1+0] = i;
((int*)ARR_DATA_PTR(result))[1*1+0] = ((int*)ARR_DATA_PTR(v1))[1*nitems1+i];
((int*)ARR_DATA_PTR(result))[2*1+0] = ((int*)ARR_DATA_PTR(v1))[0*nitems1+i];
}
}
PG_RETURN_ARRAYTYPE_P(result);
}
示例8: max_flow_many_to_many
PGDLLEXPORT Datum
max_flow_many_to_many(PG_FUNCTION_ARGS) {
FuncCallContext *funcctx;
TupleDesc tuple_desc;
/**************************************************************************/
pgr_flow_t *result_tuples = 0;
size_t result_count = 0;
/**************************************************************************/
if (SRF_IS_FIRSTCALL()) {
MemoryContext oldcontext;
funcctx = SRF_FIRSTCALL_INIT();
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
/**********************************************************************/
process(
text_to_cstring(PG_GETARG_TEXT_P(0)),
PG_GETARG_ARRAYTYPE_P(1),
PG_GETARG_ARRAYTYPE_P(2),
text_to_cstring(PG_GETARG_TEXT_P(3)),
PG_GETARG_BOOL(4),
&result_tuples,
&result_count);
/* */
/**********************************************************************/
#if PGSQL_VERSION > 95
funcctx->max_calls = result_count;
#else
funcctx->max_calls = (uint32_t)result_count;
#endif
funcctx->user_fctx = result_tuples;
if (get_call_result_type(fcinfo, NULL, &tuple_desc)
!= TYPEFUNC_COMPOSITE) {
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("function returning record called in context "
"that cannot accept type record")));
}
funcctx->tuple_desc = tuple_desc;
MemoryContextSwitchTo(oldcontext);
}
funcctx = SRF_PERCALL_SETUP();
tuple_desc = funcctx->tuple_desc;
result_tuples = (pgr_flow_t *) funcctx->user_fctx;
if (funcctx->call_cntr < funcctx->max_calls) {
HeapTuple tuple;
Datum result;
Datum *values;
bool *nulls;
size_t call_cntr = funcctx->call_cntr;
/**********************************************************************/
/* MODIFY AS NEEDED */
values = palloc(6 * sizeof(Datum));
nulls = palloc(6 * sizeof(bool));
size_t i;
for (i = 0; i < 6; ++i) {
nulls[i] = false;
}
values[0] = Int32GetDatum(call_cntr + 1);
values[1] = Int64GetDatum(result_tuples[call_cntr].edge);
values[2] = Int64GetDatum(result_tuples[call_cntr].source);
values[3] = Int64GetDatum(result_tuples[call_cntr].target);
values[4] = Int64GetDatum(result_tuples[call_cntr].flow);
values[5] = Int64GetDatum(result_tuples[call_cntr].residual_capacity);
/**********************************************************************/
tuple = heap_form_tuple(tuple_desc, values, nulls);
result = HeapTupleGetDatum(tuple);
SRF_RETURN_NEXT(funcctx, result);
} else {
SRF_RETURN_DONE(funcctx);
}
}
示例9: array_multi_index
Datum array_multi_index( PG_FUNCTION_ARGS ) {
if( PG_ARGISNULL(0) ) {
PG_RETURN_NULL();
}
if( PG_ARGISNULL(1) ) {
PG_RETURN_NULL();
}
ArrayType* values = PG_GETARG_ARRAYTYPE_P( 0 );
ArrayType* indices = PG_GETARG_ARRAYTYPE_P( 1 );
Oid values_type = ARR_ELEMTYPE( values );
int16 values_width;
bool values_passbyvalue;
char values_alignmentcode;
Datum* values_content;
bool* values_nullflags;
int values_length;
get_typlenbyvalalign( values_type, &values_width, &values_passbyvalue, &values_alignmentcode );
deconstruct_array( values, values_type, values_width, values_passbyvalue, values_alignmentcode, &values_content, &values_nullflags, &values_length );
Oid indices_type = ARR_ELEMTYPE( indices );
int16 indices_width;
bool indices_passbyvalue;
char indices_alignmentcode;
Datum* indices_content;
bool* indices_nullflags;
int indices_length;
get_typlenbyvalalign( indices_type, &indices_width, &indices_passbyvalue, &indices_alignmentcode );
deconstruct_array( indices, indices_type, indices_width, indices_passbyvalue, indices_alignmentcode, &indices_content, &indices_nullflags, &indices_length );
Oid results_type = values_type;
int16 results_width = values_width;
bool results_passbyvalue = values_passbyvalue;
char results_alignmentcode = values_alignmentcode;
Datum* results_content = (Datum *)palloc( sizeof(Datum) * indices_length );
bool* results_nullflags = (bool *)palloc0( sizeof(bool) * indices_length );
int results_length = indices_length;
int i;
for( i = 0; i < indices_length; ++i ) {
if( indices_nullflags[i] ) {
results_content[i] = 0;
results_nullflags[i] = true;
} else if( indices_content[i] - 1 >= (long unsigned)values_length ) {
results_content[i] = 0;
results_nullflags[i] = true;
} else {
results_content[i] = values_content[ indices_content[i] - 1 ];
results_nullflags[i] = values_nullflags[ indices_content[i] - 1 ];
}
}
int dims[1];
int lbs[1];
dims[0] = results_length;
lbs[0] = 1;
ArrayType* results = construct_md_array( results_content, results_nullflags, 1, dims, lbs, results_type, results_width, results_passbyvalue, results_alignmentcode );
pfree( results_content );
pfree( results_nullflags );
PG_RETURN_ARRAYTYPE_P( results );
}
示例10: __vcrf_sum_array
Datum
__vcrf_sum_array(PG_FUNCTION_ARGS)
{
ArrayType *v1,
*v2;
ArrayType *result;
// int *dims,
// *lbs,
int ndims,
// nitems,
ndatabytes,
nbytes;
int nitems1, nitems2;
// int *dims1,
// *lbs1,
// ndims1,
// nitems1,
// ndatabytes1;
// int *dims2,
// *lbs2,
// ndims2,
// nitems2,
// ndatabytes2;
int i;
// char *dat1,
// *dat2;
// bits8 *bitmap1,
// *bitmap2;
Oid element_type;
// Oid element_type1;
// Oid element_type2;
int32 dataoffset;
int spos;
if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
abort();
// pointers to the postgres arrays
v1 = PG_GETARG_ARRAYTYPE_P(0);
v2 = PG_GETARG_ARRAYTYPE_P(1);
// postgres type of elements in an array
element_type = ARR_ELEMTYPE(v1);
// number of items in the arrays
nitems1 = ARR_DIMS(v1)[0];
nitems2 = ARR_DIMS(v2)[0];
// if (nitems1 == 0 || nitems2 == 0 || nitems2 % nitems1 != 0)
// abort();
/* new array is the same as v1 for top 1 only !! */
ndims = ARR_NDIM(v1);
ndatabytes = ARR_SIZE(v1) - ARR_DATA_OFFSET(v1);
dataoffset = 0; /* marker for no null bitmap */
nbytes = ndatabytes + ARR_OVERHEAD_NONULLS(ndims);
result = (ArrayType *) palloc(nbytes);
SET_VARSIZE(result, nbytes);
result->ndim = ndims;
result->dataoffset = dataoffset;
result->elemtype = element_type;
for(i=0;i<ndims;i++) {
ARR_DIMS(result)[i]=ARR_DIMS(v1)[i];
ARR_LBOUND(result)[i]=ARR_LBOUND(v1)[i];
}
// array v2 can either have nitems1 or nitems1*nitems1 or (nitems1+1)*nitems1 items, because of the -1 arrays from MR
// for the first and third case we want to skip the first nitems1 items
//int spos=0;
spos = nitems2 % (nitems1*nitems1);
for(i=0; i<nitems1; i++)
((int*)ARR_DATA_PTR(result))[i] = 0;
// do sum over all possible value of y' calculation here
for(i = spos; i < nitems2; i++) {
int k = (i-spos) / nitems1;
int k_rem = i % nitems1;
int new_score = ((int*)ARR_DATA_PTR(v1))[0*nitems1+k] + ((int*)ARR_DATA_PTR(v2))[i];
// 0.5 is for rounding
((int*)ARR_DATA_PTR(result))[0*nitems1+k_rem] = (int)(log(exp(((int*)ARR_DATA_PTR(result))[0*nitems1+k_rem]/1000.0) + exp(new_score/1000.0))*1000.0 + 0.5);
}
PG_RETURN_ARRAYTYPE_P(result);
}
示例11: tsp_matrix
Datum
tsp_matrix(PG_FUNCTION_ARGS)
{
FuncCallContext *funcctx;
int call_cntr;
int max_calls;
TupleDesc tuple_desc;
AttInMetadata *attinmeta;
DTYPE *matrix;
int *tsp_res;
int num;
/* stuff done only on the first call of the function */
if (SRF_IS_FIRSTCALL()) {
MemoryContext oldcontext;
//int path_count;
int ret=-1;
/* create a function context for cross-call persistence */
funcctx = SRF_FIRSTCALL_INIT();
/* switch to memory context appropriate for multiple function calls */
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
matrix = get_pgarray(&num, PG_GETARG_ARRAYTYPE_P(0));
ret = solve_tsp(matrix, num,
PG_GETARG_INT32(1), // start index
PG_GETARG_INT32(2), // end index
&tsp_res);
pfree(matrix);
if (ret < 0) {
elog(ERROR, "Error, failed to solve TSP.");
}
funcctx->max_calls = num;
funcctx->user_fctx = tsp_res;
/* Build a tuple descriptor for our result type */
if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("function returning record called in context "
"that cannot accept type record")));
funcctx->tuple_desc = BlessTupleDesc(tuple_desc);
/*
* generate attribute metadata needed later to produce tuples from raw
* C strings
*/
//attinmeta = TupleDescGetAttInMetadata(tuple_desc);
//funcctx->attinmeta = attinmeta;
MemoryContextSwitchTo(oldcontext);
}
/* stuff done on every call of the function */
funcctx = SRF_PERCALL_SETUP();
call_cntr = funcctx->call_cntr;
max_calls = funcctx->max_calls;
tuple_desc = funcctx->tuple_desc;
tsp_res = funcctx->user_fctx;
DBG("Trying to allocate some memory");
DBG("call_cntr = %i, max_calls = %i", call_cntr, max_calls);
if (call_cntr < max_calls) { /* do when there is more left to send */
HeapTuple tuple;
Datum result;
Datum *values;
char* nulls;
values = palloc(2 * sizeof(Datum));
nulls = palloc(2 * sizeof(char));
values[0] = Int32GetDatum(call_cntr);
nulls[0] = ' ';
values[1] = Int32GetDatum(tsp_res[call_cntr]);
nulls[1] = ' ';
DBG("RESULT: %d, %d", call_cntr, tsp_res[call_cntr]);
DBG("Heap making");
tuple = heap_formtuple(tuple_desc, values, nulls);
DBG("Datum making");
/* make the tuple into a datum */
result = HeapTupleGetDatum(tuple);
DBG("RESULT: seq:%d, id:%d", call_cntr, tsp_res[call_cntr]);
DBG("Trying to free some memory");
/* clean up (this is not really necessary) */
pfree(values);
//.........这里部分代码省略.........
示例12: jsonb_object_two_arg
/*
* SQL function jsonb_object(text[], text[])
*
* take separate name and value arrays of text to construct a jsonb object
* pairwise.
*/
Datum
jsonb_object_two_arg(PG_FUNCTION_ARGS)
{
ArrayType *key_array = PG_GETARG_ARRAYTYPE_P(0);
ArrayType *val_array = PG_GETARG_ARRAYTYPE_P(1);
int nkdims = ARR_NDIM(key_array);
int nvdims = ARR_NDIM(val_array);
Datum *key_datums,
*val_datums;
bool *key_nulls,
*val_nulls;
int key_count,
val_count,
i;
JsonbInState result;
memset(&result, 0, sizeof(JsonbInState));
(void) pushJsonbValue(&result.parseState, WJB_BEGIN_OBJECT, NULL);
if (nkdims > 1 || nkdims != nvdims)
ereport(ERROR,
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
errmsg("wrong number of array subscripts")));
if (nkdims == 0)
goto close_object;
deconstruct_array(key_array,
TEXTOID, -1, false, 'i',
&key_datums, &key_nulls, &key_count);
deconstruct_array(val_array,
TEXTOID, -1, false, 'i',
&val_datums, &val_nulls, &val_count);
if (key_count != val_count)
ereport(ERROR,
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
errmsg("mismatched array dimensions")));
for (i = 0; i < key_count; ++i)
{
JsonbValue v;
char *str;
int len;
if (key_nulls[i])
ereport(ERROR,
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
errmsg("null value not allowed for object key")));
str = TextDatumGetCString(key_datums[i]);
len = strlen(str);
v.type = jbvString;
v.val.string.len = len;
v.val.string.val = str;
(void) pushJsonbValue(&result.parseState, WJB_KEY, &v);
if (val_nulls[i])
{
v.type = jbvNull;
}
else
{
str = TextDatumGetCString(val_datums[i]);
len = strlen(str);
v.type = jbvString;
v.val.string.len = len;
v.val.string.val = str;
}
(void) pushJsonbValue(&result.parseState, WJB_VALUE, &v);
}
pfree(key_datums);
pfree(key_nulls);
pfree(val_datums);
pfree(val_nulls);
close_object:
result.res = pushJsonbValue(&result.parseState, WJB_END_OBJECT, NULL);
PG_RETURN_POINTER(JsonbValueToJsonb(result.res));
}
示例13: jsonb_object
/*
* SQL function jsonb_object(text[])
*
* take a one or two dimensional array of text as name value pairs
* for a jsonb object.
*
*/
Datum
jsonb_object(PG_FUNCTION_ARGS)
{
ArrayType *in_array = PG_GETARG_ARRAYTYPE_P(0);
int ndims = ARR_NDIM(in_array);
Datum *in_datums;
bool *in_nulls;
int in_count,
count,
i;
JsonbInState result;
memset(&result, 0, sizeof(JsonbInState));
(void) pushJsonbValue(&result.parseState, WJB_BEGIN_OBJECT, NULL);
switch (ndims)
{
case 0:
goto close_object;
break;
case 1:
if ((ARR_DIMS(in_array)[0]) % 2)
ereport(ERROR,
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
errmsg("array must have even number of elements")));
break;
case 2:
if ((ARR_DIMS(in_array)[1]) != 2)
ereport(ERROR,
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
errmsg("array must have two columns")));
break;
default:
ereport(ERROR,
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
errmsg("wrong number of array subscripts")));
}
deconstruct_array(in_array,
TEXTOID, -1, false, 'i',
&in_datums, &in_nulls, &in_count);
count = in_count / 2;
for (i = 0; i < count; ++i)
{
JsonbValue v;
char *str;
int len;
if (in_nulls[i * 2])
ereport(ERROR,
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
errmsg("null value not allowed for object key")));
str = TextDatumGetCString(in_datums[i * 2]);
len = strlen(str);
v.type = jbvString;
v.val.string.len = len;
v.val.string.val = str;
(void) pushJsonbValue(&result.parseState, WJB_KEY, &v);
if (in_nulls[i * 2 + 1])
{
v.type = jbvNull;
}
else
{
str = TextDatumGetCString(in_datums[i * 2 + 1]);
len = strlen(str);
v.type = jbvString;
v.val.string.len = len;
v.val.string.val = str;
}
(void) pushJsonbValue(&result.parseState, WJB_VALUE, &v);
}
pfree(in_datums);
pfree(in_nulls);
close_object:
result.res = pushJsonbValue(&result.parseState, WJB_END_OBJECT, NULL);
//.........这里部分代码省略.........
示例14: hstore_from_array
Datum
hstore_from_array(PG_FUNCTION_ARGS)
{
ArrayType *in_array = PG_GETARG_ARRAYTYPE_P(0);
int ndims = ARR_NDIM(in_array);
int count;
int4 buflen;
HStore *out;
Pairs *pairs;
Datum *in_datums;
bool *in_nulls;
int in_count;
int i;
Assert(ARR_ELEMTYPE(in_array) == TEXTOID);
switch (ndims)
{
case 0:
out = hstorePairs(NULL, 0, 0);
PG_RETURN_POINTER(out);
case 1:
if ((ARR_DIMS(in_array)[0]) % 2)
ereport(ERROR,
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
errmsg("array must have even number of elements")));
break;
case 2:
if ((ARR_DIMS(in_array)[1]) != 2)
ereport(ERROR,
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
errmsg("array must have two columns")));
break;
default:
ereport(ERROR,
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
errmsg("wrong number of array subscripts")));
}
deconstruct_array(in_array,
TEXTOID, -1, false, 'i',
&in_datums, &in_nulls, &in_count);
count = in_count / 2;
pairs = palloc(count * sizeof(Pairs));
for (i = 0; i < count; ++i)
{
if (in_nulls[i * 2])
ereport(ERROR,
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
errmsg("null value not allowed for hstore key")));
if (in_nulls[i * 2 + 1])
{
pairs[i].key = VARDATA_ANY(in_datums[i * 2]);
pairs[i].val = NULL;
pairs[i].keylen = hstoreCheckKeyLen(VARSIZE_ANY_EXHDR(in_datums[i * 2]));
pairs[i].vallen = 4;
pairs[i].isnull = true;
pairs[i].needfree = false;
}
else
{
pairs[i].key = VARDATA_ANY(in_datums[i * 2]);
pairs[i].val = VARDATA_ANY(in_datums[i * 2 + 1]);
pairs[i].keylen = hstoreCheckKeyLen(VARSIZE_ANY_EXHDR(in_datums[i * 2]));
pairs[i].vallen = hstoreCheckValLen(VARSIZE_ANY_EXHDR(in_datums[i * 2 + 1]));
pairs[i].isnull = false;
pairs[i].needfree = false;
}
}
count = hstoreUniquePairs(pairs, count, &buflen);
out = hstorePairs(pairs, count, buflen);
PG_RETURN_POINTER(out);
}
示例15: hstore_delete_array
Datum
hstore_delete_array(PG_FUNCTION_ARGS)
{
HStore *hs = PG_GETARG_HS(0);
HStore *out = palloc(VARSIZE(hs));
int hs_count = HS_COUNT(hs);
char *ps,
*bufd,
*pd;
HEntry *es,
*ed;
int i,
j;
int outcount = 0;
ArrayType *key_array = PG_GETARG_ARRAYTYPE_P(1);
int nkeys;
Pairs *key_pairs = hstoreArrayToPairs(key_array, &nkeys);
SET_VARSIZE(out, VARSIZE(hs));
HS_SETCOUNT(out, hs_count); /* temporary! */
ps = STRPTR(hs);
es = ARRPTR(hs);
bufd = pd = STRPTR(out);
ed = ARRPTR(out);
if (nkeys == 0)
{
/* return a copy of the input, unchanged */
memcpy(out, hs, VARSIZE(hs));
HS_FIXSIZE(out, hs_count);
HS_SETCOUNT(out, hs_count);
PG_RETURN_POINTER(out);
}
/*
* this is in effect a merge between hs and key_pairs, both of which are
* already sorted by (keylen,key); we take keys from hs only
*/
for (i = j = 0; i < hs_count;)
{
int difference;
if (j >= nkeys)
difference = -1;
else
{
int skeylen = HS_KEYLEN(es, i);
if (skeylen == key_pairs[j].keylen)
difference = strncmp(HS_KEY(es, ps, i),
key_pairs[j].key,
key_pairs[j].keylen);
else
difference = (skeylen > key_pairs[j].keylen) ? 1 : -1;
}
if (difference > 0)
++j;
else if (difference == 0)
++i, ++j;
else
{
HS_COPYITEM(ed, bufd, pd,
HS_KEY(es, ps, i), HS_KEYLEN(es, i),
HS_VALLEN(es, i), HS_VALISNULL(es, i));
++outcount;
++i;
}
}
HS_FINALIZE(out, outcount, bufd, pd);
PG_RETURN_POINTER(out);
}