Kouhei Sutou
null+****@clear*****
Wed Feb 1 15:23:08 JST 2017
Kouhei Sutou 2017-02-01 15:23:08 +0900 (Wed, 01 Feb 2017) New Revision: a0ddfa1247532faed0e005913389e667fd90d07e https://github.com/groonga/groonga/commit/a0ddfa1247532faed0e005913389e667fd90d07e Message: window_function: fix wrong result with window.group_keys and window.sort_keys Modified files: lib/window_function.c test/command/suite/select/columns/window_function/window_sum/group_sort.expected test/command/suite/select/columns/window_function/window_sum/group_sort.test Modified: lib/window_function.c (+41 -5) =================================================================== --- lib/window_function.c 2017-02-01 13:08:08 +0900 (5e7d615) +++ lib/window_function.c 2017-02-01 15:23:08 +0900 (79071b6) @@ -181,11 +181,47 @@ grn_window_set_sort_keys(grn_ctx *ctx, GRN_API_RETURN(ctx->rc); } - grn_table_sort(ctx, - window->grouped_table, - 0, -1, - sorted, - sort_keys, n_sort_keys); + if (window->table == window->grouped_table) { + grn_table_sort(ctx, + window->grouped_table, + 0, -1, + sorted, + sort_keys, n_sort_keys); + } else { + size_t i; + grn_table_sort_key *adjusted_sort_keys; + + adjusted_sort_keys = GRN_MALLOC(sizeof(grn_table_sort_key) * n_sort_keys); + if (!adjusted_sort_keys) { + ERR(ctx->rc, + "[window][set][sort-keys] " + "failed to allocate adjusted sort keys: %s", + ctx->errbuf); + grn_obj_unlink(ctx, sorted); + GRN_API_RETURN(ctx->rc); + } + for (i = 0; i < n_sort_keys; i++) { + grn_accessor *key; + + adjusted_sort_keys[i] = sort_keys[i]; + /* TODO: Ugly. We should use accessor_new(). */ + key = GRN_CALLOC(sizeof(grn_accessor)); + key->header.type = GRN_ACCESSOR; + key->action = GRN_ACCESSOR_GET_KEY; + key->obj = window->grouped_table; + key->next = (grn_accessor *)(sort_keys[i].key); + adjusted_sort_keys[i].key = (grn_obj *)key; + } + grn_table_sort(ctx, + window->grouped_table, + 0, -1, + sorted, + adjusted_sort_keys, n_sort_keys); + for (i = 0; i < n_sort_keys; i++) { + GRN_FREE(adjusted_sort_keys[i].key); + } + GRN_FREE(adjusted_sort_keys); + } if (ctx->rc != GRN_SUCCESS) { ERR(ctx->rc, "[window][set][sort-keys] " Modified: test/command/suite/select/columns/window_function/window_sum/group_sort.expected (+3 -3) =================================================================== --- test/command/suite/select/columns/window_function/window_sum/group_sort.expected 2017-02-01 13:08:08 +0900 (2847c88) +++ test/command/suite/select/columns/window_function/window_sum/group_sort.expected 2017-02-01 15:23:08 +0900 (adb6ca0) @@ -6,12 +6,12 @@ column_create Logs price COLUMN_SCALAR UInt32 [[0,0.0,0.0],true] load --table Logs [ +{"item": "item2", "price": 333}, {"item": "item1", "price": 666}, -{"item": "item1", "price": 999}, +{"item": "item3", "price": 222}, {"item": "item1", "price": 777}, {"item": "item2", "price": 111}, -{"item": "item2", "price": 333}, -{"item": "item3", "price": 222} +{"item": "item1", "price": 999} ] [[0,0.0,0.0],6] select Logs --columns[sum].stage initial --columns[sum].value 'window_sum(price)' --columns[sum].type UInt32 --columns[sum].window.group_keys item --columns[sum].window.sort_keys price --output_columns 'item, price, sum' --sort_keys item,price Modified: test/command/suite/select/columns/window_function/window_sum/group_sort.test (+3 -3) =================================================================== --- test/command/suite/select/columns/window_function/window_sum/group_sort.test 2017-02-01 13:08:08 +0900 (3a6f148) +++ test/command/suite/select/columns/window_function/window_sum/group_sort.test 2017-02-01 15:23:08 +0900 (2af0962) @@ -4,12 +4,12 @@ column_create Logs price COLUMN_SCALAR UInt32 load --table Logs [ +{"item": "item2", "price": 333}, {"item": "item1", "price": 666}, -{"item": "item1", "price": 999}, +{"item": "item3", "price": 222}, {"item": "item1", "price": 777}, {"item": "item2", "price": 111}, -{"item": "item2", "price": 333}, -{"item": "item3", "price": 222} +{"item": "item1", "price": 999} ] select Logs \ -------------- next part -------------- HTML����������������������������...Télécharger