qFill.c 18.7 KB
Newer Older
H
hzcheng 已提交
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 "os.h"
H
Haojun Liao 已提交
17

H
Haojun Liao 已提交
18
#include "qAggMain.h"
19
#include "taosdef.h"
H
hzcheng 已提交
20
#include "taosmsg.h"
H
Haojun Liao 已提交
21 22 23 24 25
#include "ttype.h"

#include "qFill.h"
#include "qExtbuffer.h"
#include "queryLog.h"
H
Haojun Liao 已提交
26
#include "qExecutor.h"
H
hzcheng 已提交
27

28
#define FILL_IS_ASC_FILL(_f) ((_f)->order == TSDB_ORDER_ASC)
H
Haojun Liao 已提交
29
#define DO_INTERPOLATION(_v1, _v2, _k1, _k2, _k) ((_v1) + ((_v2) - (_v1)) * (((double)(_k)) - ((double)(_k1))) / (((double)(_k2)) - ((double)(_k1))))
H
hzcheng 已提交
30

H
Haojun Liao 已提交
31
static void setTagsValue(SFillInfo* pFillInfo, void** data, int32_t genRows) {
32 33
  for(int32_t j = 0; j < pFillInfo->numOfCols; ++j) {
    SFillColInfo* pCol = &pFillInfo->pFillCol[j];
H
Haojun Liao 已提交
34
    if (TSDB_COL_IS_NORMAL_COL(pCol->flag) || TSDB_COL_IS_UD_COL(pCol->flag)) {
35 36 37
      continue;
    }

H
Haojun Liao 已提交
38
    char* val1 = elePtrAt(data[j], pCol->col.bytes, genRows);
39

H
Haojun Liao 已提交
40 41 42 43 44 45 46 47
    assert(pCol->tagIndex >= 0 && pCol->tagIndex < pFillInfo->numOfTags);
    SFillTagColInfo* pTag = &pFillInfo->pTags[pCol->tagIndex];

    assert (pTag->col.colId == pCol->col.colId);
    assignVal(val1, pTag->tagVal, pCol->col.bytes, pCol->col.type);
  }
}

H
Haojun Liao 已提交
48
static void setNullValueForRow(SFillInfo* pFillInfo, void** data, int32_t numOfCol, int32_t rowIndex) {
H
Haojun Liao 已提交
49 50 51 52
  // the first are always the timestamp column, so start from the second column.
  for (int32_t i = 1; i < numOfCol; ++i) {
    SFillColInfo* pCol = &pFillInfo->pFillCol[i];

H
Haojun Liao 已提交
53
    char* output = elePtrAt(data[i], pCol->col.bytes, rowIndex);
H
Haojun Liao 已提交
54
    setNull(output, pCol->col.type, pCol->col.bytes);
H
hzcheng 已提交
55 56 57
  }
}

