v13 support
[smlar.git] / smlar.c
1 #include "smlar.h"
2
3 #include "fmgr.h"
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"
21 #endif
22 #include "utils/syscache.h"
23 #include "utils/typcache.h"
24
25 PG_MODULE_MAGIC;
26
27 #if (PG_VERSION_NUM >= 90400)
28 #define SNAPSHOT NULL
29 #else
30 #define SNAPSHOT SnapshotNow
31 #endif
32
33 #if PG_VERSION_NUM >= 130000
34 #define heap_open(r, l)                 table_open((r), (l))
35 #define heap_close(r, l)                table_close((r), (l))
36 #endif
37
38 static Oid
39 getDefaultOpclass(Oid amoid, Oid typid)
40 {
41         ScanKeyData     skey;
42         SysScanDesc     scan;
43         HeapTuple       tuple;
44         Relation        heapRel;
45         Oid                     opclassOid = InvalidOid;
46
47         heapRel = heap_open(OperatorClassRelationId, AccessShareLock);
48
49         ScanKeyInit(&skey,
50                                 Anum_pg_opclass_opcmethod,
51                                 BTEqualStrategyNumber,  F_OIDEQ,
52                                 ObjectIdGetDatum(amoid));
53
54         scan = systable_beginscan(heapRel,
55                                                                 OpclassAmNameNspIndexId, true,
56                                                                 SNAPSHOT, 1, &skey);
57
58         while (HeapTupleIsValid((tuple = systable_getnext(scan))))
59         {
60                 Form_pg_opclass opclass = (Form_pg_opclass)GETSTRUCT(tuple);
61
62                 if ( opclass->opcintype == typid && opclass->opcdefault )
63                 {
64                         if ( OidIsValid(opclassOid) )
65                                 elog(ERROR, "Ambiguous opclass for type %u (access method %u)", typid, amoid); 
66 #if (PG_VERSION_NUM >= 120000)
67                         opclassOid = opclass->oid;
68 #else
69                         opclassOid = HeapTupleGetOid(tuple);
70 #endif
71                 }
72         }
73
74         systable_endscan(scan);
75         heap_close(heapRel, AccessShareLock);
76
77         return opclassOid;
78 }
79
80 static Oid
81 getAMProc(Oid amoid, Oid typid)
82 {
83         Oid             opclassOid = getDefaultOpclass(amoid, typid);
84         Oid             procOid = InvalidOid;
85         Oid             opfamilyOid;
86         ScanKeyData     skey[4];
87         SysScanDesc     scan;
88         HeapTuple       tuple;
89         Relation        heapRel;
90
91         if ( !OidIsValid(opclassOid) )
92         {
93                 typid = getBaseType(typid);
94                 opclassOid = getDefaultOpclass(amoid, typid);
95         }
96
97         if ( !OidIsValid(opclassOid) )
98         {
99                 CatCList        *catlist;
100                 int                     i;
101
102                 /*
103                  * Search binary-coercible type
104                  */
105 #ifdef SearchSysCacheList1
106                 catlist = SearchSysCacheList1(CASTSOURCETARGET,
107                                                                           ObjectIdGetDatum(typid));
108 #else
109                 catlist = SearchSysCacheList(CASTSOURCETARGET, 1,
110                                                                                 ObjectIdGetDatum(typid),
111                                                                                 0, 0, 0);
112 #endif
113
114                 for (i = 0; i < catlist->n_members; i++)
115                 {
116                         HeapTuple               tuple = &catlist->members[i]->tuple;
117                         Form_pg_cast    castForm = (Form_pg_cast)GETSTRUCT(tuple);
118
119                         if ( castForm->castfunc == InvalidOid && castForm->castcontext == COERCION_CODE_IMPLICIT )
120                         {
121                                 typid = castForm->casttarget;
122                                 opclassOid = getDefaultOpclass(amoid, typid);
123                                 if( OidIsValid(opclassOid) )
124                                         break;
125                         }
126                 }
127
128                 ReleaseSysCacheList(catlist);
129         }
130
131         if ( !OidIsValid(opclassOid) )
132                 return InvalidOid;
133
134         opfamilyOid = get_opclass_family(opclassOid);
135
136         heapRel = heap_open(AccessMethodProcedureRelationId, AccessShareLock);
137         ScanKeyInit(&skey[0],
138                                 Anum_pg_amproc_amprocfamily,
139                                 BTEqualStrategyNumber, F_OIDEQ,
140                                 ObjectIdGetDatum(opfamilyOid));
141         ScanKeyInit(&skey[1],
142                                 Anum_pg_amproc_amproclefttype,
143                                 BTEqualStrategyNumber, F_OIDEQ,
144                                 ObjectIdGetDatum(typid));
145         ScanKeyInit(&skey[2],
146                                 Anum_pg_amproc_amprocrighttype,
147                                 BTEqualStrategyNumber, F_OIDEQ,
148                                 ObjectIdGetDatum(typid));
149 #if PG_VERSION_NUM >= 90200
150         ScanKeyInit(&skey[3],
151                                 Anum_pg_amproc_amprocnum,
152                                 BTEqualStrategyNumber, F_OIDEQ,
153                                 Int32GetDatum(BTORDER_PROC));
154 #endif
155
156         scan = systable_beginscan(heapRel, AccessMethodProcedureIndexId, true,
157                                                                 SNAPSHOT,
158 #if PG_VERSION_NUM >= 90200
159                                                                 4,
160 #else
161                                                                 3,
162 #endif
163                                                                 skey);
164         while (HeapTupleIsValid(tuple = systable_getnext(scan)))
165         {
166                 Form_pg_amproc amprocform = (Form_pg_amproc) GETSTRUCT(tuple);
167
168                 switch(amoid)
169                 {
170                         case BTREE_AM_OID:
171                         case HASH_AM_OID:
172                                 if ( OidIsValid(procOid) )
173                                         elog(ERROR,"Ambiguous support function for type %u (opclass %u)", typid, opfamilyOid);
174                                 procOid = amprocform->amproc;
175                                 break;
176                         default:
177                                 elog(ERROR,"Unsupported access method");
178                 }
179         }
180
181         systable_endscan(scan);
182         heap_close(heapRel, AccessShareLock);
183
184         return procOid;
185 }
186
187 static ProcTypeInfo *cacheProcs = NULL;
188 static int nCacheProcs = 0;
189
190 #ifndef TupleDescAttr
191 #define TupleDescAttr(tupdesc, i)       ((tupdesc)->attrs[(i)])
192 #endif
193
194 static ProcTypeInfo
195 fillProcs(Oid typid)
196 {
197         ProcTypeInfo    info = malloc(sizeof(ProcTypeInfoData));
198
199         if (!info)
200                 elog(ERROR, "Can't allocate %u memory", (uint32)sizeof(ProcTypeInfoData));
201
202         info->typid = typid;
203         info->typtype = get_typtype(typid);
204
205         if (info->typtype == 'c')
206         {
207                 /* composite type */
208                 TupleDesc               tupdesc;
209                 MemoryContext   oldcontext;
210
211                 tupdesc = lookup_rowtype_tupdesc(typid, -1);
212
213                 if (tupdesc->natts != 2)
214                         elog(ERROR,"Composite type has wrong number of fields");
215                 if (TupleDescAttr(tupdesc, 1)->atttypid != FLOAT4OID)
216                         elog(ERROR,"Second field of composite type is not float4");
217
218                 oldcontext = MemoryContextSwitchTo(TopMemoryContext);
219                 info->tupDesc = CreateTupleDescCopyConstr(tupdesc);
220                 MemoryContextSwitchTo(oldcontext);
221
222                 ReleaseTupleDesc(tupdesc);
223
224                 info->cmpFuncOid = getAMProc(BTREE_AM_OID,
225                                                                          TupleDescAttr(info->tupDesc, 0)->atttypid);
226                 info->hashFuncOid = getAMProc(HASH_AM_OID,
227                                                                           TupleDescAttr(info->tupDesc, 0)->atttypid);
228         }
229         else
230         {
231                 info->tupDesc = NULL;
232
233                 /* plain type */
234                 info->cmpFuncOid = getAMProc(BTREE_AM_OID, typid);
235                 info->hashFuncOid = getAMProc(HASH_AM_OID, typid);
236         }
237
238         get_typlenbyvalalign(typid, &info->typlen, &info->typbyval, &info->typalign);
239         info->hashFuncInited = info->cmpFuncInited = false;
240
241
242         return info;
243 }
244
245 void
246 getFmgrInfoCmp(ProcTypeInfo info)
247 {
248         if ( info->cmpFuncInited == false )
249         {
250                 if ( !OidIsValid(info->cmpFuncOid) )
251                         elog(ERROR, "Could not find cmp function for type %u", info->typid);
252
253                 fmgr_info_cxt( info->cmpFuncOid, &info->cmpFunc, TopMemoryContext );
254                 info->cmpFuncInited = true;
255         }
256 }
257
258 void
259 getFmgrInfoHash(ProcTypeInfo info)
260 {
261         if ( info->hashFuncInited == false )
262         {
263                 if ( !OidIsValid(info->hashFuncOid) )
264                         elog(ERROR, "Could not find hash function for type %u", info->typid);
265
266                 fmgr_info_cxt( info->hashFuncOid, &info->hashFunc, TopMemoryContext );
267                 info->hashFuncInited = true;
268         }
269 }
270
271 static int
272 cmpProcTypeInfo(const void *a, const void *b)
273 {
274         ProcTypeInfo av = *(ProcTypeInfo*)a;
275         ProcTypeInfo bv = *(ProcTypeInfo*)b;
276
277         Assert( av->typid != bv->typid );
278
279         return ( av->typid > bv->typid ) ? 1 : -1;
280 }
281
282 ProcTypeInfo
283 findProcs(Oid typid)
284 {
285         ProcTypeInfo    info = NULL;
286
287         if ( nCacheProcs == 1 )
288         {
289                 if ( cacheProcs[0]->typid == typid )
290                 {
291                         /*cacheProcs[0]->hashFuncInited = cacheProcs[0]->cmpFuncInited = false;*/
292                         return cacheProcs[0];
293                 }
294         }
295         else if ( nCacheProcs > 1 )
296         {
297                 ProcTypeInfo    *StopMiddle;
298                 ProcTypeInfo    *StopLow = cacheProcs,
299                                                 *StopHigh = cacheProcs + nCacheProcs;
300
301                 while (StopLow < StopHigh) {
302                         StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
303                         info = *StopMiddle;
304
305                         if ( info->typid == typid )
306                         {
307                                 /* info->hashFuncInited = info->cmpFuncInited = false; */
308                                 return info;
309                         }
310                         else if ( info->typid < typid )
311                                 StopLow = StopMiddle + 1;
312                         else
313                                 StopHigh = StopMiddle;
314                 }
315
316                 /* not found */
317         } 
318
319         info = fillProcs(typid);
320         if ( nCacheProcs == 0 )
321         {
322                 cacheProcs = malloc(sizeof(ProcTypeInfo));
323
324                 if (!cacheProcs)
325                         elog(ERROR, "Can't allocate %u memory", (uint32)sizeof(ProcTypeInfo));
326                 else
327                 {
328                         nCacheProcs = 1;
329                         cacheProcs[0] = info;
330                 }
331         }
332         else
333         {
334                 ProcTypeInfo    *cacheProcsTmp = realloc(cacheProcs, (nCacheProcs+1) * sizeof(ProcTypeInfo));
335
336                 if (!cacheProcsTmp)
337                         elog(ERROR, "Can't allocate %u memory", (uint32)sizeof(ProcTypeInfo) * (nCacheProcs+1));
338                 else
339                 {
340                         cacheProcs = cacheProcsTmp;
341                         cacheProcs[ nCacheProcs ] = info;
342                         nCacheProcs++;
343                         qsort(cacheProcs, nCacheProcs, sizeof(ProcTypeInfo), cmpProcTypeInfo);
344                 }
345         }
346
347         /* info->hashFuncInited = info->cmpFuncInited = false; */
348
349         return info;
350 }
351
352 /*
353  * WARNING. Array2SimpleArray* doesn't copy Datum!
354  */
355 SimpleArray * 
356 Array2SimpleArray(ProcTypeInfo info, ArrayType *a)
357 {
358         SimpleArray     *s = palloc(sizeof(SimpleArray));
359
360         CHECKARRVALID(a);
361
362         if ( info == NULL )
363                 info = findProcs(ARR_ELEMTYPE(a));
364
365         s->info = info;
366         s->df = NULL;
367         s->hash = NULL;
368
369         deconstruct_array(a, info->typid,
370                                                 info->typlen, info->typbyval, info->typalign,
371                                                 &s->elems, NULL, &s->nelems);
372
373         return s;
374 }
375
376 static Datum
377 deconstructCompositeType(ProcTypeInfo info, Datum in, double *weight)
378 {
379         HeapTupleHeader rec = DatumGetHeapTupleHeader(in);
380         HeapTupleData   tuple;
381         Datum                   values[2];
382         bool                    nulls[2];
383
384         /* Build a temporary HeapTuple control structure */
385         tuple.t_len = HeapTupleHeaderGetDatumLength(rec);
386         ItemPointerSetInvalid(&(tuple.t_self));
387         tuple.t_tableOid = InvalidOid;
388         tuple.t_data = rec;
389
390         heap_deform_tuple(&tuple, info->tupDesc, values, nulls);
391         if (nulls[0] || nulls[1])
392                 elog(ERROR, "Both fields in composite type could not be NULL");
393
394         if (weight)
395                 *weight = DatumGetFloat4(values[1]);
396         return values[0];
397 }
398
399 static int
400 cmpArrayElem(const void *a, const void *b, void *arg)
401 {
402         ProcTypeInfo    info = (ProcTypeInfo)arg;
403
404         if (info->tupDesc)
405                 /* composite type */
406                 return DatumGetInt32( FCall2( &info->cmpFunc,
407                                                 deconstructCompositeType(info, *(Datum*)a, NULL),
408                                                 deconstructCompositeType(info, *(Datum*)b, NULL) ) );
409
410         return DatumGetInt32( FCall2( &info->cmpFunc,
411                                                         *(Datum*)a, *(Datum*)b ) );
412 }
413
414 SimpleArray *
415 Array2SimpleArrayS(ProcTypeInfo info, ArrayType *a)
416 {
417         SimpleArray     *s = Array2SimpleArray(info, a);
418
419         if ( s->nelems > 1 )
420         {
421                 getFmgrInfoCmp(s->info);
422
423                 qsort_arg(s->elems, s->nelems, sizeof(Datum), cmpArrayElem, s->info);
424         }
425
426         return s;
427 }
428
429 typedef struct cmpArrayElemData {
430         ProcTypeInfo    info;
431         bool                    hasDuplicate;
432
433 } cmpArrayElemData;
434
435 static int
436 cmpArrayElemArg(const void *a, const void *b, void *arg)
437 {
438         cmpArrayElemData        *data = (cmpArrayElemData*)arg;
439         int                                     res;
440
441         if (data->info->tupDesc)
442                 res =  DatumGetInt32( FCall2( &data->info->cmpFunc,
443                                         deconstructCompositeType(data->info, *(Datum*)a, NULL),
444                                         deconstructCompositeType(data->info, *(Datum*)b, NULL) ) );
445         else
446                 res = DatumGetInt32( FCall2( &data->info->cmpFunc,
447                                                                 *(Datum*)a, *(Datum*)b ) );
448
449         if ( res == 0 )
450                 data->hasDuplicate = true;
451
452         return res;
453 }
454
455 /*
456  * Uniquefy array and calculate TF. Although 
457  * result doesn't depend on normalization, we
458  * normalize TF by length array to have possiblity
459  * to limit estimation for index support.
460  *
461  * Cache signals of needing of TF caclulation
462  */
463
464 SimpleArray *
465 Array2SimpleArrayU(ProcTypeInfo info, ArrayType *a, void *cache)
466 {
467         SimpleArray     *s = Array2SimpleArray(info, a);
468         StatElem        *stat = NULL;
469
470         if ( s->nelems > 0 && cache )
471         {
472                 s->df = palloc(sizeof(double) * s->nelems);
473                 s->df[0] = 1.0; /* init */
474         }
475
476         if ( s->nelems > 1 )
477         {
478                 cmpArrayElemData        data;
479                 int                                     i;
480
481                 getFmgrInfoCmp(s->info);
482                 data.info = s->info;
483                 data.hasDuplicate = false;
484
485                 qsort_arg(s->elems, s->nelems, sizeof(Datum), cmpArrayElemArg, &data);
486
487                 if ( data.hasDuplicate )
488                 {
489                         Datum   *tmp,
490                                         *dr,
491                                         *data;
492                         int             num = s->nelems,
493                                         cmp;
494
495                         data = tmp = dr = s->elems;
496
497                         while (tmp - data < num)
498                         {
499                                 cmp = (tmp == dr) ? 0 : cmpArrayElem(tmp, dr, s->info);
500                                 if ( cmp != 0 )
501                                 {
502                                         *(++dr) = *tmp++;
503                                         if ( cache ) 
504                                                 s->df[ dr - data ] = 1.0;
505                                 }
506                                 else
507                                 {
508                                         if ( cache )
509                                                 s->df[ dr - data ] += 1.0;
510                                         tmp++;
511                                 }
512                         }
513
514                         s->nelems = dr + 1 - s->elems;
515
516                         if ( cache )
517                         {
518                                 int tfm = getTFMethod();
519
520                                 for(i=0;i<s->nelems;i++)
521                                 {
522                                         stat = fingArrayStat(cache, s->info->typid, s->elems[i], stat);
523                                         if ( stat )
524                                         {
525                                                 switch(tfm)
526                                                 {
527                                                         case TF_LOG:
528                                                                 s->df[i] = (1.0 + log( s->df[i] ));
529                                                         case TF_N:
530                                                                 s->df[i] *= stat->idf;
531                                                                 break;
532                                                         case TF_CONST:
533                                                                 s->df[i] = stat->idf;
534                                                                 break;
535                                                         default:
536                                                                 elog(ERROR,"Unknown TF method: %d", tfm);
537                                                 }
538                                         }
539                                         else
540                                         {
541                                                 s->df[i] = 0.0; /* unknown word */
542                                         }
543                                 }
544                         }
545                 }
546                 else if ( cache )
547                 {
548                         for(i=0;i<s->nelems;i++)
549                         {
550                                 stat = fingArrayStat(cache, s->info->typid, s->elems[i], stat);
551                                 if ( stat )
552                                         s->df[i] = stat->idf;
553                                 else
554                                         s->df[i] = 0.0;
555                         }
556                 }
557         }
558         else if (s->nelems > 0 && cache)
559         {
560                 stat = fingArrayStat(cache, s->info->typid, s->elems[0], stat);
561                 if ( stat )
562                         s->df[0] = stat->idf;
563                 else
564                         s->df[0] = 0.0;
565         }
566
567         return s;
568 }
569
570 static int
571 numOfIntersect(SimpleArray *a, SimpleArray *b)
572 {
573         int                             cnt = 0,
574                                         cmp;
575         Datum                   *aptr = a->elems,
576                                         *bptr = b->elems;
577         ProcTypeInfo    info = a->info;
578
579         Assert( a->info->typid == b->info->typid );
580
581         getFmgrInfoCmp(info);
582
583         while( aptr - a->elems < a->nelems && bptr - b->elems < b->nelems )
584         {
585                 cmp = cmpArrayElem(aptr, bptr, info);
586                 if ( cmp < 0 )
587                         aptr++;
588                 else if ( cmp > 0 )
589                         bptr++;
590                 else
591                 {
592                         cnt++;
593                         aptr++;
594                         bptr++;
595                 }
596         }
597
598         return cnt;
599 }
600
601 static double
602 TFIDFSml(SimpleArray *a, SimpleArray *b)
603 {
604         int                             cmp;
605         Datum                   *aptr = a->elems,
606                                         *bptr = b->elems;
607         ProcTypeInfo    info = a->info;
608         double                  res = 0.0;
609         double                  suma = 0.0, sumb = 0.0;
610
611         Assert( a->info->typid == b->info->typid );
612         Assert( a->df );
613         Assert( b->df );
614
615         getFmgrInfoCmp(info);
616
617         while( aptr - a->elems < a->nelems && bptr - b->elems < b->nelems )
618         {
619                 cmp = cmpArrayElem(aptr, bptr, info);
620                 if ( cmp < 0 )
621                 {
622                         suma += a->df[ aptr - a->elems ] * a->df[ aptr - a->elems ];
623                         aptr++;
624                 }
625                 else if ( cmp > 0 )
626                 {
627                         sumb += b->df[ bptr - b->elems ] * b->df[ bptr - b->elems ];
628                         bptr++;
629                 }
630                 else
631                 {
632                         res += a->df[ aptr - a->elems ] * b->df[ bptr - b->elems ];
633                         suma += a->df[ aptr - a->elems ] * a->df[ aptr - a->elems ];
634                         sumb += b->df[ bptr - b->elems ] * b->df[ bptr - b->elems ];
635                         aptr++;
636                         bptr++;
637                 }
638         }
639
640         /*
641          * Compute last elements
642          */
643         while( aptr - a->elems < a->nelems )
644         {
645                 suma += a->df[ aptr - a->elems ] * a->df[ aptr - a->elems ];
646                 aptr++;
647         }
648
649         while( bptr - b->elems < b->nelems )
650         {
651                 sumb += b->df[ bptr - b->elems ] * b->df[ bptr - b->elems ];
652                 bptr++;
653         }
654
655         if ( suma > 0.0 && sumb > 0.0 )
656                 res = res / sqrt( suma * sumb );
657         else
658                 res = 0.0;
659
660         return res;
661 }
662
663
664 PG_FUNCTION_INFO_V1(arraysml);
665 Datum   arraysml(PG_FUNCTION_ARGS);
666 Datum
667 arraysml(PG_FUNCTION_ARGS)
668 {
669         ArrayType               *a, *b;
670         SimpleArray             *sa, *sb;
671
672         fcinfo->flinfo->fn_extra = SearchArrayCache(
673                                                         fcinfo->flinfo->fn_extra,
674                                                         fcinfo->flinfo->fn_mcxt,
675                                                         PG_GETARG_DATUM(0), &a, &sa, NULL);
676         fcinfo->flinfo->fn_extra = SearchArrayCache(
677                                                         fcinfo->flinfo->fn_extra,
678                                                         fcinfo->flinfo->fn_mcxt,
679                                                         PG_GETARG_DATUM(1), &b, &sb, NULL);
680
681         if ( ARR_ELEMTYPE(a) != ARR_ELEMTYPE(b) )
682                 elog(ERROR,"Arguments array are not the same type!");
683
684         if (ARRISVOID(a) || ARRISVOID(b))
685                  PG_RETURN_FLOAT4(0.0);
686
687         switch(getSmlType())
688         {
689                 case ST_TFIDF:
690                         PG_RETURN_FLOAT4( TFIDFSml(sa, sb) );
691                         break;
692                 case ST_COSINE:
693                         {
694                                 int                             cnt;
695                                 double                  power;
696
697                                 power = ((double)(sa->nelems)) * ((double)(sb->nelems));
698                                 cnt = numOfIntersect(sa, sb);
699
700                                 PG_RETURN_FLOAT4(  ((double)cnt) / sqrt( power ) );
701                         }
702                         break;
703                 case ST_OVERLAP:
704                         {
705                                 float4 res = (float4)numOfIntersect(sa, sb);
706
707                                 PG_RETURN_FLOAT4(res);
708                         }
709                         break;
710                 default:
711                         elog(ERROR,"Unsupported formula type of similarity");
712         }
713
714         PG_RETURN_FLOAT4(0.0); /* keep compiler quiet */
715 }
716
717 PG_FUNCTION_INFO_V1(arraysmlw);
718 Datum   arraysmlw(PG_FUNCTION_ARGS);
719 Datum
720 arraysmlw(PG_FUNCTION_ARGS)
721 {
722         ArrayType               *a, *b;
723         SimpleArray             *sa, *sb;
724         bool                    useIntersect = PG_GETARG_BOOL(2);
725         double                  numerator = 0.0;
726         double                  denominatorA = 0.0,
727                                         denominatorB = 0.0,
728                                         tmpA, tmpB;
729         int                             cmp;
730         ProcTypeInfo    info;
731         int                             ai = 0, bi = 0;
732
733         fcinfo->flinfo->fn_extra = SearchArrayCache(
734                                                         fcinfo->flinfo->fn_extra,
735                                                         fcinfo->flinfo->fn_mcxt,
736                                                         PG_GETARG_DATUM(0), &a, &sa, NULL);
737         fcinfo->flinfo->fn_extra = SearchArrayCache(
738                                                         fcinfo->flinfo->fn_extra,
739                                                         fcinfo->flinfo->fn_mcxt,
740                                                         PG_GETARG_DATUM(1), &b, &sb, NULL);
741
742         if ( ARR_ELEMTYPE(a) != ARR_ELEMTYPE(b) )
743                 elog(ERROR,"Arguments array are not the same type!");
744
745         if (ARRISVOID(a) || ARRISVOID(b))
746                  PG_RETURN_FLOAT4(0.0);
747
748         info = sa->info;
749         if (info->tupDesc == NULL)
750                 elog(ERROR, "Only weigthed (composite) types should be used");
751         getFmgrInfoCmp(info);
752
753         while(ai < sa->nelems && bi < sb->nelems)
754         {
755                 Datum   ad = deconstructCompositeType(info, sa->elems[ai], &tmpA),
756                                 bd = deconstructCompositeType(info, sb->elems[bi], &tmpB);
757
758                 cmp = DatumGetInt32(FCall2(&info->cmpFunc, ad, bd));
759
760                 if ( cmp < 0 ) {
761                         if (useIntersect == false)
762                                 denominatorA += tmpA * tmpA;
763                         ai++;
764                 } else if ( cmp > 0 ) {
765                         if (useIntersect == false)
766                                 denominatorB += tmpB * tmpB;
767                         bi++;
768                 } else {
769                         denominatorA += tmpA * tmpA;
770                         denominatorB += tmpB * tmpB;
771                         numerator += tmpA * tmpB;
772                         ai++;
773                         bi++;
774                 }
775         }
776
777         if (useIntersect == false) {
778                 while(ai < sa->nelems) {
779                         deconstructCompositeType(info, sa->elems[ai], &tmpA);
780                         denominatorA += tmpA * tmpA;
781                         ai++;
782                 }
783
784                 while(bi < sb->nelems) {
785                         deconstructCompositeType(info, sb->elems[bi], &tmpB);
786                         denominatorB += tmpB * tmpB;
787                         bi++;
788                 }
789         }
790
791         if (numerator != 0.0) {
792                 numerator = numerator / sqrt( denominatorA * denominatorB );
793         }
794
795         PG_RETURN_FLOAT4(numerator);
796 }
797
798 PG_FUNCTION_INFO_V1(arraysml_op);
799 Datum   arraysml_op(PG_FUNCTION_ARGS);
800 Datum
801 arraysml_op(PG_FUNCTION_ARGS)
802 {
803         ArrayType               *a, *b;
804         SimpleArray             *sa, *sb;
805         double                  power = 0.0;
806
807         fcinfo->flinfo->fn_extra = SearchArrayCache(
808                                                         fcinfo->flinfo->fn_extra,
809                                                         fcinfo->flinfo->fn_mcxt,
810                                                         PG_GETARG_DATUM(0), &a, &sa, NULL);
811         fcinfo->flinfo->fn_extra = SearchArrayCache(
812                                                         fcinfo->flinfo->fn_extra,
813                                                         fcinfo->flinfo->fn_mcxt,
814                                                         PG_GETARG_DATUM(1), &b, &sb, NULL);
815
816         if ( ARR_ELEMTYPE(a) != ARR_ELEMTYPE(b) )
817                 elog(ERROR,"Arguments array are not the same type!");
818
819         if (ARRISVOID(a) || ARRISVOID(b))
820                  PG_RETURN_BOOL(false);
821
822         switch(getSmlType())
823         {
824                 case ST_TFIDF:
825                         power = TFIDFSml(sa, sb);
826                         break;
827                 case ST_COSINE:
828                         {
829                                 int                             cnt;
830
831                                 power = sqrt( ((double)(sa->nelems)) * ((double)(sb->nelems)) );
832
833                                 if (  ((double)Min(sa->nelems, sb->nelems)) / power < GetSmlarLimit()  )
834                                         PG_RETURN_BOOL(false);
835
836                                 cnt = numOfIntersect(sa, sb);
837                                 power = ((double)cnt) / power;
838                         }
839                         break;
840                 case ST_OVERLAP:
841                         power = (double)numOfIntersect(sa, sb);
842                         break;
843                 default:
844                         elog(ERROR,"Unsupported formula type of similarity");
845         }
846
847         PG_RETURN_BOOL(power >= GetSmlarLimit());
848 }
849
850 #define QBSIZE          8192
851 static char cachedFormula[QBSIZE];
852 static int      cachedLen  = 0;
853 static void     *cachedPlan = NULL;
854
855 PG_FUNCTION_INFO_V1(arraysml_func);
856 Datum   arraysml_func(PG_FUNCTION_ARGS);
857 Datum
858 arraysml_func(PG_FUNCTION_ARGS)
859 {
860         ArrayType               *a, *b;
861         SimpleArray             *sa, *sb;
862         int                             cnt;
863         float4                  result = -1.0;
864         Oid                             arg[] = {INT4OID, INT4OID, INT4OID};
865         Datum                   pars[3];
866         bool                    isnull;
867         void                    *plan;
868         int                             stat;
869         text                    *formula = PG_GETARG_TEXT_P(2);
870
871         fcinfo->flinfo->fn_extra = SearchArrayCache(
872                                                         fcinfo->flinfo->fn_extra,
873                                                         fcinfo->flinfo->fn_mcxt,
874                                                         PG_GETARG_DATUM(0), &a, &sa, NULL);
875         fcinfo->flinfo->fn_extra = SearchArrayCache(
876                                                         fcinfo->flinfo->fn_extra,
877                                                         fcinfo->flinfo->fn_mcxt,
878                                                         PG_GETARG_DATUM(1), &b, &sb, NULL);
879
880         if ( ARR_ELEMTYPE(a) != ARR_ELEMTYPE(b) )
881                 elog(ERROR,"Arguments array are not the same type!");
882
883         if (ARRISVOID(a) || ARRISVOID(b))
884                  PG_RETURN_BOOL(false);
885
886         cnt = numOfIntersect(sa, sb);
887
888         if ( VARSIZE(formula) - VARHDRSZ > QBSIZE - 1024 )
889                 elog(ERROR,"Formula is too long");
890
891         SPI_connect();
892
893         if ( cachedPlan == NULL || cachedLen != VARSIZE(formula) - VARHDRSZ ||
894                                 memcmp( cachedFormula, VARDATA(formula), VARSIZE(formula) - VARHDRSZ ) != 0 )
895         {
896                 char                    *ptr, buf[QBSIZE];
897
898                 *cachedFormula = '\0';
899                 if ( cachedPlan )
900                         SPI_freeplan(cachedPlan);
901                 cachedPlan = NULL;
902                 cachedLen = 0;
903
904                 ptr = stpcpy( buf, "SELECT (" );
905                 memcpy( ptr, VARDATA(formula), VARSIZE(formula) - VARHDRSZ );
906                 ptr += VARSIZE(formula) - VARHDRSZ;
907                 ptr = stpcpy( ptr, ")::float4 FROM");
908                 ptr = stpcpy( ptr, " (SELECT $1 ::float8 AS i, $2 ::float8 AS a, $3 ::float8 AS b) AS N;");
909                 *ptr = '\0';
910
911                 plan = SPI_prepare(buf, 3, arg);
912                 if (!plan)
913                         elog(ERROR, "SPI_prepare() failed");
914
915                 cachedPlan = SPI_saveplan(plan);
916                 if (!cachedPlan)
917                         elog(ERROR, "SPI_saveplan() failed");
918
919                 SPI_freeplan(plan);
920                 cachedLen = VARSIZE(formula) - VARHDRSZ;
921                 memcpy( cachedFormula, VARDATA(formula), VARSIZE(formula) - VARHDRSZ );
922         }
923
924         plan = cachedPlan;
925
926
927         pars[0] = Int32GetDatum( cnt );
928         pars[1] = Int32GetDatum( sa->nelems );
929         pars[2] = Int32GetDatum( sb->nelems );
930
931         stat = SPI_execute_plan(plan, pars, NULL, true, 3);
932         if (stat < 0)
933                 elog(ERROR, "SPI_execute_plan() returns %d", stat);
934
935         if ( SPI_processed > 0)
936                 result = DatumGetFloat4(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &isnull));
937
938         SPI_finish();
939
940         PG_RETURN_FLOAT4(result);
941 }
942
943 PG_FUNCTION_INFO_V1(array_unique);
944 Datum   array_unique(PG_FUNCTION_ARGS);
945 Datum
946 array_unique(PG_FUNCTION_ARGS)
947 {
948         ArrayType               *a = PG_GETARG_ARRAYTYPE_P(0);
949         ArrayType               *res;
950         SimpleArray             *sa;
951
952         sa = Array2SimpleArrayU(NULL, a, NULL);
953
954         res = construct_array(  sa->elems, 
955                                                         sa->nelems,
956                                                         sa->info->typid,
957                                                         sa->info->typlen,
958                                                         sa->info->typbyval,
959                                                         sa->info->typalign);
960
961         pfree(sa->elems);
962         pfree(sa);
963         PG_FREE_IF_COPY(a, 0);
964
965         PG_RETURN_ARRAYTYPE_P(res);
966 }
967
968 PG_FUNCTION_INFO_V1(inarray);
969 Datum   inarray(PG_FUNCTION_ARGS);
970 Datum
971 inarray(PG_FUNCTION_ARGS)
972 {
973         ArrayType               *a;
974         SimpleArray             *sa;
975         Datum                   query = PG_GETARG_DATUM(1);
976         Oid                             queryTypeOid;
977         Datum                   *StopLow,
978                                         *StopHigh,
979                                         *StopMiddle;
980         int                             cmp;
981
982         fcinfo->flinfo->fn_extra = SearchArrayCache(
983                                                         fcinfo->flinfo->fn_extra,
984                                                         fcinfo->flinfo->fn_mcxt,
985                                                         PG_GETARG_DATUM(0), &a, &sa, NULL);
986
987         queryTypeOid = get_fn_expr_argtype(fcinfo->flinfo, 1);
988
989         if ( queryTypeOid == InvalidOid )
990                 elog(ERROR,"inarray: could not determine actual argument type");
991
992         if ( queryTypeOid != sa->info->typid )
993                 elog(ERROR,"inarray: Type of array's element and type of argument are not the same");
994
995         getFmgrInfoCmp(sa->info);
996         StopLow = sa->elems;
997         StopHigh = sa->elems + sa->nelems;
998
999         while (StopLow < StopHigh)
1000         {
1001                 StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
1002                 cmp = cmpArrayElem(StopMiddle, &query, sa->info);
1003
1004                 if ( cmp == 0 )
1005                 {
1006                         /* found */
1007                         if ( PG_NARGS() >= 3 )
1008                                 PG_RETURN_DATUM(PG_GETARG_DATUM(2));
1009                         PG_RETURN_FLOAT4(1.0);
1010                 }
1011                 else if (cmp < 0)
1012                         StopLow = StopMiddle + 1;
1013                 else
1014                         StopHigh = StopMiddle;
1015         }
1016
1017         if ( PG_NARGS() >= 4 )
1018                 PG_RETURN_DATUM(PG_GETARG_DATUM(3));
1019         PG_RETURN_FLOAT4(0.0);
1020 }