Skip to content

Commit

Permalink
ARROW-4174: [Ruby] Add support for building composite array from raw …
Browse files Browse the repository at this point in the history
…Ruby objects

Author: Kouhei Sutou <[email protected]>

Closes #3327 from kou/ruby-array-builder and squashes the following commits:

20e5874 <Kouhei Sutou> Add support for old GObject Introspection
36b993b <Kouhei Sutou>  Add support for building composite array from raw Ruby objects
  • Loading branch information
kou authored and shiro615 committed Jan 7, 2019
1 parent 16460d3 commit 84e10b6
Show file tree
Hide file tree
Showing 16 changed files with 728 additions and 5 deletions.
21 changes: 21 additions & 0 deletions c_glib/arrow-glib/array-builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3863,6 +3863,27 @@ garrow_decimal128_array_builder_append_value(GArrowDecimal128ArrayBuilder *build
"[decimal128-array-builder][append-value]");
}

/**
* garrow_decimal128_array_builder_append_null:
* @builder: A #GArrowDecimal128ArrayBuilder.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Returns: %TRUE on success, %FALSE if there was an error.
*
* It appends a new NULL element.
*
* Since: 0.12.0
*/
gboolean
garrow_decimal128_array_builder_append_null(GArrowDecimal128ArrayBuilder *builder,
GError **error)
{
return garrow_array_builder_append_null<arrow::Decimal128Builder *>
(GARROW_ARRAY_BUILDER(builder),
error,
"[decimal128-array-builder][append-null]");
}

G_END_DECLS

GArrowArrayBuilder *
Expand Down
3 changes: 3 additions & 0 deletions c_glib/arrow-glib/array-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -1486,5 +1486,8 @@ GARROW_AVAILABLE_IN_0_12
gboolean garrow_decimal128_array_builder_append_value(GArrowDecimal128ArrayBuilder *builder,
GArrowDecimal128 *value,
GError **error);
GARROW_AVAILABLE_IN_0_12
gboolean garrow_decimal128_array_builder_append_null(GArrowDecimal128ArrayBuilder *builder,
GError **error);

G_END_DECLS
18 changes: 18 additions & 0 deletions c_glib/arrow-glib/decimal128.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,24 @@ garrow_decimal128_new_integer(const gint64 data)
return garrow_decimal128_new_raw(&arrow_decimal);
}

/**
* garrow_decimal128_equal:
* @decimal: A #GArrowDecimal128.
* @other_decimal: A #GArrowDecimal128 to be compared.
*
* Returns: %TRUE if both of them is the same value, %FALSE otherwise.
*
* Since: 0.12.0
*/
gboolean
garrow_decimal128_equal(GArrowDecimal128 *decimal,
GArrowDecimal128 *other_decimal)
{
const auto arrow_decimal = garrow_decimal128_get_raw(decimal);
const auto arrow_other_decimal = garrow_decimal128_get_raw(other_decimal);
return *arrow_decimal == *arrow_other_decimal;
}

