Header And Logo

PostgreSQL
| The world's most advanced open source database.

Data Structures | Defines | Typedefs | Functions

rangetypes.c File Reference

#include "postgres.h"
#include "access/hash.h"
#include "lib/stringinfo.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/date.h"
#include "utils/int8.h"
#include "utils/lsyscache.h"
#include "utils/rangetypes.h"
#include "utils/timestamp.h"
Include dependency graph for rangetypes.c:

Go to the source code of this file.

Data Structures

struct  RangeIOData

Defines

#define RANGE_EMPTY_LITERAL   "empty"
#define TYPE_IS_PACKABLE(typlen, typstorage)   ((typlen) == -1 && (typstorage) != 'p')

Typedefs

typedef struct RangeIOData RangeIOData

Functions

static RangeIODataget_range_io_data (FunctionCallInfo fcinfo, Oid rngtypid, IOFuncSelector func)
static char range_parse_flags (const char *flags_str)
static void range_parse (const char *input_str, char *flags, char **lbound_str, char **ubound_str)
static const char * range_parse_bound (const char *string, const char *ptr, char **bound_str, bool *infinite)
static char * range_deparse (char flags, const char *lbound_str, const char *ubound_str)
static char * range_bound_escape (const char *value)
static Size datum_compute_size (Size sz, Datum datum, bool typbyval, char typalign, int16 typlen, char typstorage)
static Pointer datum_write (Pointer ptr, Datum datum, bool typbyval, char typalign, int16 typlen, char typstorage)
Datum range_in (PG_FUNCTION_ARGS)
Datum range_out (PG_FUNCTION_ARGS)
Datum range_recv (PG_FUNCTION_ARGS)
Datum range_send (PG_FUNCTION_ARGS)
Datum range_constructor2 (PG_FUNCTION_ARGS)
Datum range_constructor3 (PG_FUNCTION_ARGS)
Datum range_lower (PG_FUNCTION_ARGS)
Datum range_upper (PG_FUNCTION_ARGS)
Datum range_empty (PG_FUNCTION_ARGS)
Datum range_lower_inc (PG_FUNCTION_ARGS)
Datum range_upper_inc (PG_FUNCTION_ARGS)
Datum range_lower_inf (PG_FUNCTION_ARGS)
Datum range_upper_inf (PG_FUNCTION_ARGS)
Datum range_contains_elem (PG_FUNCTION_ARGS)
Datum elem_contained_by_range (PG_FUNCTION_ARGS)
bool range_eq_internal (TypeCacheEntry *typcache, RangeType *r1, RangeType *r2)
Datum range_eq (PG_FUNCTION_ARGS)
bool range_ne_internal (TypeCacheEntry *typcache, RangeType *r1, RangeType *r2)
Datum range_ne (PG_FUNCTION_ARGS)
Datum range_contains (PG_FUNCTION_ARGS)
Datum range_contained_by (PG_FUNCTION_ARGS)
bool range_before_internal (TypeCacheEntry *typcache, RangeType *r1, RangeType *r2)
Datum range_before (PG_FUNCTION_ARGS)
bool range_after_internal (TypeCacheEntry *typcache, RangeType *r1, RangeType *r2)
Datum range_after (PG_FUNCTION_ARGS)
bool bounds_adjacent (TypeCacheEntry *typcache, RangeBound boundA, RangeBound boundB)
bool range_adjacent_internal (TypeCacheEntry *typcache, RangeType *r1, RangeType *r2)
Datum range_adjacent (PG_FUNCTION_ARGS)
bool range_overlaps_internal (TypeCacheEntry *typcache, RangeType *r1, RangeType *r2)
Datum range_overlaps (PG_FUNCTION_ARGS)
bool range_overleft_internal (TypeCacheEntry *typcache, RangeType *r1, RangeType *r2)
Datum range_overleft (PG_FUNCTION_ARGS)
bool range_overright_internal (TypeCacheEntry *typcache, RangeType *r1, RangeType *r2)
Datum range_overright (PG_FUNCTION_ARGS)
Datum range_minus (PG_FUNCTION_ARGS)
Datum range_union (PG_FUNCTION_ARGS)
Datum range_intersect (PG_FUNCTION_ARGS)
Datum range_cmp (PG_FUNCTION_ARGS)
Datum range_lt (PG_FUNCTION_ARGS)
Datum range_le (PG_FUNCTION_ARGS)
Datum range_ge (PG_FUNCTION_ARGS)
Datum range_gt (PG_FUNCTION_ARGS)
Datum hash_range (PG_FUNCTION_ARGS)
Datum int4range_canonical (PG_FUNCTION_ARGS)
Datum int8range_canonical (PG_FUNCTION_ARGS)
Datum daterange_canonical (PG_FUNCTION_ARGS)
Datum int4range_subdiff (PG_FUNCTION_ARGS)
Datum int8range_subdiff (PG_FUNCTION_ARGS)
Datum numrange_subdiff (PG_FUNCTION_ARGS)
Datum daterange_subdiff (PG_FUNCTION_ARGS)
Datum tsrange_subdiff (PG_FUNCTION_ARGS)
Datum tstzrange_subdiff (PG_FUNCTION_ARGS)
TypeCacheEntryrange_get_typcache (FunctionCallInfo fcinfo, Oid rngtypid)
RangeTyperange_serialize (TypeCacheEntry *typcache, RangeBound *lower, RangeBound *upper, bool empty)
void range_deserialize (TypeCacheEntry *typcache, RangeType *range, RangeBound *lower, RangeBound *upper, bool *empty)
char range_get_flags (RangeType *range)
void range_set_contain_empty (RangeType *range)
RangeTypemake_range (TypeCacheEntry *typcache, RangeBound *lower, RangeBound *upper, bool empty)
int range_cmp_bounds (TypeCacheEntry *typcache, RangeBound *b1, RangeBound *b2)
int range_cmp_bound_values (TypeCacheEntry *typcache, RangeBound *b1, RangeBound *b2)
RangeTypemake_empty_range (TypeCacheEntry *typcache)
bool range_contains_internal (TypeCacheEntry *typcache, RangeType *r1, RangeType *r2)
bool range_contained_by_internal (TypeCacheEntry *typcache, RangeType *r1, RangeType *r2)
bool range_contains_elem_internal (TypeCacheEntry *typcache, RangeType *r, Datum val)

Define Documentation

#define RANGE_EMPTY_LITERAL   "empty"

Definition at line 44 of file rangetypes.c.

Referenced by range_deparse(), and range_parse().

#define TYPE_IS_PACKABLE (   typlen,
  typstorage 
)    ((typlen) == -1 && (typstorage) != 'p')

Definition at line 2287 of file rangetypes.c.

Referenced by datum_compute_size(), and datum_write().


Typedef Documentation

typedef struct RangeIOData RangeIOData

Function Documentation

bool bounds_adjacent ( TypeCacheEntry typcache,
RangeBound  boundA,
RangeBound  boundB 
)

Definition at line 731 of file rangetypes.c.

References Assert, FmgrInfo::fn_oid, RangeBound::inclusive, RangeBound::lower, make_range(), OidIsValid, range_cmp_bound_values(), RangeIsEmpty, and TypeCacheEntry::rng_canonical_finfo.

Referenced by range_adjacent_internal(), and spg_range_quad_inner_consistent().

{
    int         cmp;

    Assert(!boundA.lower && boundB.lower);

    cmp = range_cmp_bound_values(typcache, &boundA, &boundB);
    if (cmp < 0)
    {
        RangeType *r;

        /*
         * Bounds do not overlap; see if there are points in between.
         */

        /* in a continuous subtype, there are assumed to be points between */
        if (!OidIsValid(typcache->rng_canonical_finfo.fn_oid))
            return false;

        /*
         * The bounds are of a discrete range type; so make a range A..B and
         * see if it's empty.
         */

        /* flip the inclusion flags */
        boundA.inclusive = !boundA.inclusive;
        boundB.inclusive = !boundB.inclusive;
        /* change upper/lower labels to avoid Assert failures */
        boundA.lower = true;
        boundB.lower = false;
        r = make_range(typcache, &boundA, &boundB, false);
        return RangeIsEmpty(r);
    }
    else if (cmp == 0)
        return boundA.inclusive != boundB.inclusive;
    else
        return false;       /* bounds overlap */
}

Datum daterange_canonical ( PG_FUNCTION_ARGS   ) 

Definition at line 1307 of file rangetypes.c.

References date_pli(), DirectFunctionCall2, RangeBound::inclusive, RangeBound::infinite, Int32GetDatum, PG_GETARG_RANGE, PG_RETURN_RANGE, range_deserialize(), range_get_typcache(), range_serialize(), RangeTypeGetOid, and RangeBound::val.

{
    RangeType  *r = PG_GETARG_RANGE(0);
    TypeCacheEntry *typcache;
    RangeBound  lower;
    RangeBound  upper;
    bool        empty;

    typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r));

    range_deserialize(typcache, r, &lower, &upper, &empty);

    if (empty)
        PG_RETURN_RANGE(r);

    if (!lower.infinite && !lower.inclusive)
    {
        lower.val = DirectFunctionCall2(date_pli, lower.val, Int32GetDatum(1));
        lower.inclusive = true;
    }

    if (!upper.infinite && upper.inclusive)
    {
        upper.val = DirectFunctionCall2(date_pli, upper.val, Int32GetDatum(1));
        upper.inclusive = false;
    }

    PG_RETURN_RANGE(range_serialize(typcache, &lower, &upper, false));
}

Datum daterange_subdiff ( PG_FUNCTION_ARGS   ) 

Definition at line 1384 of file rangetypes.c.

References PG_GETARG_INT32, and PG_RETURN_FLOAT8.

static Size datum_compute_size ( Size  sz,
Datum  datum,
bool  typbyval,
char  typalign,
int16  typlen,
char  typstorage 
) [static]

Definition at line 2295 of file rangetypes.c.

