Skip to content
Merged
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
5 changes: 5 additions & 0 deletions ruby/red-arrow/ext/arrow/converters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ namespace red_arrow {
return large_list_array_value_converter_->convert(array, i);
}

VALUE ArrayValueConverter::convert(const arrow::FixedSizeListArray& array,
const int64_t i) {
return fixed_size_list_array_value_converter_->convert(array, i);
}

VALUE ArrayValueConverter::convert(const arrow::StructArray& array,
const int64_t i) {
return struct_array_value_converter_->convert(array, i);
Expand Down
118 changes: 118 additions & 0 deletions ruby/red-arrow/ext/arrow/converters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
namespace red_arrow {
class ListArrayValueConverter;
class LargeListArrayValueConverter;
class FixedSizeListArrayValueConverter;
class StructArrayValueConverter;
class MapArrayValueConverter;
class UnionArrayValueConverter;
Expand All @@ -40,6 +41,7 @@ namespace red_arrow {
: decimal_buffer_(),
list_array_value_converter_(nullptr),
large_list_array_value_converter_(nullptr),
fixed_size_list_array_value_converter_(nullptr),
struct_array_value_converter_(nullptr),
map_array_value_converter_(nullptr),
union_array_value_converter_(nullptr),
Expand All @@ -48,12 +50,14 @@ namespace red_arrow {

inline void set_sub_value_converters(ListArrayValueConverter* list_array_value_converter,
LargeListArrayValueConverter* large_list_array_value_converter,
FixedSizeListArrayValueConverter* fixed_size_list_array_value_converter,
StructArrayValueConverter* struct_array_value_converter,
MapArrayValueConverter* map_array_value_converter,
UnionArrayValueConverter* union_array_value_converter,
DictionaryArrayValueConverter* dictionary_array_value_converter) {
list_array_value_converter_ = list_array_value_converter;
large_list_array_value_converter_ = large_list_array_value_converter;
fixed_size_list_array_value_converter_ = fixed_size_list_array_value_converter;
struct_array_value_converter_ = struct_array_value_converter;
map_array_value_converter_ = map_array_value_converter;
union_array_value_converter_ = union_array_value_converter;
Expand Down Expand Up @@ -286,6 +290,9 @@ namespace red_arrow {
VALUE convert(const arrow::LargeListArray& array,
const int64_t i);

VALUE convert(const arrow::FixedSizeListArray& array,
const int64_t i);

VALUE convert(const arrow::StructArray& array,
const int64_t i);

Expand Down Expand Up @@ -322,6 +329,7 @@ namespace red_arrow {
std::string decimal_buffer_;
ListArrayValueConverter* list_array_value_converter_;
LargeListArrayValueConverter* large_list_array_value_converter_;
FixedSizeListArrayValueConverter* fixed_size_list_array_value_converter_;
StructArrayValueConverter* struct_array_value_converter_;
MapArrayValueConverter* map_array_value_converter_;
UnionArrayValueConverter* union_array_value_converter_;
Expand Down Expand Up @@ -385,6 +393,7 @@ namespace red_arrow {
VISIT(Duration)
VISIT(List)
VISIT(LargeList)
VISIT(FixedSizeList)
VISIT(Struct)
VISIT(Map)
VISIT(SparseUnion)
Expand Down Expand Up @@ -485,6 +494,108 @@ namespace red_arrow {
VISIT(Duration)
VISIT(List)
VISIT(LargeList)
VISIT(FixedSizeList)
VISIT(Struct)
VISIT(Map)
VISIT(SparseUnion)
VISIT(DenseUnion)
VISIT(Dictionary)
VISIT(Decimal128)
VISIT(Decimal256)
// TODO
// VISIT(Extension)

#undef VISIT

private:
template <typename ArrayType>
inline VALUE convert_value(const ArrayType& array,
const int64_t i) {
return array_value_converter_->convert(array, i);
}

template <typename ArrayType>
arrow::Status visit_value(const ArrayType& array) {
if (array.null_count() > 0) {
for (int64_t i = 0; i < length_; ++i) {
auto value = Qnil;
if (!array.IsNull(i + offset_)) {
value = convert_value(array, i + offset_);
}
rb_ary_push(result_, value);
}
} else {
for (int64_t i = 0; i < length_; ++i) {
rb_ary_push(result_, convert_value(array, i + offset_));
}
}
return arrow::Status::OK();
}

ArrayValueConverter* array_value_converter_;
int32_t offset_;
int32_t length_;
VALUE result_;
};

class FixedSizeListArrayValueConverter : public arrow::ArrayVisitor {
public:
explicit FixedSizeListArrayValueConverter(ArrayValueConverter* converter)
: array_value_converter_(converter),
offset_(0),
length_(0),
result_(Qnil) {}

VALUE convert(const arrow::FixedSizeListArray& array, const int64_t index) {
auto values = array.values().get();
auto offset_keep = offset_;
auto length_keep = length_;
offset_ = array.value_offset(index);
length_ = array.value_length(index);
auto result_keep = result_;
result_ = rb_ary_new_capa(length_);
check_status(values->Accept(this),
"[raw-records][fixed-size-list-array]");
offset_ = offset_keep;
length_ = length_keep;
auto result_return = result_;
result_ = result_keep;
return result_return;
}

#define VISIT(TYPE) \
arrow::Status Visit(const arrow::TYPE ## Array& array) override { \
return visit_value(array); \
}

VISIT(Null)
VISIT(Boolean)
VISIT(Int8)
VISIT(Int16)
VISIT(Int32)
VISIT(Int64)
VISIT(UInt8)
VISIT(UInt16)
VISIT(UInt32)
VISIT(UInt64)
VISIT(HalfFloat)
VISIT(Float)
VISIT(Double)
VISIT(Binary)
VISIT(String)
VISIT(FixedSizeBinary)
VISIT(Date32)
VISIT(Date64)
VISIT(Time32)
VISIT(Time64)
VISIT(Timestamp)
VISIT(MonthInterval)
VISIT(DayTimeInterval)
VISIT(MonthDayNanoInterval)
VISIT(Duration)
VISIT(List)
VISIT(LargeList)
VISIT(FixedSizeList)
VISIT(Struct)
VISIT(Map)
VISIT(SparseUnion)
Expand Down Expand Up @@ -593,6 +704,7 @@ namespace red_arrow {
VISIT(Duration)
VISIT(List)
VISIT(LargeList)
VISIT(FixedSizeList)
VISIT(Struct)
VISIT(Map)
VISIT(SparseUnion)
Expand Down Expand Up @@ -697,6 +809,7 @@ namespace red_arrow {
VISIT(Duration)
VISIT(List)
VISIT(LargeList)
VISIT(FixedSizeList)
VISIT(Struct)
VISIT(Map)
VISIT(SparseUnion)
Expand Down Expand Up @@ -802,6 +915,7 @@ namespace red_arrow {
VISIT(Duration)
VISIT(List)
VISIT(LargeList)
VISIT(FixedSizeList)
VISIT(Struct)
VISIT(Map)
VISIT(SparseUnion)
Expand Down Expand Up @@ -917,6 +1031,7 @@ namespace red_arrow {
VISIT(Duration)
VISIT(List)
VISIT(LargeList)
VISIT(FixedSizeList)
VISIT(Struct)
VISIT(Map)
VISIT(SparseUnion)
Expand Down Expand Up @@ -947,13 +1062,15 @@ namespace red_arrow {
: array_value_converter_(),
list_array_value_converter_(&array_value_converter_),
large_list_array_value_converter_(&array_value_converter_),
fixed_size_list_array_value_converter_(&array_value_converter_),
struct_array_value_converter_(&array_value_converter_),
map_array_value_converter_(&array_value_converter_),
union_array_value_converter_(&array_value_converter_),
dictionary_array_value_converter_(&array_value_converter_) {
array_value_converter_.
set_sub_value_converters(&list_array_value_converter_,
&large_list_array_value_converter_,
&fixed_size_list_array_value_converter_,
&struct_array_value_converter_,
&map_array_value_converter_,
&union_array_value_converter_,
Expand All @@ -969,6 +1086,7 @@ namespace red_arrow {
ArrayValueConverter array_value_converter_;
ListArrayValueConverter list_array_value_converter_;
LargeListArrayValueConverter large_list_array_value_converter_;
FixedSizeListArrayValueConverter fixed_size_list_array_value_converter_;
StructArrayValueConverter struct_array_value_converter_;
MapArrayValueConverter map_array_value_converter_;
UnionArrayValueConverter union_array_value_converter_;
Expand Down
4 changes: 4 additions & 0 deletions ruby/red-arrow/ext/arrow/raw-records.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ namespace red_arrow {
VISIT(MonthDayNanoInterval)
VISIT(Duration)
VISIT(List)
VISIT(LargeList)
VISIT(FixedSizeList)
VISIT(Struct)
VISIT(Map)
VISIT(SparseUnion)
Expand Down Expand Up @@ -241,6 +243,8 @@ namespace red_arrow {
VISIT(MonthDayNanoInterval)
VISIT(Duration)
VISIT(List)
VISIT(LargeList)
VISIT(FixedSizeList)
VISIT(Struct)
VISIT(Map)
VISIT(SparseUnion)
Expand Down
1 change: 1 addition & 0 deletions ruby/red-arrow/ext/arrow/values.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ namespace red_arrow {
VISIT(Duration)
VISIT(List)
VISIT(LargeList)
VISIT(FixedSizeList)
VISIT(Struct)
VISIT(Map)
VISIT(SparseUnion)
Expand Down
118 changes: 118 additions & 0 deletions ruby/red-arrow/lib/arrow/fixed-size-list-data-type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# 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.

module Arrow
class FixedSizeListDataType
include ListFieldResolvable

alias_method :initialize_raw, :initialize
private :initialize_raw

# Creates a new {Arrow::FixedSizeListDataType}.
#
# @overload initialize(field, size)
#
# @param field [Arrow::Field, Hash] The field of the fixed size
# list data type. You can also specify field description by
# `Hash`.
#
# See {Arrow::Field.new} how to specify field description.
#
# @param size [Integer] The number of values in each element.
#
# @example Create a fixed size list data type with {Arrow::Field}
# visible_field = Arrow::Field.new("visible", :boolean)
# size = 2
# Arrow::FixedSizeListDataType.new(visible_field, size)
#
# @example Create a list data type with field description
# description = {name: "visible", type: :boolean}
# size = 2
# Arrow::FixedSizeListDataType.new(description, size)
#
# @overload initialize(description)
#
# @param description [Hash] The description of the fixed size
# list data type. It must have `:field` value and `:size`
# value.
#
# @option description [Arrow::Field, Hash] :field The field of
# the list data type. You can also specify field description
# by `Hash`.
#
# See {Arrow::Field.new} how to specify field description.
#
# @option description [Integer] :size The number of values of
# each element of the fixed size list data type.
#
# @example Create a fixed size list data type with {Arrow::Field}
# visible_field = Arrow::Field.new("visible", :boolean)
# Arrow::FixedSizeListDataType.new(field: visible_field, size: 2)
#
# @example Create a fixed size list data type with field description
# Arrow::FixedSizeListDataType.new(field: {
# name: "visible",
# type: :boolean,
# },
# size: 2)
#
# @overload initialize(data_type, size)
#
# @param data_type [Arrow::DataType, String, Symbol,
# ::Array<String>, ::Array<Symbol>, Hash] The element data
# type of the fixed size list data type. A field is created
# with the default name `"item"` from the data type
# automatically.
#
# See {Arrow::DataType.resolve} how to specify data type.
#
# @param size [Integer] The number of values in each
# element.
#
# @example Create a fixed size list data type with {Arrow::DataType}
# size = 2
# Arrow::FixedSizeListDataType.new(Arrow::BooleanDataType.new,
# size)
#
# @example Create a fixed size list data type with data type name as String
# size = 2
# Arrow::FixedSizeListDataType.new("boolean", size)
#
# @example Create a fixed size list data type with data type name as Symbol
# size = 2
# Arrow::FixedSizeListDataType.new(:boolean, size)
#
# @example Create a fixed size list data type with data type as Array
# size = 2
# Arrow::FixedSizeListDataType.new([:time32, :milli], size)
def initialize(*args)
n_args = args.size
case n_args
when 1
description = args[0]
size = description.delete(:size)
initialize_raw(resolve_field(description), size)
when 2
field, size = args
initialize_raw(resolve_field(field), size)
else
message = "wrong number of arguments (given #{n_args}, expected 1..2)"
raise ArgumentError, message
end
end
end
end
1 change: 1 addition & 0 deletions ruby/red-arrow/lib/arrow/libraries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
require_relative "fixed-size-binary-array"
require_relative "fixed-size-binary-array-builder"
require_relative "fixed-size-list-array-builder"
require_relative "fixed-size-list-data-type"
require_relative "function"
require_relative "group"
require_relative "half-float"
Expand Down
Loading
Loading