Skip to content

Commit

Permalink
apacheGH-44761: [GLib] Add garrow_table_validate() (apache#45414)
Browse files Browse the repository at this point in the history
### Rationale for this change

[Table::Validate](https://arrow.apache.org/docs/cpp/api/table.html#_CPPv4NK5arrow5Table8ValidateEv) is available in the C++ API.
But, GLib doesn't support that method yet.

### What changes are included in this PR?

This PR adds a validation method in the table class.
Before this change, the `Validate()` method was used in 
`garrow_table_new_values()`, `garrow_table_new_arrays()`,
and `garrow_table_new_chunked_arrays()` functions implicitly.
This PR removes them and adds it as a separate method.
Users need to call `garrow_table_validate()` explicitly by themselves.
This is a backward incompatible change.

### Are these changes tested?

Yes.

### Are there any user-facing changes?

Yes.

**This PR includes breaking changes to public APIs.**

* GitHub Issue: apache#44761

Lead-authored-by: Hiroyuki Sato <hiroysato@gmail.com>
Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
Signed-off-by: Sutou Kouhei <kou@clear-code.com>
  • Loading branch information
hiroyuki-sato and kou authored Feb 5, 2025
1 parent 1567be0 commit 2cbc7d6
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 24 deletions.
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.
*
* 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

0 comments on commit 2cbc7d6

Please sign in to comment.