Skip to content

Commit 9f1ccfb

Browse files
kouwesm
authored andcommitted
[GLib][Ruby] Follow DictionaryArray changes
1 parent 89e274d commit 9f1ccfb

File tree

8 files changed

+77
-46
lines changed

8 files changed

+77
-46
lines changed

c_glib/arrow-glib/composite-array.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -534,25 +534,37 @@ garrow_dictionary_array_class_init(GArrowDictionaryArrayClass *klass)
534534

535535
/**
536536
* garrow_dictionary_array_new:
537-
* @data_type: The data type of dictionary.
537+
* @data_type: The data type of the dictionary array.
538538
* @indices: The indices of values in dictionary.
539+
* @dictionary: The dictionary of the dictionary array.
540+
* @error: (nullable): Return location for a #GError or %NULL.
539541
*
540-
* Returns: A newly created #GArrowDictionaryArray.
542+
* Returns: (nullable): A newly created #GArrowDictionaryArray
543+
* or %NULL on error.
541544
*
542545
* Since: 0.8.0
543546
*/
544547
GArrowDictionaryArray *
545548
garrow_dictionary_array_new(GArrowDataType *data_type,
546-
GArrowArray *indices)
549+
GArrowArray *indices,
550+
GArrowArray *dictionary,
551+
GError **error)
547552
{
548553
const auto arrow_data_type = garrow_data_type_get_raw(data_type);
549554
const auto arrow_indices = garrow_array_get_raw(indices);
550-
auto arrow_dictionary_array =
551-
std::make_shared<arrow::DictionaryArray>(arrow_data_type,
552-
arrow_indices);
553-
auto arrow_array =
554-
std::static_pointer_cast<arrow::Array>(arrow_dictionary_array);
555-
return GARROW_DICTIONARY_ARRAY(garrow_array_new_raw(&arrow_array));
555+
const auto arrow_dictionary = garrow_array_get_raw(dictionary);
556+
std::shared_ptr<arrow::Array> arrow_dictionary_array;
557+
auto status = arrow::DictionaryArray::FromArrays(arrow_data_type,
558+
arrow_indices,
559+
arrow_dictionary,
560+
&arrow_dictionary_array);
561+
if (garrow_error_check(error, status, "[dictionary-array][new]")) {
562+
auto arrow_array =
563+
std::static_pointer_cast<arrow::Array>(arrow_dictionary_array);
564+
return GARROW_DICTIONARY_ARRAY(garrow_array_new_raw(&arrow_array));
565+
} else {
566+
return NULL;
567+
}
556568
}
557569

558570
/**

c_glib/arrow-glib/composite-array.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,10 @@ struct _GArrowDictionaryArrayClass
151151
};
152152

153153
GArrowDictionaryArray *
154-
garrow_dictionary_array_new(GArrowDataType *data_type, GArrowArray *indices);
154+
garrow_dictionary_array_new(GArrowDataType *data_type,
155+
GArrowArray *indices,
156+
GArrowArray *dictionary,
157+
GError **error);
155158
GArrowArray *
156159
garrow_dictionary_array_get_indices(GArrowDictionaryArray *array);
157160
GArrowArray *

c_glib/arrow-glib/composite-data-type.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ garrow_dictionary_data_type_class_init(GArrowDictionaryDataTypeClass *klass)
513513
/**
514514
* garrow_dictionary_data_type_new:
515515
* @index_data_type: The data type of index.
516-
* @dictionary: The dictionary.
516+
* @value_data_type: The data type of dictionary values.
517517
* @ordered: Whether dictionary contents are ordered or not.
518518
*
519519
* Returns: The newly created dictionary data type.
@@ -522,13 +522,13 @@ garrow_dictionary_data_type_class_init(GArrowDictionaryDataTypeClass *klass)
522522
*/
523523
GArrowDictionaryDataType *
524524
garrow_dictionary_data_type_new(GArrowDataType *index_data_type,
525-
GArrowArray *dictionary,
525+
GArrowDataType *value_data_type,
526526
gboolean ordered)
527527
{
528528
auto arrow_index_data_type = garrow_data_type_get_raw(index_data_type);
529-
auto arrow_dictionary = garrow_array_get_raw(dictionary);
529+
auto arrow_value_data_type = garrow_data_type_get_raw(value_data_type);
530530
auto arrow_data_type = arrow::dictionary(arrow_index_data_type,
531-
arrow_dictionary,
531+
arrow_value_data_type,
532532
ordered);
533533
return GARROW_DICTIONARY_DATA_TYPE(garrow_data_type_new_raw(&arrow_data_type));
534534
}
@@ -552,21 +552,21 @@ garrow_dictionary_data_type_get_index_data_type(GArrowDictionaryDataType *dictio
552552
}
553553

