Skip to content

Commit

Permalink
escalate: add an experimental function
Browse files Browse the repository at this point in the history
It's similar to the existing match escalation mechanism but this is more
generic.

The existing match escalation mechanism is just for one full text
search by inverted index. But escalate() is for any conditions.

Syntax:

    escalate(THRESHOLD_1, CONDITION_1,
             THRESHOLD_2, CONDITION_2,
             ...,
             THRESHOLD_N, CONDITION_N)

THRESHOLD is a positive number such 0 and 29.

CONDITION is a string that uses script syntax such as "number_column >
29".

THRESHOLD is used for executing the corresponding CONDITION. If the
current result set has less than or equal to THRESHOLD_1 records, the
corresponding CONDITION_1 is executed. If the next result set has less
than or equal to THRESHOLD_2 records, the corresponding CONDITION_2 is
executed. If the next result set has greater than THRESHOLD_3 records,
escalate() is finished.

If all CONDITIONs are executed, "escalate(THRESHOLD_1, CONDITION_1,
..., THRESHOLD_N, CONDITION_N)" is same as "CONDITION_1 || ... ||
CONDITION_N".

escalate() can be worked with logical operators such as "&&" and "&!":

    number_column > 10 && escalate(THRESHOLD_1, CONDITION_1,
                                   ...,
                                   THRESHOLD_N, CONDITION_N)
    number_column > 10 &! escalate(THRESHOLD_1, CONDITION_1,
                                   ...,
                                   THRESHOLD_N, CONDITION_N)

They are same as "number_column > 10 && (CONDITION_1 || ... ||
CONDITION_N)" and "number_column > 10 &! (CONDITION_1 || ... ||
CONDITION_N)". But these behavior may be changed because this behavior
may not be useful.
  • Loading branch information
kou committed Aug 26, 2022
1 parent ec94ced commit 6d9ccb0
Show file tree
Hide file tree
Showing 55 changed files with 1,524 additions and 90 deletions.
1 change: 1 addition & 0 deletions lib/grn_proc.h
Expand Up @@ -49,6 +49,7 @@ void grn_proc_init_config_delete(grn_ctx *ctx);
void grn_proc_init_define_selector(grn_ctx *ctx);
void grn_proc_init_dump(grn_ctx *ctx);
void grn_proc_init_edit_distance(grn_ctx *ctx);
void grn_proc_init_escalate(grn_ctx *ctx);
void grn_proc_init_fuzzy_search(grn_ctx *ctx);
void grn_proc_init_highlight(grn_ctx *ctx);
void grn_proc_init_highlight_full(grn_ctx *ctx);
Expand Down
2 changes: 2 additions & 0 deletions lib/proc.c
Expand Up @@ -5143,4 +5143,6 @@ grn_db_init_builtin_commands(grn_ctx *ctx)
grn_proc_init_column_create_similar(ctx);

grn_proc_init_thread_dump(ctx);

grn_proc_init_escalate(ctx);
}
1 change: 1 addition & 0 deletions lib/proc/c_sources.am
Expand Up @@ -3,6 +3,7 @@ libgrnproc_c_sources = \
proc_column.c \
proc_config.c \
proc_dump.c \
proc_escalate.c \
proc_fuzzy_search.c \
proc_highlight.c \
proc_in_records.c \
Expand Down
248 changes: 248 additions & 0 deletions lib/proc/proc_escalate.c
@@ -0,0 +1,248 @@
/*
Copyright(C) 2022 Sutou Kouhei <kou@clear-code.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 2.1 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "../grn_ctx.h"
#include "../grn_output.h"
#include "../grn_proc.h"

#include <groonga/plugin.h>

static grn_rc
selector_escalate(grn_ctx *ctx,
grn_obj *table,
grn_obj *index,
int n_args,
grn_obj **args,
grn_obj *result_set,
grn_operator op)
{
const char *tag = "[escalate]";

if (((n_args - 1) % 2) != 0) {
ERR(GRN_INVALID_ARGUMENT,
"%s wrong number of arguments (%d for 0, 2, 4, 6, ...)",
tag,
n_args - 1);
return ctx->rc;
}

if (n_args == 1) {
return ctx->rc;
}

grn_obj *current_result_set = result_set;
grn_obj *aggregated_result_set = NULL;

int i;
for (i = 1; i < n_args; i += 2) {
grn_obj *threashold = args[i];
grn_obj *condition_text = args[i + 1];

if (!grn_obj_is_number_family_bulk(ctx, threashold)) {
grn_obj inspected;
GRN_TEXT_INIT(&inspected, 0);
grn_inspect(ctx, &inspected, threashold);
ERR(GRN_INVALID_ARGUMENT,
"%s the %dth argument must be threshold as number: %.*s",
tag,
i - 1,
(int)GRN_TEXT_LEN(&inspected),
GRN_TEXT_VALUE(&inspected));
GRN_OBJ_FIN(ctx, &inspected);
goto exit;
}

if (!grn_obj_is_text_family_bulk(ctx, condition_text)) {
grn_obj inspected;
GRN_TEXT_INIT(&inspected, 0);
grn_inspect(ctx, &inspected, condition_text);
ERR(GRN_INVALID_ARGUMENT,
"%s the %dth argument must be condition as string: %.*s",
tag,
(i - 1) + 1,
(int)GRN_TEXT_LEN(&inspected),
GRN_TEXT_VALUE(&inspected));
GRN_OBJ_FIN(ctx, &inspected);
goto exit;
}

grn_obj casted_threshold;
GRN_INT64_INIT(&casted_threshold, 0);
grn_obj_cast(ctx, threashold, &casted_threshold, false);
int64_t raw_threshold = GRN_INT64_VALUE(&casted_threshold);
GRN_OBJ_FIN(ctx, &casted_threshold);
bool need_to_evaluate_condition =
(raw_threshold < 0 ||
(int64_t)grn_table_size(ctx, current_result_set) <= raw_threshold);
if (!need_to_evaluate_condition) {
break;
}

grn_obj *dummy_variable;
grn_obj *condition;
GRN_EXPR_CREATE_FOR_QUERY(ctx, table, condition, dummy_variable);
if (!condition) {
goto exit;
}

grn_expr_parse(ctx,
condition,
GRN_TEXT_VALUE(condition_text),
GRN_TEXT_LEN(condition_text),
NULL,
GRN_OP_MATCH,
GRN_OP_AND,
GRN_EXPR_SYNTAX_SCRIPT);
if (ctx->rc != GRN_SUCCESS) {
grn_obj_close(ctx, condition);
goto exit;
}

grn_obj *target_result_set = NULL;
if (op == GRN_OP_OR) {
target_result_set = result_set;
} else {
/* We use a copy of the original result set as the base result set
* to reduce result set size as much as possible. It's for
* performance. */
target_result_set =
grn_table_create(ctx,
NULL, 0,
NULL,
GRN_OBJ_TABLE_HASH_KEY|GRN_OBJ_WITH_SUBREC,
table,
NULL);
if (!target_result_set) {
grn_obj_close(ctx, condition);
goto exit;
}
grn_rc rc = grn_result_set_copy(ctx,
(grn_hash *)result_set,
(grn_hash *)target_result_set);
if (rc != GRN_SUCCESS) {
grn_obj_close(ctx, target_result_set);
grn_obj_close(ctx, condition);
goto exit;
}
}
grn_operator select_op = op;
if (op == GRN_OP_AND_NOT) {
/* We need to use AND for AND_NOT because
*
* (BASE &! (((BASE &! A) || (BASE &! B))))
*
* doesn't equal to
*
* (BASE &! (A || B))
*
* . If we use AND here, the expression is the following:
*
* (BASE &! ((BASE && A) || (BASE && B)))
*
* And it equals to
*
* (BASE &! (A || B))
*
* . */
select_op = GRN_OP_AND;
}
grn_table_select(ctx, table, condition, target_result_set, select_op);
grn_obj_close(ctx, condition);
if (ctx->rc != GRN_SUCCESS) {
grn_obj_close(ctx, target_result_set);
goto exit;
}

if (op != GRN_OP_OR) {
if (!aggregated_result_set) {
aggregated_result_set = target_result_set;
} else {
grn_table_setoperation(ctx,
aggregated_result_set,
target_result_set,
aggregated_result_set,
GRN_OP_OR);
grn_obj_close(ctx, target_result_set);
if (ctx->rc != GRN_SUCCESS) {
goto exit;
}
}
if (current_result_set != result_set) {
grn_obj_close(ctx, current_result_set);
}
current_result_set =
grn_table_create(ctx,
NULL, 0,
NULL,
GRN_OBJ_TABLE_HASH_KEY|GRN_OBJ_WITH_SUBREC,
table,
NULL);
grn_rc rc = grn_result_set_copy(ctx,
(grn_hash *)result_set,
(grn_hash *)current_result_set);
if (rc != GRN_SUCCESS) {
goto exit;
}
grn_table_setoperation(ctx,
current_result_set,
aggregated_result_set,
current_result_set,
op);
if (ctx->rc != GRN_SUCCESS) {
goto exit;
}
}
}

