MobilityDB 1.3
Loading...
Searching...
No Matches
Functions
lifting.c File Reference

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

#include "temporal/lifting.h"
#include <assert.h>
#include <postgres.h>
#include <utils/timestamp.h>
#include <meos.h>
#include <meos_internal.h>
#include "temporal/temporal_restrict.h"
#include "temporal/tsequence.h"
#include "temporal/tsequenceset.h"
#include "temporal/type_util.h"
#include "geo/tgeo_spatialfuncs.h"

Functions

static int eafunc_tcontseq_tcontseq (const TSequence *seq1, const TSequence *seq2, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function (dispatch function) More...
 
static int eafunc_tcontseq_tcontseq_discfn (const TSequence *seq1, const TSequence *seq2, LiftedFunctionInfo *lfinfo, Span *inter)
 Synchronize two temporal values and apply to them a lifted function. More...
 
static int eafunc_tcontseq_tdiscseq (const TSequence *seq1, const TSequence *seq2, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function. More...
 
static int eafunc_tcontseq_tinstant (const TSequence *seq, const TInstant *inst, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function. More...
 
static int eafunc_tcontseq_tsequenceset (const TSequence *seq, const TSequenceSet *ss, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function. More...
 
static int eafunc_tdiscseq_tcontseq (const TSequence *seq1, const TSequence *seq2, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function. More...
 
static int eafunc_tdiscseq_tdiscseq (const TSequence *seq1, const TSequence *seq2, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function. More...
 
static int eafunc_tdiscseq_tinstant (const TSequence *seq, const TInstant *inst, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function. More...
 
static int eafunc_tdiscseq_tsequenceset (const TSequence *seq, const TSequenceSet *ss, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function. More...
 
static int eafunc_tdiscstepseq_base (const TSequence *seq, Datum value, LiftedFunctionInfo *lfinfo)
 Apply a lifted function to a temporal discrete or step sequence and a base value. More...
 
int eafunc_temporal_base (const Temporal *temp, Datum value, LiftedFunctionInfo *lfinfo)
 Apply a lifted function to a temporal value and a base value. More...
 
int eafunc_temporal_temporal (const Temporal *temp1, const Temporal *temp2, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function. More...
 
static int eafunc_tinstant_base (const TInstant *inst, Datum value, LiftedFunctionInfo *lfinfo)
 Apply a lifted function to a temporal instant and a base value. More...
 
static int eafunc_tinstant_tcontseq (const TInstant *inst, const TSequence *seq, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function. More...
 
static int eafunc_tinstant_tdiscseq (const TInstant *inst, const TSequence *seq, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function. More...
 
static int eafunc_tinstant_tinstant (const TInstant *inst1, const TInstant *inst2, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function. More...
 
static int eafunc_tinstant_tsequenceset (const TInstant *inst, const TSequenceSet *ss, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function. More...
 
static int eafunc_tlinearseq_base (const TSequence *seq, Datum value, LiftedFunctionInfo *lfinfo)
 Apply a lifted function to a temporal continuous sequence and a base value. More...
 
static int eafunc_tsequence_base (const TSequence *seq, Datum value, LiftedFunctionInfo *lfinfo)
 Apply a lifted function to a temporal sequence and a base value (dispatch function) More...
 
static int eafunc_tsequenceset_base (const TSequenceSet *ss, Datum value, LiftedFunctionInfo *lfinfo)
 Apply a lifted function to a temporal sequence set and a base value. More...
 
static int eafunc_tsequenceset_tcontseq (const TSequenceSet *ss, const TSequence *seq, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function. More...
 
static int eafunc_tsequenceset_tdiscseq (const TSequenceSet *ss, const TSequence *seq, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function. More...
 
static int eafunc_tsequenceset_tinstant (const TSequenceSet *ss, const TInstant *inst, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function. More...
 
static int eafunc_tsequenceset_tsequenceset (const TSequenceSet *ss1, const TSequenceSet *ss2, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function. More...
 
static int eafunc_tstepseq_tstepseq (const TSequence *seq1, const TSequence *seq2, LiftedFunctionInfo *lfinfo, Span *inter)
 Synchronize two temporal values and apply to them a lifted function. More...
 
static void lfinfo_invert_args (LiftedFunctionInfo *lfinfo)
 Invert the arguments of the lfinfo struct. More...
 
static Datum tfunc_base (Datum value, LiftedFunctionInfo *lfinfo)
 Apply the variadic function with the optional arguments to a base value. More...
 
static Datum tfunc_base_base (Datum value1, Datum value2, LiftedFunctionInfo *lfinfo)
 
Temporaltfunc_tcontseq_tcontseq (const TSequence *seq1, const TSequence *seq2, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function. More...
 
static int tfunc_tcontseq_tcontseq_discfn (const TSequence *seq1, const TSequence *seq2, LiftedFunctionInfo *lfinfo, Span *inter, TSequence **result)
 Synchronize two temporal values and apply to them a lifted function. More...
 
static int tfunc_tcontseq_tcontseq_dispatch (const TSequence *seq1, const TSequence *seq2, LiftedFunctionInfo *lfinfo, TSequence **result)
 Synchronize two temporal values and apply to them a lifted function. More...
 
static int tfunc_tcontseq_tcontseq_single (const TSequence *seq1, const TSequence *seq2, LiftedFunctionInfo *lfinfo, Span *inter, TSequence **result)
 Synchronize two temporal values and apply to them a lifted function. More...
 
static TSequencetfunc_tcontseq_tdiscseq (const TSequence *seq1, const TSequence *seq2, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function. More...
 
static TInstanttfunc_tcontseq_tinstant (const TSequence *seq, const TInstant *inst, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function with an optional argument. More...
 
static Temporaltfunc_tcontseq_tsequenceset (const TSequence *seq, const TSequenceSet *ss, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function. More...
 
static TSequencetfunc_tdiscseq_tcontseq (const TSequence *seq1, const TSequence *seq2, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function. More...
 
TSequencetfunc_tdiscseq_tdiscseq (const TSequence *seq1, const TSequence *seq2, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function. More...
 
static TInstanttfunc_tdiscseq_tinstant (const TSequence *seq, const TInstant *inst, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function. More...
 
static TSequencetfunc_tdiscseq_tsequenceset (const TSequence *seq, const TSequenceSet *ss, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function. More...
 
Temporaltfunc_temporal (const Temporal *temp, LiftedFunctionInfo *lfinfo)
 Apply a lifted function to a temporal value (dispatch function) More...
 
Temporaltfunc_temporal_base (const Temporal *temp, Datum value, LiftedFunctionInfo *lfinfo)
 Apply a lifted function to a temporal value and a base value (dispatch function) More...
 
Temporaltfunc_temporal_temporal (const Temporal *temp1, const Temporal *temp2, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function. More...
 
TInstanttfunc_tinstant (const TInstant *inst, LiftedFunctionInfo *lfinfo)
 Apply a lifted function with the optional arguments to a temporal instant. More...
 
TInstanttfunc_tinstant_base (const TInstant *inst, Datum value, LiftedFunctionInfo *lfinfo)
 Apply a lifted function to a temporal value and the base value. More...
 
static TInstanttfunc_tinstant_tcontseq (const TInstant *inst, const TSequence *seq, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function. More...
 
static TInstanttfunc_tinstant_tdiscseq (const TInstant *inst, const TSequence *seq, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function. More...
 
TInstanttfunc_tinstant_tinstant (const TInstant *inst1, const TInstant *inst2, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function. More...
 
static TInstanttfunc_tinstant_tsequenceset (const TInstant *inst, const TSequenceSet *ss, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function. More...
 
Temporaltfunc_tlinearseq_base (const TSequence *seq, Datum value, LiftedFunctionInfo *lfinfo)
 Apply a lifted function to a temporal value and a base value. More...
 
static int tfunc_tlinearseq_base_discfn (const TSequence *seq, Datum value, LiftedFunctionInfo *lfinfo, TSequence **result)
 Apply a lifted function to a temporal value and a base value when the function has instantaneuous discontinuties. More...
 
static int tfunc_tlinearseq_base_turnpt (const TSequence *seq, Datum value, LiftedFunctionInfo *lfinfo, TSequence **result)
 Apply a lifted function to a temporal value and a base value when turning points should be added. More...
 
static int tfunc_tlinearseq_tstepseq (const TSequence *seq1, const TSequence *seq2, LiftedFunctionInfo *lfinfo, Span *inter, TSequence **result)
 Synchronize two temporal values and apply to them a lifted function. More...
 
TSequencetfunc_tsequence (const TSequence *seq, LiftedFunctionInfo *lfinfo)
 Apply a lifted function to a temporal sequence. More...
 
TSequencetfunc_tsequence_base (const TSequence *seq, Datum value, LiftedFunctionInfo *lfinfo)
 Apply a lifted function to a temporal value and a base value. More...
 
TSequenceSettfunc_tsequenceset (const TSequenceSet *ss, LiftedFunctionInfo *lfinfo)
 Apply a lifted function to a temporal sequence set. More...
 
TSequenceSettfunc_tsequenceset_base (const TSequenceSet *ss, Datum value, LiftedFunctionInfo *lfinfo)
 Apply a lifted function to a temporal value and a base value. More...
 
static Temporaltfunc_tsequenceset_tcontseq (const TSequenceSet *ss, const TSequence *seq, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function. More...
 
static TSequencetfunc_tsequenceset_tdiscseq (const TSequenceSet *ss, const TSequence *seq, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function. More...
 
static TInstanttfunc_tsequenceset_tinstant (const TSequenceSet *ss, const TInstant *inst, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted function. More...
 
TSequenceSettfunc_tsequenceset_tsequenceset (const TSequenceSet *ss1, const TSequenceSet *ss2, LiftedFunctionInfo *lfinfo)
 Synchronize two temporal values and apply to them a lifted 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 round 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 types 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 spatiotemporal 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.

Note
Before calling the function, we must cast it back to a fixed-parameter function pointer. This is necessary because arm64 handles fixed and variadic parameters differently. See: https://developer.apple.com/documentation/apple-silicon/addressing-architectural-differences-in-your-macos-code#Dont-Redeclare-a-Function-to-Have-Variable-Parameters and: https://developer.apple.com/documentation/apple-silicon/addressing-architectural-differences-in-your-macos-code#Enable-Strict-Type-Enforcement-for-Dynamic-Method-Dispatching
typedef Datum (*varfunc) (Datum, ...);
typedef Datum (*datum_func1)(Datum);
{
Datum resvalue;
if (lfinfo->numparam == 0)
{
datum_func1 noParamFunc = (datum_func1)(*lfinfo->func);
resvalue = noParamFunc(temporalinst_value(inst));
}
else if (lfinfo->numparam == 1)
{
datum_func2 oneParamFunc = (datum_func2)(*lfinfo->func);
resvalue = oneParamFunc(temporalinst_value(inst), lfinfo->param[0]);
}
else
{
"Number of function parameters not supported: %u", lfinfo->numparam);
return NULL;
}
return tinstant_make_free(resvalue, lfinfo->restype, inst->t);
}
// Definitions for TSequence, TSequence, and TSequenceSet
[...]
// Dispatch function
{
// Dispatch depending on the temporal type
[...]
}
TInstant * tfunc_tinstant(const TInstant *inst, LiftedFunctionInfo *lfinfo)
Apply a lifted function with the optional arguments to a temporal instant.
Definition: lifting.c:220
Temporal * tfunc_temporal(const Temporal *temp, LiftedFunctionInfo *lfinfo)
Apply a lifted function to a temporal value (dispatch function)
Definition: lifting.c:261
Datum(* varfunc)(Datum,...)
Definition: temporal.h:283
Datum(* datum_func2)(Datum, Datum)
Definition: temporal.h:296
Datum(* datum_func1)(Datum)
Definition: temporal.h:295
@ MEOS_ERR_INTERNAL_ERROR
Definition: meos.h:300
void meos_error(int errlevel, int errcode, const char *format,...)
Function handling error messages.
Definition: error.c:186
TInstant * tinstant_make_free(Datum value, meosType temptype, TimestampTz t)
Return a temporal instant created from the values and free the base value after the creation.
Definition: tinstant.c:275
#define ERROR
Definition: postgres.h:59
uintptr_t Datum
Definition: postgres_ext_defs.in.h:7
int numparam
Number of parameters of the function.
Definition: lifting.h:59
Datum param[MAX_PARAMS]
Datum array for the parameters of the function.
Definition: lifting.h:60
meosType restype
Type of the result of the function.
Definition: lifting.h:62
varfunc func
Variadic function that is lifted.
Definition: lifting.h:58
Definition: lifting.h:57
TimestampTz t
Timestamp (8 bytes)
Definition: meos.h:190
Structure to represent temporal values of instant subtype.
Definition: meos.h:185
Structure to represent the common structure of temporal values of any temporal subtype.
Definition: meos.h:173

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

// Transform the geometry to a geography
tgeompoint_tgeogpoint(PG_FUNCTION_ARGS)
{
memset(&lfinfo, 0, sizeof(LiftedFunctionInfo));
lfinfo->func = (varfunc) &datum_geom_to_geog;
lfinfo->numparam = 1;
lfinfo->restype = T_TGEOGPOINT;
lfinfo->tpfn_base = NULL;
lfinfo->tpfn_temp = NULL;
Temporal *result = tfunc_temporal(temp, (Datum) NULL, lfinfo);
PG_FREE_IF_COPY(temp, 0);
}
@ T_TGEOGPOINT
temporal geography point type
Definition: meos_catalog.h:105
#define PG_GETARG_TEMPORAL_P(X)
Definition: temporal.h:320
#define PG_RETURN_TEMPORAL_P(X)
Definition: temporal.h:325
tpfunc_base tpfn_base
Turning point function for temporal and base types.
Definition: lifting.h:67
tpfunc_temp tpfn_temp
Turning point function for two temporal types.
Definition: lifting.h:68
#define PGDLLEXPORT
Definition: win32.h:57