Skip to content

Commit be38880

Browse files
GH-44759: [GLib] Add garrow_record_batch_validate() (#45353)
### Rationale for this change [RecordBatch::Validate](https://arrow.apache.org/docs/cpp/api/table.html#_CPPv4NK5arrow11RecordBatch8ValidateEv) 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 record-batch class. Before this change, the `Validate()` method was used in the `garrow_record_batch_new` implicitly. This PR removes it and adds it as a separate method. Users need to call `garrow_record_batch_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: #44759 Authored-by: Hiroyuki Sato <hiroysato@gmail.com> Signed-off-by: Sutou Kouhei <kou@clear-code.com>
1 parent 2c90daf commit be38880

3 files changed

Lines changed: 53 additions & 6 deletions

File tree

c_glib/arrow-glib/record-batch.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,7 @@ garrow_record_batch_new(GArrowSchema *schema,
191191
}
192192

193193
auto arrow_record_batch = arrow::RecordBatch::Make(arrow_schema, n_rows, arrow_columns);
194-
auto status = arrow_record_batch->Validate();
195-
if (garrow_error_check(error, status, tag)) {
196-
return garrow_record_batch_new_raw(&arrow_record_batch);
197-
} else {
198-
return NULL;
199-
}
194+
return garrow_record_batch_new_raw(&arrow_record_batch);
200195
}
201196

202197
/**
@@ -702,3 +697,19 @@ garrow_record_batch_iterator_get_raw(GArrowRecordBatchIterator *iterator)
702697
auto priv = GARROW_RECORD_BATCH_ITERATOR_GET_PRIVATE(iterator);
703698
return &priv->iterator;
704699
}
700+
701+
/**
702+
* garrow_record_batch_validate
703+
* @record_batch: A #GArrowRecordBatch
704+
* @error: (nullable): Return location for a #GError or %NULL.
705+
*
706+
* Returns: %TRUE on success, %FALSE on error.
707+
*
708+
* Since: 20.0.0
709+
*/
710+
gboolean
711+
garrow_record_batch_validate(GArrowRecordBatch *record_batch, GError **error)
712+
{
713+
const auto arrow_record_batch = garrow_record_batch_get_raw(record_batch);
714+
return garrow::check(error, arrow_record_batch->Validate(), "[record-batch][validate]");
715+
}

c_glib/arrow-glib/record-batch.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ garrow_record_batch_serialize(GArrowRecordBatch *record_batch,
109109
GArrowWriteOptions *options,
110110
GError **error);
111111

112+
GARROW_AVAILABLE_IN_20_0
113+
gboolean
114+
garrow_record_batch_validate(GArrowRecordBatch *record_batch, GError **error);
115+
112116
#define GARROW_TYPE_RECORD_BATCH_ITERATOR (garrow_record_batch_iterator_get_type())
113117
GARROW_AVAILABLE_IN_0_17
114118
G_DECLARE_DERIVABLE_TYPE(GArrowRecordBatchIterator,

c_glib/test/test-record-batch.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,5 +189,37 @@ def test_serialize
189189
assert_equal(@record_batch,
190190
input_stream.read_record_batch(@record_batch.schema))
191191
end
192+
193+
sub_test_case("#validate") do
194+
def setup
195+
@id_field = Arrow::Field.new("id", Arrow::UInt8DataType.new)
196+
@name_field = Arrow::Field.new("name", Arrow::StringDataType.new)
197+
@schema = Arrow::Schema.new([@id_field, @name_field])
198+
199+
@id_value = build_uint_array([1])
200+
@name_value = build_string_array(["abc"])
201+
@values = [@id_value, @name_value]
202+
end
203+
204+
def test_valid
205+
n_rows = @id_value.length
206+
record_batch = Arrow::RecordBatch.new(@schema, n_rows, @values)
207+
208+
assert do
209+
record_batch.validate
210+
end
211+
end
212+
213+
def test_invalid
214+
message = "[record-batch][validate]: Invalid: " +
215+
"Number of rows in column 0 did not match batch: 1 vs 2"
216+
n_rows = @id_value.length + 1 # incorrect number of rows
217+
218+
record_batch = Arrow::RecordBatch.new(@schema, n_rows, @values)
219+
assert_raise(Arrow::Error::Invalid.new(message)) do
220+
record_batch.validate
221+
end
222+
end
223+
end
192224
end
193225
end

0 commit comments

Comments
 (0)