示例#1
0
文件: kmeans.c 项目: CheJharia/madlib
Datum
internal_get_array_of_close_canopies(PG_FUNCTION_ARGS)
{
    SvecType       *svec;
    Datum          *all_canopies;
    int             num_all_canopies;
    float8          threshold;
    PGFunction      metric_fn;
    
    ArrayType      *close_canopies_arr;
    int32          *close_canopies;
    int             num_close_canopies;
    size_t          bytes;
    MemoryContext   mem_context_for_function_calls;
    
    svec = PG_GETARG_SVECTYPE_P(verify_arg_nonnull(fcinfo, 0));
    get_svec_array_elms(PG_GETARG_ARRAYTYPE_P(verify_arg_nonnull(fcinfo, 1)),
        &all_canopies, &num_all_canopies);
    threshold = PG_GETARG_FLOAT8(verify_arg_nonnull(fcinfo, 2));
    metric_fn = get_metric_fn(PG_GETARG_INT32(verify_arg_nonnull(fcinfo, 3)));
    
    mem_context_for_function_calls = setup_mem_context_for_functional_calls();
    close_canopies = (int32 *) palloc(sizeof(int32) * num_all_canopies);
    num_close_canopies = 0;
    for (int i = 0; i < num_all_canopies; i++) {
        if (compute_metric(metric_fn, mem_context_for_function_calls,
                PointerGetDatum(svec), all_canopies[i]) < threshold)
            close_canopies[num_close_canopies++] = i + 1 /* lower bound */;
    }
    MemoryContextDelete(mem_context_for_function_calls);

    /* If we cannot find any close canopy, return NULL. Note that the result
     * we return will be passed to internal_kmeans_closest_centroid() and if the
     * array of close canopies is NULL, then internal_kmeans_closest_centroid()
     * will consider and compute the distance to all centroids. */
    if (num_close_canopies == 0)
        PG_RETURN_NULL();

    bytes = ARR_OVERHEAD_NONULLS(1) + sizeof(int32) * num_close_canopies;
    close_canopies_arr = (ArrayType *) palloc0(bytes);
    SET_VARSIZE(close_canopies_arr, bytes);
    ARR_ELEMTYPE(close_canopies_arr) = INT4OID;
    ARR_NDIM(close_canopies_arr) = 1;
    ARR_DIMS(close_canopies_arr)[0] = num_close_canopies;
    ARR_LBOUND(close_canopies_arr)[0] = 1;
    memcpy(ARR_DATA_PTR(close_canopies_arr), close_canopies,
        sizeof(int32) * num_close_canopies);

    PG_RETURN_ARRAYTYPE_P(close_canopies_arr);
}
示例#2
0
/*
 *		dsign			- returns -1 if the argument is less than 0, 0
 *						  if the argument is equal to 0, and 1 if the
 *						  argument is greater than zero.
 */
Datum
dsign(PG_FUNCTION_ARGS)
{
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		result;

	if (arg1 > 0)
		result = 1.0;
	else if (arg1 < 0)
		result = -1.0;
	else
		result = 0.0;

	PG_RETURN_FLOAT8(result);
}
示例#3
0
Datum
hashfloat8(PG_FUNCTION_ARGS)
{
	float8		key = PG_GETARG_FLOAT8(0);

	/*
	 * On IEEE-float machines, minus zero and zero have different bit
	 * patterns but should compare as equal.  We must ensure that they
	 * have the same hash value, which is most easily done this way:
	 */
	if (key == (float8) 0)
		PG_RETURN_UINT32(0);

	return hash_any((unsigned char *) &key, sizeof(key));
}
示例#4
0
Datum earth_get_square(PG_FUNCTION_ARGS)
{
	Point	   *pt = PG_GETARG_POINT_P(0);
	double	   dist = PG_GETARG_FLOAT8(1);
	BOX	   *box;

    if(!( (earth_check_point(pt) == 0) && (earth_check_dist(dist) == 0))) 
    		ereport(ERROR,
			(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
			errmsg("attempt to get a box form a dist:%.10f rad for a point:(lat=%.10f, lon=%.10f) out of range",dist,pt->x,pt->y)));
	
	box = earth_get_box_internal(pt, dist);

	PG_RETURN_BOX_P(box);
}
示例#5
0
文件: cash.c 项目: yangineer/cscd43-1
/* cash_div_flt8()
 * Divide cash by float8.
 *
 * XXX Don't know if rounding or truncating is correct behavior.
 * Round for now. - tgl 97/04/15
 */
