[Groonga-commit] pgroonga/pgroonga at 58a9ab2 [master] Add &@>

Back to archive index

Kouhei Sutou null+****@clear*****
Thu Jan 28 14:22:59 JST 2016


Kouhei Sutou	2016-01-28 14:22:59 +0900 (Thu, 28 Jan 2016)

  New Revision: 58a9ab2b8e429382682e0e93a7bd31372fc48135
  https://github.com/pgroonga/pgroonga/commit/58a9ab2b8e429382682e0e93a7bd31372fc48135

  Message:
    Add &@>

  Added files:
    expected/full-text-search/text/single/match-contain/bitmapscan.out
    expected/full-text-search/text/single/match-contain/indexscan.out
    expected/full-text-search/text/single/match-contain/seqscan.out
    sql/full-text-search/text/single/match-contain/bitmapscan.sql
    sql/full-text-search/text/single/match-contain/indexscan.sql
    sql/full-text-search/text/single/match-contain/seqscan.sql
  Modified files:
    pgroonga.sql
    src/pgroonga.c
    src/pgroonga.h

  Added: expected/full-text-search/text/single/match-contain/bitmapscan.out (+22 -0) 100644
===================================================================
--- /dev/null
+++ expected/full-text-search/text/single/match-contain/bitmapscan.out    2016-01-28 14:22:59 +0900 (097ac75)
@@ -0,0 +1,22 @@
+CREATE TABLE memos (
+  id integer,
+  content text
+);
+INSERT INTO memos VALUES (1, 'PostgreSQL is a RDBMS.');
+INSERT INTO memos VALUES (2, 'Groonga is fast full text search engine.');
+INSERT INTO memos VALUES (3, 'PGroonga is a PostgreSQL extension that uses Groonga.');
+CREATE INDEX pgrn_index ON memos
+ USING pgroonga (content pgroonga.text_full_text_search_ops_v2);
+SET enable_seqscan = off;
+SET enable_indexscan = off;
+SET enable_bitmapscan = on;
+SELECT id, content
+  FROM memos
+ WHERE content &@> Array['rdbms', 'engine'];
+ id |                 content                  
+----+------------------------------------------
+  1 | PostgreSQL is a RDBMS.
+  2 | Groonga is fast full text search engine.
+(2 rows)
+
+DROP TABLE memos;

  Added: expected/full-text-search/text/single/match-contain/indexscan.out (+22 -0) 100644
===================================================================
--- /dev/null
+++ expected/full-text-search/text/single/match-contain/indexscan.out    2016-01-28 14:22:59 +0900 (af38fc9)
@@ -0,0 +1,22 @@
+CREATE TABLE memos (
+  id integer,
+  content text
+);
+INSERT INTO memos VALUES (1, 'PostgreSQL is a RDBMS.');
+INSERT INTO memos VALUES (2, 'Groonga is fast full text search engine.');
+INSERT INTO memos VALUES (3, 'PGroonga is a PostgreSQL extension that uses Groonga.');
+CREATE INDEX pgrn_index ON memos
+ USING pgroonga (content pgroonga.text_full_text_search_ops_v2);
+SET enable_seqscan = off;
+SET enable_indexscan = on;
+SET enable_bitmapscan = off;
+SELECT id, content
+  FROM memos
+ WHERE content &@> Array['rdbms', 'engine'];
+ id |                 content                  
+----+------------------------------------------
+  1 | PostgreSQL is a RDBMS.
+  2 | Groonga is fast full text search engine.
+(2 rows)
+
+DROP TABLE memos;

  Added: expected/full-text-search/text/single/match-contain/seqscan.out (+20 -0) 100644
===================================================================
--- /dev/null
+++ expected/full-text-search/text/single/match-contain/seqscan.out    2016-01-28 14:22:59 +0900 (d830ff5)
@@ -0,0 +1,20 @@
+CREATE TABLE memos (
+  id integer,
+  content text
+);
+INSERT INTO memos VALUES (1, 'PostgreSQL is a RDBMS.');
+INSERT INTO memos VALUES (2, 'Groonga is fast full text search engine.');
+INSERT INTO memos VALUES (3, 'PGroonga is a PostgreSQL extension that uses Groonga.');
+SET enable_seqscan = on;
+SET enable_indexscan = off;
+SET enable_bitmapscan = off;
+SELECT id, content
+  FROM memos
+ WHERE content &@> Array['rdbms', 'engine'];
+ id |                 content                  
+----+------------------------------------------
+  1 | PostgreSQL is a RDBMS.
+  2 | Groonga is fast full text search engine.
+(2 rows)
+
+DROP TABLE memos;

  Modified: pgroonga.sql (+16 -2)