References att_addlength_datum, att_align_datum, DatumGetPointer, TYPE_IS_PACKABLE, VARATT_CAN_MAKE_SHORT, and VARATT_CONVERTED_SHORT_SIZE.

Referenced by range_serialize().

{
    if (TYPE_IS_PACKABLE(typlen, typstorage) &&
        VARATT_CAN_MAKE_SHORT(DatumGetPointer(val)))
    {
        /*
         * we're anticipating converting to a short varlena header, so adjust
         * length and don't count any alignment
         */
        data_length += VARATT_CONVERTED_SHORT_SIZE(DatumGetPointer(val));
    }
    else
    {
        data_length = att_align_datum(data_length, typalign, typlen, val);
        data_length = att_addlength_datum(data_length, typlen, val);
    }

    return data_length;
}

static Pointer datum_write ( Pointer  ptr,
Datum  datum,
bool  typbyval,
char  typalign,
int16  typlen,
char  typstorage 
) [static]

Definition at line 2321 of file rangetypes.c.

References Assert, att_align_nominal, DatumGetCString, DatumGetPointer, elog, ERROR, SET_VARSIZE_SHORT, store_att_byval, TYPE_IS_PACKABLE, val, VARATT_CAN_MAKE_SHORT, VARATT_CONVERTED_SHORT_SIZE, VARATT_IS_EXTERNAL, VARATT_IS_SHORT, VARDATA, VARSIZE, and VARSIZE_SHORT.

Referenced by range_serialize().

{
    Size        data_length;

    if (typbyval)
    {
        /* pass-by-value */
        ptr = (char *) att_align_nominal(ptr, typalign);
        store_att_byval(ptr, datum, typlen);
        data_length = typlen;
    }
    else if (typlen == -1)
    {
        /* varlena */
        Pointer     val = DatumGetPointer(datum);

        if (VARATT_IS_EXTERNAL(val))
        {
            /*
             * Throw error, because we must never put a toast pointer inside a
             * range object.  Caller should have detoasted it.
             */
            elog(ERROR, "cannot store a toast pointer inside a range");
            data_length = 0;    /* keep compiler quiet */
        }
        else if (VARATT_IS_SHORT(val))
        {
            /* no alignment for short varlenas */
            data_length = VARSIZE_SHORT(val);
            memcpy(ptr, val, data_length);
        }
        else if (TYPE_IS_PACKABLE(typlen, typstorage) &&
                 VARATT_CAN_MAKE_SHORT(val))
        {
            /* convert to short varlena -- no alignment */
            data_length = VARATT_CONVERTED_SHORT_SIZE(val);
            SET_VARSIZE_SHORT(ptr, data_length);
            memcpy(ptr + 1, VARDATA(val), data_length - 1);
        }
        else
        {
            /* full 4-byte header varlena */
            ptr = (char *) att_align_nominal(ptr, typalign);
            data_length = VARSIZE(val);
            memcpy(ptr, val, data_length);
        }
    }
    else if (typlen == -2)
    {
        /* cstring ... never needs alignment */
        Assert(typalign == 'c');
        data_length = strlen(DatumGetCString(datum)) + 1;
        memcpy(ptr, DatumGetPointer(datum), data_length);
    }
    else
    {
        /* fixed-length pass-by-reference */
        ptr = (char *) att_align_nominal(ptr, typalign);
        Assert(typlen > 0);
        data_length = typlen;
        memcpy(ptr, DatumGetPointer(datum), data_length);
    }

    ptr += data_length;

    return ptr;
}

Datum elem_contained_by_range ( PG_FUNCTION_ARGS   ) 
static RangeIOData * get_range_io_data ( FunctionCallInfo  fcinfo,
Oid  rngtypid,
IOFuncSelector  func 
) [static]

Definition at line 292 of file rangetypes.c.

References elog, ereport, errcode(), errmsg(), ERROR, FunctionCallInfoData::flinfo, fmgr_info_cxt(), FmgrInfo::fn_extra, FmgrInfo::fn_mcxt, format_type_be(), get_type_io_data(), IOFunc_receive, lookup_type_cache(), MemoryContextAlloc(), NULL, OidIsValid, RangeIOData::proc, TypeCacheEntry::rngelemtype, RangeIOData::typcache, TypeCacheEntry::type_id, TYPECACHE_RANGE_INFO, RangeIOData::typiofunc, and RangeIOData::typioparam.

Referenced by range_in(), range_out(), range_recv(), and range_send().

{
    RangeIOData *cache = (RangeIOData *) fcinfo->flinfo->fn_extra;

    if (cache == NULL || cache->typcache->type_id != rngtypid)
    {
        int16       typlen;
        bool        typbyval;
        char        typalign;
        char        typdelim;

        cache = (RangeIOData *) MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
                                                   sizeof(RangeIOData));
        cache->typcache = lookup_type_cache(rngtypid, TYPECACHE_RANGE_INFO);
        if (cache->typcache->rngelemtype == NULL)
            elog(ERROR, "type %u is not a range type", rngtypid);

        /* get_type_io_data does more than we need, but is convenient */
        get_type_io_data(cache->typcache->rngelemtype->type_id,
                         func,
                         &typlen,
                         &typbyval,
                         &typalign,
                         &typdelim,
                         &cache->typioparam,
                         &cache->typiofunc);

        if (!OidIsValid(cache->typiofunc))
        {
            /* this could only happen for receive or send */
            if (func == IOFunc_receive)
                ereport(ERROR,
                        (errcode(ERRCODE_UNDEFINED_FUNCTION),
                     errmsg("no binary input function available for type %s",
                    format_type_be(cache->typcache->rngelemtype->type_id))));
            else
                ereport(ERROR,
                        (errcode(ERRCODE_UNDEFINED_FUNCTION),
                    errmsg("no binary output function available for type %s",
                    format_type_be(cache->typcache->rngelemtype->type_id))));
        }
        fmgr_info_cxt(cache->typiofunc, &cache->proc,
                      fcinfo->flinfo->fn_mcxt);

        fcinfo->flinfo->fn_extra = (void *) cache;
    }

    return cache;
}

Datum hash_range ( PG_FUNCTION_ARGS   ) 

Definition at line 1177 of file rangetypes.c.

References DatumGetUInt32, ereport, errcode(), errmsg(), ERROR, FmgrInfo::fn_oid, format_type_be(), FunctionCall1Coll(), TypeCacheEntry::hash_proc_finfo, hash_uint32(), lookup_type_cache(), OidIsValid, PG_GETARG_RANGE, PG_RETURN_INT32, range_deserialize(), range_get_flags(), range_get_typcache(), RANGE_HAS_LBOUND, RANGE_HAS_UBOUND, RangeTypeGetOid, TypeCacheEntry::rng_collation, TypeCacheEntry::rngelemtype, TypeCacheEntry::type_id, TYPECACHE_HASH_PROC_FINFO, and RangeBound::val.

{
    RangeType  *r = PG_GETARG_RANGE(0);
    uint32      result;
    TypeCacheEntry *typcache;
    TypeCacheEntry *scache;
    RangeBound  lower;
    RangeBound  upper;
    bool        empty;
    char        flags;
    uint32      lower_hash;
    uint32      upper_hash;

    typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r));

    /* deserialize */
    range_deserialize(typcache, r, &lower, &upper, &empty);
    flags = range_get_flags(r);

    /*
     * Look up the element type's hash function, if not done already.
     */
    scache = typcache->rngelemtype;
    if (!OidIsValid(scache->hash_proc_finfo.fn_oid))
    {
        scache = lookup_type_cache(scache->type_id, TYPECACHE_HASH_PROC_FINFO);
        if (!OidIsValid(scache->hash_proc_finfo.fn_oid))
            ereport(ERROR,
                    (errcode(ERRCODE_UNDEFINED_FUNCTION),
                     errmsg("could not identify a hash function for type %s",
                            format_type_be(scache->type_id))));
    }

    /*
     * Apply the hash function to each bound.
     */
    if (RANGE_HAS_LBOUND(flags))
        lower_hash = DatumGetUInt32(FunctionCall1Coll(&scache->hash_proc_finfo,
                                                      typcache->rng_collation,
                                                      lower.val));
    else
        lower_hash = 0;

    if (RANGE_HAS_UBOUND(flags))
        upper_hash = DatumGetUInt32(FunctionCall1Coll(&scache->hash_proc_finfo,
                                                      typcache->rng_collation,
                                                      upper.val));
    else
        upper_hash = 0;

    /* Merge hashes of flags and bounds */
    result = hash_uint32((uint32) flags);
    result ^= lower_hash;
    result = (result << 1) | (result >> 31);
    result ^= upper_hash;

    PG_RETURN_INT32(result);
}

Datum int4range_canonical ( PG_FUNCTION_ARGS   ) 

Definition at line 1245 of file rangetypes.c.

References DirectFunctionCall2, RangeBound::inclusive, RangeBound::infinite, Int32GetDatum, int4pl(), PG_GETARG_RANGE, PG_RETURN_RANGE, range_deserialize(), range_get_typcache(), range_serialize(), RangeTypeGetOid, and RangeBound::val.

{
    RangeType  *r = PG_GETARG_RANGE(0);
    TypeCacheEntry *typcache;
    RangeBound  lower;
    RangeBound  upper;
    bool        empty;

    typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r));

    range_deserialize(typcache, r, &lower, &upper, &empty);

    if (empty)
        PG_RETURN_RANGE(r);

    if (!lower.infinite && !lower.inclusive)
    {
        lower.val = DirectFunctionCall2(int4pl, lower.val, Int32GetDatum(1));
        lower.inclusive = true;
    }

    if (!upper.infinite && upper.inclusive)
    {
        upper.val = DirectFunctionCall2(int4pl, upper.val, Int32GetDatum(1));
        upper.inclusive = false;
    }

    PG_RETURN_RANGE(range_serialize(typcache, &lower, &upper, false));
}

Datum int4range_subdiff ( PG_FUNCTION_ARGS   ) 