Datum
cash_div_flt8(PG_FUNCTION_ARGS)
{
    Cash		c = PG_GETARG_CASH(0);
    float8		f = PG_GETARG_FLOAT8(1);
    Cash		result;

    if (f == 0.0)
        ereport(ERROR,
                (errcode(ERRCODE_DIVISION_BY_ZERO),
                 errmsg("division by zero")));

    result = rint(c / f);
    PG_RETURN_CASH(result);
}
示例#6
0
Datum aggr_InfoGain(PG_FUNCTION_ARGS) {
	ArrayType *state  = PG_GETARG_ARRAYTYPE_P(0);
	int dimstate = ARR_NDIM(state);
    int *dimsstate = ARR_DIMS(state);
	int numstate = ArrayGetNItems(dimstate,dimsstate);
    float8 *vals_state=(float8 *)ARR_DATA_PTR(state);
    
	float8 truevalue = PG_GETARG_FLOAT8(1);
	float8 trueweight = PG_GETARG_FLOAT8(2);
	int32 posclasses = PG_GETARG_INT32(3);
	int32 trueclass = PG_GETARG_INT32(5);
   	ArrayType *pgarray;

	vals_state[0] += trueweight;
	vals_state[trueclass] += trueweight;
	
	vals_state[(int)(truevalue*(posclasses+1))] += trueweight;
	vals_state[(int)(truevalue*(posclasses+1) + trueclass)] += trueweight;
  
    pgarray = construct_array((Datum *)vals_state,
		numstate,FLOAT8OID,
		sizeof(float8),true,'d');
    PG_RETURN_ARRAYTYPE_P(pgarray);
}
示例#7
0
/* Create a one dimensional box with identical upper and lower coordinates */
Datum
cube_f8(PG_FUNCTION_ARGS)
{
	double		x = PG_GETARG_FLOAT8(0);
	NDBOX	   *result;
	int			size;

	size = offsetof(NDBOX, x[0]) +sizeof(double) * 2;
	result = (NDBOX *) palloc0(size);
	SET_VARSIZE(result, size);
	result->dim = 1;
	result->x[0] = result->x[1] = x;

	PG_RETURN_NDBOX(result);
}
示例#8
0
/* Create a one dimensional box with identical upper and lower coordinates */
Datum
cube_f8(PG_FUNCTION_ARGS)
{
	NDBOX	   *result;
	int			size;

	size = offsetof(NDBOX, x[0]) + sizeof(double) * 2;
	result = (NDBOX *) palloc(size);
	memset(result, 0, size);
	result->size = size;
	result->dim = 1;
	result->x[0] = PG_GETARG_FLOAT8(0);
	result->x[1] = result->x[0];

	PG_RETURN_NDBOX(result);
}
示例#9
0
/*
 *		dsqrt			- returns square root of arg1
 */
