You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by uw...@apache.org on 2018/11/09 12:07:08 UTC
[arrow] branch master updated: ARROW-3733: [GLib] Add to_string()
to GArrowTable and GArrowColumn
This is an automated email from the ASF dual-hosted git repository.
uwe pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/master by this push:
new e5e4490 ARROW-3733: [GLib] Add to_string() to GArrowTable and GArrowColumn
e5e4490 is described below
commit e5e44903748bda9ab0228fca6ac43999f3dd66e6
Author: Kouhei Sutou <ko...@clear-code.com>
AuthorDate: Fri Nov 9 13:06:58 2018 +0100
ARROW-3733: [GLib] Add to_string() to GArrowTable and GArrowColumn
This changes have many test code change. It's caused by changing table check way from dumped text comparison to direct table comparison. If we use direct table comparison, dump text format change doesn't effect test code. We'll get more stable test suite by this change.
Author: Kouhei Sutou <ko...@clear-code.com>
Closes #2925 from kou/glib-table-to-string and squashes the following commits:
ffdf3c53 <Kouhei Sutou> Add to_string() to GArrowTable and GArrowColumn
---
c_glib/arrow-glib/basic-array.cpp | 1 -
c_glib/arrow-glib/column.cpp | 28 ++
c_glib/arrow-glib/column.h | 45 +--
c_glib/arrow-glib/field.cpp | 5 +-
c_glib/arrow-glib/table.cpp | 27 ++
c_glib/arrow-glib/table.h | 2 +
c_glib/test/helper/buildable.rb | 44 +--
c_glib/test/parquet/test-arrow-file-writer.rb | 26 +-
c_glib/test/test-orc-file-reader.rb | 430 +++++++++-----------------
c_glib/test/test-table.rb | 16 +
10 files changed, 271 insertions(+), 353 deletions(-)
diff --git a/c_glib/arrow-glib/basic-array.cpp b/c_glib/arrow-glib/basic-array.cpp
index 671882b..1fc83b5 100644
--- a/c_glib/arrow-glib/basic-array.cpp
+++ b/c_glib/arrow-glib/basic-array.cpp
@@ -29,7 +29,6 @@
#include <arrow-glib/type.hpp>
#include <arrow-glib/decimal.hpp>
-#include <iostream>
#include <sstream>
template <typename T>
diff --git a/c_glib/arrow-glib/column.cpp b/c_glib/arrow-glib/column.cpp
index 22c7ab8..06ab0b7 100644
--- a/c_glib/arrow-glib/column.cpp
+++ b/c_glib/arrow-glib/column.cpp
@@ -25,8 +25,11 @@
#include <arrow-glib/chunked-array.hpp>
#include <arrow-glib/column.hpp>
#include <arrow-glib/data-type.hpp>
+#include <arrow-glib/error.hpp>
#include <arrow-glib/field.hpp>
+#include <sstream>
+
G_BEGIN_DECLS
/**
@@ -364,6 +367,31 @@ garrow_column_get_data(GArrowColumn *column)
return garrow_chunked_array_new_raw(&arrow_chunked_array);
}
+/**
+ * garrow_column_to_string:
+ * @column: A #GArrowColumn.
+ * @error: (nullable): Return location for a #GError or %NULL.
+ *
+ * Returns: (nullable): The formatted column content or %NULL on error.
+ *
+ * The returned string should be freed when with g_free() when no
+ * longer needed.
+ *
+ * Since: 0.12.0
+ */
+gchar *
+garrow_column_to_string(GArrowColumn *column, GError **error)
+{
+ const auto arrow_column = garrow_column_get_raw(column);
+ std::stringstream sink;
+ auto status = arrow::PrettyPrint(*arrow_column, 0, &sink);
+ if (garrow_error_check(error, status, "[column][to-string]")) {
+ return g_strdup(sink.str().c_str());
+ } else {
+ return NULL;
+ }
+}
+
G_END_DECLS
GArrowColumn *
diff --git a/c_glib/arrow-glib/column.h b/c_glib/arrow-glib/column.h
index f2c181d..2745958 100644
--- a/c_glib/arrow-glib/column.h
+++ b/c_glib/arrow-glib/column.h
@@ -25,48 +25,17 @@
G_BEGIN_DECLS
-#define GARROW_TYPE_COLUMN \
- (garrow_column_get_type())
-#define GARROW_COLUMN(obj) \
- (G_TYPE_CHECK_INSTANCE_CAST((obj), \
- GARROW_TYPE_COLUMN, \
- GArrowColumn))
-#define GARROW_COLUMN_CLASS(klass) \
- (G_TYPE_CHECK_CLASS_CAST((klass), \
- GARROW_TYPE_COLUMN, \
- GArrowColumnClass))
-#define GARROW_IS_COLUMN(obj) \
- (G_TYPE_CHECK_INSTANCE_TYPE((obj), \
- GARROW_TYPE_COLUMN))
-#define GARROW_IS_COLUMN_CLASS(klass) \
- (G_TYPE_CHECK_CLASS_TYPE((klass), \
- GARROW_TYPE_COLUMN))
-#define GARROW_COLUMN_GET_CLASS(obj) \
- (G_TYPE_INSTANCE_GET_CLASS((obj), \
- GARROW_TYPE_COLUMN, \
- GArrowColumnClass))
-
-typedef struct _GArrowColumn GArrowColumn;
-typedef struct _GArrowColumnClass GArrowColumnClass;
-
-/**
- * GArrowColumn:
- *
- * It wraps `arrow::Column`.
- */
-struct _GArrowColumn
-{
- /*< private >*/
- GObject parent_instance;
-};
-
+#define GARROW_TYPE_COLUMN (garrow_column_get_type())
+G_DECLARE_DERIVABLE_TYPE(GArrowColumn,
+ garrow_column,
+ GARROW,
+ COLUMN,
+ GObject)
struct _GArrowColumnClass
{
GObjectClass parent_class;
};
-GType garrow_column_get_type (void) G_GNUC_CONST;
-
GArrowColumn *garrow_column_new_array(GArrowField *field,
GArrowArray *array);
GArrowColumn *garrow_column_new_chunked_array(GArrowField *field,
@@ -84,5 +53,7 @@ GArrowField *garrow_column_get_field (GArrowColumn *column);
const gchar *garrow_column_get_name (GArrowColumn *column);
GArrowDataType *garrow_column_get_data_type (GArrowColumn *column);
GArrowChunkedArray *garrow_column_get_data (GArrowColumn *column);
+gchar *garrow_column_to_string (GArrowColumn *column,
+ GError **error);
G_END_DECLS
diff --git a/c_glib/arrow-glib/field.cpp b/c_glib/arrow-glib/field.cpp
index 09c7ca3..8fd9dd6 100644
--- a/c_glib/arrow-glib/field.cpp
+++ b/c_glib/arrow-glib/field.cpp
@@ -135,9 +135,8 @@ GArrowField *
garrow_field_new(const gchar *name,
GArrowDataType *data_type)
{
- auto arrow_field =
- std::make_shared<arrow::Field>(name,
- garrow_data_type_get_raw(data_type));
+ auto arrow_data_type = garrow_data_type_get_raw(data_type);
+ auto arrow_field = std::make_shared<arrow::Field>(name, arrow_data_type);
return garrow_field_new_raw(&arrow_field);
}
diff --git a/c_glib/arrow-glib/table.cpp b/c_glib/arrow-glib/table.cpp
index b9946a3..5e72c87 100644
--- a/c_glib/arrow-glib/table.cpp
+++ b/c_glib/arrow-glib/table.cpp
@@ -26,6 +26,8 @@
#include <arrow-glib/schema.hpp>
#include <arrow-glib/table.hpp>
+#include <sstream>
+
G_BEGIN_DECLS
/**
@@ -306,6 +308,31 @@ garrow_table_replace_column(GArrowTable *table,
}
}
+/**
+ * garrow_table_to_string:
+ * @table: A #GArrowTable.
+ * @error: (nullable): Return location for a #GError or %NULL.
+ *
+ * Returns: (nullable): The formatted table content or %NULL on error.
+ *
+ * The returned string should be freed when with g_free() when no
+ * longer needed.
+ *
+ * Since: 0.12.0
+ */
+gchar *
+garrow_table_to_string(GArrowTable *table, GError **error)
+{
+ const auto arrow_table = garrow_table_get_raw(table);
+ std::stringstream sink;
+ auto status = arrow::PrettyPrint(*arrow_table, 0, &sink);
+ if (garrow_error_check(error, status, "[table][to-string]")) {
+ return g_strdup(sink.str().c_str());
+ } else {
+ return NULL;
+ }
+}
+
G_END_DECLS
GArrowTable *
diff --git a/c_glib/arrow-glib/table.h b/c_glib/arrow-glib/table.h
index 19843f3..ef7b0f5 100644
--- a/c_glib/arrow-glib/table.h
+++ b/c_glib/arrow-glib/table.h
@@ -58,5 +58,7 @@ GArrowTable *garrow_table_replace_column(GArrowTable *table,
guint i,
GArrowColumn *column,
GError **error);
+gchar *garrow_table_to_string (GArrowTable *table,
+ GError **error);
G_END_DECLS
diff --git a/c_glib/test/helper/buildable.rb b/c_glib/test/helper/buildable.rb
index 0f56fec..d6d1ff8 100644
--- a/c_glib/test/helper/buildable.rb
+++ b/c_glib/test/helper/buildable.rb
@@ -105,19 +105,11 @@ module Helper
value_field = Arrow::Field.new("value", value_data_type)
data_type = Arrow::ListDataType.new(value_field)
builder = Arrow::ListArrayBuilder.new(data_type)
- value_builder = builder.value_builder
values_list.each do |values|
if values.nil?
builder.append_null
else
- builder.append
- values.each do |value|
- if value.nil?
- value_builder.append_null
- else
- value_builder.append(value)
- end
- end
+ append_to_builder(builder, values)
end
end
builder.finish
@@ -130,19 +122,35 @@ module Helper
if struct.nil?
builder.append_null
else
+ append_to_builder(builder, struct)
+ end
+ end
+ builder.finish
+ end
+
+ def append_to_builder(builder, value)
+ if value.nil?
+ builder.append_null
+ else
+ data_type = builder.value_data_type
+ case data_type
+ when Arrow::ListDataType
+ builder.append
+ value_builder = builder.value_builder
+ value.each do |v|
+ append_to_builder(value_builder, v)
+ end
+ when Arrow::StructDataType
builder.append
- struct.each do |name, value|
- field_builder_index = fields.index {|field| field.name == name}
- field_builder = builder.get_field_builder(field_builder_index)
- if value.nil?
- field_builder.append_null
- else
- field_builder.append(value)
- end
+ value.each do |name, v|
+ field_index = data_type.get_field_index(name)
+ field_builder = builder.get_field_builder(field_index)
+ append_to_builder(field_builder, v)
end
+ else
+ builder.append(value)
end
end
- builder.finish
end
def build_table(arrays)
diff --git a/c_glib/test/parquet/test-arrow-file-writer.rb b/c_glib/test/parquet/test-arrow-file-writer.rb
index d6c775e..b5a8fd1 100644
--- a/c_glib/test/parquet/test-arrow-file-writer.rb
+++ b/c_glib/test/parquet/test-arrow-file-writer.rb
@@ -34,23 +34,13 @@ class TestParquetArrowFileWriter < Test::Unit::TestCase
reader = Parquet::ArrowFileReader.new(@file.path)
reader.use_threads = true
- assert_equal(enabled_values.length / chunk_size, reader.n_row_groups)
- table = reader.read_table
- table = reader.read_table
- table_data = table.n_columns.times.collect do |i|
- column = table.get_column(i)
- data = []
- column.data.chunks.each do |chunk|
- chunk.length.times do |j|
- if chunk.null?(j)
- data << nil
- else
- data << chunk.get_value(j)
- end
- end
- end
- [column.name, data]
- end
- assert_equal([["enabled", enabled_values]], table_data)
+ assert_equal([
+ enabled_values.length / chunk_size,
+ table,
+ ],
+ [
+ reader.n_row_groups,
+ reader.read_table,
+ ])
end
end
diff --git a/c_glib/test/test-orc-file-reader.rb b/c_glib/test/test-orc-file-reader.rb
index 7316b48..544112c 100644
--- a/c_glib/test/test-orc-file-reader.rb
+++ b/c_glib/test/test-orc-file-reader.rb
@@ -16,6 +16,7 @@
# under the License.
class TestORCFileReader < Test::Unit::TestCase
+ include Helper::Buildable
include Helper::Omittable
include Helper::Fixture
@@ -51,303 +52,180 @@ map: list<item: struct<key: string, value: struct<int1: int32, string1: string>>
assert_equal([1, 3], @reader.field_indices)
end
- sub_test_case("#read_stripes") do
- test("all") do
- table = @reader.read_stripes
- dump = table.n_columns.times.collect do |i|
- column = table.get_column(i)
- [
- column.field.to_s,
- column.data.chunks.collect(&:to_s),
- ]
- end
- expected = [
- ["boolean1: bool", ["[\n false,\n true\n]"]],
- ["byte1: int8", ["[\n 1,\n 100\n]"]],
- ["short1: int16", ["[\n 1024,\n 2048\n]"]],
- ["int1: int32", ["[\n 65536,\n 65536\n]"]],
- [
- "long1: int64",
- ["[\n 9223372036854775807,\n 9223372036854775807\n]"],
- ],
- ["float1: float", ["[\n 1,\n 2\n]"]],
- ["double1: double", ["[\n -15,\n -5\n]"]],
- ["bytes1: binary", ["[\n 0001020304,\n \n]"]],
- ["string1: string", ["[\n \"hi\",\n \"bye\"\n]"]],
- [
- "middle: " +
- "struct<list: " +
- "list<item: struct<int1: int32, string1: string>>>",
- [
- <<-STRUCT.chomp
--- is_valid: all not null
--- child 0 type: list<item: struct<int1: int32, string1: string>>
- [
- -- is_valid: all not null
- -- child 0 type: int32
- [
- 1,
- 2
- ]
- -- child 1 type: string
- [
- "bye",
- "sigh"
- ],
- -- is_valid: all not null
- -- child 0 type: int32
- [
- 1,
- 2
- ]
- -- child 1 type: string
- [
- "bye",
- "sigh"
- ]
- ]
- STRUCT
- ]
- ],
- [
- "list: list<item: struct<int1: int32, string1: string>>",
- [
- <<-LIST.chomp
-[
- -- is_valid: all not null
- -- child 0 type: int32
- [
- 3,
- 4
- ]
- -- child 1 type: string
- [
- "good",
- "bad"
- ],
- -- is_valid: all not null
- -- child 0 type: int32
+ def item_fields
[
- 100000000,
- -100000,
- 1234
+ Arrow::Field.new("int1", Arrow::Int32DataType.new),
+ Arrow::Field.new("string1", Arrow::StringDataType.new),
]
- -- child 1 type: string
+ end
+
+ def item_data_type
+ Arrow::StructDataType.new(item_fields)
+ end
+
+ def build_items_array(items_array)
+ build_list_array(item_data_type, items_array)
+ end
+
+ def items_data_type
+ Arrow::ListDataType.new(Arrow::Field.new("item", item_data_type))
+ end
+
+ def middle_fields
[
- "cat",
- "in",
- "hat"
+ Arrow::Field.new("list", items_data_type),
]
-]
- LIST
- ]
- ],
- [
- "map: list<item: " +
- "struct<key: string, value: " +
- "struct<int1: int32, string1: string>>>",
- [
- <<-MAP.chomp
-[
- -- is_valid: all not null
- -- child 0 type: string
- []
- -- child 1 type: struct<int1: int32, string1: string>
- -- is_valid: all not null
- -- child 0 type: int32
- []
- -- child 1 type: string
- [],
- -- is_valid: all not null
- -- child 0 type: string
+ end
+
+ def build_middle_array(middles)
+ build_struct_array(middle_fields, middles)
+ end
+
+ def key_value_fields
[
- "chani",
- "mauddib"
+ Arrow::Field.new("key", Arrow::StringDataType.new),
+ Arrow::Field.new("value", item_data_type),
]
- -- child 1 type: struct<int1: int32, string1: string>
- -- is_valid: all not null
- -- child 0 type: int32
- [
- 5,
- 1
- ]
- -- child 1 type: string
- [
- "chani",
- "mauddib"
- ]
-]
- MAP
- ],
- ],
- ]
- expected.zip(dump).each do |ex, actual|
- assert_equal(ex, actual)
- end
+ end
+
+ def key_value_data_type
+ Arrow::StructDataType.new(key_value_fields)
+ end
+
+ def build_key_value_array(key_value_array)
+ build_list_array(key_value_data_type, key_value_array)
+ end
+
+ def middle_array
+ build_middle_array([
+ {
+ "list" => [
+ {
+ "int1" => 1,
+ "string1" => "bye",
+ },
+ {
+ "int1" => 2,
+ "string1" => "sigh",
+ },
+ ],
+ },
+ {
+ "list" => [
+ {
+ "int1" => 1,
+ "string1" => "bye",
+ },
+ {
+ "int1" => 2,
+ "string1" => "sigh",
+ },
+ ],
+ },
+ ])
+ end
+
+ def list_array
+ build_items_array([
+ [
+ {
+ "int1" => 3,
+ "string1" => "good",
+ },
+ {
+ "int1" => 4,
+ "string1" => "bad",
+ },
+ ],
+ [
+ {
+ "int1" => 100000000,
+ "string1" => "cat",
+ },
+ {
+ "int1" => -100000,
+ "string1" => "in",
+ },
+ {
+ "int1" => 1234,
+ "string1" => "hat",
+ },
+ ]
+ ])
+ end
+
+ def map_array
+ build_key_value_array([
+ [
+ ],
+ [
+ {
+ "key" => "chani",
+ "value" => {
+ "int1" => 5,
+ "string1" => "chani",
+ },
+ },
+ {
+ "key" => "mauddib",
+ "value" => {
+ "int1" => 1,
+ "string1" => "mauddib",
+ },
+ },
+ ],
+ ])
+ end
+
+ def all_columns
+ {
+ "boolean1" => build_boolean_array([false, true]),
+ "byte1" => build_int8_array([1, 100]),
+ "short1" => build_int16_array([1024, 2048]),
+ "int1" => build_int32_array([65536, 65536]),
+ "long1" => build_int64_array([
+ 9223372036854775807,
+ 9223372036854775807,
+ ]),
+ "float1" => build_float_array([1.0, 2.0]),
+ "double1" => build_double_array([-15.0, -5.0]),
+ "bytes1" => build_binary_array(["\x00\x01\x02\x03\x04", ""]),
+ "string1" => build_string_array(["hi", "bye"]),
+ "middle" => middle_array,
+ "list" => list_array,
+ "map" => map_array,
+ }
+ end
+
+ sub_test_case("#read_stripes") do
+ test("all") do
+ assert_equal(build_table(all_columns),
+ @reader.read_stripes)
end
test("select fields") do
require_gi_bindings(3, 2, 6)
@reader.field_indices = [1, 3]
- table = @reader.read_stripes
- dump = table.n_columns.times.collect do |i|
- column = table.get_column(i)
- [
- column.field.to_s,
- column.data.chunks.collect(&:to_s),
- ]
- end
- assert_equal([
- ["boolean1: bool", ["[\n false,\n true\n]"]],
- ["short1: int16", ["[\n 1024,\n 2048\n]"]],
- ],
- dump)
+ assert_equal(build_table("boolean1" => build_boolean_array([false, true]),
+ "short1" => build_int16_array([1024, 2048])),
+ @reader.read_stripes)
end
end
sub_test_case("#read_stripe") do
test("all") do
- record_batch = @reader.read_stripe(0)
- dump = record_batch.n_columns.times.collect do |i|
- [
- record_batch.schema.get_field(i).to_s,
- record_batch.get_column(i).to_s,
- ]
- end
- expected = [
- ["boolean1: bool", "[\n false,\n true\n]"],
- ["byte1: int8", "[\n 1,\n 100\n]"],
- ["short1: int16", "[\n 1024,\n 2048\n]"],
- ["int1: int32", "[\n 65536,\n 65536\n]"],
- [
- "long1: int64",
- "[\n 9223372036854775807,\n 9223372036854775807\n]",
- ],
- ["float1: float", "[\n 1,\n 2\n]"],
- ["double1: double", "[\n -15,\n -5\n]"],
- ["bytes1: binary", "[\n 0001020304,\n \n]"],
- ["string1: string", "[\n \"hi\",\n \"bye\"\n]"],
- [
- "middle: " +
- "struct<list: " +
- "list<item: struct<int1: int32, string1: string>>>",
- <<-STRUCT.chomp
--- is_valid: all not null
--- child 0 type: list<item: struct<int1: int32, string1: string>>
- [
- -- is_valid: all not null
- -- child 0 type: int32
- [
- 1,
- 2
- ]
- -- child 1 type: string
- [
- "bye",
- "sigh"
- ],
- -- is_valid: all not null
- -- child 0 type: int32
- [
- 1,
- 2
- ]
- -- child 1 type: string
- [
- "bye",
- "sigh"
- ]
- ]
- STRUCT
- ],
- [
- "list: list<item: struct<int1: int32, string1: string>>",
- <<-LIST.chomp
-[
- -- is_valid: all not null
- -- child 0 type: int32
- [
- 3,
- 4
- ]
- -- child 1 type: string
- [
- "good",
- "bad"
- ],
- -- is_valid: all not null
- -- child 0 type: int32
- [
- 100000000,
- -100000,
- 1234
- ]
- -- child 1 type: string
- [
- "cat",
- "in",
- "hat"
- ]
-]
- LIST
- ],
- [
- "map: list<item: " +
- "struct<key: string, value: " +
- "struct<int1: int32, string1: string>>>",
- <<-MAP.chomp
-[
- -- is_valid: all not null
- -- child 0 type: string
- []
- -- child 1 type: struct<int1: int32, string1: string>
- -- is_valid: all not null
- -- child 0 type: int32
- []
- -- child 1 type: string
- [],
- -- is_valid: all not null
- -- child 0 type: string
- [
- "chani",
- "mauddib"
- ]
- -- child 1 type: struct<int1: int32, string1: string>
- -- is_valid: all not null
- -- child 0 type: int32
- [
- 5,
- 1
- ]
- -- child 1 type: string
- [
- "chani",
- "mauddib"
- ]
-]
- MAP
- ],
- ]
- expected.zip(dump).each do |ex, actual|
- assert_equal(ex, actual)
- end
+ assert_equal(build_record_batch(all_columns),
+ @reader.read_stripe(0))
end
test("select fields") do
require_gi_bindings(3, 2, 6)
@reader.field_indices = [1, 3]
- record_batch = @reader.read_stripe(0)
- dump = record_batch.n_columns.times.collect do |i|
- [
- record_batch.schema.get_field(i).to_s,
- record_batch.get_column(i).to_s,
- ]
- end
- assert_equal([
- ["boolean1: bool", "[\n false,\n true\n]"],
- ["short1: int16", "[\n 1024,\n 2048\n]"],
- ],
- dump)
+ boolean1 = build_boolean_array([false, true])
+ short1 = build_int16_array([1024, 2048])
+ assert_equal(build_record_batch("boolean1" => boolean1,
+ "short1" => short1),
+ @reader.read_stripe(0))
end
end
diff --git a/c_glib/test/test-table.rb b/c_glib/test/test-table.rb
index 70ee653..4394ad1 100644
--- a/c_glib/test/test-table.rb
+++ b/c_glib/test/test-table.rb
@@ -118,5 +118,21 @@ class TestTable < Test::Unit::TestCase
assert_equal(["added", "valid"],
new_table.schema.fields.collect(&:name))
end
+
+ def test_to_s
+ table = build_table("valid" => build_boolean_array([true, false, true]))
+ assert_equal(<<-TABLE, table.to_s)
+valid: bool
+----
+valid:
+ [
+ [
+ true,
+ false,
+ true
+ ]
+ ]
+ TABLE
+ end
end
end