1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* misc.c
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2001-01-24 20:43:33 +01:00
|
|
|
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2001-10-25 07:50:21 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/utils/adt/misc.c,v 1.23 2001/10/25 05:49:44 momjian Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
1997-07-28 02:57:08 +02:00
|
|
|
#include <sys/types.h>
|
1996-07-09 08:22:35 +02:00
|
|
|
#include <sys/file.h>
|
1998-06-09 21:20:59 +02:00
|
|
|
#include <time.h>
|
2000-06-05 09:29:25 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "postgres.h"
|
2000-06-05 09:29:25 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "utils/builtins.h"
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
|
|
|
|
/*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Check if data is Null
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
nullvalue(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
if (PG_ARGISNULL(0))
|
|
|
|
PG_RETURN_BOOL(true);
|
|
|
|
PG_RETURN_BOOL(false);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
/*
|
|
|
|
* Check if data is not Null
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
nonnullvalue(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
if (PG_ARGISNULL(0))
|
|
|
|
PG_RETURN_BOOL(false);
|
|
|
|
PG_RETURN_BOOL(true);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* oidrand (oid o, int4 X)-
|
2000-08-07 02:51:42 +02:00
|
|
|
* 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.
|
1997-09-07 07:04:48 +02:00
|
|
|
* Useful for doing random sampling or subsetting.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* Example use:
|
1997-09-07 07:04:48 +02:00
|
|
|
* select * from TEMP where oidrand(TEMP.oid, 10)
|
1996-07-09 08:22:35 +02:00
|
|
|
* will return about 1/10 of the tuples in TEMP
|
|
|
|
*
|
2001-03-22 05:01:46 +01:00
|
|
|
* NOTE: the OID input is not used at all. It is there just because of
|
2000-08-07 02:51:42 +02:00
|
|
|
* 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.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1998-06-09 21:20:59 +02:00
|
|
|
|
|
|
|
static bool random_initialized = false;
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
oidrand(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
#ifdef NOT_USED
|
|
|
|
Oid o = PG_GETARG_OID(0);
|
|
|
|
#endif
|
|
|
|
int32 X = PG_GETARG_INT32(1);
|
1997-09-08 04:41:22 +02:00
|
|
|
bool result;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (X == 0)
|
2000-06-05 09:29:25 +02:00
|
|
|
PG_RETURN_BOOL(true);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1998-06-09 21:20:59 +02:00
|
|
|
/*
|
1998-09-01 06:40:42 +02:00
|
|
|
* 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.
|
1998-06-09 21:20:59 +02:00
|
|
|
*/
|
|
|
|
if (!random_initialized)
|
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
srandom((unsigned int) time(NULL));
|
1998-06-09 21:20:59 +02:00
|
|
|
random_initialized = true;
|
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
result = (random() % X == 0);
|
2000-06-05 09:29:25 +02:00
|
|
|
PG_RETURN_BOOL(result);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
oidsrand(int32 X) -
|
1997-09-07 07:04:48 +02:00
|
|
|
seeds the random number generator
|
2000-06-05 09:29:25 +02:00
|
|
|
always returns true
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
oidsrand(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
int32 X = PG_GETARG_INT32(0);
|
|
|
|
|
2000-08-07 02:51:42 +02:00
|
|
|
srandom((unsigned int) X);
|
1998-06-09 21:20:59 +02:00
|
|
|
random_initialized = true;
|
2000-06-05 09:29:25 +02:00
|
|
|
PG_RETURN_BOOL(true);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
userfntest(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
int32 i = PG_GETARG_INT32(0);
|
|
|
|
|
|
|
|
PG_RETURN_INT32(i);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|