Skip to content

Commit 4948319

Browse files
authored
docs: Add barrier function documentation and use it in KNN join example (#72)
1 parent 82439b2 commit 4948319

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

docs/reference/sql-joins.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,37 @@ FROM cities AS cities_l
5252
INNER JOIN cities AS cities_r
5353
ON ST_KNN(cities_l.geometry, cities_r.geometry, 5, false)
5454
```
55+
56+
## Optimization Barrier
57+
58+
Use the `barrier` function to prevent filter pushdown and control predicate evaluation order in complex spatial joins. This function creates an optimization barrier by evaluating boolean expressions at runtime.
59+
60+
The `barrier` function takes a boolean expression as a string, followed by pairs of variable names and their values that will be substituted into the expression:
61+
62+
```sql
63+
barrier(expression, var_name1, var_value1, var_name2, var_value2, ...)
64+
```
65+
66+
The placement of filters relative to KNN joins changes the semantic meaning of the query:
67+
68+
- **Filter before KNN**: First filters the data, then finds K nearest neighbors from the filtered subset. This answers "What are the K nearest high-rated restaurants?"
69+
- **Filter after KNN**: First finds K nearest neighbors from all data, then filters those results. This answers "Of the K nearest restaurants, which ones are high-rated?"
70+
71+
### Example
72+
73+
Find the 3 nearest high-rated restaurants to luxury hotels, ensuring the KNN join completes before filtering.
74+
75+
```sql
76+
SELECT
77+
h.name AS hotel,
78+
r.name AS restaurant,
79+
r.rating
80+
FROM hotels AS h
81+
INNER JOIN restaurants AS r
82+
ON ST_KNN(h.geometry, r.geometry, 3, false)
83+
WHERE barrier('rating > 4.0 AND stars >= 4',
84+
'rating', r.rating,
85+
'stars', h.stars)
86+
```
87+
88+
With the barrier function, this query first finds the 3 nearest restaurants to each hotel (regardless of rating), then filters to keep only those pairs where the restaurant has rating > 4.0 and the hotel has stars >= 4. Without the barrier, an optimizer might push the filters down, changing the query to first filter for high-rated restaurants and luxury hotels, then find the 3 nearest among those filtered sets.

0 commit comments

Comments
 (0)