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-45649: [GLib] Add GArrowBinaryViewArray #45650

Merged
merged 4 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 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,73 @@ 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));
}

/**
* garrow_binary_view_array_get_value:
* @array: A #GArrowBinaryViewArray.
* @i: The index of the target value.
*
* Returns: (transfer full): The @i-th value.
*/
GBytes *
garrow_binary_view_array_get_value(GArrowBinaryViewArray *array, gint64 i)
{
auto arrow_array = garrow_array_get_raw(GARROW_ARRAY(array));
auto view = static_cast<arrow::BinaryViewArray *>(arrow_array.get())->GetView(i);
return g_bytes_new_static(view.data(), view.length());
}

G_DEFINE_TYPE(GArrowDate32Array, garrow_date32_array, GARROW_TYPE_NUMERIC_ARRAY)

static void
Expand Down Expand Up @@ -3750,6 +3822,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
22 changes: 22 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,28 @@ 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);

GARROW_AVAILABLE_IN_20_0
GBytes *
garrow_binary_view_array_get_value(GArrowBinaryViewArray *array, gint64 i);

#define GARROW_TYPE_DATE32_ARRAY (garrow_date32_array_get_type())
GARROW_AVAILABLE_IN_ALL
G_DECLARE_DERIVABLE_TYPE(
Expand Down
36 changes: 36 additions & 0 deletions c_glib/test/test-binary-view-array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we improve variable name something like the following?

Suggested change
view_bytes = 16
short_string_space = 12

(I think that we don't need to include length space here.)

FYI: The specification: https://arrow.apache.org/docs/format/Columnar.html#variable-size-binary-view-layout

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got the exception Error: test_new(TestBinaryViewArray): Arrow::Error::Invalid: [array][validate-full]: Invalid: Buffer #1 too small in array of type binary_view and length 1: expected at least 16 byte(s), got 8 if I don't use padding.

  def test_new
    # https://arrow.apache.org/docs/format/Columnar.html#variable-size-binary-view-layout
    short_binary_data = "test"
    short_view_buffer_space = 12
    short_view_buffer = [short_binary_data.size].pack("l")
    #short_view_buffer += short_binary_data.ljust(short_view_buffer_space, "\x00")
    short_view_buffer += short_binary_data

    arrow_view_buffer = Arrow::Buffer.new(short_view_buffer)
    arrow_data_buffer = Arrow::Buffer.new(short_binary_data)
    bitmap = Arrow::Buffer.new([0b1].pack("C*"))

    binary_view_array = Arrow::BinaryViewArray.new(1,
                                                   arrow_view_buffer,
                                                   [arrow_data_buffer],
                                                   bitmap,
                                                   0,
                                                   0)
    assert do
      binary_view_array.validate_full
    end
    assert_equal(short_binary_data, binary_view_array.get_value(0).to_s)
  end

buffer_string = "test"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we improve variable name something like the following?

Suggested change
buffer_string = "test"
short_binary_data = "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
assert_equal(buffer_string, str_view.get_value(0).to_s)
end
end