]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/c_glib/test/test-cast.rb
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / c_glib / test / test-cast.rb
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 TestCast < Test::Unit::TestCase
19 include Helper::Buildable
20 include Helper::Omittable
21
22 def test_safe
23 data = [-1, 2, nil]
24 assert_equal(build_int32_array(data),
25 build_int8_array(data).cast(Arrow::Int32DataType.new))
26 end
27
28 sub_test_case("allow-int-overflow") do
29 def test_default
30 assert_raise(Arrow::Error::Invalid) do
31 build_int32_array([128]).cast(Arrow::Int8DataType.new)
32 end
33 end
34
35 def test_true
36 options = Arrow::CastOptions.new
37 options.allow_int_overflow = true
38 assert_equal(build_int8_array([-128]),
39 build_int32_array([128]).cast(Arrow::Int8DataType.new,
40 options))
41 end
42 end
43
44 sub_test_case("allow-time-truncate") do
45 def test_default
46 after_epoch_in_milli = 1504953190854 # 2017-09-09T10:33:10.854Z
47 second_timestamp_data_type = Arrow::TimestampDataType.new(:second)
48 milli_array = build_timestamp_array(:milli, [after_epoch_in_milli])
49 assert_raise(Arrow::Error::Invalid) do
50 milli_array.cast(second_timestamp_data_type)
51 end
52 end
53
54 def test_true
55 options = Arrow::CastOptions.new
56 options.allow_time_truncate = true
57 after_epoch_in_milli = 1504953190854 # 2017-09-09T10:33:10.854Z
58 second_array = build_timestamp_array(:second,
59 [after_epoch_in_milli / 1000])
60 milli_array = build_timestamp_array(:milli, [after_epoch_in_milli])
61 second_timestamp_data_type = Arrow::TimestampDataType.new(:second)
62 assert_equal(second_array,
63 milli_array.cast(second_timestamp_data_type, options))
64 end
65 end
66
67 sub_test_case("allow-time-overflow") do
68 def test_default
69 after_epoch_in_second = 95617584000 # 5000-01-01T00:00:00Z
70 nano_timestamp_data_type = Arrow::TimestampDataType.new(:nano)
71 second_array = build_timestamp_array(:second, [after_epoch_in_second])
72 assert_raise(Arrow::Error::Invalid) do
73 second_array.cast(nano_timestamp_data_type)
74 end
75 end
76
77 def test_true
78 options = Arrow::CastOptions.new
79 options.allow_time_overflow = true
80 after_epoch_in_second = 95617584000 # 5000-01-01T00:00:00Z
81 second_array = build_timestamp_array(:second,
82 [after_epoch_in_second])
83 after_epoch_in_nano_overflowed =
84 (after_epoch_in_second * 1000 * 1000 * 1000) % (2 ** 64)
85 nano_array = build_timestamp_array(:nano,
86 [after_epoch_in_nano_overflowed])
87 nano_timestamp_data_type = Arrow::TimestampDataType.new(:nano)
88 assert_equal(nano_array,
89 second_array.cast(nano_timestamp_data_type, options))
90 end
91 end
92
93 sub_test_case("allow-decimal-truncate") do
94 def test_default
95 decimal128_data_type = Arrow::Decimal128DataType.new(8, 2)
96 decimal128_array = build_decimal128_array(decimal128_data_type,
97 ["23423445"])
98 assert_raise(Arrow::Error::Invalid) do
99 decimal128_array.cast(Arrow::Int64DataType.new)
100 end
101 end
102
103 def test_true
104 options = Arrow::CastOptions.new
105 options.allow_decimal_truncate = true
106 decimal128_data_type = Arrow::Decimal128DataType.new(8, 2)
107 decimal128_array = build_decimal128_array(decimal128_data_type,
108 ["23423445"])
109 assert_equal(build_int64_array([234234]),
110 decimal128_array.cast(Arrow::Int64DataType.new, options))
111 end
112 end
113
114 sub_test_case("allow-float-truncate") do
115 def test_default
116 assert_raise(Arrow::Error::Invalid) do
117 build_float_array([1.1]).cast(Arrow::Int8DataType.new)
118 end
119 end
120
121 def test_true
122 options = Arrow::CastOptions.new
123 options.allow_float_truncate = true
124 int8_data_type = Arrow::Int8DataType.new
125 assert_equal(build_int8_array([1]),
126 build_float_array([1.1]).cast(int8_data_type, options))
127 end
128 end
129
130 sub_test_case("allow-invalid-utf8") do
131 def test_default
132 assert_raise(Arrow::Error::Invalid) do
133 build_binary_array(["\xff"]).cast(Arrow::StringDataType.new)
134 end
135 end
136
137 def test_true
138 options = Arrow::CastOptions.new
139 options.allow_invalid_utf8 = true
140 string_data_type = Arrow::StringDataType.new
141 assert_equal(build_string_array(["\xff"]),
142 build_binary_array(["\xff"]).cast(string_data_type, options))
143 end
144 end
145 end