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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
CREATE EXTENSION roaringbitmap;
CREATE EXTENSION pgfaceting;
CREATE SCHEMA facetingtestsuite;
CREATE TYPE facetingtestsuite.mimetype AS ENUM (
'application/pdf',
'text/html',
'image/jpeg',
'image/png',
'application/msword',
'text/csv',
'application/zip',
'application/vnd.ms-powerpoint'
);
CREATE TABLE facetingtestsuite.employee (
id int8 primary key,
full_name text,
department text
);
CREATE TABLE facetingtestsuite.categories (
id int8 primary key,
owner_id int8 REFERENCES facetingtestsuite.employee (id)
);
CREATE TABLE facetingtestsuite.documents (
id int8 primary key,
created timestamptz not null,
finished timestamptz,
category_id int8 REFERENCES facetingtestsuite.categories (id),
tags text[],
type facetingtestsuite.mimetype,
size int8,
title text
);
CREATE TABLE facetingtestsuite.authors (
document_id int8 REFERENCES facetingtestsuite.documents (id) ON DELETE CASCADE,
author_id int8 REFERENCES facetingtestsuite.employee (id),
PRIMARY KEY (document_id, author_id)
);
COPY facetingtestsuite.employee (id, full_name, department) FROM stdin;
COPY facetingtestsuite.categories (id, owner_id) FROM stdin;
COPY facetingtestsuite.documents (id, created, finished, category_id, tags, type, size, title) FROM stdin;
COPY facetingtestsuite.authors FROM stdin;
SELECT faceting.add_faceting_to_table('facetingtestsuite.documents',
key => 'id',
facets => array[
faceting.datetrunc_facet('created', 'month'),
faceting.datetrunc_facet('finished', 'month'),
faceting.plain_facet('category_id'),
faceting.array_facet('tags'),
faceting.bucket_facet('size', buckets => array[0,1000,5000,10000,50000,100000,500000]),
faceting.joined_plain_facet('author_id',
from_clause => 'facetingtestsuite.authors a',
correlation => 'a.document_id = {TABLE}.id',
p_facet_name => 'author')
],
populate => false
);
add_faceting_to_table
-----------------------
(1 row)
SELECT faceting.populate_facets('facetingtestsuite.documents'::regclass);
populate_facets
-----------------
(1 row)
SELECT * FROM faceting.top_values('facetingtestsuite.documents'::regclass);
facet_name | facet_value | cardinality
-------------+------------------------------+-------------
created | Tue Dec 01 00:00:00 2009 PST | 10
finished | Fri Jan 01 00:00:00 2010 PST | 6
finished | Tue Dec 01 00:00:00 2009 PST | 4
category_id | 24 | 4
category_id | 8 | 2
category_id | 9 | 2
category_id | 12 | 1
tags | blue | 7
tags | orange | 5
tags | green | 4
tags | burlywood | 2
tags | olive | 2
size | 6 | 7
size | 7 | 2
size | 5 | 1
author | 1 | 7
author | 2 | 4
author | 3 | 2
(18 rows)
SELECT faceting.add_facets('facetingtestsuite.documents',
facets=>array[
faceting.plain_facet('type'),
faceting.joined_plain_facet('e.department',
from_clause => 'facetingtestsuite.categories c JOIN facetingtestsuite.employee e ON c.owner_id = e.id',
correlation => 'c.id = {TABLE}.category_id')
]);
add_facets
------------
7
8
(2 rows)
SELECT faceting.populate_facets_query('facetingtestsuite.documents'::regclass::oid);
populate_facets_query
--------------------------------------------------------------------------------------------------------------------------------------------------------------
+
SELECT facet_id, (id >> 20)::int4 chunk_id, facet_value collate "POSIX", rb_build_agg((id & ((1 << 20) - 1))::int4 ORDER BY id) +
FROM facetingtestsuite.documents d, +
LATERAL ( +
VALUES (1, date_trunc('month', d.created)::text), +
(2, date_trunc('month', d.finished)::text), +
(3, d.category_id::text), +
(5, width_bucket(d.size, '{0,1000,5000,10000,50000,100000,500000}')::text), +
(7, d.type::text) +
UNION ALL +
(SELECT 4, element_value::text FROM unnest(d.tags) element_value) +
UNION ALL +
(SELECT 6, author_id::text FROM facetingtestsuite.authors a WHERE a.document_id = d.id) +
UNION ALL +
(SELECT 8, e.department::text FROM facetingtestsuite.categories c JOIN facetingtestsuite.employee e ON c.owner_id = e.id WHERE c.id = d.category_id)+
) t(facet_id, facet_value) +
WHERE facet_value IS NOT NULL +
GROUP BY facet_id, facet_value collate "POSIX", chunk_id +
(1 row)
SELECT * FROM faceting.top_values('facetingtestsuite.documents'::regclass);
facet_name | facet_value | cardinality
-------------+------------------------------+-------------
created | Tue Dec 01 00:00:00 2009 PST | 10
finished | Fri Jan 01 00:00:00 2010 PST | 6
finished | Tue Dec 01 00:00:00 2009 PST | 4
category_id | 24 | 4
category_id | 8 | 2
category_id | 9 | 2
category_id | 12 | 1
tags | blue | 7
tags | orange | 5
tags | green | 4
tags | burlywood | 2
tags | olive | 2
size | 6 | 7
size | 7 | 2
size | 5 | 1
author | 1 | 7
author | 2 | 4
author | 3 | 2
type | application/pdf | 5
type | text/html | 3
type | image/jpeg | 2
department | Sales | 7
department | Director | 2
(23 rows)
COPY facetingtestsuite.documents (id, created, finished, category_id, tags, type, size, title) FROM stdin;
SELECT faceting.merge_deltas('facetingtestsuite.documents'::regclass);
merge_deltas
--------------
(1 row)
SELECT * FROM faceting.top_values('facetingtestsuite.documents'::regclass);
facet_name | facet_value | cardinality
-------------+------------------------------+-------------
created | Tue Dec 01 00:00:00 2009 PST | 20
finished | Fri Jan 01 00:00:00 2010 PST | 15
finished | Tue Dec 01 00:00:00 2009 PST | 5
category_id | 24 | 12
category_id | 9 | 4
category_id | 8 | 2
category_id | 12 | 1
tags | blue | 15
tags | orange | 14
tags | green | 6
tags | brown | 4
tags | red | 3
size | 6 | 14
size | 7 | 4
size | 4 | 1
size | 5 | 1
author | 1 | 7
author | 2 | 4
author | 3 | 2
type | application/pdf | 10
type | image/jpeg | 3
type | image/png | 3
type | text/html | 3
type | text/csv | 1
department | Sales | 7
department | Director | 2
(26 rows)
(SELECT 'created' AS facet_name, date_trunc('month', created)::text AS facet_value, COUNT(*) AS cardinality FROM facetingtestsuite.documents GROUP BY 1, 2 ORDER BY 3 DESC, 2 LIMIT 5)
UNION ALL
(SELECT 'finished', date_trunc('month', finished)::text, COUNT(*) FROM facetingtestsuite.documents GROUP BY 1, 2 ORDER BY 3 DESC, 2 LIMIT 5)
UNION ALL
(SELECT 'category_id', category_id::text, COUNT(*) FROM facetingtestsuite.documents GROUP BY 1, 2 ORDER BY 3 DESC, 2 LIMIT 5)
UNION ALL
(SELECT 'type', type::text, COUNT(*) FROM facetingtestsuite.documents GROUP BY 1, 2 ORDER BY 3 DESC, 2 LIMIT 5)
UNION ALL
(SELECT 'size', width_bucket(size, array[0,1000,5000,10000,50000,100000,500000])::text, COUNT(*) FROM facetingtestsuite.documents GROUP BY 1, 2 ORDER BY 3 DESC, 2 LIMIT 5);
facet_name | facet_value | cardinality
-------------+------------------------------+-------------
created | Tue Dec 01 00:00:00 2009 PST | 20
finished | Fri Jan 01 00:00:00 2010 PST | 15
finished | Tue Dec 01 00:00:00 2009 PST | 5
category_id | 24 | 12
category_id | 9 | 4
category_id | 8 | 2
category_id | 12 | 1
category_id | | 1
type | application/pdf | 10
type | image/jpeg | 3
type | image/png | 3
type | text/html | 3
type | text/csv | 1
size | 6 | 14
size | 7 | 4
size | 4 | 1
size | 5 | 1
(17 rows)
SELECT * FROM faceting.count_results('facetingtestsuite.documents'::regclass,
filters => array[row('category_id', 24)]::faceting.facet_filter[]);
facet_name | facet_value | cardinality
------------+------------------------------+-------------
created | Tue Dec 01 00:00:00 2009 PST | 12
finished | Fri Jan 01 00:00:00 2010 PST | 10
finished | Tue Dec 01 00:00:00 2009 PST | 2
tags | orange | 11
tags | blue | 9
tags | green | 5
tags | brown | 4
tags | red | 3
tags | darkslateblue | 2
tags | aqua | 1
tags | burlywood | 1
tags | cadetblue | 1
tags | candy pink | 1
tags | chartreuse | 1
tags | cherry | 1
tags | chocolate | 1
tags | coral | 1
tags | cyan | 1
tags | dimgray | 1
tags | dirt brown | 1
tags | floralwhite | 1
tags | ivory | 1
tags | lavender | 1
tags | lightpink | 1
tags | maroon | 1
tags | olive | 1
tags | pale gold | 1
tags | pale peach | 1
tags | peachy pink | 1
tags | purple | 1
tags | antiquewhite | 0
tags | aqua blue | 0
tags | aquamarine | 0
tags | bisque | 0
tags | lightcoral | 0
tags | mustard brown | 0
tags | pink | 0
tags | red purple | 0
tags | rust | 0
tags | very light pink | 0
size | 6 | 9
size | 7 | 2
size | 4 | 1
size | 5 | 0
author | 1 | 3
author | 2 | 2
author | 3 | 1
type | application/pdf | 5
type | image/jpeg | 3
type | text/html | 2
type | image/png | 1
type | text/csv | 1
department | Sales | 4
department | Director | 0
(54 rows)
DELETE FROM facetingtestsuite.documents WHERE 'red' = ANY (tags);
SELECT faceting.merge_deltas('facetingtestsuite.documents'::regclass);
merge_deltas
--------------
(1 row)
SELECT * FROM faceting.top_values('facetingtestsuite.documents'::regclass, facets=>array['tags', 'type']);
facet_name | facet_value | cardinality
------------+-----------------+-------------
tags | blue | 13
tags | orange | 11
tags | green | 5
tags | brown | 4
tags | aqua | 2
tags | burlywood | 2
tags | olive | 2
type | application/pdf | 9
type | image/png | 3
type | image/jpeg | 2
type | text/html | 2
type | text/csv | 1
(12 rows)
SELECT faceting.drop_facets('facetingtestsuite.documents', array['type', 'tags', 'not existing']);
drop_facets
-------------
tags
type
(2 rows)
SELECT * FROM faceting.top_values('facetingtestsuite.documents'::regclass);
facet_name | facet_value | cardinality
-------------+------------------------------+-------------
created | Tue Dec 01 00:00:00 2009 PST | 17
finished | Fri Jan 01 00:00:00 2010 PST | 13
finished | Tue Dec 01 00:00:00 2009 PST | 4
category_id | 24 | 9
category_id | 9 | 4
category_id | 8 | 2
category_id | 12 | 1
size | 6 | 11
size | 7 | 4
size | 4 | 1
size | 5 | 1
author | 1 | 7
author | 2 | 4
author | 3 | 2
department | Sales | 7
department | Director | 2
(16 rows)
SELECT faceting.drop_faceting('facetingtestsuite.documents');
drop_faceting
---------------
t
(1 row)
-- Check that adding faceting back in works
SELECT faceting.add_faceting_to_table('facetingtestsuite.documents',
key => 'id',
facets => array[faceting.plain_facet('category_id')]);
add_faceting_to_table
-----------------------
(1 row)
SELECT * FROM faceting.top_values('facetingtestsuite.documents'::regclass);
facet_name | facet_value | cardinality
-------------+-------------+-------------
category_id | 24 | 9
category_id | 9 | 4
category_id | 8 | 2
category_id | 12 | 1
(4 rows)
|