/**
* garrow_decimal128_to_string_scale:
* @decimal: A #GArrowDecimal128.
Expand Down
4 changes: 4 additions & 0 deletions c_glib/arrow-glib/decimal128.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#pragma once

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

G_BEGIN_DECLS

Expand All @@ -37,6 +38,9 @@ struct _GArrowDecimal128Class

GArrowDecimal128 *garrow_decimal128_new_string(const gchar *data);
GArrowDecimal128 *garrow_decimal128_new_integer(const gint64 data);
GARROW_AVAILABLE_IN_0_12
gboolean garrow_decimal128_equal(GArrowDecimal128 *decimal,
GArrowDecimal128 *other_decimal);
gchar *garrow_decimal128_to_string_scale(GArrowDecimal128 *decimal,
gint32 scale);
gchar *garrow_decimal128_to_string(GArrowDecimal128 *decimal);
Expand Down
8 changes: 5 additions & 3 deletions ruby/red-arrow/lib/arrow/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ class Array

class << self
def new(*args)
return super if args.size != 1

builder_class_name = "#{name}Builder"
if const_defined?(builder_class_name)
builder_class = const_get(builder_class_name)
builder_class.build(*args)
if args.size == builder_class.method(:build).arity
builder_class.build(*args)
else
super
end
else
super
end
Expand Down
64 changes: 64 additions & 0 deletions ruby/red-arrow/lib/arrow/decimal128-array-builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# 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.

require "bigdecimal"

module Arrow
class Decimal128ArrayBuilder
class << self
def build(data_type, values)
builder = new(data_type)
builder.build(values)
end
end

alias_method :append_value_raw, :append_value
def append_value(value)
case value
when nil
return append_null
when String
value = Decimal128.new(value)
when Float
value = Decimal128.new(value.to_s)
when BigDecimal
value = Decimal128.new(value.to_s)
end
append_value_raw(value)
end

def append_values(values, is_valids=nil)
if is_valids
is_valids.each_with_index do |is_valid, i|
if is_valid
append_value(values[i])
else
append_null
end
end
else
values.each do |value|
if value.nil?
append_null
else
append_value(value)
end
end
end
end
end
end
2 changes: 1 addition & 1 deletion ruby/red-arrow/lib/arrow/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def initialize(*args)
name = args[0]
data_type = DataType.resolve(args[1])
else
message = "wrong number of arguments (given, #{n_args}, expected 1..2)"
message = "wrong number of arguments (given #{n_args}, expected 1..2)"
raise ArgumentError, message
end

Expand Down
86 changes: 86 additions & 0 deletions ruby/red-arrow/lib/arrow/list-array-builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# 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 ListArrayBuilder
class << self
def build(data_type, values)
builder = new(data_type)
builder.build(values)
end
end

alias_method :append_value_raw, :append_value

# @overload append_value
#
# Starts appending a list record. You also need to append list
# value by {#value_builder}.
#
# @overload append_value(list)
#
# Appends a list record including list value.
#
# @param value [nil, ::Array] The list value of the record.
#
# If this is `nil`, the list record is null.
#
# If this is `Array`, it's the list value of the record.
#
# @since 0.12.0
def append_value(*args)
n_args = args.size

case n_args
when 0
append_value_raw
when 1
value = args[0]
case value
when nil
append_null
when ::Array
append_value_raw
@value_builder ||= value_builder
@value_builder.append_values(value, nil)
else
message = "list value must be nil or Array: #{value.inspect}"
raise ArgumentError, message
end
else
message = "wrong number of arguments (given #{n_args}, expected 0..1)"
raise ArgumentError, message
end
end

def append_values(lists, is_valids=nil)
if is_valids
is_valids.each_with_index do |is_valid, i|
if is_valid
append_value(lists[i])
else
append_null
end
end
else
lists.each do |list|
append_value(list)
end
end
end
end
end
5 changes: 4 additions & 1 deletion ruby/red-arrow/lib/arrow/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ def require_libraries
require "arrow/date32-array-builder"
require "arrow/date64-array"
require "arrow/date64-array-builder"
require "arrow/decimal128-array-builder"
require "arrow/decimal128-data-type"
require "arrow/dense-union-data-type"
require "arrow/dictionary-data-type"
require "arrow/field"
require "arrow/file-output-stream"
require "arrow/list-array-builder"
require "arrow/list-data-type"
require "arrow/path-extension"
require "arrow/record"
Expand All @@ -59,6 +61,7 @@ def require_libraries
require "arrow/slicer"
require "arrow/sparse-union-data-type"
require "arrow/struct-array"
require "arrow/struct-array-builder"
require "arrow/struct-data-type"
require "arrow/table"
require "arrow/table-formatter"
Expand Down Expand Up @@ -101,7 +104,7 @@ def load_method_info(info, klass, method_name)
end
super(info, klass, method_name)
else
super
super
end
end
end
Expand Down
Loading

0 comments on commit 84e10b6

Please sign in to comment.