Datum
dsqrt(PG_FUNCTION_ARGS)
{
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		result;

	if (arg1 < 0)
		ereport(ERROR,
				(errcode(ERRCODE_FLOATING_POINT_EXCEPTION),
				 errmsg("cannot take square root of a negative number")));

	result = sqrt(arg1);

	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
}
示例#10
0
文件: convert.c 项目: 50wu/gpdb
Datum
orafce_to_char_float8(PG_FUNCTION_ARGS)
{
	float8		arg0 = PG_GETARG_FLOAT8(0);
	StringInfo	buf = makeStringInfo();
	struct lconv *lconv = PGLC_localeconv();
	char	   *p;

	appendStringInfo(buf, "%f", arg0);

	for (p = buf->data; *p; p++)
		if (*p == '.')
			*p = lconv->decimal_point[0];

	PG_RETURN_TEXT_P(cstring_to_text(buf->data));
}
示例#11
0
Datum
float48div(PG_FUNCTION_ARGS)
{
	float4		arg1 = PG_GETARG_FLOAT4(0);
	float8		arg2 = PG_GETARG_FLOAT8(1);
	float8		result;

	if (arg2 == 0.0)
		ereport(ERROR,
				(errcode(ERRCODE_DIVISION_BY_ZERO),
				 errmsg("division by zero")));

	result = arg1 / arg2;
	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
}
示例#12
0
文件: cube.c 项目: adam8157/gpdb
/* Create a one dimensional box with identical upper and lower coordinates */
Datum
cube_f8(PG_FUNCTION_ARGS)
{
	double		x = PG_GETARG_FLOAT8(0);
	NDBOX	   *result;
	int			size;

	size = POINT_SIZE(1);
	result = (NDBOX *) palloc0(size);
	SET_VARSIZE(result, size);
	SET_DIM(result, 1);
	SET_POINT_BIT(result);
	result->x[0] = x;

	PG_RETURN_NDBOX(result);
}
示例#13
0
文件: colors.c 项目: zejn/pg-colors
Datum
delta_e_cmc_full(PG_FUNCTION_ARGS)
{
    float8  result;
    float8 l1 = PG_GETARG_FLOAT8(0);
    float8 a1 = PG_GETARG_FLOAT8(1);
    float8 b1 = PG_GETARG_FLOAT8(2);
    float8 l2 = PG_GETARG_FLOAT8(3);
    float8 a2 = PG_GETARG_FLOAT8(4);
    float8 b2 = PG_GETARG_FLOAT8(5);
    float8 pl = PG_GETARG_FLOAT8(6);
    float8 pc = PG_GETARG_FLOAT8(7);

    result = colors_delta_e_cmc(l1, a1, b1, l2, a2, b2, pl, pc);

    PG_RETURN_FLOAT8(result);

}
示例#14
0
文件: alert.c 项目: WhiteCatmi/orafce
Datum
dbms_alert_waitany(PG_FUNCTION_ARGS)
{
	float8 timeout;
	TupleDesc   tupdesc;
	AttInMetadata       *attinmeta;
	HeapTuple   tuple;
	Datum       result;
	char *str[3] = {NULL, NULL, "1"};
	int cycle = 0;
	float8 endtime;
	TupleDesc	btupdesc;

	if (PG_ARGISNULL(0))
		timeout = TDAYS;
	else
		timeout = PG_GETARG_FLOAT8(0);

	WATCH_PRE(timeout, endtime, cycle);
	if (ora_lock_shmem(SHMEMMSGSZ, MAX_PIPES, MAX_EVENTS, MAX_LOCKS, false))
	{
		str[1]  = find_and_remove_message_item(-1, sid,
							   true, false, false, NULL, &str[0]);
		if (str[0])
		{
			str[2] = "0";
			LWLockRelease(shmem_lock);
			break;
		}
		LWLockRelease(shmem_lock);
	}
	WATCH_POST(timeout, endtime, cycle);

	get_call_result_type(fcinfo, NULL, &tupdesc);
	btupdesc = BlessTupleDesc(tupdesc);
	attinmeta = TupleDescGetAttInMetadata(btupdesc);
	tuple = BuildTupleFromCStrings(attinmeta, str);
	result = HeapTupleGetDatum(tuple);

	if (str[0])
		pfree(str[0]);

	if (str[1])
		pfree(str[1]);

	return result;
}
示例#15
0
Datum
gbt_float8_distance(PG_FUNCTION_ARGS)
{
	GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
	float8		query = PG_GETARG_FLOAT8(1);

	/* Oid		subtype = PG_GETARG_OID(3); */
	float8KEY  *kkk = (float8KEY *) DatumGetPointer(entry->key);
	GBT_NUMKEY_R key;

	key.lower = (GBT_NUMKEY *) &kkk->lower;
	key.upper = (GBT_NUMKEY *) &kkk->upper;

	PG_RETURN_FLOAT8(
			gbt_num_distance(&key, (void *) &query, GIST_LEAF(entry), &tinfo)
		);
}
示例#16
0
文件: kmeans.c 项目: 0x0all/madlib
Datum
internal_remove_close_canopies(PG_FUNCTION_ARGS) {
    ArrayType      *all_canopies_arr;
    Datum          *all_canopies;
    int             num_all_canopies;
    PGFunction      metric_fn;
    float8          threshold;
    
    Datum          *close_canopies;
    int             num_close_canopies;
    bool            addIndexI;
    MemoryContext   mem_context_for_function_calls;

    all_canopies_arr = PG_GETARG_ARRAYTYPE_P(verify_arg_nonnull(fcinfo, 0));
    get_svec_array_elms(all_canopies_arr, &all_canopies, &num_all_canopies);
    metric_fn = get_metric_fn(PG_GETARG_INT32(verify_arg_nonnull(fcinfo, 1)));
    threshold = PG_GETARG_FLOAT8(verify_arg_nonnull(fcinfo, 2));
    
    mem_context_for_function_calls = setup_mem_context_for_functional_calls();
    close_canopies = (Datum *) palloc(sizeof(Datum) * num_all_canopies);
    num_close_canopies = 0;
    for (int i = 0; i < num_all_canopies; i++) {
        addIndexI = true;
        for (int j = 0; j < num_close_canopies; j++) {
            if (compute_metric(metric_fn, mem_context_for_function_calls,
                all_canopies[i], close_canopies[j]) < threshold) {
                
                addIndexI = false;
                break;
            }
        }
        if (addIndexI)
            close_canopies[num_close_canopies++] = all_canopies[i];
    }
    MemoryContextDelete(mem_context_for_function_calls);
    
    PG_RETURN_ARRAYTYPE_P(
        construct_array(
            close_canopies, /* elems */
            num_close_canopies, /* nelems */
            ARR_ELEMTYPE(all_canopies_arr), /* elmtype */
            -1, /* elmlen */
            false, /* elmbyval */
            'd') /* elmalign */
        );
}
示例#17
0
Datum
gbt_float8_consistent(PG_FUNCTION_ARGS)
{

	GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
	float8		query = PG_GETARG_FLOAT8(1);
	float8KEY  *kkk = (float8KEY *) DatumGetPointer(entry->key);
	GBT_NUMKEY_R key;
	StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);

	key.lower = (GBT_NUMKEY *) & kkk->lower;
	key.upper = (GBT_NUMKEY *) & kkk->upper;

	PG_RETURN_BOOL(
				   gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
		);
}
示例#18
0
/*
 * pg_sleep - delay for N seconds
 */
