|
| 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 | + include Helper::Buildable |
| 20 | + |
| 21 | + def setup |
| 22 | + @options = Arrow::MakeStructOptions.new |
| 23 | + end |
| 24 | + |
| 25 | + def test_make_struct_function |
| 26 | + a = build_int8_array([1, 2, 3]) |
| 27 | + b = build_boolean_array([true, false, nil]) |
| 28 | + args = [ |
| 29 | + Arrow::ArrayDatum.new(a), |
| 30 | + Arrow::ArrayDatum.new(b), |
| 31 | + ] |
| 32 | + @options.add_field("a", true, nil) |
| 33 | + @options.add_field("b", true, nil) |
| 34 | + make_struct_function = Arrow::Function.find("make_struct") |
| 35 | + result = make_struct_function.execute(args, @options).value |
| 36 | + |
| 37 | + expected = build_struct_array( |
| 38 | + [ |
| 39 | + Arrow::Field.new("a", Arrow::Int8DataType.new), |
| 40 | + Arrow::Field.new("b", Arrow::BooleanDataType.new), |
| 41 | + ], |
| 42 | + [ |
| 43 | + {"a" => 1, "b" => true}, |
| 44 | + {"a" => 2, "b" => false}, |
| 45 | + {"a" => 3, "b" => nil}, |
| 46 | + ] |
| 47 | + ) |
| 48 | + assert_equal(expected, result) |
| 49 | + end |
| 50 | + |
| 51 | + def test_make_struct_function_with_nullability |
| 52 | + a = build_int8_array([1, 2, 3]) |
| 53 | + b = build_boolean_array([true, false, nil]) |
| 54 | + args = [ |
| 55 | + Arrow::ArrayDatum.new(a), |
| 56 | + Arrow::ArrayDatum.new(b), |
| 57 | + ] |
| 58 | + @options.add_field("a", false, nil) |
| 59 | + @options.add_field("b", true, nil) |
| 60 | + make_struct_function = Arrow::Function.find("make_struct") |
| 61 | + result = make_struct_function.execute(args, @options).value |
| 62 | + |
| 63 | + expected = build_struct_array( |
| 64 | + [ |
| 65 | + Arrow::Field.new("a", Arrow::Int8DataType.new, false), |
| 66 | + Arrow::Field.new("b", Arrow::BooleanDataType.new, true), |
| 67 | + ], |
| 68 | + [ |
| 69 | + {"a" => 1, "b" => true}, |
| 70 | + {"a" => 2, "b" => false}, |
| 71 | + {"a" => 3, "b" => nil}, |
| 72 | + ] |
| 73 | + ) |
| 74 | + assert_equal(expected, result) |
| 75 | + end |
| 76 | + |
| 77 | + def test_make_struct_function_with_metadata |
| 78 | + a = build_int8_array([1, 2, 3]) |
| 79 | + b = build_boolean_array([true, false, nil]) |
| 80 | + args = [ |
| 81 | + Arrow::ArrayDatum.new(a), |
| 82 | + Arrow::ArrayDatum.new(b), |
| 83 | + ] |
| 84 | + metadata1 = {"key1" => "value1"} |
| 85 | + metadata2 = {"key2" => "value2"} |
| 86 | + @options.add_field("a", true, metadata1) |
| 87 | + @options.add_field("b", true, metadata2) |
| 88 | + make_struct_function = Arrow::Function.find("make_struct") |
| 89 | + result = make_struct_function.execute(args, @options).value |
| 90 | + |
| 91 | + fields = result.value_data_type.fields |
| 92 | + assert_equal(metadata1, fields[0].metadata) |
| 93 | + assert_equal(metadata2, fields[1].metadata) |
| 94 | + end |
| 95 | +end |
0 commit comments