tfill.c 24.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

16
#include "filter.h"
17
#include "os.h"
18
#include "query.h"
19
#include "taosdef.h"
H
Hongze Cheng 已提交
20
#include "tmsg.h"
21 22
#include "ttypes.h"

23
#include "executorInt.h"
S
common  
Shengliang Guan 已提交
24
#include "tcommon.h"
25
#include "thash.h"
26 27
#include "ttime.h"

28
#include "executorInt.h"
X
Xiaoyu Wang 已提交
29
#include "function.h"
30
#include "querynodes.h"
X
Xiaoyu Wang 已提交
31
#include "tdatablock.h"
32 33
#include "tfill.h"

34
#define FILL_IS_ASC_FILL(_f) ((_f)->order == TSDB_ORDER_ASC)
X
Xiaoyu Wang 已提交
35 36
#define DO_INTERPOLATION(_v1, _v2, _k1, _k2, _k) \
  ((_v1) + ((_v2) - (_v1)) * (((double)(_k)) - ((double)(_k1))) / (((double)(_k2)) - ((double)(_k1))))
37

H
Haojun Liao 已提交
38 39
static void doSetVal(SColumnInfoData* pDstColInfoData, int32_t rowIndex, const SGroupKeys* pKey);

D
dapan1121 已提交
40 41
static void setNotFillColumn(SFillInfo* pFillInfo, SColumnInfoData* pDstColInfo, int32_t rowIndex, int32_t colIdx) {
  SRowVal* p = NULL;
5
54liuyao 已提交
42 43
  if (pFillInfo->type == TSDB_FILL_NEXT) {
    p = FILL_IS_ASC_FILL(pFillInfo) ? &pFillInfo->next : &pFillInfo->prev;
D
dapan1121 已提交
44
  } else {
5
54liuyao 已提交
45
    p = FILL_IS_ASC_FILL(pFillInfo) ? &pFillInfo->prev : &pFillInfo->next;
D
dapan1121 已提交
46
  }
47

D
dapan1121 已提交
48 49 50 51
  SGroupKeys* pKey = taosArrayGet(p->pRowVal, colIdx);
  doSetVal(pDstColInfo, rowIndex, pKey);
}

H
Haojun Liao 已提交
52
static void setNullRow(SSDataBlock* pBlock, SFillInfo* pFillInfo, int32_t rowIndex) {
5
54liuyao 已提交
53 54 55
  for (int32_t i = 0; i < pFillInfo->numOfCols; ++i) {
    SFillColInfo*    pCol = &pFillInfo->pFillCol[i];
    int32_t          dstSlotId = GET_DEST_SLOT_ID(pCol);
H
Haojun Liao 已提交
56 57
    SColumnInfoData* pDstColInfo = taosArrayGet(pBlock->pDataBlock, dstSlotId);
    if (pCol->notFillCol) {
58 59
      bool filled = fillIfWindowPseudoColumn(pFillInfo, pCol, pDstColInfo, rowIndex);
      if (!filled) {
D
dapan1121 已提交
60
        setNotFillColumn(pFillInfo, pDstColInfo, rowIndex, i);
H
Haojun Liao 已提交
61
      }
62
    } else {
63
      colDataSetNULL(pDstColInfo, rowIndex);
64
    }
65 66 67
  }
}

68
static void doSetUserSpecifiedValue(SColumnInfoData* pDst, SVariant* pVar, int32_t rowIndex, int64_t currentKey) {
69 70 71
  if (pDst->info.type == TSDB_DATA_TYPE_FLOAT) {
    float v = 0;
    GET_TYPED_DATA(v, float, pVar->nType, &pVar->i);
72
    colDataSetVal(pDst, rowIndex, (char*)&v, false);
73 74 75
  } else if (pDst->info.type == TSDB_DATA_TYPE_DOUBLE) {
    double v = 0;
    GET_TYPED_DATA(v, double, pVar->nType, &pVar->i);
76
    colDataSetVal(pDst, rowIndex, (char*)&v, false);
77 78 79
  } else if (IS_SIGNED_NUMERIC_TYPE(pDst->info.type)) {
    int64_t v = 0;
    GET_TYPED_DATA(v, int64_t, pVar->nType, &pVar->i);
80
    colDataSetVal(pDst, rowIndex, (char*)&v, false);
81
  } else if (pDst->info.type == TSDB_DATA_TYPE_TIMESTAMP) {
82
    colDataSetVal(pDst, rowIndex, (const char*)&currentKey, false);
83
  } else {  // varchar/nchar data
84
    colDataSetNULL(pDst, rowIndex);
85 86 87
  }
}

