Skip to content

opt: add geospatial inverted index recommendations #80934

@mgartner

Description

@mgartner

There should be geospatial inverted index recommendations for queries that use index accelerated geospatial functions:

var RelationshipMap = map[string]RelationshipType{
"st_covers": Covers,
"st_coveredby": CoveredBy,
"st_contains": Covers,
"st_containsproperly": Covers,
"st_crosses": Intersects,
"st_dwithin": DWithin,
"st_dfullywithin": DFullyWithin,
"st_equals": Intersects,
"st_intersects": Intersects,
"st_overlaps": Intersects,
"st_touches": Intersects,
"st_within": CoveredBy,
"st_dwithinexclusive": DWithin,
"st_dfullywithinexclusive": DFullyWithin,
}

For example, the query below has no recommendation:

defaultdb> CREATE TABLE t (k INT PRIMARY KEY, g GEOMETRY);

defaultdb> EXPLAIN SELECT k FROM t WHERE st_covers(g, 'POINT(0 0 0 0)');
                                                  info
--------------------------------------------------------------------------------------------------------
  distribution: local
  vectorized: true

  • filter
  │ filter: st_covers(g, '01010000C00000000000000000000000000000000000000000000000000000000000000000')
  │
  └── • scan
        missing stats
        table: t@t_pkey
        spans: FULL SCAN
(10 rows)

But if an index is added, a query plan using it is chosen because it is more efficient:

defaultdb> CREATE INVERTED INDEX ON t(g);
CREATE INDEX

defaultdb> EXPLAIN SELECT k FROM t WHERE st_covers(g, 'POINT(0 0 0 0)');
                                                  info
--------------------------------------------------------------------------------------------------------
  distribution: local
  vectorized: true

  • filter
  │ filter: st_covers(g, '01010000C00000000000000000000000000000000000000000000000000000000000000000')
  │
  └── • index join
      │ table: t@t_pkey
      │
      └── • inverted filter
          │ inverted column: g_inverted_key
          │ num spans: 31
          │
          └── • scan
                missing stats
                table: t@t_g_idx
                spans: 31 spans
(17 rows)

Jira issue: CRDB-15446

Epic: CRDB-14532

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementSolution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)T-sql-queriesSQL Queries Team

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions