aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/selfuncs.c
blob: 8ee93b768d38a8d2e1e93dc9578f1555ae4aea53 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
/*-------------------------------------------------------------------------
 *
 * selfuncs.c--
 *    Selectivity functions for system catalogs and builtin types
 *
 *    These routines are registered in the operator catalog in the
 *    "oprrest" and "oprjoin" attributes.
 *
 *    XXX check all the functions--I suspect them to be 1-based.
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *    $Header: /cvsroot/pgsql/src/backend/utils/adt/selfuncs.c,v 1.1.1.1 1996/07/09 06:22:05 scrappy Exp $
 *
 *-------------------------------------------------------------------------
 */
#include <stdio.h>
#include <string.h>

#include "access/heapam.h"
#include "utils/tqual.h"	/* for NowTimeQual */
#include "fmgr.h"
#include "utils/builtins.h"	/* for textout() prototype 
				  and where the declarations go */
#include "utils/elog.h"
#include "utils/palloc.h"

#include "catalog/catname.h"
#include "utils/syscache.h"
#include "utils/lsyscache.h"	/* for get_oprrest() */
#include "catalog/pg_statistic.h"


/* N is not a valid var/constant or relation id */
#define	NONVALUE(N)	((N) == -1)

/* 
 * generalize the test for functional index selectivity request
 */
#define FunctionalSelectivity(nIndKeys,attNum) (attNum==InvalidAttrNumber)

static int32 getattnvals(Oid relid, AttrNumber attnum);
static void gethilokey(Oid relid, AttrNumber attnum, Oid opid,
		       char **high, char **low);


/*
 *	eqsel		- Selectivity of "=" for any data type.
 */
float64
eqsel(Oid opid,
      Oid relid,
      AttrNumber attno,
      char *value,
      int32 flag)
{
    int32		nvals;
    float64		result;
    
    result = (float64) palloc(sizeof(float64data));
    if (NONVALUE(attno) || NONVALUE(relid))
	*result = 0.1;
    else {
	nvals = getattnvals(relid, (int) attno);
	if (nvals == 0)
	    *result = 0.0;
	else
	    *result = 1.0 / nvals;
    }
    return(result);
}

/*
 *	neqsel		- Selectivity of "!=" for any data type.
 */
float64
neqsel(Oid opid,
       Oid relid,
       AttrNumber attno,
       char *value,
       int32 flag)
{
    float64		result;
    
    result = eqsel(opid, relid, attno, value, flag);
    *result = 1.0 - *result;
    return(result);
}

/*
 *	intltsel	- Selectivity of "<" for integers.
 *			  Should work for both longs and shorts.
 */
float64
intltsel(Oid opid,
	 Oid relid,
	 AttrNumber attno,
	 int32 value,
	 int32 flag)
{
    float64 	result;
    char		*highchar, *lowchar;
    long		val, high, low, top, bottom;
    
    result = (float64) palloc(sizeof(float64data));
    if (NONVALUE(attno) || NONVALUE(relid))
	*result = 1.0 / 3;
    else {
	/* XXX          val = atol(value);*/
	val = value;
	gethilokey(relid, (int) attno, opid, &highchar, &lowchar);
	if (*highchar == 'n' || *lowchar == 'n') {
	    *result = 1.0/3.0;
	    return (result);
	}
	high = atol(highchar);
	low = atol(lowchar);
	if ((flag & SEL_RIGHT && val < low) ||
	    (!(flag & SEL_RIGHT) && val > high)) {
	    int nvals;
	    nvals = getattnvals(relid, (int) attno);
	    if (nvals == 0)
		*result = 1.0 / 3.0;
	    else
		*result = 3.0 / nvals;
	}else {
	    bottom = high - low;
	    if (bottom == 0)
		++bottom;
	    if (flag & SEL_RIGHT)
		top = val - low;
	    else
		top = high - val;
	    if (top > bottom)
		*result = 1.0;
	    else {
		if (top == 0)
		    ++top;
		*result = ((1.0 * top) / bottom);
	    }
	}
    }
    return(result);
}

/*
 *	intgtsel	- Selectivity of ">" for integers.
 *			  Should work for both longs and shorts.
 */
