Kouhei Sutou
null+****@clear*****
Wed May 17 22:09:41 JST 2017
Kouhei Sutou 2017-05-17 22:09:41 +0900 (Wed, 17 May 2017) New Revision: 9ea0f5a5959d88ed1c643c1e7a389484e29c6187 https://github.com/ranguba/rroonga/commit/9ea0f5a5959d88ed1c643c1e7a389484e29c6187 Message: Add :columns option to Table#dump_arrow Added files: test/test-table-arrow.rb Modified files: ext/groonga/rb-grn-table.c Modified: ext/groonga/rb-grn-table.c (+38 -4) =================================================================== --- ext/groonga/rb-grn-table.c 2017-05-15 12:16:24 +0900 (c6349db) +++ ext/groonga/rb-grn-table.c 2017-05-17 22:09:41 +0900 (a28b78e) @@ -2692,22 +2692,36 @@ rb_grn_table_load_arrow (VALUE self, VALUE rb_path) } /* - * @overload dump_arrow(path) + * @overload dump_arrow(path, options) * * Dump records to file in Apache Arrow file format. * * @param path [String, #to_path] the output file path. * + * @param options [::Hash] the options. + * + * @option options :columns [::Array<Groonga::Column>] the columns + * to be dumped. All columns are dumped by default. + * * @return [void] * * @since 7.0.3 */ static VALUE -rb_grn_table_dump_arrow (VALUE self, VALUE rb_path) +rb_grn_table_dump_arrow (int argc, VALUE *argv, VALUE self) { int rc; grn_ctx *context; grn_obj *table; + const char *path; + VALUE rb_path; + VALUE rb_options; + VALUE rb_columns = Qnil; + + rb_scan_args(argc, argv, "11", &rb_path, &rb_options); + rb_grn_scan_options(rb_options, + "columns", &rb_columns, + NULL); rb_grn_table_deconstruct(SELF(self), &table, &context, NULL, NULL, NULL, @@ -2727,8 +2741,28 @@ rb_grn_table_dump_arrow (VALUE self, VALUE rb_path) rb_path = rb_grn_convert_to_string(rb_path_string); } } + path = StringValueCStr(rb_path); - rc = grn_arrow_dump(context, table, StringValueCStr(rb_path)); + if (NIL_P(rb_columns)) { + rc = grn_arrow_dump(context, table, path); + } else { + grn_obj columns; + int i, n; + + rb_columns = rb_grn_convert_to_array(rb_columns); + + GRN_PTR_INIT(&columns, GRN_OBJ_VECTOR, GRN_ID_NIL); + n = RARRAY_LEN(rb_columns); + for (i = 0; i < n; i++) { + VALUE rb_column = RARRAY_PTR(rb_columns)[i]; + grn_obj *column; + + column = RVAL2GRNOBJECT(rb_column, &context); + GRN_PTR_PUT(context, &columns, column); + } + rc = grn_arrow_dump_columns(context, table, &columns, path); + GRN_OBJ_FIN(context, &columns); + } rb_grn_context_check(context, self); rb_grn_rc_check(rc, self); @@ -2823,7 +2857,7 @@ rb_grn_init_table (VALUE mGrn) rb_define_method(rb_cGrnTable, "rename", rb_grn_table_rename, 1); rb_define_method(rb_cGrnTable, "load_arrow", rb_grn_table_load_arrow, 1); - rb_define_method(rb_cGrnTable, "dump_arrow", rb_grn_table_dump_arrow, 1); + rb_define_method(rb_cGrnTable, "dump_arrow", rb_grn_table_dump_arrow, -1); rb_grn_init_table_key_support(mGrn); rb_grn_init_array(mGrn); Added: test/test-table-arrow.rb (+159 -0) 100644 =================================================================== --- /dev/null +++ test/test-table-arrow.rb 2017-05-17 22:09:41 +0900 (b7fcef2) @@ -0,0 +1,159 @@ +# Copyright (C) 2007 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 +# License version 2.1 as published by the Free Software Foundation. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +class TableArrowTest < Test::Unit::TestCase + include GroongaTestUtils + + setup :setup_database + + def assert_dump_load(type, n_records) + Groonga::Schema.define do |schema| + schema.create_table("Source") do |table| + table.__send__(type, "data") + end + + schema.create_table("Destination") do |table| + end + end + + source = Groonga["Source"] + destination = Groonga["Destination"] + + expected = [] + n_records.times do |i| + data = yield(i) + expected << {"_id" => i + 1, "data" => data} + source.add(:data => data) + end + + tempfile = Tempfile.new(["table-arrow", ".arrow"]) + source.dump_arrow(tempfile.path) + destination.load_arrow(tempfile.path) + + assert_equal(expected, + destination.collect(&:attributes)) + end + + def test_uint8 + n_records = 128 + assert_dump_load(:uint8, n_records) do |i| + i + end + end + + def test_int8 + n_records = 128 + assert_dump_load(:int8, n_records) do |i| + data = i + data = -data if (i % 2).zero? + data + end + end + + def test_uint16 + n_records = 128 + assert_dump_load(:uint16, n_records) do |i| + i * 10 + end + end + + def test_int16 + n_records = 128 + assert_dump_load(:int16, n_records) do |i| + data = i * 10 + data = -data if (i % 2).zero? + data + end + end + + def test_uint32 + n_records = 128 + assert_dump_load(:uint32, n_records) do |i| + i * 100 + end + end + + def test_int32 + n_records = 128 + assert_dump_load(:int32, n_records) do |i| + data = i * 100 + data = -data if (i % 2).zero? + data + end + end + + def test_uint64 + n_records = 128 + assert_dump_load(:uint64, n_records) do |i| + i * 100 + end + end + + def test_int64 + n_records = 128 + assert_dump_load(:int64, n_records) do |i| + data = i * 100 + data = -data if (i % 2).zero? + data + end + end + + def test_float + n_records = 128 + assert_dump_load(:float, n_records) do |i| + data = i * 1.1 + data = -data if (i % 2).zero? + data + end + end + + def test_time + n_records = 128 + now = Time.at(Time.now.to_i) + assert_dump_load(:time, n_records) do |i| + now + i + end + end + + def test_dump_columns + Groonga::Schema.define do |schema| + schema.create_table("Source") do |table| + table.int32("data1") + table.short_text("data2") + end + + schema.create_table("Destination") do |table| + end + end + + source = Groonga["Source"] + destination = Groonga["Destination"] + + expected = [] + 10.times do |i| + data1 = i * 10 + data2 = i.to_s + expected << {"_id" => i + 1, "data1" => data1} + source.add(:data1 => data1, :data2 => data2) + end + + tempfile = Tempfile.new(["table-arrow", ".arrow"]) + source.dump_arrow(tempfile.path, :columns => source.columns[0, 1]) + destination.load_arrow(tempfile.path) + + assert_equal(expected, + destination.collect(&:attributes)) + end +end -------------- next part -------------- HTML����������������������������... ダウンロード