Exemple #1
0
Datum
to_tsquery(PG_FUNCTION_ARGS)
{
	text	   *in = PG_GETARG_TEXT_P(0);
	Oid			cfgId;

	cfgId = getTSCurrentConfig(true);
	PG_RETURN_DATUM(DirectFunctionCall2(to_tsquery_byid,
										ObjectIdGetDatum(cfgId),
										PointerGetDatum(in)));
}
Datum
similarity_op(PG_FUNCTION_ARGS)
{
	float4		res = DatumGetFloat4(DirectFunctionCall2(
														 similarity,
														 PG_GETARG_DATUM(0),
														 PG_GETARG_DATUM(1)
														 ));

	PG_RETURN_BOOL(res >= trgm_limit);
}
Exemple #3
0
Datum
gpupreagg_psum_x2_numeric(PG_FUNCTION_ARGS)
{
	Assert(PG_NARGS() == 1);
	if (PG_ARGISNULL(0))
		PG_RETURN_NULL();

	PG_RETURN_NUMERIC(DirectFunctionCall2(numeric_mul,
										  PG_GETARG_DATUM(0),
										  PG_GETARG_DATUM(0)));
}
Exemple #4
0
static int
compareNumeric(Numeric a, Numeric b)
{
	return	DatumGetInt32(
				DirectFunctionCall2(
					numeric_cmp,
					PointerGetDatum(a),
					PointerGetDatum(b)
				)
			);
}
Exemple #5
0
Datum
_int_different(PG_FUNCTION_ARGS)
{
	PG_RETURN_BOOL(!DatumGetBool(
								 DirectFunctionCall2(
													 _int_same,
									   PointerGetDatum(PG_GETARG_POINTER(0)),
										PointerGetDatum(PG_GETARG_POINTER(1))
													 )
								 ));
}
Datum
tsq_mcontained(PG_FUNCTION_ARGS)
{
	PG_RETURN_DATUM(
					DirectFunctionCall2(
										tsq_mcontains,
										PG_GETARG_DATUM(1),
										PG_GETARG_DATUM(0)
										)
		);
}
Exemple #7
0
static int
tskey_cmp(const void *a, const void *b)
{
	return DatumGetInt32(
						 DirectFunctionCall2(
											 timestamp_cmp,
			  TimestampGetDatumFast(((TSKEY *) (((RIX *) a)->r))->lower),
			   TimestampGetDatumFast(((TSKEY *) (((RIX *) b)->r))->lower)
											 )
		);
}
Exemple #8
0
/*
 * Compute the difference in bytes between two WAL locations.
 */