5
54liuyao 已提交
88
// fill windows pseudo column, _wstart, _wend, _wduration and return true, otherwise return false
H
Haojun Liao 已提交
89 90
bool fillIfWindowPseudoColumn(SFillInfo* pFillInfo, SFillColInfo* pCol, SColumnInfoData* pDstColInfoData,
                              int32_t rowIndex) {
91 92 93 94 95 96 97 98
  if (!pCol->notFillCol) {
    return false;
  }
  if (pCol->pExpr->pExpr->nodeType == QUERY_NODE_COLUMN) {
    if (pCol->pExpr->base.numOfParams != 1) {
      return false;
    }
    if (pCol->pExpr->base.pParam[0].pCol->colType == COLUMN_TYPE_WINDOW_START) {
99
      colDataSetVal(pDstColInfoData, rowIndex, (const char*)&pFillInfo->currentKey, false);
100 101
      return true;
    } else if (pCol->pExpr->base.pParam[0].pCol->colType == COLUMN_TYPE_WINDOW_END) {
5
54liuyao 已提交
102
      // TODO: include endpoint
103
      SInterval* pInterval = &pFillInfo->interval;
5
54liuyao 已提交
104
      int64_t    windowEnd =
S
shenglian zhou 已提交
105
          taosTimeAdd(pFillInfo->currentKey, pInterval->interval, pInterval->intervalUnit, pInterval->precision);
106
      colDataSetVal(pDstColInfoData, rowIndex, (const char*)&windowEnd, false);
107 108
      return true;
    } else if (pCol->pExpr->base.pParam[0].pCol->colType == COLUMN_TYPE_WINDOW_DURATION) {
5
54liuyao 已提交
109
      // TODO: include endpoint
110
      colDataSetVal(pDstColInfoData, rowIndex, (const char*)&pFillInfo->interval.sliding, false);
111 112 113
      return true;
    }
  }
114 115 116
  return false;
}

117 118
static void doFillOneRow(SFillInfo* pFillInfo, SSDataBlock* pBlock, SSDataBlock* pSrcBlock, int64_t ts,
                         bool outOfBound) {
X
Xiaoyu Wang 已提交
119
  SPoint  point1, point2, point;
120 121
  int32_t step = GET_FORWARD_DIRECTION_FACTOR(pFillInfo->order);

122
  // set the primary timestamp column value
123
  int32_t index = pBlock->info.rows;
124 125 126

  // set the other values
  if (pFillInfo->type == TSDB_FILL_PREV) {
127
    for (int32_t i = 0; i < pFillInfo->numOfCols; ++i) {
128 129 130
      SFillColInfo* pCol = &pFillInfo->pFillCol[i];

      SColumnInfoData* pDstColInfoData = taosArrayGet(pBlock->pDataBlock, GET_DEST_SLOT_ID(pCol));
5
54liuyao 已提交
131
      bool             filled = fillIfWindowPseudoColumn(pFillInfo, pCol, pDstColInfoData, index);
132
      if (!filled) {
D
dapan1121 已提交
133
        setNotFillColumn(pFillInfo, pDstColInfoData, index, i);
134
      }
135 136
    }
  } else if (pFillInfo->type == TSDB_FILL_NEXT) {
137
    // todo  refactor: start from 0 not 1
138
    for (int32_t i = 0; i < pFillInfo->numOfCols; ++i) {
5
54liuyao 已提交
139
      SFillColInfo*    pCol = &pFillInfo->pFillCol[i];
140
      SColumnInfoData* pDstColInfoData = taosArrayGet(pBlock->pDataBlock, GET_DEST_SLOT_ID(pCol));
5
54liuyao 已提交
141
      bool             filled = fillIfWindowPseudoColumn(pFillInfo, pCol, pDstColInfoData, index);
142
      if (!filled) {
D
dapan1121 已提交
143
        setNotFillColumn(pFillInfo, pDstColInfoData, index, i);
144
      }
145 146 147
    }
  } else if (pFillInfo->type == TSDB_FILL_LINEAR) {
    // TODO : linear interpolation supports NULL value
148
    if (outOfBound) {
H
Haojun Liao 已提交
149
      setNullRow(pBlock, pFillInfo, index);
150
    } else {
151
      for (int32_t i = 0; i < pFillInfo->numOfCols; ++i) {
152 153
        SFillColInfo* pCol = &pFillInfo->pFillCol[i];

X
Xiaoyu Wang 已提交
154
        int32_t          dstSlotId = GET_DEST_SLOT_ID(pCol);
155
        SColumnInfoData* pDstCol = taosArrayGet(pBlock->pDataBlock, dstSlotId);
H
Haojun Liao 已提交
156 157 158
        int16_t          type = pDstCol->info.type;

        if (pCol->notFillCol) {
159 160
          bool filled = fillIfWindowPseudoColumn(pFillInfo, pCol, pDstCol, index);
          if (!filled) {
D
dapan1121 已提交
161
            setNotFillColumn(pFillInfo, pDstCol, index, i);
H
Haojun Liao 已提交
162 163
          }
        } else {
H
Haojun Liao 已提交
164
          SGroupKeys* pKey = taosArrayGet(pFillInfo->prev.pRowVal, i);
H
Haojun Liao 已提交
165
          if (IS_VAR_DATA_TYPE(type) || type == TSDB_DATA_TYPE_BOOL || pKey->isNull) {
166
            colDataSetNULL(pDstCol, index);
H
Haojun Liao 已提交
167 168
            continue;
          }
169

H
Haojun Liao 已提交
170
          SGroupKeys* pKey1 = taosArrayGet(pFillInfo->prev.pRowVal, pFillInfo->tsSlotId);
171

H
Haojun Liao 已提交
172
          int64_t prevTs = *(int64_t*)pKey1->pData;
H
Haojun Liao 已提交
173
          int32_t srcSlotId = GET_DEST_SLOT_ID(pCol);
174

H
Haojun Liao 已提交
175 176
          SColumnInfoData* pSrcCol = taosArrayGet(pSrcBlock->pDataBlock, srcSlotId);
          char*            data = colDataGetData(pSrcCol, pFillInfo->index);
177

H
Haojun Liao 已提交
178 179
          point1 = (SPoint){.key = prevTs, .val = pKey->pData};
          point2 = (SPoint){.key = ts, .val = data};
180

H
Haojun Liao 已提交
181 182 183
          int64_t out = 0;
          point = (SPoint){.key = pFillInfo->currentKey, .val = &out};
          taosGetLinearInterpolationVal(&point, type, &point1, &point2, type);
184

185
          colDataSetVal(pDstCol, index, (const char*)&out, false);
H
Haojun Liao 已提交
186
        }
187 188
      }
    }
D
dapan1121 已提交
189
  } else if (pFillInfo->type == TSDB_FILL_NULL || pFillInfo->type == TSDB_FILL_NULL_F) {  // fill with NULL
H
Haojun Liao 已提交
190
    setNullRow(pBlock, pFillInfo, index);
X
Xiaoyu Wang 已提交
191
  } else {  // fill with user specified value for each column
192
    for (int32_t i = 0; i < pFillInfo->numOfCols; ++i) {
193 194
      SFillColInfo* pCol = &pFillInfo->pFillCol[i];

5
54liuyao 已提交
195
      int32_t          slotId = GET_DEST_SLOT_ID(pCol);
H
Haojun Liao 已提交
196 197 198
      SColumnInfoData* pDst = taosArrayGet(pBlock->pDataBlock, slotId);

      if (pCol->notFillCol) {
199 200
        bool filled = fillIfWindowPseudoColumn(pFillInfo, pCol, pDst, index);
        if (!filled) {
D
dapan1121 已提交
201
          setNotFillColumn(pFillInfo, pDst, index, i);
H
Haojun Liao 已提交
202 203 204 205 206
        }
      } else {
        SVariant* pVar = &pFillInfo->pFillCol[i].fillVal;
        doSetUserSpecifiedValue(pDst, pVar, index, pFillInfo->currentKey);
      }
207 208 209
    }
  }

X
Xiaoyu Wang 已提交
210
  //  setTagsValue(pFillInfo, data, index);
211
  SInterval* pInterval = &pFillInfo->interval;
X
Xiaoyu Wang 已提交
212 213
  pFillInfo->currentKey =
      taosTimeAdd(pFillInfo->currentKey, pInterval->sliding * step, pInterval->slidingUnit, pInterval->precision);
214
  pBlock->info.rows += 1;
215 216 217
  pFillInfo->numOfCurrent++;
}

