Kouhei Sutou
null+****@clear*****
Thu Oct 5 16:49:53 JST 2017
Kouhei Sutou 2017-10-05 16:49:53 +0900 (Thu, 05 Oct 2017) New Revision: 04b7ddf35017c465af849fdb26161e1e2617fc9c https://github.com/pgroonga/pgroonga/commit/04b7ddf35017c465af849fdb26161e1e2617fc9c Message: pgroonga_query_expand: support "text" as synonym column type Added files: expected/function/query-expand/invalid-type.out expected/function/query-expand/text.out sql/function/query-expand/invalid-type.sql sql/function/query-expand/text.sql Removed files: expected/function/query-expand/not-text-array-synonyms.out sql/function/query-expand/not-text-array-synonyms.sql Modified files: src/pgrn-query-expand.c Added: expected/function/query-expand/invalid-type.out (+7 -0) 100644 =================================================================== --- /dev/null +++ expected/function/query-expand/invalid-type.out 2017-10-05 16:49:53 +0900 (6919188) @@ -0,0 +1,7 @@ +CREATE TABLE synonyms ( + term text PRIMARY KEY, + synonym int +); +SELECT pgroonga_query_expand('synonyms', 'term', 'synonym', 'PGroonga'); +ERROR: pgroonga: query_expand: synonyms column isn't text type nor text[] type: <synonyms>.<synonym> +DROP TABLE synonyms; Deleted: expected/function/query-expand/not-text-array-synonyms.out (+0 -7) 100644 =================================================================== --- expected/function/query-expand/not-text-array-synonyms.out 2017-08-23 08:47:48 +0900 (99391a7) +++ /dev/null @@ -1,7 +0,0 @@ -CREATE TABLE synonyms ( - term text PRIMARY KEY, - synonym text -); -SELECT pgroonga_query_expand('synonyms', 'term', 'synonym', 'Groonga'); -ERROR: pgroonga: query_expand: synonyms column isn't text[] type: <synonyms>.<synonym> -DROP TABLE synonyms; Added: expected/function/query-expand/text.out (+14 -0) 100644 =================================================================== --- /dev/null +++ expected/function/query-expand/text.out 2017-10-05 16:49:53 +0900 (7a27654) @@ -0,0 +1,14 @@ +CREATE TABLE synonyms ( + term text, + synonym text +); +CREATE INDEX synonyms_term_index ON synonyms (term); +INSERT INTO synonyms VALUES ('PGroonga', 'PGroonga'); +INSERT INTO synonyms VALUES ('PGroonga', 'Groonga PostgreSQL'); +SELECT pgroonga_query_expand('synonyms', 'term', 'synonym', 'PGroonga'); + pgroonga_query_expand +-------------------------------------- + ((Groonga PostgreSQL) OR (PGroonga)) +(1 row) + +DROP TABLE synonyms; Added: sql/function/query-expand/invalid-type.sql (+8 -0) 100644 =================================================================== --- /dev/null +++ sql/function/query-expand/invalid-type.sql 2017-10-05 16:49:53 +0900 (b4a523b) @@ -0,0 +1,8 @@ +CREATE TABLE synonyms ( + term text PRIMARY KEY, + synonym int +); + +SELECT pgroonga_query_expand('synonyms', 'term', 'synonym', 'PGroonga'); + +DROP TABLE synonyms; Deleted: sql/function/query-expand/not-text-array-synonyms.sql (+0 -8) 100644 =================================================================== --- sql/function/query-expand/not-text-array-synonyms.sql 2017-08-23 08:47:48 +0900 (9489322) +++ /dev/null @@ -1,8 +0,0 @@ -CREATE TABLE synonyms ( - term text PRIMARY KEY, - synonym text -); - -SELECT pgroonga_query_expand('synonyms', 'term', 'synonym', 'Groonga'); - -DROP TABLE synonyms; Added: sql/function/query-expand/text.sql (+13 -0) 100644 =================================================================== --- /dev/null +++ sql/function/query-expand/text.sql 2017-10-05 16:49:53 +0900 (7314f01) @@ -0,0 +1,13 @@ +CREATE TABLE synonyms ( + term text, + synonym text +); + +CREATE INDEX synonyms_term_index ON synonyms (term); + +INSERT INTO synonyms VALUES ('PGroonga', 'PGroonga'); +INSERT INTO synonyms VALUES ('PGroonga', 'Groonga PostgreSQL'); + +SELECT pgroonga_query_expand('synonyms', 'term', 'synonym', 'PGroonga'); + +DROP TABLE synonyms; Modified: src/pgrn-query-expand.c (+46 -24) =================================================================== --- src/pgrn-query-expand.c 2017-08-23 08:47:48 +0900 (6c19ce4) +++ src/pgrn-query-expand.c 2017-10-05 16:49:53 +0900 (fa6cb91) @@ -82,8 +82,6 @@ func_query_expander_postgresql(grn_ctx *ctx, { Datum synonymsDatum; bool isNULL; - ArrayType *synonymsArray; - int i, n; if (currentData.scan) tuple = index_getnext(currentData.scan, ForwardScanDirection); @@ -100,36 +98,58 @@ func_query_expander_postgresql(grn_ctx *ctx, if (isNULL) continue; - synonymsArray = DatumGetArrayTypeP(synonymsDatum); - n = ARR_DIMS(synonymsArray)[0]; - if (n == 0) - continue; - - if (ith_synonyms == 0) - GRN_TEXT_PUTC(ctx, expandedTerm, '('); - else - GRN_TEXT_PUTS(ctx, expandedTerm, " OR "); - - for (i = 1; i <= n; i++) + if (currentData.synonymsAttribute->atttypid == TEXTOID) { - Datum synonymDatum; - bool isNULL; text *synonym; + synonym = DatumGetTextP(synonymsDatum); - synonymDatum = array_ref(synonymsArray, 1, &i, -1, - currentData.synonymsAttribute->attlen, - currentData.synonymsAttribute->attbyval, - currentData.synonymsAttribute->attalign, - &isNULL); - synonym = DatumGetTextP(synonymDatum); - if (i > 1) + if (ith_synonyms == 0) + GRN_TEXT_PUTC(ctx, expandedTerm, '('); + else GRN_TEXT_PUTS(ctx, expandedTerm, " OR "); + GRN_TEXT_PUTC(ctx, expandedTerm, '('); GRN_TEXT_PUT(ctx, expandedTerm, VARDATA_ANY(synonym), VARSIZE_ANY_EXHDR(synonym)); GRN_TEXT_PUTC(ctx, expandedTerm, ')'); } + else + { + ArrayType *synonymsArray; + int i, n; + + synonymsArray = DatumGetArrayTypeP(synonymsDatum); + n = ARR_DIMS(synonymsArray)[0]; + if (n == 0) + continue; + + if (ith_synonyms == 0) + GRN_TEXT_PUTC(ctx, expandedTerm, '('); + else + GRN_TEXT_PUTS(ctx, expandedTerm, " OR "); + + for (i = 1; i <= n; i++) + { + Datum synonymDatum; + bool isNULL; + text *synonym; + + synonymDatum = array_ref(synonymsArray, 1, &i, -1, + currentData.synonymsAttribute->attlen, + currentData.synonymsAttribute->attbyval, + currentData.synonymsAttribute->attalign, + &isNULL); + synonym = DatumGetTextP(synonymDatum); + if (i > 1) + GRN_TEXT_PUTS(ctx, expandedTerm, " OR "); + GRN_TEXT_PUTC(ctx, expandedTerm, '('); + GRN_TEXT_PUT(ctx, expandedTerm, + VARDATA_ANY(synonym), + VARSIZE_ANY_EXHDR(synonym)); + GRN_TEXT_PUTC(ctx, expandedTerm, ')'); + } + } ith_synonyms++; @@ -182,12 +202,14 @@ PGrnFindSynonymsAttribute(const char *tableName, if (strncmp(attribute->attname.data, columnName, columnNameSize) != 0) continue; - if (attribute->atttypid != TEXTARRAYOID) + if (!(attribute->atttypid == TEXTOID || + attribute->atttypid == TEXTARRAYOID)) { ereport(ERROR, (errcode(ERRCODE_INVALID_NAME), errmsg("pgroonga: query_expand: " - "synonyms column isn't text[] type: <%s>.<%.*s>", + "synonyms column isn't text type nor text[] type: " + "<%s>.<%.*s>", tableName, (int)columnNameSize, columnName))); } -------------- next part -------------- HTML����������������������������... URL: https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20171005/afb80ff5/attachment-0001.htm