float64
intgtsel(Oid opid,
	 Oid relid,
	 AttrNumber attno,
	 int32 value,
	 int32 flag)
{
    float64		result;
    int		notflag;
    
    if (flag & 0)
	notflag = flag & ~SEL_RIGHT;
    else
	notflag = flag | SEL_RIGHT;
    result = intltsel(opid, relid, attno, value, (int32) notflag);
    return(result);
}

/*
 *	eqjoinsel	- Join selectivity of "="
 */
float64
eqjoinsel(Oid opid,
	  Oid relid1,
	  AttrNumber attno1,
	  Oid relid2,
	  AttrNumber attno2)
{
    float64		result;
    int32		num1, num2, max;
    
    result = (float64) palloc(sizeof(float64data));
    if (NONVALUE(attno1) || NONVALUE(relid1) ||
	NONVALUE(attno2) || NONVALUE(relid2))
	*result = 0.1;
    else {
	num1 = getattnvals(relid1, (int) attno1);
	num2 = getattnvals(relid2, (int) attno2);
	max = (num1 > num2) ? num1 : num2;
	if (max == 0)
	    *result = 1.0;
	else
	    *result = 1.0 / max;
    }
    return(result);
}

/*
 *	neqjoinsel	- Join selectivity of "!="
 */
float64
neqjoinsel(Oid opid,
	   Oid relid1,
	   AttrNumber attno1,
	   Oid relid2,
	   AttrNumber attno2)
{
    float64		result;
    
    result = eqjoinsel(opid, relid1, attno1, relid2, attno2);
    *result = 1.0 - *result;
    return(result);
}

/*
 *	intltjoinsel	- Join selectivity of "<"
 */
float64
intltjoinsel(Oid opid,
	     Oid relid1,
	     AttrNumber attno1,
	     Oid relid2,
	     AttrNumber attno2)
{
    float64	result;
    
    result = (float64) palloc(sizeof(float64data));
    *result = 1.0 / 3.0;
    return(result);
}

/*
 *	intgtjoinsel	- Join selectivity of ">"
 */
float64
intgtjoinsel(Oid opid,
	     Oid relid1,
	     AttrNumber attno1,
	     Oid relid2,
	     AttrNumber attno2)
{
    float64	result;
    
    result = (float64) palloc(sizeof(float64data));
    *result = 1.0 / 3.0;
    return(result);
}

/*
 *	getattnvals	- Retrieves the number of values within an attribute.
 *
 *	Note:
 *		getattnvals and gethilokey both currently use keyed
 *		relation scans and amgetattr.  Alternatively,
 *		the relation scan could be non-keyed and the tuple
 *		returned could be cast (struct X *) tuple + tuple->t_hoff.
 *		The first method is good for testing the implementation,
 *		but the second may ultimately be faster?!?  In any case,
 *		using the cast instead of amgetattr would be
 *		more efficient.  However, the cast will not work
 *		for gethilokey which accesses stahikey in struct statistic.
 */
static int32
getattnvals(Oid relid, AttrNumber attnum)
{
    HeapTuple	atp;
    int		nvals;
    
    atp = SearchSysCacheTuple(ATTNUM, 
			      ObjectIdGetDatum(relid), 
			      Int16GetDatum(attnum),
			      0,0);
    if (!HeapTupleIsValid(atp)) {
	elog(WARN, "getattnvals: no attribute tuple %d %d",
	     relid, attnum);
	return(0);
    }
    nvals = ((AttributeTupleForm ) GETSTRUCT(atp))->attnvals;
    if (nvals > 0) return(nvals);
    
    atp = SearchSysCacheTuple(RELOID, ObjectIdGetDatum(relid),
			      0,0,0);
    /* XXX -- use number of tuples as number of distinctive values
       just for now, in case number of distinctive values is
       not cached */
    if (!HeapTupleIsValid(atp)) {
	elog(WARN, "getattnvals: no relation tuple %d", relid);
	return(0);
    }
    nvals = ((Form_pg_class) GETSTRUCT(atp))->reltuples;
    return(nvals);
}

/*
 *	gethilokey	- Returns a pointer to strings containing
 *			  the high and low keys within an attribute.
 *
 *	Currently returns "0", and "0" in high and low if the statistic
 *	catalog does not contain the proper tuple.  Eventually, the
 *	statistic demon should have the tuple maintained, and it should
 *	elog() if the tuple is missing.
 *
 *	XXX Question: is this worth sticking in the catalog caches,
 *	    or will this get invalidated too often?
 */