Definition at line 1350 of file rangetypes.c.

References PG_GETARG_INT32, and PG_RETURN_FLOAT8.

Datum int8range_canonical ( PG_FUNCTION_ARGS   ) 

Definition at line 1276 of file rangetypes.c.

References DirectFunctionCall2, RangeBound::inclusive, RangeBound::infinite, Int64GetDatum(), int8pl(), PG_GETARG_RANGE, PG_RETURN_RANGE, range_deserialize(), range_get_typcache(), range_serialize(), RangeTypeGetOid, and RangeBound::val.

{
    RangeType  *r = PG_GETARG_RANGE(0);
    TypeCacheEntry *typcache;
    RangeBound  lower;
    RangeBound  upper;
    bool        empty;

    typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r));

    range_deserialize(typcache, r, &lower, &upper, &empty);

    if (empty)
        PG_RETURN_RANGE(r);

    if (!lower.infinite && !lower.inclusive)
    {
        lower.val = DirectFunctionCall2(int8pl, lower.val, Int64GetDatum(1));
        lower.inclusive = true;
    }

    if (!upper.infinite && upper.inclusive)
    {
        upper.val = DirectFunctionCall2(int8pl, upper.val, Int64GetDatum(1));
        upper.inclusive = false;
    }

    PG_RETURN_RANGE(range_serialize(typcache, &lower, &upper, false));
}

Datum int8range_subdiff ( PG_FUNCTION_ARGS   ) 

Definition at line 1359 of file rangetypes.c.

References PG_GETARG_INT64, and PG_RETURN_FLOAT8.

{
    int64       v1 = PG_GETARG_INT64(0);
    int64       v2 = PG_GETARG_INT64(1);

    PG_RETURN_FLOAT8((float8) v1 - (float8) v2);
}

RangeType* make_empty_range ( TypeCacheEntry typcache  ) 

Definition at line 1840 of file rangetypes.c.

References RangeBound::inclusive, RangeBound::infinite, RangeBound::lower, make_range(), and RangeBound::val.

Referenced by range_intersect(), and range_minus().

{
    RangeBound  lower;
    RangeBound  upper;

    lower.val = (Datum) 0;
    lower.infinite = false;
    lower.inclusive = false;
    lower.lower = true;

    upper.val = (Datum) 0;
    upper.infinite = false;
    upper.inclusive = false;
    upper.lower = false;

    return make_range(typcache, &lower, &upper, true);
}

RangeType* make_range ( TypeCacheEntry typcache,
RangeBound lower,
RangeBound upper,
bool  empty 
)
Datum numrange_subdiff ( PG_FUNCTION_ARGS   ) 

Definition at line 1368 of file rangetypes.c.

References DatumGetFloat8, DirectFunctionCall1, DirectFunctionCall2, numeric_float8(), numeric_sub(), PG_GETARG_DATUM, and PG_RETURN_FLOAT8.

{
    Datum       v1 = PG_GETARG_DATUM(0);
    Datum       v2 = PG_GETARG_DATUM(1);
    Datum       numresult;
    float8      floatresult;

    numresult = DirectFunctionCall2(numeric_sub, v1, v2);

    floatresult = DatumGetFloat8(DirectFunctionCall1(numeric_float8,
                                                     numresult));

    PG_RETURN_FLOAT8(floatresult);
}

Datum range_adjacent ( PG_FUNCTION_ARGS   ) 
bool range_adjacent_internal ( TypeCacheEntry typcache,
RangeType r1,
RangeType r2 
)

Definition at line 772 of file rangetypes.c.

References bounds_adjacent(), elog, ERROR, range_deserialize(), and RangeTypeGetOid.

Referenced by range_adjacent(), range_gist_consistent_int(), range_gist_consistent_leaf(), and spg_range_quad_leaf_consistent().

{
    RangeBound  lower1,
                lower2;
    RangeBound  upper1,
                upper2;
    bool        empty1,
                empty2;

    /* Different types should be prevented by ANYRANGE matching rules */
    if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
        elog(ERROR, "range types do not match");

    range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
    range_deserialize(typcache, r2, &lower2, &upper2, &empty2);

    /* An empty range is not adjacent to any other range */
    if (empty1 || empty2)
        return false;

    /*
     * Given two ranges A..B and C..D, the ranges are adjacent if and only if
     * B is adjacent to C, or D is adjacent to A.
     */
    return (bounds_adjacent(typcache, upper1, lower2) ||
            bounds_adjacent(typcache, upper2, lower1));
}

Datum range_after ( PG_FUNCTION_ARGS   ) 
bool range_after_internal ( TypeCacheEntry typcache,
RangeType r1,
RangeType r2 
)

Definition at line 676 of file rangetypes.c.

References elog, ERROR, range_cmp_bounds(), range_deserialize(), and RangeTypeGetOid.

Referenced by range_after(), range_gist_consistent_int(), range_gist_consistent_leaf(), and spg_range_quad_leaf_consistent().

{
    RangeBound  lower1,
                lower2;
    RangeBound  upper1,
                upper2;
    bool        empty1,
                empty2;

    /* Different types should be prevented by ANYRANGE matching rules */
    if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
        elog(ERROR, "range types do not match");

    range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
    range_deserialize(typcache, r2, &lower2, &upper2, &empty2);

    /* An empty range is neither before nor after any other range */
    if (empty1 || empty2)
        return false;

    return (range_cmp_bounds(typcache, &lower1, &upper2) > 0);
}

Datum range_before ( PG_FUNCTION_ARGS   ) 
bool range_before_internal ( TypeCacheEntry typcache,
RangeType r1,
RangeType r2 
)

Definition at line 638 of file rangetypes.c.

References elog, ERROR, range_cmp_bounds(), range_deserialize(), and RangeTypeGetOid.

Referenced by range_before(), range_gist_consistent_int(), range_gist_consistent_leaf(), and spg_range_quad_leaf_consistent().

{
    RangeBound  lower1,
                lower2;
    RangeBound  upper1,
                upper2;
    bool        empty1,
                empty2;

    /* Different types should be prevented by ANYRANGE matching rules */
    if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
        elog(ERROR, "range types do not match");

    range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
    range_deserialize(typcache, r2, &lower2, &upper2, &empty2);

    /* An empty range is neither before nor after any other range */
    if (empty1 || empty2)
        return false;

    return (range_cmp_bounds(typcache, &upper1, &lower2) < 0);
}

static char * range_bound_escape ( const char *  value  )  [static]

Definition at line 2150 of file rangetypes.c.

References appendStringInfoChar(), buf, StringInfoData::data, and initStringInfo().

Referenced by range_deparse().

{
    bool        nq;
    const char *ptr;
    StringInfoData buf;

    initStringInfo(&buf);

    /* Detect whether we need double quotes for this value */
    nq = (value[0] == '\0');    /* force quotes for empty string */
    for (ptr = value; *ptr; ptr++)
    {
        char        ch = *ptr;

        if (ch == '"' || ch == '\\' ||
            ch == '(' || ch == ')' ||
            ch == '[' || ch == ']' ||
            ch == ',' ||
            isspace((unsigned char) ch))
        {
            nq = true;
            break;
        }
    }

    /* And emit the string */
    if (nq)
        appendStringInfoChar(&buf, '"');
    for (ptr = value; *ptr; ptr++)
    {
        char        ch = *ptr;

        if (ch == '"' || ch == '\\')
            appendStringInfoChar(&buf, ch);
        appendStringInfoChar(&buf, ch);
    }
    if (nq)
        appendStringInfoChar(&buf, '"');

    return buf.data;
}

Datum range_cmp ( PG_FUNCTION_ARGS   ) 

Definition at line 1104 of file rangetypes.c.

References elog, ERROR, PG_GETARG_RANGE, PG_RETURN_INT32, range_cmp_bounds(), range_deserialize(), range_get_typcache(), and RangeTypeGetOid.

Referenced by range_ge(), range_gt(), range_le(), and range_lt().

{
    RangeType  *r1 = PG_GETARG_RANGE(0);
    RangeType  *r2 = PG_GETARG_RANGE(1);
    TypeCacheEntry *typcache;
    RangeBound  lower1,
                lower2;
    RangeBound  upper1,
                upper2;
    bool        empty1,
                empty2;
    int         cmp;

    /* Different types should be prevented by ANYRANGE matching rules */
    if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
        elog(ERROR, "range types do not match");

    typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r1));

    range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
    range_deserialize(typcache, r2, &lower2, &upper2, &empty2);

    /* For b-tree use, empty ranges sort before all else */
    if (empty1 && empty2)
        PG_RETURN_INT32(0);
    else if (empty1)
        PG_RETURN_INT32(-1);
    else if (empty2)
        PG_RETURN_INT32(1);

    if ((cmp = range_cmp_bounds(typcache, &lower1, &lower2)) != 0)
        PG_RETURN_INT32(cmp);

    PG_RETURN_INT32(range_cmp_bounds(typcache, &upper1, &upper2));
}

int range_cmp_bound_values ( TypeCacheEntry typcache,
RangeBound b1,
RangeBound b2 
)

Definition at line 1805 of file rangetypes.c.

References DatumGetInt32, FunctionCall2Coll(), RangeBound::infinite, RangeBound::lower, TypeCacheEntry::rng_cmp_proc_finfo, TypeCacheEntry::rng_collation, and RangeBound::val.

Referenced by bounds_adjacent(), and range_serialize().

{
    /*
     * First, handle cases involving infinity, which don't require invoking
     * the comparison proc.
     */
    if (b1->infinite && b2->infinite)
    {
        /*
         * Both are infinity, so they are equal unless one is lower and the
         * other not.
         */
        if (b1->lower == b2->lower)
            return 0;
        else
            return b1->lower ? -1 : 1;
    }
    else if (b1->infinite)
        return b1->lower ? -1 : 1;
    else if (b2->infinite)
        return b2->lower ? 1 : -1;

    /*
     * Both boundaries are finite, so compare the held values.
     */
    return DatumGetInt32(FunctionCall2Coll(&typcache->rng_cmp_proc_finfo,
                                           typcache->rng_collation,
                                           b1->val, b2->val));
}

