tfill.c 24.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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/>.
 */

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

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

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

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

H
Haojun Liao 已提交
36 37 38
#define GET_DEST_SLOT_ID(_p) ((_p)->pExpr->base.resSchema.slotId)

static void doSetVal(SColumnInfoData* pDstColInfoData, int32_t rowIndex, const SGroupKeys* pKey);
39
static bool fillIfWindowPseudoColumn(SFillInfo* pFillInfo, SFillColInfo* pCol, SColumnInfoData* pDstColInfoData, int32_t rowIndex);
H
Haojun Liao 已提交
40 41 42 43 44 45 46

static void setNullRow(SSDataBlock* pBlock, SFillInfo* pFillInfo, int32_t rowIndex) {
  for(int32_t i = 0; i < pFillInfo->numOfCols; ++i) {
    SFillColInfo* pCol = &pFillInfo->pFillCol[i];
    int32_t dstSlotId = GET_DEST_SLOT_ID(pCol);
    SColumnInfoData* pDstColInfo = taosArrayGet(pBlock->pDataBlock, dstSlotId);
    if (pCol->notFillCol) {
47 48
      bool filled = fillIfWindowPseudoColumn(pFillInfo, pCol, pDstColInfo, rowIndex);
      if (!filled) {
H
Haojun Liao 已提交
49
        SArray* p = FILL_IS_ASC_FILL(pFillInfo) ? pFillInfo->prev.pRowVal : pFillInfo->next.pRowVal;
H
Haojun Liao 已提交
50 51 52
        SGroupKeys* pKey = taosArrayGet(p, i);
        doSetVal(pDstColInfo, rowIndex, pKey);
      }
53
    } else {
H
Haojun Liao 已提交
54
      colDataAppendNULL(pDstColInfo, rowIndex);
55
    }
56 57 58
  }
}

59
static void doSetUserSpecifiedValue(SColumnInfoData* pDst, SVariant* pVar, int32_t rowIndex, int64_t currentKey) {
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  if (pDst->info.type == TSDB_DATA_TYPE_FLOAT) {
    float v = 0;
    GET_TYPED_DATA(v, float, pVar->nType, &pVar->i);
    colDataAppend(pDst, rowIndex, (char*)&v, false);
  } else if (pDst->info.type == TSDB_DATA_TYPE_DOUBLE) {
    double v = 0;
    GET_TYPED_DATA(v, double, pVar->nType, &pVar->i);
    colDataAppend(pDst, rowIndex, (char*)&v, false);
  } else if (IS_SIGNED_NUMERIC_TYPE(pDst->info.type)) {
    int64_t v = 0;
    GET_TYPED_DATA(v, int64_t, pVar->nType, &pVar->i);
    colDataAppend(pDst, rowIndex, (char*)&v, false);
  } else if (pDst->info.type == TSDB_DATA_TYPE_TIMESTAMP) {
    colDataAppend(pDst, rowIndex, (const char*)&currentKey, false);
  } else {  // varchar/nchar data
    colDataAppendNULL(pDst, rowIndex);
  }
}

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
//fill windows pseudo column, _wstart, _wend, _wduration and return true, otherwise return false
static bool fillIfWindowPseudoColumn(SFillInfo* pFillInfo, SFillColInfo* pCol, SColumnInfoData* pDstColInfoData, int32_t rowIndex) {
  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) {
      colDataAppend(pDstColInfoData, rowIndex, (const char*)&pFillInfo->currentKey, false);
      return true;
    } else if (pCol->pExpr->base.pParam[0].pCol->colType == COLUMN_TYPE_WINDOW_END) {
      //TODO: include endpoint
      SInterval* pInterval = &pFillInfo->interval;
      int32_t step = (pFillInfo->order == TSDB_ORDER_ASC) ? 1 : -1;
      int64_t windowEnd =
          taosTimeAdd(pFillInfo->currentKey, pInterval->sliding * step, pInterval->slidingUnit, pInterval->precision);
      colDataAppend(pDstColInfoData, rowIndex, (const char*)&windowEnd, false);
      return true;
    } else if (pCol->pExpr->base.pParam[0].pCol->colType == COLUMN_TYPE_WINDOW_DURATION) {
      //TODO: include endpoint
      colDataAppend(pDstColInfoData, rowIndex, (const char*)&pFillInfo->interval.sliding, false);
      return true;
    }
  }
