-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAggregate.java
More file actions
219 lines (190 loc) · 8.23 KB
/
Aggregate.java
File metadata and controls
219 lines (190 loc) · 8.23 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
/**
* Aggregate function operation
**/
package qp.operators;
import qp.utils.Attribute;
import qp.utils.Batch;
import qp.utils.Schema;
import qp.utils.Tuple;
import java.util.ArrayList;
import java.util.LinkedList;
public class Aggregate extends Project {
boolean eos;
// Stored tuples. This is needed because the aggregate operator scans through the entire
// underlying operator to get the value when next() is called.
LinkedList<Tuple> tuples;
ArrayList<Integer> attrTupleIndex; // Indexes of aggregated attributes (in tuple)
ArrayList<Integer> aggregateFunction; // Aggregate function of attributes
ArrayList<Integer> projectedAggregateTypes; // Projected aggregate types
ArrayList<Number> runningAggregates; // Maintain running aggregates
int runningCount;
int inputCursor;
boolean allAggregate = true; // Boolean if all attributes are aggregated
boolean aggregateDone = false;
/**
* constructor
**/
public Aggregate(Operator base, ArrayList<Attribute> as, int type) {
super(base, as, type);
this.tuples = new LinkedList<>();
this.attrTupleIndex = new ArrayList<>();
this.aggregateFunction = new ArrayList<>();
this.projectedAggregateTypes = new ArrayList<>();
this.runningAggregates = new ArrayList<>();
this.runningCount = 0;
for (Attribute attr: attrset) {
if (attr.getAggType() == Attribute.NONE) {
allAggregate = false;
break;
}
}
}
public boolean isAllAggregate() {
return allAggregate;
}
public Operator getBase() {
return base;
}
public void setBase(Operator base) {
this.base = base;
}
public boolean open() {
// Select number of tuples per batch
int tupleSize = schema.getTupleSize();
batchsize = Batch.getPageSize() / tupleSize;
if (!base.open()) return false;
this.eos = false;
this.inputCursor = 0;
Schema baseSchema = base.getSchema();
attrIndex = new int[attrset.size()];
for (int i = 0; i < attrset.size(); i++) {
Attribute attr = attrset.get(i);
int index = baseSchema.indexOf(attr.getBaseAttribute());
attrIndex[i] = index;
// Is an aggregated attribute
if (attr.getAggType() != Attribute.NONE) {
attr.setType(baseSchema.getAttribute(index).getType());
this.projectedAggregateTypes.add(attr.getProjectedType());
this.aggregateFunction.add(attr.getAggType());
this.runningAggregates.add(null);
this.attrTupleIndex.add(index);
}
}
return true;
}
private Batch getNextBatch() {
if (this.tuples.isEmpty() || aggregateDone) {
close();
return null;
}
Batch result = new Batch(batchsize);
int aggregateCount = 0;
// Add till output batch is full or no more tuples to process
while (!result.isFull() && !this.tuples.isEmpty()) {
Tuple tuple = this.tuples.removeFirst();
ArrayList<Object> current = new ArrayList<>();
// Accumulate all attributes into tuple
for (int i = 0; i < attrset.size(); i++) {
Attribute attr = attrset.get(i);
if (attr.getAggType() == Attribute.NONE) {
current.add(tuple.dataAt(attrIndex[i]));
} else {
int projectedType = this.projectedAggregateTypes.get(aggregateCount);
switch (this.aggregateFunction.get(aggregateCount)) {
case Attribute.AVG:
if (projectedType == Attribute.REAL) {
current.add(this.runningAggregates.get(aggregateCount).floatValue() / this.runningCount);
} else if (projectedType == Attribute.INT) {
current.add(this.runningAggregates.get(aggregateCount).intValue() / this.runningCount);
}
break;
case Attribute.COUNT:
current.add(this.runningCount);
break;
case Attribute.MIN:
case Attribute.MAX:
if (projectedType == Attribute.REAL) {
current.add(this.runningAggregates.get(aggregateCount).floatValue());
} else if (projectedType == Attribute.INT) {
current.add(this.runningAggregates.get(aggregateCount).intValue());
}
}
aggregateCount++;
}
}
aggregateCount = 0;
Tuple outtuple = new Tuple(current);
result.add(outtuple);
// If all attributes are aggregated, only need one tuple
if (allAggregate) {
aggregateDone = true;
close();
return result;
}
}
return result;
}
// Scans the entire base operator upon the first call of next, and then releases output in batches
public Batch next() {
int i = 0;
if (eos) {
return getNextBatch();
}
Batch inbatch = this.base.next();
while (inbatch != null) {
for (i = inputCursor; i < inbatch.size(); ++i) {
this.runningCount++;
Tuple present = inbatch.get(i);
// Accumulate values for aggregated attributes
for (int j = 0; j < this.attrTupleIndex.size(); j++) {
int aggregateType = this.aggregateFunction.get(j);
if (aggregateType == Attribute.COUNT) {
continue;
}
int aggregateIndex = this.attrTupleIndex.get(j);
Number value = (Number) present.dataAt(aggregateIndex);
Number currentValue = this.runningAggregates.get(j);
if (currentValue == null) {
this.runningAggregates.set(j, value);
} else {
switch (aggregateType) {
case Attribute.MIN:
if (this.projectedAggregateTypes.get(j) == Attribute.INT) {
this.runningAggregates.set(j, Math.min(value.intValue(), currentValue.intValue()));
} else {
this.runningAggregates.set(j, Math.min(value.floatValue(), currentValue.floatValue()));
}
break;
case Attribute.MAX:
if (this.projectedAggregateTypes.get(j) == Attribute.INT) {
this.runningAggregates.set(j, Math.max(value.intValue(), currentValue.intValue()));
} else {
this.runningAggregates.set(j, Math.max(value.floatValue(), currentValue.floatValue()));
}
break;
case Attribute.AVG:
if (this.projectedAggregateTypes.get(j) == Attribute.INT) {
this.runningAggregates.set(j, currentValue.intValue() + value.intValue());
} else {
this.runningAggregates.set(j, currentValue.floatValue() + value.floatValue());
}
break;
}
}
}
this.tuples.add(present);
}
if (i == inbatch.size())
inputCursor = 0;
else
inputCursor = i;
inbatch = this.base.next();
}
eos = true;
return getNextBatch();
}
public boolean close() {
base.close();
return true;
}
}