218 219
void doSetVal(SColumnInfoData* pDstCol, int32_t rowIndex, const SGroupKeys* pKey) {
  if (pKey->isNull) {
220
    colDataSetNULL(pDstCol, rowIndex);
221
  } else {
222
    colDataSetVal(pDstCol, rowIndex, pKey->pData, false);
223 224 225 226
  }
}

static void initBeforeAfterDataBuf(SFillInfo* pFillInfo) {
H
Haojun Liao 已提交
227
  if (taosArrayGetSize(pFillInfo->next.pRowVal) > 0) {
228 229 230
    return;
  }

231
  for (int i = 0; i < pFillInfo->numOfCols; i++) {
232
    SFillColInfo* pCol = &pFillInfo->pFillCol[i];
233

X
Xiaoyu Wang 已提交
234
    SGroupKeys  key = {0};
235
    SResSchema* pSchema = &pCol->pExpr->base.resSchema;
X
Xiaoyu Wang 已提交
236
    key.pData = taosMemoryMalloc(pSchema->bytes);
237 238
    key.isNull = true;
    key.bytes = pSchema->bytes;
X
Xiaoyu Wang 已提交
239
    key.type = pSchema->type;
240

H
Haojun Liao 已提交
241
    taosArrayPush(pFillInfo->next.pRowVal, &key);
242 243

    key.pData = taosMemoryMalloc(pSchema->bytes);
H
Haojun Liao 已提交
244
    taosArrayPush(pFillInfo->prev.pRowVal, &key);
245 246 247
  }
}

248 249
static void saveColData(SArray* rowBuf, int32_t columnIndex, const char* src, bool isNull);

250
static void copyCurrentRowIntoBuf(SFillInfo* pFillInfo, int32_t rowIndex, SRowVal* pRowVal, bool reset) {
251 252 253
  SColumnInfoData* pTsCol = taosArrayGet(pFillInfo->pSrcBlock->pDataBlock, pFillInfo->srcTsSlotId);
  pRowVal->key = ((int64_t*)pTsCol->pData)[rowIndex];

254
  for (int32_t i = 0; i < pFillInfo->numOfCols; ++i) {
H
Haojun Liao 已提交
255
    int32_t type = pFillInfo->pFillCol[i].pExpr->pExpr->nodeType;
5
54liuyao 已提交
256 257 258 259
    if ( type == QUERY_NODE_COLUMN || type == QUERY_NODE_OPERATOR || type == QUERY_NODE_FUNCTION) {
      if (!pFillInfo->pFillCol[i].notFillCol && pFillInfo->type != TSDB_FILL_NEXT) {
        continue;
      }
H
Haojun Liao 已提交
260
      int32_t srcSlotId = GET_DEST_SLOT_ID(&pFillInfo->pFillCol[i]);
261

5
54liuyao 已提交
262 263 264 265
      if (srcSlotId == pFillInfo->srcTsSlotId && pFillInfo->type == TSDB_FILL_LINEAR) {
        continue;
      }

H
Haojun Liao 已提交
266
      SColumnInfoData* pSrcCol = taosArrayGet(pFillInfo->pSrcBlock->pDataBlock, srcSlotId);
267

H
Haojun Liao 已提交
268 269
      bool  isNull = colDataIsNull_s(pSrcCol, rowIndex);
      char* p = colDataGetData(pSrcCol, rowIndex);
270

271
      saveColData(pRowVal->pRowVal, i, p, reset ? true : isNull);
H
Haojun Liao 已提交
272 273 274
    } else {
      ASSERT(0);
    }
275 276 277
  }
}

