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.17 1999/07/15 22:39:59 momjian Exp $
11 12 13
 *
 *-------------------------------------------------------------------------
 */
14
#include <sys/types.h>
15
#include <sys/file.h>
16
#include <time.h>
17 18 19 20
#include "postgres.h"
#include "utils/builtins.h"

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

33 34 35
}

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

48 49 50 51
}

/*
 * oidrand (oid o, int4 X)-
52 53 54 55
 *	  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;
56 57
 *
 * Example use:
58
 *	   select * from TEMP where oidrand(TEMP.oid, 10)
59 60 61
 * will return about 1/10 of the tuples in TEMP
 *
 */
62 63 64

static bool random_initialized = false;

65
bool
66 67
oidrand(Oid o, int32 X)
{
68
	bool		result;
69

70 71
	if (X == 0)
		return true;
72

73
	/*
74 75 76
	 * 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.
77 78 79
	 */
	if (!random_initialized)
	{
80
		srandom((unsigned int) time(NULL));
81 82 83
		random_initialized = true;
	}

84 85
	result = (random() % X == 0);
	return result;
86 87 88 89
}

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



int32
userfntest(int i)
{
106
	return i;
107
}