Datum
pg_sleep(PG_FUNCTION_ARGS)
{
	float8		secs = PG_GETARG_FLOAT8(0);
	float8		endtime;

	/*
	 * We sleep using WaitLatch, to ensure that we'll wake up promptly if an
	 * important signal (such as SIGALRM or SIGINT) arrives.  Because
	 * WaitLatch's upper limit of delay is INT_MAX milliseconds, and the user
	 * might ask for more than that, we sleep for at most 10 minutes and then
	 * loop.
	 *
	 * By computing the intended stop time initially, we avoid accumulation of
	 * extra delay across multiple sleeps.  This also ensures we won't delay
	 * less than the specified time when WaitLatch is terminated early by a
	 * non-query-canceling signal such as SIGHUP.
	 */
#define GetNowFloat()	((float8) GetCurrentTimestamp() / 1000000.0)

	endtime = GetNowFloat() + secs;

	for (;;)
	{
		float8		delay;
		long		delay_ms;

		CHECK_FOR_INTERRUPTS();

		delay = endtime - GetNowFloat();
		if (delay >= 600.0)
			delay_ms = 600000;
		else if (delay > 0.0)
			delay_ms = (long) ceil(delay * 1000.0);
		else
			break;

		(void) WaitLatch(MyLatch,
						 WL_LATCH_SET | WL_TIMEOUT,
						 delay_ms,
						 WAIT_EVENT_PG_SLEEP);
		ResetLatch(MyLatch);
	}

	PG_RETURN_VOID();
}
示例#19
0
Datum pgm_svm_train(PG_FUNCTION_ARGS){

    PGM_Matriz_Double *matrix = (PGM_Matriz_Double*)PG_GETARG_POINTER(0);
    PGM_Vetor_Double *vector = (PGM_Vetor_Double*)PG_GETARG_POINTER(1);

    struct svm_parameter *param = (struct svm_parameter*) pgm_malloc (sizeof(struct svm_parameter));
    struct svm_problem* prob;
    //Cross Validation
    int cross_validation = 0,
        n_fold = PG_GETARG_INT32(14);

    if (n_fold < 2 && n_fold != 0) exit_with_help();
    else if( n_fold >= 2){
        cross_validation = 1;
        elog(ERROR,"CROSS VALIDATION NÃO IMPLEMENTADO");
    }

    //Mount Parameter Struct
    param->svm_type = PG_GETARG_INT32(2);
	param->kernel_type= PG_GETARG_INT32(3);
	param->degree= PG_GETARG_INT32(4);
	param->gamma= PG_GETARG_FLOAT8(5);
	param->coef0= PG_GETARG_FLOAT8(6);
	param->cache_size= PG_GETARG_FLOAT8(7);
	param->eps= PG_GETARG_FLOAT8(8);
	param->C= PG_GETARG_FLOAT8(9);
	param->nr_weight = 0;
	param->weight_label = NULL;
	param->weight = NULL;
	param->nu= PG_GETARG_FLOAT8(10);
	param->p= PG_GETARG_FLOAT8(11);
	param->shrinking= PG_GETARG_INT32(12);
	param->probability= PG_GETARG_INT32(13);

	prob = PGM_Matriz_Double2svm_problem(matrix,vector,param);

	if (cross_validation){
        do_cross_validation(prob,param,n_fold);
        elog(ERROR,"CROSS VALIDATION NÃO IMPLEMENTADO"); // Pergunta ao Filipe sobre isso!
        PG_RETURN_VOID();
	}else{

        MemoryContext contextoAnterior = MemoryContextSwitchTo( CurTransactionContext );
        struct svm_model *model = svm_train(prob,param);
        MemoryContextSwitchTo( contextoAnterior );
        PG_RETURN_POINTER(model);
	}
}
示例#20
0
Datum
quantile_append_numeric(PG_FUNCTION_ARGS)
{
    
    struct_numeric * data;
    
    MemoryContext oldcontext;
    MemoryContext aggcontext;

    GET_AGG_CONTEXT("quantile_append_numeric", fcinfo, aggcontext);
    
    oldcontext = MemoryContextSwitchTo(aggcontext);
        
    if (PG_ARGISNULL(0)) {
        data = (struct_numeric*)palloc(sizeof(struct_numeric));
        data->elements  = (Numeric*)palloc(SLICE_SIZE*sizeof(Numeric));
        data->nelements = SLICE_SIZE;
        data->next = 0;
        
        data->quantiles = (double*)palloc(sizeof(double));
        data->quantiles[0] = PG_GETARG_FLOAT8(2);
        data->nquantiles = 1;
    } else {
        data = (struct_numeric*)PG_GETARG_POINTER(0);
    }
    
    /* ignore NULL values */
    if (! PG_ARGISNULL(1)) {
    
        Numeric element = PG_GETARG_NUMERIC(1);
        
        if (data->next > data->nelements-1) {
            data->elements = (Numeric*)repalloc(data->elements, sizeof(Numeric)*(data->nelements + SLICE_SIZE));
            data->nelements = data->nelements + SLICE_SIZE;
        }
        
        data->elements[data->next++] = element;
        
    }
    
    MemoryContextSwitchTo(oldcontext);
    
    PG_RETURN_POINTER(data);

}
示例#21
0
Datum
alpine_miner_lr_ca_fitness(PG_FUNCTION_ARGS)
{

        ArrayType  *beta_arg, *columns_arg;
        float8     *beta_data, *columns_data;
        int     beta_count, columns_count;

	bool add_intercept_arg;
	double weight_arg;
	int label_value_arg;

	double gx = 0.0;
	double pi = 0.0;
	double fitness = 0.0;
	if (PG_ARGISNULL(0) || PG_ARGISNULL(1) || PG_ARGISNULL(2) || PG_ARGISNULL(3) || PG_ARGISNULL(4)){
		PG_RETURN_NULL();
	}
        beta_arg = PG_GETARG_ARRAYTYPE_P(0);
        columns_arg = PG_GETARG_ARRAYTYPE_P(1);

	add_intercept_arg = PG_GETARG_BOOL(2);
	weight_arg = PG_GETARG_FLOAT8(3);
	label_value_arg = PG_GETARG_INT32(4);

	beta_data = (float8*) ARR_DATA_PTR(beta_arg);
	columns_data = (float8*) ARR_DATA_PTR(columns_arg);
	
	beta_count = ARR_DIMS(beta_arg)[0];
	columns_count = ARR_DIMS(columns_arg)[0];

	pi = alpine_miner_compute_pi(beta_data, beta_count, columns_data, columns_count, add_intercept_arg);

	if (label_value_arg == 1)
	{
		fitness = log(pi);
	}
	else
	{
		fitness = log(1.0 - pi);
	}
	fitness *= weight_arg;

	PG_RETURN_FLOAT8(fitness);
}
示例#22
0
文件: cube.c 项目: adam8157/gpdb
/* Add a dimension to an existing cube with the same values for the new
   coordinate */
