diff --git a/c_glib/arrow-glib/table.cpp b/c_glib/arrow-glib/table.cpp index e086396f8f9e..b9946a390353 100644 --- a/c_glib/arrow-glib/table.cpp +++ b/c_glib/arrow-glib/table.cpp @@ -276,6 +276,36 @@ garrow_table_remove_column(GArrowTable *table, } } +/** + * garrow_table_replace_column: + * @table: A #GArrowTable. + * @i: The index of the column to be replaced. + * @column: The newly added #GArrowColumn. + * @error: (nullable): Return location for a #GError or %NULL. + * + * Returns: (nullable) (transfer full): The newly allocated + * #GArrowTable that has @column as the @i-th column or %NULL on + * error. + * + * Since: 0.10.0 + */ +GArrowTable * +garrow_table_replace_column(GArrowTable *table, + guint i, + GArrowColumn *column, + GError **error) +{ + const auto arrow_table = garrow_table_get_raw(table); + const auto arrow_column = garrow_column_get_raw(column); + std::shared_ptr arrow_new_table; + auto status = arrow_table->SetColumn(i, arrow_column, &arrow_new_table); + if (garrow_error_check(error, status, "[table][replace-column]")) { + return garrow_table_new_raw(&arrow_new_table); + } else { + return NULL; + } +} + G_END_DECLS GArrowTable * diff --git a/c_glib/arrow-glib/table.h b/c_glib/arrow-glib/table.h index 9e21669cd11d..19843f3dc71c 100644 --- a/c_glib/arrow-glib/table.h +++ b/c_glib/arrow-glib/table.h @@ -24,48 +24,17 @@ G_BEGIN_DECLS -#define GARROW_TYPE_TABLE \ - (garrow_table_get_type()) -#define GARROW_TABLE(obj) \ - (G_TYPE_CHECK_INSTANCE_CAST((obj), \ - GARROW_TYPE_TABLE, \ - GArrowTable)) -#define GARROW_TABLE_CLASS(klass) \ - (G_TYPE_CHECK_CLASS_CAST((klass), \ - GARROW_TYPE_TABLE, \ - GArrowTableClass)) -#define GARROW_IS_TABLE(obj) \ - (G_TYPE_CHECK_INSTANCE_TYPE((obj), \ - GARROW_TYPE_TABLE)) -#define GARROW_IS_TABLE_CLASS(klass) \ - (G_TYPE_CHECK_CLASS_TYPE((klass), \ - GARROW_TYPE_TABLE)) -#define GARROW_TABLE_GET_CLASS(obj) \ - (G_TYPE_INSTANCE_GET_CLASS((obj), \ - GARROW_TYPE_TABLE, \ - GArrowTableClass)) - -typedef struct _GArrowTable GArrowTable; -typedef struct _GArrowTableClass GArrowTableClass; - -/** - * GArrowTable: - * - * It wraps `arrow::Table`. - */ -struct _GArrowTable -{ - /*< private >*/ - GObject parent_instance; -}; - +#define GARROW_TYPE_TABLE (garrow_table_get_type()) +G_DECLARE_DERIVABLE_TYPE(GArrowTable, + garrow_table, + GARROW, + TABLE, + GObject) struct _GArrowTableClass { GObjectClass parent_class; }; -GType garrow_table_get_type (void) G_GNUC_CONST; - GArrowTable *garrow_table_new (GArrowSchema *schema, GList *columns); @@ -85,5 +54,9 @@ GArrowTable *garrow_table_add_column (GArrowTable *table, GArrowTable *garrow_table_remove_column (GArrowTable *table, guint i, GError **error); +GArrowTable *garrow_table_replace_column(GArrowTable *table, + guint i, + GArrowColumn *column, + GError **error); G_END_DECLS diff --git a/c_glib/test/test-table.rb b/c_glib/test/test-table.rb index 08dd34861e51..70ee653342d3 100644 --- a/c_glib/test/test-table.rb +++ b/c_glib/test/test-table.rb @@ -110,5 +110,13 @@ def test_remove_column assert_equal(["valid"], new_table.schema.fields.collect(&:name)) end + + def test_replace_column + field = Arrow::Field.new("added", Arrow::BooleanDataType.new) + column = Arrow::Column.new(field, build_boolean_array([true])) + new_table = @table.replace_column(0, column) + assert_equal(["added", "valid"], + new_table.schema.fields.collect(&:name)) + end end end