-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathinverted_join_geospatial
233 lines (214 loc) · 3.92 KB
/
inverted_join_geospatial
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
statement ok
CREATE TABLE ltable(
lk int primary key,
geom1 geometry,
geom2 geometry
)
statement ok
INSERT INTO ltable VALUES
(1, 'POINT(3.0 3.0)', 'POINT(3.0 3.0)'),
(2, 'POINT(4.5 4.5)', 'POINT(3.0 3.0)'),
(3, 'POINT(1.5 1.5)', 'POINT(3.0 3.0)'),
(4, NULL, 'POINT(3.0 3.0)'),
(5, 'POINT(1.5 1.5)', NULL),
(6, NULL, NULL)
statement ok
CREATE TABLE rtable(
rk int primary key,
geom geometry,
INVERTED INDEX geom_index(geom)
)
statement ok
INSERT INTO rtable VALUES
(11, 'POINT(1.0 1.0)'),
(12, 'LINESTRING(1.0 1.0, 2.0 2.0)'),
(13, 'POINT(3.0 3.0)'),
(14, 'LINESTRING(4.0 4.0, 5.0 5.0)'),
(15, 'LINESTRING(40.0 40.0, 41.0 41.0)'),
(16, 'POLYGON((1.0 1.0, 5.0 1.0, 5.0 5.0, 1.0 5.0, 1.0 1.0))')
query II
SELECT lk, rk FROM ltable JOIN rtable@geom_index ON ST_Intersects(ltable.geom1, rtable.geom) ORDER BY (lk, rk)
----
1 13
1 16
2 14
2 16
3 12
3 16
5 12
5 16
query II
SELECT lk, rk FROM ltable JOIN rtable@geom_index ON ST_DWithin(ltable.geom1, rtable.geom, 2) ORDER BY (lk, rk)
----
1 12
1 13
1 14
1 16
2 14
2 16
3 11
3 12
3 16
5 11
5 12
5 16
query II
SELECT lk, rk FROM ltable JOIN rtable@geom_index
ON ST_Intersects(rtable.geom, ltable.geom1) OR ST_DWithin(ltable.geom1, rtable.geom, 2) ORDER BY (lk, rk)
----
1 12
1 13
1 14
1 16
2 14
2 16
3 11
3 12
3 16
5 11
5 12
5 16
query II
SELECT lk, rk FROM ltable JOIN rtable@geom_index
ON ST_Intersects(ltable.geom1, rtable.geom) AND ST_DWithin(rtable.geom, ltable.geom1, 2) ORDER BY (lk, rk)
----
1 13
1 16
2 14
2 16
3 12
3 16
5 12
5 16
query II
SELECT lk, rk FROM ltable JOIN rtable@geom_index
ON ST_Intersects(ltable.geom1, rtable.geom) AND ST_DWithin(rtable.geom, ltable.geom2, 2) ORDER BY (lk, rk)
----
1 13
1 16
2 14
2 16
3 12
3 16
query II
SELECT lk, rk FROM ltable JOIN rtable@geom_index
ON ST_Intersects(ltable.geom1, rtable.geom) OR ST_DWithin(rtable.geom, ltable.geom2, 2) ORDER BY (lk, rk)
----
1 12
1 13
1 14
1 16
2 12
2 13
2 14
2 16
3 12
3 13
3 14
3 16
4 12
4 13
4 14
4 16
5 12
5 16
# Run the same two queries with the primary index to verify that we get the
# same results.
query II
SELECT lk, rk FROM ltable JOIN rtable@primary
ON ST_Intersects(ltable.geom1, rtable.geom) AND ST_DWithin(rtable.geom, ltable.geom2, 2) ORDER BY (lk, rk)
----
1 13
1 16
2 14
2 16
3 12
3 16
query II
SELECT lk, rk FROM ltable JOIN rtable@primary
ON ST_Intersects(ltable.geom1, rtable.geom) OR ST_DWithin(rtable.geom, ltable.geom2, 2) ORDER BY (lk, rk)
----
1 12
1 13
1 14
1 16
2 12
2 13
2 14
2 16
3 12
3 13
3 14
3 16
4 12
4 13
4 14
4 16
5 12
5 16
query II
SELECT ltable.lk, rtable.rk FROM ltable JOIN rtable@geom_index
ON ST_Intersects(ltable.geom1, rtable.geom) AND ST_Covers(ltable.geom2, rtable.geom)
AND (ST_DFullyWithin(rtable.geom, ltable.geom1, 100) OR ST_Intersects('POINT(1.0 1.0)', rtable.geom))
----
1 13
# The following queries perform semi- and anti-joins, and don't currently use
# an inverted join.
query I
SELECT lk FROM ltable WHERE EXISTS (SELECT * FROM rtable WHERE ST_Intersects(ltable.geom2, rtable.geom))
ORDER BY lk
----
1
2
3
4
query I
SELECT rk FROM rtable WHERE EXISTS (SELECT * FROM ltable WHERE ST_Intersects(ltable.geom2, rtable.geom))
ORDER BY rk
----
13
16
query I
SELECT lk FROM ltable WHERE NOT EXISTS (SELECT * FROM rtable WHERE ST_Intersects(ltable.geom2, rtable.geom))
ORDER BY lk
----
5
6
query I
SELECT rk FROM rtable WHERE NOT EXISTS (SELECT * FROM ltable WHERE ST_Intersects(ltable.geom2, rtable.geom))
ORDER BY rk
----
11
12
14
15
# Bounding box operations.
query II
SELECT lk, rk FROM ltable JOIN rtable@geom_index ON ltable.geom1 ~ rtable.geom
ORDER BY lk, rk
----
1 13
query II
SELECT lk, rk FROM ltable JOIN rtable@geom_index ON rtable.geom ~ ltable.geom1
ORDER BY lk, rk
----
1 13
1 16
2 14
2 16
3 12
3 16
5 12
5 16
query II
SELECT lk, rk FROM ltable JOIN rtable@geom_index ON rtable.geom && ltable.geom1
ORDER BY lk, rk
----
1 13
1 16
2 14
2 16
3 12
3 16
5 12
5 16