554554
/**
555-
* garrow_dictionary_data_type_get_dictionary:
555+
* garrow_dictionary_data_type_get_value_data_type:
556556
* @dictionary_data_type: The #GArrowDictionaryDataType.
557557
*
558-
* Returns: (transfer full): The dictionary as #GArrowArray.
558+
* Returns: (transfer full): The #GArrowDataType of dictionary values.
559559
*
560-
* Since: 0.8.0
560+
* Since: 0.14.0
561561
*/
562-
GArrowArray *
563-
garrow_dictionary_data_type_get_dictionary(GArrowDictionaryDataType *dictionary_data_type)
562+
GArrowDataType *
563+
garrow_dictionary_data_type_get_value_data_type(GArrowDictionaryDataType *dictionary_data_type)
564564
{
565565
auto arrow_data_type = garrow_data_type_get_raw(GARROW_DATA_TYPE(dictionary_data_type));
566566
auto arrow_dictionary_data_type =
567567
std::static_pointer_cast<arrow::DictionaryType>(arrow_data_type);
568-
auto arrow_dictionary = arrow_dictionary_data_type->dictionary();
569-
return garrow_array_new_raw(&arrow_dictionary);
568+
auto arrow_value_data_type = arrow_dictionary_data_type->value_type();
569+
return garrow_data_type_new_raw(&arrow_value_data_type);
570570
}
571571