int range_cmp_bounds ( TypeCacheEntry typcache,
RangeBound b1,
RangeBound b2 
)

Definition at line 1731 of file rangetypes.c.

References DatumGetInt32, FunctionCall2Coll(), RangeBound::inclusive, RangeBound::infinite, RangeBound::lower, TypeCacheEntry::rng_cmp_proc_finfo, TypeCacheEntry::rng_collation, and RangeBound::val.

Referenced by bound_cmp(), calc_hist_selectivity_contained(), getQuadrant(), interval_cmp_lower(), interval_cmp_upper(), range_after_internal(), range_before_internal(), range_bound_qsort_cmp(), range_cmp(), range_contains_internal(), range_eq_internal(), range_gist_double_sorting_split(), range_gist_penalty(), range_intersect(), range_minus(), range_overlaps_internal(), range_overleft_internal(), range_overright_internal(), range_super_union(), range_union(), rbound_bsearch(), single_bound_cmp(), and spg_range_quad_inner_consistent().

{
    int32       result;

    /*
     * First, handle cases involving infinity, which don't require invoking
     * the comparison proc.
     */
    if (b1->infinite && b2->infinite)
    {
        /*
         * Both are infinity, so they are equal unless one is lower and the
         * other not.
         */
        if (b1->lower == b2->lower)
            return 0;
        else
            return b1->lower ? -1 : 1;
    }
    else if (b1->infinite)
        return b1->lower ? -1 : 1;
    else if (b2->infinite)
        return b2->lower ? 1 : -1;

    /*
     * Both boundaries are finite, so compare the held values.
     */
    result = DatumGetInt32(FunctionCall2Coll(&typcache->rng_cmp_proc_finfo,
                                             typcache->rng_collation,
                                             b1->val, b2->val));

    /*
     * If the comparison is anything other than equal, we're done. If they
     * compare equal though, we still have to consider whether the boundaries
     * are inclusive or exclusive.
     */
    if (result == 0)
    {
        if (!b1->inclusive && !b2->inclusive)
        {
            /* both are exclusive */
            if (b1->lower == b2->lower)
                return 0;
            else
                return b1->lower ? 1 : -1;
        }
        else if (!b1->inclusive)
            return b1->lower ? 1 : -1;
        else if (!b2->inclusive)
            return b2->lower ? -1 : 1;
        else
        {
            /*
             * Both are inclusive and the values held are equal, so they are
             * equal regardless of whether they are upper or lower boundaries,
             * or a mix.
             */
            return 0;
        }
    }

    return result;
}

Datum range_constructor2 ( PG_FUNCTION_ARGS   ) 

Definition at line 351 of file rangetypes.c.

References get_fn_expr_rettype(), RangeBound::inclusive, RangeBound::infinite, RangeBound::lower, make_range(), PG_ARGISNULL, PG_GETARG_DATUM, PG_RETURN_RANGE, range_get_typcache(), and RangeBound::val.

{
    Datum       arg1 = PG_GETARG_DATUM(0);
    Datum       arg2 = PG_GETARG_DATUM(1);
    Oid         rngtypid = get_fn_expr_rettype(fcinfo->flinfo);
    RangeType  *range;
    TypeCacheEntry *typcache;
    RangeBound  lower;
    RangeBound  upper;

    typcache = range_get_typcache(fcinfo, rngtypid);

    lower.val = PG_ARGISNULL(0) ? (Datum) 0 : arg1;
    lower.infinite = PG_ARGISNULL(0);
    lower.inclusive = true;
    lower.lower = true;

    upper.val = PG_ARGISNULL(1) ? (Datum) 0 : arg2;
    upper.infinite = PG_ARGISNULL(1);
    upper.inclusive = false;
    upper.lower = false;

    range = make_range(typcache, &lower, &upper, false);

    PG_RETURN_RANGE(range);
}

Datum range_constructor3 ( PG_FUNCTION_ARGS   ) 

Definition at line 380 of file rangetypes.c.

References ereport, errcode(), errmsg(), ERROR, get_fn_expr_rettype(), RangeBound::inclusive, RangeBound::infinite, RangeBound::lower, make_range(), PG_ARGISNULL, PG_GETARG_DATUM, PG_GETARG_TEXT_P, PG_RETURN_RANGE, range_get_typcache(), range_parse_flags(), text_to_cstring(), and RangeBound::val.

{
    Datum       arg1 = PG_GETARG_DATUM(0);
    Datum       arg2 = PG_GETARG_DATUM(1);
    Oid         rngtypid = get_fn_expr_rettype(fcinfo->flinfo);
    RangeType  *range;
    TypeCacheEntry *typcache;
    RangeBound  lower;
    RangeBound  upper;
    char        flags;

    typcache = range_get_typcache(fcinfo, rngtypid);

    if (PG_ARGISNULL(2))
        ereport(ERROR,
                (errcode(ERRCODE_DATA_EXCEPTION),
               errmsg("range constructor flags argument must not be null")));

    flags = range_parse_flags(text_to_cstring(PG_GETARG_TEXT_P(2)));

    lower.val = PG_ARGISNULL(0) ? (Datum) 0 : arg1;
    lower.infinite = PG_ARGISNULL(0);
    lower.inclusive = (flags & RANGE_LB_INC) != 0;
    lower.lower = true;

    upper.val = PG_ARGISNULL(1) ? (Datum) 0 : arg2;
    upper.infinite = PG_ARGISNULL(1);
    upper.inclusive = (flags & RANGE_UB_INC) != 0;
    upper.lower = false;

    range = make_range(typcache, &lower, &upper, false);

    PG_RETURN_RANGE(range);
}

Datum range_contained_by ( PG_FUNCTION_ARGS   ) 
bool range_contained_by_internal ( TypeCacheEntry typcache,
RangeType r1,
RangeType r2 
)

Definition at line 2231 of file rangetypes.c.

References range_contains_internal().

Referenced by range_contained_by(), range_gist_consistent_leaf(), and spg_range_quad_leaf_consistent().

{
    return range_contains_internal(typcache, r2, r1);
}

Datum range_contains ( PG_FUNCTION_ARGS   ) 
Datum range_contains_elem ( PG_FUNCTION_ARGS   ) 
bool range_contains_elem_internal ( TypeCacheEntry typcache,
RangeType r,
Datum  val 
)

Definition at line 2240 of file rangetypes.c.

References DatumGetInt32, FunctionCall2Coll(), RangeBound::inclusive, RangeBound::infinite, range_deserialize(), TypeCacheEntry::rng_cmp_proc_finfo, TypeCacheEntry::rng_collation, and RangeBound::val.

Referenced by elem_contained_by_range(), range_contains_elem(), range_gist_consistent_int(), range_gist_consistent_leaf(), and spg_range_quad_leaf_consistent().

{
    RangeBound  lower;
    RangeBound  upper;
    bool        empty;
    int32       cmp;

    range_deserialize(typcache, r, &lower, &upper, &empty);

    if (empty)
        return false;

    if (!lower.infinite)
    {
        cmp = DatumGetInt32(FunctionCall2Coll(&typcache->rng_cmp_proc_finfo,
                                              typcache->rng_collation,
                                              lower.val, val));
        if (cmp > 0)
            return false;
        if (cmp == 0 && !lower.inclusive)
            return false;
    }

    if (!upper.infinite)
    {
        cmp = DatumGetInt32(FunctionCall2Coll(&typcache->rng_cmp_proc_finfo,
                                              typcache->rng_collation,
                                              upper.val, val));
        if (cmp < 0)
            return false;
        if (cmp == 0 && !upper.inclusive)
            return false;
    }

    return true;
}

bool range_contains_internal ( TypeCacheEntry typcache,
RangeType r1,
RangeType r2 
)

Definition at line 2199 of file rangetypes.c.

References elog, ERROR, range_cmp_bounds(), range_deserialize(), and RangeTypeGetOid.

Referenced by range_contained_by_internal(), range_contains(), range_gist_consistent_int(), range_gist_consistent_leaf(), and spg_range_quad_leaf_consistent().

{
    RangeBound  lower1;
    RangeBound  upper1;
    bool        empty1;
    RangeBound  lower2;
    RangeBound  upper2;
    bool        empty2;

    /* Different types should be prevented by ANYRANGE matching rules */
    if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
        elog(ERROR, "range types do not match");

    range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
    range_deserialize(typcache, r2, &lower2, &upper2, &empty2);

    /* If either range is empty, the answer is easy */
    if (empty2)
        return true;
    else if (empty1)
        return false;

    /* Else we must have lower1 <= lower2 and upper1 >= upper2 */
    if (range_cmp_bounds(typcache, &lower1, &lower2) > 0)
        return false;
    if (range_cmp_bounds(typcache, &upper1, &upper2) < 0)
        return false;

    return true;
}

static char * range_deparse ( char  flags,
const char *  lbound_str,
const char *  ubound_str 
) [static]
void range_deserialize ( TypeCacheEntry typcache,
RangeType range,
RangeBound lower,
RangeBound upper,
bool empty 
)

Definition at line 1595 of file rangetypes.c.

References Assert, att_addlength_pointer, att_align_pointer, fetch_att, RangeBound::inclusive, RangeBound::infinite, RangeBound::lower, RANGE_HAS_LBOUND, RANGE_HAS_UBOUND, RangeTypeGetOid, TypeCacheEntry::rngelemtype, TypeCacheEntry::typalign, TypeCacheEntry::typbyval, TypeCacheEntry::type_id, TypeCacheEntry::typlen, RangeBound::val, and VARSIZE.

