Kouhei Sutou
null+****@clear*****
Fri Oct 24 17:07:22 JST 2014
Kouhei Sutou 2014-10-24 17:07:22 +0900 (Fri, 24 Oct 2014) New Revision: b373e5c171051a28caa48ddc114064d4a01e28ea https://github.com/groonga/groonga/commit/b373e5c171051a28caa48ddc114064d4a01e28ea Message: Add in_values() function in_values(column, value1, value2, value3, ...) equals to ((column == value1) OR (column == value2) OR (column == value3) OR ...) It will be fast than == and OR combinations. TODO: * Document me. Added files: test/command/suite/select/function/in_values/with_index/and.expected test/command/suite/select/function/in_values/with_index/and.test test/command/suite/select/function/in_values/with_index/only.expected test/command/suite/select/function/in_values/with_index/only.test test/command/suite/select/function/in_values/without_index/and.expected test/command/suite/select/function/in_values/without_index/and.test test/command/suite/select/function/in_values/without_index/only.expected test/command/suite/select/function/in_values/without_index/only.test Modified files: lib/proc.c Modified: lib/proc.c (+95 -0) =================================================================== --- lib/proc.c 2014-10-16 18:54:06 +0900 (66a9204) +++ lib/proc.c 2014-10-24 17:07:22 +0900 (b0b54fe) @@ -5254,6 +5254,93 @@ func_highlight_full(grn_ctx *ctx, int nargs, grn_obj **args, return highlighted; } +static grn_obj * +func_in_values(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) +{ + grn_obj *found; + grn_obj *target_value; + int i; + + found = GRN_PROC_ALLOC(GRN_DB_BOOL, 0); + if (!found) { + return NULL; + } + GRN_BOOL_SET(ctx, found, GRN_FALSE); + + if (nargs < 1) { + ERR(GRN_INVALID_ARGUMENT, + "in_values(): wrong number of arguments (%d for 1..)", nargs); + return found; + } + + target_value = args[0]; + for (i = 1; i < nargs; i++) { + grn_obj *value = args[i]; + grn_obj *equal_condition; + grn_obj *result; + grn_bool result_boolean = GRN_FALSE; + + /* TODO: Implement grn_obj_equal() and use it. */ + equal_condition = grn_expr_create(ctx, NULL, 0); + grn_expr_append_const(ctx, equal_condition, target_value, GRN_OP_PUSH, 1); + grn_expr_append_const(ctx, equal_condition, value, GRN_OP_PUSH, 1); + grn_expr_append_op(ctx, equal_condition, GRN_OP_EQUAL, 2); + result = grn_expr_exec(ctx, equal_condition, 0); + if (result) { + GRN_TRUEP(ctx, result, result_boolean); + } + grn_obj_unlink(ctx, equal_condition); + + if (result_boolean) { + GRN_BOOL_SET(ctx, found, GRN_TRUE); + break; + } + } + + return found; +} + +static grn_rc +selector_in_values(grn_ctx *ctx, grn_obj *table, grn_obj *index, + int nargs, grn_obj **args, + grn_obj *res, grn_operator op) +{ + grn_rc rc = GRN_SUCCESS; + int i; + + if (!index) { + return GRN_INVALID_ARGUMENT; + } + + if (nargs < 2) { + ERR(GRN_INVALID_ARGUMENT, + "in_values(): wrong number of arguments (%d for 1..)", nargs); + return ctx->rc; + } + + ctx->flags |= GRN_CTX_TEMPORARY_DISABLE_II_RESOLVE_SEL_AND; + for (i = 2; i < nargs; i++) { + grn_obj *value = args[i]; + grn_search_optarg search_options; + search_options.mode = GRN_OP_EXACT; + search_options.similarity_threshold = 0; + search_options.max_interval = 0; + search_options.weight_vector = NULL; + search_options.vector_size = 0; + search_options.proc = NULL; + search_options.max_size = 0; + if (i == nargs - 1) { + ctx->flags &= ~GRN_CTX_TEMPORARY_DISABLE_II_RESOLVE_SEL_AND; + } + rc = grn_obj_search(ctx, index, value, res, op, &search_options); + if (rc != GRN_SUCCESS) { + break; + } + } + + return rc; +} + #define DEF_VAR(v,name_str) do {\ (v).name = (name_str);\ (v).name_size = GRN_STRLEN(name_str);\ @@ -5484,4 +5571,12 @@ grn_db_init_builtin_query(grn_ctx *ctx) grn_proc_create(ctx, "highlight_full", -1, GRN_PROC_FUNCTION, func_highlight_full, NULL, NULL, 0, NULL); + + { + grn_obj *selector_proc; + + selector_proc = grn_proc_create(ctx, "in_values", -1, GRN_PROC_FUNCTION, + func_in_values, NULL, NULL, 0, NULL); + grn_proc_set_selector(ctx, selector_proc, selector_in_values); + } } Added: test/command/suite/select/function/in_values/with_index/and.expected (+58 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/function/in_values/with_index/and.expected 2014-10-24 17:07:22 +0900 (948d415) @@ -0,0 +1,58 @@ +table_create Tags TABLE_PAT_KEY ShortText +[[0,0.0,0.0],true] +table_create Memos TABLE_HASH_KEY ShortText +[[0,0.0,0.0],true] +column_create Memos tag COLUMN_SCALAR Tags +[[0,0.0,0.0],true] +column_create Tags memos_tag COLUMN_INDEX Memos tag +[[0,0.0,0.0],true] +load --table Memos +[ +{"_key": "Groonga is fast", "tag": "groonga"}, +{"_key": "Mroonga is fast", "tag": "mroonga"}, +{"_key": "Rroonga is fast", "tag": "rroonga"}, +{"_key": "Droonga is fast", "tag": "droonga"}, +{"_key": "Groonga is a HTTP server", "tag": "groonga"} +] +[[0,0.0,0.0],5] +select Memos --output_columns _key,tag --filter 'all_records() && in_values(tag, "groonga", "mroonga", "droonga")' --sortby _id +[ + [ + 0, + 0.0, + 0.0 + ], + [ + [ + [ + 4 + ], + [ + [ + "_key", + "ShortText" + ], + [ + "tag", + "Tags" + ] + ], + [ + "Groonga is fast", + "groonga" + ], + [ + "Mroonga is fast", + "mroonga" + ], + [ + "Droonga is fast", + "droonga" + ], + [ + "Groonga is a HTTP server", + "groonga" + ] + ] + ] +] Added: test/command/suite/select/function/in_values/with_index/and.test (+20 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/function/in_values/with_index/and.test 2014-10-24 17:07:22 +0900 (f1756c4) @@ -0,0 +1,20 @@ +table_create Tags TABLE_PAT_KEY ShortText + +table_create Memos TABLE_HASH_KEY ShortText +column_create Memos tag COLUMN_SCALAR Tags + +column_create Tags memos_tag COLUMN_INDEX Memos tag + +load --table Memos +[ +{"_key": "Groonga is fast", "tag": "groonga"}, +{"_key": "Mroonga is fast", "tag": "mroonga"}, +{"_key": "Rroonga is fast", "tag": "rroonga"}, +{"_key": "Droonga is fast", "tag": "droonga"}, +{"_key": "Groonga is a HTTP server", "tag": "groonga"} +] + +select Memos \ + --output_columns _key,tag \ + --filter 'all_records() && in_values(tag, "groonga", "mroonga", "droonga")' \ + --sortby _id Added: test/command/suite/select/function/in_values/with_index/only.expected (+58 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/function/in_values/with_index/only.expected 2014-10-24 17:07:22 +0900 (766d48f) @@ -0,0 +1,58 @@ +table_create Tags TABLE_PAT_KEY ShortText +[[0,0.0,0.0],true] +table_create Memos TABLE_HASH_KEY ShortText +[[0,0.0,0.0],true] +column_create Memos tag COLUMN_SCALAR Tags +[[0,0.0,0.0],true] +column_create Tags memos_tag COLUMN_INDEX Memos tag +[[0,0.0,0.0],true] +load --table Memos +[ +{"_key": "Groonga is fast", "tag": "groonga"}, +{"_key": "Mroonga is fast", "tag": "mroonga"}, +{"_key": "Rroonga is fast", "tag": "rroonga"}, +{"_key": "Droonga is fast", "tag": "droonga"}, +{"_key": "Groonga is a HTTP server", "tag": "groonga"} +] +[[0,0.0,0.0],5] +select Memos --output_columns _key,tag --filter 'in_values(tag, "groonga", "mroonga", "droonga")' --sortby _id +[ + [ + 0, + 0.0, + 0.0 + ], + [ + [ + [ + 4 + ], + [ + [ + "_key", + "ShortText" + ], + [ + "tag", + "Tags" + ] + ], + [ + "Groonga is fast", + "groonga" + ], + [ + "Mroonga is fast", + "mroonga" + ], + [ + "Droonga is fast", + "droonga" + ], + [ + "Groonga is a HTTP server", + "groonga" + ] + ] + ] +] Added: test/command/suite/select/function/in_values/with_index/only.test (+20 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/function/in_values/with_index/only.test 2014-10-24 17:07:22 +0900 (8d18f72) @@ -0,0 +1,20 @@ +table_create Tags TABLE_PAT_KEY ShortText + +table_create Memos TABLE_HASH_KEY ShortText +column_create Memos tag COLUMN_SCALAR Tags + +column_create Tags memos_tag COLUMN_INDEX Memos tag + +load --table Memos +[ +{"_key": "Groonga is fast", "tag": "groonga"}, +{"_key": "Mroonga is fast", "tag": "mroonga"}, +{"_key": "Rroonga is fast", "tag": "rroonga"}, +{"_key": "Droonga is fast", "tag": "droonga"}, +{"_key": "Groonga is a HTTP server", "tag": "groonga"} +] + +select Memos \ + --output_columns _key,tag \ + --filter 'in_values(tag, "groonga", "mroonga", "droonga")' \ + --sortby _id Added: test/command/suite/select/function/in_values/without_index/and.expected (+56 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/function/in_values/without_index/and.expected 2014-10-24 17:07:22 +0900 (8954ecf) @@ -0,0 +1,56 @@ +table_create Tags TABLE_PAT_KEY ShortText +[[0,0.0,0.0],true] +table_create Memos TABLE_HASH_KEY ShortText +[[0,0.0,0.0],true] +column_create Memos tag COLUMN_SCALAR Tags +[[0,0.0,0.0],true] +load --table Memos +[ +{"_key": "Groonga is fast", "tag": "groonga"}, +{"_key": "Mroonga is fast", "tag": "mroonga"}, +{"_key": "Rroonga is fast", "tag": "rroonga"}, +{"_key": "Droonga is fast", "tag": "droonga"}, +{"_key": "Groonga is a HTTP server", "tag": "groonga"} +] +[[0,0.0,0.0],5] +select Memos --output_columns _key,tag --filter 'all_records() && in_values(tag, "groonga", "mroonga", "droonga")' --sortby _id +[ + [ + 0, + 0.0, + 0.0 + ], + [ + [ + [ + 4 + ], + [ + [ + "_key", + "ShortText" + ], + [ + "tag", + "Tags" + ] + ], + [ + "Groonga is fast", + "groonga" + ], + [ + "Mroonga is fast", + "mroonga" + ], + [ + "Droonga is fast", + "droonga" + ], + [ + "Groonga is a HTTP server", + "groonga" + ] + ] + ] +] Added: test/command/suite/select/function/in_values/without_index/and.test (+18 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/function/in_values/without_index/and.test 2014-10-24 17:07:22 +0900 (ca75aa5) @@ -0,0 +1,18 @@ +table_create Tags TABLE_PAT_KEY ShortText + +table_create Memos TABLE_HASH_KEY ShortText +column_create Memos tag COLUMN_SCALAR Tags + +load --table Memos +[ +{"_key": "Groonga is fast", "tag": "groonga"}, +{"_key": "Mroonga is fast", "tag": "mroonga"}, +{"_key": "Rroonga is fast", "tag": "rroonga"}, +{"_key": "Droonga is fast", "tag": "droonga"}, +{"_key": "Groonga is a HTTP server", "tag": "groonga"} +] + +select Memos \ + --output_columns _key,tag \ + --filter 'all_records() && in_values(tag, "groonga", "mroonga", "droonga")' \ + --sortby _id Added: test/command/suite/select/function/in_values/without_index/only.expected (+56 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/function/in_values/without_index/only.expected 2014-10-24 17:07:22 +0900 (ea2593d) @@ -0,0 +1,56 @@ +table_create Tags TABLE_PAT_KEY ShortText +[[0,0.0,0.0],true] +table_create Memos TABLE_HASH_KEY ShortText +[[0,0.0,0.0],true] +column_create Memos tag COLUMN_SCALAR Tags +[[0,0.0,0.0],true] +load --table Memos +[ +{"_key": "Groonga is fast", "tag": "groonga"}, +{"_key": "Mroonga is fast", "tag": "mroonga"}, +{"_key": "Rroonga is fast", "tag": "rroonga"}, +{"_key": "Droonga is fast", "tag": "droonga"}, +{"_key": "Groonga is a HTTP server", "tag": "groonga"} +] +[[0,0.0,0.0],5] +select Memos --output_columns _key,tag --filter 'in_values(tag, "groonga", "mroonga", "droonga")' --sortby _id +[ + [ + 0, + 0.0, + 0.0 + ], + [ + [ + [ + 4 + ], + [ + [ + "_key", + "ShortText" + ], + [ + "tag", + "Tags" + ] + ], + [ + "Groonga is fast", + "groonga" + ], + [ + "Mroonga is fast", + "mroonga" + ], + [ + "Droonga is fast", + "droonga" + ], + [ + "Groonga is a HTTP server", + "groonga" + ] + ] + ] +] Added: test/command/suite/select/function/in_values/without_index/only.test (+18 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/function/in_values/without_index/only.test 2014-10-24 17:07:22 +0900 (87b1af0) @@ -0,0 +1,18 @@ +table_create Tags TABLE_PAT_KEY ShortText + +table_create Memos TABLE_HASH_KEY ShortText +column_create Memos tag COLUMN_SCALAR Tags + +load --table Memos +[ +{"_key": "Groonga is fast", "tag": "groonga"}, +{"_key": "Mroonga is fast", "tag": "mroonga"}, +{"_key": "Rroonga is fast", "tag": "rroonga"}, +{"_key": "Droonga is fast", "tag": "droonga"}, +{"_key": "Groonga is a HTTP server", "tag": "groonga"} +] + +select Memos \ + --output_columns _key,tag \ + --filter 'in_values(tag, "groonga", "mroonga", "droonga")' \ + --sortby _id -------------- next part -------------- HTML����������������������������...Download