572572
/**

c_glib/arrow-glib/composite-data-type.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,13 @@ struct _GArrowDictionaryDataTypeClass
145145

146146
GArrowDictionaryDataType *
147147
garrow_dictionary_data_type_new(GArrowDataType *index_data_type,
148-
GArrowArray *dictionary,
148+
GArrowDataType *value_data_type,
149149
gboolean ordered);
150150
GArrowDataType *
151151
garrow_dictionary_data_type_get_index_data_type(GArrowDictionaryDataType *dictionary_data_type);
152-
GArrowArray *
153-
garrow_dictionary_data_type_get_dictionary(GArrowDictionaryDataType *dictionary_data_type);
152+
GARROW_AVAILABLE_IN_0_14
153+
GArrowDataType *
154+
garrow_dictionary_data_type_get_value_data_type(GArrowDictionaryDataType *dictionary_data_type);
154155
gboolean
155156
garrow_dictionary_data_type_is_ordered(GArrowDictionaryDataType *dictionary_data_type);
156157

c_glib/test/test-dictionary-array.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@ def setup
2323
@dictionary = build_string_array(["C", "C++", "Ruby"])
2424
@ordered = false
2525
@data_type = Arrow::DictionaryDataType.new(@index_data_type,
26-
@dictionary,
26+
@dictionary.value_data_type,
2727
@ordered)
2828
end
2929

3030
sub_test_case(".new") do
3131
def test_new
3232
indices = build_int32_array([0, 2, 2, 1, 0])
33-
dictionary_array = Arrow::DictionaryArray.new(@data_type, indices)
33+
dictionary_array = Arrow::DictionaryArray.new(@data_type,
34+
indices,
35+
@dictionary)
3436
assert_equal(<<-STRING.chomp, dictionary_array.to_s)
3537
3638
-- dictionary:
@@ -55,7 +57,9 @@ def test_new
5557
def setup
5658
super
5759
@indices = build_int32_array([0, 2, 2, 1, 0])
58-
@dictionary_array = Arrow::DictionaryArray.new(@data_type, @indices)
60+
@dictionary_array = Arrow::DictionaryArray.new(@data_type,
61+
@indices,
62+
@dictionary)
5963
end
6064

6165
def test_indices

c_glib/test/test-dictionary-data-type.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ class TestDictionaryDataType < Test::Unit::TestCase
2020

2121
def setup
2222
@index_data_type = Arrow::Int32DataType.new
23-
@dictionary = build_string_array(["C", "C++", "Ruby"])
23+
@value_data_type = Arrow::StringDataType.new
2424
@ordered = true
2525
@data_type = Arrow::DictionaryDataType.new(@index_data_type,
26-
@dictionary,
26+
@value_data_type,
2727
@ordered)
2828
end
2929

@@ -44,8 +44,8 @@ def test_index_data_type
4444
assert_equal(@index_data_type, @data_type.index_data_type)
4545
end
4646

47-
def test_dictionary
48-
assert_equal(@dictionary, @data_type.dictionary)
47+
def test_value_data_type
48+
assert_equal(@value_data_type, @data_type.value_data_type)
4949
end
5050

5151
def test_ordered?

ruby/red-arrow/lib/arrow/dictionary-data-type.rb

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class DictionaryDataType
2222

2323
# Creates a new {Arrow::DictionaryDataType}.
2424
#
25-
# @overload initialize(index_data_type, dictionary, ordered)
25+
# @overload initialize(index_data_type, value_data_type, ordered)
2626
#
2727
# @param index_data_type [Arrow::DataType, Hash, String, Symbol]
2828
# The index data type of the dictionary data type. It must be
@@ -39,18 +39,23 @@ class DictionaryDataType
3939
# See {Arrow::DataType.resolve} how to specify data type
4040
# description.
4141
#
42-
# @param dictionary [Arrow::Array] The real values of the
43-
# dictionary data type.
42+
# @param value_data_type [Arrow::DataType, Hash, String, Symbol]
43+
# The value data type of the dictionary data type.
44+
#
45+
# You can specify data type as a description by `Hash`.
46+
#
47+
# See {Arrow::DataType.resolve} how to specify data type
48+
# description.
4449
#
4550
# @param ordered [Boolean] Whether dictionary contents are
4651
# ordered or not.
4752
#
4853
# @example Create a dictionary data type for {0: "Hello", 1: "World"}
4954
# index_data_type = :int8
50-
# dictionary = Arrow::StringArray.new(["Hello", "World"])
55+
# value_data_type = :string
5156
# ordered = true
5257
# Arrow::DictionaryDataType.new(index_data_type,
53-
# dictionary,
58+
# value_data_type,
5459
# ordered)
5560
#
5661
# @overload initialize(description)
@@ -74,33 +79,39 @@ class DictionaryDataType
7479
# See {Arrow::DataType.resolve} how to specify data type
7580
# description.
7681
#
77-
# @option description [Arrow::Array] :dictionary The real values
78-
# of the dictionary data type.
82+
# @option description [Arrow::DataType, Hash, String, Symbol]
83+
# :value_data_type
84+
# The value data type of the dictionary data type.
85+
#
86+
# You can specify data type as a description by `Hash`.
87+
#
88+
# See {Arrow::DataType.resolve} how to specify data type
89+
# description.
7990
#
8091
# @option description [Boolean] :ordered Whether dictionary
8192
# contents are ordered or not.
8293
#
8394
# @example Create a dictionary data type for {0: "Hello", 1: "World"}
84-
# dictionary = Arrow::StringArray.new(["Hello", "World"])
8595
# Arrow::DictionaryDataType.new(index_data_type: :int8,
86-
# dictionary: dictionary,
96+
# value_data_type: :string,
8797
# ordered: true)
8898
def initialize(*args)
8999
n_args = args.size
90100
case n_args
91101
when 1
92102
description = args[0]
93103
index_data_type = description[:index_data_type]
94-
dictionary = description[:dictionary]
104+
value_data_type = description[:value_data_type]
95105
ordered = description[:ordered]
96106
when 3
97-
index_data_type, dictionary, ordered = args
107+
index_data_type, value_data_type, ordered = args
98108
else
99109
message = "wrong number of arguments (given, #{n_args}, expected 1 or 3)"
100110
raise ArgumentError, message
101111
end
102112
index_data_type = DataType.resolve(index_data_type)
103-
initialize_raw(index_data_type, dictionary, ordered)
113+
value_data_type = DataType.resolve(value_data_type)
114+
initialize_raw(index_data_type, value_data_type, ordered)
104115
end
105116
end
106117
end

ruby/red-arrow/test/test-dictionary-data-type.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@ class DictionaryDataTypeTest < Test::Unit::TestCase
1919
sub_test_case(".new") do
2020
def setup
2121
@index_data_type = :int8
22-
@dictionary = Arrow::StringArray.new(["Hello", "World"])
22+
@value_data_type = :string
2323
@ordered = true
2424
end
2525

2626
test("ordered arguments") do
2727
assert_equal("dictionary<values=string, indices=int8, ordered=1>",
2828
Arrow::DictionaryDataType.new(@index_data_type,
29-
@dictionary,
29+
@value_data_type,
3030
@ordered).to_s)
3131
end
3232

3333
test("description") do
3434
assert_equal("dictionary<values=string, indices=int8, ordered=1>",
3535
Arrow::DictionaryDataType.new(index_data_type: @index_data_type,
36-
dictionary: @dictionary,
36+
value_data_type: @value_data_type,
3737
ordered: @ordered).to_s)
3838
end
3939
end

0 commit comments

Comments
 (0)