Advertisement
Guest User

Untitled

a guest
Aug 4th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.24 KB | None | 0 0
  1. Datum
  2. sequence_contained(PG_FUNCTION_ARGS)
  3. {
  4.     return DirectFunctionCall2(contains_sequence,
  5.                                PG_GETARG_DATUM(1),
  6.                                PG_GETARG_DATUM(0));
  7. }
  8.  
  9. Datum
  10. contains_sequence(PG_FUNCTION_ARGS)
  11. {
  12.     ArrayType              *a = PG_GETARG_ARRAYTYPE_P(0);
  13.     ArrayType              *b = PG_GETARG_ARRAYTYPE_P(1);
  14.     Oid                     element_type = ARR_ELEMTYPE(a);
  15.     int                     na, nb;
  16.     int                     i, j;
  17.     char                   *pa;
  18.     TypeCacheEntry         *typentry;
  19.     FunctionCallInfoData    locfcinfo;
  20.     Oid                     collation = PG_GET_COLLATION();
  21.  
  22.     if (array_contains_nulls(a) || array_contains_nulls(b))
  23.         ereport(ERROR,
  24.                 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
  25.                  errmsg("arrays must not contain nulls")));
  26.  
  27.     na = ArrayGetNItems(ARR_NDIM(a), ARR_DIMS(a));
  28.     nb = ArrayGetNItems(ARR_NDIM(b), ARR_DIMS(b));
  29.     pa = ARR_DATA_PTR(a);
  30.  
  31.     typentry = (TypeCacheEntry *) fcinfo->flinfo->fn_extra;
  32.     if (typentry == NULL ||
  33.         typentry->type_id != element_type)
  34.     {
  35.         typentry = lookup_type_cache(element_type,
  36.                                      TYPECACHE_EQ_OPR_FINFO);
  37.         if (!OidIsValid(typentry->eq_opr_finfo.fn_oid))
  38.             ereport(ERROR,
  39.                     (errcode(ERRCODE_UNDEFINED_FUNCTION),
  40.             errmsg("could not identify an equality operator for type %s",
  41.                    format_type_be(element_type))));
  42.  
  43.         fcinfo->flinfo->fn_extra = (void *) typentry;
  44.     }
  45.  
  46.     InitFunctionCallInfoData(locfcinfo, &typentry->eq_opr_finfo, 2,
  47.                              collation, NULL, NULL);
  48.  
  49.     for (i = 0; i <= na - nb; ++i)
  50.     {
  51.         char       *ppa = pa;
  52.         char       *pb = ARR_DATA_PTR(b);
  53.  
  54.         for (j = 0; j < nb; ++j)
  55.         {
  56.             locfcinfo.arg[0] = fetch_att(ppa, typentry->typbyval, typentry->typlen);
  57.             locfcinfo.arg[1] = fetch_att(pb, typentry->typbyval, typentry->typlen);
  58.             locfcinfo.argnull[0] = false;
  59.             locfcinfo.argnull[1] = false;
  60.             locfcinfo.isnull = false;
  61.             if (!DatumGetBool(FunctionCallInvoke(&locfcinfo)))
  62.                 break;
  63.  
  64.             pb = att_addlength_pointer(pb, typentry->typlen, pb);
  65.             pb = (char *) att_align_nominal(pb, typentry->typalign);
  66.  
  67.             ppa = att_addlength_pointer(ppa, typentry->typlen, ppa);
  68.             ppa = (char *) att_align_nominal(ppa, typentry->typalign);
  69.         }
  70.  
  71.         if (j == nb)
  72.             PG_RETURN_BOOL(true);
  73.  
  74.         pa = att_addlength_pointer(pa, typentry->typlen, pa);
  75.         pa = (char *) att_align_nominal(pa, typentry->typalign);
  76.     }
  77.  
  78.     PG_RETURN_BOOL(false);
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement