Skip to content

Commit d4d26fe

Browse files
hiroyuki-satokou
andauthored
GH-45649: [GLib] Add GArrowBinaryViewArray (#45650)
### Rationale for this change [arrow::BinaryViewArray](https://arrow.apache.org/docs/cpp/api/array.html#_CPPv4N5arrow15BinaryViewArrayE) is available in the C++ API. But, GLib doesn't support that method yet. ### What changes are included in this PR? Add `GArrowBinaryViewArray` for wrapping `arrow::BinaryViewArray` class ### Are these changes tested? Yes. ### Are there any user-facing changes? Yes. * GitHub Issue: #45649 Lead-authored-by: Hiroyuki Sato <[email protected]> Co-authored-by: Sutou Kouhei <[email protected]> Signed-off-by: Sutou Kouhei <[email protected]>
1 parent 0fbf982 commit d4d26fe

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

c_glib/arrow-glib/basic-array.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ G_BEGIN_DECLS
127127
* string data. If you don't have Arrow format data, you need to
128128
* use #GArrowLargeStringArrayBuilder to create a new array.
129129
*
130+
* #GArrayBinaryViewArray is a class for variable-size binary view array.
131+
* It can store zero or more binary view data. If you don't have Arrow
132+
* format data, you need to use #GArrowBinaryViewArrayBuilder to create
133+
* a new array.
134+
*
130135
* #GArrowFixedSizeBinaryArray is a class for fixed size binary array.
131136
* It can store zero or more fixed size binary data. If you don't have
132137
* Arrow format data, you need to use
@@ -2530,6 +2535,73 @@ garrow_large_string_array_get_string(GArrowLargeStringArray *array, gint64 i)
25302535
i);
25312536
}
25322537

2538+
G_DEFINE_TYPE(GArrowBinaryViewArray, garrow_binary_view_array, GARROW_TYPE_ARRAY)
2539+
static void
2540+
garrow_binary_view_array_init(GArrowBinaryViewArray *object)
2541+
{
2542+
}
2543+
2544+
static void
2545+
garrow_binary_view_array_class_init(GArrowBinaryViewArrayClass *klass)
2546+
{
2547+
}
2548+
2549+
/**
2550+
* garrow_binary_view_array_new:
2551+
* @length: The number of elements.
2552+
* @views: The view buffer.
2553+
* @data_buffers: (element-type GArrowBuffer): The data buffers.
2554+
* @null_bitmap: (nullable): The bitmap that shows null elements. The
2555+
* N-th element is null when the N-th bit is 0, not null otherwise.
2556+
* If the array has no null elements, the bitmap must be %NULL and
2557+
* @n_nulls is 0.
2558+
* @n_nulls: The number of null elements. If -1 is specified, the
2559+
* number of nulls are computed from @null_bitmap.
2560+
* @offset: The position of the first element.
2561+
*
2562+
* Returns: A newly created #GArrowBinaryViewArray.
2563+
*
2564+
* Since: 20.0.0
2565+
*/
2566+
GArrowBinaryViewArray *
2567+
garrow_binary_view_array_new(gint64 length,
2568+
GArrowBuffer *views,
2569+
GList *data_buffers,
2570+
GArrowBuffer *null_bitmap,
2571+
gint64 n_nulls,
2572+
gint64 offset)
2573+
{
2574+
std::vector<std::shared_ptr<arrow::Buffer>> arrow_data_buffers;
2575+
for (GList *node = data_buffers; node; node = g_list_next(node)) {
2576+
arrow_data_buffers.push_back(garrow_buffer_get_raw(GARROW_BUFFER(node->data)));
2577+
}
2578+
auto binary_view_array =
2579+
std::make_shared<arrow::BinaryViewArray>(arrow::binary_view(),
2580+
length,
2581+
garrow_buffer_get_raw(views),
2582+
std::move(arrow_data_buffers),
2583+
garrow_buffer_get_raw(null_bitmap),
2584+
n_nulls,
2585+
offset);
2586+
return GARROW_BINARY_VIEW_ARRAY(
2587+
g_object_new(GARROW_TYPE_BINARY_VIEW_ARRAY, "array", &binary_view_array, nullptr));
2588+
}
2589+
2590+
/**
2591+
* garrow_binary_view_array_get_value:
2592+
* @array: A #GArrowBinaryViewArray.
2593+
* @i: The index of the target value.
2594+
*
2595+
* Returns: (transfer full): The @i-th value.
2596+
*/
2597+
GBytes *
2598+
garrow_binary_view_array_get_value(GArrowBinaryViewArray *array, gint64 i)
2599+
{
2600+
auto arrow_array = garrow_array_get_raw(GARROW_ARRAY(array));
2601+
auto view = static_cast<arrow::BinaryViewArray *>(arrow_array.get())->GetView(i);
2602+
return g_bytes_new_static(view.data(), view.length());
2603+
}
2604+
25332605
G_DEFINE_TYPE(GArrowDate32Array, garrow_date32_array, GARROW_TYPE_NUMERIC_ARRAY)
25342606

