MobilityDB  1.0
Functions
lifting.c File Reference

Generic functions for lifting functions and operators on temporal types. More...

#include "general/lifting.h"
#include <assert.h>
#include <utils/timestamp.h>
#include "general/period.h"
#include "general/timeops.h"
#include "general/temporaltypes.h"
#include "general/temporal_util.h"
Include dependency graph for lifting.c:

Functions

static Datum tfunc_base (Datum value, LiftedFunctionInfo *lfinfo)
 
TInstanttfunc_tinstant (const TInstant *inst, LiftedFunctionInfo *lfinfo)
 Applies the function with the optional arguments to the temporal value. More...
 
TInstantSettfunc_tinstantset (const TInstantSet *ti, LiftedFunctionInfo *lfinfo)
 Applies the function to the temporal value. More...
 
TSequencetfunc_tsequence (const TSequence *seq, LiftedFunctionInfo *lfinfo)
 Applies the function to the temporal value. More...
 
TSequenceSettfunc_tsequenceset (const TSequenceSet *ts, LiftedFunctionInfo *lfinfo)
 Applies the function to the temporal value. More...
 
Temporaltfunc_temporal (const Temporal *temp, LiftedFunctionInfo *lfinfo)
 Applies the function to the temporal value (dispatch function) More...
 
static Datum tfunc_base_base (Datum value1, Datum value2, LiftedFunctionInfo *lfinfo)
 
TInstanttfunc_tinstant_base (const TInstant *inst, Datum value, LiftedFunctionInfo *lfinfo)
 Applies the function to the temporal value and the base value. More...
 
TInstantSettfunc_tinstantset_base (const TInstantSet *ti, Datum value, LiftedFunctionInfo *lfinfo)
 Applies the function to the temporal value and the base value. More...
 
static int tfunc_tsequence_base_scan (TSequence **result, const TSequence *seq, Datum value, LiftedFunctionInfo *lfinfo)
 Applies the function to the temporal value and the base value when no turning points should be added and when the function does not have instantaneous discontinuities. More...
 
static int tfunc_tsequence_base_turnpt (TSequence **result, const TSequence *seq, Datum value, LiftedFunctionInfo *lfinfo)
 Applies the function to the temporal value and the base value when turning points should be added. More...
 
static int tfunc_tsequence_base_discont (TSequence **result, const TSequence *seq, Datum value, LiftedFunctionInfo *lfinfo)
 Applies the function to the temporal value and the base value when the function has instantaneuous discontinuties. More...
 
Temporaltfunc_tsequence_base (const TSequence *seq, Datum value, LiftedFunctionInfo *lfinfo)
 Applies the function to the temporal value and the base value. More...
 
TSequenceSettfunc_tsequenceset_base (const TSequenceSet *ts, Datum value, LiftedFunctionInfo *lfinfo)
 Applies the function to the temporal value and the base value. More...
 
Temporaltfunc_temporal_base (const Temporal *temp, Datum value, LiftedFunctionInfo *lfinfo)
 Applies the function to the temporal value and the base value (dispatch function) More...
 
TInstantsync_tfunc_tinstant_tinstant (const TInstant *inst1, const TInstant *inst2, LiftedFunctionInfo *lfinfo)
 Synchronizes the temporal values and applies to them the function. More...
 
static TInstantsync_tfunc_tinstantset_tinstant (const TInstantSet *ti, const TInstant *inst, LiftedFunctionInfo *lfinfo)
 Synchronizes the temporal values and applies to them the function. More...
 
static void lfinfo_invert (LiftedFunctionInfo *lfinfo)
 Invert the arguments of the lfinfo struct. More...
 
static TInstantsync_tfunc_tinstant_tinstantset (const TInstant *inst, const TInstantSet *ti, LiftedFunctionInfo *lfinfo)
 Synchronizes the temporal values and applies to them the function. More...
 
static TInstantsync_tfunc_tsequence_tinstant (const TSequence *seq, const TInstant *inst, LiftedFunctionInfo *lfinfo)
 Synchronizes the temporal values and applies to them the function with the optional argument. More...
 
