aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/misc.c
blob: af08bda8789f4ecec850f4e1a64e1fe76b6aef39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*-------------------------------------------------------------------------
 *
 * misc.c
 *
 *
 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  $Header: /cvsroot/pgsql/src/backend/utils/adt/misc.c,v 1.22 2001/03/22 03:59:51 momjian Exp $
 *
 *-------------------------------------------------------------------------
 */
#include <sys/types.h>
#include <sys/file.h>
#include <time.h>

#include "postgres.h"

#include "utils/builtins.h"


/*
 * Check if data is Null
 */
Datum
nullvalue(PG_FUNCTION_ARGS)
{
	if (PG_ARGISNULL(0))
		PG_RETURN_BOOL(true);
	PG_RETURN_BOOL(false);
}

/*
 * Check if data is not Null
 */
Datum
nonnullvalue(PG_FUNCTION_ARGS)
{
	if (PG_ARGISNULL(0))
		PG_RETURN_BOOL(false);
	PG_RETURN_BOOL(true);
}

/*
 * oidrand (oid o, int4 X)-
 *	  Takes in an oid and a int4 X, and will return 'true' about 1/X of
 *	  the time.  If X == 0, this will always return true.
 *	  Useful for doing random sampling or subsetting.
 *
 * Example use:
 *	   select * from TEMP where oidrand(TEMP.oid, 10)
 * will return about 1/10 of the tuples in TEMP
 *
 * NOTE: the OID input is not used at all.	It is there just because of
 * an old optimizer bug: a qual expression containing no variables was
 * mistakenly assumed to be a constant.  Pretending to access the row's OID
 * prevented the optimizer from treating the oidrand() result as constant.
 */

static bool random_initialized = false;

Datum
oidrand(PG_FUNCTION_ARGS)
{
#ifdef NOT_USED
	Oid			o = PG_GETARG_OID(0);

#endif
	int32		X = PG_GETARG_INT32(1);
	bool		result;

	if (X == 0)
		PG_RETURN_BOOL(true);

	/*
	 * 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;
	}

	result = (random() % X == 0);
	PG_RETURN_BOOL(result);
}

/*
   oidsrand(int32 X) -
	  seeds the random number generator
	  always returns true
*/
Datum
oidsrand(PG_FUNCTION_ARGS)
{
	int32		X = PG_GETARG_INT32(0);

	srandom((unsigned int) X);
	random_initialized = true;
	PG_RETURN_BOOL(true);
}


Datum
userfntest(PG_FUNCTION_ARGS)
{
	int32		i = PG_GETARG_INT32(0);

	PG_RETURN_INT32(i);
}