-
Notifications
You must be signed in to change notification settings - Fork 136
/
IntersectOperatorDescriptorTest.java
222 lines (193 loc) · 9.34 KB
/
IntersectOperatorDescriptorTest.java
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
/*
* 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.
*/
package org.apache.hyracks.tests.unit;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.List;
import org.apache.hyracks.api.comm.IFrame;
import org.apache.hyracks.api.comm.IFrameWriter;
import org.apache.hyracks.api.context.IHyracksTaskContext;
import org.apache.hyracks.api.dataflow.OperatorDescriptorId;
import org.apache.hyracks.api.dataflow.value.IBinaryComparatorFactory;
import org.apache.hyracks.api.dataflow.value.INormalizedKeyComputerFactory;
import org.apache.hyracks.api.dataflow.value.ISerializerDeserializer;
import org.apache.hyracks.api.dataflow.value.RecordDescriptor;
import org.apache.hyracks.api.exceptions.HyracksDataException;
import org.apache.hyracks.api.exceptions.HyracksException;
import org.apache.hyracks.api.job.IOperatorDescriptorRegistry;
import org.apache.hyracks.data.std.accessors.PointableBinaryComparatorFactory;
import org.apache.hyracks.data.std.primitive.IntegerPointable;
import org.apache.hyracks.dataflow.common.data.marshalling.IntegerSerializerDeserializer;
import org.apache.hyracks.dataflow.std.intersect.IntersectOperatorDescriptor;
import org.apache.hyracks.tests.util.InputFrameGenerator;
import org.apache.hyracks.tests.util.MultiThreadTaskEmulator;
import org.apache.hyracks.tests.util.OutputFrameVerifier;
import org.apache.hyracks.test.support.TestUtils;
import org.junit.Before;
import org.junit.Test;
public class IntersectOperatorDescriptorTest {
IOperatorDescriptorRegistry mockRegistry = when(
mock(IOperatorDescriptorRegistry.class).createOperatorDescriptorId(any()))
.thenReturn(new OperatorDescriptorId(1)).getMock();
MultiThreadTaskEmulator multiThreadTaskEmulator = new MultiThreadTaskEmulator();
InputFrameGenerator frameGenerator = new InputFrameGenerator(256);
IHyracksTaskContext ctx = TestUtils.create(256);
int nInputs;
int nProjectFields;
int[][] compareFields;
RecordDescriptor[] inputRecordDescriptor;
INormalizedKeyComputerFactory normalizedKeyFactory;
IBinaryComparatorFactory[] comparatorFactory;
RecordDescriptor outRecordDescriptor;
protected void initializeParameters() {
compareFields = new int[nInputs][];
inputRecordDescriptor = new RecordDescriptor[nInputs];
normalizedKeyFactory = null;
comparatorFactory = new IBinaryComparatorFactory[] {
PointableBinaryComparatorFactory.of(IntegerPointable.FACTORY),
PointableBinaryComparatorFactory.of(IntegerPointable.FACTORY)
};
for (int i = 0; i < nInputs; i++) {
compareFields[i] = new int[nProjectFields];
for (int f = 0; f < nProjectFields; f++) {
compareFields[i][f] = f;
}
}
for (int i = 0; i < nInputs; i++) {
inputRecordDescriptor[i] = new RecordDescriptor(new ISerializerDeserializer[] {
IntegerSerializerDeserializer.INSTANCE,
IntegerSerializerDeserializer.INSTANCE,
IntegerSerializerDeserializer.INSTANCE
});
}
outRecordDescriptor = new RecordDescriptor(new ISerializerDeserializer[] {
IntegerSerializerDeserializer.INSTANCE,
IntegerSerializerDeserializer.INSTANCE
});
}
@Before
public void setUpInput() {
nInputs = 3;
nProjectFields = 2;
initializeParameters();
}
@Test
public void testNormalOperatorInitialization() throws HyracksException {
IntersectOperatorDescriptor operatorDescriptor = new IntersectOperatorDescriptor(mockRegistry, nInputs,
compareFields, normalizedKeyFactory, comparatorFactory, outRecordDescriptor);
assertEquals(nInputs, operatorDescriptor.getInputArity());
}
@Test
public void testCommonIntersect() throws Exception {
List<Object[]> answer = new ArrayList<>();
List<IFrame>[] inputFrames = new ArrayList[nInputs];
prepareCommonDataFrame(inputFrames, answer);
executeAndVerifyResult(inputFrames, answer);
}
@Test
public void testNullOutputIntersect() throws Exception {
List<Object[]> answer = new ArrayList<>();
List<IFrame>[] inputFrames = new ArrayList[nInputs];
prepareNullResultDataFrame(inputFrames, answer);
executeAndVerifyResult(inputFrames, answer);
}
@Test
public void testOneInputIsVeryShortIntersect() throws Exception {
List<Object[]> answer = new ArrayList<>();
List<IFrame>[] inputFrames = new ArrayList[nInputs];
prepareOneInputIsVeryShortDataFrame(inputFrames, answer);
executeAndVerifyResult(inputFrames, answer);
}
@Test
public void testAllSameInputIntersect() throws Exception {
List<Object[]> answer = new ArrayList<>();
List<IFrame>[] inputFrames = new ArrayList[nInputs];
prepareAllSameInputDataFrame(inputFrames, answer);
executeAndVerifyResult(inputFrames, answer);
}
@Test
public void testOnlyOneInputIntersect() throws Exception {
nInputs = 1;
initializeParameters();
List<Object[]> answer = new ArrayList<>();
List<IFrame>[] inputFrames = new ArrayList[nInputs];
prepareAllSameInputDataFrame(inputFrames, answer);
executeAndVerifyResult(inputFrames, answer);
}
private void executeAndVerifyResult(List<IFrame>[] inputFrames, List<Object[]> answer) throws Exception {
IntersectOperatorDescriptor.IntersectOperatorNodePushable pushable =
new IntersectOperatorDescriptor.IntersectOperatorNodePushable(ctx, nInputs, inputRecordDescriptor,
compareFields, null, comparatorFactory);
assertEquals(nInputs, pushable.getInputArity());
IFrameWriter[] writers = new IFrameWriter[nInputs];
for (int i = 0; i < nInputs; i++) {
writers[i] = pushable.getInputFrameWriter(i);
}
IFrameWriter resultVerifier = new OutputFrameVerifier(outRecordDescriptor, answer);
pushable.setOutputFrameWriter(0, resultVerifier, outRecordDescriptor);
multiThreadTaskEmulator.runInParallel(writers, inputFrames);
}
protected void prepareCommonDataFrame(List<IFrame>[] inputFrames, List<Object[]> answer)
throws HyracksDataException {
for (int i = 0; i < nInputs; i++) {
List<Object[]> inputObjects = new ArrayList<>();
generateRecordStream(inputObjects, inputRecordDescriptor[i], i + 1, (i + 1) * 100, 1);
inputFrames[i] = frameGenerator.generateDataFrame(inputRecordDescriptor[i], inputObjects);
}
generateRecordStream(answer, outRecordDescriptor, nInputs, 100, 1);
}
protected void prepareNullResultDataFrame(List<IFrame>[] inputFrames, List<Object[]> answer)
throws HyracksDataException {
for (int i = 0; i < nInputs; i++) {
List<Object[]> inputObjects = new ArrayList<>();
generateRecordStream(inputObjects, inputRecordDescriptor[i], (i + 1) * 100, (i + 2) * 100, 1);
inputFrames[i] = frameGenerator.generateDataFrame(inputRecordDescriptor[i], inputObjects);
}
}
protected void prepareOneInputIsVeryShortDataFrame(List<IFrame>[] inputFrames, List<Object[]> answer)
throws HyracksDataException {
for (int i = 0; i < nInputs; i++) {
List<Object[]> inputObjects = new ArrayList<>();
generateRecordStream(inputObjects, inputRecordDescriptor[i], i, i * 100 + 1, 1);
inputFrames[i] = frameGenerator.generateDataFrame(inputRecordDescriptor[i], inputObjects);
}
}
protected void prepareAllSameInputDataFrame(List<IFrame>[] inputFrames, List<Object[]> answer)
throws HyracksDataException {
for (int i = 0; i < nInputs; i++) {
List<Object[]> inputObjects = new ArrayList<>();
generateRecordStream(inputObjects, inputRecordDescriptor[i], 0, 100, 1);
inputFrames[i] = frameGenerator.generateDataFrame(inputRecordDescriptor[i], inputObjects);
}
generateRecordStream(answer, outRecordDescriptor, 0, 100, 1);
}
private void generateRecordStream(List<Object[]> inputs, RecordDescriptor recordDesc,
int start, int end, int step) {
for (int i = start; i < end; i += step) {
Object[] obj = new Object[recordDesc.getFieldCount()];
for (int f = 0; f < recordDesc.getFieldCount(); f++) {
obj[f] = i;
}
inputs.add(obj);
}
}
}