static TInstantsync_tfunc_tinstant_tsequence (const TInstant *inst, const TSequence *seq, LiftedFunctionInfo *lfinfo)
 Synchronizes the temporal values and applies to them the function with the optional argument. More...
 
static TInstantsync_tfunc_tsequenceset_tinstant (const TSequenceSet *ts, const TInstant *inst, LiftedFunctionInfo *lfinfo)
 Synchronizes the temporal values and applies to them the function with the optional argument. More...
 
static TInstantsync_tfunc_tinstant_tsequenceset (const TInstant *inst, const TSequenceSet *ts, LiftedFunctionInfo *lfinfo)
 Synchronizes the temporal values and applies to them the function with the optional argument. More...
 
TInstantSetsync_tfunc_tinstantset_tinstantset (const TInstantSet *ti1, const TInstantSet *ti2, LiftedFunctionInfo *lfinfo)
 Synchronizes the temporal values and applies to them the function with the optional argument. More...
 
static TInstantSetsync_tfunc_tsequence_tinstantset (const TSequence *seq, const TInstantSet *ti, LiftedFunctionInfo *lfinfo)
 Synchronizes the temporal values and applies to them the function with the optional argument. More...
 
static TInstantSetsync_tfunc_tinstantset_tsequence (const TInstantSet *ti, const TSequence *seq, LiftedFunctionInfo *lfinfo)
 Synchronizes the temporal values and applies to them the function with the optional argument. More...
 
static TInstantSetsync_tfunc_tsequenceset_tinstantset (const TSequenceSet *ts, const TInstantSet *ti, LiftedFunctionInfo *lfinfo)
 Synchronizes the temporal values and applies to them the function with the optional argument. More...
 
static TInstantSetsync_tfunc_tinstantset_tsequenceset (const TInstantSet *ti, const TSequenceSet *ts, LiftedFunctionInfo *lfinfo)
 Synchronizes the temporal values and applies to them the function with the optional argument. More...
 
static int sync_tfunc_tsequence_tsequence_lineareq (TSequence **result, const TSequence *seq1, const TSequence *seq2, LiftedFunctionInfo *lfinfo, Period *inter)
 Synchronizes the temporal values and applies to them the function. More...
 
static int sync_tfunc_tsequence_tsequence_linearstep (TSequence **result, const TSequence *seq1, const TSequence *seq2, LiftedFunctionInfo *lfinfo, Period *inter)
 Synchronizes the temporal values and applies to them the function. More...
 
static int sync_tfunc_tsequence_tsequence_discont (TSequence **result, const TSequence *seq1, const TSequence *seq2, LiftedFunctionInfo *lfinfo, Period *inter)
 Synchronizes the temporal values and applies to them the function. More...
 
static int sync_tfunc_tsequence_tsequence_dispatch (TSequence **result, const TSequence *seq1, const TSequence *seq2, LiftedFunctionInfo *lfinfo)
 Synchronizes the temporal values and applies to them the function (dispatch function) More...
 
static Temporalsync_tfunc_tsequence_tsequence (const TSequence *seq1, const TSequence *seq2, LiftedFunctionInfo *lfinfo)
 Synchronizes the temporal values and applies to them the function. More...
 
static TSequenceSetsync_tfunc_tsequenceset_tsequence (const TSequenceSet *ts, const TSequence *seq, LiftedFunctionInfo *lfinfo)
 Synchronizes the temporal values and applies to them the function. More...
 
static TSequenceSetsync_tfunc_tsequence_tsequenceset (const TSequence *seq, const TSequenceSet *ts, LiftedFunctionInfo *lfinfo)
 Synchronizes the temporal values and applies to them the function. More...
 
static TSequenceSetsync_tfunc_tsequenceset_tsequenceset (const TSequenceSet *ts1, const TSequenceSet *ts2, LiftedFunctionInfo *lfinfo)
 Synchronizes the temporal values and applies to them the function. More...
 