278
static int32_t fillResultImpl(SFillInfo* pFillInfo, SSDataBlock* pBlock, int32_t outputRows) {
279 280
  pFillInfo->numOfCurrent = 0;

281
  SColumnInfoData* pTsCol = taosArrayGet(pFillInfo->pSrcBlock->pDataBlock, pFillInfo->srcTsSlotId);
282 283

  int32_t step = GET_FORWARD_DIRECTION_FACTOR(pFillInfo->order);
X
Xiaoyu Wang 已提交
284
  bool    ascFill = FILL_IS_ASC_FILL(pFillInfo);
285

286 287 288
#if 0
  ASSERT(ascFill && (pFillInfo->currentKey >= pFillInfo->start) || (!ascFill && (pFillInfo->currentKey <= pFillInfo->start)));
#endif
289 290

  while (pFillInfo->numOfCurrent < outputRows) {
291
    int64_t ts = ((int64_t*)pTsCol->pData)[pFillInfo->index];
292 293

    // set the next value for interpolation
5
54liuyao 已提交
294 295
    if (pFillInfo->currentKey < ts && ascFill) {
      SRowVal* pRVal = pFillInfo->type == TSDB_FILL_NEXT ? &pFillInfo->next : &pFillInfo->prev;
296
      copyCurrentRowIntoBuf(pFillInfo, pFillInfo->index, pRVal, false);
5
54liuyao 已提交
297 298
    } else if (pFillInfo->currentKey > ts && !ascFill) {
      SRowVal* pRVal = pFillInfo->type == TSDB_FILL_NEXT ? &pFillInfo->prev : &pFillInfo->next;
299
      copyCurrentRowIntoBuf(pFillInfo, pFillInfo->index, pRVal, false);
300 301
    }

X
Xiaoyu Wang 已提交
302 303
    if (((pFillInfo->currentKey < ts && ascFill) || (pFillInfo->currentKey > ts && !ascFill)) &&
        pFillInfo->numOfCurrent < outputRows) {
304
      // fill the gap between two input rows
X
Xiaoyu Wang 已提交
305 306
      while (((pFillInfo->currentKey < ts && ascFill) || (pFillInfo->currentKey > ts && !ascFill)) &&
             pFillInfo->numOfCurrent < outputRows) {
307
        doFillOneRow(pFillInfo, pBlock, pFillInfo->pSrcBlock, ts, false);
308 309 310 311 312 313 314 315
      }

      // output buffer is full, abort
      if (pFillInfo->numOfCurrent == outputRows) {
        pFillInfo->numOfTotal += pFillInfo->numOfCurrent;
        return outputRows;
      }
    } else {
316
      ASSERT(pFillInfo->currentKey == ts);
317
      int32_t index = pBlock->info.rows;
318

319
      if (pFillInfo->type == TSDB_FILL_NEXT) {
320
        int32_t nextRowIndex = pFillInfo->index + 1;
321 322 323 324 325 326
        if ((pFillInfo->index + 1) < pFillInfo->numOfRows) {
          copyCurrentRowIntoBuf(pFillInfo, nextRowIndex, &pFillInfo->next, false);
        } else {
          // reset to null after last row
          copyCurrentRowIntoBuf(pFillInfo, nextRowIndex, &pFillInfo->next, true);
        }
327 328
      }

H
Haojun Liao 已提交
329
      // copy rows to dst buffer
330 331 332
      for (int32_t i = 0; i < pFillInfo->numOfCols; ++i) {
        SFillColInfo* pCol = &pFillInfo->pFillCol[i];

333
        int32_t dstSlotId = GET_DEST_SLOT_ID(pCol);
334

335
        SColumnInfoData* pDst = taosArrayGet(pBlock->pDataBlock, dstSlotId);
H
Haojun Liao 已提交
336
        SColumnInfoData* pSrc = taosArrayGet(pFillInfo->pSrcBlock->pDataBlock, dstSlotId);
337 338

        char* src = colDataGetData(pSrc, pFillInfo->index);
H
Haojun Liao 已提交
339
        if (!colDataIsNull_s(pSrc, pFillInfo->index)) {
340
          colDataSetVal(pDst, index, src, false);
H
Haojun Liao 已提交
341
          saveColData(pFillInfo->prev.pRowVal, i, src, false);
342 343 344
          if (pFillInfo->srcTsSlotId == dstSlotId) {
            pFillInfo->prev.key = *(int64_t*)src;
          }
H
Haojun Liao 已提交
345
        } else {  // the value is null
346
          if (pDst->info.type == TSDB_DATA_TYPE_TIMESTAMP) {
347
            colDataSetVal(pDst, index, (const char*)&pFillInfo->currentKey, false);
348 349
          } else {  // i > 0 and data is null , do interpolation
            if (pFillInfo->type == TSDB_FILL_PREV) {
H
Haojun Liao 已提交
350
              SArray*     p = FILL_IS_ASC_FILL(pFillInfo) ? pFillInfo->prev.pRowVal : pFillInfo->next.pRowVal;
351 352 353 354
              SGroupKeys* pKey = taosArrayGet(p, i);
              doSetVal(pDst, index, pKey);
            } else if (pFillInfo->type == TSDB_FILL_LINEAR) {
              bool isNull = colDataIsNull_s(pSrc, pFillInfo->index);
355
              colDataSetVal(pDst, index, src, isNull);
H
Haojun Liao 已提交
356
              saveColData(pFillInfo->prev.pRowVal, i, src, isNull);  // todo:
D
dapan1121 已提交
357
            } else if (pFillInfo->type == TSDB_FILL_NULL || pFillInfo->type == TSDB_FILL_NULL_F) {
358
              colDataSetNULL(pDst, index);
359
            } else if (pFillInfo->type == TSDB_FILL_NEXT) {
H
Haojun Liao 已提交
360
              SArray*     p = FILL_IS_ASC_FILL(pFillInfo) ? pFillInfo->next.pRowVal : pFillInfo->prev.pRowVal;
361 362 363 364 365 366
              SGroupKeys* pKey = taosArrayGet(p, i);
              doSetVal(pDst, index, pKey);
            } else {
              SVariant* pVar = &pFillInfo->pFillCol[i].fillVal;
              doSetUserSpecifiedValue(pDst, pVar, index, pFillInfo->currentKey);
            }
367 368 369 370 371
          }
        }
      }

      // set the tag value for final result
X
Xiaoyu Wang 已提交
372 373 374
      SInterval* pInterval = &pFillInfo->interval;
      pFillInfo->currentKey =
          taosTimeAdd(pFillInfo->currentKey, pInterval->sliding * step, pInterval->slidingUnit, pInterval->precision);
375

376
      pBlock->info.rows += 1;
377 378 379 380 381 382 383 384 385 386 387 388 389
      pFillInfo->index += 1;
      pFillInfo->numOfCurrent += 1;
    }

    if (pFillInfo->index >= pFillInfo->numOfRows || pFillInfo->numOfCurrent >= outputRows) {
      pFillInfo->numOfTotal += pFillInfo->numOfCurrent;
      return pFillInfo->numOfCurrent;
    }
  }

  return pFillInfo->numOfCurrent;
}

390
static void saveColData(SArray* rowBuf, int32_t columnIndex, const char* src, bool isNull) {
X
Xiaoyu Wang 已提交
391
  SGroupKeys* pKey = taosArrayGet(rowBuf, columnIndex);
392 393 394
  if (isNull) {
    pKey->isNull = true;
  } else {
H
Haojun Liao 已提交
395 396 397 398 399
    if (IS_VAR_DATA_TYPE(pKey->type)) {
      memcpy(pKey->pData, src, varDataTLen(src));
    } else {
      memcpy(pKey->pData, src, pKey->bytes);
    }
400 401 402 403 404
    pKey->isNull = false;
  }
}

