Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions c_glib/arrow-glib/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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::Table> 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 *
Expand Down
47 changes: 10 additions & 37 deletions c_glib/arrow-glib/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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
8 changes: 8 additions & 0 deletions c_glib/test/test-table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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