]> git.proxmox.com Git - ceph.git/blame - ceph/src/arrow/ruby/red-arrow/test/test-function.rb
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / ruby / red-arrow / test / test-function.rb
CommitLineData
1d09f67e
TL
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
18class FunctionTest < Test::Unit::TestCase
19 sub_test_case("#execute") do
20 test("Arrow::Array") do
21 or_function = Arrow::Function.find("or")
22 args = [
23 Arrow::BooleanArray.new([true, false, false]),
24 Arrow::BooleanArray.new([true, false, true]),
25 ]
26 assert_equal([true, false, true],
27 or_function.execute(args).value.to_a)
28 end
29
30 test("Array") do
31 or_function = Arrow::Function.find("or")
32 args = [
33 [true, false, false],
34 [true, false, true],
35 ]
36 assert_equal([true, false, true],
37 or_function.execute(args).value.to_a)
38 end
39
40 test("Arrow::ChunkedArray") do
41 or_function = Arrow::Function.find("or")
42 args = [
43 Arrow::ChunkedArray.new([
44 Arrow::BooleanArray.new([true]),
45 Arrow::BooleanArray.new([false, false]),
46 ]),
47 Arrow::ChunkedArray.new([
48 Arrow::BooleanArray.new([true, false]),
49 Arrow::BooleanArray.new([true]),
50 ]),
51 ]
52 assert_equal([true, false, true],
53 or_function.execute(args).value.to_a)
54 end
55
56 test("Arrow::Scalar") do
57 add_function = Arrow::Function.find("add")
58 args = [
59 Arrow::Int8Array.new([1, 2, 3]),
60 Arrow::Int8Scalar.new(5),
61 ]
62 assert_equal([6, 7, 8],
63 add_function.execute(args).value.to_a)
64 end
65
66 test("Integer") do
67 add_function = Arrow::Function.find("add")
68 args = [
69 [1, 2, 3],
70 5,
71 ]
72 assert_equal([6, 7, 8],
73 add_function.execute(args).value.to_a)
74 end
75
76 test("Float") do
77 add_function = Arrow::Function.find("add")
78 args = [
79 [1, 2, 3],
80 5.1,
81 ]
82 assert_equal([6.1, 7.1, 8.1],
83 add_function.execute(args).value.to_a)
84 end
85
86 test("true") do
87 and_function = Arrow::Function.find("and")
88 args = [
89 Arrow::BooleanArray.new([true, false, false]),
90 true,
91 ]
92 assert_equal([true, false, false],
93 and_function.execute(args).value.to_a)
94 end
95
96 test("false") do
97 or_function = Arrow::Function.find("or")
98 args = [
99 Arrow::BooleanArray.new([true, false, false]),
100 false,
101 ]
102 assert_equal([true, false, false],
103 or_function.execute(args).value.to_a)
104 end
105
106 test("String") do
107 ascii_upper_function = Arrow::Function.find("ascii_upper")
108 args = [
109 "Hello",
110 ]
111 assert_equal("HELLO",
112 ascii_upper_function.execute(args).value.to_s)
113 end
114
115 test("Date") do
116 cast_function = Arrow::Function.find("cast")
117 date = Date.new(2021, 6, 12)
118 args = [date]
119 options = Arrow::CastOptions.new
120 options.to_data_type = Arrow::TimestampDataType.new(:second)
121 time = Time.utc(date.year,
122 date.month,
123 date.day)
124 assert_equal(Arrow::TimestampScalar.new(options.to_data_type,
125 time.to_i),
126 cast_function.execute(args, options).value)
127 end
128
129 test("Arrow::Time: second") do
130 cast_function = Arrow::Function.find("cast")
131 arrow_time = Arrow::Time.new(Arrow::TimeUnit::SECOND,
132 # 00:10:00
133 60 * 10)
134 args = [arrow_time]
135 options = Arrow::CastOptions.new
136 options.to_data_type = Arrow::Time64DataType.new(:micro)
137 assert_equal(Arrow::Time64Scalar.new(options.to_data_type,
138 # 00:10:00.000000
139 60 * 10 * 1000 * 1000),
140 cast_function.execute(args, options).value)
141 end
142
143 test("Arrow::Time: micro") do
144 cast_function = Arrow::Function.find("cast")
145 arrow_time = Arrow::Time.new(Arrow::TimeUnit::MICRO,
146 # 00:10:00.000000
147 60 * 10 * 1000 * 1000)
148 args = [arrow_time]
149 options = Arrow::CastOptions.new
150 options.to_data_type = Arrow::Time32DataType.new(:second)
151 options.allow_time_truncate = true
152 assert_equal(Arrow::Time32Scalar.new(options.to_data_type,
153 # 00:10:00
154 60 * 10),
155 cast_function.execute(args, options).value)
156 end
157
158 test("Time") do
159 cast_function = Arrow::Function.find("cast")
160 time = Time.utc(2021, 6, 12, 1, 2, 3, 1)
161 args = [time]
162 options = Arrow::CastOptions.new
163 options.to_data_type = Arrow::TimestampDataType.new(:second)
164 options.allow_time_truncate = true
165 time = Time.utc(time.year,
166 time.month,
167 time.day,
168 time.hour,
169 time.min,
170 time.sec)
171 assert_equal(Arrow::TimestampScalar.new(options.to_data_type,
172 time.to_i),
173 cast_function.execute(args, options).value)
174 end
175 end
176end