static int64_t appendFilledResult(SFillInfo* pFillInfo, SSDataBlock* pBlock, int64_t resultCapacity) {
405 406 407 408 409 410
  /*
   * These data are generated according to fill strategy, since the current timestamp is out of the time window of
   * real result set. Note that we need to keep the direct previous result rows, to generated the filled data.
   */
  pFillInfo->numOfCurrent = 0;
  while (pFillInfo->numOfCurrent < resultCapacity) {
411
    doFillOneRow(pFillInfo, pBlock, pFillInfo->pSrcBlock, pFillInfo->start, true);
412 413 414 415
  }

  pFillInfo->numOfTotal += pFillInfo->numOfCurrent;

H
Haojun Liao 已提交
416
  ASSERT(pFillInfo->numOfCurrent == resultCapacity);
417 418 419 420 421 422 423 424 425 426 427
  return resultCapacity;
}

static int32_t taosNumOfRemainRows(SFillInfo* pFillInfo) {
  if (pFillInfo->numOfRows == 0 || (pFillInfo->numOfRows > 0 && pFillInfo->index >= pFillInfo->numOfRows)) {
    return 0;
  }

  return pFillInfo->numOfRows - pFillInfo->index;
}

H
Haojun Liao 已提交
428
struct SFillInfo* taosCreateFillInfo(TSKEY skey, int32_t numOfFillCols, int32_t numOfNotFillCols, int32_t capacity,
429
                                     SInterval* pInterval, int32_t fillType, struct SFillColInfo* pCol,
430
                                     int32_t primaryTsSlotId, int32_t order, const char* id) {
431 432 433 434
  if (fillType == TSDB_FILL_NONE) {
    return NULL;
  }

wafwerar's avatar
wafwerar 已提交
435
  SFillInfo* pFillInfo = taosMemoryCalloc(1, sizeof(SFillInfo));
436 437 438 439 440
  if (pFillInfo == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

441
  pFillInfo->order = order;
442 443
  pFillInfo->srcTsSlotId = primaryTsSlotId;

5
54liuyao 已提交
444
  for (int32_t i = 0; i < numOfNotFillCols; ++i) {
445
    SFillColInfo* p = &pCol[i + numOfFillCols];
5
54liuyao 已提交
446
    int32_t       srcSlotId = GET_DEST_SLOT_ID(p);
447 448 449 450 451 452
    if (srcSlotId == primaryTsSlotId) {
      pFillInfo->tsSlotId = i + numOfFillCols;
      break;
    }
  }

453
  taosResetFillInfo(pFillInfo, skey);
454

X
Xiaoyu Wang 已提交
455 456
  pFillInfo->type = fillType;
  pFillInfo->pFillCol = pCol;
H
Haojun Liao 已提交
457
  pFillInfo->numOfCols = numOfFillCols + numOfNotFillCols;
X
Xiaoyu Wang 已提交
458 459 460 461
  pFillInfo->alloc = capacity;
  pFillInfo->id = id;
  pFillInfo->interval = *pInterval;

H
Haojun Liao 已提交
462 463
  pFillInfo->next.pRowVal = taosArrayInit(pFillInfo->numOfCols, sizeof(SGroupKeys));
  pFillInfo->prev.pRowVal = taosArrayInit(pFillInfo->numOfCols, sizeof(SGroupKeys));
464 465

  initBeforeAfterDataBuf(pFillInfo);
466 467 468 469
  return pFillInfo;
}

void taosResetFillInfo(SFillInfo* pFillInfo, TSKEY startTimestamp) {
X
Xiaoyu Wang 已提交
470 471 472 473 474
  pFillInfo->start = startTimestamp;
  pFillInfo->currentKey = startTimestamp;
  pFillInfo->end = startTimestamp;
  pFillInfo->index = -1;
  pFillInfo->numOfRows = 0;
475
  pFillInfo->numOfCurrent = 0;
X
Xiaoyu Wang 已提交
476
  pFillInfo->numOfTotal = 0;
477 478 479 480 481 482
}

void* taosDestroyFillInfo(SFillInfo* pFillInfo) {
  if (pFillInfo == NULL) {
    return NULL;
  }
H
Haojun Liao 已提交
483 484
  for (int32_t i = 0; i < taosArrayGetSize(pFillInfo->prev.pRowVal); ++i) {
    SGroupKeys* pKey = taosArrayGet(pFillInfo->prev.pRowVal, i);
485 486
    taosMemoryFree(pKey->pData);
  }
H
Haojun Liao 已提交
487 488 489
  taosArrayDestroy(pFillInfo->prev.pRowVal);
  for (int32_t i = 0; i < taosArrayGetSize(pFillInfo->next.pRowVal); ++i) {
    SGroupKeys* pKey = taosArrayGet(pFillInfo->next.pRowVal, i);
490 491
    taosMemoryFree(pKey->pData);
  }
H
Haojun Liao 已提交
492
  taosArrayDestroy(pFillInfo->next.pRowVal);
493

5
54liuyao 已提交
494 495 496
  //  for (int32_t i = 0; i < pFillInfo->numOfTags; ++i) {
  //    taosMemoryFreeClear(pFillInfo->pTags[i].tagVal);
  //  }
497

498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
  // free pFillCol
  if (pFillInfo->pFillCol) {
    for (int32_t i = 0; i < pFillInfo->numOfCols; i++) {
      SFillColInfo* pCol = &pFillInfo->pFillCol[i];
      if (!pCol->notFillCol) {
        if (pCol->fillVal.nType == TSDB_DATA_TYPE_VARBINARY || pCol->fillVal.nType == TSDB_DATA_TYPE_VARCHAR ||
            pCol->fillVal.nType == TSDB_DATA_TYPE_NCHAR || pCol->fillVal.nType == TSDB_DATA_TYPE_JSON) {
          if (pCol->fillVal.pz) {
            taosMemoryFree(pCol->fillVal.pz);
            pCol->fillVal.pz = NULL;
          }
        }
      }
    }
  }

wafwerar's avatar
wafwerar 已提交
514 515 516
  taosMemoryFreeClear(pFillInfo->pTags);
  taosMemoryFreeClear(pFillInfo->pFillCol);
  taosMemoryFreeClear(pFillInfo);
517 518 519 520 521 522 523 524
  return NULL;
}

void taosFillSetStartInfo(SFillInfo* pFillInfo, int32_t numOfRows, TSKEY endKey) {
  if (pFillInfo->type == TSDB_FILL_NONE) {
    return;
  }

525
  // the endKey is now the aligned time window value. truncate time window isn't correct.
526
  pFillInfo->end = endKey;
527 528 529 530 531 532

#if 0
  if (pFillInfo->order == TSDB_ORDER_ASC) {
    ASSERT(pFillInfo->start <= pFillInfo->end);
  } else {
    ASSERT(pFillInfo->start >= pFillInfo->end);
533
  }
534
#endif
535

X
Xiaoyu Wang 已提交
536
  pFillInfo->index = 0;
537 538 539 540
  pFillInfo->numOfRows = numOfRows;
}

void taosFillSetInputDataBlock(SFillInfo* pFillInfo, const SSDataBlock* pInput) {
X
Xiaoyu Wang 已提交
541
  pFillInfo->pSrcBlock = (SSDataBlock*)pInput;
542 543
}

544 545 546 547 548 549 550
void taosFillUpdateStartTimestampInfo(SFillInfo* pFillInfo, int64_t ts) {
  pFillInfo->start = ts;
  pFillInfo->currentKey = ts;
}

bool taosFillNotStarted(const SFillInfo* pFillInfo) {return pFillInfo->start == pFillInfo->currentKey;}

551 552 553 554 555 556
bool taosFillHasMoreResults(SFillInfo* pFillInfo) {
  int32_t remain = taosNumOfRemainRows(pFillInfo);
  if (remain > 0) {
    return true;
  }

557 558 559
  bool ascFill = FILL_IS_ASC_FILL(pFillInfo);
  if (pFillInfo->numOfTotal > 0 &&
      (((pFillInfo->end > pFillInfo->start) && ascFill) || (pFillInfo->end < pFillInfo->start && !ascFill))) {
560 561 562 563 564 565 566
    return getNumOfResultsAfterFillGap(pFillInfo, pFillInfo->end, 4096) > 0;
  }

  return false;
}

int64_t getNumOfResultsAfterFillGap(SFillInfo* pFillInfo, TSKEY ekey, int32_t maxNumOfRows) {
X
Xiaoyu Wang 已提交
567
  int32_t  numOfRows = taosNumOfRemainRows(pFillInfo);
568 569

  TSKEY ekey1 = ekey;
H
Hongze Cheng 已提交
570

571 572
  int64_t numOfRes = -1;
  if (numOfRows > 0) {  // still fill gap within current data block, not generating data after the result set.
D
dapan1121 已提交
573 574
    SColumnInfoData* pCol = taosArrayGet(pFillInfo->pSrcBlock->pDataBlock, pFillInfo->srcTsSlotId);    
    int64_t* tsList = (int64_t*)pCol->pData;
S
slzhou 已提交
575
    TSKEY lastKey = tsList[pFillInfo->numOfRows - 1];
X
Xiaoyu Wang 已提交
576 577
    numOfRes = taosTimeCountInterval(lastKey, pFillInfo->currentKey, pFillInfo->interval.sliding,
                                     pFillInfo->interval.slidingUnit, pFillInfo->interval.precision);
578
    numOfRes += 1;
H
Haojun Liao 已提交
579
    ASSERT(numOfRes >= numOfRows);
X
Xiaoyu Wang 已提交
580
  } else {  // reach the end of data
581
    if ((ekey1 < pFillInfo->currentKey && FILL_IS_ASC_FILL(pFillInfo)) ||
582
        (ekey1 > pFillInfo->currentKey && !FILL_IS_ASC_FILL(pFillInfo))) {
583 584
      return 0;
    }
585

X
Xiaoyu Wang 已提交
586 587
    numOfRes = taosTimeCountInterval(ekey1, pFillInfo->currentKey, pFillInfo->interval.sliding,
                                     pFillInfo->interval.slidingUnit, pFillInfo->interval.precision);
588 589 590 591 592 593
    numOfRes += 1;
  }

  return (numOfRes > maxNumOfRows) ? maxNumOfRows : numOfRes;
}

X
Xiaoyu Wang 已提交
594 595
int32_t taosGetLinearInterpolationVal(SPoint* point, int32_t outputType, SPoint* point1, SPoint* point2,
                                      int32_t inputType) {
596 597 598 599
  double v1 = -1, v2 = -1;
  GET_TYPED_DATA(v1, double, inputType, point1->val);
  GET_TYPED_DATA(v2, double, inputType, point2->val);

600 601 602 603 604 605
  double r = 0;
  if (!IS_BOOLEAN_TYPE(inputType)) {
    r = DO_INTERPOLATION(v1, v2, point1->key, point2->key, point->key);
  } else {
    r = (v1 < 1 || v2 < 1) ? 0 : 1;
  }
606 607 608 609 610
  SET_TYPED_DATA(point->val, outputType, r);

  return TSDB_CODE_SUCCESS;
}

611
int64_t taosFillResultDataBlock(SFillInfo* pFillInfo, SSDataBlock* p, int32_t capacity) {
612 613 614
  int32_t remain = taosNumOfRemainRows(pFillInfo);

  int64_t numOfRes = getNumOfResultsAfterFillGap(pFillInfo, pFillInfo->end, capacity);
H
Haojun Liao 已提交
615
  ASSERT(numOfRes <= capacity);
616 617 618

  // no data existed for fill operation now, append result according to the fill strategy
  if (remain == 0) {
619
    appendFilledResult(pFillInfo, p, numOfRes);
620
  } else {
X
Xiaoyu Wang 已提交
621
    fillResultImpl(pFillInfo, p, (int32_t)numOfRes);
H
Haojun Liao 已提交
622
    ASSERT(numOfRes == pFillInfo->numOfCurrent);
623 624
  }

625
  qDebug("fill:%p, generated fill result, src block:%d, index:%d, brange:%" PRId64 "-%" PRId64 ", currentKey:%" PRId64
626 627 628
         ", current : % d, total : % d, %s",
         pFillInfo, pFillInfo->numOfRows, pFillInfo->index, pFillInfo->start, pFillInfo->end, pFillInfo->currentKey,
         pFillInfo->numOfCurrent, pFillInfo->numOfTotal, pFillInfo->id);
629 630 631

  return numOfRes;
}
632

X
Xiaoyu Wang 已提交
633
int64_t getFillInfoStart(struct SFillInfo* pFillInfo) { return pFillInfo->start; }
634

H
Haojun Liao 已提交
635
SFillColInfo* createFillColInfo(SExprInfo* pExpr, int32_t numOfFillExpr, SExprInfo* pNotFillExpr,
636 637
                                int32_t numOfNoFillExpr, const struct SNodeListNode* pValNode) {
  SFillColInfo* pFillCol = taosMemoryCalloc(numOfFillExpr + numOfNoFillExpr, sizeof(SFillColInfo));
638 639 640 641
  if (pFillCol == NULL) {
    return NULL;
  }

X
Xiaoyu Wang 已提交
642
  size_t len = (pValNode != NULL) ? LIST_LENGTH(pValNode->pNodeList) : 0;
H
Haojun Liao 已提交
643
  for (int32_t i = 0; i < numOfFillExpr; ++i) {
644 645
    SExprInfo* pExprInfo = &pExpr[i];
    pFillCol[i].pExpr = pExprInfo;
H
Haojun Liao 已提交
646
    pFillCol[i].notFillCol = false;
647

648 649 650
    // todo refactor
    if (len > 0) {
      // if the user specified value is less than the column, alway use the last one as the fill value
X
Xiaoyu Wang 已提交
651
      int32_t index = (i >= len) ? (len - 1) : i;
652 653

      SValueNode* pv = (SValueNode*)nodesListGetNode(pValNode->pNodeList, index);
X
Xiaoyu Wang 已提交
654
      nodesValueNodeToVariant(pv, &pFillCol[i].fillVal);
655
    }
656
  }
G
Ganlin Zhao 已提交
657
  pFillCol->numOfFillExpr = numOfFillExpr;
658

659
  for (int32_t i = 0; i < numOfNoFillExpr; ++i) {
H
Haojun Liao 已提交
660 661 662 663 664
    SExprInfo* pExprInfo = &pNotFillExpr[i];
    pFillCol[i + numOfFillExpr].pExpr = pExprInfo;
    pFillCol[i + numOfFillExpr].notFillCol = true;
  }

665
  return pFillCol;
666
}