Datum
cube_c_f8(PG_FUNCTION_ARGS)
{
	NDBOX	   *cube = PG_GETARG_NDBOX(0);
	double		x = PG_GETARG_FLOAT8(1);
	NDBOX	   *result;
	int			size;
	int			i;

	if (DIM(cube) + 1 > CUBE_MAX_DIM)
		ereport(ERROR,
				(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
				 errmsg("can't extend cube"),
				 errdetail("A cube cannot have more than %d dimensions.",
							   CUBE_MAX_DIM)));

	if (IS_POINT(cube))
	{
		size = POINT_SIZE((DIM(cube) + 1));
		result = (NDBOX *) palloc0(size);
		SET_VARSIZE(result, size);
		SET_DIM(result, DIM(cube) + 1);
		SET_POINT_BIT(result);
		for (i = 0; i < DIM(cube); i++)
			result->x[i] = cube->x[i];
		result->x[DIM(result) - 1] = x;
	}
	else
	{
		size = CUBE_SIZE((DIM(cube) + 1));
		result = (NDBOX *) palloc0(size);
		SET_VARSIZE(result, size);
		SET_DIM(result, DIM(cube) + 1);
		for (i = 0; i < DIM(cube); i++)
		{
			result->x[i] = cube->x[i];
			result->x[DIM(result) + i] = cube->x[DIM(cube) + i];
		}
		result->x[DIM(result) - 1] = x;
		result->x[2 * DIM(result) - 1] = x;
	}

	PG_FREE_IF_COPY(cube, 0);
	PG_RETURN_NDBOX(result);
}
示例#23
0
Datum
quantile_append_double_array(PG_FUNCTION_ARGS)
{
    
    struct_double * data;
    
    MemoryContext oldcontext;
    MemoryContext aggcontext;

    GET_AGG_CONTEXT("quantile_append_double_array", fcinfo, aggcontext);

    oldcontext = MemoryContextSwitchTo(aggcontext);
        
    if (PG_ARGISNULL(0)) {
        data = (struct_double*)palloc(sizeof(struct_double));
        data->elements  = (double*)palloc(SLICE_SIZE*sizeof(double));
        data->nelements = SLICE_SIZE;
        data->next = 0;
        
        /* read the array of quantiles */
        data->quantiles = array_to_double(fcinfo, PG_GETARG_ARRAYTYPE_P(2), &data->nquantiles);
        
    } else {
        data = (struct_double*)PG_GETARG_POINTER(0);
    }
    
    /* ignore NULL values */
    if (! PG_ARGISNULL(1)) {
    
        double element = PG_GETARG_FLOAT8(1);
        
        if (data->next > data->nelements-1) {
            data->elements = (double*)repalloc(data->elements, sizeof(double)*(data->nelements + SLICE_SIZE));
            data->nelements = data->nelements + SLICE_SIZE;
        }
        
        data->elements[data->next++] = element;
        
    }
    
    MemoryContextSwitchTo(oldcontext);
    
    PG_RETURN_POINTER(data);

}
示例#24
0
文件: misc.c 项目: Joe-xXx/postgres
/*
 * pg_sleep - delay for N seconds
 */