Temporalsync_tfunc_temporal_temporal (const Temporal *temp1, const Temporal *temp2, LiftedFunctionInfo *lfinfo)
 Synchronizes the temporal values and applies to them the function (dispatch function) More...
 

Detailed Description

Generic functions for lifting functions and operators on temporal types.

These functions are used for lifting arithmetic operators (+, -, *, /), Boolean operators (and, or, not), comparisons (<, <=, >, >=), distance (<->), spatial relationships (tcontains), etc.

The lifting of functions and operators must take into account the following characteristic of the function to be lifted

  1. The number of arguments of the function
    • unary functions, such as degrees for temporal floats or setPrecision for temporal points.
    • binary functions and operators, such as arithmetic operators and comparisons (e.g., + or <) or spatial relationships functions (e.g.,tintersects).
  2. The type of the arguments for binary functions
    • a temporal type and a base type. In this case the non-lifted function is applied to each instant of the temporal type.
    • two temporal types. In this case the operands must be synchronized and the function is applied to each pair of synchronized instants.
  3. Whether the type of the arguments may vary. For example, temporal numbers can be of different base type (that is, integer and float). Therefore, the Oids of the arguments must be taken into account when computing binary operators (e.g., + or <) for temporal numbers.
  4. The number of optional parameters of the function
    • no arguments, such as most spatial relationships functions (e.g., tintersects).
    • one argument, such as spatial relationships functions that need an additional parameter (e.g., tdwithin).
    • two arguments, e.g., when assembling a temporal point from two temporal floats, the SRID and a boolean flag stating whether the resulting temporal point is geometric or geographic are needed.
  5. Whether the function has instantaneous discontinuities at the crossings. Examples of such functions are temporal comparisons for temporal floats or temporal spatial relationships since the value of the result may change immediately before, at, or immediately after a crossing.
  6. Whether intermediate points between synchronized instants must be added to take into account the crossings or the turning points (or local minimum/maximum) of the function. For example, tfloat + tfloat only needs to synchronize the arguments while tfloat * tfloat requires in addition to add the turning point, which is the timestamp between the two consecutive synchronized instants in which the linear functions defined by the two segments are equal.

Examples

A struct named LiftedFunctionInfo is used to describe the above characteristics of the function to be lifted. Such struct is filled by the calling function and is passed through the dispatch functions. To avoid code redundancy when coping with functions with 2, 3, and 4 arguments, variadic function pointers are used. The idea is sketched next.

typedef Datum (*varfunc) (Datum, ...);
{
Datum resvalue;
if (lfinfo->numparam == 1)
resvalue = (*lfinfo->func)(temporalinst_value(inst));
else if (lfinfo->numparam == 2)
resvalue = (*lfinfo->func)(temporalinst_value(inst), lfinfo->param[0]);
resvalue = (*lfinfo->func)(temporalinst_value(inst), lfinfo->param[0]);
else
elog(ERROR, "Number of function parameters not supported: %u",
lfinfo->numparam);
TInstant *result = tinstant_make(resvalue, inst->t, lfinfo->restypid);
DATUM_FREE(resvalue, lfinfo->restypid);
return result;
}
// Definitions for TInstantSet, TSequence, and TSequenceSet
[...]
// Dispatch function
{
// Dispatch depending on the temporal type
[...]
}

An example of use of the lifting functions is given next.

// Transform the geometry to a geography
PGDLLEXPORT Datum
tgeompoint_to_tgeogpoint(PG_FUNCTION_ARGS)
{
// We only need to fill these parameters for tfunc_temporal
memset(&lfinfo, 0, sizeof(LiftedFunctionInfo));
lfinfo->func = (varfunc) &geom_to_geog;
lfinfo->numparam = 1;
lfinfo->tpfunc_base = NULL;
lfinfo->tpfunc = NULL;
Temporal *result = tfunc_temporal(temp, (Datum) NULL, lfinfo);
PG_FREE_IF_COPY(temp, 0);
PG_RETURN_POINTER(result);
}