22 #include "../postgis_config.h"
24 #if POSTGIS_PGSQL_VERSION >= 120
29 #include "access/htup_details.h"
30 #include "access/stratnum.h"
31 #include "catalog/namespace.h"
32 #include "catalog/pg_opfamily.h"
33 #include "catalog/pg_type_d.h"
34 #include "catalog/pg_am_d.h"
35 #include "nodes/supportnodes.h"
36 #include "nodes/nodeFuncs.h"
37 #include "nodes/makefuncs.h"
38 #include "optimizer/optimizer.h"
39 #include "parser/parse_func.h"
40 #include "utils/array.h"
41 #include "utils/builtins.h"
42 #include "utils/lsyscache.h"
43 #include "utils/numeric.h"
44 #include "utils/syscache.h"
48 #include "lwgeom_pg.h"
51 Datum postgis_index_supportfn(PG_FUNCTION_ARGS);
59 ST_INTERSECTS_IDX = 0,
64 ST_3DINTERSECTS_IDX = 5,
65 ST_CONTAINSPROPERLY_IDX = 6,
70 ST_DFULLYWITHIN_IDX = 11,
71 ST_3DDWITHIN_IDX = 12,
72 ST_3DDFULLYWITHIN_IDX = 13,
73 ST_LINECROSSINGDIRECTION_IDX = 14,
74 ST_ORDERINGEQUALS_IDX = 15,
78 static const int16 GeometryStrategies[] = {
79 [ST_INTERSECTS_IDX] = RTOverlapStrategyNumber,
80 [ST_DWITHIN_IDX] = RTOverlapStrategyNumber,
81 [ST_CONTAINS_IDX] = RTContainsStrategyNumber,
82 [ST_WITHIN_IDX] = RTContainedByStrategyNumber,
83 [ST_TOUCHES_IDX] = RTOverlapStrategyNumber,
84 [ST_3DINTERSECTS_IDX] = RTOverlapStrategyNumber,
85 [ST_CONTAINSPROPERLY_IDX] = RTContainsStrategyNumber,
86 [ST_COVEREDBY_IDX] = RTContainedByStrategyNumber,
87 [ST_OVERLAPS_IDX] = RTOverlapStrategyNumber,
88 [ST_COVERS_IDX] = RTContainsStrategyNumber,
89 [ST_CROSSES_IDX] = RTOverlapStrategyNumber,
90 [ST_DFULLYWITHIN_IDX] = RTOverlapStrategyNumber,
91 [ST_3DDWITHIN_IDX] = RTOverlapStrategyNumber,
92 [ST_3DDFULLYWITHIN_IDX] = RTOverlapStrategyNumber,
93 [ST_LINECROSSINGDIRECTION_IDX] = RTOverlapStrategyNumber,
94 [ST_ORDERINGEQUALS_IDX] = RTSameStrategyNumber,
95 [ST_EQUALS_IDX] = RTSameStrategyNumber
99 static const int16 GeographyStrategies[] = {
100 [ST_INTERSECTS_IDX] = RTOverlapStrategyNumber,
101 [ST_DWITHIN_IDX] = RTOverlapStrategyNumber,
102 [ST_CONTAINS_IDX] = InvalidStrategy,
103 [ST_WITHIN_IDX] = InvalidStrategy,
104 [ST_TOUCHES_IDX] = InvalidStrategy,
105 [ST_3DINTERSECTS_IDX] = InvalidStrategy,
106 [ST_CONTAINSPROPERLY_IDX] = InvalidStrategy,
107 [ST_COVEREDBY_IDX] = RTOverlapStrategyNumber,
108 [ST_OVERLAPS_IDX] = InvalidStrategy,
109 [ST_COVERS_IDX] = RTOverlapStrategyNumber,
110 [ST_CROSSES_IDX] = InvalidStrategy,
111 [ST_DFULLYWITHIN_IDX] = InvalidStrategy,
112 [ST_3DDWITHIN_IDX] = InvalidStrategy,
113 [ST_3DDFULLYWITHIN_IDX] = InvalidStrategy,
114 [ST_LINECROSSINGDIRECTION_IDX] = InvalidStrategy,
115 [ST_ORDERINGEQUALS_IDX] = InvalidStrategy,
116 [ST_EQUALS_IDX] = InvalidStrategy
120 get_strategy_by_type(Oid first_type, uint16_t index)
122 if (first_type == postgis_oid(GEOMETRYOID))
124 return GeometryStrategies[index];
127 if (first_type == postgis_oid(GEOGRAPHYOID))
129 return GeographyStrategies[index];
132 return InvalidStrategy;
158 static const IndexableFunction IndexableFunctions[] = {
159 {
"st_intersects", ST_INTERSECTS_IDX, 2, 0, 2},
160 {
"st_dwithin", ST_DWITHIN_IDX, 3, 3, 2},
161 {
"st_contains", ST_CONTAINS_IDX, 2, 0, 2},
162 {
"st_within", ST_WITHIN_IDX, 2, 0, 2},
163 {
"st_touches", ST_TOUCHES_IDX, 2, 0, 2},
164 {
"st_3dintersects", ST_3DINTERSECTS_IDX, 2, 0, 3},
165 {
"st_containsproperly", ST_CONTAINSPROPERLY_IDX, 2, 0, 2},
166 {
"st_coveredby", ST_COVEREDBY_IDX, 2, 0, 2},
167 {
"st_overlaps", ST_OVERLAPS_IDX, 2, 0, 2},
168 {
"st_covers", ST_COVERS_IDX, 2, 0, 2},
169 {
"st_crosses", ST_CROSSES_IDX, 2, 0, 2},
170 {
"st_dfullywithin", ST_DFULLYWITHIN_IDX, 3, 3, 2},
171 {
"st_3ddwithin", ST_3DDWITHIN_IDX, 3, 3, 3},
172 {
"st_3ddfullywithin", ST_3DDFULLYWITHIN_IDX, 3, 3, 3},
173 {
"st_linecrossingdirection", ST_LINECROSSINGDIRECTION_IDX, 2, 0, 2},
174 {
"st_orderingequals", ST_ORDERINGEQUALS_IDX, 2, 0, 2},
175 {
"st_equals", ST_EQUALS_IDX, 2, 0, 2},
187 const char *opfamilyname;
191 static const OpFamilyDim OpFamilyDims[] = {
192 {
"gist_geometry_ops_2d", 2},
193 {
"gist_geometry_ops_nd", 3},
194 {
"brin_geometry_inclusion_ops_2d", 2},
195 {
"brin_geometry_inclusion_ops_3d", 3},
196 {
"brin_geometry_inclusion_ops_4d", 3},
197 {
"spgist_geometry_ops_2d", 2},
198 {
"spgist_geometry_ops_3d", 3},
199 {
"spgist_geometry_ops_nd", 3},
212 needsSpatialIndex(Oid funcid, IndexableFunction *idxfn)
214 const IndexableFunction *idxfns = IndexableFunctions;
215 const char *fn_name = get_func_name(funcid);
219 if(strcmp(idxfns->fn_name, fn_name) == 0)
226 while (idxfns->fn_name);
232 opFamilyDim(
const char* opfamily)
234 const OpFamilyDim *idxdims = OpFamilyDims;
237 if(strcmp(idxdims->opfamilyname, opfamily) == 0)
239 return idxdims->dims;
243 while (idxdims->opfamilyname);
254 opFamilyAmOid(Oid opfamilyoid, uint8_t* dims)
256 Form_pg_opfamily familyform;
259 HeapTuple familytup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamilyoid));
260 if (!HeapTupleIsValid(familytup))
261 elog(ERROR,
"cache lookup failed for operator family %u", opfamilyoid);
262 familyform = (Form_pg_opfamily) GETSTRUCT(familytup);
263 opfamilyam = familyform->opfmethod;
264 opfamilyname = NameStr(familyform->opfname);
265 elog(DEBUG3,
"%s: found opfamily %s [%u]", __func__, opfamilyname, opfamilyam);
267 *dims = opFamilyDim(opfamilyname);
269 ReleaseSysCache(familytup);
280 expandFunctionOid(Oid geotype, Oid callingfunc)
282 const Oid radiustype = FLOAT8OID;
283 const Oid expandfn_args[2] = {geotype, radiustype};
284 const bool noError =
true;
286 char *nspname = get_namespace_name(get_func_namespace(callingfunc));
287 List *expandfn_name = list_make2(makeString(nspname), makeString(
"st_expand"));
288 Oid expandfn_oid = LookupFuncName(expandfn_name, 2, expandfn_args, noError);
289 if (expandfn_oid == InvalidOid)
297 expandfn_name = list_make2(makeString(nspname), makeString(
"_st_expand"));
298 expandfn_oid = LookupFuncName(expandfn_name, 2, expandfn_args, noError);
299 if (expandfn_oid == InvalidOid)
300 elog(ERROR,
"%s: unable to lookup 'st_expand(Oid[%u], Oid[%u])'", __func__, geotype, radiustype);
322 Datum postgis_index_supportfn(PG_FUNCTION_ARGS)
324 Node *rawreq = (Node *) PG_GETARG_POINTER(0);
331 postgis_initialize_cache();
333 if (IsA(rawreq, SupportRequestSelectivity))
335 SupportRequestSelectivity *req = (SupportRequestSelectivity *) rawreq;
345 POSTGIS_DEBUGF(2,
"%s: got selectivity %g", __func__, req->selectivity);
346 PG_RETURN_POINTER(req);
353 if (IsA(rawreq, SupportRequestIndexCondition))
355 SupportRequestIndexCondition *req = (SupportRequestIndexCondition *) rawreq;
357 if (is_funcclause(req->node))
359 FuncExpr *clause = (FuncExpr *) req->node;
360 Oid funcid = clause->funcid;
361 IndexableFunction idxfn = {NULL, 0, 0, 0, 0};
362 Oid opfamilyoid = req->opfamily;
364 if (needsSpatialIndex(funcid, &idxfn))
366 int nargs = list_length(clause->args);
367 Node *leftarg, *rightarg;
368 Oid leftdatatype, rightdatatype, oproid;
369 bool swapped =
false;
378 uint8_t opfamilydims;
379 Oid opfamilyam = opFamilyAmOid(opfamilyoid, &opfamilydims);
380 if (opfamilyam != GIST_AM_OID &&
381 opfamilyam != SPGIST_AM_OID &&
382 opfamilyam != BRIN_AM_OID)
384 PG_RETURN_POINTER((Node *)NULL);
391 if (req->indexarg > 1)
392 PG_RETURN_POINTER((Node *)NULL);
399 if (opfamilydims == 3 && idxfn.dims != 3)
400 PG_RETURN_POINTER((Node *)NULL);
405 if (nargs < 2 || nargs < idxfn.expand_arg)
406 elog(ERROR,
"%s: associated with function with %d arguments", __func__, nargs);
418 if (req->indexarg == 0)
420 leftarg = linitial(clause->args);
421 rightarg = lsecond(clause->args);
425 rightarg = linitial(clause->args);
426 leftarg = lsecond(clause->args);
434 leftdatatype = exprType(leftarg);
435 rightdatatype = exprType(rightarg);
442 oproid = get_opfamily_member(opfamilyoid,
445 get_strategy_by_type(leftdatatype, idxfn.index));
446 if (!OidIsValid(oproid))
448 "no spatial operator found for '%s': opfamily %u type %d",
460 if (idxfn.expand_arg)
463 Node *radiusarg = (Node *) list_nth(clause->args, idxfn.expand_arg-1);
464 Oid expandfn_oid = expandFunctionOid(rightdatatype, clause->funcid);
466 FuncExpr *expandexpr = makeFuncExpr(expandfn_oid, rightdatatype,
467 list_make2(rightarg, radiusarg),
468 InvalidOid, InvalidOid, COERCE_EXPLICIT_CALL);
474 #if POSTGIS_PGSQL_VERSION >= 140
475 if (!is_pseudo_constant_for_index(req->root, (Node*)expandexpr, req->index))
477 if (!is_pseudo_constant_for_index((Node*)expandexpr, req->index))
479 PG_RETURN_POINTER((Node*)NULL);
482 expr = make_opclause(oproid, BOOLOID,
false,
483 (Expr *) leftarg, (Expr *) expandexpr,
484 InvalidOid, InvalidOid);
486 ret = (Node *)(list_make1(expr));
501 #if POSTGIS_PGSQL_VERSION >= 140
502 if (!is_pseudo_constant_for_index(req->root, rightarg, req->index))
504 if (!is_pseudo_constant_for_index(rightarg, req->index))
506 PG_RETURN_POINTER((Node*)NULL);
515 oproid = get_commutator(oproid);
516 if (!OidIsValid(oproid))
517 PG_RETURN_POINTER((Node *)NULL);
520 expr = make_opclause(oproid, BOOLOID,
false,
521 (Expr *) leftarg, (Expr *) rightarg,
522 InvalidOid, InvalidOid);
524 ret = (Node *)(list_make1(expr));
534 PG_RETURN_POINTER(ret);
538 elog(WARNING,
"support function '%s' called from unsupported spatial function", __func__);
543 PG_RETURN_POINTER(ret);
PG_FUNCTION_INFO_V1(geom2d_brin_inclusion_add_value)
float8 gserialized_joinsel_internal(PlannerInfo *root, List *args, JoinType jointype, int mode)
float8 gserialized_sel_internal(PlannerInfo *root, List *args, int varRelid, int mode)
This function should return an estimation of the number of rows returned by a query involving an over...
This library is the generic geometry handling section of PostGIS.