===================================================================
--- pgroonga.sql    2016-01-27 10:06:53 +0900 (e8e1d68)
+++ pgroonga.sql    2016-01-28 14:22:59 +0900 (511f806)
@@ -206,7 +206,7 @@ CREATE FUNCTION pgroonga.options(internal)
 DELETE FROM pg_catalog.pg_am WHERE amname = 'pgroonga';
 INSERT INTO pg_catalog.pg_am VALUES(
 	'pgroonga',	-- amname
-	12,		-- amstrategies
+	13,		-- amstrategies
 	0,		-- amsupport
 	true,		-- amcanorder
 	true,		-- amcanorderbyop
@@ -388,6 +388,20 @@ CREATE OPERATOR &?> (
 	RIGHTARG = text[]
 );
 
+CREATE FUNCTION pgroonga.match_contain_text(text, text[])
+	RETURNS bool
+	AS 'MODULE_PATHNAME', 'pgroonga_match_contain_text'
+	LANGUAGE C
+	IMMUTABLE
+	STRICT;
+
+CREATE OPERATOR &@> (
+	PROCEDURE = pgroonga.match_contain_text,
+	LEFTARG = text,
+	RIGHTARG = text[]
+);
+
 CREATE OPERATOR CLASS pgroonga.text_full_text_search_ops_v2 FOR TYPE text
 	USING pgroonga AS
-		OPERATOR 12 &?> (text, text[]);
+		OPERATOR 12 &?> (text, text[]),
+		OPERATOR 13 &@> (text, text[]);

  Added: sql/full-text-search/text/single/match-contain/bitmapscan.sql (+21 -0) 100644
===================================================================
--- /dev/null
+++ sql/full-text-search/text/single/match-contain/bitmapscan.sql    2016-01-28 14:22:59 +0900 (d2bfdb1)
@@ -0,0 +1,21 @@
+CREATE TABLE memos (
+  id integer,
+  content text
+);
+
+INSERT INTO memos VALUES (1, 'PostgreSQL is a RDBMS.');
+INSERT INTO memos VALUES (2, 'Groonga is fast full text search engine.');
+INSERT INTO memos VALUES (3, 'PGroonga is a PostgreSQL extension that uses Groonga.');
+
+CREATE INDEX pgrn_index ON memos
+ USING pgroonga (content pgroonga.text_full_text_search_ops_v2);
+
+SET enable_seqscan = off;
+SET enable_indexscan = off;
+SET enable_bitmapscan = on;
+
+SELECT id, content
+  FROM memos
+ WHERE content &@> Array['rdbms', 'engine'];
+
+DROP TABLE memos;

  Added: sql/full-text-search/text/single/match-contain/indexscan.sql (+21 -0) 100644
===================================================================
--- /dev/null
+++ sql/full-text-search/text/single/match-contain/indexscan.sql    2016-01-28 14:22:59 +0900 (60fed74)
@@ -0,0 +1,21 @@
+CREATE TABLE memos (
+  id integer,
+  content text
+);
+
+INSERT INTO memos VALUES (1, 'PostgreSQL is a RDBMS.');
+INSERT INTO memos VALUES (2, 'Groonga is fast full text search engine.');
+INSERT INTO memos VALUES (3, 'PGroonga is a PostgreSQL extension that uses Groonga.');
+
+CREATE INDEX pgrn_index ON memos
+ USING pgroonga (content pgroonga.text_full_text_search_ops_v2);
+
+SET enable_seqscan = off;
+SET enable_indexscan = on;
+SET enable_bitmapscan = off;
+
+SELECT id, content
+  FROM memos
+ WHERE content &@> Array['rdbms', 'engine'];
+
+DROP TABLE memos;

  Added: sql/full-text-search/text/single/match-contain/seqscan.sql (+18 -0) 100644
===================================================================
--- /dev/null
+++ sql/full-text-search/text/single/match-contain/seqscan.sql    2016-01-28 14:22:59 +0900 (1897290)
@@ -0,0 +1,18 @@
+CREATE TABLE memos (
+  id integer,
+  content text
+);
+
+INSERT INTO memos VALUES (1, 'PostgreSQL is a RDBMS.');
+INSERT INTO memos VALUES (2, 'Groonga is fast full text search engine.');
+INSERT INTO memos VALUES (3, 'PGroonga is a PostgreSQL extension that uses Groonga.');
+
+SET enable_seqscan = on;
+SET enable_indexscan = off;
+SET enable_bitmapscan = off;
+
+SELECT id, content
+  FROM memos
+ WHERE content &@> Array['rdbms', 'engine'];
+
+DROP TABLE memos;

  Modified: src/pgroonga.c (+80 -6)
===================================================================
--- src/pgroonga.c    2016-01-27 10:06:53 +0900 (5d28a8f)
+++ src/pgroonga.c    2016-01-28 14:22:59 +0900 (aa34fa8)
@@ -132,6 +132,7 @@ PG_FUNCTION_INFO_V1(pgroonga_match_regexp_varchar);
 
 /* v2 */
 PG_FUNCTION_INFO_V1(pgroonga_query_contain_text);
+PG_FUNCTION_INFO_V1(pgroonga_match_contain_text);
 
 PG_FUNCTION_INFO_V1(pgroonga_insert);
 PG_FUNCTION_INFO_V1(pgroonga_beginscan);
@@ -1460,6 +1461,40 @@ pgroonga_query_contain_text(PG_FUNCTION_ARGS)
 	PG_RETURN_BOOL(matched);
 }
 
