Kouhei Sutou
null+****@clear*****
Tue Apr 25 15:40:50 JST 2017
Kouhei Sutou 2017-04-25 15:40:50 +0900 (Tue, 25 Apr 2017) New Revision: 4ea68c5991868b9012825ef93748374fe52b4753 https://github.com/ranguba/rroonga/commit/4ea68c5991868b9012825ef93748374fe52b4753 Message: Column#apply_window_function supports :group_keys Modified files: ext/groonga/rb-grn-data-column.c test/test-data-column.rb Modified: ext/groonga/rb-grn-data-column.c (+20 -0) =================================================================== --- ext/groonga/rb-grn-data-column.c 2017-04-25 15:25:35 +0900 (609b316) +++ ext/groonga/rb-grn-data-column.c 2017-04-25 15:40:50 +0900 (4b13911) @@ -93,6 +93,7 @@ rb_grn_data_column_apply_window_function (int argc, VALUE *argv, VALUE self) grn_obj *window_function_call = NULL; VALUE rb_options; VALUE rb_sort_keys; + VALUE rb_group_keys; VALUE rb_builder; VALUE rb_window_function_call; @@ -106,6 +107,7 @@ rb_grn_data_column_apply_window_function (int argc, VALUE *argv, VALUE self) rb_scan_args(argc, argv, "01", &rb_options); rb_grn_scan_options(rb_options, "sort_keys", &rb_sort_keys, + "group_keys", &rb_group_keys, NULL); if (!NIL_P(rb_sort_keys)) { @@ -126,6 +128,24 @@ rb_grn_data_column_apply_window_function (int argc, VALUE *argv, VALUE self) rb_table); } + if (!NIL_P(rb_group_keys)) { + VALUE rb_table; + + if (!RVAL2CBOOL(rb_obj_is_kind_of(rb_group_keys, rb_cArray))) + rb_raise(rb_eArgError, ":group_keys should be an array of key: <%s>", + rb_grn_inspect(rb_group_keys)); + + definition.n_group_keys = RARRAY_LEN(rb_group_keys); + definition.group_keys = ALLOCA_N(grn_table_sort_key, + definition.n_group_keys); + rb_table = GRNOBJECT2RVAL(Qnil, context, table, GRN_FALSE); + rb_grn_table_sort_keys_fill(context, + definition.group_keys, + definition.n_group_keys, + rb_group_keys, + rb_table); + } + rb_builder = rb_grn_record_expression_builder_new(rb_table, Qnil); rb_window_function_call = rb_grn_record_expression_builder_build(rb_builder); Modified: test/test-data-column.rb (+43 -1) =================================================================== --- test/test-data-column.rb 2017-04-25 15:25:35 +0900 (105a2ea) +++ test/test-data-column.rb 2017-04-25 15:40:50 +0900 (392b6e5) @@ -1,4 +1,4 @@ -# Copyright (C) 2016 Kouhei Sutou <kou �� clear-code.com> +# Copyright (C) 2016-2017 Kouhei Sutou <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 @@ -49,5 +49,47 @@ class DataColumnTest < Test::Unit::TestCase ], comments.collect {|comment| [comment.id, comment.nth]}) end + + def test_group_keys + Groonga::Schema.define do |schema| + schema.create_table("Comments") do |table| + table.uint32("nth") + table.short_text("category") + end + end + comments = Groonga["Comments"] + nth = Groonga["Comments.nth"] + + 3.times do + comments.add(:category => "a") + end + 3.times do + comments.add(:category => "b") + end + + options = { + :sort_keys => [["_id", "desc"]], + :group_keys => ["category"], + } + nth.apply_window_function(options) do |record| + record.call("record_number") + end + values = comments.collect do |comment| + [ + comment.id, + comment.nth, + comment.category, + ] + end + assert_equal([ + [1, 3, "a"], + [2, 2, "a"], + [3, 1, "a"], + [4, 3, "b"], + [5, 2, "b"], + [6, 1, "b"], + ], + values) + end end end -------------- next part -------------- HTML����������������������������...Download