-
Notifications
You must be signed in to change notification settings - Fork 34
/
TimedAggregators.scala
183 lines (149 loc) · 5.44 KB
/
TimedAggregators.scala
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
/*
* Copyright (C) 2023 The Chronon Authors.
*
* Licensed 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 ai.chronon.aggregator.base
import ai.chronon.aggregator.base.TimeTuple.typ
import ai.chronon.api._
import java.util
object TimeTuple extends Ordering[util.ArrayList[Any]] {
type typ = util.ArrayList[Any]
def `type`(inputType: DataType): DataType =
StructType(
s"TimePair_${DataType.toString(inputType)}",
Array(
StructField("epochMillis", LongType),
StructField("payload", inputType)
)
)
def make(ts: Long, payload: Any): util.ArrayList[Any] = {
val ir = new util.ArrayList[Any](2)
ir.add(ts)
ir.add(payload)
ir
}
def reset(ts: Long, payload: Any, tup: util.ArrayList[Any]): Unit = {
tup.set(0, ts)
tup.set(1, payload)
}
def getTs(tup: util.ArrayList[Any]): Long = tup.get(0).asInstanceOf[Long]
override def compare(x: util.ArrayList[Any], y: util.ArrayList[Any]): Int = {
java.lang.Long.compare(getTs(x), getTs(y))
}
}
abstract class TimeOrdered(inputType: DataType) extends TimedAggregator[Any, TimeTuple.typ, Any] {
override def outputType: DataType = inputType
override def irType: DataType = TimeTuple.`type`(inputType)
override def prepare(input: Any, ts: Long): TimeTuple.typ =
TimeTuple.make(ts, input)
override def finalize(ir: util.ArrayList[Any]): Any = ir.get(1)
override def normalize(ir: TimeTuple.typ): Array[Any] = ArrayUtils.toArray(ir)
override def denormalize(ir: Any): TimeTuple.typ =
ArrayUtils.fromArray(ir.asInstanceOf[Array[Any]])
override def clone(ir: TimeTuple.typ): TimeTuple.typ = ir.clone().asInstanceOf[TimeTuple.typ]
}
class First(inputType: DataType) extends TimeOrdered(inputType) {
//mutating
override def update(
ir: util.ArrayList[Any],
input: Any,
ts: Long
): util.ArrayList[Any] = {
if (TimeTuple.getTs(ir) > ts) {
TimeTuple.reset(ts, input, ir)
}
ir
}
override def merge(
ir1: util.ArrayList[Any],
ir2: util.ArrayList[Any]
): util.ArrayList[Any] =
TimeTuple.min(ir1, ir2)
}
class Last(inputType: DataType) extends TimeOrdered(inputType) {
//mutating
override def update(
ir: util.ArrayList[Any],
input: Any,
ts: Long
): util.ArrayList[Any] = {
if (TimeTuple.getTs(ir) < ts) {
TimeTuple.reset(ts, input, ir)
}
ir
}
override def merge(
ir1: util.ArrayList[Any],
ir2: util.ArrayList[Any]
): util.ArrayList[Any] =
TimeTuple.max(ir1, ir2)
}
// FIRSTK LASTK ==============================================================
// FIRSTK LASTK ==============================================================
class OrderByLimitTimed(
inputType: DataType,
limit: Int,
ordering: Ordering[TimeTuple.typ]
) extends TimedAggregator[Any, util.ArrayList[TimeTuple.typ], util.ArrayList[Any]] {
type Container = util.ArrayList[TimeTuple.typ]
private val minHeap = new MinHeap[TimeTuple.typ](limit, ordering)
override def outputType: DataType = ListType(inputType)
override def irType: DataType = ListType(TimeTuple.`type`(inputType))
override final def prepare(input: Any, ts: Long): Container = {
// val gson = new Gson()
val tuple = TimeTuple.make(ts, input)
// println(s"init: ${gson.toJson(tuple)}")
val arr = new Container()
arr.add(tuple)
arr
}
override final def update(state: Container, input: Any, ts: Long): Container =
// TODO: tuple making is unnecessary if we are not going to insert
// one idea is to make insert return a boolean instead, and use that as a cue to reset a tuple object
// which is a class member
minHeap.insert(state, TimeTuple.make(ts, input))
override final def merge(state1: Container, state2: Container): Container =
minHeap.merge(state1, state2)
override def finalize(state: Container): util.ArrayList[Any] = {
val sorted = minHeap.sort(state)
val result = new util.ArrayList[Any](state.size())
val it = sorted.iterator
while (it.hasNext) {
result.add(it.next().get(1))
}
result
}
override def normalize(
ir: util.ArrayList[TimeTuple.typ]
): util.ArrayList[Array[Any]] = {
val result = new util.ArrayList[Array[Any]](ir.size())
val it = ir.iterator()
while (it.hasNext) {
result.add(ArrayUtils.toArray(it.next))
}
result
}
override def denormalize(ir: Any): util.ArrayList[TimeTuple.typ] = {
val irCast = ir.asInstanceOf[util.ArrayList[Array[Any]]]
val result = new util.ArrayList[TimeTuple.typ](irCast.size())
val it = irCast.iterator()
while (it.hasNext) {
result.add(ArrayUtils.fromArray(it.next()))
}
result
}
override def clone(ir: util.ArrayList[typ]): util.ArrayList[typ] = ir.clone().asInstanceOf[util.ArrayList[typ]]
}
class LastK(inputType: DataType, k: Int) extends OrderByLimitTimed(inputType, k, TimeTuple.reverse)
class FirstK(inputType: DataType, k: Int) extends OrderByLimitTimed(inputType, k, TimeTuple)