tfill.c 23.9 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"

5
54liuyao 已提交
23
#include "executorimpl.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 251 252 253
static void copyCurrentRowIntoBuf(SFillInfo* pFillInfo, int32_t rowIndex, SRowVal* pRowVal) {
  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, 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 296 297 298 299
    if (pFillInfo->currentKey < ts && ascFill) {
      SRowVal* pRVal = pFillInfo->type == TSDB_FILL_NEXT ? &pFillInfo->next : &pFillInfo->prev;
      copyCurrentRowIntoBuf(pFillInfo, pFillInfo->index, pRVal);
    } else if (pFillInfo->currentKey > ts && !ascFill) {
      SRowVal* pRVal = pFillInfo->type == TSDB_FILL_NEXT ? &pFillInfo->prev : &pFillInfo->next;
      copyCurrentRowIntoBuf(pFillInfo, pFillInfo->index, pRVal);
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 && (pFillInfo->index + 1) < pFillInfo->numOfRows) {
320
        int32_t nextRowIndex = pFillInfo->index + 1;
321
        copyCurrentRowIntoBuf(pFillInfo, nextRowIndex, &pFillInfo->next);
322 323
      }

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

328
        int32_t dstSlotId = GET_DEST_SLOT_ID(pCol);
329

330
        SColumnInfoData* pDst = taosArrayGet(pBlock->pDataBlock, dstSlotId);
H
Haojun Liao 已提交
331
        SColumnInfoData* pSrc = taosArrayGet(pFillInfo->pSrcBlock->pDataBlock, dstSlotId);
332 333

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

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

371
      pBlock->info.rows += 1;
372 373 374 375 376 377 378 379 380 381 382 383 384
      pFillInfo->index += 1;
      pFillInfo->numOfCurrent += 1;
    }

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

  return pFillInfo->numOfCurrent;
}

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