+/**
+ * pgroonga.match_contain(target text, keywords text[]) : bool
+ */
+Datum
+pgroonga_match_contain_text(PG_FUNCTION_ARGS)
+{
+	text *target = PG_GETARG_TEXT_PP(0);
+	ArrayType *keywords = PG_GETARG_ARRAYTYPE_P(1);
+	grn_bool matched;
+	int i, n;
+
+	n = ARR_DIMS(keywords)[0];
+	for (i = 1; i <= n; i++)
+	{
+		Datum keywordDatum;
+		text *keyword;
+		bool isNULL;
+
+		keywordDatum = array_ref(keywords, 1, &i, -1, -1, false, 'i', &isNULL);
+		if (isNULL)
+			continue;
+
+		keyword = DatumGetTextPP(keywordDatum);
+		matched = pgroonga_match_term_raw(VARDATA_ANY(target),
+										  VARSIZE_ANY_EXHDR(target),
+										  VARDATA_ANY(keyword),
+										  VARSIZE_ANY_EXHDR(keyword));
+		if (matched)
+			break;
+	}
+
+	PG_RETURN_BOOL(matched);
+}
+
 static void
 PGrnInsert(Relation index,
 		   grn_obj *sourcesTable,
@@ -1978,6 +2013,20 @@ PGrnSearchBuildConditionQuery(PGrnScanOpaque so,
 	}
 }
 
+static void
+PGrnSearchBuildConditionBinaryOperation(PGrnSearchData *data,
+										grn_obj *targetColumn,
+										grn_obj *value,
+										grn_operator operator)
+{
+	grn_expr_append_obj(ctx, data->expression,
+						targetColumn, GRN_OP_PUSH, 1);
+	grn_expr_append_op(ctx, data->expression, GRN_OP_GET_VALUE, 1);
+	grn_expr_append_const(ctx, data->expression,
+						  value, GRN_OP_PUSH, 1);
+	grn_expr_append_op(ctx, data->expression, operator, 2);
+}
+
 static bool
 PGrnSearchBuildCondition(IndexScanDesc scan,
 						 PGrnScanOpaque so,
@@ -2047,6 +2096,7 @@ PGrnSearchBuildCondition(IndexScanDesc scan,
 		operator = GRN_OP_REGEXP;
 		break;
 	case PGrnQueryContainStrategyNumber:
+	case PGrnMatchContainStrategyNumber:
 		switch (attribute->atttypid)
 		{
 		case TEXTOID:
@@ -2111,13 +2161,37 @@ PGrnSearchBuildCondition(IndexScanDesc scan,
 		}
 		break;
 	}
+	case PGrnMatchContainStrategyNumber:
+	{
+		grn_obj *keywords = &(buffers->general);
+		grn_obj keywordBuffer;
+		unsigned int i, n;
+
+		GRN_TEXT_INIT(&keywordBuffer, GRN_OBJ_DO_SHALLOW_COPY);
+		n = grn_vector_size(ctx, keywords);
+		for (i = 0; i < n; i++)
+		{
+			const char *keyword;
+			unsigned int keywordSize;
+
+			keywordSize = grn_vector_get_element(ctx, keywords, i,
+												&keyword, NULL, NULL);
+			GRN_TEXT_SET(ctx, &keywordBuffer, keyword, keywordSize);
+			PGrnSearchBuildConditionBinaryOperation(data,
+													targetColumn,
+													&keywordBuffer,
+													GRN_OP_MATCH);
+			if (i > 0)
+				grn_expr_append_op(ctx, data->expression, GRN_OP_OR, 2);
+		}
+		GRN_OBJ_FIN(ctx, &keywordBuffer);
+		break;
+	}
 	default:
-		grn_expr_append_obj(ctx, data->expression,
-							targetColumn, GRN_OP_PUSH, 1);
-		grn_expr_append_op(ctx, data->expression, GRN_OP_GET_VALUE, 1);
-		grn_expr_append_const(ctx, data->expression,
-							  &(buffers->general), GRN_OP_PUSH, 1);
-		grn_expr_append_op(ctx, data->expression, operator, 2);
+		PGrnSearchBuildConditionBinaryOperation(data,
+												targetColumn,
+												&(buffers->general),
+												operator);
 		break;
 	}
 

  Modified: src/pgroonga.h (+2 -1)
===================================================================
--- src/pgroonga.h    2016-01-27 10:06:53 +0900 (c42eeaf)
+++ src/pgroonga.h    2016-01-28 14:22:59 +0900 (322d0ad)
@@ -24,7 +24,8 @@
 #define PGrnRegexpStrategyNumber		10	/* operator @~ (@~ in Groonga)  */
 #define PGrnJSONContainStrategyNumber	11	/* operator @> */
 
-#define PGrnQueryContainStrategyNumber	12	/* operator &?> */
+#define PGrnQueryContainStrategyNumber	12	/* operator &?> (query in Groonga) */
+#define PGrnMatchContainStrategyNumber	13	/* operator &@> (@ in Groonga) */
 
 /* file and table names */
 #define PGrnLogBasename					"pgroonga.log"
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index