Referenced by calc_hist_selectivity(), compute_range_stats(), daterange_canonical(), getQuadrant(), hash_range(), int4range_canonical(), int8range_canonical(), range_adjacent_internal(), range_after_internal(), range_before_internal(), range_cmp(), range_contains_elem_internal(), range_contains_internal(), range_eq_internal(), range_gist_double_sorting_split(), range_gist_penalty(), range_gist_single_sorting_split(), range_intersect(), range_lower(), range_minus(), range_out(), range_overlaps_internal(), range_overleft_internal(), range_overright_internal(), range_send(), range_super_union(), range_union(), range_upper(), spg_range_quad_inner_consistent(), and spg_range_quad_picksplit().

{
    char        flags;
    int16       typlen;
    bool        typbyval;
    char        typalign;
    Pointer     ptr;
    Datum       lbound;
    Datum       ubound;

    /* assert caller passed the right typcache entry */
    Assert(RangeTypeGetOid(range) == typcache->type_id);

    /* fetch the flag byte from datum's last byte */
    flags = *((char *) range + VARSIZE(range) - 1);

    /* fetch information about range's element type */
    typlen = typcache->rngelemtype->typlen;
    typbyval = typcache->rngelemtype->typbyval;
    typalign = typcache->rngelemtype->typalign;

    /* initialize data pointer just after the range OID */
    ptr = (Pointer) (range + 1);

    /* fetch lower bound, if any */
    if (RANGE_HAS_LBOUND(flags))
    {
        /* att_align_pointer cannot be necessary here */
        lbound = fetch_att(ptr, typbyval, typlen);
        ptr = (Pointer) att_addlength_pointer(ptr, typlen, ptr);
    }
    else
        lbound = (Datum) 0;

    /* fetch upper bound, if any */
    if (RANGE_HAS_UBOUND(flags))
    {
        ptr = (Pointer) att_align_pointer(ptr, typalign, typlen, ptr);
        ubound = fetch_att(ptr, typbyval, typlen);
        /* no need for att_addlength_pointer */
    }
    else
        ubound = (Datum) 0;

    /* emit results */

    *empty = (flags & RANGE_EMPTY) != 0;

    lower->val = lbound;
    lower->infinite = (flags & RANGE_LB_INF) != 0;
    lower->inclusive = (flags & RANGE_LB_INC) != 0;
    lower->lower = true;

    upper->val = ubound;
    upper->infinite = (flags & RANGE_UB_INF) != 0;
    upper->inclusive = (flags & RANGE_UB_INC) != 0;
    upper->lower = false;
}

Datum range_empty ( PG_FUNCTION_ARGS   ) 

Definition at line 465 of file rangetypes.c.

References PG_GETARG_RANGE, PG_RETURN_BOOL, RANGE_EMPTY, and range_get_flags().

{
    RangeType  *r1 = PG_GETARG_RANGE(0);
    char        flags = range_get_flags(r1);

    PG_RETURN_BOOL(flags & RANGE_EMPTY);
}

Datum range_eq ( PG_FUNCTION_ARGS   ) 
bool range_eq_internal ( TypeCacheEntry typcache,
RangeType r1,
RangeType r2 
)

Definition at line 547 of file rangetypes.c.

References elog, ERROR, range_cmp_bounds(), range_deserialize(), and RangeTypeGetOid.

Referenced by range_eq(), range_gist_consistent_leaf(), range_gist_same(), range_ne_internal(), and spg_range_quad_leaf_consistent().

{
    RangeBound  lower1,
                lower2;
    RangeBound  upper1,
                upper2;
    bool        empty1,
                empty2;

    /* Different types should be prevented by ANYRANGE matching rules */
    if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
        elog(ERROR, "range types do not match");

    range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
    range_deserialize(typcache, r2, &lower2, &upper2, &empty2);

    if (empty1 && empty2)
        return true;
    if (empty1 != empty2)
        return false;

    if (range_cmp_bounds(typcache, &lower1, &lower2) != 0)
        return false;

    if (range_cmp_bounds(typcache, &upper1, &upper2) != 0)
        return false;

    return true;
}

Datum range_ge ( PG_FUNCTION_ARGS   ) 

Definition at line 1158 of file rangetypes.c.

References cmp(), PG_RETURN_BOOL, and range_cmp().

{
    int         cmp = range_cmp(fcinfo);

    PG_RETURN_BOOL(cmp >= 0);
}

char range_get_flags ( RangeType range  ) 

Definition at line 1662 of file rangetypes.c.

References VARSIZE.

Referenced by get_gist_range_class(), hash_range(), range_empty(), range_gist_same(), range_lower_inc(), range_lower_inf(), range_out(), range_send(), range_super_union(), range_upper_inc(), and range_upper_inf().

{
    /* fetch the flag byte from datum's last byte */
    return *((char *) range + VARSIZE(range) - 1);
}

TypeCacheEntry* range_get_typcache ( FunctionCallInfo  fcinfo,
Oid  rngtypid 
)
Datum range_gt ( PG_FUNCTION_ARGS   ) 

Definition at line 1166 of file rangetypes.c.

References cmp(), PG_RETURN_BOOL, and range_cmp().

{
    int         cmp = range_cmp(fcinfo);

    PG_RETURN_BOOL(cmp > 0);
}

Datum range_in ( PG_FUNCTION_ARGS   ) 

Definition at line 79 of file rangetypes.c.

References get_range_io_data(), RangeBound::inclusive, RangeBound::infinite, InputFunctionCall(), IOFunc_input, RangeBound::lower, make_range(), PG_GETARG_CSTRING, PG_GETARG_INT32, PG_GETARG_OID, PG_RETURN_RANGE, RangeIOData::proc, RANGE_EMPTY, RANGE_HAS_LBOUND, RANGE_HAS_UBOUND, range_parse(), RangeIOData::typcache, RangeIOData::typioparam, and RangeBound::val.

{
    char       *input_str = PG_GETARG_CSTRING(0);
    Oid         rngtypoid = PG_GETARG_OID(1);
    Oid         typmod = PG_GETARG_INT32(2);
    RangeType  *range;
    RangeIOData *cache;
    char        flags;
    char       *lbound_str;
    char       *ubound_str;
    RangeBound  lower;
    RangeBound  upper;

    cache = get_range_io_data(fcinfo, rngtypoid, IOFunc_input);

    /* parse */
    range_parse(input_str, &flags, &lbound_str, &ubound_str);

    /* call element type's input function */
    if (RANGE_HAS_LBOUND(flags))
        lower.val = InputFunctionCall(&cache->proc, lbound_str,
                                      cache->typioparam, typmod);
    if (RANGE_HAS_UBOUND(flags))
        upper.val = InputFunctionCall(&cache->proc, ubound_str,
                                      cache->typioparam, typmod);

    lower.infinite = (flags & RANGE_LB_INF) != 0;
    lower.inclusive = (flags & RANGE_LB_INC) != 0;
    lower.lower = true;
    upper.infinite = (flags & RANGE_UB_INF) != 0;
    upper.inclusive = (flags & RANGE_UB_INC) != 0;
    upper.lower = false;

    /* serialize and canonicalize */
    range = make_range(cache->typcache, &lower, &upper, flags & RANGE_EMPTY);

    PG_RETURN_RANGE(range);
}

Datum range_intersect ( PG_FUNCTION_ARGS   ) 

Definition at line 1061 of file rangetypes.c.

References DatumGetBool, elog, ERROR, make_empty_range(), make_range(), PG_GETARG_RANGE, PG_RETURN_RANGE, range_cmp_bounds(), range_deserialize(), range_get_typcache(), range_overlaps(), and RangeTypeGetOid.

{
    RangeType  *r1 = PG_GETARG_RANGE(0);
    RangeType  *r2 = PG_GETARG_RANGE(1);
    TypeCacheEntry *typcache;
    RangeBound  lower1,
                lower2;
    RangeBound  upper1,
                upper2;
    bool        empty1,
                empty2;
    RangeBound *result_lower;
    RangeBound *result_upper;

    /* Different types should be prevented by ANYRANGE matching rules */
    if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
        elog(ERROR, "range types do not match");

    typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r1));

    range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
    range_deserialize(typcache, r2, &lower2, &upper2, &empty2);

    if (empty1 || empty2 || !DatumGetBool(range_overlaps(fcinfo)))
        PG_RETURN_RANGE(make_empty_range(typcache));

    if (range_cmp_bounds(typcache, &lower1, &lower2) >= 0)
        result_lower = &lower1;
    else
        result_lower = &lower2;

    if (range_cmp_bounds(typcache, &upper1, &upper2) <= 0)
        result_upper = &upper1;
    else
        result_upper = &upper2;

    PG_RETURN_RANGE(make_range(typcache, result_lower, result_upper, false));
}

Datum range_le ( PG_FUNCTION_ARGS   ) 

Definition at line 1150 of file rangetypes.c.

References cmp(), PG_RETURN_BOOL, and range_cmp().

{
    int         cmp = range_cmp(fcinfo);

    PG_RETURN_BOOL(cmp <= 0);
}

Datum range_lower ( PG_FUNCTION_ARGS   ) 

Definition at line 420 of file rangetypes.c.

References RangeBound::infinite, PG_GETARG_RANGE, PG_RETURN_DATUM, PG_RETURN_NULL, range_deserialize(), range_get_typcache(), RangeTypeGetOid, and RangeBound::val.

{
    RangeType  *r1 = PG_GETARG_RANGE(0);
    TypeCacheEntry *typcache;
    RangeBound  lower;
    RangeBound  upper;
    bool        empty;

    typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r1));

    range_deserialize(typcache, r1, &lower, &upper, &empty);

    /* Return NULL if there's no finite lower bound */
    if (empty || lower.infinite)
        PG_RETURN_NULL();

    PG_RETURN_DATUM(lower.val);
}

Datum range_lower_inc ( PG_FUNCTION_ARGS   ) 

Definition at line 475 of file rangetypes.c.

References PG_GETARG_RANGE, PG_RETURN_BOOL, range_get_flags(), and RANGE_LB_INC.

{
    RangeType  *r1 = PG_GETARG_RANGE(0);
    char        flags = range_get_flags(r1);

    PG_RETURN_BOOL(flags & RANGE_LB_INC);
}

