Kouhei Sutou
null+****@clear*****
Sun Feb 14 15:40:24 JST 2016
Kouhei Sutou 2016-02-14 15:40:24 +0900 (Sun, 14 Feb 2016) New Revision: 341a0f7685b5cc7bb61cdd6d01eb58d2c3b5ff7f https://github.com/groonga/groonga/commit/341a0f7685b5cc7bb61cdd6d01eb58d2c3b5ff7f Message: Fix a bug that sort by integer patricia trie key returns unsorted result GitHub: GitHub #476 Reported by Ryunosuke SATO. Thanks!!! Added files: test/command/suite/select/sort/key/double_array_trie.expected test/command/suite/select/sort/key/double_array_trie.test test/command/suite/select/sort/key/hash_key.expected test/command/suite/select/sort/key/hash_key.test test/command/suite/select/sort/key/patricia_trie.expected test/command/suite/select/sort/key/patricia_trie.test Modified files: lib/db.c lib/grn_pat.h lib/pat.c Modified: lib/db.c (+30 -0) =================================================================== --- lib/db.c 2016-02-14 15:04:09 +0900 (f0cbe01) +++ lib/db.c 2016-02-14 15:40:24 +0900 (4d849ea) @@ -11091,6 +11091,31 @@ is_sub_record_accessor(grn_ctx *ctx, grn_obj *obj) return GRN_FALSE; } +static grn_bool +is_encoded_pat_key_accessor(grn_ctx *ctx, grn_obj *obj) +{ + grn_accessor *accessor; + + if (!grn_obj_is_accessor(ctx, obj)) { + return GRN_FALSE; + } + + accessor = (grn_accessor *)obj; + while (accessor->next) { + accessor = accessor->next; + } + + if (accessor->action != GRN_ACCESSOR_GET_KEY) { + return GRN_FALSE; + } + + if (accessor->obj->header.type != GRN_TABLE_PAT_KEY) { + return GRN_FALSE; + } + + return grn_pat_is_key_encoded(ctx, (grn_pat *)(accessor->obj)); +} + static int range_is_idp(grn_obj *obj) { @@ -11171,6 +11196,7 @@ grn_table_sort(grn_ctx *ctx, grn_obj *table, int offset, int limit, int j; grn_bool have_compressed_column = GRN_FALSE; grn_bool have_sub_record_accessor = GRN_FALSE; + grn_bool have_encoded_pat_key_accessor = GRN_FALSE; grn_bool have_index_value_get = GRN_FALSE; grn_table_sort_key *kp; for (kp = keys, j = n_keys; j; kp++, j--) { @@ -11180,6 +11206,9 @@ grn_table_sort(grn_ctx *ctx, grn_obj *table, int offset, int limit, if (is_sub_record_accessor(ctx, kp->key)) { have_sub_record_accessor = GRN_TRUE; } + if (is_encoded_pat_key_accessor(ctx, kp->key)) { + have_encoded_pat_key_accessor = GRN_TRUE; + } if (range_is_idp(kp->key)) { kp->offset = KEY_ID; } else { @@ -11254,6 +11283,7 @@ grn_table_sort(grn_ctx *ctx, grn_obj *table, int offset, int limit, } if (have_compressed_column || have_sub_record_accessor || + have_encoded_pat_key_accessor || have_index_value_get) { i = grn_table_sort_value(ctx, table, offset, limit, result, keys, n_keys); Modified: lib/grn_pat.h (+2 -0) =================================================================== --- lib/grn_pat.h 2016-02-14 15:04:09 +0900 (6886e90) +++ lib/grn_pat.h 2016-02-14 15:40:24 +0900 (10aa0f1) @@ -114,6 +114,8 @@ GRN_API grn_rc grn_pat_fuzzy_search(grn_ctx *ctx, grn_pat *pat, uint32_t grn_pat_total_key_size(grn_ctx *ctx, grn_pat *pat); +grn_bool grn_pat_is_key_encoded(grn_ctx *ctx, grn_pat *pat); + #ifdef __cplusplus } #endif Modified: lib/pat.c (+16 -0) =================================================================== --- lib/pat.c 2016-02-14 15:04:09 +0900 (3707d11) +++ lib/pat.c 2016-02-14 15:40:24 +0900 (4681181) @@ -3528,3 +3528,19 @@ grn_pat_total_key_size(grn_ctx *ctx, grn_pat *pat) { return pat->header->curr_key; } + +grn_bool +grn_pat_is_key_encoded(grn_ctx *ctx, grn_pat *pat) +{ + grn_obj *domain; + uint32_t key_size; + + domain = grn_ctx_at(ctx, pat->obj.header.domain); + if (grn_obj_is_type(ctx, domain)) { + key_size = grn_type_size(ctx, domain); + } else { + key_size = sizeof(grn_id); + } + + return KEY_NEEDS_CONVERT(pat, key_size); +} Added: test/command/suite/select/sort/key/double_array_trie.expected (+46 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/sort/key/double_array_trie.expected 2016-02-14 15:40:24 +0900 (4f80e8e) @@ -0,0 +1,46 @@ +table_create Numbers TABLE_DAT_KEY Int32 +[[0,0.0,0.0],true] +load --table Numbers +[ +{"_key": 127}, +{"_key": 256}, +{"_key": -255} +] +[[0,0.0,0.0],3] +select Numbers --sortby _key +[ + [ + 0, + 0.0, + 0.0 + ], + [ + [ + [ + 3 + ], + [ + [ + "_id", + "UInt32" + ], + [ + "_key", + "Int32" + ] + ], + [ + 3, + -255 + ], + [ + 1, + 127 + ], + [ + 2, + 256 + ] + ] + ] +] Added: test/command/suite/select/sort/key/double_array_trie.test (+10 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/sort/key/double_array_trie.test 2016-02-14 15:40:24 +0900 (ce060f4) @@ -0,0 +1,10 @@ +table_create Numbers TABLE_DAT_KEY Int32 + +load --table Numbers +[ +{"_key": 127}, +{"_key": 256}, +{"_key": -255} +] + +select Numbers --sortby _key Added: test/command/suite/select/sort/key/hash_key.expected (+46 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/sort/key/hash_key.expected 2016-02-14 15:40:24 +0900 (6dccef6) @@ -0,0 +1,46 @@ +table_create Numbers TABLE_HASH_KEY Int32 +[[0,0.0,0.0],true] +load --table Numbers +[ +{"_key": 127}, +{"_key": 256}, +{"_key": -255} +] +[[0,0.0,0.0],3] +select Numbers --sortby _key +[ + [ + 0, + 0.0, + 0.0 + ], + [ + [ + [ + 3 + ], + [ + [ + "_id", + "UInt32" + ], + [ + "_key", + "Int32" + ] + ], + [ + 3, + -255 + ], + [ + 1, + 127 + ], + [ + 2, + 256 + ] + ] + ] +] Added: test/command/suite/select/sort/key/hash_key.test (+10 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/sort/key/hash_key.test 2016-02-14 15:40:24 +0900 (d75e120) @@ -0,0 +1,10 @@ +table_create Numbers TABLE_HASH_KEY Int32 + +load --table Numbers +[ +{"_key": 127}, +{"_key": 256}, +{"_key": -255} +] + +select Numbers --sortby _key Added: test/command/suite/select/sort/key/patricia_trie.expected (+46 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/sort/key/patricia_trie.expected 2016-02-14 15:40:24 +0900 (e0cf7a4) @@ -0,0 +1,46 @@ +table_create Numbers TABLE_PAT_KEY Int32 +[[0,0.0,0.0],true] +load --table Numbers +[ +{"_key": 127}, +{"_key": 256}, +{"_key": -255} +] +[[0,0.0,0.0],3] +select Numbers --sortby _key +[ + [ + 0, + 0.0, + 0.0 + ], + [ + [ + [ + 3 + ], + [ + [ + "_id", + "UInt32" + ], + [ + "_key", + "Int32" + ] + ], + [ + 3, + -255 + ], + [ + 1, + 127 + ], + [ + 2, + 256 + ] + ] + ] +] Added: test/command/suite/select/sort/key/patricia_trie.test (+10 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/sort/key/patricia_trie.test 2016-02-14 15:40:24 +0900 (c62d1d4) @@ -0,0 +1,10 @@ +table_create Numbers TABLE_PAT_KEY Int32 + +load --table Numbers +[ +{"_key": 127}, +{"_key": 256}, +{"_key": -255} +] + +select Numbers --sortby _key -------------- next part -------------- HTML����������������������������... Download