misc.c 2.0 KB
Newer Older
1 2 3
/*-------------------------------------------------------------------------
 *
 * misc.c--
4
 *
5 6 7 8 9
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
10
 *	  $Header: /cvsroot/pgsql/src/backend/utils/adt/misc.c,v 1.13 1998/06/09 19:20:59 momjian Exp $
11 12 13
 *
 *-------------------------------------------------------------------------
 */
14
#include <sys/types.h>
15
#include <sys/file.h>
16
#include <time.h>
17 18 19 20 21 22
#include "postgres.h"
#include "utils/datum.h"
#include "catalog/pg_type.h"
#include "utils/builtins.h"

/*-------------------------------------------------------------------------
23
 * Check if data is Null
24 25
 */
bool
26
nullvalue(Datum value, bool *isNull)
27
{
28 29 30 31 32 33 34
	if (*isNull)
	{
		*isNull = false;
		return (true);
	}
	return (false);

35 36 37
}

/*----------------------------------------------------------------------*
38
 *	   check if data is not Null										*
39 40
 *--------------------------------------------------------------------- */
bool
41
nonnullvalue(Datum value, bool *isNull)
42
{
43 44 45 46 47 48 49
	if (*isNull)
	{
		*isNull = false;
		return (false);
	}
	return (true);

50 51 52 53
}

/*
 * oidrand (oid o, int4 X)-
54 55 56 57
 *	  takes in an oid and a int4 X, and will return 'true'
 *	about 1/X of the time.
 *	  Useful for doing random sampling or subsetting.
 *	if X == 0, this will always return true;
58 59
 *
 * Example use:
60
 *	   select * from TEMP where oidrand(TEMP.oid, 10)
61 62 63
 * will return about 1/10 of the tuples in TEMP
 *
 */
64 65 66

static bool random_initialized = false;

67
bool
68 69
oidrand(Oid o, int32 X)
{
70
	bool		result;
71

72 73
	if (X == 0)
		return true;
74

75 76 77 78 79 80 81 82 83 84 85
	/*
	 *	We do this because the cancel key is actually a random, so we don't
	 *	want them to be able to request random numbers using our postmaster
	 *	seeded value.
	 */
	if (!random_initialized)
	{
		srandom((unsigned int)time(NULL));
		random_initialized = true;
	}

86 87
	result = (random() % X == 0);
	return result;
88 89 90 91
}

/*
   oidsrand(int32 X) -
92 93 94
	  seeds the random number generator
	  always return true
*/
95 96 97
bool
oidsrand(int32 X)
{
98
	srand(X);
99
	random_initialized = true;
100
	return true;
101 102 103 104 105 106 107
}



int32
userfntest(int i)
{
108
	return (i);
109
}