Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
140 changes: 140 additions & 0 deletions c_glib/arrow-glib/array-builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ G_BEGIN_DECLS
* you use this builder instead of specific integer size builder such
* as #GArrowInt8ArrayBuilder.
*
* #GArrowUIntArrayBuilder is the class to create a new unsigned
* integer array. Unsigned integer size is automatically chosen. It's
* recommend that you use this builder instead of specific unsigned
* integer size builder such as #GArrowUInt8ArrayBuilder.
*
* #GArrowInt8ArrayBuilder is the class to create a new
* #GArrowInt8Array.
*
Expand Down Expand Up @@ -587,6 +592,141 @@ garrow_int_array_builder_append_nulls(GArrowIntArrayBuilder *builder,
}


G_DEFINE_TYPE(GArrowUIntArrayBuilder,
garrow_uint_array_builder,
GARROW_TYPE_ARRAY_BUILDER)

static void
garrow_uint_array_builder_init(GArrowUIntArrayBuilder *builder)
{
}

static void
garrow_uint_array_builder_class_init(GArrowUIntArrayBuilderClass *klass)
{
}

/**
* garrow_uint_array_builder_new:
*
* Returns: A newly created #GArrowUIntArrayBuilder.
*
* Since: 0.8.0
*/
GArrowUIntArrayBuilder *
garrow_uint_array_builder_new(void)
{
auto memory_pool = arrow::default_memory_pool();
auto arrow_builder = new arrow::AdaptiveUIntBuilder(memory_pool);
auto builder = garrow_array_builder_new_raw(arrow_builder,
GARROW_TYPE_UINT_ARRAY_BUILDER);
return GARROW_UINT_ARRAY_BUILDER(builder);
}

/**
* garrow_uint_array_builder_append:
* @builder: A #GArrowUIntArrayBuilder.
* @value: A unsigned int value.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Returns: %TRUE on success, %FALSE if there was an error.
*
* Since: 0.8.0
*/
gboolean
garrow_uint_array_builder_append(GArrowUIntArrayBuilder *builder,
guint64 value,
GError **error)
{
return garrow_array_builder_append<arrow::AdaptiveUIntBuilder *>
(GARROW_ARRAY_BUILDER(builder),
value,
error,
"[uint-array-builder][append]");
}

/**
* garrow_uint_array_builder_append_values:
* @builder: A #GArrowUIntArrayBuilder.
* @values: (array length=values_length): The array of unsigned int.
* @values_length: The length of `values`.
* @is_valids: (nullable) (array length=is_valids_length): The array of
* boolean that shows whether the Nth value is valid or not. If the
* Nth `is_valids` is %TRUE, the Nth `values` is valid value. Otherwise
* the Nth value is null value.
* @is_valids_length: The length of `is_valids`.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Append multiple values at once. It's efficient than multiple
* `append()` and `append_null()` calls.
*
* Returns: %TRUE on success, %FALSE if there was an error.
*
* Since: 0.8.0
*/
gboolean
garrow_uint_array_builder_append_values(GArrowUIntArrayBuilder *builder,
const guint64 *values,
gint64 values_length,
const gboolean *is_valids,
gint64 is_valids_length,
GError **error)
{
return garrow_array_builder_append_values<arrow::AdaptiveUIntBuilder *>
(GARROW_ARRAY_BUILDER(builder),
reinterpret_cast<const uint64_t *>(values),
values_length,
is_valids,
is_valids_length,
error,
"[uint-array-builder][append-values]");
}

/**
* garrow_uint_array_builder_append_null:
* @builder: A #GArrowUIntArrayBuilder.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Returns: %TRUE on success, %FALSE if there was an error.
*
* Since: 0.8.0
*/
gboolean
garrow_uint_array_builder_append_null(GArrowUIntArrayBuilder *builder,
GError **error)
{
return garrow_array_builder_append_null<arrow::AdaptiveUIntBuilder *>
(GARROW_ARRAY_BUILDER(builder),
error,
"[uint-array-builder][append-null]");
}

/**
* garrow_uint_array_builder_append_nulls:
* @builder: A #GArrowUIntArrayBuilder.
* @n: The number of null values to be appended.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Append multiple nulls at once. It's efficient than multiple
* `append_null()` calls.
*
* Returns: %TRUE on success, %FALSE if there was an error.
*
* Since: 0.8.0
*/
gboolean
garrow_uint_array_builder_append_nulls(GArrowUIntArrayBuilder *builder,
gint64 n,
GError **error)
{
return garrow_array_builder_append_nulls<arrow::AdaptiveUIntBuilder *>
(GARROW_ARRAY_BUILDER(builder),
n,
error,
"[uint-array-builder][append-nulls]");
}


G_DEFINE_TYPE(GArrowInt8ArrayBuilder,
garrow_int8_array_builder,
GARROW_TYPE_ARRAY_BUILDER)
Expand Down
73 changes: 32 additions & 41 deletions c_glib/arrow-glib/array-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,50 +20,16 @@
#pragma once

#include <arrow-glib/array.h>
#include <arrow-glib/gobject-type.h>

G_BEGIN_DECLS

