Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 686e018

Browse files
df7cbmsdemlei
authored andcommitted
Implement subset/superset for GIN
1 parent 6335bfe commit 686e018

File tree

5 files changed

+99
-12
lines changed

5 files changed

+99
-12
lines changed

expected/moc100.out

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ EXPLAIN (ANALYZE, BUFFERS, TIMING OFF, SUMMARY OFF)
5959
SELECT * FROM moc100 WHERE coverage && '4/0';
6060
QUERY PLAN
6161
--------------------------------------------------------------------------------
62-
Seq Scan on moc100 (cost=0.00..6.25 rows=1 width=96) (actual rows=35 loops=1)
62+
Seq Scan on moc100 (cost=0.00..6.26 rows=1 width=96) (actual rows=35 loops=1)
6363
Filter: (coverage && '4/0'::smoc)
64-
Rows Removed by Filter: 65
64+
Rows Removed by Filter: 66
6565
Buffers: shared hit=114
6666
(4 rows)
6767

@@ -79,3 +79,31 @@ EXPLAIN (ANALYZE, BUFFERS, TIMING OFF, SUMMARY OFF)
7979
Buffers: shared hit=9
8080
(7 rows)
8181

82+
EXPLAIN (ANALYZE, BUFFERS, TIMING OFF, SUMMARY OFF)
83+
SELECT * FROM moc100 WHERE coverage <@ '4/0';
84+
QUERY PLAN
85+
------------------------------------------------------------------------------------------------------------
86+
Bitmap Heap Scan on moc100 (cost=44.00..48.01 rows=1 width=96) (actual rows=1 loops=1)
87+
Recheck Cond: (coverage <@ '4/0'::smoc)
88+
Rows Removed by Index Recheck: 35
89+
Heap Blocks: exact=5
90+
Buffers: shared hit=33
91+
-> Bitmap Index Scan on moc100_coverage_idx (cost=0.00..44.00 rows=1 width=0) (actual rows=36 loops=1)
92+
Index Cond: (coverage <@ '4/0'::smoc)
93+
Buffers: shared hit=12
94+
(8 rows)
95+
96+
EXPLAIN (ANALYZE, BUFFERS, TIMING OFF, SUMMARY OFF)
97+
SELECT * FROM moc100 WHERE coverage @> '4/0';
98+
QUERY PLAN
99+
------------------------------------------------------------------------------------------------------------
100+
Bitmap Heap Scan on moc100 (cost=36.00..40.01 rows=1 width=96) (actual rows=28 loops=1)
101+
Recheck Cond: (coverage @> '4/0'::smoc)
102+
Rows Removed by Index Recheck: 1
103+
Heap Blocks: exact=4
104+
Buffers: shared hit=36
105+
-> Bitmap Index Scan on moc100_coverage_idx (cost=0.00..36.00 rows=1 width=0) (actual rows=29 loops=1)
106+
Index Cond: (coverage @> '4/0'::smoc)
107+
Buffers: shared hit=9
108+
(8 rows)
109+

moc.c

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <stddef.h>
55
#include <string.h>
6+
#include <access/gin.h>
67

78
#include "circle.h"
89
#include "polygon.h"
@@ -12,6 +13,7 @@ PG_FUNCTION_INFO_V1(smoc_out);
1213
PG_FUNCTION_INFO_V1(moc_debug);
1314
PG_FUNCTION_INFO_V1(set_smoc_output_type);
1415
PG_FUNCTION_INFO_V1(smoc_info);
16+
PG_FUNCTION_INFO_V1(smoc_area);
1517
PG_FUNCTION_INFO_V1(smoc_order);
1618
PG_FUNCTION_INFO_V1(smoc_eq);
1719
PG_FUNCTION_INFO_V1(smoc_neq);
@@ -406,6 +408,15 @@ smoc_info(PG_FUNCTION_ARGS)
406408
PG_RETURN_TEXT_P(cstring_to_text(p));
407409
}
408410

