Skip to content

Commit

Permalink
apacheGH-45649: [GLib] Add GArrowBinaryViewArray
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroyuki-sato committed Mar 2, 2025
1 parent 31994b5 commit 3958c00
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
60 changes: 60 additions & 0 deletions c_glib/arrow-glib/basic-array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ G_BEGIN_DECLS
* string data. If you don't have Arrow format data, you need to
* use #GArrowLargeStringArrayBuilder to create a new array.
*
* #GArrayBinaryViewArray is a class for variable-size binary view array
* It can store zero or more binary view data. If you don't have Arrow
* format data, you need to use #GArrowBinaryViewArrayBuilder to create
* a new array.
*
* #GArrowFixedSizeBinaryArray is a class for fixed size binary array.
* It can store zero or more fixed size binary data. If you don't have
* Arrow format data, you need to use
Expand Down Expand Up @@ -2530,6 +2535,58 @@ garrow_large_string_array_get_string(GArrowLargeStringArray *array, gint64 i)
i);
}

G_DEFINE_TYPE(GArrowBinaryViewArray, garrow_binary_view_array, GARROW_TYPE_ARRAY)
static void
garrow_binary_view_array_init(GArrowBinaryViewArray *object)
{
}

static void
garrow_binary_view_array_class_init(GArrowBinaryViewArrayClass *klass)
{
}

/**
* garrow_binary_view_array_new:
* @length: The number of elements.
* @views: The view buffer.
* @data_buffers: (element-type GArrowBuffer): The data buffers.
* @null_bitmap: (nullable): The bitmap that shows null elements. The
* N-th element is null when the N-th bit is 0, not null otherwise.
* If the array has no null elements, the bitmap must be %NULL and
* @n_nulls is 0.
* @n_nulls: The number of null elements. If -1 is specified, the
* number of nulls are computed from @null_bitmap.
* @offset: The position of the first element.
*
* Returns: A newly created #GArrowBinaryViewArray.
*
* Since: 20.0.0
*/
GArrowBinaryViewArray *
garrow_binary_view_array_new(gint64 length,
GArrowBuffer *views,
GList *data_buffers,
GArrowBuffer *null_bitmap,
gint64 n_nulls,
gint64 offset)
{
std::vector<std::shared_ptr<arrow::Buffer>> arrow_data_buffers;
for (GList *node = data_buffers; node; node = g_list_next(node)) {
arrow_data_buffers.push_back(garrow_buffer_get_raw(GARROW_BUFFER(node->data)));
}
auto binary_view_array =
std::make_shared<arrow::BinaryViewArray>(arrow::binary_view(),
length,
garrow_buffer_get_raw(views),
std::move(arrow_data_buffers),
garrow_buffer_get_raw(null_bitmap),
n_nulls,
offset);
return GARROW_BINARY_VIEW_ARRAY(
g_object_new(GARROW_TYPE_BINARY_VIEW_ARRAY, "array", &binary_view_array, nullptr));
}

G_DEFINE_TYPE(GArrowDate32Array, garrow_date32_array, GARROW_TYPE_NUMERIC_ARRAY)

static void
Expand Down Expand Up @@ -3750,6 +3807,9 @@ garrow_array_new_raw_valist(std::shared_ptr<arrow::Array> *arrow_array,
case arrow::Type::type::RUN_END_ENCODED:
type = GARROW_TYPE_RUN_END_ENCODED_ARRAY;
break;
case arrow::Type::type::BINARY_VIEW:
type = GARROW_TYPE_BINARY_VIEW_ARRAY;
break;
default:
type = GARROW_TYPE_ARRAY;
break;
Expand Down
18 changes: 18 additions & 0 deletions c_glib/arrow-glib/basic-array.h
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,24 @@ GARROW_AVAILABLE_IN_0_16
gchar *
garrow_large_string_array_get_string(GArrowLargeStringArray *array, gint64 i);

#define GARROW_TYPE_BINARY_VIEW_ARRAY (garrow_binary_view_array_get_type())
GARROW_AVAILABLE_IN_20_0
G_DECLARE_DERIVABLE_TYPE(
GArrowBinaryViewArray, garrow_binary_view_array, GARROW, BINARY_VIEW_ARRAY, GArrowArray)
struct _GArrowBinaryViewArrayClass
{
GArrowArrayClass parent_class;
};

GARROW_AVAILABLE_IN_20_0
GArrowBinaryViewArray *
garrow_binary_view_array_new(gint64 length,
GArrowBuffer *views,
GList *data_buffers,
GArrowBuffer *null_bitmap,
gint64 n_nulls,
gint64 offset);

#define GARROW_TYPE_DATE32_ARRAY (garrow_date32_array_get_type())
GARROW_AVAILABLE_IN_ALL
G_DECLARE_DERIVABLE_TYPE(
Expand Down
35 changes: 35 additions & 0 deletions c_glib/test/test-binary-view-array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

class TestBinaryViewArray < Test::Unit::TestCase
def test_new
view_bytes = 16
buffer_string = "test"
view_data = [ buffer_string.size ].pack("l")
view_data += buffer_string
view_data += "\x00" * (view_bytes - buffer_string.size)

view = Arrow::Buffer.new(view_data)
data_buffer = Arrow::Buffer.new(buffer_string)
bitmap = Arrow::Buffer.new([0b1].pack("C*"))

str_view = Arrow::BinaryViewArray.new(1,view,[data_buffer],bitmap,0, 0)
assert do
str_view.validate_full
end
end
end

0 comments on commit 3958c00

Please sign in to comment.