Navigation Menu

Skip to content

Commit

Permalink
pgroonga_query_expand: support "text" as synonym column type
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Oct 5, 2017
1 parent 2e6c055 commit 04b7ddf
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 39 deletions.
7 changes: 7 additions & 0 deletions expected/function/query-expand/invalid-type.out
@@ -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;
7 changes: 0 additions & 7 deletions expected/function/query-expand/not-text-array-synonyms.out

This file was deleted.

14 changes: 14 additions & 0 deletions expected/function/query-expand/text.out
@@ -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;
8 changes: 8 additions & 0 deletions sql/function/query-expand/invalid-type.sql
@@ -0,0 +1,8 @@
CREATE TABLE synonyms (
term text PRIMARY KEY,
synonym int
);

SELECT pgroonga_query_expand('synonyms', 'term', 'synonym', 'PGroonga');

DROP TABLE synonyms;
8 changes: 0 additions & 8 deletions sql/function/query-expand/not-text-array-synonyms.sql

This file was deleted.

13 changes: 13 additions & 0 deletions sql/function/query-expand/text.sql
@@ -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;
70 changes: 46 additions & 24 deletions src/pgrn-query-expand.c
Expand Up @@ -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);
Expand All @@ -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++;

Expand Down Expand Up @@ -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)));
}
Expand Down

0 comments on commit 04b7ddf

Please sign in to comment.