misc.c 2.0 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * misc.c
4
 *
5
 *
B
Add:  
Bruce Momjian 已提交
6 7
 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
 * Portions Copyright (c) 1994, Regents of the University of California
8 9 10
 *
 *
 * IDENTIFICATION
B
Add:  
Bruce Momjian 已提交
11
 *	  $Header: /cvsroot/pgsql/src/backend/utils/adt/misc.c,v 1.18 2000/01/26 05:57:14 momjian Exp $
12 13 14
 *
 *-------------------------------------------------------------------------
 */
15
#include <sys/types.h>
16
#include <sys/file.h>
17
#include <time.h>
18 19 20 21
#include "postgres.h"
#include "utils/builtins.h"

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

34 35 36
}

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

49 50 51 52
}

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

static bool random_initialized = false;

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

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

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

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

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



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