static void
gethilokey(Oid relid,
	   AttrNumber attnum,
	   Oid opid,
	   char **high,
	   char **low)
{
    register Relation	rdesc;
    register HeapScanDesc	sdesc;
    static ScanKeyData	key[3] = {
	{ 0, Anum_pg_statistic_starelid, F_OIDEQ },
	{ 0, Anum_pg_statistic_staattnum, F_INT2EQ },
	{ 0, Anum_pg_statistic_staop, F_OIDEQ }
    };
    bool		isnull;
    HeapTuple		tuple;
    
    rdesc = heap_openr(StatisticRelationName);

    key[0].sk_argument = ObjectIdGetDatum(relid);
    key[1].sk_argument = Int16GetDatum((int16) attnum);
    key[2].sk_argument = ObjectIdGetDatum(opid);
    sdesc = heap_beginscan(rdesc, 0, NowTimeQual, 3, key);
    tuple = heap_getnext(sdesc, 0, (Buffer *) NULL);
    if (!HeapTupleIsValid(tuple)) {
	*high = "n";
	*low = "n";
	/* XXX 		elog(WARN, "gethilokey: statistic tuple not found");*/
	return;
    }
    *high = textout((struct varlena *)
		    heap_getattr(tuple,
				 InvalidBuffer,
				 Anum_pg_statistic_stahikey,
				 RelationGetTupleDescriptor(rdesc),
				 &isnull));
    if (isnull)
	elog(DEBUG, "gethilokey: high key is null");
    *low = textout((struct varlena *)
		   heap_getattr(tuple,
				InvalidBuffer,
				Anum_pg_statistic_stalokey,
				RelationGetTupleDescriptor(rdesc),
				&isnull));
    if (isnull)
	elog(DEBUG, "gethilokey: low key is null");
    heap_endscan(sdesc);
    heap_close(rdesc);
}

float64
btreesel(Oid operatorObjectId,
	 Oid indrelid,
	 AttrNumber attributeNumber,
	 char *constValue,
	 int32 constFlag,
	 int32 nIndexKeys,
	 Oid indexrelid)
{
    float64 result;
    float64data resultData;
    
    if (FunctionalSelectivity(nIndexKeys, attributeNumber)) {
	/*
	 * Need to call the functions selectivity
	 * function here.  For now simply assume it's
	 * 1/3 since functions don't currently
	 * have selectivity functions
	 */
	resultData = 1.0 / 3.0;
	result = &resultData;
    }
    else {
	result = (float64)fmgr(get_oprrest (operatorObjectId),
			       (char*)operatorObjectId,
			       (char*)indrelid,
			       (char*)attributeNumber,
			       (char*)constValue,
			       (char*)constFlag,
			       NULL);
    }
    
    if (!PointerIsValid(result))
	elog(WARN, "Btree Selectivity: bad pointer");
    if (*result < 0.0 || *result > 1.0)
	elog(WARN, "Btree Selectivity: bad value %lf", *result);
    
    return(result);
}

float64
btreenpage(Oid operatorObjectId,
	   Oid indrelid,
	   AttrNumber attributeNumber,
	   char *constValue,
	   int32 constFlag,
	   int32 nIndexKeys,
	   Oid indexrelid)
{
    float64 temp, result;
    float64data tempData;
    HeapTuple atp;
    int npage;
    
    if (FunctionalSelectivity(nIndexKeys, attributeNumber)) {
	/*
	 * Need to call the functions selectivity
	 * function here.  For now simply assume it's
	 * 1/3 since functions don't currently
	 * have selectivity functions
	 */
	tempData = 1.0 / 3.0;
	temp = &tempData;
    }
    else {
	temp = (float64)fmgr(get_oprrest (operatorObjectId),
			     (char*)operatorObjectId,
			     (char*)indrelid,
			     (char*)attributeNumber,
			     (char*)constValue,
			     (char*)constFlag,
			     NULL);
    }
    atp = SearchSysCacheTuple(RELOID, 
			      ObjectIdGetDatum(indexrelid),
			      0,0,0);
    if (!HeapTupleIsValid(atp)) {
	elog(WARN, "btreenpage: no index tuple %d", indexrelid);
	return(0);
    }
    
    npage = ((Form_pg_class) GETSTRUCT(atp))->relpages;
    result = (float64)palloc(sizeof(float64data));
    *result = *temp * npage;
    return(result);
}