H
Haojun Liao 已提交
58
static void doFillOneRowResult(SFillInfo* pFillInfo, void** data, char** srcData, int64_t ts, bool outOfBound) {
H
Haojun Liao 已提交
59 60
  char* prev = pFillInfo->prevValues;
  char* next = pFillInfo->nextValues;
H
hzcheng 已提交
61 62

  SPoint point1, point2, point;
63
  int32_t step = GET_FORWARD_DIRECTION_FACTOR(pFillInfo->order);
H
hzcheng 已提交
64

H
Haojun Liao 已提交
65 66
  // set the primary timestamp column value
  int32_t index = pFillInfo->numOfCurrent;
H
Haojun Liao 已提交
67
  char* val = elePtrAt(data[0], TSDB_KEYSIZE, index);
H
Haojun Liao 已提交
68
  *(TSKEY*) val = pFillInfo->currentKey;
H
hzcheng 已提交
69 70

  // set the other values
H
Haojun Liao 已提交
71 72
  if (pFillInfo->type == TSDB_FILL_PREV) {
    char* p = FILL_IS_ASC_FILL(pFillInfo) ? prev : next;
73 74 75 76 77 78 79 80

    if (p != NULL) {
      for (int32_t i = 1; i < pFillInfo->numOfCols; ++i) {
        SFillColInfo* pCol = &pFillInfo->pFillCol[i];
        if (TSDB_COL_IS_TAG(pCol->flag)) {
          continue;
        }

H
Haojun Liao 已提交
81
        char* output = elePtrAt(data[i], pCol->col.bytes, index);
82 83 84 85 86 87 88 89
        assignVal(output, p + pCol->col.offset, pCol->col.bytes, pCol->col.type);
      }
    } else {  // no prev value yet, set the value for NULL
      setNullValueForRow(pFillInfo, data, pFillInfo->numOfCols, index);
    }
  } else if (pFillInfo->type == TSDB_FILL_NEXT) {
    char* p = FILL_IS_ASC_FILL(pFillInfo)? next : prev;

90
    if (p != NULL) {
H
Haojun Liao 已提交
91
      for (int32_t i = 1; i < pFillInfo->numOfCols; ++i) {
92
        SFillColInfo* pCol = &pFillInfo->pFillCol[i];
H
Haojun Liao 已提交
93 94
        if (TSDB_COL_IS_TAG(pCol->flag)) {
          continue;
H
hzcheng 已提交
95
        }
H
hjxilinx 已提交
96

H
Haojun Liao 已提交
97
        char* output = elePtrAt(data[i], pCol->col.bytes, index);
H
Haojun Liao 已提交
98
        assignVal(output, p + pCol->col.offset, pCol->col.bytes, pCol->col.type);
H
hzcheng 已提交
99
      }
100
    } else { // no prev value yet, set the value for NULL
H
Haojun Liao 已提交
101
      setNullValueForRow(pFillInfo, data, pFillInfo->numOfCols, index);
H
hzcheng 已提交
102
    }
H
Haojun Liao 已提交
103 104 105
  } else if (pFillInfo->type == TSDB_FILL_LINEAR) {
    if (prev != NULL && !outOfBound) {
      for (int32_t i = 1; i < pFillInfo->numOfCols; ++i) {
106
        SFillColInfo* pCol = &pFillInfo->pFillCol[i];
H
Haojun Liao 已提交
107 108 109 110
        if (TSDB_COL_IS_TAG(pCol->flag)) {
          continue;
        }

111 112
        int16_t type  = pCol->col.type;
        int16_t bytes = pCol->col.bytes;
113

H
Haojun Liao 已提交
114
        char *val1 = elePtrAt(data[i], pCol->col.bytes, index);
H
Haojun Liao 已提交
115
        if (type == TSDB_DATA_TYPE_BINARY|| type == TSDB_DATA_TYPE_NCHAR || type == TSDB_DATA_TYPE_BOOL) {
116
          setNull(val1, pCol->col.type, bytes);
H
hzcheng 已提交
117 118 119
          continue;
        }

D
dapan1121 已提交
120
        bool exceedMax = false, exceedMin = false;
H
Haojun Liao 已提交
121 122
        point1 = (SPoint){.key = *(TSKEY*)(prev), .val = prev + pCol->col.offset};
        point2 = (SPoint){.key = ts, .val = srcData[i] + pFillInfo->index * bytes};
123 124 125 126
        if (isNull(point2.val, type)) {
          setNull(val1, pCol->col.type, bytes);
          continue;
        }
H
Haojun Liao 已提交
127
        point  = (SPoint){.key = pFillInfo->currentKey, .val = val1};
D
dapan1121 已提交
128
        taosGetLinearInterpolationVal(&point, type, &point1, &point2, type, &exceedMax, &exceedMin);
H
hzcheng 已提交
129 130
      }
    } else {
H
Haojun Liao 已提交
131
      setNullValueForRow(pFillInfo, data, pFillInfo->numOfCols, index);
H
hzcheng 已提交
132
    }
H
Haojun Liao 已提交
133
  } else { // fill the default value */
H
Haojun Liao 已提交
134
    for (int32_t i = 1; i < pFillInfo->numOfCols; ++i) {
135
      SFillColInfo* pCol = &pFillInfo->pFillCol[i];
H
Haojun Liao 已提交
136
      if (TSDB_COL_IS_TAG(pCol->flag)/* || IS_VAR_DATA_TYPE(pCol->col.type)*/) {
H
Haojun Liao 已提交
137 138 139
        continue;
      }

H
Haojun Liao 已提交
140
      char* val1 = elePtrAt(data[i], pCol->col.bytes, index);
141
      assignVal(val1, (char*)&pCol->fillVal.i, pCol->col.bytes, pCol->col.type);
H
hzcheng 已提交
142 143 144
    }
  }

H
Haojun Liao 已提交
145 146
  setTagsValue(pFillInfo, data, index);
  pFillInfo->currentKey = taosTimeAdd(pFillInfo->currentKey, pFillInfo->interval.sliding * step, pFillInfo->interval.slidingUnit, pFillInfo->precision);
147
  pFillInfo->numOfCurrent++;
H
hzcheng 已提交
148 149
}