Datum
pg_sleep(PG_FUNCTION_ARGS)
{
	float8		secs = PG_GETARG_FLOAT8(0);
	float8		endtime;

	/*
	 * We break the requested sleep into segments of no more than 1 second, to
	 * put an upper bound on how long it will take us to respond to a cancel
	 * or die interrupt.  (Note that pg_usleep is interruptible by signals on
	 * some platforms but not others.)	Also, this method avoids exposing
	 * pg_usleep's upper bound on allowed delays.
	 *
	 * By computing the intended stop time initially, we avoid accumulation of
	 * extra delay across multiple sleeps.	This also ensures we won't delay
	 * less than the specified time if pg_usleep is interrupted by other
	 * signals such as SIGHUP.
	 */

#ifdef HAVE_INT64_TIMESTAMP
#define GetNowFloat()	((float8) GetCurrentTimestamp() / 1000000.0)
#else
#define GetNowFloat()	GetCurrentTimestamp()
#endif

	endtime = GetNowFloat() + secs;

	for (;;)
	{
		float8		delay;

		CHECK_FOR_INTERRUPTS();
		delay = endtime - GetNowFloat();
		if (delay >= 1.0)
			pg_usleep(1000000L);
		else if (delay > 0.0)
			pg_usleep((long) ceil(delay * 1000000.0));
		else
			break;
	}

	PG_RETURN_VOID();
}
Datum LWGEOM_simplify2d(PG_FUNCTION_ARGS)
{
	GSERIALIZED *geom = (GSERIALIZED *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
	GSERIALIZED *result;
	LWGEOM *in = lwgeom_from_gserialized(geom);
	LWGEOM *out;
	double dist = PG_GETARG_FLOAT8(1);

	out = lwgeom_simplify(in, dist);
	if ( ! out ) PG_RETURN_NULL();

	/* COMPUTE_BBOX TAINTING */
	if ( in->bbox ) lwgeom_add_bbox(out);

	result = geometry_serialize(out);
	lwgeom_free(out);
	PG_FREE_IF_COPY(geom, 0);
	PG_RETURN_POINTER(result);
}
示例#26
0
/*
 *		dtan			- returns the tangent of arg1 (radians)
 */
Datum
dtan(PG_FUNCTION_ARGS)
{
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		result;

	errno = 0;
	result = tan(arg1);
	if (errno != 0
#ifdef HAVE_FINITE
		|| !finite(result)
#endif
		)
		ereport(ERROR,
				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
				 errmsg("input is out of range")));

	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
}
示例#27
0
文件: kmeans.c 项目: 0x0all/madlib
Datum
internal_kmeans_canopy_transition(PG_FUNCTION_ARGS) {
    ArrayType      *canopies_arr;
    Datum          *canopies;
    int             num_canopies;
    SvecType       *point;
    PGFunction      metric_fn;
    float8          threshold;

    MemoryContext   mem_context_for_function_calls;
    
    canopies_arr = PG_GETARG_ARRAYTYPE_P(verify_arg_nonnull(fcinfo, 0));
    get_svec_array_elms(canopies_arr, &canopies, &num_canopies);
    point = PG_GETARG_SVECTYPE_P(verify_arg_nonnull(fcinfo, 1));
    metric_fn = get_metric_fn(PG_GETARG_INT32(verify_arg_nonnull(fcinfo, 2)));
    threshold = PG_GETARG_FLOAT8(verify_arg_nonnull(fcinfo, 3));
    
    mem_context_for_function_calls = setup_mem_context_for_functional_calls();
    for (int i = 0; i < num_canopies; i++) {
        if (compute_metric(metric_fn, mem_context_for_function_calls,
            PointerGetDatum(point), canopies[i]) < threshold)
            PG_RETURN_ARRAYTYPE_P(canopies_arr);
    }
    MemoryContextDelete(mem_context_for_function_calls);
    
    int idx = (ARR_NDIM(canopies_arr) == 0)
        ? 1
        : ARR_LBOUND(canopies_arr)[0] + ARR_DIMS(canopies_arr)[0];
    return PointerGetDatum(
        array_set(
            canopies_arr, /* array: the initial array object (mustn't be NULL) */
            1, /* nSubscripts: number of subscripts supplied */
            &idx, /* indx[]: the subscript values */
            PointerGetDatum(point), /* dataValue: the datum to be inserted at the given position */
            false, /* isNull: whether dataValue is NULL */
            -1, /* arraytyplen: pg_type.typlen for the array type */
            -1, /* elmlen: pg_type.typlen for the array's element type */
            false, /* elmbyval: pg_type.typbyval for the array's element type */
            'd') /* elmalign: pg_type.typalign for the array's element type */
        );
}
示例#28
0
文件: colors.c 项目: zejn/pg-colors
Datum
delta_e_cie_1994(PG_FUNCTION_ARGS)
{
    float8 result;
    float8 l1 = PG_GETARG_FLOAT8(0);
    float8 a1 = PG_GETARG_FLOAT8(1);
    float8 b1 = PG_GETARG_FLOAT8(2);
    float8 l2 = PG_GETARG_FLOAT8(3);
    float8 a2 = PG_GETARG_FLOAT8(4);
    float8 b2 = PG_GETARG_FLOAT8(5);

    result = colors_delta_e_cie_1994(l1, a1, b1, l2, a2, b2, 1.0, 1.0, 1.0, 0.045, 0.015);

    PG_RETURN_FLOAT8(result);
}
示例#29
0
/*
 *		dlog10			- returns the base 10 logarithm of arg1
 */
Datum
dlog10(PG_FUNCTION_ARGS)
{
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		result;

	if (arg1 == 0.0)
		ereport(ERROR,
				(errcode(ERRCODE_FLOATING_POINT_EXCEPTION),
				 errmsg("cannot take logarithm of zero")));

	if (arg1 < 0)
		ereport(ERROR,
				(errcode(ERRCODE_FLOATING_POINT_EXCEPTION),
				 errmsg("cannot take logarithm of a negative number")));

	result = log10(arg1);

	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
}
示例#30
0
/*
 *		float8_text		- converts a float8 number to a text string
 */
Datum
float8_text(PG_FUNCTION_ARGS)
{
	float8		num = PG_GETARG_FLOAT8(0);
	text	   *result;
	int			len;
	char	   *str;

	str = DatumGetCString(DirectFunctionCall1(float8out,
											  Float8GetDatum(num)));

	len = strlen(str) + VARHDRSZ;

	result = (text *) palloc(len);

	VARATT_SIZEP(result) = len;
	memcpy(VARDATA(result), str, (len - VARHDRSZ));

	pfree(str);

	PG_RETURN_TEXT_P(result);
}