Skip to content

Commit

Permalink
[GLib][Ruby] Follow DictionaryArray changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kou authored and wesm committed May 17, 2019
1 parent 89e274d commit 9f1ccfb
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 46 deletions.
30 changes: 21 additions & 9 deletions c_glib/arrow-glib/composite-array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,25 +534,37 @@ garrow_dictionary_array_class_init(GArrowDictionaryArrayClass *klass)

/**
* garrow_dictionary_array_new:
* @data_type: The data type of dictionary.
* @data_type: The data type of the dictionary array.
* @indices: The indices of values in dictionary.
* @dictionary: The dictionary of the dictionary array.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Returns: A newly created #GArrowDictionaryArray.
* Returns: (nullable): A newly created #GArrowDictionaryArray
* or %NULL on error.
*
* Since: 0.8.0
*/
GArrowDictionaryArray *
garrow_dictionary_array_new(GArrowDataType *data_type,
GArrowArray *indices)
GArrowArray *indices,
GArrowArray *dictionary,
GError **error)
{
const auto arrow_data_type = garrow_data_type_get_raw(data_type);
const auto arrow_indices = garrow_array_get_raw(indices);
auto arrow_dictionary_array =
std::make_shared<arrow::DictionaryArray>(arrow_data_type,
arrow_indices);
auto arrow_array =
std::static_pointer_cast<arrow::Array>(arrow_dictionary_array);
return GARROW_DICTIONARY_ARRAY(garrow_array_new_raw(&arrow_array));
const auto arrow_dictionary = garrow_array_get_raw(dictionary);
std::shared_ptr<arrow::Array> arrow_dictionary_array;
auto status = arrow::DictionaryArray::FromArrays(arrow_data_type,
arrow_indices,
arrow_dictionary,
&arrow_dictionary_array);
if (garrow_error_check(error, status, "[dictionary-array][new]")) {
auto arrow_array =
std::static_pointer_cast<arrow::Array>(arrow_dictionary_array);
return GARROW_DICTIONARY_ARRAY(garrow_array_new_raw(&arrow_array));
} else {
return NULL;
}
}

/**
Expand Down
5 changes: 4 additions & 1 deletion c_glib/arrow-glib/composite-array.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ struct _GArrowDictionaryArrayClass
};

GArrowDictionaryArray *
garrow_dictionary_array_new(GArrowDataType *data_type, GArrowArray *indices);
garrow_dictionary_array_new(GArrowDataType *data_type,
GArrowArray *indices,
GArrowArray *dictionary,
GError **error);
GArrowArray *
garrow_dictionary_array_get_indices(GArrowDictionaryArray *array);
GArrowArray *
Expand Down
22 changes: 11 additions & 11 deletions c_glib/arrow-glib/composite-data-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ garrow_dictionary_data_type_class_init(GArrowDictionaryDataTypeClass *klass)
/**
* garrow_dictionary_data_type_new:
* @index_data_type: The data type of index.
* @dictionary: The dictionary.
* @value_data_type: The data type of dictionary values.
* @ordered: Whether dictionary contents are ordered or not.
*
* Returns: The newly created dictionary data type.
Expand All @@ -522,13 +522,13 @@ garrow_dictionary_data_type_class_init(GArrowDictionaryDataTypeClass *klass)
*/
GArrowDictionaryDataType *
garrow_dictionary_data_type_new(GArrowDataType *index_data_type,
GArrowArray *dictionary,
GArrowDataType *value_data_type,
gboolean ordered)
{
auto arrow_index_data_type = garrow_data_type_get_raw(index_data_type);
auto arrow_dictionary = garrow_array_get_raw(dictionary);
auto arrow_value_data_type = garrow_data_type_get_raw(value_data_type);
auto arrow_data_type = arrow::dictionary(arrow_index_data_type,
arrow_dictionary,
arrow_value_data_type,
ordered);
return GARROW_DICTIONARY_DATA_TYPE(garrow_data_type_new_raw(&arrow_data_type));
}
Expand All @@ -552,21 +552,21 @@ garrow_dictionary_data_type_get_index_data_type(GArrowDictionaryDataType *dictio
}

