forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel_test.cc
More file actions
416 lines (332 loc) · 13.1 KB
/
Copy pathkernel_test.cc
File metadata and controls
416 lines (332 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include <memory>
#include <string>
#include <vector>
#include <gtest/gtest.h>
#include "arrow/array/util.h"
#include "arrow/compute/kernel.h"
#include "arrow/status.h"
#include "arrow/testing/gtest_util.h"
#include "arrow/type.h"
#include "arrow/util/key_value_metadata.h"
namespace arrow {
namespace compute {
// ----------------------------------------------------------------------
// TypeMatcher
TEST(TypeMatcher, SameTypeId) {
std::shared_ptr<TypeMatcher> matcher = match::SameTypeId(Type::DECIMAL);
ASSERT_TRUE(matcher->Matches(*decimal(12, 2)));
ASSERT_FALSE(matcher->Matches(*int8()));
ASSERT_EQ("Type::DECIMAL128", matcher->ToString());
ASSERT_TRUE(matcher->Equals(*matcher));
ASSERT_TRUE(matcher->Equals(*match::SameTypeId(Type::DECIMAL)));
ASSERT_FALSE(matcher->Equals(*match::SameTypeId(Type::TIMESTAMP)));
}
TEST(TypeMatcher, TimestampTypeUnit) {
auto matcher = match::TimestampTypeUnit(TimeUnit::MILLI);
auto matcher2 = match::Time32TypeUnit(TimeUnit::MILLI);
ASSERT_TRUE(matcher->Matches(*timestamp(TimeUnit::MILLI)));
ASSERT_TRUE(matcher->Matches(*timestamp(TimeUnit::MILLI, "utc")));
ASSERT_FALSE(matcher->Matches(*timestamp(TimeUnit::SECOND)));
ASSERT_FALSE(matcher->Matches(*time32(TimeUnit::MILLI)));
ASSERT_TRUE(matcher2->Matches(*time32(TimeUnit::MILLI)));
// Check ToString representation
ASSERT_EQ("timestamp(s)", match::TimestampTypeUnit(TimeUnit::SECOND)->ToString());
ASSERT_EQ("timestamp(ms)", match::TimestampTypeUnit(TimeUnit::MILLI)->ToString());
ASSERT_EQ("timestamp(us)", match::TimestampTypeUnit(TimeUnit::MICRO)->ToString());
ASSERT_EQ("timestamp(ns)", match::TimestampTypeUnit(TimeUnit::NANO)->ToString());
// Equals implementation
ASSERT_TRUE(matcher->Equals(*matcher));
ASSERT_TRUE(matcher->Equals(*match::TimestampTypeUnit(TimeUnit::MILLI)));
ASSERT_FALSE(matcher->Equals(*match::TimestampTypeUnit(TimeUnit::MICRO)));
ASSERT_FALSE(matcher->Equals(*match::Time32TypeUnit(TimeUnit::MILLI)));
}
// ----------------------------------------------------------------------
// InputType
TEST(InputType, AnyTypeConstructor) {
// Check the ANY_TYPE ctors
InputType ty;
ASSERT_EQ(InputType::ANY_TYPE, ty.kind());
}
TEST(InputType, Constructors) {
// Exact type constructor
InputType ty1(int8());
ASSERT_EQ(InputType::EXACT_TYPE, ty1.kind());
AssertTypeEqual(*int8(), *ty1.type());
InputType ty1_implicit = int8();
ASSERT_TRUE(ty1.Equals(ty1_implicit));
// Same type id constructor
InputType ty2(Type::DECIMAL);
ASSERT_EQ(InputType::USE_TYPE_MATCHER, ty2.kind());
ASSERT_EQ("Type::DECIMAL128", ty2.ToString());
ASSERT_TRUE(ty2.type_matcher().Matches(*decimal(12, 2)));
ASSERT_FALSE(ty2.type_matcher().Matches(*int16()));
// Implicit construction in a vector
std::vector<InputType> types = {int8(), InputType(Type::DECIMAL)};
ASSERT_TRUE(types[0].Equals(ty1));
ASSERT_TRUE(types[1].Equals(ty2));
// Copy constructor
InputType ty3 = ty1;
InputType ty4 = ty2;
ASSERT_TRUE(ty3.Equals(ty1));
ASSERT_TRUE(ty4.Equals(ty2));
// Move constructor
InputType ty5 = std::move(ty3);
InputType ty6 = std::move(ty4);
ASSERT_TRUE(ty5.Equals(ty1));
ASSERT_TRUE(ty6.Equals(ty2));
// ToString
ASSERT_EQ("int8", ty1.ToString());
ASSERT_EQ("Type::DECIMAL128", ty2.ToString());
InputType ty7(match::TimestampTypeUnit(TimeUnit::MICRO));
ASSERT_EQ("timestamp(us)", ty7.ToString());
InputType ty8;
ASSERT_EQ("any", ty8.ToString());
}
TEST(InputType, Equals) {
InputType t1 = int8();
InputType t2 = int8();
InputType t3 = int32();
InputType t5(Type::DECIMAL);
InputType t6(Type::DECIMAL);
ASSERT_TRUE(t1.Equals(t2));
ASSERT_EQ(t1, t2);
ASSERT_NE(t1, t3);
ASSERT_FALSE(t1.Equals(t5));
ASSERT_NE(t1, t5);
ASSERT_EQ(t5, t5);
ASSERT_EQ(t5, t6);
// NOTE: For the time being, we treat int32() and Type::INT32 as being
// different. This could obviously be fixed later to make these equivalent
ASSERT_NE(InputType(int8()), InputType(Type::INT32));
// Check that field metadata excluded from equality checks
InputType t9 = list(
field("item", utf8(), /*nullable=*/true, key_value_metadata({"foo"}, {"bar"})));
InputType t10 = list(field("item", utf8()));
ASSERT_TRUE(t9.Equals(t10));
}
TEST(InputType, Hash) {
InputType t0;
InputType t1 = int8();
InputType t2(Type::DECIMAL);
// These checks try to determine first of all whether Hash always returns the
// same value, and whether the elements of the type are all incorporated into
// the Hash
ASSERT_EQ(t0.Hash(), t0.Hash());
ASSERT_EQ(t1.Hash(), t1.Hash());
ASSERT_EQ(t2.Hash(), t2.Hash());
ASSERT_NE(t0.Hash(), t1.Hash());
ASSERT_NE(t0.Hash(), t2.Hash());
ASSERT_NE(t1.Hash(), t2.Hash());
}
TEST(InputType, Matches) {
InputType input1 = int8();
ASSERT_TRUE(input1.Matches(*int8()));
ASSERT_TRUE(input1.Matches(*int8()));
ASSERT_FALSE(input1.Matches(*int16()));
InputType input2(Type::DECIMAL);
ASSERT_TRUE(input2.Matches(*decimal(12, 2)));
auto ty2 = decimal(12, 2);
auto ty3 = float64();
ASSERT_OK_AND_ASSIGN(std::shared_ptr<Array> arr2, MakeArrayOfNull(ty2, 1));
ASSERT_OK_AND_ASSIGN(std::shared_ptr<Array> arr3, MakeArrayOfNull(ty3, 1));
ASSERT_OK_AND_ASSIGN(std::shared_ptr<Scalar> scalar2, arr2->GetScalar(0));
ASSERT_TRUE(input2.Matches(Datum(arr2)));
ASSERT_TRUE(input2.Matches(Datum(scalar2)));
ASSERT_FALSE(input2.Matches(*ty3));
ASSERT_FALSE(input2.Matches(arr3));
}
// ----------------------------------------------------------------------
// OutputType
TEST(OutputType, Constructors) {
OutputType ty1 = int8();
ASSERT_EQ(OutputType::FIXED, ty1.kind());
AssertTypeEqual(*int8(), *ty1.type());
auto DummyResolver = [](KernelContext*,
const std::vector<TypeHolder>& args) -> Result<TypeHolder> {
return int32();
};
OutputType ty2(DummyResolver);
ASSERT_EQ(OutputType::COMPUTED, ty2.kind());
ASSERT_OK_AND_ASSIGN(TypeHolder out_type2, ty2.Resolve(nullptr, {}));
ASSERT_EQ(out_type2, int32());
// Copy constructor
OutputType ty3 = ty1;
ASSERT_EQ(OutputType::FIXED, ty3.kind());
AssertTypeEqual(*ty1.type(), *ty3.type());
OutputType ty4 = ty2;
ASSERT_EQ(OutputType::COMPUTED, ty4.kind());
ASSERT_OK_AND_ASSIGN(TypeHolder out_type4, ty4.Resolve(nullptr, {}));
ASSERT_EQ(out_type4, int32());
// Move constructor
OutputType ty5 = std::move(ty1);
ASSERT_EQ(OutputType::FIXED, ty5.kind());
AssertTypeEqual(*int8(), *ty5.type());
OutputType ty6 = std::move(ty4);
ASSERT_EQ(OutputType::COMPUTED, ty6.kind());
ASSERT_OK_AND_ASSIGN(TypeHolder out_type6, ty6.Resolve(nullptr, {}));
ASSERT_EQ(out_type6, int32());
// ToString
// ty1 was copied to ty3
ASSERT_EQ("int8", ty3.ToString());
ASSERT_EQ("computed", ty2.ToString());
}
TEST(OutputType, Resolve) {
OutputType ty1(int32());
ASSERT_OK_AND_ASSIGN(TypeHolder result, ty1.Resolve(nullptr, {}));
ASSERT_EQ(result, int32());
ASSERT_OK_AND_ASSIGN(result, ty1.Resolve(nullptr, {int8()}));
ASSERT_EQ(result, int32());
ASSERT_OK_AND_ASSIGN(result, ty1.Resolve(nullptr, {int8(), int8()}));
ASSERT_EQ(result, int32());
auto resolver = [](KernelContext*,
const std::vector<TypeHolder>& args) -> Result<TypeHolder> {
return args[0];
};
OutputType ty2(resolver);
ASSERT_OK_AND_ASSIGN(result, ty2.Resolve(nullptr, {utf8()}));
ASSERT_EQ(result, utf8());
// Type resolver that returns an error
OutputType ty3(
[](KernelContext* ctx, const std::vector<TypeHolder>& types) -> Result<TypeHolder> {
// NB: checking the value types versus the function arity should be
// validated elsewhere, so this is just for illustration purposes
if (types.size() == 0) {
return Status::Invalid("Need at least one argument");
}
return types[0];
});
ASSERT_RAISES(Invalid, ty3.Resolve(nullptr, {}));
// Type resolver that returns a fixed value
OutputType ty4(
[](KernelContext* ctx, const std::vector<TypeHolder>& types) -> Result<TypeHolder> {
return int32();
});
ASSERT_OK_AND_ASSIGN(result, ty4.Resolve(nullptr, {int8()}));
ASSERT_EQ(result, int32());
ASSERT_OK_AND_ASSIGN(result, ty4.Resolve(nullptr, {int8()}));
ASSERT_EQ(result, int32());
}
// ----------------------------------------------------------------------
// KernelSignature
TEST(KernelSignature, Basics) {
// (int8, decimal) -> utf8
std::vector<InputType> in_types({int8(), InputType(Type::DECIMAL)});
OutputType out_type(utf8());
KernelSignature sig(in_types, out_type);
ASSERT_EQ(2, sig.in_types().size());
ASSERT_TRUE(sig.in_types()[0].type()->Equals(*int8()));
ASSERT_TRUE(sig.in_types()[0].Matches(*int8()));
ASSERT_TRUE(sig.in_types()[1].Matches(*decimal(12, 2)));
}
TEST(KernelSignature, Equals) {
KernelSignature sig1({}, utf8());
KernelSignature sig1_copy({}, utf8());
KernelSignature sig2({int8()}, utf8());
// Output type doesn't matter (for now)
KernelSignature sig3({int8()}, int32());
KernelSignature sig4({int8(), int16()}, utf8());
KernelSignature sig4_copy({int8(), int16()}, utf8());
KernelSignature sig5({int8(), int16(), int32()}, utf8());
ASSERT_EQ(sig1, sig1);
ASSERT_EQ(sig2, sig3);
ASSERT_NE(sig3, sig4);
// Different sig objects, but same sig
ASSERT_EQ(sig1, sig1_copy);
ASSERT_EQ(sig4, sig4_copy);
// Match first 2 args, but not third
ASSERT_NE(sig4, sig5);
}
TEST(KernelSignature, VarArgsEquals) {
KernelSignature sig1({int8()}, utf8(), /*is_varargs=*/true);
KernelSignature sig2({int8()}, utf8(), /*is_varargs=*/true);
KernelSignature sig3({int8()}, utf8());
ASSERT_EQ(sig1, sig2);
ASSERT_NE(sig2, sig3);
}
TEST(KernelSignature, Hash) {
// Some basic tests to ensure that the hashes are deterministic and that all
// input arguments are incorporated
KernelSignature sig1({}, utf8());
KernelSignature sig2({int8()}, utf8());
KernelSignature sig3({int8(), int32()}, utf8());
ASSERT_EQ(sig1.Hash(), sig1.Hash());
ASSERT_EQ(sig2.Hash(), sig2.Hash());
ASSERT_NE(sig1.Hash(), sig2.Hash());
ASSERT_NE(sig2.Hash(), sig3.Hash());
}
TEST(KernelSignature, MatchesInputs) {
// () -> boolean
KernelSignature sig1({}, boolean());
ASSERT_TRUE(sig1.MatchesInputs({}));
ASSERT_FALSE(sig1.MatchesInputs({int8()}));
// (int8, decimal) -> boolean
KernelSignature sig2({int8(), InputType(Type::DECIMAL)}, boolean());
ASSERT_FALSE(sig2.MatchesInputs({}));
ASSERT_FALSE(sig2.MatchesInputs({int8()}));
ASSERT_TRUE(sig2.MatchesInputs({int8(), decimal(12, 2)}));
// (int8, int32) -> boolean
KernelSignature sig3({int8(), int32()}, boolean());
ASSERT_FALSE(sig3.MatchesInputs({}));
// Unqualified, these are ANY type and do not match because the kernel
// requires a scalar and an array
ASSERT_TRUE(sig3.MatchesInputs({int8(), int32()}));
ASSERT_FALSE(sig3.MatchesInputs({int8(), int16()}));
}
TEST(KernelSignature, VarArgsMatchesInputs) {
{
KernelSignature sig({int8()}, utf8(), /*is_varargs=*/true);
std::vector<TypeHolder> args = {int8()};
ASSERT_TRUE(sig.MatchesInputs(args));
args.push_back(int8());
args.push_back(int8());
ASSERT_TRUE(sig.MatchesInputs(args));
args.push_back(int32());
ASSERT_FALSE(sig.MatchesInputs(args));
}
{
KernelSignature sig({int8(), utf8()}, utf8(), /*is_varargs=*/true);
std::vector<TypeHolder> args = {int8()};
ASSERT_TRUE(sig.MatchesInputs(args));
args.push_back(utf8());
args.push_back(utf8());
ASSERT_TRUE(sig.MatchesInputs(args));
args.push_back(int32());
ASSERT_FALSE(sig.MatchesInputs(args));
}
}
TEST(KernelSignature, ToString) {
std::vector<InputType> in_types = {InputType(int8()), InputType(Type::DECIMAL),
InputType(utf8())};
KernelSignature sig(in_types, utf8());
ASSERT_EQ("(int8, Type::DECIMAL128, string) -> string", sig.ToString());
OutputType out_type(
[](KernelContext*, const std::vector<TypeHolder>& args) -> Result<TypeHolder> {
return Status::Invalid("NYI");
});
KernelSignature sig2({int8(), Type::DECIMAL}, out_type);
ASSERT_EQ("(int8, Type::DECIMAL128) -> computed", sig2.ToString());
}
TEST(KernelSignature, VarArgsToString) {
KernelSignature sig({int8()}, utf8(), /*is_varargs=*/true);
ASSERT_EQ("varargs[int8*] -> string", sig.ToString());
KernelSignature sig2({utf8(), int8()}, utf8(), /*is_varargs=*/true);
ASSERT_EQ("varargs[string, int8*] -> string", sig2.ToString());
}
} // namespace compute
} // namespace arrow