Datum range_lower_inf ( PG_FUNCTION_ARGS   ) 

Definition at line 495 of file rangetypes.c.

References PG_GETARG_RANGE, PG_RETURN_BOOL, range_get_flags(), and RANGE_LB_INF.

{
    RangeType  *r1 = PG_GETARG_RANGE(0);
    char        flags = range_get_flags(r1);

    PG_RETURN_BOOL(flags & RANGE_LB_INF);
}

Datum range_lt ( PG_FUNCTION_ARGS   ) 

Definition at line 1142 of file rangetypes.c.

References cmp(), PG_RETURN_BOOL, and range_cmp().

{
    int         cmp = range_cmp(fcinfo);

    PG_RETURN_BOOL(cmp < 0);
}

Datum range_minus ( PG_FUNCTION_ARGS   ) 

Definition at line 946 of file rangetypes.c.

References elog, ereport, errcode(), errmsg(), ERROR, RangeBound::inclusive, RangeBound::lower, make_empty_range(), make_range(), PG_GETARG_RANGE, PG_RETURN_NULL, PG_RETURN_RANGE, range_cmp_bounds(), range_deserialize(), range_get_typcache(), and RangeTypeGetOid.

{
    RangeType  *r1 = PG_GETARG_RANGE(0);
    RangeType  *r2 = PG_GETARG_RANGE(1);
    TypeCacheEntry *typcache;
    RangeBound  lower1,
                lower2;
    RangeBound  upper1,
                upper2;
    bool        empty1,
                empty2;
    int         cmp_l1l2,
                cmp_l1u2,
                cmp_u1l2,
                cmp_u1u2;

    /* Different types should be prevented by ANYRANGE matching rules */
    if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
        elog(ERROR, "range types do not match");

    typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r1));

    range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
    range_deserialize(typcache, r2, &lower2, &upper2, &empty2);

    /* if either is empty, r1 is the correct answer */
    if (empty1 || empty2)
        PG_RETURN_RANGE(r1);

    cmp_l1l2 = range_cmp_bounds(typcache, &lower1, &lower2);
    cmp_l1u2 = range_cmp_bounds(typcache, &lower1, &upper2);
    cmp_u1l2 = range_cmp_bounds(typcache, &upper1, &lower2);
    cmp_u1u2 = range_cmp_bounds(typcache, &upper1, &upper2);

    if (cmp_l1l2 < 0 && cmp_u1u2 > 0)
        ereport(ERROR,
                (errcode(ERRCODE_DATA_EXCEPTION),
              errmsg("result of range difference would not be contiguous")));

    if (cmp_l1u2 > 0 || cmp_u1l2 < 0)
        PG_RETURN_RANGE(r1);

    if (cmp_l1l2 >= 0 && cmp_u1u2 <= 0)
        PG_RETURN_RANGE(make_empty_range(typcache));

    if (cmp_l1l2 <= 0 && cmp_u1l2 >= 0 && cmp_u1u2 <= 0)
    {
        lower2.inclusive = !lower2.inclusive;
        lower2.lower = false;   /* it will become the upper bound */
        PG_RETURN_RANGE(make_range(typcache, &lower1, &lower2, false));
    }

    if (cmp_l1l2 >= 0 && cmp_u1u2 >= 0 && cmp_l1u2 <= 0)
    {
        upper2.inclusive = !upper2.inclusive;
        upper2.lower = true;    /* it will become the lower bound */
        PG_RETURN_RANGE(make_range(typcache, &upper2, &upper1, false));
    }

    elog(ERROR, "unexpected case in range_minus");
    PG_RETURN_NULL();
}

Datum range_ne ( PG_FUNCTION_ARGS   ) 
bool range_ne_internal ( TypeCacheEntry typcache,
RangeType r1,
RangeType r2 
)

Definition at line 592 of file rangetypes.c.

References range_eq_internal().

Referenced by range_ne().

{
    return (!range_eq_internal(typcache, r1, r2));
}

Datum range_out ( PG_FUNCTION_ARGS   ) 

Definition at line 119 of file rangetypes.c.

References get_range_io_data(), IOFunc_output, OutputFunctionCall(), PG_GETARG_RANGE, PG_RETURN_CSTRING, RangeIOData::proc, range(), range_deparse(), range_deserialize(), range_get_flags(), RANGE_HAS_LBOUND, RANGE_HAS_UBOUND, RangeTypeGetOid, RangeIOData::typcache, and RangeBound::val.

Referenced by anyrange_out().

{
    RangeType  *range = PG_GETARG_RANGE(0);
    char       *output_str;
    RangeIOData *cache;
    char        flags;
    char       *lbound_str = NULL;
    char       *ubound_str = NULL;
    RangeBound  lower;
    RangeBound  upper;
    bool        empty;

    cache = get_range_io_data(fcinfo, RangeTypeGetOid(range), IOFunc_output);

    /* deserialize */
    range_deserialize(cache->typcache, range, &lower, &upper, &empty);
    flags = range_get_flags(range);

    /* call element type's output function */
    if (RANGE_HAS_LBOUND(flags))
        lbound_str = OutputFunctionCall(&cache->proc, lower.val);
    if (RANGE_HAS_UBOUND(flags))
        ubound_str = OutputFunctionCall(&cache->proc, upper.val);

    /* construct result string */
    output_str = range_deparse(flags, lbound_str, ubound_str);

    PG_RETURN_CSTRING(output_str);
}

Datum range_overlaps ( PG_FUNCTION_ARGS   ) 
bool range_overlaps_internal ( TypeCacheEntry typcache,
RangeType r1,
RangeType r2 
)

Definition at line 815 of file rangetypes.c.

References elog, ERROR, range_cmp_bounds(), range_deserialize(), and RangeTypeGetOid.

Referenced by range_gist_consistent_int(), range_gist_consistent_leaf(), range_overlaps(), and spg_range_quad_leaf_consistent().

{
    RangeBound  lower1,
                lower2;
    RangeBound  upper1,
                upper2;
    bool        empty1,
                empty2;

    /* Different types should be prevented by ANYRANGE matching rules */
    if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
        elog(ERROR, "range types do not match");

    range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
    range_deserialize(typcache, r2, &lower2, &upper2, &empty2);

    /* An empty range does not overlap any other range */
    if (empty1 || empty2)
        return false;

    if (range_cmp_bounds(typcache, &lower1, &lower2) >= 0 &&
        range_cmp_bounds(typcache, &lower1, &upper2) <= 0)
        return true;

    if (range_cmp_bounds(typcache, &lower2, &lower1) >= 0 &&
        range_cmp_bounds(typcache, &lower2, &upper1) <= 0)
        return true;

    return false;
}

Datum range_overleft ( PG_FUNCTION_ARGS   ) 
bool range_overleft_internal ( TypeCacheEntry typcache,
RangeType r1,
RangeType r2 
)

Definition at line 861 of file rangetypes.c.

References elog, ERROR, range_cmp_bounds(), range_deserialize(), and RangeTypeGetOid.

Referenced by range_gist_consistent_int(), range_gist_consistent_leaf(), range_overleft(), and spg_range_quad_leaf_consistent().

{
    RangeBound  lower1,
                lower2;
    RangeBound  upper1,
                upper2;
    bool        empty1,
                empty2;

    /* Different types should be prevented by ANYRANGE matching rules */
    if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
        elog(ERROR, "range types do not match");

    range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
    range_deserialize(typcache, r2, &lower2, &upper2, &empty2);

    /* An empty range is neither before nor after any other range */
    if (empty1 || empty2)
        return false;

    if (range_cmp_bounds(typcache, &upper1, &upper2) <= 0)
        return true;

    return false;
}

Datum range_overright ( PG_FUNCTION_ARGS   ) 
bool range_overright_internal ( TypeCacheEntry typcache,
RangeType r1,
RangeType r2 
)

Definition at line 902 of file rangetypes.c.

References elog, ERROR, PG_RETURN_BOOL, range_cmp_bounds(), range_deserialize(), and RangeTypeGetOid.

Referenced by range_gist_consistent_int(), range_gist_consistent_leaf(), range_overright(), and spg_range_quad_leaf_consistent().

{
    RangeBound  lower1,
                lower2;
    RangeBound  upper1,
                upper2;
    bool        empty1,
                empty2;

    /* Different types should be prevented by ANYRANGE matching rules */
    if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
        elog(ERROR, "range types do not match");

    range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
    range_deserialize(typcache, r2, &lower2, &upper2, &empty2);

    /* An empty range is neither before nor after any other range */
    if (empty1 || empty2)
        PG_RETURN_BOOL(false);

    if (range_cmp_bounds(typcache, &lower1, &lower2) >= 0)
        PG_RETURN_BOOL(true);

    PG_RETURN_BOOL(false);
}

static void range_parse ( const char *  input_str,
char *  flags,
char **  lbound_str,
char **  ubound_str 
) [static]

Definition at line 1942 of file rangetypes.c.

References ereport, errcode(), errdetail(), errmsg(), ERROR, pg_strncasecmp(), RANGE_EMPTY_LITERAL, and range_parse_bound().

Referenced by range_in().

