Exemplo n.º 1
0
END_TEST

START_TEST(test_merge)
{
	TypeCacheEntry *typ = get_int8_type();
	FSS *fss1 = FSSCreate(K, typ);
	FSS *fss2 = FSSCreate(K, typ);
	int i, j;
	Datum *values1, *values2;
	uint64_t *freqs1, *freqs2;
	int soft_errors;

	for (j = 0; j < 10; j++)
	{
		FSS *tmp = FSSCreate(K, typ);

		for (i = 0; i < NUM_ITEMS; i++)
		{
			int value = 10 * gaussian();
			value %= 500;
			FSSIncrement(fss1, value);
			FSSIncrement(tmp, value);
			assert_sorted(fss1);
			assert_sorted(tmp);
		}

		fss2 = FSSMerge(fss2, tmp);
		FSSDestroy(tmp);
	}

	values1 = FSSTopK(fss1, K, NULL);
	freqs1 = FSSTopKCounts(fss1, K, NULL);
	values2 = FSSTopK(fss2, K, NULL);
	freqs2 = FSSTopKCounts(fss2, K, NULL);
	soft_errors = 0;

	for (i = 0; i < 10; i++)
	{
		int value1 = values1[i];
		int value2 = values2[i];

		ck_assert(value1 == value2);

		if (abs(freqs1[i] - freqs2[i]) > 10)
			soft_errors++;
	}

	ck_assert(soft_errors < 3);
}
Exemplo n.º 2
0
Datum
fss_agg_trans(PG_FUNCTION_ARGS)
{
	MemoryContext old;
	MemoryContext context;
	FSS *state;
	Datum incoming = PG_GETARG_DATUM(1);

	if (!AggCheckCallContext(fcinfo, &context))
		elog(ERROR, "fss_agg_trans called in non-aggregate context");

	old = MemoryContextSwitchTo(context);

	if (PG_ARGISNULL(0))
	{
		uint16_t k = PG_GETARG_INT64(2);
		Oid type = AggGetInitialArgType(fcinfo);
		TypeCacheEntry *typ = lookup_type_cache(type, 0);
		fcinfo->flinfo->fn_extra = typ;
		state = FSSCreate(k, typ);
	}
	else
		state = fss_fix_ptrs(PG_GETARG_VARLENA_P(0));

	FSSIncrement(state, incoming);

	MemoryContextSwitchTo(old);

	PG_RETURN_POINTER(state);
}
Exemplo n.º 3
0
Datum
fss_empty(PG_FUNCTION_ARGS)
{
	Oid typid = PG_GETARG_OID(0);
	uint16_t k = PG_GETARG_INT64(1);
	FSS *fss = FSSCreate(k, lookup_type_cache(typid, 0));
	PG_RETURN_POINTER(fss);
}
Exemplo n.º 4
0
Datum
fss_increment(PG_FUNCTION_ARGS)
{
	FSS *fss;
	TypeCacheEntry *typ = lookup_type_cache(get_fn_expr_argtype(fcinfo->flinfo, 1), 0);

	if (PG_ARGISNULL(0))
		fss = FSSCreate(DEFAULT_K, typ);
	else
	{
		fss = fss_fix_ptrs(PG_GETARG_VARLENA_P(0));

		if (fss->typ.typoid != typ->type_id)
			elog(ERROR, "type mismatch for incoming value");
	}

	FSSIncrement(fss, PG_GETARG_DATUM(1));

	PG_RETURN_POINTER(fss);
}
Exemplo n.º 5
0
END_TEST

START_TEST(test_weighted)
{
	TypeCacheEntry *typ = get_int8_type();
	FSS *fss = FSSCreate(K, typ);
	int i;
	int *counts = palloc0(sizeof(uint64_t) * 1000);
	Datum *values;
	uint64_t *freqs;
	int min_freq = NUM_ITEMS / fss->m;

	for (i = 0; i <= min_freq; i++)
	{
		FSSIncrementWeighted(fss, 1, 10);
		FSSIncrementWeighted(fss, 2, 20);
		FSSIncrementWeighted(fss, 3, 30);
		counts[1] += 10;
		counts[2] += 20;
		counts[3] += 30;
		assert_sorted(fss);
	}

	for (i = 0; i < NUM_ITEMS - (2 * min_freq); i++)
	{
		int value = (int) uniform() % 500 + 4;
		FSSIncrementWeighted(fss, value, 1);
		counts[value]++;
		assert_sorted(fss);
	}

	values = FSSTopK(fss, K, NULL);
	freqs = FSSTopKCounts(fss, K, NULL);

	for (i = 0; i < 3; i++)
	{
		int value = values[i];
		ck_assert(value == 1 || value == 2 || value == 3);
		ck_assert(abs(freqs[i] - counts[value]) == 0);
	}
}