Datum
pg_xlog_location_diff(PG_FUNCTION_ARGS)
{
	Datum		result;

	result = DirectFunctionCall2(pg_lsn_mi,
								 PG_GETARG_DATUM(0),
								 PG_GETARG_DATUM(1));

	PG_RETURN_NUMERIC(result);
}
Exemple #9
0
static int
gbt_macadkey_cmp(const void *a, const void *b)
{
	return DatumGetInt32(
						 DirectFunctionCall2(
											 macaddr_cmp,
										PointerGetDatum(&((Nsrt *) a)->t[0]),
										 PointerGetDatum(&((Nsrt *) b)->t[0])
											 )
		);
}
Exemple #10
0
Datum
gbt_time_penalty(PG_FUNCTION_ARGS)
{
	timeKEY    *origentry = (timeKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
	timeKEY    *newentry = (timeKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
	float	   *result = (float *) PG_GETARG_POINTER(2);
	Interval   *intr;
	double		res;
	double		res2;

	intr = DatumGetIntervalP(DirectFunctionCall2(
												 time_mi_time,
										  P_TimeADTGetDatum(newentry->upper),
									   P_TimeADTGetDatum(origentry->upper)));
	res = INTERVAL_TO_SEC(intr);
	res = Max(res, 0);

	intr = DatumGetIntervalP(DirectFunctionCall2(
												 time_mi_time,
										 P_TimeADTGetDatum(origentry->lower),
										P_TimeADTGetDatum(newentry->lower)));
	res2 = INTERVAL_TO_SEC(intr);
	res2 = Max(res2, 0);

	res += res2;

	*result = 0.0;

	if (res > 0)
	{
		intr = DatumGetIntervalP(DirectFunctionCall2(
													 time_mi_time,
										 P_TimeADTGetDatum(origentry->upper),
									   P_TimeADTGetDatum(origentry->lower)));
		*result += FLT_MIN;
		*result += (float) (res / (res + INTERVAL_TO_SEC(intr)));
		*result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
	}

	PG_RETURN_POINTER(result);
}
Exemple #11
0
static float8
gbt_time_dist(const void *a, const void *b)
{
	const TimeADT *aa = (const TimeADT *) a;
	const TimeADT *bb = (const TimeADT *) b;
	Interval	  *i;

	i = DatumGetIntervalP(DirectFunctionCall2(time_mi_time,
											  TimeADTGetDatumFast(*aa),
											  TimeADTGetDatumFast(*bb)));
	return (float8) Abs(INTERVAL_TO_SEC(i));
}
Exemple #12
0
/*
 * The GiST Penalty method for boxes (also used for points)
 *
 * As in the R-tree paper, we use change in area as our penalty metric
 */
Datum
gist_box_penalty(PG_FUNCTION_ARGS)
{
	GISTENTRY  *origentry = (GISTENTRY *) PG_GETARG_POINTER(0);
	GISTENTRY  *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
	float	   *result = (float *) PG_GETARG_POINTER(2);
	Datum		ud;

	ud = DirectFunctionCall2(rt_box_union, origentry->key, newentry->key);
	*result = (float) (size_box(ud) - size_box(origentry->key));
	PG_RETURN_POINTER(result);
}
Exemple #13
0
Datum
pgstrom_final_avg_int8_final(PG_FUNCTION_ARGS)
{
	ArrayType	   *xarray = PG_GETARG_ARRAYTYPE_P(0);
	int64		   *x = (int64 *)ARR_DATA_PTR(xarray);

	return DirectFunctionCall2(numeric_div,
							   DirectFunctionCall1(int8_numeric,
												   Int64GetDatum(x[0])),
							   DirectFunctionCall1(int8_numeric,
												   Int64GetDatum(x[1])));
}
Exemple #14
0
/* plainto_tsquery(text, text) */
Datum
tsa_plainto_tsquery_name(PG_FUNCTION_ARGS)
{
	text	   *cfgname = PG_GETARG_TEXT_P(0);
	Datum		arg1 = PG_GETARG_DATUM(1);
	Oid			config_oid;

	config_oid = TextGetObjectId(regconfigin, cfgname);

	return DirectFunctionCall2(plainto_tsquery_byid,
							   ObjectIdGetDatum(config_oid), arg1);
}
Exemple #15
0
Datum
spheretrans_point_inverse(PG_FUNCTION_ARGS)
{
	Datum		sp = PG_GETARG_DATUM(0);
	SEuler	   *se = (SEuler *) PG_GETARG_POINTER(1);
	SEuler		tmp;
	Datum		ret;

	spheretrans_inverse(&tmp, se);
	ret = DirectFunctionCall2(spheretrans_point,
							  sp, PointerGetDatum(&tmp));
	PG_RETURN_DATUM(ret);
}
Exemple #16
0
Interval *
abs_interval(Interval *a)
{
	static Interval zero = {0, 0, 0};

	if (DatumGetBool(DirectFunctionCall2(interval_lt,
										 IntervalPGetDatum(a),
										 IntervalPGetDatum(&zero))))
		a = DatumGetIntervalP(DirectFunctionCall1(interval_um,
												  IntervalPGetDatum(a)));

	return a;
}
Exemple #17
0
/*
** Equality method
*/
Datum
gbox_same(PG_FUNCTION_ARGS)
{
	BOX		   *b1 = (BOX *) PG_GETARG_POINTER(0);
	BOX		   *b2 = (BOX *) PG_GETARG_POINTER(1);
	bool	   *result = (bool *) PG_GETARG_POINTER(2);

	if (b1 && b2)
		*result = DatumGetBool(DirectFunctionCall2(box_same, PointerGetDatum(b1), PointerGetDatum(b2)));
	else
		*result = (b1 == NULL && b2 == NULL) ? TRUE : FALSE;
	PG_RETURN_POINTER(result);
}
Exemple #18
0
Datum
pt_in_widget(PG_FUNCTION_ARGS)
{
	Point	   *point = PG_GETARG_POINT_P(0);
	WIDGET	   *widget = (WIDGET *) PG_GETARG_POINTER(1);
	float8		distance;

	distance = DatumGetFloat8(DirectFunctionCall2(point_distance,
												  PointPGetDatum(point),
											PointPGetDatum(&widget->center)));

	PG_RETURN_BOOL(distance < widget->radius);
}
Exemple #19
0
Datum
interpt_pp(PG_FUNCTION_ARGS)
{
	PATH	   *p1 = PG_GETARG_PATH_P(0);
	PATH	   *p2 = PG_GETARG_PATH_P(1);
	int			i,
				j;
	LSEG		seg1,
				seg2;
	bool		found;			/* We've found the intersection */

	found = false;				/* Haven't found it yet */

	for (i = 0; i < p1->npts - 1 && !found; i++)
	{
		regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i + 1]);
		for (j = 0; j < p2->npts - 1 && !found; j++)
		{
			regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j + 1]);
			if (DatumGetBool(DirectFunctionCall2(lseg_intersect,
												 LsegPGetDatum(&seg1),
												 LsegPGetDatum(&seg2))))
				found = true;
		}
	}

	if (!found)
		PG_RETURN_NULL();

	/*
	 * Note: DirectFunctionCall2 will kick out an error if lseg_interpt()
	 * returns NULL, but that should be impossible since we know the two
	 * segments intersect.
	 */
	PG_RETURN_DATUM(DirectFunctionCall2(lseg_interpt,
										LsegPGetDatum(&seg1),
										LsegPGetDatum(&seg2)));
}
Exemple #20
0
Datum
gbt_date_penalty(PG_FUNCTION_ARGS)
{
	dateKEY    *origentry = (dateKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
	dateKEY    *newentry = (dateKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
	float	   *result = (float *) PG_GETARG_POINTER(2);
	int32		diff,
				res;

	diff = DatumGetInt32(DirectFunctionCall2(
											 date_mi,
											 DateADTGetDatum(newentry->upper),
										 DateADTGetDatum(origentry->upper)));

	res = Max(diff, 0);

	diff = DatumGetInt32(DirectFunctionCall2(
											 date_mi,
										   DateADTGetDatum(origentry->lower),
										  DateADTGetDatum(newentry->lower)));

	res += Max(diff, 0);

	*result = 0.0;

	if (res > 0)
	{
		diff = DatumGetInt32(DirectFunctionCall2(
												 date_mi,
										   DateADTGetDatum(origentry->upper),
										 DateADTGetDatum(origentry->lower)));
		*result += FLT_MIN;
		*result += (float) (res / ((double) (res + diff)));
		*result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
	}

	PG_RETURN_POINTER(result);
}
Exemple #21
0
/*
 * pgstrom.psum_x2(numeric)
 */
Datum
pgstrom_partial_sum_x2_numeric(PG_FUNCTION_ARGS)
{
	Datum		value;

	if (!PG_ARGISNULL(0))
		value = PG_GETARG_DATUM(0);	/* a valid numeric value */
	else
		value = DirectFunctionCall3(numeric_in,
									CStringGetDatum("0"),
									ObjectIdGetDatum(InvalidOid),
									Int32GetDatum(-1));
	return DirectFunctionCall2(numeric_mul, value, value);
}
Exemple #22
0
static float
size_box(Datum box)
{
	if (DatumGetPointer(box) != NULL)
	{
		float		size;

		DirectFunctionCall2(rt_box_size,
							box, PointerGetDatum(&size));
		return size;
	}
	else
		return 0.0;
}
Exemple #23
0
Datum
to_tsquery_name(PG_FUNCTION_ARGS)
{
	text	   *name = PG_GETARG_TEXT_P(0);
	Datum		res;

	SET_FUNCOID();
	res = DirectFunctionCall2(to_tsquery,
							  Int32GetDatum(name2id_cfg(name)),
							  PG_GETARG_DATUM(1));

	PG_FREE_IF_COPY(name, 0);
	PG_RETURN_DATUM(res);
}
Exemple #24
0
Datum
ipaddr_cmp(PG_FUNCTION_ARGS)
{
    IP_P ipp1 = PG_GETARG_IP_P(0);
	IP_P ipp2 = PG_GETARG_IP_P(1);
	IP ip1;
	IP ip2;
	int af1 = ip_unpack(ipp1, &ip1);
	int af2 = ip_unpack(ipp2, &ip2);
	int32 retval;

	if (af1 != af2)
	{
		retval = (af1 > af2) ? 1 : -1;
	}
	else
	{
		switch (af1)
		{
			case PGSQL_AF_INET:
				retval = DatumGetInt32(DirectFunctionCall2(ip4_cmp, IP4GetDatum(ip1.ip4), IP4GetDatum(ip2.ip4)));
				break;

			case PGSQL_AF_INET6:
				retval = DatumGetInt32(DirectFunctionCall2(ip6_cmp, IP6PGetDatum(&ip1.ip6), IP6PGetDatum(&ip2.ip6)));
				break;

			default:
				ipaddr_internal_error();
		}
	}

	PG_FREE_IF_COPY(ipp1,0);
	PG_FREE_IF_COPY(ipp2,1);

    PG_RETURN_INT32(retval);
}
Exemple #25
0
static Jsonb *
jnumber_op(PGFunction f, Jsonb *l, Jsonb *r)
{
	FunctionCallInfoData fcinfo;
	JsonbValue *jv;
	Datum		n;

	AssertArg(r != NULL);

	if (!((l == NULL || JB_ROOT_IS_SCALAR(l)) && JB_ROOT_IS_SCALAR(r)))
		ereport_op(f, l, r);

	InitFunctionCallInfoData(fcinfo, NULL, 0, InvalidOid, NULL, NULL);

	if (l != NULL)
	{
		jv = getIthJsonbValueFromContainer(&l->root, 0);
		if (jv->type != jbvNumeric)
			ereport_op(f, l, r);

		fcinfo.arg[fcinfo.nargs] = NumericGetDatum(jv->val.numeric);
		fcinfo.argnull[fcinfo.nargs] = false;
		fcinfo.nargs++;
	}

	jv = getIthJsonbValueFromContainer(&r->root, 0);
	if (jv->type != jbvNumeric)
		ereport_op(f, l, r);

	fcinfo.arg[fcinfo.nargs] = NumericGetDatum(jv->val.numeric);
	fcinfo.argnull[fcinfo.nargs] = false;
	fcinfo.nargs++;

	n = (*f) (&fcinfo);
	if (fcinfo.isnull)
		elog(ERROR, "function %p returned NULL", (void *) f);

	if (f == numeric_power || f == numeric_div)
	{
		int			s;

		s = DatumGetInt32(DirectFunctionCall1(numeric_scale, fcinfo.arg[0])) +
			DatumGetInt32(DirectFunctionCall1(numeric_scale, fcinfo.arg[1]));
		if (s == 0)
			n = DirectFunctionCall2(numeric_trunc, n, 0);
	}

	return numeric_to_jnumber(DatumGetNumeric(n));
}
Exemple #26
0
Datum
gpupreagg_corr_psum_xy(PG_FUNCTION_ARGS)
{
	Assert(PG_NARGS() == 3);
	/* Aggregate Filter */
	if (PG_ARGISNULL(0) || !PG_GETARG_BOOL(0))
		PG_RETURN_NULL();
	/* NULL checks */
	if (PG_ARGISNULL(1) || PG_ARGISNULL(2))
		PG_RETURN_NULL();
	/* calculation of X*X with overflow checks */
	PG_RETURN_DATUM(DirectFunctionCall2(float8mul,
										PG_GETARG_FLOAT8(1),
										PG_GETARG_FLOAT8(2)));
}
Datum
_numeric_weighted_mean_final(PG_FUNCTION_ARGS)
{
	WeightedMeanInternalState *state;
	Datum		total;
	Datum		zero = make_numeric(0);

	state = PG_ARGISNULL(0)
			? NULL
			: (WeightedMeanInternalState *) PG_GETARG_POINTER(0);

	/* No row has ever been processed. */
	if (state == NULL)
		return zero;

	if (DatumGetBool(DirectFunctionCall2(numeric_eq,
										 zero, state->running_weight)))
		total = zero;
	else
		total = DirectFunctionCall2(numeric_div,
									state->running_sum, state->running_weight);

	PG_RETURN_NUMERIC(total);
}
Exemple #28
0
static inline
IP_P
ipaddr_transform_2d(Datum d1, Datum d2, PGFunction ip4func, PGFunction ip6func)
{
    IP_P ipp = DatumGetIP_P(d1);
	IP ip;
	int af = ip_unpack(ipp, &ip);

	switch (af)
	{
		case PGSQL_AF_INET:
			ip.ip4 = DatumGetIP4(DirectFunctionCall2(ip4func, IP4GetDatum(ip.ip4), d2));
			break;

		case PGSQL_AF_INET6:
			ip.ip6 = *(DatumGetIP6P(DirectFunctionCall2(ip6func, IP6PGetDatum(&ip.ip6), d2)));
			break;

		default:
			ipaddr_internal_error();
	}

    return ip_pack(af, &ip);
}
Exemple #29
0
Datum
ipaddr_minus_ipaddr(PG_FUNCTION_ARGS)
{
    Datum minuend = PG_GETARG_DATUM(0);
    Datum subtrahend = PG_GETARG_DATUM(1);
    Datum res;
	IP ip1;
	IP ip2;
	int af1 = ip_unpack(DatumGetIP_P(minuend), &ip1);
	int af2 = ip_unpack(DatumGetIP_P(subtrahend), &ip2);

	if (af1 != af2)
		ereport(ERROR,
				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
				 errmsg("invalid mixing of IP address families")));
				
	switch (af1)
	{
		case PGSQL_AF_INET:
			res = DirectFunctionCall2(numeric_sub,
									  DirectFunctionCall1(ip4_cast_to_numeric,IP4GetDatum(ip1.ip4)),
									  DirectFunctionCall1(ip4_cast_to_numeric,IP4GetDatum(ip2.ip4)));
			break;

		case PGSQL_AF_INET6:
			res = DirectFunctionCall2(numeric_sub,
									  DirectFunctionCall1(ip6_cast_to_numeric,IP6PGetDatum(&ip1.ip6)),
									  DirectFunctionCall1(ip6_cast_to_numeric,IP6PGetDatum(&ip2.ip6)));
			break;

		default:
			ipaddr_internal_error();
	}

    PG_RETURN_DATUM(res);
}
Exemple #30
0
static float8
gbt_ts_dist(const void *a, const void *b)
{
	const Timestamp *aa = (const Timestamp *) a;
	const Timestamp *bb = (const Timestamp *) b;
	Interval   *i;

	if (TIMESTAMP_NOT_FINITE(*aa) || TIMESTAMP_NOT_FINITE(*bb))
		return get_float8_infinity();

	i = DatumGetIntervalP(DirectFunctionCall2(timestamp_mi,
											  TimestampGetDatumFast(*aa),
											  TimestampGetDatumFast(*bb)));
	return (float8) Abs(INTERVAL_TO_SEC(i));
}