/**
* garrow_dictionary_data_type_get_dictionary:
* garrow_dictionary_data_type_get_value_data_type:
* @dictionary_data_type: The #GArrowDictionaryDataType.
*
* Returns: (transfer full): The dictionary as #GArrowArray.
* Returns: (transfer full): The #GArrowDataType of dictionary values.
*
* Since: 0.8.0
* Since: 0.14.0
*/
GArrowArray *
garrow_dictionary_data_type_get_dictionary(GArrowDictionaryDataType *dictionary_data_type)
GArrowDataType *
garrow_dictionary_data_type_get_value_data_type(GArrowDictionaryDataType *dictionary_data_type)
{
auto arrow_data_type = garrow_data_type_get_raw(GARROW_DATA_TYPE(dictionary_data_type));
auto arrow_dictionary_data_type =
std::static_pointer_cast<arrow::DictionaryType>(arrow_data_type);
auto arrow_dictionary = arrow_dictionary_data_type->dictionary();
return garrow_array_new_raw(&arrow_dictionary);
auto arrow_value_data_type = arrow_dictionary_data_type->value_type();
return garrow_data_type_new_raw(&arrow_value_data_type);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions c_glib/arrow-glib/composite-data-type.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,13 @@ struct _GArrowDictionaryDataTypeClass

GArrowDictionaryDataType *
garrow_dictionary_data_type_new(GArrowDataType *index_data_type,
GArrowArray *dictionary,
GArrowDataType *value_data_type,
gboolean ordered);
GArrowDataType *
garrow_dictionary_data_type_get_index_data_type(GArrowDictionaryDataType *dictionary_data_type);
GArrowArray *
garrow_dictionary_data_type_get_dictionary(GArrowDictionaryDataType *dictionary_data_type);
GARROW_AVAILABLE_IN_0_14
GArrowDataType *
garrow_dictionary_data_type_get_value_data_type(GArrowDictionaryDataType *dictionary_data_type);
gboolean
garrow_dictionary_data_type_is_ordered(GArrowDictionaryDataType *dictionary_data_type);

Expand Down
10 changes: 7 additions & 3 deletions c_glib/test/test-dictionary-array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ def setup
@dictionary = build_string_array(["C", "C++", "Ruby"])
@ordered = false
@data_type = Arrow::DictionaryDataType.new(@index_data_type,
@dictionary,
@dictionary.value_data_type,
@ordered)
end

sub_test_case(".new") do
def test_new
indices = build_int32_array([0, 2, 2, 1, 0])
dictionary_array = Arrow::DictionaryArray.new(@data_type, indices)
dictionary_array = Arrow::DictionaryArray.new(@data_type,
indices,
@dictionary)
assert_equal(<<-STRING.chomp, dictionary_array.to_s)
-- dictionary:
Expand All @@ -55,7 +57,9 @@ def test_new
def setup
super
@indices = build_int32_array([0, 2, 2, 1, 0])
@dictionary_array = Arrow::DictionaryArray.new(@data_type, @indices)
@dictionary_array = Arrow::DictionaryArray.new(@data_type,
@indices,
@dictionary)
end

def test_indices
Expand Down
8 changes: 4 additions & 4 deletions c_glib/test/test-dictionary-data-type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class TestDictionaryDataType < Test::Unit::TestCase

def setup
@index_data_type = Arrow::Int32DataType.new
@dictionary = build_string_array(["C", "C++", "Ruby"])
@value_data_type = Arrow::StringDataType.new
@ordered = true
@data_type = Arrow::DictionaryDataType.new(@index_data_type,
@dictionary,
@value_data_type,
@ordered)
end

Expand All @@ -44,8 +44,8 @@ def test_index_data_type
assert_equal(@index_data_type, @data_type.index_data_type)
end

def test_dictionary
assert_equal(@dictionary, @data_type.dictionary)
def test_value_data_type
assert_equal(@value_data_type, @data_type.value_data_type)
end

def test_ordered?
Expand Down
35 changes: 23 additions & 12 deletions ruby/red-arrow/lib/arrow/dictionary-data-type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class DictionaryDataType

# Creates a new {Arrow::DictionaryDataType}.
#
# @overload initialize(index_data_type, dictionary, ordered)
# @overload initialize(index_data_type, value_data_type, ordered)
#
# @param index_data_type [Arrow::DataType, Hash, String, Symbol]
# The index data type of the dictionary data type. It must be
Expand All @@ -39,18 +39,23 @@ class DictionaryDataType
# See {Arrow::DataType.resolve} how to specify data type
# description.
#
# @param dictionary [Arrow::Array] The real values of the
# dictionary data type.
# @param value_data_type [Arrow::DataType, Hash, String, Symbol]
# The value data type of the dictionary data type.
#
# You can specify data type as a description by `Hash`.
#
# See {Arrow::DataType.resolve} how to specify data type
# description.
#
# @param ordered [Boolean] Whether dictionary contents are
# ordered or not.
#
# @example Create a dictionary data type for {0: "Hello", 1: "World"}
# index_data_type = :int8
# dictionary = Arrow::StringArray.new(["Hello", "World"])
# value_data_type = :string
# ordered = true
# Arrow::DictionaryDataType.new(index_data_type,
# dictionary,
# value_data_type,
# ordered)
#
# @overload initialize(description)
Expand All @@ -74,33 +79,39 @@ class DictionaryDataType
# See {Arrow::DataType.resolve} how to specify data type
# description.
#
# @option description [Arrow::Array] :dictionary The real values
# of the dictionary data type.
# @option description [Arrow::DataType, Hash, String, Symbol]
# :value_data_type
# The value data type of the dictionary data type.
#
# You can specify data type as a description by `Hash`.
#
# See {Arrow::DataType.resolve} how to specify data type
# description.
#
# @option description [Boolean] :ordered Whether dictionary
# contents are ordered or not.
#
# @example Create a dictionary data type for {0: "Hello", 1: "World"}
# dictionary = Arrow::StringArray.new(["Hello", "World"])
# Arrow::DictionaryDataType.new(index_data_type: :int8,
# dictionary: dictionary,
# value_data_type: :string,
# ordered: true)
def initialize(*args)
n_args = args.size
case n_args
when 1
description = args[0]
index_data_type = description[:index_data_type]
dictionary = description[:dictionary]
value_data_type = description[:value_data_type]
ordered = description[:ordered]
when 3
index_data_type, dictionary, ordered = args
index_data_type, value_data_type, ordered = args
else
message = "wrong number of arguments (given, #{n_args}, expected 1 or 3)"
raise ArgumentError, message
end
index_data_type = DataType.resolve(index_data_type)
initialize_raw(index_data_type, dictionary, ordered)
value_data_type = DataType.resolve(value_data_type)
initialize_raw(index_data_type, value_data_type, ordered)
end
end
end
6 changes: 3 additions & 3 deletions ruby/red-arrow/test/test-dictionary-data-type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ class DictionaryDataTypeTest < Test::Unit::TestCase
sub_test_case(".new") do
def setup
@index_data_type = :int8
@dictionary = Arrow::StringArray.new(["Hello", "World"])
@value_data_type = :string
@ordered = true
end

test("ordered arguments") do
assert_equal("dictionary<values=string, indices=int8, ordered=1>",
Arrow::DictionaryDataType.new(@index_data_type,
@dictionary,
@value_data_type,
@ordered).to_s)
end

test("description") do
assert_equal("dictionary<values=string, indices=int8, ordered=1>",
Arrow::DictionaryDataType.new(index_data_type: @index_data_type,
dictionary: @dictionary,
value_data_type: @value_data_type,
ordered: @ordered).to_s)
end
end
Expand Down

0 comments on commit 9f1ccfb

Please sign in to comment.