{
    const char *ptr = string;
    bool        infinite;

    *flags = 0;

    /* consume whitespace */
    while (*ptr != '\0' && isspace((unsigned char) *ptr))
        ptr++;

    /* check for empty range */
    if (pg_strncasecmp(ptr, RANGE_EMPTY_LITERAL,
                       strlen(RANGE_EMPTY_LITERAL)) == 0)
    {
        *flags = RANGE_EMPTY;
        *lbound_str = NULL;
        *ubound_str = NULL;

        ptr += strlen(RANGE_EMPTY_LITERAL);

        /* the rest should be whitespace */
        while (*ptr != '\0' && isspace((unsigned char) *ptr))
            ptr++;

        /* should have consumed everything */
        if (*ptr != '\0')
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                     errmsg("malformed range literal: \"%s\"",
                            string),
                     errdetail("Junk after \"empty\" key word.")));

        return;
    }

    if (*ptr == '[')
    {
        *flags |= RANGE_LB_INC;
        ptr++;
    }
    else if (*ptr == '(')
        ptr++;
    else
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                 errmsg("malformed range literal: \"%s\"",
                        string),
                 errdetail("Missing left parenthesis or bracket.")));

    ptr = range_parse_bound(string, ptr, lbound_str, &infinite);
    if (infinite)
        *flags |= RANGE_LB_INF;

    if (*ptr == ',')
        ptr++;
    else
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                 errmsg("malformed range literal: \"%s\"",
                        string),
                 errdetail("Missing comma after lower bound.")));

    ptr = range_parse_bound(string, ptr, ubound_str, &infinite);
    if (infinite)
        *flags |= RANGE_UB_INF;

    if (*ptr == ']')
    {
        *flags |= RANGE_UB_INC;
        ptr++;
    }
    else if (*ptr == ')')
        ptr++;
    else    /* must be a comma */
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                 errmsg("malformed range literal: \"%s\"",
                        string),
                 errdetail("Too many commas.")));

    /* consume whitespace */
    while (*ptr != '\0' && isspace((unsigned char) *ptr))
        ptr++;

    if (*ptr != '\0')
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                 errmsg("malformed range literal: \"%s\"",
                        string),
                 errdetail("Junk after right parenthesis or bracket.")));
}

static const char * range_parse_bound ( const char *  string,
const char *  ptr,
char **  bound_str,
bool infinite 
) [static]

Definition at line 2051 of file rangetypes.c.

References appendStringInfoChar(), buf, StringInfoData::data, ereport, errcode(), errdetail(), errmsg(), ERROR, and initStringInfo().

Referenced by range_parse().

{
    StringInfoData buf;

    /* Check for null: completely empty input means null */
    if (*ptr == ',' || *ptr == ')' || *ptr == ']')
    {
        *bound_str = NULL;
        *infinite = true;
    }
    else
    {
        /* Extract string for this bound */
        bool        inquote = false;

        initStringInfo(&buf);
        while (inquote || !(*ptr == ',' || *ptr == ')' || *ptr == ']'))
        {
            char        ch = *ptr++;

            if (ch == '\0')
                ereport(ERROR,
                        (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                         errmsg("malformed range literal: \"%s\"",
                                string),
                         errdetail("Unexpected end of input.")));
            if (ch == '\\')
            {
                if (*ptr == '\0')
                    ereport(ERROR,
                            (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                             errmsg("malformed range literal: \"%s\"",
                                    string),
                             errdetail("Unexpected end of input.")));
                appendStringInfoChar(&buf, *ptr++);
            }
            else if (ch == '\"')
            {
                if (!inquote)
                    inquote = true;
                else if (*ptr == '\"')
                {
                    /* doubled quote within quote sequence */
                    appendStringInfoChar(&buf, *ptr++);
                }
                else
                    inquote = false;
            }
            else
                appendStringInfoChar(&buf, ch);
        }

        *bound_str = buf.data;
        *infinite = false;
    }

    return ptr;
}

static char range_parse_flags ( const char *  flags_str  )  [static]

Definition at line 1870 of file rangetypes.c.

References ereport, errcode(), errhint(), errmsg(), and ERROR.

Referenced by range_constructor3().

{
    char        flags = 0;

    if (flags_str[0] == '\0' ||
        flags_str[1] == '\0' ||
        flags_str[2] != '\0')
        ereport(ERROR,
                (errcode(ERRCODE_SYNTAX_ERROR),
                 errmsg("invalid range bound flags"),
                 errhint("Valid values are \"[]\", \"[)\", \"(]\", and \"()\".")));

    switch (flags_str[0])
    {
        case '[':
            flags |= RANGE_LB_INC;
            break;
        case '(':
            break;
        default:
            ereport(ERROR,
                    (errcode(ERRCODE_SYNTAX_ERROR),
                     errmsg("invalid range bound flags"),
                 errhint("Valid values are \"[]\", \"[)\", \"(]\", and \"()\".")));
    }

    switch (flags_str[1])
    {
        case ']':
            flags |= RANGE_UB_INC;
            break;
        case ')':
            break;
        default:
            ereport(ERROR,
                    (errcode(ERRCODE_SYNTAX_ERROR),
                     errmsg("invalid range bound flags"),
                 errhint("Valid values are \"[]\", \"[)\", \"(]\", and \"()\".")));
    }

    return flags;
}

Datum range_recv ( PG_FUNCTION_ARGS   ) 

Definition at line 157 of file rangetypes.c.

References appendBinaryStringInfo(), buf, StringInfoData::data, get_range_io_data(), RangeBound::inclusive, RangeBound::infinite, initStringInfo(), IOFunc_receive, RangeBound::lower, make_range(), pfree(), PG_GETARG_INT32, PG_GETARG_OID, PG_GETARG_POINTER, PG_RETURN_RANGE, pq_getmsgbyte(), pq_getmsgbytes(), pq_getmsgend(), pq_getmsgint(), RangeIOData::proc, RANGE_EMPTY, RANGE_HAS_LBOUND, RANGE_HAS_UBOUND, RANGE_LB_INC, RANGE_LB_INF, RANGE_UB_INC, ReceiveFunctionCall(), RangeIOData::typcache, RangeIOData::typioparam, and RangeBound::val.

{
    StringInfo  buf = (StringInfo) PG_GETARG_POINTER(0);
    Oid         rngtypoid = PG_GETARG_OID(1);
    int32       typmod = PG_GETARG_INT32(2);
    RangeType  *range;
    RangeIOData *cache;
    char        flags;
    RangeBound  lower;
    RangeBound  upper;

    cache = get_range_io_data(fcinfo, rngtypoid, IOFunc_receive);

    /* receive the flags... */
    flags = (unsigned char) pq_getmsgbyte(buf);

    /*
     * Mask out any unsupported flags, particularly RANGE_xB_NULL which would
     * confuse following tests.  Note that range_serialize will take care of
     * cleaning up any inconsistencies in the remaining flags.
     */
    flags &= (RANGE_EMPTY |
              RANGE_LB_INC |
              RANGE_LB_INF |
              RANGE_UB_INC |
              RANGE_UB_INF);

    /* receive the bounds ... */
    if (RANGE_HAS_LBOUND(flags))
    {
        uint32      bound_len = pq_getmsgint(buf, 4);
        const char *bound_data = pq_getmsgbytes(buf, bound_len);
        StringInfoData bound_buf;

        initStringInfo(&bound_buf);
        appendBinaryStringInfo(&bound_buf, bound_data, bound_len);

        lower.val = ReceiveFunctionCall(&cache->proc,
                                        &bound_buf,
                                        cache->typioparam,
                                        typmod);
        pfree(bound_buf.data);
    }
    else
        lower.val = (Datum) 0;

    if (RANGE_HAS_UBOUND(flags))
    {
        uint32      bound_len = pq_getmsgint(buf, 4);
        const char *bound_data = pq_getmsgbytes(buf, bound_len);
        StringInfoData bound_buf;

        initStringInfo(&bound_buf);
        appendBinaryStringInfo(&bound_buf, bound_data, bound_len);

        upper.val = ReceiveFunctionCall(&cache->proc,
                                        &bound_buf,
                                        cache->typioparam,
                                        typmod);
        pfree(bound_buf.data);
    }
    else
        upper.val = (Datum) 0;

    pq_getmsgend(buf);

    /* finish constructing RangeBound representation */
    lower.infinite = (flags & RANGE_LB_INF) != 0;
    lower.inclusive = (flags & RANGE_LB_INC) != 0;
    lower.lower = true;
    upper.infinite = (flags & RANGE_UB_INF) != 0;
    upper.inclusive = (flags & RANGE_UB_INC) != 0;
    upper.lower = false;

    /* serialize and canonicalize */
    range = make_range(cache->typcache, &lower, &upper, flags & RANGE_EMPTY);

    PG_RETURN_RANGE(range);
}

Datum range_send ( PG_FUNCTION_ARGS   ) 

Definition at line 238 of file rangetypes.c.

References buf, get_range_io_data(), IOFunc_send, makeStringInfo(), PG_GETARG_RANGE, PG_RETURN_BYTEA_P, PointerGetDatum, pq_begintypsend(), pq_endtypsend(), pq_sendbyte(), pq_sendbytes(), pq_sendint(), RangeIOData::proc, range(), range_deserialize(), range_get_flags(), RANGE_HAS_LBOUND, RANGE_HAS_UBOUND, RangeTypeGetOid, SendFunctionCall(), RangeIOData::typcache, RangeBound::val, VARDATA, and VARSIZE.

{
    RangeType  *range = PG_GETARG_RANGE(0);
    StringInfo  buf = makeStringInfo();
    RangeIOData *cache;
    char        flags;
    RangeBound  lower;
    RangeBound  upper;
    bool        empty;

    cache = get_range_io_data(fcinfo, RangeTypeGetOid(range), IOFunc_send);

    /* deserialize */
    range_deserialize(cache->typcache, range, &lower, &upper, &empty);
    flags = range_get_flags(range);

    /* construct output */
    pq_begintypsend(buf);

    pq_sendbyte(buf, flags);

    if (RANGE_HAS_LBOUND(flags))
    {
        Datum       bound = PointerGetDatum(SendFunctionCall(&cache->proc,
                                                             lower.val));
        uint32      bound_len = VARSIZE(bound) - VARHDRSZ;
        char       *bound_data = VARDATA(bound);

        pq_sendint(buf, bound_len, 4);
        pq_sendbytes(buf, bound_data, bound_len);
    }

    if (RANGE_HAS_UBOUND(flags))
    {
        Datum       bound = PointerGetDatum(SendFunctionCall(&cache->proc,
                                                             upper.val));
        uint32      bound_len = VARSIZE(bound) - VARHDRSZ;
        char       *bound_data = VARDATA(bound);

        pq_sendint(buf, bound_len, 4);
        pq_sendbytes(buf, bound_data, bound_len);
    }

    PG_RETURN_BYTEA_P(pq_endtypsend(buf));
}

