Skip to content

Commit

Permalink
Added support of <@@ GIN operator
Browse files Browse the repository at this point in the history
Added support of <@@ strategy (partial contained in).
Added tests for that case.
  • Loading branch information
theirix committed Oct 3, 2012
1 parent b846232 commit 6f2f51d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Makefile
Expand Up @@ -9,8 +9,8 @@ TESTS = $(wildcard test/sql/*.sql)
REGRESS = $(patsubst test/sql/%.sql,%,$(TESTS))
REGRESS_OPTS = --inputdir=test
PG_CONFIG := pg_config
#PG_CPPFLAGS =
EXTRA_CLEAN = sql/$(EXTENSION)--$(EXTVERSION).sql
#PG_CPPFLAGS = -g -O0
EXTRA_CLEAN = sql/$(EXTENSION)--$(EXTVERSION).sql

all: sql/$(EXTENSION)--$(EXTVERSION).sql

Expand Down
6 changes: 3 additions & 3 deletions sql/parray_gin.sql
Expand Up @@ -67,9 +67,9 @@ create operator <@@ (
create operator class parray_gin_ops
for type _text using gin
as
operator 7 @> (anyarray,anyarray), -- strict text[], text[]
operator 8 @@> (_text,_text), -- partial text[], text[]
--operator 9 <@@ (_text,_text), -- partial text[], text[]
operator 7 @> (anyarray,anyarray), -- strict text[], text[]
operator 8 @@> (_text,_text), -- partial text[], text[]
operator 9 <@@ (_text,_text), -- partial text[], text[]
function 1 parray_gin_compare(internal, internal),
function 2 parray_gin_extract_value(internal, internal, internal),
function 3 parray_gin_extract_query(internal, internal, internal, internal, internal, internal, internal),
Expand Down
24 changes: 15 additions & 9 deletions src/parray_gin.c
Expand Up @@ -30,7 +30,7 @@
PG_MODULE_MAGIC;

/* Log level, usually DEBUG5 (silent) or NOTICE (messages are sent to client side) */
#define PARRAY_GIN_TRACE_LEVEL NOTICE
#define PARRAY_GIN_TRACE_LEVEL DEBUG5

/* Controls logging from GIN functions. A lot of output */
#define TRACE_LIKE_HELL 0
Expand All @@ -41,14 +41,15 @@ PG_MODULE_MAGIC;
#define PARRAY_GIN_STRATEGY_CONTAINS 7
/* @@> operator strategy */
#define PARRAY_GIN_STRATEGY_CONTAINS_PARTIAL 8
/* <@@ operator strategy
#define PARRAY_GIN_STRATEGY_CONTAINED_BY_PARTIAL 9 */
/* <@@ operator strategy */
#define PARRAY_GIN_STRATEGY_CONTAINED_BY_PARTIAL 9

/*
* Internal functions declarations
*/

int32 gin_compare_string_partial(char *a, int lena, char *b, int lenb);
void* memmem_ported(const void *l, size_t l_len, const void *s, size_t s_len);


/*
Expand Down Expand Up @@ -364,7 +365,8 @@ parray_gin_extract_query(PG_FUNCTION_ARGS)
int nelems;

if (strategy != PARRAY_GIN_STRATEGY_CONTAINS &&
strategy != PARRAY_GIN_STRATEGY_CONTAINS_PARTIAL)
strategy != PARRAY_GIN_STRATEGY_CONTAINS_PARTIAL &&
strategy != PARRAY_GIN_STRATEGY_CONTAINED_BY_PARTIAL)
{
ereport(ERROR, (errcode(ERRCODE_INVALID_NAME), errmsg("wrong strategy %d", strategy)));
}
Expand All @@ -388,12 +390,13 @@ parray_gin_extract_query(PG_FUNCTION_ARGS)
{
*pmatch = NULL;
}
else if (strategy == PARRAY_GIN_STRATEGY_CONTAINS_PARTIAL)
else if (strategy == PARRAY_GIN_STRATEGY_CONTAINS_PARTIAL ||
strategy == PARRAY_GIN_STRATEGY_CONTAINED_BY_PARTIAL)
{
*pmatch = (bool*)palloc(sizeof(bool) * *nkeys);
for (i = 0; i < *nkeys; ++i)
{
*pmatch[i] = TRUE;
(*pmatch)[i] = TRUE;
}
}

Expand Down Expand Up @@ -438,7 +441,8 @@ parray_gin_consistent(PG_FUNCTION_ARGS)
int i;

if (strategy != PARRAY_GIN_STRATEGY_CONTAINS &&
strategy != PARRAY_GIN_STRATEGY_CONTAINS_PARTIAL)
strategy != PARRAY_GIN_STRATEGY_CONTAINS_PARTIAL &&
strategy != PARRAY_GIN_STRATEGY_CONTAINED_BY_PARTIAL)
{
ereport(ERROR, (errcode(ERRCODE_INVALID_NAME), errmsg("wrong strategy %d", strategy)));
}
Expand Down Expand Up @@ -567,7 +571,8 @@ parray_gin_compare_partial(PG_FUNCTION_ARGS)
int result;

if (strategy != PARRAY_GIN_STRATEGY_CONTAINS &&
strategy != PARRAY_GIN_STRATEGY_CONTAINS_PARTIAL)
strategy != PARRAY_GIN_STRATEGY_CONTAINS_PARTIAL &&
strategy != PARRAY_GIN_STRATEGY_CONTAINED_BY_PARTIAL)
{
ereport(ERROR, (errcode(ERRCODE_INVALID_NAME), errmsg("wrong strategy %d", strategy)));
}
Expand All @@ -582,7 +587,8 @@ parray_gin_compare_partial(PG_FUNCTION_ARGS)
else
result = 0;
}
else if (strategy == PARRAY_GIN_STRATEGY_CONTAINS_PARTIAL)
else if (strategy == PARRAY_GIN_STRATEGY_CONTAINS_PARTIAL ||
strategy == PARRAY_GIN_STRATEGY_CONTAINED_BY_PARTIAL)
{
result = gin_compare_string_partial(str_partial_key, strlen(str_partial_key), str_key, strlen(str_key));
}
Expand Down
12 changes: 12 additions & 0 deletions test/expected/regress.out
Expand Up @@ -67,6 +67,9 @@ t
-- t
select array['foo', 'boo', 'baz'] @@> array['oo'];
t
-- t
select array['foo', 'boo', 'baz'] @@> array['ba', 'fo'];
t
-- f
select array['foo', 'boo', 'baz'] @@> array['ooz'];
f
Expand All @@ -77,6 +80,9 @@ t
select array['fo'] <@@ array['foo', 'bar', 'baz'];
t
-- t
select array['ba', 'fo'] <@@ array['foo', 'bar', 'baz'];
t
-- t
select array['ba'] <@@ array['foo', 'bar', 'baz'];
t
-- t
Expand Down Expand Up @@ -146,5 +152,11 @@ select count(*) from test_table where val @> array['qux'];
-- 30
select count(*) from test_table where val @@> array[''];
30
-- 3
select count(*) from test_table where val <@@ array['kung-foo4','xxxbar4','xxxbaz4'];
3
-- 0
select count(*) from test_table where val <@@ array['kung-foo4'];
0
\t off
\pset format aligned
9 changes: 9 additions & 0 deletions test/sql/regress.sql
Expand Up @@ -51,6 +51,8 @@ select array['foo', 'foobar', 'baz'] @@> array['foo'];

-- t
select array['foo', 'boo', 'baz'] @@> array['oo'];
-- t
select array['foo', 'boo', 'baz'] @@> array['ba', 'fo'];
-- f
select array['foo', 'boo', 'baz'] @@> array['ooz'];
-- t
Expand All @@ -60,6 +62,8 @@ select array['food', 'booze', 'baz'] @@> array['ooz'];
-- t
select array['fo'] <@@ array['foo', 'bar', 'baz'];
-- t
select array['ba', 'fo'] <@@ array['foo', 'bar', 'baz'];
-- t
select array['ba'] <@@ array['foo', 'bar', 'baz'];
-- t
select array['b'] <@@ array['foo', 'bar', 'baz'];
Expand Down Expand Up @@ -120,5 +124,10 @@ select count(*) from test_table where val @> array['qux'];
-- 30
select count(*) from test_table where val @@> array[''];

-- 3
select count(*) from test_table where val <@@ array['kung-foo4','xxxbar4','xxxbaz4'];
-- 0
select count(*) from test_table where val <@@ array['kung-foo4'];

\t off
\pset format aligned

0 comments on commit 6f2f51d

Please sign in to comment.