Skip to content

Commit 2527980

Browse files
committed
Implement Arrow::MakeStructOptions.try_convert
1 parent 425b208 commit 2527980

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

ruby/red-arrow/lib/arrow/libraries.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
require_relative "list-array-builder"
8282
require_relative "list-data-type"
8383
require_relative "list-slice-options"
84+
require_relative "make-struct-options"
8485
require_relative "map-array"
8586
require_relative "map-array-builder"
8687
require_relative "map-data-type"
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
module Arrow
19+
class MakeStructOptions
20+
class << self
21+
def try_convert(value)
22+
case value
23+
when Hash
24+
options = new
25+
field_names = value[:field_names] || []
26+
field_nullability = value[:field_nullability] || []
27+
field_metadata = value[:field_metadata] || []
28+
field_names.zip(field_nullability, field_metadata) do |name, nullability, metadata|
29+
options.add_field(name, nullability, metadata)
30+
end
31+
options
32+
else
33+
nil
34+
end
35+
end
36+
end
37+
end
38+
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
class TestMakeStructOptions < Test::Unit::TestCase
19+
def test_make_struct_function
20+
a = Arrow::Int8Array.new([1, 2, 3])
21+
b = Arrow::BooleanArray.new([true, false, nil])
22+
metadata1 = {"a" => "b"}
23+
metadata2 = {"c" => "d"}
24+
options = {
25+
field_names: ["a", "b"],
26+
field_nullability: [false, true],
27+
field_metadata: [metadata1, metadata2]
28+
}
29+
args = [a, b]
30+
make_struct_function = Arrow::Function.find("make_struct")
31+
result = make_struct_function.execute(args, options).value
32+
33+
expected = Arrow::StructArray.new(
34+
Arrow::StructDataType.new(
35+
[
36+
Arrow::Field.new("a", Arrow::Int8DataType.new, false),
37+
Arrow::Field.new("b", Arrow::BooleanDataType.new, true),
38+
]
39+
),
40+
[
41+
{"a" => 1, "b" => true},
42+
{"a" => 2, "b" => false},
43+
{"a" => 3, "b" => nil},
44+
]
45+
)
46+
assert_equal(expected, result)
47+
fields = result.value_data_type.fields
48+
assert_equal(metadata1, fields[0].metadata)
49+
assert_equal(metadata2, fields[1].metadata)
50+
end
51+
end

0 commit comments

Comments
 (0)