4 #include "access/genam.h"
5 #include "access/heapam.h"
6 #include "access/htup_details.h"
7 #include "access/nbtree.h"
8 #include "catalog/indexing.h"
9 #include "catalog/pg_am.h"
10 #include "catalog/pg_amproc.h"
11 #include "catalog/pg_cast.h"
12 #include "catalog/pg_opclass.h"
13 #include "catalog/pg_type.h"
14 #include "executor/spi.h"
15 #include "utils/catcache.h"
16 #include "utils/fmgroids.h"
17 #include "utils/lsyscache.h"
18 #include "utils/memutils.h"
19 #if (PG_VERSION_NUM < 120000)
20 #include "utils/tqual.h"
22 #include "utils/syscache.h"
23 #include "utils/typcache.h"
27 #if (PG_VERSION_NUM >= 90400)
30 #define SNAPSHOT SnapshotNow
35 getDefaultOpclass(Oid amoid, Oid typid)
41 Oid opclassOid = InvalidOid;
43 heapRel = heap_open(OperatorClassRelationId, AccessShareLock);
46 Anum_pg_opclass_opcmethod,
47 BTEqualStrategyNumber, F_OIDEQ,
48 ObjectIdGetDatum(amoid));
50 scan = systable_beginscan(heapRel,
51 OpclassAmNameNspIndexId, true,
54 while (HeapTupleIsValid((tuple = systable_getnext(scan))))
56 Form_pg_opclass opclass = (Form_pg_opclass)GETSTRUCT(tuple);
58 if ( opclass->opcintype == typid && opclass->opcdefault )
60 if ( OidIsValid(opclassOid) )
61 elog(ERROR, "Ambiguous opclass for type %u (access method %u)", typid, amoid);
62 #if (PG_VERSION_NUM >= 120000)
63 opclassOid = opclass->oid;
65 opclassOid = HeapTupleGetOid(tuple);
70 systable_endscan(scan);
71 heap_close(heapRel, AccessShareLock);
77 getAMProc(Oid amoid, Oid typid)
79 Oid opclassOid = getDefaultOpclass(amoid, typid);
80 Oid procOid = InvalidOid;
87 if ( !OidIsValid(opclassOid) )
89 typid = getBaseType(typid);
90 opclassOid = getDefaultOpclass(amoid, typid);
93 if ( !OidIsValid(opclassOid) )
99 * Search binary-coercible type
101 #ifdef SearchSysCacheList1
102 catlist = SearchSysCacheList1(CASTSOURCETARGET,
103 ObjectIdGetDatum(typid));
105 catlist = SearchSysCacheList(CASTSOURCETARGET, 1,
106 ObjectIdGetDatum(typid),
110 for (i = 0; i < catlist->n_members; i++)
112 HeapTuple tuple = &catlist->members[i]->tuple;
113 Form_pg_cast castForm = (Form_pg_cast)GETSTRUCT(tuple);
115 if ( castForm->castfunc == InvalidOid && castForm->castcontext == COERCION_CODE_IMPLICIT )
117 typid = castForm->casttarget;
118 opclassOid = getDefaultOpclass(amoid, typid);
119 if( OidIsValid(opclassOid) )
124 ReleaseSysCacheList(catlist);
127 if ( !OidIsValid(opclassOid) )
130 opfamilyOid = get_opclass_family(opclassOid);
132 heapRel = heap_open(AccessMethodProcedureRelationId, AccessShareLock);
133 ScanKeyInit(&skey[0],
134 Anum_pg_amproc_amprocfamily,
135 BTEqualStrategyNumber, F_OIDEQ,
136 ObjectIdGetDatum(opfamilyOid));
137 ScanKeyInit(&skey[1],
138 Anum_pg_amproc_amproclefttype,
139 BTEqualStrategyNumber, F_OIDEQ,
140 ObjectIdGetDatum(typid));
141 ScanKeyInit(&skey[2],
142 Anum_pg_amproc_amprocrighttype,
143 BTEqualStrategyNumber, F_OIDEQ,
144 ObjectIdGetDatum(typid));
145 #if PG_VERSION_NUM >= 90200
146 ScanKeyInit(&skey[3],
147 Anum_pg_amproc_amprocnum,
148 BTEqualStrategyNumber, F_OIDEQ,
149 Int32GetDatum(BTORDER_PROC));
152 scan = systable_beginscan(heapRel, AccessMethodProcedureIndexId, true,
154 #if PG_VERSION_NUM >= 90200
160 while (HeapTupleIsValid(tuple = systable_getnext(scan)))
162 Form_pg_amproc amprocform = (Form_pg_amproc) GETSTRUCT(tuple);
168 if ( OidIsValid(procOid) )
169 elog(ERROR,"Ambiguous support function for type %u (opclass %u)", typid, opfamilyOid);
170 procOid = amprocform->amproc;
173 elog(ERROR,"Unsupported access method");
177 systable_endscan(scan);
178 heap_close(heapRel, AccessShareLock);
183 static ProcTypeInfo *cacheProcs = NULL;
184 static int nCacheProcs = 0;
186 #ifndef TupleDescAttr
187 #define TupleDescAttr(tupdesc, i) ((tupdesc)->attrs[(i)])
193 ProcTypeInfo info = malloc(sizeof(ProcTypeInfoData));
196 elog(ERROR, "Can't allocate %u memory", (uint32)sizeof(ProcTypeInfoData));
199 info->typtype = get_typtype(typid);
201 if (info->typtype == 'c')
205 MemoryContext oldcontext;
207 tupdesc = lookup_rowtype_tupdesc(typid, -1);
209 if (tupdesc->natts != 2)
210 elog(ERROR,"Composite type has wrong number of fields");
211 if (TupleDescAttr(tupdesc, 1)->atttypid != FLOAT4OID)
212 elog(ERROR,"Second field of composite type is not float4");
214 oldcontext = MemoryContextSwitchTo(TopMemoryContext);
215 info->tupDesc = CreateTupleDescCopyConstr(tupdesc);
216 MemoryContextSwitchTo(oldcontext);
218 ReleaseTupleDesc(tupdesc);
220 info->cmpFuncOid = getAMProc(BTREE_AM_OID,
221 TupleDescAttr(info->tupDesc, 0)->atttypid);
222 info->hashFuncOid = getAMProc(HASH_AM_OID,
223 TupleDescAttr(info->tupDesc, 0)->atttypid);
227 info->tupDesc = NULL;
230 info->cmpFuncOid = getAMProc(BTREE_AM_OID, typid);
231 info->hashFuncOid = getAMProc(HASH_AM_OID, typid);
234 get_typlenbyvalalign(typid, &info->typlen, &info->typbyval, &info->typalign);
235 info->hashFuncInited = info->cmpFuncInited = false;
242 getFmgrInfoCmp(ProcTypeInfo info)
244 if ( info->cmpFuncInited == false )
246 if ( !OidIsValid(info->cmpFuncOid) )
247 elog(ERROR, "Could not find cmp function for type %u", info->typid);
249 fmgr_info_cxt( info->cmpFuncOid, &info->cmpFunc, TopMemoryContext );
250 info->cmpFuncInited = true;
255 getFmgrInfoHash(ProcTypeInfo info)
257 if ( info->hashFuncInited == false )
259 if ( !OidIsValid(info->hashFuncOid) )
260 elog(ERROR, "Could not find hash function for type %u", info->typid);
262 fmgr_info_cxt( info->hashFuncOid, &info->hashFunc, TopMemoryContext );
263 info->hashFuncInited = true;
268 cmpProcTypeInfo(const void *a, const void *b)
270 ProcTypeInfo av = *(ProcTypeInfo*)a;
271 ProcTypeInfo bv = *(ProcTypeInfo*)b;
273 Assert( av->typid != bv->typid );
275 return ( av->typid > bv->typid ) ? 1 : -1;
281 ProcTypeInfo info = NULL;
283 if ( nCacheProcs == 1 )
285 if ( cacheProcs[0]->typid == typid )
287 /*cacheProcs[0]->hashFuncInited = cacheProcs[0]->cmpFuncInited = false;*/
288 return cacheProcs[0];
291 else if ( nCacheProcs > 1 )
293 ProcTypeInfo *StopMiddle;
294 ProcTypeInfo *StopLow = cacheProcs,
295 *StopHigh = cacheProcs + nCacheProcs;
297 while (StopLow < StopHigh) {
298 StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
301 if ( info->typid == typid )
303 /* info->hashFuncInited = info->cmpFuncInited = false; */
306 else if ( info->typid < typid )
307 StopLow = StopMiddle + 1;
309 StopHigh = StopMiddle;
315 info = fillProcs(typid);
316 if ( nCacheProcs == 0 )
318 cacheProcs = malloc(sizeof(ProcTypeInfo));
321 elog(ERROR, "Can't allocate %u memory", (uint32)sizeof(ProcTypeInfo));
325 cacheProcs[0] = info;
330 ProcTypeInfo *cacheProcsTmp = realloc(cacheProcs, (nCacheProcs+1) * sizeof(ProcTypeInfo));
333 elog(ERROR, "Can't allocate %u memory", (uint32)sizeof(ProcTypeInfo) * (nCacheProcs+1));
336 cacheProcs = cacheProcsTmp;
337 cacheProcs[ nCacheProcs ] = info;
339 qsort(cacheProcs, nCacheProcs, sizeof(ProcTypeInfo), cmpProcTypeInfo);
343 /* info->hashFuncInited = info->cmpFuncInited = false; */
349 * WARNING. Array2SimpleArray* doesn't copy Datum!
352 Array2SimpleArray(ProcTypeInfo info, ArrayType *a)
354 SimpleArray *s = palloc(sizeof(SimpleArray));
359 info = findProcs(ARR_ELEMTYPE(a));
365 deconstruct_array(a, info->typid,
366 info->typlen, info->typbyval, info->typalign,
367 &s->elems, NULL, &s->nelems);
373 deconstructCompositeType(ProcTypeInfo info, Datum in, double *weight)
375 HeapTupleHeader rec = DatumGetHeapTupleHeader(in);
380 /* Build a temporary HeapTuple control structure */
381 tuple.t_len = HeapTupleHeaderGetDatumLength(rec);
382 ItemPointerSetInvalid(&(tuple.t_self));
383 tuple.t_tableOid = InvalidOid;
386 heap_deform_tuple(&tuple, info->tupDesc, values, nulls);
387 if (nulls[0] || nulls[1])
388 elog(ERROR, "Both fields in composite type could not be NULL");
391 *weight = DatumGetFloat4(values[1]);
396 cmpArrayElem(const void *a, const void *b, void *arg)
398 ProcTypeInfo info = (ProcTypeInfo)arg;
402 return DatumGetInt32( FCall2( &info->cmpFunc,
403 deconstructCompositeType(info, *(Datum*)a, NULL),
404 deconstructCompositeType(info, *(Datum*)b, NULL) ) );
406 return DatumGetInt32( FCall2( &info->cmpFunc,
407 *(Datum*)a, *(Datum*)b ) );
411 Array2SimpleArrayS(ProcTypeInfo info, ArrayType *a)
413 SimpleArray *s = Array2SimpleArray(info, a);
417 getFmgrInfoCmp(s->info);
419 qsort_arg(s->elems, s->nelems, sizeof(Datum), cmpArrayElem, s->info);
425 typedef struct cmpArrayElemData {
432 cmpArrayElemArg(const void *a, const void *b, void *arg)
434 cmpArrayElemData *data = (cmpArrayElemData*)arg;
437 if (data->info->tupDesc)
438 res = DatumGetInt32( FCall2( &data->info->cmpFunc,
439 deconstructCompositeType(data->info, *(Datum*)a, NULL),
440 deconstructCompositeType(data->info, *(Datum*)b, NULL) ) );
442 res = DatumGetInt32( FCall2( &data->info->cmpFunc,
443 *(Datum*)a, *(Datum*)b ) );
446 data->hasDuplicate = true;
452 * Uniquefy array and calculate TF. Although
453 * result doesn't depend on normalization, we
454 * normalize TF by length array to have possiblity
455 * to limit estimation for index support.
457 * Cache signals of needing of TF caclulation
461 Array2SimpleArrayU(ProcTypeInfo info, ArrayType *a, void *cache)
463 SimpleArray *s = Array2SimpleArray(info, a);
464 StatElem *stat = NULL;
466 if ( s->nelems > 0 && cache )
468 s->df = palloc(sizeof(double) * s->nelems);
469 s->df[0] = 1.0; /* init */
474 cmpArrayElemData data;
477 getFmgrInfoCmp(s->info);
479 data.hasDuplicate = false;
481 qsort_arg(s->elems, s->nelems, sizeof(Datum), cmpArrayElemArg, &data);
483 if ( data.hasDuplicate )
491 data = tmp = dr = s->elems;
493 while (tmp - data < num)
495 cmp = (tmp == dr) ? 0 : cmpArrayElem(tmp, dr, s->info);
500 s->df[ dr - data ] = 1.0;
505 s->df[ dr - data ] += 1.0;
510 s->nelems = dr + 1 - s->elems;
514 int tfm = getTFMethod();
516 for(i=0;i<s->nelems;i++)
518 stat = fingArrayStat(cache, s->info->typid, s->elems[i], stat);
524 s->df[i] = (1.0 + log( s->df[i] ));
526 s->df[i] *= stat->idf;
529 s->df[i] = stat->idf;
532 elog(ERROR,"Unknown TF method: %d", tfm);
537 s->df[i] = 0.0; /* unknown word */
544 for(i=0;i<s->nelems;i++)
546 stat = fingArrayStat(cache, s->info->typid, s->elems[i], stat);
548 s->df[i] = stat->idf;
554 else if (s->nelems > 0 && cache)
556 stat = fingArrayStat(cache, s->info->typid, s->elems[0], stat);
558 s->df[0] = stat->idf;
567 numOfIntersect(SimpleArray *a, SimpleArray *b)
571 Datum *aptr = a->elems,
573 ProcTypeInfo info = a->info;
575 Assert( a->info->typid == b->info->typid );
577 getFmgrInfoCmp(info);
579 while( aptr - a->elems < a->nelems && bptr - b->elems < b->nelems )
581 cmp = cmpArrayElem(aptr, bptr, info);
598 TFIDFSml(SimpleArray *a, SimpleArray *b)
601 Datum *aptr = a->elems,
603 ProcTypeInfo info = a->info;
605 double suma = 0.0, sumb = 0.0;
607 Assert( a->info->typid == b->info->typid );
611 getFmgrInfoCmp(info);
613 while( aptr - a->elems < a->nelems && bptr - b->elems < b->nelems )
615 cmp = cmpArrayElem(aptr, bptr, info);
618 suma += a->df[ aptr - a->elems ] * a->df[ aptr - a->elems ];
623 sumb += b->df[ bptr - b->elems ] * b->df[ bptr - b->elems ];
628 res += a->df[ aptr - a->elems ] * b->df[ bptr - b->elems ];
629 suma += a->df[ aptr - a->elems ] * a->df[ aptr - a->elems ];
630 sumb += b->df[ bptr - b->elems ] * b->df[ bptr - b->elems ];
637 * Compute last elements
639 while( aptr - a->elems < a->nelems )
641 suma += a->df[ aptr - a->elems ] * a->df[ aptr - a->elems ];
645 while( bptr - b->elems < b->nelems )
647 sumb += b->df[ bptr - b->elems ] * b->df[ bptr - b->elems ];
651 if ( suma > 0.0 && sumb > 0.0 )
652 res = res / sqrt( suma * sumb );
660 PG_FUNCTION_INFO_V1(arraysml);
661 Datum arraysml(PG_FUNCTION_ARGS);
663 arraysml(PG_FUNCTION_ARGS)
666 SimpleArray *sa, *sb;
668 fcinfo->flinfo->fn_extra = SearchArrayCache(
669 fcinfo->flinfo->fn_extra,
670 fcinfo->flinfo->fn_mcxt,
671 PG_GETARG_DATUM(0), &a, &sa, NULL);
672 fcinfo->flinfo->fn_extra = SearchArrayCache(
673 fcinfo->flinfo->fn_extra,
674 fcinfo->flinfo->fn_mcxt,
675 PG_GETARG_DATUM(1), &b, &sb, NULL);
677 if ( ARR_ELEMTYPE(a) != ARR_ELEMTYPE(b) )
678 elog(ERROR,"Arguments array are not the same type!");
680 if (ARRISVOID(a) || ARRISVOID(b))
681 PG_RETURN_FLOAT4(0.0);
686 PG_RETURN_FLOAT4( TFIDFSml(sa, sb) );
693 power = ((double)(sa->nelems)) * ((double)(sb->nelems));
694 cnt = numOfIntersect(sa, sb);
696 PG_RETURN_FLOAT4( ((double)cnt) / sqrt( power ) );
701 float4 res = (float4)numOfIntersect(sa, sb);
703 PG_RETURN_FLOAT4(res);
707 elog(ERROR,"Unsupported formula type of similarity");
710 PG_RETURN_FLOAT4(0.0); /* keep compiler quiet */
713 PG_FUNCTION_INFO_V1(arraysmlw);
714 Datum arraysmlw(PG_FUNCTION_ARGS);
716 arraysmlw(PG_FUNCTION_ARGS)
719 SimpleArray *sa, *sb;
720 bool useIntersect = PG_GETARG_BOOL(2);
721 double numerator = 0.0;
722 double denominatorA = 0.0,
729 fcinfo->flinfo->fn_extra = SearchArrayCache(
730 fcinfo->flinfo->fn_extra,
731 fcinfo->flinfo->fn_mcxt,
732 PG_GETARG_DATUM(0), &a, &sa, NULL);
733 fcinfo->flinfo->fn_extra = SearchArrayCache(
734 fcinfo->flinfo->fn_extra,
735 fcinfo->flinfo->fn_mcxt,
736 PG_GETARG_DATUM(1), &b, &sb, NULL);
738 if ( ARR_ELEMTYPE(a) != ARR_ELEMTYPE(b) )
739 elog(ERROR,"Arguments array are not the same type!");
741 if (ARRISVOID(a) || ARRISVOID(b))
742 PG_RETURN_FLOAT4(0.0);
745 if (info->tupDesc == NULL)
746 elog(ERROR, "Only weigthed (composite) types should be used");
747 getFmgrInfoCmp(info);
749 while(ai < sa->nelems && bi < sb->nelems)
751 Datum ad = deconstructCompositeType(info, sa->elems[ai], &tmpA),
752 bd = deconstructCompositeType(info, sb->elems[bi], &tmpB);
754 cmp = DatumGetInt32(FCall2(&info->cmpFunc, ad, bd));
757 if (useIntersect == false)
758 denominatorA += tmpA * tmpA;
760 } else if ( cmp > 0 ) {
761 if (useIntersect == false)
762 denominatorB += tmpB * tmpB;
765 denominatorA += tmpA * tmpA;
766 denominatorB += tmpB * tmpB;
767 numerator += tmpA * tmpB;
773 if (useIntersect == false) {
774 while(ai < sa->nelems) {
775 deconstructCompositeType(info, sa->elems[ai], &tmpA);
776 denominatorA += tmpA * tmpA;
780 while(bi < sb->nelems) {
781 deconstructCompositeType(info, sb->elems[bi], &tmpB);
782 denominatorB += tmpB * tmpB;
787 if (numerator != 0.0) {
788 numerator = numerator / sqrt( denominatorA * denominatorB );
791 PG_RETURN_FLOAT4(numerator);
794 PG_FUNCTION_INFO_V1(arraysml_op);
795 Datum arraysml_op(PG_FUNCTION_ARGS);
797 arraysml_op(PG_FUNCTION_ARGS)
800 SimpleArray *sa, *sb;
803 fcinfo->flinfo->fn_extra = SearchArrayCache(
804 fcinfo->flinfo->fn_extra,
805 fcinfo->flinfo->fn_mcxt,
806 PG_GETARG_DATUM(0), &a, &sa, NULL);
807 fcinfo->flinfo->fn_extra = SearchArrayCache(
808 fcinfo->flinfo->fn_extra,
809 fcinfo->flinfo->fn_mcxt,
810 PG_GETARG_DATUM(1), &b, &sb, NULL);
812 if ( ARR_ELEMTYPE(a) != ARR_ELEMTYPE(b) )
813 elog(ERROR,"Arguments array are not the same type!");
815 if (ARRISVOID(a) || ARRISVOID(b))
816 PG_RETURN_BOOL(false);
821 power = TFIDFSml(sa, sb);
827 power = sqrt( ((double)(sa->nelems)) * ((double)(sb->nelems)) );
829 if ( ((double)Min(sa->nelems, sb->nelems)) / power < GetSmlarLimit() )
830 PG_RETURN_BOOL(false);
832 cnt = numOfIntersect(sa, sb);
833 power = ((double)cnt) / power;
837 power = (double)numOfIntersect(sa, sb);
840 elog(ERROR,"Unsupported formula type of similarity");
843 PG_RETURN_BOOL(power >= GetSmlarLimit());
847 static char cachedFormula[QBSIZE];
848 static int cachedLen = 0;
849 static void *cachedPlan = NULL;
851 PG_FUNCTION_INFO_V1(arraysml_func);
852 Datum arraysml_func(PG_FUNCTION_ARGS);
854 arraysml_func(PG_FUNCTION_ARGS)
857 SimpleArray *sa, *sb;
859 float4 result = -1.0;
860 Oid arg[] = {INT4OID, INT4OID, INT4OID};
865 text *formula = PG_GETARG_TEXT_P(2);
867 fcinfo->flinfo->fn_extra = SearchArrayCache(
868 fcinfo->flinfo->fn_extra,
869 fcinfo->flinfo->fn_mcxt,
870 PG_GETARG_DATUM(0), &a, &sa, NULL);
871 fcinfo->flinfo->fn_extra = SearchArrayCache(
872 fcinfo->flinfo->fn_extra,
873 fcinfo->flinfo->fn_mcxt,
874 PG_GETARG_DATUM(1), &b, &sb, NULL);
876 if ( ARR_ELEMTYPE(a) != ARR_ELEMTYPE(b) )
877 elog(ERROR,"Arguments array are not the same type!");
879 if (ARRISVOID(a) || ARRISVOID(b))
880 PG_RETURN_BOOL(false);
882 cnt = numOfIntersect(sa, sb);
884 if ( VARSIZE(formula) - VARHDRSZ > QBSIZE - 1024 )
885 elog(ERROR,"Formula is too long");
889 if ( cachedPlan == NULL || cachedLen != VARSIZE(formula) - VARHDRSZ ||
890 memcmp( cachedFormula, VARDATA(formula), VARSIZE(formula) - VARHDRSZ ) != 0 )
892 char *ptr, buf[QBSIZE];
894 *cachedFormula = '\0';
896 SPI_freeplan(cachedPlan);
900 ptr = stpcpy( buf, "SELECT (" );
901 memcpy( ptr, VARDATA(formula), VARSIZE(formula) - VARHDRSZ );
902 ptr += VARSIZE(formula) - VARHDRSZ;
903 ptr = stpcpy( ptr, ")::float4 FROM");
904 ptr = stpcpy( ptr, " (SELECT $1 ::float8 AS i, $2 ::float8 AS a, $3 ::float8 AS b) AS N;");
907 plan = SPI_prepare(buf, 3, arg);
909 elog(ERROR, "SPI_prepare() failed");
911 cachedPlan = SPI_saveplan(plan);
913 elog(ERROR, "SPI_saveplan() failed");
916 cachedLen = VARSIZE(formula) - VARHDRSZ;
917 memcpy( cachedFormula, VARDATA(formula), VARSIZE(formula) - VARHDRSZ );
923 pars[0] = Int32GetDatum( cnt );
924 pars[1] = Int32GetDatum( sa->nelems );
925 pars[2] = Int32GetDatum( sb->nelems );
927 stat = SPI_execute_plan(plan, pars, NULL, true, 3);
929 elog(ERROR, "SPI_execute_plan() returns %d", stat);
931 if ( SPI_processed > 0)
932 result = DatumGetFloat4(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &isnull));
936 PG_RETURN_FLOAT4(result);
939 PG_FUNCTION_INFO_V1(array_unique);
940 Datum array_unique(PG_FUNCTION_ARGS);
942 array_unique(PG_FUNCTION_ARGS)
944 ArrayType *a = PG_GETARG_ARRAYTYPE_P(0);
948 sa = Array2SimpleArrayU(NULL, a, NULL);
950 res = construct_array( sa->elems,
959 PG_FREE_IF_COPY(a, 0);
961 PG_RETURN_ARRAYTYPE_P(res);
964 PG_FUNCTION_INFO_V1(inarray);
965 Datum inarray(PG_FUNCTION_ARGS);
967 inarray(PG_FUNCTION_ARGS)
971 Datum query = PG_GETARG_DATUM(1);
978 fcinfo->flinfo->fn_extra = SearchArrayCache(
979 fcinfo->flinfo->fn_extra,
980 fcinfo->flinfo->fn_mcxt,
981 PG_GETARG_DATUM(0), &a, &sa, NULL);
983 queryTypeOid = get_fn_expr_argtype(fcinfo->flinfo, 1);
985 if ( queryTypeOid == InvalidOid )
986 elog(ERROR,"inarray: could not determine actual argument type");
988 if ( queryTypeOid != sa->info->typid )
989 elog(ERROR,"inarray: Type of array's element and type of argument are not the same");
991 getFmgrInfoCmp(sa->info);
993 StopHigh = sa->elems + sa->nelems;
995 while (StopLow < StopHigh)
997 StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
998 cmp = cmpArrayElem(StopMiddle, &query, sa->info);
1003 if ( PG_NARGS() >= 3 )
1004 PG_RETURN_DATUM(PG_GETARG_DATUM(2));
1005 PG_RETURN_FLOAT4(1.0);
1008 StopLow = StopMiddle + 1;
1010 StopHigh = StopMiddle;
1013 if ( PG_NARGS() >= 4 )
1014 PG_RETURN_DATUM(PG_GETARG_DATUM(3));
1015 PG_RETURN_FLOAT4(0.0);