36
36
37
37
import com .google .common .util .concurrent .Futures ;
38
38
import com .google .common .util .concurrent .ListenableFuture ;
39
+ import org .apache .tsfile .block .column .Column ;
39
40
import org .apache .tsfile .block .column .ColumnBuilder ;
40
41
import org .apache .tsfile .enums .TSDataType ;
41
42
import org .apache .tsfile .read .common .block .TsBlock ;
@@ -81,13 +82,20 @@ public class InferenceOperator implements ProcessOperator {
81
82
private final TsBlockSerde serde = new TsBlockSerde ();
82
83
private InferenceWindowType windowType = null ;
83
84
85
+ private final boolean generateTimeColumn ;
86
+ private long maxTimestamp ;
87
+ private long minTimestamp ;
88
+ private long interval ;
89
+ private long currentRowIndex ;
90
+
84
91
public InferenceOperator (
85
92
OperatorContext operatorContext ,
86
93
Operator child ,
87
94
ModelInferenceDescriptor modelInferenceDescriptor ,
88
95
ExecutorService modelInferenceExecutor ,
89
96
List <String > targetColumnNames ,
90
97
List <String > inputColumnNames ,
98
+ boolean generateTimeColumn ,
91
99
long maxRetainedSize ,
92
100
long maxReturnSize ) {
93
101
this .operatorContext = operatorContext ;
@@ -106,6 +114,14 @@ public InferenceOperator(
106
114
if (modelInferenceDescriptor .getInferenceWindowParameter () != null ) {
107
115
windowType = modelInferenceDescriptor .getInferenceWindowParameter ().getWindowType ();
108
116
}
117
+
118
+ if (generateTimeColumn ) {
119
+ this .interval = 0 ;
120
+ this .minTimestamp = Long .MAX_VALUE ;
121
+ this .maxTimestamp = Long .MIN_VALUE ;
122
+ this .currentRowIndex = 0 ;
123
+ }
124
+ this .generateTimeColumn = generateTimeColumn ;
109
125
}
110
126
111
127
@ Override
@@ -140,6 +156,15 @@ public boolean hasNext() throws Exception {
140
156
return !finished || (results != null && results .size () != resultIndex );
141
157
}
142
158
159
+ private void fillTimeColumn (TsBlock tsBlock ) {
160
+ Column timeColumn = tsBlock .getTimeColumn ();
161
+ long [] time = timeColumn .getLongs ();
162
+ for (int i = 0 ; i < time .length ; i ++) {
163
+ time [i ] = maxTimestamp + interval * currentRowIndex ;
164
+ currentRowIndex ++;
165
+ }
166
+ }
167
+
143
168
@ Override
144
169
public TsBlock next () throws Exception {
145
170
if (inferenceExecutionFuture == null ) {
@@ -156,6 +181,9 @@ public TsBlock next() throws Exception {
156
181
157
182
if (results != null && resultIndex != results .size ()) {
158
183
TsBlock tsBlock = serde .deserialize (results .get (resultIndex ));
184
+ if (generateTimeColumn ) {
185
+ fillTimeColumn (tsBlock );
186
+ }
159
187
resultIndex ++;
160
188
return tsBlock ;
161
189
}
@@ -177,6 +205,9 @@ public TsBlock next() throws Exception {
177
205
178
206
finished = true ;
179
207
TsBlock resultTsBlock = serde .deserialize (inferenceResp .inferenceResult .get (0 ));
208
+ if (generateTimeColumn ) {
209
+ fillTimeColumn (resultTsBlock );
210
+ }
180
211
results = inferenceResp .inferenceResult ;
181
212
resultIndex ++;
182
213
return resultTsBlock ;
@@ -194,7 +225,12 @@ private void appendTsBlockToBuilder(TsBlock inputTsBlock) {
194
225
ColumnBuilder [] columnBuilders = inputTsBlockBuilder .getValueColumnBuilders ();
195
226
totalRow += inputTsBlock .getPositionCount ();
196
227
for (int i = 0 ; i < inputTsBlock .getPositionCount (); i ++) {
197
- timeColumnBuilder .writeLong (inputTsBlock .getTimeByIndex (i ));
228
+ long timestamp = inputTsBlock .getTimeByIndex (i );
229
+ if (generateTimeColumn ) {
230
+ minTimestamp = Math .min (minTimestamp , timestamp );
231
+ maxTimestamp = Math .max (maxTimestamp , timestamp );
232
+ }
233
+ timeColumnBuilder .writeLong (timestamp );
198
234
for (int columnIndex = 0 ; columnIndex < inputTsBlock .getValueColumnCount (); columnIndex ++) {
199
235
columnBuilders [columnIndex ].write (inputTsBlock .getColumn (columnIndex ), i );
200
236
}
@@ -259,6 +295,10 @@ private TsBlock preProcess(TsBlock inputTsBlock) {
259
295
260
296
private void submitInferenceTask () {
261
297
298
+ if (generateTimeColumn ) {
299
+ interval = (maxTimestamp - minTimestamp ) / totalRow ;
300
+ }
301
+
262
302
TsBlock inputTsBlock = inputTsBlockBuilder .build ();
263
303
264
304
TsBlock finalInputTsBlock = preProcess (inputTsBlock );
0 commit comments