#define GARROW_TYPE_ARRAY_BUILDER \
(garrow_array_builder_get_type())
#define GARROW_ARRAY_BUILDER(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj), \
GARROW_TYPE_ARRAY_BUILDER, \
GArrowArrayBuilder))
#define GARROW_ARRAY_BUILDER_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass), \
GARROW_TYPE_ARRAY_BUILDER, \
GArrowArrayBuilderClass))
#define GARROW_IS_ARRAY_BUILDER(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj), \
GARROW_TYPE_ARRAY_BUILDER))
#define GARROW_IS_ARRAY_BUILDER_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE((klass), \
GARROW_TYPE_ARRAY_BUILDER))
#define GARROW_ARRAY_BUILDER_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS((obj), \
GARROW_TYPE_ARRAY_BUILDER, \
GArrowArrayBuilderClass))

typedef struct _GArrowArrayBuilder GArrowArrayBuilder;
typedef struct _GArrowArrayBuilderClass GArrowArrayBuilderClass;

/**
* GArrowArrayBuilder:
*
* It wraps `arrow::ArrayBuilder`.
*/
struct _GArrowArrayBuilder
{
/*< private >*/
GObject parent_instance;
};

struct _GArrowArrayBuilderClass
{
GObjectClass parent_class;
};

GType garrow_array_builder_get_type (void) G_GNUC_CONST;
#define GARROW_TYPE_ARRAY_BUILDER (garrow_array_builder_get_type())
GARROW_DECLARE_TYPE(GArrowArrayBuilder,
garrow_array_builder,
GARROW,
ARRAY_BUILDER,
GObject)

GArrowArray *garrow_array_builder_finish (GArrowArrayBuilder *builder,
GError **error);
Expand Down Expand Up @@ -189,6 +155,31 @@ gboolean garrow_int_array_builder_append_nulls(GArrowIntArrayBuilder *builder,
GError **error);


#define GARROW_TYPE_UINT_ARRAY_BUILDER (garrow_uint_array_builder_get_type())
GARROW_DECLARE_TYPE(GArrowUIntArrayBuilder,
garrow_uint_array_builder,
GARROW,
UINT_ARRAY_BUILDER,
GArrowArrayBuilder)

GArrowUIntArrayBuilder *garrow_uint_array_builder_new(void);

gboolean garrow_uint_array_builder_append(GArrowUIntArrayBuilder *builder,
guint64 value,
GError **error);
gboolean garrow_uint_array_builder_append_values(GArrowUIntArrayBuilder *builder,
const guint64 *values,
gint64 values_length,
const gboolean *is_valids,
gint64 is_valids_length,
GError **error);
gboolean garrow_uint_array_builder_append_null(GArrowUIntArrayBuilder *builder,
GError **error);
gboolean garrow_uint_array_builder_append_nulls(GArrowUIntArrayBuilder *builder,
gint64 n,
GError **error);


#define GARROW_TYPE_INT8_ARRAY_BUILDER \
(garrow_int8_array_builder_get_type())
#define GARROW_INT8_ARRAY_BUILDER(obj) \
Expand Down
4 changes: 4 additions & 0 deletions c_glib/test/helper/buildable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def build_int_array(values)
build_array(Arrow::IntArrayBuilder.new, values)
end

def build_uint_array(values)
build_array(Arrow::UIntArrayBuilder.new, values)
end

def build_int8_array(values)
build_array(Arrow::Int8ArrayBuilder.new, values)
end
Expand Down
22 changes: 22 additions & 0 deletions c_glib/test/test-array-builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,28 @@ def sample_values
end
end

sub_test_case("UIntArrayBuilder") do
def create_builder
Arrow::UIntArrayBuilder.new
end

def builder_class_name
"uint-array-builder"
end

def sample_values
[1, 2, 3]
end

sub_test_case("#append_values") do
include ArrayBuilderAppendValuesTests
end

sub_test_case("#append_nulls") do
include ArrayBuilderAppendNullsTests
end
end

sub_test_case("Int8ArrayBuilder") do
def create_builder
Arrow::Int8ArrayBuilder.new
Expand Down
59 changes: 59 additions & 0 deletions c_glib/test/test-uint-array-builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# 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 TestUIntArrayBuilder < Test::Unit::TestCase
include Helper::Buildable

def test_uint8
values = [0, 2]
assert_equal(build_uint_array([*values, nil]),
Arrow::UInt8Array.new(3,
Arrow::Buffer.new(values.pack("C*")),
Arrow::Buffer.new([0b011].pack("C*")),
-1))
end

def test_uint16
border_value = 2 ** 8
values = [0, border_value]
assert_equal(build_uint_array([*values, nil]),
Arrow::UInt16Array.new(3,
Arrow::Buffer.new(values.pack("S*")),
Arrow::Buffer.new([0b011].pack("C*")),
-1))
end

def test_uint32
border_value = 2 ** 16
values = [0, border_value]
assert_equal(build_uint_array([*values, nil]),
Arrow::UInt32Array.new(3,
Arrow::Buffer.new(values.pack("L*")),
Arrow::Buffer.new([0b011].pack("C*")),
-1))
end

def test_uint64
border_value = 2 ** 32
values = [0, border_value]
assert_equal(build_uint_array([*values, nil]),
Arrow::UInt64Array.new(3,
Arrow::Buffer.new(values.pack("Q*")),
Arrow::Buffer.new([0b011].pack("C*")),
-1))
end
end