H
Haojun Liao 已提交
150 151
static void initBeforeAfterDataBuf(SFillInfo* pFillInfo, char** next) {
  if (*next != NULL) {
152 153
    return;
  }
154

H
Haojun Liao 已提交
155
  *next = calloc(1, pFillInfo->rowSize);
156 157
  for (int i = 1; i < pFillInfo->numOfCols; i++) {
    SFillColInfo* pCol = &pFillInfo->pFillCol[i];
H
Haojun Liao 已提交
158 159 160 161
    setNull(*next + pCol->col.offset, pCol->col.type, pCol->col.bytes);
  }
}

H
Haojun Liao 已提交
162
static void copyCurrentRowIntoBuf(SFillInfo* pFillInfo, char** srcData, char* buf) {
H
Haojun Liao 已提交
163 164 165 166
  int32_t rowIndex = pFillInfo->index;
  for (int32_t i = 0; i < pFillInfo->numOfCols; ++i) {
    SFillColInfo* pCol = &pFillInfo->pFillCol[i];
    memcpy(buf + pCol->col.offset, srcData[i] + rowIndex * pCol->col.bytes, pCol->col.bytes);
167 168 169
  }
}

H
Haojun Liao 已提交
170
static int32_t fillResultImpl(SFillInfo* pFillInfo, void** data, int32_t outputRows) {
171
  pFillInfo->numOfCurrent = 0;
H
hzcheng 已提交
172

H
Haojun Liao 已提交
173 174 175
  char** srcData = pFillInfo->pData;
  char** prev = &pFillInfo->prevValues;
  char** next = &pFillInfo->nextValues;
H
hzcheng 已提交
176

177
  int32_t step = GET_FORWARD_DIRECTION_FACTOR(pFillInfo->order);
H
hzcheng 已提交
178

H
Haojun Liao 已提交
179 180
  if (FILL_IS_ASC_FILL(pFillInfo)) {
    assert(pFillInfo->currentKey >= pFillInfo->start);
H
hzcheng 已提交
181
  } else {
H
Haojun Liao 已提交
182 183
    assert(pFillInfo->currentKey <= pFillInfo->start);
  }
H
hzcheng 已提交
184

H
Haojun Liao 已提交
185 186 187
  while (pFillInfo->numOfCurrent < outputRows) {
    int64_t ts = ((int64_t*)pFillInfo->pData[0])[pFillInfo->index];

188
    // set the next value for interpolation
H
Haojun Liao 已提交
189 190 191
    if ((pFillInfo->currentKey < ts && FILL_IS_ASC_FILL(pFillInfo)) ||
        (pFillInfo->currentKey > ts && !FILL_IS_ASC_FILL(pFillInfo))) {
      initBeforeAfterDataBuf(pFillInfo, next);
H
Haojun Liao 已提交
192
      copyCurrentRowIntoBuf(pFillInfo, srcData, *next);
H
Haojun Liao 已提交
193 194 195 196
    }

    if (((pFillInfo->currentKey < ts && FILL_IS_ASC_FILL(pFillInfo)) || (pFillInfo->currentKey > ts && !FILL_IS_ASC_FILL(pFillInfo))) &&
        pFillInfo->numOfCurrent < outputRows) {
H
Haojun Liao 已提交
197

H
Haojun Liao 已提交
198 199 200 201 202
      // fill the gap between two actual input rows
      while (((pFillInfo->currentKey < ts && FILL_IS_ASC_FILL(pFillInfo)) ||
              (pFillInfo->currentKey > ts && !FILL_IS_ASC_FILL(pFillInfo))) &&
             pFillInfo->numOfCurrent < outputRows) {
        doFillOneRowResult(pFillInfo, data, srcData, ts, false);
H
hzcheng 已提交
203 204
      }

H
Haojun Liao 已提交
205 206 207 208 209 210 211 212
      // output buffer is full, abort
      if (pFillInfo->numOfCurrent == outputRows) {
        pFillInfo->numOfTotal += pFillInfo->numOfCurrent;
        return outputRows;
      }
    } else {
      assert(pFillInfo->currentKey == ts);
      initBeforeAfterDataBuf(pFillInfo, prev);
W
wpan 已提交
213 214 215 216 217 218
      if (pFillInfo->type == TSDB_FILL_NEXT && (pFillInfo->index + 1) < pFillInfo->numOfRows) {
        initBeforeAfterDataBuf(pFillInfo, next);
        ++pFillInfo->index;
        copyCurrentRowIntoBuf(pFillInfo, srcData, *next);
        --pFillInfo->index;
      }
H
hzcheng 已提交
219

H
Haojun Liao 已提交
220 221 222
      // assign rows to dst buffer
      for (int32_t i = 0; i < pFillInfo->numOfCols; ++i) {
        SFillColInfo* pCol = &pFillInfo->pFillCol[i];
H
Haojun Liao 已提交
223
        if (TSDB_COL_IS_TAG(pCol->flag)/* || IS_VAR_DATA_TYPE(pCol->col.type)*/) {
H
Haojun Liao 已提交
224
          continue;
H
hjxilinx 已提交
225
        }
226

H
Haojun Liao 已提交
227
        char* output = elePtrAt(data[i], pCol->col.bytes, pFillInfo->numOfCurrent);
H
Haojun Liao 已提交
228 229 230 231 232 233 234 235 236 237 238 239
        char* src = elePtrAt(srcData[i], pCol->col.bytes, pFillInfo->index);

        if (i == 0 || (pCol->functionId != TSDB_FUNC_COUNT && !isNull(src, pCol->col.type)) ||
            (pCol->functionId == TSDB_FUNC_COUNT && GET_INT64_VAL(src) != 0)) {
          assignVal(output, src, pCol->col.bytes, pCol->col.type);
          memcpy(*prev + pCol->col.offset, src, pCol->col.bytes);
        } else {  // i > 0 and data is null , do interpolation
          if (pFillInfo->type == TSDB_FILL_PREV) {
            assignVal(output, *prev + pCol->col.offset, pCol->col.bytes, pCol->col.type);
          } else if (pFillInfo->type == TSDB_FILL_LINEAR) {
            assignVal(output, src, pCol->col.bytes, pCol->col.type);
            memcpy(*prev + pCol->col.offset, src, pCol->col.bytes);
W
wpan 已提交
240 241 242 243 244 245
          } else if (pFillInfo->type == TSDB_FILL_NEXT) {
            if (*next) {
              assignVal(output, *next + pCol->col.offset, pCol->col.bytes, pCol->col.type);
            } else {
              setNull(output, pCol->col.type, pCol->col.bytes);
            }
W
wpan 已提交
246 247 248
            if (!FILL_IS_ASC_FILL(pFillInfo)) {
              memcpy(*prev + pCol->col.offset, output, pCol->col.bytes);
            }
H
Haojun Liao 已提交
249 250
          } else {
            assignVal(output, (char*)&pCol->fillVal.i, pCol->col.bytes, pCol->col.type);
H
hzcheng 已提交
251 252
          }
        }
H
hjxilinx 已提交
253
      }
H
hzcheng 已提交
254

H
Haojun Liao 已提交
255 256
      // set the tag value for final result
      setTagsValue(pFillInfo, data, pFillInfo->numOfCurrent);
H
hzcheng 已提交
257

H
Haojun Liao 已提交
258
      pFillInfo->currentKey = taosTimeAdd(pFillInfo->currentKey, pFillInfo->interval.sliding * step,
259
                                          pFillInfo->interval.slidingUnit, pFillInfo->precision);
H
Haojun Liao 已提交
260 261 262
      pFillInfo->index += 1;
      pFillInfo->numOfCurrent += 1;
    }
H
hzcheng 已提交
263

H
Haojun Liao 已提交
264 265 266
    if (pFillInfo->index >= pFillInfo->numOfRows || pFillInfo->numOfCurrent >= outputRows) {
      /* the raw data block is exhausted, next value does not exists */
      if (pFillInfo->index >= pFillInfo->numOfRows) {
H
Haojun Liao 已提交
267
        tfree(*next);
H
hzcheng 已提交
268
      }
H
Haojun Liao 已提交
269 270 271

      pFillInfo->numOfTotal += pFillInfo->numOfCurrent;
      return pFillInfo->numOfCurrent;
H
hzcheng 已提交
272 273
    }
  }
H
Haojun Liao 已提交
274 275

  return pFillInfo->numOfCurrent;
H
hzcheng 已提交
276
}
277