411+
Datum
412+
smoc_area(PG_FUNCTION_ARGS)
413+
{
414+
/* get just the MOC header: */
415+
Smoc *moc = (Smoc *) PG_DETOAST_DATUM_SLICE(PG_GETARG_DATUM(0), 0,
416+
MOC_HEADER_VARSIZE);
417+
PG_RETURN_INT64(moc->area);
418+
}
419+
409420
Datum
410421
smoc_order(PG_FUNCTION_ARGS)
411422
{
@@ -1056,13 +1067,15 @@ smoc_gin_extract_query(PG_FUNCTION_ARGS)
10561067
char* moc_a_base = MOC_BASE(moc_a);
10571068
int32 moc_a_end = VARSIZE(moc_a) - VARHDRSZ;
10581069
int32* nkeys = (int32 *) PG_GETARG_POINTER(1);
1059-
//StrategyNumber st = PG_GETARG_UINT16(2);
1070+
StrategyNumber st = PG_GETARG_UINT16(2);
1071+
int32* searchmode = (int32 *) PG_GETARG_POINTER(6);
10601072
int32 nalloc = 4;
10611073
Datum* keys = palloc(nalloc * sizeof(Datum));
10621074

10631075
*nkeys = 0;
10641076

1065-
//Assert(st == 1);
1077+
if (st == MOC_GIN_STRATEGY_SUBSET)
1078+
*searchmode = GIN_SEARCH_MODE_INCLUDE_EMPTY;
10661079

10671080
for (int32 a = moc_a->data_begin; a < moc_a_end; a = next_interval(a))
10681081
{
@@ -1096,22 +1109,49 @@ Datum
10961109
smoc_gin_consistent(PG_FUNCTION_ARGS)
10971110
{
10981111
bool* check = (bool *) PG_GETARG_POINTER(0);
1099-
//StrategyNumber st = PG_GETARG_UINT16(1);
1112+
StrategyNumber st = PG_GETARG_UINT16(1);
11001113
//Smoc* moc_a = (Smoc *) PG_DETOAST_DATUM(PG_GETARG_DATUM(2));
11011114
//int32 moc_a_end = VARSIZE(moc_a) - VARHDRSZ;
11021115
int32 nkeys = PG_GETARG_INT32(3);
11031116
bool* recheck = (bool *) PG_GETARG_POINTER(5);
11041117

1105-
//Assert(st == 1);
1106-
1107-
for (int i = 0; i < nkeys; i++)
1118+
switch (st)
11081119
{
1109-
if (check[i])
1110-
{
1120+
case MOC_GIN_STRATEGY_INTERSECTS:
1121+
/* return true if we have any overlap */
1122+
for (int i = 0; i < nkeys; i++)
1123+
{
1124+
if (check[i])
1125+
{
1126+
*recheck = true;
1127+
PG_RETURN_BOOL(true);
1128+
}
1129+
}
1130+
1131+
PG_RETURN_BOOL(false);
1132+
1133+
case MOC_GIN_STRATEGY_SUBSET:
1134+
/* defer decision to recheck */
11111135
*recheck = true;
11121136
PG_RETURN_BOOL(true);
1113-
}
1137+
1138+
case MOC_GIN_STRATEGY_SUPERSET:
1139+
/* return true when all pixels are contained in the indexed value */
1140+
for (int i = 0; i < nkeys; i++)
1141+
{
1142+
if (! check[i])
1143+
{
1144+
PG_RETURN_BOOL(false);
1145+
}
1146+
}
1147+
1148+
*recheck = true;
1149+
PG_RETURN_BOOL(true);
1150+
1151+
default:
1152+
Assert(0);
11141153
}
11151154

1116-
PG_RETURN_BOOL(false);
1155+
/* not reached */
1156+
PG_RETURN_NULL();
11171157
}

pgs_moc.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Datum smoc_out(PG_FUNCTION_ARGS);
5656
Datum moc_debug(PG_FUNCTION_ARGS);
5757
Datum set_smoc_output_type(PG_FUNCTION_ARGS);
5858
Datum smoc_info(PG_FUNCTION_ARGS);
59+
Datum smoc_area(PG_FUNCTION_ARGS);
5960
Datum smoc_order(PG_FUNCTION_ARGS);
6061
Datum smoc_eq(PG_FUNCTION_ARGS);
6162
Datum smoc_neq(PG_FUNCTION_ARGS);
@@ -116,5 +117,7 @@ next_interval(int32 a)
116117

117118
#define MOC_GIN_ORDER 5 /* order 5 has 12 * 4^5 = 12288 pixels */
118119
#define MOC_GIN_STRATEGY_INTERSECTS 1
120+
#define MOC_GIN_STRATEGY_SUBSET 2
121+
#define MOC_GIN_STRATEGY_SUPERSET 3
119122

120123
#endif

pgs_moc_ops.sql.in

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ CREATE FUNCTION smoc_info (smoc)
88
IMMUTABLE
99
STRICT;
1010

11+
CREATE FUNCTION smoc_area (smoc)
12+
RETURNS bigint
13+
AS 'MODULE_PATHNAME'
14+
LANGUAGE C
15+
PARALLEL SAFE
16+
IMMUTABLE
17+
STRICT;
18+
1119
-- boolean operations
1220

1321
CREATE FUNCTION smoc_eq (smoc, smoc)
@@ -311,6 +319,7 @@ CREATE OPERATOR CLASS smoc_gin_ops
311319
DEFAULT FOR TYPE smoc USING gin AS
312320
OPERATOR 1 &&,
313321
OPERATOR 2 <@,
322+
OPERATOR 3 @>,
314323
FUNCTION 1 btint4cmp (int4, int4),
315324
FUNCTION 2 smoc_gin_extract_value (smoc, internal, internal),
316325
FUNCTION 3 smoc_gin_extract_query (smoc, internal, int2, internal, internal, internal, internal),

sql/moc100.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ ivo://svo.cab/cat/bdsslow 4/13,24,27,31,46,265,269,298,321,357,455,522,525,529,5
105105
ivo://svo.cab/cat/miles 3/75,101,113,151,192,244,314,342,356,362,404,496 4/6,11,13-15,20-21,23,27-30,36,44-45,49-50,57,64,66,72,75,78,81,83,85,88-89,95,97,102,107,111,113,115,117,133,137-138,151-152,154,156-157,162,164,167,171,173-174,182,185,187,189,193-194,196,198,204,218,225-228,232,241,244,248,254,258,261,268,270,273,276-277,279,281-282,284-285,287,291,293,298,304,306,313,315-317,319,322,329,331,338-339,343,345,349-351,354,357,364-365,370,373-374,378-379,383-385,387,389,391-393,396,398,408,420,422-424,426,428,430,432,441,443,446,451,456,458,466-467,470,475-476,481,485,497,505,508,513,517,520-521,528-529,536-537,543-544,547-548,552,557,560-561,565-567,570,574,576,584,586-587,589,592-593,595-596,599,609,612,614-617,625,634,640-642,646-648,656,662,668,671,673,675,693,699,712-713,717,724,727,729-730,739-740,742,745,753,756,758,766,774,776,778-779,782,784,789,794,796,799,808,810,814,817,822,827,830-831,833-834,837-838,842,845,850-852,854-855,857-858,861-862,867,870,875,878,881,884-886,889,891-894,899-901,907-908,910,914-915,920,922,924-925,928-929,931-932,935,938,943-945,948,952-953,956,960,962,967,969,974,982-984,999,1001-1004,1007,1009,1011-1013,1017-1018,1021-1022,1039-1040,1042,1051,1055,1059,1061,1067-1068,1070-1071,1077-1079,1081,1089,1091,1093,1096,1098,1100,1105,1107,1109,1113,1121-1122,1124,1126,1133,1135-1137,1139,1143-1144,1146,1148,1151-1154,1159-1160,1168-1169,1174,1176,1180-1181,1186-1187,1193,1198,1202-1203,1205,1207,1211,1224,1229,1236,1238,1242,1244-1246,1254,1261,1267,1269-1270,1273-1275,1277,1292,1300,1305,1311,1313,1316-1317,1325,1327,1329,1331,1335-1336,1338,1341,1343,1345-1346,1348,1350,1356-1358,1363-1364,1367,1374-1376,1378,1382,1386-1387,1391-1392,1397,1399,1405,1407,1412,1414,1419,1422,1429,1431,1435,1441,1443,1452,1457,1462-1464,1466,1469-1471,1473,1475-1476,1480,1486,1488,1491-1492,1495-1496,1498,1500,1503,1506,1510,1512-1513,1521-1522,1524,1531,1542,1548-1549,1551-1552,1554,1557-1559,1563,1574-1575,1587,1590,1600,1602,1608,1611-1613,1615,1621-1622,1624,1626,1632,1635,1638,1643-1645,1653-1654,1657,1660,1666-1667,1669,1673,1684-1686,1688,1692-1693,1698-1699,1703,1707,1710,1714,1719,1735,1741,1743,1750,1752,1758-1760,1763,1771,1780,1783,1786-1787,1791,1798-1799,1802,1809-1810,1817,1819,1824,1831,1834-1835,1845-1846,1853,1858,1860-1861,1866,1880,1888,1892,1894-1897,1902,1904,1907-1908,1911,1916,1919-1920,1923,1925-1926,1928,1930,1933-1935,1940,1942,1948,1954-1955,1959,1962-1963,1965,1967-1969,1971,1974,1976,1980-1982,1992,1998-1999,2001,2005-2006,2011-2012,2018,2021-2022,2028-2029,2034-2036,2038,2040-2041,2047,2167,2229,2233,2235,2249,2252,2257,2263,2273,2283,2285,2290,2293,2295-2298,2491,2494-2495,2505,2508-2509,2513,2521,2530,2535-2536,2539,2543-2546,2548-2550,2555-2556,2558-2559,2681,2684,2687,2743,2750,2761,2763,2765-2767,2772,2777,2781,2783,2785,2790,2798,2801,2803,2805,2808,2937,2997,2999,3007,3013,3018,3032,3035-3036,3046,3060,3064-3065 \N
106106
ivo://svo.cab/cat/gbs 4/0,182,273,285,388,547,552,794,809,831,1007,1089,1181,1382,1393,1443,1471,1544,1643,1653,1659,1685,1741,1791,1929,2080,2194,2252,2293,2379,2388,2610,2630,2640,2783 \N
107107
ivo://cds.vizier/j/a+as/124/353 6/24603,24796,25383,26065,27212,29368,29399,31018,31258,37132,37341,37368,37839,38172,38336,40233,40819,40923,41095,41369,42053,42077,42289,42371,42691,42948,43013,43076,43238,43515,43882,43962,43976,44113,44157,44810,44836,47606 \N
108+
empty 0/ \N
108109
\.
109110

110111
CREATE INDEX ON moc100 USING GIN (coverage);
@@ -121,3 +122,9 @@ SET enable_seqscan = off;
121122

122123
EXPLAIN (ANALYZE, BUFFERS, TIMING OFF, SUMMARY OFF)
123124
SELECT * FROM moc100 WHERE coverage && '4/0';
125+
126+
EXPLAIN (ANALYZE, BUFFERS, TIMING OFF, SUMMARY OFF)
127+
SELECT * FROM moc100 WHERE coverage <@ '4/0';
128+
129+
EXPLAIN (ANALYZE, BUFFERS, TIMING OFF, SUMMARY OFF)
130+
SELECT * FROM moc100 WHERE coverage @> '4/0';

0 commit comments

Comments
 (0)