Skip to content

Commit

Permalink
Implement Groonga::Column#apply_window_function
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Jun 14, 2016
1 parent 5186cba commit 5ac24bb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
12 changes: 11 additions & 1 deletion ext/groonga/rb-grn-column.c
Expand Up @@ -829,14 +829,19 @@ rb_grn_column_apply_window_function (int argc, VALUE *argv, VALUE self)
grn_ctx *context;
grn_obj *column;
grn_obj *table;
VALUE rb_table;
grn_window_definition definition;
grn_obj *window_function_call = NULL;
VALUE rb_options;
VALUE rb_sort_keys;
VALUE rb_builder;
VALUE rb_window_function_call;

rb_grn_column_deconstruct(SELF(self), &column, &context,
NULL, &table,
NULL, NULL, NULL);
rb_table = GRNOBJECT2RVAL(Qnil, context, table, GRN_FALSE);

memset(&definition, 0, sizeof(grn_window_definition));

rb_scan_args(argc, argv, "01", &rb_options);
Expand All @@ -862,7 +867,12 @@ rb_grn_column_apply_window_function (int argc, VALUE *argv, VALUE self)
rb_table);
}

/* TODO: set window_function_call */
rb_builder = rb_grn_record_expression_builder_new(rb_table, Qnil);
rb_window_function_call =
rb_grn_record_expression_builder_build(rb_builder);
rb_grn_object_deconstruct(RB_GRN_OBJECT(DATA_PTR(rb_window_function_call)),
&window_function_call, NULL,
NULL, NULL, NULL, NULL);

rc = grn_table_apply_window_function(context,
table,
Expand Down
29 changes: 29 additions & 0 deletions test/test-column.rb
Expand Up @@ -349,6 +349,35 @@ def test_table?
end
end

def test_apply_window_function
Groonga::Schema.define do |schema|
schema.create_table("Comments") do |table|
table.uint32("nth")
end
end
comments = Groonga["Comments"]
nth = Groonga["Comments.nth"]

5.times do
comments.add
end

options = {
:sort_keys => [["_id", "desc"]],
}
nth.apply_window_function(options) do |record|
record.call("record_number")
end
assert_equal([
[1, 5],
[2, 4],
[3, 3],
[4, 2],
[5, 1],
],
comments.collect {|comment| [comment.id, comment.nth]})
end

private
def assert_content_search(expected_records, term)
records = @bookmarks_index_content.search(term).records
Expand Down

0 comments on commit 5ac24bb

Please sign in to comment.