static int64_t appendFilledResult(SFillInfo* pFillInfo, SSDataBlock* pBlock, int64_t resultCapacity) {
400 401 402 403 404 405
  /*
   * 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) {
406
    doFillOneRow(pFillInfo, pBlock, pFillInfo->pSrcBlock, pFillInfo->start, true);
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
  }

  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 已提交
423
struct SFillInfo* taosCreateFillInfo(TSKEY skey, int32_t numOfFillCols, int32_t numOfNotFillCols, int32_t capacity,
424
                                     SInterval* pInterval, int32_t fillType, struct SFillColInfo* pCol,
425
                                     int32_t primaryTsSlotId, int32_t order, const char* id) {
426 427 428 429
  if (fillType == TSDB_FILL_NONE) {
    return NULL;
  }

wafwerar's avatar
wafwerar 已提交
430
  SFillInfo* pFillInfo = taosMemoryCalloc(1, sizeof(SFillInfo));
431 432 433 434 435
  if (pFillInfo == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

436
  pFillInfo->order = order;
437 438
  pFillInfo->srcTsSlotId = primaryTsSlotId;

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

448
  taosResetFillInfo(pFillInfo, skey);
449

X
Xiaoyu Wang 已提交
450 451
  pFillInfo->type = fillType;
  pFillInfo->pFillCol = pCol;
H
Haojun Liao 已提交
452
  pFillInfo->numOfCols = numOfFillCols + numOfNotFillCols;
X
Xiaoyu Wang 已提交
453 454 455 456
  pFillInfo->alloc = capacity;
  pFillInfo->id = id;
  pFillInfo->interval = *pInterval;

H
Haojun Liao 已提交
457 458
  pFillInfo->next.pRowVal = taosArrayInit(pFillInfo->numOfCols, sizeof(SGroupKeys));
  pFillInfo->prev.pRowVal = taosArrayInit(pFillInfo->numOfCols, sizeof(SGroupKeys));
459 460

  initBeforeAfterDataBuf(pFillInfo);
461 462 463 464
  return pFillInfo;
}

void taosResetFillInfo(SFillInfo* pFillInfo, TSKEY startTimestamp) {
X
Xiaoyu Wang 已提交
465 466 467 468 469
  pFillInfo->start = startTimestamp;
  pFillInfo->currentKey = startTimestamp;
  pFillInfo->end = startTimestamp;
  pFillInfo->index = -1;
  pFillInfo->numOfRows = 0;
470
  pFillInfo->numOfCurrent = 0;
X
Xiaoyu Wang 已提交
471
  pFillInfo->numOfTotal = 0;
472 473 474 475 476 477
}

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

5
54liuyao 已提交
489 490 491
  //  for (int32_t i = 0; i < pFillInfo->numOfTags; ++i) {
  //    taosMemoryFreeClear(pFillInfo->pTags[i].tagVal);
  //  }
492

493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
  // 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 已提交
509 510 511
  taosMemoryFreeClear(pFillInfo->pTags);
  taosMemoryFreeClear(pFillInfo->pFillCol);
  taosMemoryFreeClear(pFillInfo);
512 513 514 515 516 517 518 519 520 521
  return NULL;
}

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)) {
522
    pFillInfo->end = taosTimeTruncate(endKey, &pFillInfo->interval, pFillInfo->interval.precision);
523 524
  }

X
Xiaoyu Wang 已提交
525
  pFillInfo->index = 0;
526 527 528 529
  pFillInfo->numOfRows = numOfRows;
}

void taosFillSetInputDataBlock(SFillInfo* pFillInfo, const SSDataBlock* pInput) {
X
Xiaoyu Wang 已提交
530
  pFillInfo->pSrcBlock = (SSDataBlock*)pInput;
531 532 533 534 535 536 537 538
}

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

539 540 541
  bool ascFill = FILL_IS_ASC_FILL(pFillInfo);
  if (pFillInfo->numOfTotal > 0 &&
      (((pFillInfo->end > pFillInfo->start) && ascFill) || (pFillInfo->end < pFillInfo->start && !ascFill))) {
542 543 544 545 546 547 548
    return getNumOfResultsAfterFillGap(pFillInfo, pFillInfo->end, 4096) > 0;
  }

  return false;
}

int64_t getNumOfResultsAfterFillGap(SFillInfo* pFillInfo, TSKEY ekey, int32_t maxNumOfRows) {
X
Xiaoyu Wang 已提交
549
  int32_t  numOfRows = taosNumOfRemainRows(pFillInfo);
550 551

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

553 554
  int64_t numOfRes = -1;
  if (numOfRows > 0) {  // still fill gap within current data block, not generating data after the result set.
D
dapan1121 已提交
555 556
    SColumnInfoData* pCol = taosArrayGet(pFillInfo->pSrcBlock->pDataBlock, pFillInfo->srcTsSlotId);    
    int64_t* tsList = (int64_t*)pCol->pData;
S
slzhou 已提交
557
    TSKEY lastKey = tsList[pFillInfo->numOfRows - 1];
X
Xiaoyu Wang 已提交
558 559
    numOfRes = taosTimeCountInterval(lastKey, pFillInfo->currentKey, pFillInfo->interval.sliding,
                                     pFillInfo->interval.slidingUnit, pFillInfo->interval.precision);
560 561
    numOfRes += 1;
    assert(numOfRes >= numOfRows);
X
Xiaoyu Wang 已提交
562
  } else {  // reach the end of data
563
    if ((ekey1 < pFillInfo->currentKey && FILL_IS_ASC_FILL(pFillInfo)) ||
564
        (ekey1 >= pFillInfo->currentKey && !FILL_IS_ASC_FILL(pFillInfo))) {
565 566
      return 0;
    }
X
Xiaoyu Wang 已提交
567 568
    numOfRes = taosTimeCountInterval(ekey1, pFillInfo->currentKey, pFillInfo->interval.sliding,
                                     pFillInfo->interval.slidingUnit, pFillInfo->interval.precision);
569 570 571 572 573 574
    numOfRes += 1;
  }

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

X
Xiaoyu Wang 已提交
575 576
int32_t taosGetLinearInterpolationVal(SPoint* point, int32_t outputType, SPoint* point1, SPoint* point2,
                                      int32_t inputType) {
577 578 579 580
  double v1 = -1, v2 = -1;
  GET_TYPED_DATA(v1, double, inputType, point1->val);
  GET_TYPED_DATA(v2, double, inputType, point2->val);

581 582 583 584 585 586
  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;
  }
587 588 589 590 591
  SET_TYPED_DATA(point->val, outputType, r);

  return TSDB_CODE_SUCCESS;
}

592
int64_t taosFillResultDataBlock(SFillInfo* pFillInfo, SSDataBlock* p, int32_t capacity) {
593 594 595 596 597 598 599
  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) {
600
    appendFilledResult(pFillInfo, p, numOfRes);
601
  } else {
X
Xiaoyu Wang 已提交
602
    fillResultImpl(pFillInfo, p, (int32_t)numOfRes);
603 604 605
    assert(numOfRes == pFillInfo->numOfCurrent);
  }

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

  return numOfRes;
}
613

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

H
Haojun Liao 已提交
616
SFillColInfo* createFillColInfo(SExprInfo* pExpr, int32_t numOfFillExpr, SExprInfo* pNotFillExpr,
617 618
                                int32_t numOfNoFillExpr, const struct SNodeListNode* pValNode) {
  SFillColInfo* pFillCol = taosMemoryCalloc(numOfFillExpr + numOfNoFillExpr, sizeof(SFillColInfo));
619 620 621 622
  if (pFillCol == NULL) {
    return NULL;
  }

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

629 630 631
    // 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 已提交
632
      int32_t index = (i >= len) ? (len - 1) : i;
633 634

      SValueNode* pv = (SValueNode*)nodesListGetNode(pValNode->pNodeList, index);
X
Xiaoyu Wang 已提交
635
      nodesValueNodeToVariant(pv, &pFillCol[i].fillVal);
636
    }
637
  }
G
Ganlin Zhao 已提交
638
  pFillCol->numOfFillExpr = numOfFillExpr;
639

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

646
  return pFillCol;
647
}