float64
hashsel(Oid operatorObjectId,
	Oid indrelid,
	AttrNumber attributeNumber,
	char *constValue,
	int32 constFlag,
	int32 nIndexKeys,
	Oid indexrelid)
{
    
    float64 result;
    float64data resultData;
    HeapTuple atp;
    int ntuples;
    
    if (FunctionalSelectivity(nIndexKeys, attributeNumber)) {
	/*
	 * Need to call the functions selectivity
	 * function here.  For now simply use 1/Number of Tuples
	 * since functions don't currently
	 * have selectivity functions
	 */
	
	atp = SearchSysCacheTuple(RELOID, ObjectIdGetDatum(indexrelid),
				  0,0,0);
	if (!HeapTupleIsValid(atp)) {
	    elog(WARN, "hashsel: no index tuple %d", indexrelid);
	    return(0);
	}
	ntuples = ((Form_pg_class) GETSTRUCT(atp))->reltuples;
	if (ntuples > 0) {
	    resultData =  1.0 / (float64data) ntuples;
	}
	else {
	    resultData = (float64data) (1.0 / 100.0);
	}
	result = &resultData;
	
    }
    else {
	result = (float64)fmgr(get_oprrest (operatorObjectId),
			       (char*)operatorObjectId,
			       (char*)indrelid,
			       (char*)attributeNumber,
			       (char*)constValue,
			       (char*)constFlag,
			       NULL);
    }
    
    if (!PointerIsValid(result))
	elog(WARN, "Hash Table Selectivity: bad pointer");
    if (*result < 0.0 || *result > 1.0)
	elog(WARN, "Hash Table Selectivity: bad value %lf", *result);
    
    return(result);
    
    
}

float64
hashnpage(Oid operatorObjectId,
	  Oid indrelid,
	  AttrNumber attributeNumber,
	  char *constValue,
	  int32 constFlag,
	  int32 nIndexKeys,
	  Oid indexrelid)
{
    float64 temp, result;
    float64data tempData;
    HeapTuple atp;
    int npage;
    int ntuples;
    
    atp = SearchSysCacheTuple(RELOID, ObjectIdGetDatum(indexrelid),
			      0,0,0);
    if (!HeapTupleIsValid(atp)) {
	elog(WARN, "hashsel: no index tuple %d", indexrelid);
	return(0);
    }
    
    
    if (FunctionalSelectivity(nIndexKeys, attributeNumber)) {
	/*
	 * Need to call the functions selectivity
	 * function here.  For now, use 1/Number of Tuples
	 * since functions don't currently
	 * have selectivity functions
	 */
	
	ntuples = ((Form_pg_class) GETSTRUCT(atp))->reltuples;
	if (ntuples > 0) {
	    tempData =  1.0 / (float64data) ntuples;
	}
	else {
	    tempData = (float64data) (1.0 / 100.0);
	}
	temp = &tempData;
	
    }
    else {
	temp = (float64)fmgr(get_oprrest (operatorObjectId),
			     (char*)operatorObjectId,
			     (char*)indrelid,
			     (char*)attributeNumber,
			     (char*)constValue,
			     (char*)constFlag,
			     NULL);
    }
    
    npage = ((Form_pg_class) GETSTRUCT(atp))->relpages;
    result = (float64)palloc(sizeof(float64data));
    *result = *temp * npage;
    return(result);
}


float64
rtsel(Oid operatorObjectId,
      Oid indrelid,
      AttrNumber attributeNumber,
      char *constValue,
      int32 constFlag,
      int32 nIndexKeys,
      Oid indexrelid)
{
    return (btreesel(operatorObjectId, indrelid, attributeNumber,
		     constValue, constFlag, nIndexKeys, indexrelid));
}

float64
rtnpage(Oid operatorObjectId,
	Oid indrelid,
	AttrNumber attributeNumber,
	char *constValue,
	int32 constFlag,
	int32 nIndexKeys,
	Oid indexrelid)
{
    return (btreenpage(operatorObjectId, indrelid, attributeNumber,
		       constValue, constFlag, nIndexKeys, indexrelid));
}