25352607
static void
@@ -3750,6 +3822,9 @@ garrow_array_new_raw_valist(std::shared_ptr<arrow::Array> *arrow_array,
37503822
case arrow::Type::type::RUN_END_ENCODED:
37513823
type = GARROW_TYPE_RUN_END_ENCODED_ARRAY;
37523824
break;
3825+
case arrow::Type::type::BINARY_VIEW:
3826+
type = GARROW_TYPE_BINARY_VIEW_ARRAY;
3827+
break;
37533828
default:
37543829
type = GARROW_TYPE_ARRAY;
37553830
break;

c_glib/arrow-glib/basic-array.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,28 @@ GARROW_AVAILABLE_IN_0_16
602602
gchar *
603603
garrow_large_string_array_get_string(GArrowLargeStringArray *array, gint64 i);
604604

605+
#define GARROW_TYPE_BINARY_VIEW_ARRAY (garrow_binary_view_array_get_type())
606+
GARROW_AVAILABLE_IN_20_0
607+
G_DECLARE_DERIVABLE_TYPE(
608+
GArrowBinaryViewArray, garrow_binary_view_array, GARROW, BINARY_VIEW_ARRAY, GArrowArray)
609+
struct _GArrowBinaryViewArrayClass
610+
{
611+
GArrowArrayClass parent_class;
612+
};
613+
614+
GARROW_AVAILABLE_IN_20_0
615+
GArrowBinaryViewArray *
616+
garrow_binary_view_array_new(gint64 length,
617+
GArrowBuffer *views,
618+
GList *data_buffers,
619+
GArrowBuffer *null_bitmap,
620+
gint64 n_nulls,
621+
gint64 offset);
622+
623+
GARROW_AVAILABLE_IN_20_0
624+
GBytes *
625+
garrow_binary_view_array_get_value(GArrowBinaryViewArray *array, gint64 i);
626+
605627
#define GARROW_TYPE_DATE32_ARRAY (garrow_date32_array_get_type())
606628
GARROW_AVAILABLE_IN_ALL
607629
G_DECLARE_DERIVABLE_TYPE(
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
class TestBinaryViewArray < Test::Unit::TestCase
19+
def test_new
20+
short_binary_data = "test"
21+
short_view_buffer_space = 12
22+
short_view_buffer = [short_binary_data.size].pack("l")
23+
short_view_buffer += short_binary_data.ljust(short_view_buffer_space, "\x00")
24+
25+
arrow_view_buffer = Arrow::Buffer.new(short_view_buffer)
26+
arrow_data_buffer = Arrow::Buffer.new(short_binary_data)
27+
bitmap = Arrow::Buffer.new([0b1].pack("C*"))
28+
29+
binary_view_array = Arrow::BinaryViewArray.new(1,
30+
arrow_view_buffer,
31+
[arrow_data_buffer],
32+
bitmap,
33+
0,
34+
0)
35+
assert do
36+
binary_view_array.validate_full
37+
end
38+
assert_equal(short_binary_data, binary_view_array.get_value(0).to_s)
39+
end
40+
end

0 commit comments

Comments
 (0)