You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by ko...@apache.org on 2018/11/09 01:34:47 UTC
[arrow] branch master updated: ARROW-3725: [GLib] Add field readers
to GArrowStructDataType
This is an automated email from the ASF dual-hosted git repository.
kou 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 a1c00ac ARROW-3725: [GLib] Add field readers to GArrowStructDataType
a1c00ac is described below
commit a1c00accf2432a1ebdbdddb1909129ab4e79d6bf
Author: Kouhei Sutou <ko...@clear-code.com>
AuthorDate: Fri Nov 9 10:34:31 2018 +0900
ARROW-3725: [GLib] Add field readers to GArrowStructDataType
Author: Kouhei Sutou <ko...@clear-code.com>
Closes #2923 from kou/glib-struct-data-type-field-readers and squashes the following commits:
353869a9 <Kouhei Sutou> Fix indent
5cd53a38 <Kouhei Sutou> Add field readers to GArrowStructDataType
---
c_glib/arrow-glib/composite-data-type.cpp | 115 ++++++++++++++++++++++++++++++
c_glib/arrow-glib/composite-data-type.h | 56 +++++----------
c_glib/test/test-struct-data-type.rb | 80 +++++++++++++++++++++
3 files changed, 214 insertions(+), 37 deletions(-)
diff --git a/c_glib/arrow-glib/composite-data-type.cpp b/c_glib/arrow-glib/composite-data-type.cpp
index 5f742e5..36a839b 100644
--- a/c_glib/arrow-glib/composite-data-type.cpp
+++ b/c_glib/arrow-glib/composite-data-type.cpp
@@ -136,6 +136,121 @@ garrow_struct_data_type_new(GList *fields)
return data_type;
}
+/**
+ * garrow_struct_data_type_get_n_fields:
+ * @data_type: A #GArrowStructDataType.
+ *
+ * Returns: The number of fields of the struct data type.
+ *
+ * Since: 0.12.0
+ */
+gint
+garrow_struct_data_type_get_n_fields(GArrowStructDataType *data_type)
+{
+ auto arrow_data_type = garrow_data_type_get_raw(GARROW_DATA_TYPE(data_type));
+ return arrow_data_type->num_children();
+}
+
+/**
+ * garrow_struct_data_type_get_fields:
+ * @data_type: A #GArrowStructDataType.
+ *
+ * Returns: (transfer full) (element-type GArrowField):
+ * The fields of the struct data type.
+ *
+ * Since: 0.12.0
+ */
+GList *
+garrow_struct_data_type_get_fields(GArrowStructDataType *data_type)
+{
+ auto arrow_data_type = garrow_data_type_get_raw(GARROW_DATA_TYPE(data_type));
+ auto arrow_fields = arrow_data_type->children();
+
+ GList *fields = NULL;
+ for (auto arrow_field : arrow_fields) {
+ fields = g_list_prepend(fields, garrow_field_new_raw(&arrow_field));
+ }
+ return g_list_reverse(fields);
+}
+
+/**
+ * garrow_struct_data_type_get_field:
+ * @data_type: A #GArrowStructDataType.
+ * @i: The index of the target field.
+ *
+ * Returns: (transfer full) (nullable):
+ * The field at the index in the struct data type or %NULL on not found.
+ *
+ * Since: 0.12.0
+ */
+GArrowField *
+garrow_struct_data_type_get_field(GArrowStructDataType *data_type,
+ gint i)
+{
+ auto arrow_data_type = garrow_data_type_get_raw(GARROW_DATA_TYPE(data_type));
+
+ while (i < 0) {
+ i += arrow_data_type->num_children();
+ }
+ if (i >= arrow_data_type->num_children()) {
+ return NULL;
+ }
+
+ auto arrow_field = arrow_data_type->child(i);
+ if (arrow_field) {
+ return garrow_field_new_raw(&arrow_field);
+ } else {
+ return NULL;
+ }
+}
+
+/**
+ * garrow_struct_data_type_get_field_by_name:
+ * @data_type: A #GArrowStructDataType.
+ * @name: The name of the target field.
+ *
+ * Returns: (transfer full) (nullable):
+ * The field that has the name in the struct data type or %NULL on not found.
+ *
+ * Since: 0.12.0
+ */
+GArrowField *
+garrow_struct_data_type_get_field_by_name(GArrowStructDataType *data_type,
+ const gchar *name)
+{
+ auto arrow_data_type = garrow_data_type_get_raw(GARROW_DATA_TYPE(data_type));
+ auto arrow_struct_data_type =
+ std::static_pointer_cast<arrow::StructType>(arrow_data_type);
+
+ auto arrow_field = arrow_struct_data_type->GetChildByName(name);
+ if (arrow_field) {
+ return garrow_field_new_raw(&arrow_field);
+ } else {
+ return NULL;
+ }
+}
+
+/**
+ * garrow_struct_data_type_get_field_index:
+ * @data_type: A #GArrowStructDataType.
+ * @name: The name of the target field.
+ *
+ * Returns: The index of the target index in the struct data type
+ * or `-1` on not found.
+ *
+ * Since: 0.12.0
+ */
+gint
+garrow_struct_data_type_get_field_index(GArrowStructDataType *data_type,
+ const gchar *name)
+{
+ auto arrow_data_type = garrow_data_type_get_raw(GARROW_DATA_TYPE(data_type));
+ auto arrow_struct_data_type =
+ std::static_pointer_cast<arrow::StructType>(arrow_data_type);
+
+ return arrow_struct_data_type->GetChildIndex(name);
+}
+
G_DEFINE_TYPE(GArrowDictionaryDataType, \
garrow_dictionary_data_type, \
diff --git a/c_glib/arrow-glib/composite-data-type.h b/c_glib/arrow-glib/composite-data-type.h
index 11b8301..7d6a02b 100644
--- a/c_glib/arrow-glib/composite-data-type.h
+++ b/c_glib/arrow-glib/composite-data-type.h
@@ -70,49 +70,31 @@ GArrowListDataType *garrow_list_data_type_new (GArrowField *field);
GArrowField *garrow_list_data_type_get_value_field (GArrowListDataType *list_data_type);
-#define GARROW_TYPE_STRUCT_DATA_TYPE \
- (garrow_struct_data_type_get_type())
-#define GARROW_STRUCT_DATA_TYPE(obj) \
- (G_TYPE_CHECK_INSTANCE_CAST((obj), \
- GARROW_TYPE_STRUCT_DATA_TYPE, \
- GArrowStructDataType))
-#define GARROW_STRUCT_DATA_TYPE_CLASS(klass) \
- (G_TYPE_CHECK_CLASS_CAST((klass), \
- GARROW_TYPE_STRUCT_DATA_TYPE, \
- GArrowStructDataTypeClass))
-#define GARROW_IS_STRUCT_DATA_TYPE(obj) \
- (G_TYPE_CHECK_INSTANCE_TYPE((obj), \
- GARROW_TYPE_STRUCT_DATA_TYPE))
-#define GARROW_IS_STRUCT_DATA_TYPE_CLASS(klass) \
- (G_TYPE_CHECK_CLASS_TYPE((klass), \
- GARROW_TYPE_STRUCT_DATA_TYPE))
-#define GARROW_STRUCT_DATA_TYPE_GET_CLASS(obj) \
- (G_TYPE_INSTANCE_GET_CLASS((obj), \
- GARROW_TYPE_STRUCT_DATA_TYPE, \
- GArrowStructDataTypeClass))
-
-typedef struct _GArrowStructDataType GArrowStructDataType;
-typedef struct _GArrowStructDataTypeClass GArrowStructDataTypeClass;
-
-/**
- * GArrowStructDataType:
- *
- * It wraps `arrow::StructType`.
- */
-struct _GArrowStructDataType
-{
- /*< private >*/
- GArrowDataType parent_instance;
-};
-
+#define GARROW_TYPE_STRUCT_DATA_TYPE (garrow_struct_data_type_get_type())
+G_DECLARE_DERIVABLE_TYPE(GArrowStructDataType,
+ garrow_struct_data_type,
+ GARROW,
+ STRUCT_DATA_TYPE,
+ GArrowDataType)
struct _GArrowStructDataTypeClass
{
GArrowDataTypeClass parent_class;
};
-GType garrow_struct_data_type_get_type (void) G_GNUC_CONST;
GArrowStructDataType *garrow_struct_data_type_new (GList *fields);
-
+gint
+garrow_struct_data_type_get_n_fields(GArrowStructDataType *data_type);
+GList *
+garrow_struct_data_type_get_fields(GArrowStructDataType *data_type);
+GArrowField *
+garrow_struct_data_type_get_field(GArrowStructDataType *data_type,
+ gint i);
+GArrowField *
+garrow_struct_data_type_get_field_by_name(GArrowStructDataType *data_type,
+ const gchar *name);
+gint
+garrow_struct_data_type_get_field_index(GArrowStructDataType *data_type,
+ const gchar *name);
#define GARROW_TYPE_DICTIONARY_DATA_TYPE (garrow_dictionary_data_type_get_type())
G_DECLARE_DERIVABLE_TYPE(GArrowDictionaryDataType,
diff --git a/c_glib/test/test-struct-data-type.rb b/c_glib/test/test-struct-data-type.rb
new file mode 100644
index 0000000..ce94e41
--- /dev/null
+++ b/c_glib/test/test-struct-data-type.rb
@@ -0,0 +1,80 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+class TestStructDataType < Test::Unit::TestCase
+ def setup
+ @enabled_field = Arrow::Field.new("enabled", Arrow::BooleanDataType.new)
+ @message_field = Arrow::Field.new("message", Arrow::StringDataType.new)
+ @fields = [@enabled_field, @message_field]
+ @data_type = Arrow::StructDataType.new(@fields)
+ end
+
+ def test_type
+ assert_equal(Arrow::Type::STRUCT, @data_type.id)
+ end
+
+ def test_to_s
+ assert_equal("struct<enabled: bool, message: string>",
+ @data_type.to_s)
+ end
+
+ def test_n_fields
+ assert_equal(2, @data_type.n_fields)
+ end
+
+ def test_fields
+ assert_equal(@fields, @data_type.fields)
+ end
+
+ sub_test_case("#get_field") do
+ def test_found
+ assert_equal(@fields[1], @data_type.get_field(1))
+ end
+
+ def test_negative
+ assert_equal(@fields[-1], @data_type.get_field(-1))
+ end
+
+ def test_over
+ assert_equal(nil, @data_type.get_field(2))
+ end
+ end
+
+ sub_test_case("#get_field_by_name") do
+ def test_found
+ assert_equal(@enabled_field,
+ @data_type.get_field_by_name("enabled"))
+ end
+
+ def test_not_found
+ assert_equal(nil,
+ @data_type.get_field_by_name("nonexistent"))
+ end
+ end
+
+ sub_test_case("#get_field_by_name") do
+ def test_found
+ assert_equal(@fields.index(@enabled_field),
+ @data_type.get_field_index("enabled"))
+ end
+
+ def test_not_found
+ assert_equal(-1,
+ @data_type.get_field_index("nonexistent"))
+ end
+ end
+end