Filter: One Odd Duck

Hi Folks,

Over at his blog, Nicklas Avéns questions the behavior of Filter. He's right: Filter is a little odd. It is also very much by design.

Before we look at filter, let's review the data flow for a typical spatial query. A spatial selection generally has two internal components: a primary filter, which is designed to be fast, but which may produce extra results; and a secondary filter, which is slower, but only works to remove the false positives passed by the primary filter.

The primary filter is accomplished by using the spatial index. The secondary filter requires calls to actual geometry routines, and will be much more expensive. But the secondary filter also guarantees index-independent—and geometrically correct—results.

Without an index, most operations will just skip the primary filter. The result is slow, but returns the same results as the full primary–secondary pathway.

Filter is designed for a pretty common, but somewhat odd use case. Imagine that you want to display a portion of a map: you want to retrieve from the database those objects that would show on the map, but if you happen to retrieve a few extras you don't care—they'll just fall off the display.

In other words, false positives are okay. In fact, they're often preferred, because the overall performance is can be much better if you can skip the expensive geometry operations and instead render some additional objects.

So we added Filter, which simply skips the secondary filter and gives you the quick index-only results.

But what if you don't have an index? We had two choices, either (a) return an error, or (b) revert to STIntersects. Note that both of these options have index-dependent behavior: that ship sailed when we decided to skip the secondary filter.

We chose to allow the operation, but revert to STIntersects in the non-indexed case. This gives us clear semantics: the correct results are always a subset of what we return. It's true that for SQL Server, these are somewhat odd semantics, since they are index-dependent.

But this is the only place in spatial where we have such behavior. If you want consistent results regardless of index, use STIntersects or any of the other methods we provide. If your use case fits the one I described above, you may very well want Filter.

Call it useful, call it odd. Call it Filter.

Cheers,
-Isaac