RangeType* range_serialize ( TypeCacheEntry typcache,
RangeBound lower,
RangeBound upper,
bool  empty 
)

Definition at line 1466 of file rangetypes.c.

References Assert, datum_compute_size(), datum_write(), ereport, errcode(), errmsg(), ERROR, RangeBound::inclusive, RangeBound::infinite, RangeBound::lower, MAXALIGN, palloc0(), PG_DETOAST_DATUM_PACKED, PointerGetDatum, range_cmp_bound_values(), RANGE_HAS_LBOUND, RANGE_HAS_UBOUND, RangeType::rangetypid, TypeCacheEntry::rngelemtype, SET_VARSIZE, TypeCacheEntry::typalign, TypeCacheEntry::typbyval, TypeCacheEntry::type_id, TypeCacheEntry::typlen, TypeCacheEntry::typstorage, and RangeBound::val.

Referenced by compute_range_stats(), daterange_canonical(), int4range_canonical(), int8range_canonical(), make_range(), rangesel(), and spg_range_quad_picksplit().

{
    RangeType  *range;
    int         cmp;
    Size        msize;
    Pointer     ptr;
    int16       typlen;
    bool        typbyval;
    char        typalign;
    char        typstorage;
    char        flags = 0;

    /*
     * Verify range is not invalid on its face, and construct flags value,
     * preventing any non-canonical combinations such as infinite+inclusive.
     */
    Assert(lower->lower);
    Assert(!upper->lower);

    if (empty)
        flags |= RANGE_EMPTY;
    else
    {
        cmp = range_cmp_bound_values(typcache, lower, upper);

        /* error check: if lower bound value is above upper, it's wrong */
        if (cmp > 0)
            ereport(ERROR,
                    (errcode(ERRCODE_DATA_EXCEPTION),
                     errmsg("range lower bound must be less than or equal to range upper bound")));

        /* if bounds are equal, and not both inclusive, range is empty */
        if (cmp == 0 && !(lower->inclusive && upper->inclusive))
            flags |= RANGE_EMPTY;
        else
        {
            /* infinite boundaries are never inclusive */
            if (lower->infinite)
                flags |= RANGE_LB_INF;
            else if (lower->inclusive)
                flags |= RANGE_LB_INC;
            if (upper->infinite)
                flags |= RANGE_UB_INF;
            else if (upper->inclusive)
                flags |= RANGE_UB_INC;
        }
    }

    /* Fetch information about range's element type */
    typlen = typcache->rngelemtype->typlen;
    typbyval = typcache->rngelemtype->typbyval;
    typalign = typcache->rngelemtype->typalign;
    typstorage = typcache->rngelemtype->typstorage;

    /* Count space for varlena header and range type's OID */
    msize = sizeof(RangeType);
    Assert(msize == MAXALIGN(msize));

    /* Count space for bounds */
    if (RANGE_HAS_LBOUND(flags))
    {
        /*
         * Make sure item to be inserted is not toasted.  It is essential that
         * we not insert an out-of-line toast value pointer into a range
         * object, for the same reasons that arrays and records can't contain
         * them.  It would work to store a compressed-in-line value, but we
         * prefer to decompress and then let compression be applied to the
         * whole range object if necessary.  But, unlike arrays, we do allow
         * short-header varlena objects to stay as-is.
         */
        if (typlen == -1)
            lower->val = PointerGetDatum(PG_DETOAST_DATUM_PACKED(lower->val));

        msize = datum_compute_size(msize, lower->val, typbyval, typalign,
                                   typlen, typstorage);
    }

    if (RANGE_HAS_UBOUND(flags))
    {
        /* Make sure item to be inserted is not toasted */
        if (typlen == -1)
            upper->val = PointerGetDatum(PG_DETOAST_DATUM_PACKED(upper->val));

        msize = datum_compute_size(msize, upper->val, typbyval, typalign,
                                   typlen, typstorage);
    }

    /* Add space for flag byte */
    msize += sizeof(char);

    /* Note: zero-fill is required here, just as in heap tuples */
    range = (RangeType *) palloc0(msize);
    SET_VARSIZE(range, msize);

    /* Now fill in the datum */
    range->rangetypid = typcache->type_id;

    ptr = (char *) (range + 1);

    if (RANGE_HAS_LBOUND(flags))
    {
        Assert(lower->lower);
        ptr = datum_write(ptr, lower->val, typbyval, typalign, typlen,
                          typstorage);
    }

    if (RANGE_HAS_UBOUND(flags))
    {
        Assert(!upper->lower);
        ptr = datum_write(ptr, upper->val, typbyval, typalign, typlen,
                          typstorage);
    }

    *((char *) ptr) = flags;

    return range;
}

void range_set_contain_empty ( RangeType range  ) 

Definition at line 1676 of file rangetypes.c.

References VARSIZE.

Referenced by range_super_union().

{
    char       *flagsp;

    /* flag byte is datum's last byte */
    flagsp = (char *) range + VARSIZE(range) - 1;

    *flagsp |= RANGE_CONTAIN_EMPTY;
}

Datum range_union ( PG_FUNCTION_ARGS   ) 

Definition at line 1011 of file rangetypes.c.

References DatumGetBool, elog, ereport, errcode(), errmsg(), ERROR, make_range(), PG_GETARG_RANGE, PG_RETURN_RANGE, range_adjacent(), range_cmp_bounds(), range_deserialize(), range_get_typcache(), range_overlaps(), and RangeTypeGetOid.

{
    RangeType  *r1 = PG_GETARG_RANGE(0);
    RangeType  *r2 = PG_GETARG_RANGE(1);
    TypeCacheEntry *typcache;
    RangeBound  lower1,
                lower2;
    RangeBound  upper1,
                upper2;
    bool        empty1,
                empty2;
    RangeBound *result_lower;
    RangeBound *result_upper;

    /* Different types should be prevented by ANYRANGE matching rules */
    if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
        elog(ERROR, "range types do not match");

    typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r1));

    range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
    range_deserialize(typcache, r2, &lower2, &upper2, &empty2);

    /* if either is empty, the other is the correct answer */
    if (empty1)
        PG_RETURN_RANGE(r2);
    if (empty2)
        PG_RETURN_RANGE(r1);

    if (!DatumGetBool(range_overlaps(fcinfo)) &&
        !DatumGetBool(range_adjacent(fcinfo)))
        ereport(ERROR,
                (errcode(ERRCODE_DATA_EXCEPTION),
                 errmsg("result of range union would not be contiguous")));

    if (range_cmp_bounds(typcache, &lower1, &lower2) < 0)
        result_lower = &lower1;
    else
        result_lower = &lower2;

    if (range_cmp_bounds(typcache, &upper1, &upper2) > 0)
        result_upper = &upper1;
    else
        result_upper = &upper2;

    PG_RETURN_RANGE(make_range(typcache, result_lower, result_upper, false));
}

Datum range_upper ( PG_FUNCTION_ARGS   ) 

Definition at line 441 of file rangetypes.c.

References RangeBound::infinite, PG_GETARG_RANGE, PG_RETURN_DATUM, PG_RETURN_NULL, range_deserialize(), range_get_typcache(), RangeTypeGetOid, and RangeBound::val.

{
    RangeType  *r1 = PG_GETARG_RANGE(0);
    TypeCacheEntry *typcache;
    RangeBound  lower;
    RangeBound  upper;
    bool        empty;

    typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r1));

    range_deserialize(typcache, r1, &lower, &upper, &empty);

    /* Return NULL if there's no finite upper bound */
    if (empty || upper.infinite)
        PG_RETURN_NULL();

    PG_RETURN_DATUM(upper.val);
}

Datum range_upper_inc ( PG_FUNCTION_ARGS   ) 

Definition at line 485 of file rangetypes.c.

References PG_GETARG_RANGE, PG_RETURN_BOOL, range_get_flags(), and RANGE_UB_INC.

{
    RangeType  *r1 = PG_GETARG_RANGE(0);
    char        flags = range_get_flags(r1);

    PG_RETURN_BOOL(flags & RANGE_UB_INC);
}

Datum range_upper_inf ( PG_FUNCTION_ARGS   ) 

Definition at line 505 of file rangetypes.c.

References PG_GETARG_RANGE, PG_RETURN_BOOL, range_get_flags(), and RANGE_UB_INF.

{
    RangeType  *r1 = PG_GETARG_RANGE(0);
    char        flags = range_get_flags(r1);

    PG_RETURN_BOOL(flags & RANGE_UB_INF);
}

Datum tsrange_subdiff ( PG_FUNCTION_ARGS   ) 

Definition at line 1393 of file rangetypes.c.

References PG_GETARG_TIMESTAMP, PG_RETURN_FLOAT8, and USECS_PER_SEC.

{
    Timestamp   v1 = PG_GETARG_TIMESTAMP(0);
    Timestamp   v2 = PG_GETARG_TIMESTAMP(1);
    float8      result;

#ifdef HAVE_INT64_TIMESTAMP
    result = ((float8) v1 - (float8) v2) / USECS_PER_SEC;
#else
    result = v1 - v2;
#endif

    PG_RETURN_FLOAT8(result);
}

Datum tstzrange_subdiff ( PG_FUNCTION_ARGS   ) 

Definition at line 1409 of file rangetypes.c.

References PG_GETARG_TIMESTAMP, PG_RETURN_FLOAT8, and USECS_PER_SEC.

{
    Timestamp   v1 = PG_GETARG_TIMESTAMP(0);
    Timestamp   v2 = PG_GETARG_TIMESTAMP(1);
    float8      result;

#ifdef HAVE_INT64_TIMESTAMP
    result = ((float8) v1 - (float8) v2) / USECS_PER_SEC;
#else
    result = v1 - v2;
#endif

    PG_RETURN_FLOAT8(result);
}