if (op != GRN_OP_OR && aggregated_result_set) {
grn_table_setoperation(ctx,
result_set,
aggregated_result_set,
result_set,
op);
if (ctx->rc != GRN_SUCCESS) {
goto exit;
}
}

exit :
if (aggregated_result_set) {
grn_obj_close(ctx, aggregated_result_set);
}
if (current_result_set != result_set) {
grn_obj_close(ctx, current_result_set);
}

return ctx->rc;
}

void
grn_proc_init_escalate(grn_ctx *ctx)
{
grn_obj *selector_proc;

selector_proc = grn_proc_create(ctx,
"escalate", -1,
GRN_PROC_FUNCTION,
NULL,
NULL,
NULL,
0,
NULL);
grn_proc_set_selector(ctx, selector_proc, selector_escalate);
grn_proc_set_selector_operator(ctx, selector_proc, GRN_OP_NOP);
}
6 changes: 3 additions & 3 deletions test/command/suite/schema/plugins.expected
Expand Up @@ -247,15 +247,15 @@ schema
},
"token_filters": {
"TokenFilterNFKC100": {
"id": 224,
"id": 225,
"name": "TokenFilterNFKC100"
},
"TokenFilterNFKC121": {
"id": 225,
"id": 226,
"name": "TokenFilterNFKC121"
},
"TokenFilterNFKC130": {
"id": 226,
"id": 227,
"name": "TokenFilterNFKC130"
}
},
Expand Down
Expand Up @@ -246,15 +246,15 @@ schema
},
"token_filters": {
"TokenFilterNFKC100": {
"id": 224,
"id": 225,
"name": "TokenFilterNFKC100"
},
"TokenFilterNFKC121": {
"id": 225,
"id": 226,
"name": "TokenFilterNFKC121"
},
"TokenFilterNFKC130": {
"id": 226,
"id": 227,
"name": "TokenFilterNFKC130"
}
},
Expand Down
Expand Up @@ -246,15 +246,15 @@ schema
},
"token_filters": {
"TokenFilterNFKC100": {
"id": 224,
"id": 225,
"name": "TokenFilterNFKC100"
},
"TokenFilterNFKC121": {
"id": 225,
"id": 226,
"name": "TokenFilterNFKC121"
},
"TokenFilterNFKC130": {
"id": 226,
"id": 227,
"name": "TokenFilterNFKC130"
}
},
Expand Down
Expand Up @@ -246,15 +246,15 @@ schema
},
"token_filters": {
"TokenFilterNFKC100": {
"id": 224,
"id": 225,
"name": "TokenFilterNFKC100"
},
"TokenFilterNFKC121": {
"id": 225,
"id": 226,
"name": "TokenFilterNFKC121"
},
"TokenFilterNFKC130": {
"id": 226,
"id": 227,
"name": "TokenFilterNFKC130"
}
},
Expand Down
Expand Up @@ -248,15 +248,15 @@ schema
},
"token_filters": {
"TokenFilterNFKC100": {
"id": 224,
"id": 225,
"name": "TokenFilterNFKC100"
},
"TokenFilterNFKC121": {
"id": 225,
"id": 226,
"name": "TokenFilterNFKC121"
},
"TokenFilterNFKC130": {
"id": 226,
"id": 227,
"name": "TokenFilterNFKC130"
}
},
Expand Down
Expand Up @@ -248,15 +248,15 @@ schema
},
"token_filters": {
"TokenFilterNFKC100": {
"id": 224,
"id": 225,
"name": "TokenFilterNFKC100"
},
"TokenFilterNFKC121": {
"id": 225,
"id": 226,
"name": "TokenFilterNFKC121"
},
"TokenFilterNFKC130": {
"id": 226,
"id": 227,
"name": "TokenFilterNFKC130"
}
},
Expand Down

0 comments on commit 6d9ccb0

Please sign in to comment.