H
Haojun Liao 已提交
278
static int64_t appendFilledResult(SFillInfo* pFillInfo, void** output, int64_t resultCapacity) {
H
Haojun Liao 已提交
279 280 281 282 283 284 285 286
  /*
   * 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) {
    doFillOneRowResult(pFillInfo, output, pFillInfo->pData, pFillInfo->start, true);
  }
H
Haojun Liao 已提交
287

H
Haojun Liao 已提交
288
  pFillInfo->numOfTotal += pFillInfo->numOfCurrent;
H
Haojun Liao 已提交
289

H
Haojun Liao 已提交
290 291 292 293
  assert(pFillInfo->numOfCurrent == resultCapacity);
  return resultCapacity;
}

294 295 296
// there are no duplicated tags in the SFillTagColInfo list
static int32_t setTagColumnInfo(SFillInfo* pFillInfo, int32_t numOfCols, int32_t capacity) {
  int32_t rowsize = 0;
H
Haojun Liao 已提交
297
  int32_t numOfTags = 0;
298 299 300 301

  int32_t k = 0;
  for (int32_t i = 0; i < numOfCols; ++i) {
    SFillColInfo* pColInfo = &pFillInfo->pFillCol[i];
H
Haojun Liao 已提交
302
    pFillInfo->pData[i] = NULL;
303

H
Haojun Liao 已提交
304 305 306
    if (TSDB_COL_IS_TAG(pColInfo->flag) || pColInfo->col.type == TSDB_DATA_TYPE_BINARY) {
      numOfTags += 1;

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
      bool exists = false;
      int32_t index = -1;
      for (int32_t j = 0; j < k; ++j) {
        if (pFillInfo->pTags[j].col.colId == pColInfo->col.colId) {
          exists = true;
          index = j;
          break;
        }
      }

      if (!exists) {
        SSchema* pSchema = &pFillInfo->pTags[k].col;
        pSchema->colId = pColInfo->col.colId;
        pSchema->type  = pColInfo->col.type;
        pSchema->bytes = pColInfo->col.bytes;

        pFillInfo->pTags[k].tagVal = calloc(1, pColInfo->col.bytes);
        pColInfo->tagIndex = k;

        k += 1;
      } else {
        pColInfo->tagIndex = index;
      }
    }

    rowsize += pColInfo->col.bytes;
  }

H
Haojun Liao 已提交
335 336
  pFillInfo->numOfTags = numOfTags;

337 338 339 340 341 342 343 344 345 346 347 348
  assert(k <= pFillInfo->numOfTags);
  return rowsize;
}

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

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

349
SFillInfo* taosCreateFillInfo(int32_t order, TSKEY skey, int32_t numOfTags, int32_t capacity, int32_t numOfCols,
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
                            int64_t slidingTime, int8_t slidingUnit, int8_t precision, int32_t fillType,
                            SFillColInfo* pCol, void* handle) {
  if (fillType == TSDB_FILL_NONE) {
    return NULL;
  }

  SFillInfo* pFillInfo = calloc(1, sizeof(SFillInfo));
  taosResetFillInfo(pFillInfo, skey);

  pFillInfo->order     = order;
  pFillInfo->type      = fillType;
  pFillInfo->pFillCol  = pCol;
  pFillInfo->numOfTags = numOfTags;
  pFillInfo->numOfCols = numOfCols;
  pFillInfo->precision = precision;
  pFillInfo->alloc     = capacity;
  pFillInfo->handle    = handle;

  pFillInfo->interval.interval     = slidingTime;
  pFillInfo->interval.intervalUnit = slidingUnit;
  pFillInfo->interval.sliding      = slidingTime;
  pFillInfo->interval.slidingUnit  = slidingUnit;

  pFillInfo->pData = malloc(POINTER_BYTES * numOfCols);
H
Haojun Liao 已提交
374 375 376 377

//  if (numOfTags > 0) {
    pFillInfo->pTags = calloc(numOfCols, sizeof(SFillTagColInfo));
    for (int32_t i = 0; i < numOfCols; ++i) {
378 379
      pFillInfo->pTags[i].col.colId = -2;  // TODO
    }
H
Haojun Liao 已提交
380
//  }
381 382 383 384 385 386 387 388 389 390

  pFillInfo->rowSize = setTagColumnInfo(pFillInfo, pFillInfo->numOfCols, pFillInfo->alloc);
  assert(pFillInfo->rowSize > 0);

  return pFillInfo;
}

void taosResetFillInfo(SFillInfo* pFillInfo, TSKEY startTimestamp) {
  pFillInfo->start        = startTimestamp;
  pFillInfo->currentKey   = startTimestamp;
H
Haojun Liao 已提交
391
  pFillInfo->end          = startTimestamp;
392 393 394 395 396 397 398 399 400 401 402 403 404
  pFillInfo->index        = -1;
  pFillInfo->numOfRows    = 0;
  pFillInfo->numOfCurrent = 0;
  pFillInfo->numOfTotal   = 0;
}

void* taosDestroyFillInfo(SFillInfo* pFillInfo) {
  if (pFillInfo == NULL) {
    return NULL;
  }

  tfree(pFillInfo->prevValues);
  tfree(pFillInfo->nextValues);
H
Haojun Liao 已提交
405

H
Haojun Liao 已提交
406 407 408 409 410
  for(int32_t i = 0; i < pFillInfo->numOfTags; ++i) {
    tfree(pFillInfo->pTags[i].tagVal);
  }

  tfree(pFillInfo->pTags);
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
  
  tfree(pFillInfo->pData);
  tfree(pFillInfo->pFillCol);
  
  tfree(pFillInfo);
  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)) {
    pFillInfo->end = taosTimeTruncate(endKey, &pFillInfo->interval, pFillInfo->precision);
  }

  pFillInfo->index     = 0;
  pFillInfo->numOfRows = numOfRows;
}

H
Haojun Liao 已提交
433 434
void taosFillSetInputDataBlock(SFillInfo* pFillInfo, const SSDataBlock* pInput) {
  for (int32_t i = 0; i < pFillInfo->numOfCols; ++i) {
H
Haojun Liao 已提交
435 436
    SFillColInfo* pCol = &pFillInfo->pFillCol[i];

H
Haojun Liao 已提交
437
    SColumnInfoData* pColData = taosArrayGet(pInput->pDataBlock, i);
H
Haojun Liao 已提交
438
    pFillInfo->pData[i] = pColData->pData;
H
Haojun Liao 已提交
439

440
    if (TSDB_COL_IS_TAG(pCol->flag)) {  // copy the tag value to tag value buffer
H
Haojun Liao 已提交
441 442 443
      SFillTagColInfo* pTag = &pFillInfo->pTags[pCol->tagIndex];
      assert (pTag->col.colId == pCol->col.colId);
      memcpy(pTag->tagVal, pColData->pData, pCol->col.bytes);  // TODO not memcpy??
444 445 446 447 448
    }
  }
}

bool taosFillHasMoreResults(SFillInfo* pFillInfo) {
H
Haojun Liao 已提交
449 450 451 452 453 454 455 456 457 458 459
  int32_t remain = taosNumOfRemainRows(pFillInfo);
  if (remain > 0) {
    return true;
  }

  if (pFillInfo->numOfTotal > 0 && (((pFillInfo->end > pFillInfo->start) && FILL_IS_ASC_FILL(pFillInfo)) ||
                                    (pFillInfo->end < pFillInfo->start && !FILL_IS_ASC_FILL(pFillInfo)))) {
    return getNumOfResultsAfterFillGap(pFillInfo, pFillInfo->end, 4096) > 0;
  }

  return false;
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
}

int64_t getNumOfResultsAfterFillGap(SFillInfo* pFillInfo, TSKEY ekey, int32_t maxNumOfRows) {
  int64_t* tsList = (int64_t*) pFillInfo->pData[0];

  int32_t numOfRows = taosNumOfRemainRows(pFillInfo);

  TSKEY ekey1 = ekey;
  if (!FILL_IS_ASC_FILL(pFillInfo)) {
    pFillInfo->end = taosTimeTruncate(ekey, &pFillInfo->interval, pFillInfo->precision);
  }

  int64_t numOfRes = -1;
  if (numOfRows > 0) {  // still fill gap within current data block, not generating data after the result set.
    TSKEY lastKey = tsList[pFillInfo->numOfRows - 1];
    numOfRes = taosTimeCountInterval(
      lastKey,
      pFillInfo->currentKey,
      pFillInfo->interval.sliding,
      pFillInfo->interval.slidingUnit,
      pFillInfo->precision);
    numOfRes += 1;
    assert(numOfRes >= numOfRows);
  } else { // reach the end of data
    if ((ekey1 < pFillInfo->currentKey && FILL_IS_ASC_FILL(pFillInfo)) ||
        (ekey1 > pFillInfo->currentKey && !FILL_IS_ASC_FILL(pFillInfo))) {
      return 0;
    }
    numOfRes = taosTimeCountInterval(
      ekey1,
      pFillInfo->currentKey,
      pFillInfo->interval.sliding,
      pFillInfo->interval.slidingUnit,
      pFillInfo->precision);
    numOfRes += 1;
  }

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

D
dapan1121 已提交
500 501
int32_t taosGetLinearInterpolationVal(SPoint* point, int32_t outputType, SPoint* point1, SPoint* point2, int32_t inputType, bool *exceedMax, bool *exceedMin) {
  double v1 = -1, v2 = -1, vmax = -1, vmin = -1;
502 503
  GET_TYPED_DATA(v1, double, inputType, point1->val);
  GET_TYPED_DATA(v2, double, inputType, point2->val);
D
dapan1121 已提交
504
  GET_TYPED_DATA(vmax, double, outputType, getDataMax(outputType));
D
dapan1121 已提交
505
  GET_TYPED_DATA(vmin, double, outputType, getDataMin(outputType));
D
dapan1121 已提交
506
  
507
  double r = DO_INTERPOLATION(v1, v2, point1->key, point2->key, point->key);
D
dapan1121 已提交
508 509
  if (r >= vmax) {
    *exceedMax = true;
D
dapan1121 已提交
510 511
  } else if (r <= vmin) {
    *exceedMin = true;
D
dapan1121 已提交
512 513
  }

514
  SET_TYPED_DATA(point->val, outputType, r);
515 516 517 518

  return TSDB_CODE_SUCCESS;
}

H
Haojun Liao 已提交
519
int64_t taosFillResultDataBlock(SFillInfo* pFillInfo, void** output, int32_t capacity) {
H
Haojun Liao 已提交
520 521
  int32_t remain = taosNumOfRemainRows(pFillInfo);

522
  int64_t numOfRes = getNumOfResultsAfterFillGap(pFillInfo, pFillInfo->end, capacity);
H
Haojun Liao 已提交
523 524 525 526
  assert(numOfRes <= capacity);

  // no data existed for fill operation now, append result according to the fill strategy
  if (remain == 0) {
527
    appendFilledResult(pFillInfo, output, numOfRes);
H
Haojun Liao 已提交
528
  } else {
H
Haojun Liao 已提交
529
    fillResultImpl(pFillInfo, output, (int32_t) numOfRes);
H
Haojun Liao 已提交
530
    assert(numOfRes == pFillInfo->numOfCurrent);
H
Haojun Liao 已提交
531 532
  }

H
Haojun Liao 已提交
533 534 535
  qDebug("fill:%p, generated fill result, src block:%d, index:%d, brange:%"PRId64"-%"PRId64", currentKey:%"PRId64", current:%d, total:%d, %p",
      pFillInfo, pFillInfo->numOfRows, pFillInfo->index, pFillInfo->start, pFillInfo->end, pFillInfo->currentKey, pFillInfo->numOfCurrent,
         pFillInfo->numOfTotal, pFillInfo->handle);
H
Haojun Liao 已提交
536

H
Haojun Liao 已提交
537
  return numOfRes;
538
}