1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
/**********************************************************************
* $Id: lwgeom_accum.c 7197 2011-05-19 02:32:27Z pramsey $
*
* PostGIS - Spatial Types for PostgreSQL
* http://postgis.refractions.net
* Copyright 2009 Paul Ramsey <pramsey@opengeo.org>
*
* This is free software; you can redistribute and/or modify it under
* the terms of the GNU General Public Licence. See the COPYING file.
*
**********************************************************************/
#include "postgres.h"
#include "fmgr.h"
#include "funcapi.h"
#include "access/tupmacs.h"
#include "utils/array.h"
#include "utils/lsyscache.h"
#include "liblwgeom.h"
#include "lwgeom_pg.h"
/* Local prototypes */
Datum PGISDirectFunctionCall1(PGFunction func, Datum arg1);
Datum pgis_geometry_accum_transfn(PG_FUNCTION_ARGS);
Datum pgis_geometry_accum_finalfn(PG_FUNCTION_ARGS);
Datum pgis_geometry_union_finalfn(PG_FUNCTION_ARGS);
Datum pgis_geometry_collect_finalfn(PG_FUNCTION_ARGS);
Datum pgis_geometry_polygonize_finalfn(PG_FUNCTION_ARGS);
Datum pgis_geometry_makeline_finalfn(PG_FUNCTION_ARGS);
Datum pgis_abs_in(PG_FUNCTION_ARGS);
Datum pgis_abs_out(PG_FUNCTION_ARGS);
/* External prototypes */
Datum pgis_union_geometry_array(PG_FUNCTION_ARGS);
Datum LWGEOM_collect_garray(PG_FUNCTION_ARGS);
Datum polygonize_garray(PG_FUNCTION_ARGS);
Datum LWGEOM_makeline_garray(PG_FUNCTION_ARGS);
/** @file
** Versions of PostgreSQL < 8.4 perform array accumulation internally using
** pass by value, which is very slow working with large/many geometries.
** Hence PostGIS currently implements its own aggregate for building
** geometry arrays using pass by reference, which is significantly faster and
** similar to the method used in PostgreSQL 8.4.
**
** Hence we can revert this to the original aggregate functions from 1.3 at
** whatever point PostgreSQL 8.4 becomes the minimum version we support :)
*/
/**
** To pass the internal ArrayBuildState pointer between the
** transfn and finalfn we need to wrap it into a custom type first,
** the pgis_abs type in our case.
*/
typedef struct
{
ArrayBuildState *a;
}
pgis_abs;
/**
** We're never going to use this type externally so the in/out
** functions are dummies.
*/
PG_FUNCTION_INFO_V1(pgis_abs_in);
Datum
pgis_abs_in(PG_FUNCTION_ARGS)
{
ereport(ERROR,(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("function pgis_abs_in not implemented")));
PG_RETURN_POINTER(NULL);
}
PG_FUNCTION_INFO_V1(pgis_abs_out);
Datum
pgis_abs_out(PG_FUNCTION_ARGS)
{
ereport(ERROR,(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("function pgis_abs_out not implemented")));
PG_RETURN_POINTER(NULL);
}
/**
** The transfer function hooks into the PostgreSQL accumArrayResult()
** function (present since 8.0) to build an array in a side memory
** context.
*/
PG_FUNCTION_INFO_V1(pgis_geometry_accum_transfn);
Datum
pgis_geometry_accum_transfn(PG_FUNCTION_ARGS)
{
Oid arg1_typeid = get_fn_expr_argtype(fcinfo->flinfo, 1);
MemoryContext aggcontext;
ArrayBuildState *state;
pgis_abs *p;
Datum elem;
if (arg1_typeid == InvalidOid)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("could not determine input data type")));
if (fcinfo->context && IsA(fcinfo->context, AggState))
aggcontext = ((AggState *) fcinfo->context)->aggcontext;
#if POSTGIS_PGSQL_VERSION == 84
else if (fcinfo->context && IsA(fcinfo->context, WindowAggState))
aggcontext = ((WindowAggState *) fcinfo->context)->wincontext;
#endif
#if POSTGIS_PGSQL_VERSION > 84
else if (fcinfo->context && IsA(fcinfo->context, WindowAggState))
aggcontext = ((WindowAggState *) fcinfo->context)->aggcontext;
#endif
else
{
/* cannot be called directly because of dummy-type argument */
elog(ERROR, "array_agg_transfn called in non-aggregate context");
aggcontext = NULL; /* keep compiler quiet */
}
if ( PG_ARGISNULL(0) )
{
p = (pgis_abs*) palloc(sizeof(pgis_abs));
p->a = NULL;
}
else
{
p = (pgis_abs*) PG_GETARG_POINTER(0);
}
state = p->a;
elem = PG_ARGISNULL(1) ? (Datum) 0 : PG_GETARG_DATUM(1);
state = accumArrayResult(state,
elem,
PG_ARGISNULL(1),
arg1_typeid,
aggcontext);
p->a = state;
PG_RETURN_POINTER(p);
}
Datum pgis_accum_finalfn(pgis_abs *p, MemoryContext mctx, FunctionCallInfo fcinfo);
/**
** The final function rescues the built array from the side memory context
** using the PostgreSQL built-in function makeMdArrayResult
*/
Datum
pgis_accum_finalfn(pgis_abs *p, MemoryContext mctx, FunctionCallInfo fcinfo)
{
int dims[1];
int lbs[1];
ArrayBuildState *state;
Datum result;
/* cannot be called directly because of internal-type argument */
Assert(fcinfo->context &&
(IsA(fcinfo->context, AggState)
#if POSTGIS_PGSQL_VERSION >= 84
|| IsA(fcinfo->context, WindowAggState)
#endif
));
state = p->a;
dims[0] = state->nelems;
lbs[0] = 1;
#if POSTGIS_PGSQL_VERSION < 84
result = makeMdArrayResult(state, 1, dims, lbs, mctx);
#else
result = makeMdArrayResult(state, 1, dims, lbs, mctx, false);
#endif
return result;
}
/**
** The "accum" final function just returns the geometry[]
*/
PG_FUNCTION_INFO_V1(pgis_geometry_accum_finalfn);
Datum
pgis_geometry_accum_finalfn(PG_FUNCTION_ARGS)
{
pgis_abs *p;
Datum result = 0;
if (PG_ARGISNULL(0))
PG_RETURN_NULL(); /* returns null iff no input values */
p = (pgis_abs*) PG_GETARG_POINTER(0);
result = pgis_accum_finalfn(p, CurrentMemoryContext, fcinfo);
PG_RETURN_DATUM(result);
}
/**
* The "accum" final function passes the geometry[] to a union
* conversion before returning the result.
*/
PG_FUNCTION_INFO_V1(pgis_geometry_union_finalfn);
Datum
pgis_geometry_union_finalfn(PG_FUNCTION_ARGS)
{
pgis_abs *p;
Datum result = 0;
Datum geometry_array = 0;
if (PG_ARGISNULL(0))
PG_RETURN_NULL(); /* returns null iff no input values */
p = (pgis_abs*) PG_GETARG_POINTER(0);
geometry_array = pgis_accum_finalfn(p, CurrentMemoryContext, fcinfo);
result = PGISDirectFunctionCall1( pgis_union_geometry_array, geometry_array );
if (!result)
PG_RETURN_NULL();
PG_RETURN_DATUM(result);
}
/**
* The "collect" final function passes the geometry[] to a geometrycollection
* conversion before returning the result.
*/
PG_FUNCTION_INFO_V1(pgis_geometry_collect_finalfn);
Datum
pgis_geometry_collect_finalfn(PG_FUNCTION_ARGS)
{
pgis_abs *p;
Datum result = 0;
Datum geometry_array = 0;
if (PG_ARGISNULL(0))
PG_RETURN_NULL(); /* returns null iff no input values */
p = (pgis_abs*) PG_GETARG_POINTER(0);
geometry_array = pgis_accum_finalfn(p, CurrentMemoryContext, fcinfo);
result = PGISDirectFunctionCall1( LWGEOM_collect_garray, geometry_array );
if (!result)
PG_RETURN_NULL();
PG_RETURN_DATUM(result);
}
/**
* The "polygonize" final function passes the geometry[] to a polygonization
* before returning the result.
*/
PG_FUNCTION_INFO_V1(pgis_geometry_polygonize_finalfn);
Datum
pgis_geometry_polygonize_finalfn(PG_FUNCTION_ARGS)
{
pgis_abs *p;
Datum result = 0;
Datum geometry_array = 0;
if (PG_ARGISNULL(0))
PG_RETURN_NULL(); /* returns null iff no input values */
p = (pgis_abs*) PG_GETARG_POINTER(0);
geometry_array = pgis_accum_finalfn(p, CurrentMemoryContext, fcinfo);
result = DirectFunctionCall1( polygonize_garray, geometry_array );
PG_RETURN_DATUM(result);
}
/**
* The "makeline" final function passes the geometry[] to a line builder
* before returning the result.
*/
PG_FUNCTION_INFO_V1(pgis_geometry_makeline_finalfn);
Datum
pgis_geometry_makeline_finalfn(PG_FUNCTION_ARGS)
{
pgis_abs *p;
Datum result = 0;
Datum geometry_array = 0;
if (PG_ARGISNULL(0))
PG_RETURN_NULL(); /* returns null iff no input values */
p = (pgis_abs*) PG_GETARG_POINTER(0);
geometry_array = pgis_accum_finalfn(p, CurrentMemoryContext, fcinfo);
result = PGISDirectFunctionCall1( LWGEOM_makeline_garray, geometry_array );
if (!result)
PG_RETURN_NULL();
PG_RETURN_DATUM(result);
}
/**
* A modified version of PostgreSQL's DirectFunctionCall1 which allows NULL results; this
* is required for aggregates that return NULL.
*/
Datum
PGISDirectFunctionCall1(PGFunction func, Datum arg1)
{
FunctionCallInfoData fcinfo;
Datum result;
#if POSTGIS_PGSQL_VERSION > 90
/* PgSLQ 9.1 requires five arcuments to InitFunctionCallInfoData */
InitFunctionCallInfoData(fcinfo, NULL, 1, InvalidOid, NULL, NULL);
#else
InitFunctionCallInfoData(fcinfo, NULL, 1, NULL, NULL);
#endif
fcinfo.arg[0] = arg1;
fcinfo.argnull[0] = false;
result = (*func) (&fcinfo);
/* Check for null result, returning a "NULL" Datum if indicated */
if (fcinfo.isnull)
return (Datum) 0;
return result;
}
|