Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-44761: [GLib] Add garrow_table_validate() #45414

Merged
merged 9 commits into from
Feb 5, 2025
46 changes: 22 additions & 24 deletions c_glib/arrow-glib/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,20 +339,10 @@ garrow_table_new_values(GArrowSchema *schema, GList *values, GError **error)

if (!arrow_chunked_arrays.empty()) {
auto arrow_table = arrow::Table::Make(arrow_schema, std::move(arrow_chunked_arrays));
auto status = arrow_table->Validate();
if (garrow_error_check(error, status, context)) {
return garrow_table_new_raw(&arrow_table);
} else {
return NULL;
}
return garrow_table_new_raw(&arrow_table);
} else if (!arrow_arrays.empty()) {
auto arrow_table = arrow::Table::Make(arrow_schema, std::move(arrow_arrays));
auto status = arrow_table->Validate();
if (garrow_error_check(error, status, context)) {
return garrow_table_new_raw(&arrow_table);
} else {
return NULL;
}
return garrow_table_new_raw(&arrow_table);
} else {
auto maybe_table =
arrow::Table::FromRecordBatches(arrow_schema, std::move(arrow_record_batches));
Expand Down Expand Up @@ -390,12 +380,7 @@ garrow_table_new_chunked_arrays(GArrowSchema *schema,
}

auto arrow_table = arrow::Table::Make(arrow_schema, arrow_chunked_arrays);
auto status = arrow_table->Validate();
if (garrow_error_check(error, status, "[table][new][chunked-arrays]")) {
return garrow_table_new_raw(&arrow_table);
} else {
return NULL;
}
return garrow_table_new_raw(&arrow_table);
}

/**
Expand All @@ -422,12 +407,7 @@ garrow_table_new_arrays(GArrowSchema *schema,
}

auto arrow_table = arrow::Table::Make(arrow_schema, arrow_arrays);
auto status = arrow_table->Validate();
if (garrow_error_check(error, status, "[table][new][arrays]")) {
return garrow_table_new_raw(&arrow_table);
} else {
return NULL;
}
return garrow_table_new_raw(&arrow_table);
}

/**
Expand Down Expand Up @@ -756,6 +736,24 @@ garrow_table_combine_chunks(GArrowTable *table, GError **error)
}
}

/**
* garrow_table_validate
* @table: A #GArrowTable
* @error: (nullable): Return location for a #GError or %NULL.
*
hiroyuki-sato marked this conversation as resolved.
Show resolved Hide resolved
* Validate the given table. This is a cheap validation.
*
* Returns: %TRUE on success, %FALSE on error.
*
* Since: 20.0.0
*/
gboolean
garrow_table_validate(GArrowTable *table, GError **error)
{
const auto arrow_table = garrow_table_get_raw(table);
return garrow::check(error, arrow_table->Validate(), "[table][validate]");
}

typedef struct GArrowFeatherWritePropertiesPrivate_
{
arrow::ipc::feather::WriteProperties properties;
Expand Down
4 changes: 4 additions & 0 deletions c_glib/arrow-glib/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ GARROW_AVAILABLE_IN_0_16
GArrowTable *
garrow_table_combine_chunks(GArrowTable *table, GError **error);

GARROW_AVAILABLE_IN_20_0
gboolean
garrow_table_validate(GArrowTable *table, GError **error);

#define GARROW_TYPE_FEATHER_WRITE_PROPERTIES (garrow_feather_write_properties_get_type())
GARROW_AVAILABLE_IN_0_17
G_DECLARE_DERIVABLE_TYPE(GArrowFeatherWriteProperties,
Expand Down
31 changes: 31 additions & 0 deletions c_glib/test/test-table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,37 @@ def test_combine_chunks
all_values)
end

sub_test_case("#validate") do
def setup
@id_field = Arrow::Field.new("id", Arrow::UInt8DataType.new)
@name_field = Arrow::Field.new("name", Arrow::StringDataType.new)
@schema = Arrow::Schema.new([@id_field, @name_field])

@id_array = build_uint_array([1])
@name_array = build_string_array(["abc"])
@arrays = [@id_array, @name_array]
end

def test_valid
table = Arrow::Table.new(@schema, @arrays)

assert do
table.validate
end
end

def test_invalid
message = "[table][validate]: Invalid: " +
"Column 1 named name expected length 1 but got length 2"

invalid_values = [@id_array, build_string_array(["abc", "def"])]
table = Arrow::Table.new(@schema, invalid_values)
assert_raise(Arrow::Error::Invalid.new(message)) do
table.validate
end
end
end

sub_test_case("#write_as_feather") do
def setup
super
Expand Down
Loading