105 106 107
  return false;
}

108 109
static void doFillOneRow(SFillInfo* pFillInfo, SSDataBlock* pBlock, SSDataBlock* pSrcBlock, int64_t ts,
                         bool outOfBound) {
X
Xiaoyu Wang 已提交
110
  SPoint  point1, point2, point;
111 112
  int32_t step = GET_FORWARD_DIRECTION_FACTOR(pFillInfo->order);

113
  // set the primary timestamp column value
114
  int32_t index = pBlock->info.rows;
115 116 117

  // set the other values
  if (pFillInfo->type == TSDB_FILL_PREV) {
H
Haojun Liao 已提交
118
    SArray* p = FILL_IS_ASC_FILL(pFillInfo)? pFillInfo->prev.pRowVal : pFillInfo->next.pRowVal;
119

120
    for (int32_t i = 0; i < pFillInfo->numOfCols; ++i) {
121 122 123
      SFillColInfo* pCol = &pFillInfo->pFillCol[i];

      SColumnInfoData* pDstColInfoData = taosArrayGet(pBlock->pDataBlock, GET_DEST_SLOT_ID(pCol));
124 125
      bool filled = fillIfWindowPseudoColumn(pFillInfo, pCol, pDstColInfoData, index);
      if (!filled) {
126 127 128
        SGroupKeys* pKey = taosArrayGet(p, i);
        doSetVal(pDstColInfoData, index, pKey);
      }
129 130
    }
  } else if (pFillInfo->type == TSDB_FILL_NEXT) {
H
Haojun Liao 已提交
131
    SArray* p = FILL_IS_ASC_FILL(pFillInfo) ? pFillInfo->next.pRowVal : pFillInfo->prev.pRowVal;
132
    // todo  refactor: start from 0 not 1
133
    for (int32_t i = 0; i < pFillInfo->numOfCols; ++i) {
134 135
      SFillColInfo* pCol = &pFillInfo->pFillCol[i];
      SColumnInfoData* pDstColInfoData = taosArrayGet(pBlock->pDataBlock, GET_DEST_SLOT_ID(pCol));
136 137
      bool filled = fillIfWindowPseudoColumn(pFillInfo, pCol, pDstColInfoData, index);
      if (!filled) {
138 139 140
        SGroupKeys* pKey = taosArrayGet(p, i);
        doSetVal(pDstColInfoData, index, pKey);
      }
141 142 143
    }
  } else if (pFillInfo->type == TSDB_FILL_LINEAR) {
    // TODO : linear interpolation supports NULL value
144
    if (outOfBound) {
H
Haojun Liao 已提交
145
      setNullRow(pBlock, pFillInfo, index);
146
    } else {
147
      for (int32_t i = 0; i < pFillInfo->numOfCols; ++i) {
148 149
        SFillColInfo* pCol = &pFillInfo->pFillCol[i];

X
Xiaoyu Wang 已提交
150
        int32_t          dstSlotId = GET_DEST_SLOT_ID(pCol);
151
        SColumnInfoData* pDstCol = taosArrayGet(pBlock->pDataBlock, dstSlotId);
H
Haojun Liao 已提交
152 153 154
        int16_t          type = pDstCol->info.type;

        if (pCol->notFillCol) {
155 156
          bool filled = fillIfWindowPseudoColumn(pFillInfo, pCol, pDstCol, index);
          if (!filled) {
H
Haojun Liao 已提交
157
            SArray* p = FILL_IS_ASC_FILL(pFillInfo) ? pFillInfo->prev.pRowVal : pFillInfo->next.pRowVal;
H
Haojun Liao 已提交
158 159 160 161
            SGroupKeys* pKey = taosArrayGet(p, i);
            doSetVal(pDstCol, index, pKey);
          }
        } else {
H
Haojun Liao 已提交
162
          SGroupKeys* pKey = taosArrayGet(pFillInfo->prev.pRowVal, i);
H
Haojun Liao 已提交
163 164 165 166
          if (IS_VAR_DATA_TYPE(type) || type == TSDB_DATA_TYPE_BOOL || pKey->isNull) {
            colDataAppendNULL(pDstCol, index);
            continue;
          }
167

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

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

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

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

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

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

H
Haojun Liao 已提交
193 194 195 196
      int32_t slotId = GET_DEST_SLOT_ID(pCol);
      SColumnInfoData* pDst = taosArrayGet(pBlock->pDataBlock, slotId);

      if (pCol->notFillCol) {
197 198
        bool filled = fillIfWindowPseudoColumn(pFillInfo, pCol, pDst, index);
        if (!filled) {
H
Haojun Liao 已提交
199
          SArray* p = FILL_IS_ASC_FILL(pFillInfo) ? pFillInfo->prev.pRowVal : pFillInfo->next.pRowVal;
H
Haojun Liao 已提交
200 201 202 203 204 205 206
          SGroupKeys* pKey = taosArrayGet(p, i);
          doSetVal(pDst, index, pKey);
        }
      } 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 220 221 222 223 224 225 226
void doSetVal(SColumnInfoData* pDstCol, int32_t rowIndex, const SGroupKeys* pKey) {
  if (pKey->isNull) {
    colDataAppendNULL(pDstCol, rowIndex);
  } else {
    colDataAppend(pDstCol, rowIndex, pKey->pData, false);
  }
}

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 250
static void saveColData(SArray* rowBuf, int32_t columnIndex, const char* src, bool isNull);

static void copyCurrentRowIntoBuf(SFillInfo* pFillInfo, int32_t rowIndex, SArray* pRow) {
251
  for (int32_t i = 0; i < pFillInfo->numOfCols; ++i) {
H
Haojun Liao 已提交
252 253 254
    int32_t type = pFillInfo->pFillCol[i].pExpr->pExpr->nodeType;
    if (type == QUERY_NODE_COLUMN) {
      int32_t srcSlotId = GET_DEST_SLOT_ID(&pFillInfo->pFillCol[i]);
255

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

H
Haojun Liao 已提交
258 259 260 261 262 263 264 265 266 267 268 269
      bool  isNull = colDataIsNull_s(pSrcCol, rowIndex);
      char* p = colDataGetData(pSrcCol, rowIndex);
      saveColData(pRow, i, p, isNull);
    } else if (type == QUERY_NODE_OPERATOR) {
      SColumnInfoData* pSrcCol = taosArrayGet(pFillInfo->pSrcBlock->pDataBlock, i);

      bool             isNull = colDataIsNull_s(pSrcCol, rowIndex);
      char*            p = colDataGetData(pSrcCol, rowIndex);
      saveColData(pRow, i, p, isNull);
    } else {
      ASSERT(0);
    }
270 271 272
  }
}

273
static int32_t fillResultImpl(SFillInfo* pFillInfo, SSDataBlock* pBlock, int32_t outputRows) {
274 275
  pFillInfo->numOfCurrent = 0;

276
  SColumnInfoData* pTsCol = taosArrayGet(pFillInfo->pSrcBlock->pDataBlock, pFillInfo->srcTsSlotId);
277 278

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

281 282 283
#if 0
  ASSERT(ascFill && (pFillInfo->currentKey >= pFillInfo->start) || (!ascFill && (pFillInfo->currentKey <= pFillInfo->start)));
#endif
284 285

  while (pFillInfo->numOfCurrent < outputRows) {
286
    int64_t ts = ((int64_t*)pTsCol->pData)[pFillInfo->index];
287 288

    // set the next value for interpolation
289
    if ((pFillInfo->currentKey < ts && ascFill) || (pFillInfo->currentKey > ts && !ascFill)) {
H
Haojun Liao 已提交
290
      copyCurrentRowIntoBuf(pFillInfo, pFillInfo->index, pFillInfo->next.pRowVal);
291 292
    }

X
Xiaoyu Wang 已提交
293 294
    if (((pFillInfo->currentKey < ts && ascFill) || (pFillInfo->currentKey > ts && !ascFill)) &&
        pFillInfo->numOfCurrent < outputRows) {
295
      // fill the gap between two input rows
X
Xiaoyu Wang 已提交
296 297
      while (((pFillInfo->currentKey < ts && ascFill) || (pFillInfo->currentKey > ts && !ascFill)) &&
             pFillInfo->numOfCurrent < outputRows) {
298
        doFillOneRow(pFillInfo, pBlock, pFillInfo->pSrcBlock, ts, false);
299 300 301 302 303 304 305 306
      }

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

310
      if (pFillInfo->type == TSDB_FILL_NEXT && (pFillInfo->index + 1) < pFillInfo->numOfRows) {
311
        int32_t nextRowIndex = pFillInfo->index + 1;
H
Haojun Liao 已提交
312
        copyCurrentRowIntoBuf(pFillInfo, nextRowIndex, pFillInfo->next.pRowVal);
313 314
      }

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

319
        int32_t dstSlotId = GET_DEST_SLOT_ID(pCol);
320

321
        SColumnInfoData* pDst = taosArrayGet(pBlock->pDataBlock, dstSlotId);
H
Haojun Liao 已提交
322
        SColumnInfoData* pSrc = taosArrayGet(pFillInfo->pSrcBlock->pDataBlock, dstSlotId);
323 324

        char* src = colDataGetData(pSrc, pFillInfo->index);
H
Haojun Liao 已提交
325 326
        if (!colDataIsNull_s(pSrc, pFillInfo->index)) {
          colDataAppend(pDst, index, src, false);
H
Haojun Liao 已提交
327
          saveColData(pFillInfo->prev.pRowVal, i, src, false);
H
Haojun Liao 已提交
328
        } else {  // the value is null
329 330 331 332
          if (pDst->info.type == TSDB_DATA_TYPE_TIMESTAMP) {
            colDataAppend(pDst, index, (const char*)&pFillInfo->currentKey, false);
          } else {  // i > 0 and data is null , do interpolation
            if (pFillInfo->type == TSDB_FILL_PREV) {
H
Haojun Liao 已提交
333
              SArray*     p = FILL_IS_ASC_FILL(pFillInfo) ? pFillInfo->prev.pRowVal : pFillInfo->next.pRowVal;
334 335 336 337 338
              SGroupKeys* pKey = taosArrayGet(p, i);
              doSetVal(pDst, index, pKey);
            } else if (pFillInfo->type == TSDB_FILL_LINEAR) {
              bool isNull = colDataIsNull_s(pSrc, pFillInfo->index);
              colDataAppend(pDst, index, src, isNull);
H
Haojun Liao 已提交
339
              saveColData(pFillInfo->prev.pRowVal, i, src, isNull);  // todo:
340 341 342
            } else if (pFillInfo->type == TSDB_FILL_NULL) {
              colDataAppendNULL(pDst, index);
            } else if (pFillInfo->type == TSDB_FILL_NEXT) {
H
Haojun Liao 已提交
343
              SArray*     p = FILL_IS_ASC_FILL(pFillInfo) ? pFillInfo->next.pRowVal : pFillInfo->prev.pRowVal;
344 345 346 347 348 349
              SGroupKeys* pKey = taosArrayGet(p, i);
              doSetVal(pDst, index, pKey);
            } else {
              SVariant* pVar = &pFillInfo->pFillCol[i].fillVal;
              doSetUserSpecifiedValue(pDst, pVar, index, pFillInfo->currentKey);
            }
350 351 352 353 354
          }
        }
      }

      // set the tag value for final result
X
Xiaoyu Wang 已提交
355 356 357 358
      //      setTagsValue(pFillInfo, data, pFillInfo->numOfCurrent);
      SInterval* pInterval = &pFillInfo->interval;
      pFillInfo->currentKey =
          taosTimeAdd(pFillInfo->currentKey, pInterval->sliding * step, pInterval->slidingUnit, pInterval->precision);
359

360
      pBlock->info.rows += 1;
361 362 363 364 365 366 367 368 369 370 371 372 373
      pFillInfo->index += 1;
      pFillInfo->numOfCurrent += 1;
    }

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

  return pFillInfo->numOfCurrent;
}

374
static void saveColData(SArray* rowBuf, int32_t columnIndex, const char* src, bool isNull) {
X
Xiaoyu Wang 已提交
375
  SGroupKeys* pKey = taosArrayGet(rowBuf, columnIndex);
376 377 378
  if (isNull) {
    pKey->isNull = true;
  } else {
H
Haojun Liao 已提交
379 380 381 382 383
    if (IS_VAR_DATA_TYPE(pKey->type)) {
      memcpy(pKey->pData, src, varDataTLen(src));
    } else {
      memcpy(pKey->pData, src, pKey->bytes);
    }
384 385 386 387 388
    pKey->isNull = false;
  }
}

static int64_t appendFilledResult(SFillInfo* pFillInfo, SSDataBlock* pBlock, int64_t resultCapacity) {
389 390 391 392 393 394
  /*
   * 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) {
395
    doFillOneRow(pFillInfo, pBlock, pFillInfo->pSrcBlock, pFillInfo->start, true);
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
  }

  pFillInfo->numOfTotal += pFillInfo->numOfCurrent;

  assert(pFillInfo->numOfCurrent == resultCapacity);
  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 已提交
412
struct SFillInfo* taosCreateFillInfo(TSKEY skey, int32_t numOfFillCols, int32_t numOfNotFillCols, int32_t capacity,
413
                                     SInterval* pInterval, int32_t fillType, struct SFillColInfo* pCol,
414
                                     int32_t primaryTsSlotId, int32_t order, const char* id) {
415 416 417 418
  if (fillType == TSDB_FILL_NONE) {
    return NULL;
  }

wafwerar's avatar
wafwerar 已提交
419
  SFillInfo* pFillInfo = taosMemoryCalloc(1, sizeof(SFillInfo));
420 421 422 423 424
  if (pFillInfo == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

425
  pFillInfo->order = order;
426 427 428 429
  pFillInfo->srcTsSlotId = primaryTsSlotId;

  for(int32_t i = 0; i < numOfNotFillCols; ++i) {
    SFillColInfo* p = &pCol[i + numOfFillCols];
H
Haojun Liao 已提交
430
    int32_t srcSlotId = GET_DEST_SLOT_ID(p);
431 432 433 434 435 436
    if (srcSlotId == primaryTsSlotId) {
      pFillInfo->tsSlotId = i + numOfFillCols;
      break;
    }
  }

437
  taosResetFillInfo(pFillInfo, skey);
438

X
Xiaoyu Wang 已提交
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
  switch (fillType) {
    case FILL_MODE_NONE:
      pFillInfo->type = TSDB_FILL_NONE;
      break;
    case FILL_MODE_PREV:
      pFillInfo->type = TSDB_FILL_PREV;
      break;
    case FILL_MODE_NULL:
      pFillInfo->type = TSDB_FILL_NULL;
      break;
    case FILL_MODE_LINEAR:
      pFillInfo->type = TSDB_FILL_LINEAR;
      break;
    case FILL_MODE_NEXT:
      pFillInfo->type = TSDB_FILL_NEXT;
      break;
    case FILL_MODE_VALUE:
      pFillInfo->type = TSDB_FILL_SET_VALUE;
      break;
458 459 460 461 462
    default:
      terrno = TSDB_CODE_INVALID_PARA;
      return NULL;
  }

X
Xiaoyu Wang 已提交
463 464
  pFillInfo->type = fillType;
  pFillInfo->pFillCol = pCol;
H
Haojun Liao 已提交
465
  pFillInfo->numOfCols = numOfFillCols + numOfNotFillCols;
X
Xiaoyu Wang 已提交
466 467 468 469
  pFillInfo->alloc = capacity;
  pFillInfo->id = id;
  pFillInfo->interval = *pInterval;

H
Haojun Liao 已提交
470 471
  pFillInfo->next.pRowVal = taosArrayInit(pFillInfo->numOfCols, sizeof(SGroupKeys));
  pFillInfo->prev.pRowVal = taosArrayInit(pFillInfo->numOfCols, sizeof(SGroupKeys));
472 473

  initBeforeAfterDataBuf(pFillInfo);
474 475 476 477
  return pFillInfo;
}

void taosResetFillInfo(SFillInfo* pFillInfo, TSKEY startTimestamp) {
X
Xiaoyu Wang 已提交
478 479 480 481 482
  pFillInfo->start = startTimestamp;
  pFillInfo->currentKey = startTimestamp;
  pFillInfo->end = startTimestamp;
  pFillInfo->index = -1;
  pFillInfo->numOfRows = 0;
483
  pFillInfo->numOfCurrent = 0;
X
Xiaoyu Wang 已提交
484
  pFillInfo->numOfTotal = 0;
485 486 487 488 489 490
}

void* taosDestroyFillInfo(SFillInfo* pFillInfo) {
  if (pFillInfo == NULL) {
    return NULL;
  }
H
Haojun Liao 已提交
491 492
  for (int32_t i = 0; i < taosArrayGetSize(pFillInfo->prev.pRowVal); ++i) {
    SGroupKeys* pKey = taosArrayGet(pFillInfo->prev.pRowVal, i);
493 494
    taosMemoryFree(pKey->pData);
  }
H
Haojun Liao 已提交
495 496 497
  taosArrayDestroy(pFillInfo->prev.pRowVal);
  for (int32_t i = 0; i < taosArrayGetSize(pFillInfo->next.pRowVal); ++i) {
    SGroupKeys* pKey = taosArrayGet(pFillInfo->next.pRowVal, i);
498 499
    taosMemoryFree(pKey->pData);
  }
H
Haojun Liao 已提交
500
  taosArrayDestroy(pFillInfo->next.pRowVal);
501

H
Haojun Liao 已提交
502 503 504
//  for (int32_t i = 0; i < pFillInfo->numOfTags; ++i) {
//    taosMemoryFreeClear(pFillInfo->pTags[i].tagVal);
//  }
505

wafwerar's avatar
wafwerar 已提交
506 507 508
  taosMemoryFreeClear(pFillInfo->pTags);
  taosMemoryFreeClear(pFillInfo->pFillCol);
  taosMemoryFreeClear(pFillInfo);
509 510 511
  return NULL;
}

512 513 514 515 516 517 518 519
void taosFillSetDataOrderInfo(SFillInfo* pFillInfo, int32_t order) {
  if (pFillInfo == NULL || (order != TSDB_ORDER_ASC && order != TSDB_ORDER_DESC)) {
    return;
  }

  pFillInfo->order = order;
}

520 521 522 523 524 525 526
void taosFillSetStartInfo(SFillInfo* pFillInfo, int32_t numOfRows, TSKEY endKey) {
  if (pFillInfo->type == TSDB_FILL_NONE) {
    return;
  }

  pFillInfo->end = endKey;
  if (!FILL_IS_ASC_FILL(pFillInfo)) {
527
    pFillInfo->end = taosTimeTruncate(endKey, &pFillInfo->interval, pFillInfo->interval.precision);
528 529
  }

X
Xiaoyu Wang 已提交
530
  pFillInfo->index = 0;
531 532 533 534
  pFillInfo->numOfRows = numOfRows;
}

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

bool taosFillHasMoreResults(SFillInfo* pFillInfo) {
  int32_t remain = taosNumOfRemainRows(pFillInfo);
  if (remain > 0) {
    return true;
  }

544 545 546
  bool ascFill = FILL_IS_ASC_FILL(pFillInfo);
  if (pFillInfo->numOfTotal > 0 &&
      (((pFillInfo->end > pFillInfo->start) && ascFill) || (pFillInfo->end < pFillInfo->start && !ascFill))) {
547 548 549 550 551 552 553
    return getNumOfResultsAfterFillGap(pFillInfo, pFillInfo->end, 4096) > 0;
  }

  return false;
}

int64_t getNumOfResultsAfterFillGap(SFillInfo* pFillInfo, TSKEY ekey, int32_t maxNumOfRows) {
554
  SColumnInfoData* pCol = taosArrayGet(pFillInfo->pSrcBlock->pDataBlock, pFillInfo->srcTsSlotId);
555

X
Xiaoyu Wang 已提交
556 557
  int64_t* tsList = (int64_t*)pCol->pData;
  int32_t  numOfRows = taosNumOfRemainRows(pFillInfo);
558 559 560

  TSKEY ekey1 = ekey;
  if (!FILL_IS_ASC_FILL(pFillInfo)) {
561
    pFillInfo->end = taosTimeTruncate(ekey, &pFillInfo->interval, pFillInfo->interval.precision);
562 563 564 565
  }

  int64_t numOfRes = -1;
  if (numOfRows > 0) {  // still fill gap within current data block, not generating data after the result set.
S
slzhou 已提交
566
    TSKEY lastKey = tsList[pFillInfo->numOfRows - 1];
X
Xiaoyu Wang 已提交
567 568
    numOfRes = taosTimeCountInterval(lastKey, pFillInfo->currentKey, pFillInfo->interval.sliding,
                                     pFillInfo->interval.slidingUnit, pFillInfo->interval.precision);
569 570
    numOfRes += 1;
    assert(numOfRes >= numOfRows);
X
Xiaoyu Wang 已提交
571
  } else {  // reach the end of data
572 573 574 575
    if ((ekey1 < pFillInfo->currentKey && FILL_IS_ASC_FILL(pFillInfo)) ||
        (ekey1 > pFillInfo->currentKey && !FILL_IS_ASC_FILL(pFillInfo))) {
      return 0;
    }
X
Xiaoyu Wang 已提交
576 577
    numOfRes = taosTimeCountInterval(ekey1, pFillInfo->currentKey, pFillInfo->interval.sliding,
                                     pFillInfo->interval.slidingUnit, pFillInfo->interval.precision);
578 579 580 581 582 583
    numOfRes += 1;
  }

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

X
Xiaoyu Wang 已提交
584 585
int32_t taosGetLinearInterpolationVal(SPoint* point, int32_t outputType, SPoint* point1, SPoint* point2,
                                      int32_t inputType) {
586 587 588 589 590 591 592 593 594 595
  double v1 = -1, v2 = -1;
  GET_TYPED_DATA(v1, double, inputType, point1->val);
  GET_TYPED_DATA(v2, double, inputType, point2->val);

  double r = DO_INTERPOLATION(v1, v2, point1->key, point2->key, point->key);
  SET_TYPED_DATA(point->val, outputType, r);

  return TSDB_CODE_SUCCESS;
}

596
int64_t taosFillResultDataBlock(SFillInfo* pFillInfo, SSDataBlock* p, int32_t capacity) {
597 598 599 600 601 602 603
  int32_t remain = taosNumOfRemainRows(pFillInfo);

  int64_t numOfRes = getNumOfResultsAfterFillGap(pFillInfo, pFillInfo->end, capacity);
  assert(numOfRes <= capacity);

  // no data existed for fill operation now, append result according to the fill strategy
  if (remain == 0) {
604
    appendFilledResult(pFillInfo, p, numOfRes);
605
  } else {
X
Xiaoyu Wang 已提交
606
    fillResultImpl(pFillInfo, p, (int32_t)numOfRes);
607 608 609
    assert(numOfRes == pFillInfo->numOfCurrent);
  }

610
  qDebug("fill:%p, generated fill result, src block:%d, index:%d, brange:%" PRId64 "-%" PRId64 ", currentKey:%" PRId64
611 612 613
         ", current : % d, total : % d, %s",
         pFillInfo, pFillInfo->numOfRows, pFillInfo->index, pFillInfo->start, pFillInfo->end, pFillInfo->currentKey,
         pFillInfo->numOfCurrent, pFillInfo->numOfTotal, pFillInfo->id);
614 615 616

  return numOfRes;
}
617

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

H
Haojun Liao 已提交
620 621 622
SFillColInfo* createFillColInfo(SExprInfo* pExpr, int32_t numOfFillExpr, SExprInfo* pNotFillExpr,
                                int32_t numOfNotFillExpr, const struct SNodeListNode* pValNode) {
  SFillColInfo* pFillCol = taosMemoryCalloc(numOfFillExpr + numOfNotFillExpr, sizeof(SFillColInfo));
623 624 625 626
  if (pFillCol == NULL) {
    return NULL;
  }

X
Xiaoyu Wang 已提交
627
  size_t len = (pValNode != NULL) ? LIST_LENGTH(pValNode->pNodeList) : 0;
H
Haojun Liao 已提交
628
  for (int32_t i = 0; i < numOfFillExpr; ++i) {
629 630
    SExprInfo* pExprInfo = &pExpr[i];
    pFillCol[i].pExpr = pExprInfo;
H
Haojun Liao 已提交
631
    pFillCol[i].notFillCol = false;
632

633 634 635
    // 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 已提交
636
      int32_t index = (i >= len) ? (len - 1) : i;
637 638

      SValueNode* pv = (SValueNode*)nodesListGetNode(pValNode->pNodeList, index);
X
Xiaoyu Wang 已提交
639
      nodesValueNodeToVariant(pv, &pFillCol[i].fillVal);
640
    }
641 642
  }

H
Haojun Liao 已提交
643 644 645 646 647 648
  for(int32_t i = 0; i < numOfNotFillExpr; ++i) {
    SExprInfo* pExprInfo = &pNotFillExpr[i];
    pFillCol[i + numOfFillExpr].pExpr = pExprInfo;
    pFillCol[i + numOfFillExpr].notFillCol = true;
  }

649
  return pFillCol;
650
}