tfill.c 19.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 "function.h"
17
#include "os.h"
18
#include "querynodes.h"
19 20

#include "taosdef.h"
H
Hongze Cheng 已提交
21
#include "tmsg.h"
22 23 24 25
#include "ttypes.h"

#include "tfill.h"
#include "function.h"
S
common  
Shengliang Guan 已提交
26
#include "tcommon.h"
27
#include "thash.h"
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#include "ttime.h"

#define FILL_IS_ASC_FILL(_f) ((_f)->order == TSDB_ORDER_ASC)
#define DO_INTERPOLATION(_v1, _v2, _k1, _k2, _k) ((_v1) + ((_v2) - (_v1)) * (((double)(_k)) - ((double)(_k1))) / (((double)(_k2)) - ((double)(_k1))))

static void setTagsValue(SFillInfo* pFillInfo, void** data, int32_t genRows) {
  for(int32_t j = 0; j < pFillInfo->numOfCols; ++j) {
    SFillColInfo* pCol = &pFillInfo->pFillCol[j];
    if (TSDB_COL_IS_NORMAL_COL(pCol->flag) || TSDB_COL_IS_UD_COL(pCol->flag)) {
      continue;
    }

    char* val1 = elePtrAt(data[j], pCol->col.bytes, genRows);

    assert(pCol->tagIndex >= 0 && pCol->tagIndex < pFillInfo->numOfTags);
    SFillTagColInfo* pTag = &pFillInfo->pTags[pCol->tagIndex];

45
//    assert (pTag->col.colId == pCol->col.colId);
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    assignVal(val1, pTag->tagVal, pCol->col.bytes, pCol->col.type);
  }
}

static void setNullValueForRow(SFillInfo* pFillInfo, void** data, int32_t numOfCol, int32_t rowIndex) {
  // 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];

    char* output = elePtrAt(data[i], pCol->col.bytes, rowIndex);
    setNull(output, pCol->col.type, pCol->col.bytes);
  }
}

static void doFillOneRowResult(SFillInfo* pFillInfo, void** data, char** srcData, int64_t ts, bool outOfBound) {
  char* prev = pFillInfo->prevValues;
  char* next = pFillInfo->nextValues;

  SPoint point1, point2, point;
  int32_t step = GET_FORWARD_DIRECTION_FACTOR(pFillInfo->order);

  // set the primary timestamp column value
  int32_t index = pFillInfo->numOfCurrent;
  char* val = elePtrAt(data[0], TSDB_KEYSIZE, index);
  *(TSKEY*) val = pFillInfo->currentKey;

  // set the other values
  if (pFillInfo->type == TSDB_FILL_PREV) {
    char* p = FILL_IS_ASC_FILL(pFillInfo) ? prev : next;

    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;
        }

        char* output = elePtrAt(data[i], pCol->col.bytes, index);
84
//        assignVal(output, p + pCol->offset, pCol->col.bytes, pCol->col.type);
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
      }
    } 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;

    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;
        }

        char* output = elePtrAt(data[i], pCol->col.bytes, index);
100
//        assignVal(output, p + pCol->offset, pCol->col.bytes, pCol->col.type);
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
      }
    } else { // no prev value yet, set the value for NULL
      setNullValueForRow(pFillInfo, data, pFillInfo->numOfCols, index);
    }
  } else if (pFillInfo->type == TSDB_FILL_LINEAR) {
    // TODO : linear interpolation supports NULL value
    if (prev != NULL && !outOfBound) {
      for (int32_t i = 1; i < pFillInfo->numOfCols; ++i) {
        SFillColInfo* pCol = &pFillInfo->pFillCol[i];
        if (TSDB_COL_IS_TAG(pCol->flag)) {
          continue;
        }

        int16_t type  = pCol->col.type;
        int16_t bytes = pCol->col.bytes;

        char *val1 = elePtrAt(data[i], pCol->col.bytes, index);
        if (type == TSDB_DATA_TYPE_BINARY|| type == TSDB_DATA_TYPE_NCHAR || type == TSDB_DATA_TYPE_BOOL) {
          setNull(val1, pCol->col.type, bytes);
          continue;
        }

123
        point1 = (SPoint){.key = *(TSKEY*)(prev), .val = prev + pCol->offset};
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
        point2 = (SPoint){.key = ts, .val = srcData[i] + pFillInfo->index * bytes};
        point  = (SPoint){.key = pFillInfo->currentKey, .val = val1};
        taosGetLinearInterpolationVal(&point, type, &point1, &point2, type);
      }
    } else {
      setNullValueForRow(pFillInfo, data, pFillInfo->numOfCols, index);
    }
  } else { // fill the default value */
    for (int32_t i = 1; i < pFillInfo->numOfCols; ++i) {
      SFillColInfo* pCol = &pFillInfo->pFillCol[i];
      if (TSDB_COL_IS_TAG(pCol->flag)/* || IS_VAR_DATA_TYPE(pCol->col.type)*/) {
        continue;
      }

      char* val1 = elePtrAt(data[i], pCol->col.bytes, index);
139
      assignVal(val1, (char*)&pCol->val, pCol->col.bytes, pCol->col.type);
140 141 142 143
    }
  }

  setTagsValue(pFillInfo, data, index);
144 145
  pFillInfo->currentKey = taosTimeAdd(pFillInfo->currentKey, pFillInfo->interval.sliding * step, pFillInfo->interval.slidingUnit,
      pFillInfo->interval.precision);
146 147 148 149 150 151 152 153
  pFillInfo->numOfCurrent++;
}

static void initBeforeAfterDataBuf(SFillInfo* pFillInfo, char** next) {
  if (*next != NULL) {
    return;
  }

wafwerar's avatar
wafwerar 已提交
154
  *next = taosMemoryCalloc(1, pFillInfo->rowSize);
155 156
  for (int i = 1; i < pFillInfo->numOfCols; i++) {
    SFillColInfo* pCol = &pFillInfo->pFillCol[i];
157
    setNull(*next + pCol->offset, pCol->col.type, pCol->col.bytes);
158 159 160 161 162 163 164
  }
}

static void copyCurrentRowIntoBuf(SFillInfo* pFillInfo, char** srcData, char* buf) {
  int32_t rowIndex = pFillInfo->index;
  for (int32_t i = 0; i < pFillInfo->numOfCols; ++i) {
    SFillColInfo* pCol = &pFillInfo->pFillCol[i];
165
    memcpy(buf + pCol->offset, srcData[i] + rowIndex * pCol->col.bytes, pCol->col.bytes);
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
  }
}

static int32_t fillResultImpl(SFillInfo* pFillInfo, void** data, int32_t outputRows) {
  pFillInfo->numOfCurrent = 0;

  char** srcData = pFillInfo->pData;
  char** prev = &pFillInfo->prevValues;
  char** next = &pFillInfo->nextValues;

  int32_t step = GET_FORWARD_DIRECTION_FACTOR(pFillInfo->order);

  if (FILL_IS_ASC_FILL(pFillInfo)) {
    assert(pFillInfo->currentKey >= pFillInfo->start);
  } else {
    assert(pFillInfo->currentKey <= pFillInfo->start);
  }

  while (pFillInfo->numOfCurrent < outputRows) {
    int64_t ts = ((int64_t*)pFillInfo->pData[0])[pFillInfo->index];

    // set the next value for interpolation
    if ((pFillInfo->currentKey < ts && FILL_IS_ASC_FILL(pFillInfo)) ||
        (pFillInfo->currentKey > ts && !FILL_IS_ASC_FILL(pFillInfo))) {
      initBeforeAfterDataBuf(pFillInfo, next);
      copyCurrentRowIntoBuf(pFillInfo, srcData, *next);
    }

    if (((pFillInfo->currentKey < ts && FILL_IS_ASC_FILL(pFillInfo)) || (pFillInfo->currentKey > ts && !FILL_IS_ASC_FILL(pFillInfo))) &&
        pFillInfo->numOfCurrent < outputRows) {

      // 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);
      }

      // output buffer is full, abort
      if (pFillInfo->numOfCurrent == outputRows) {
        pFillInfo->numOfTotal += pFillInfo->numOfCurrent;
        return outputRows;
      }
    } else {
      assert(pFillInfo->currentKey == ts);
      initBeforeAfterDataBuf(pFillInfo, prev);
      if (pFillInfo->type == TSDB_FILL_NEXT && (pFillInfo->index + 1) < pFillInfo->numOfRows) {
        initBeforeAfterDataBuf(pFillInfo, next);
        ++pFillInfo->index;
        copyCurrentRowIntoBuf(pFillInfo, srcData, *next);
        --pFillInfo->index;
      }

      // assign rows to dst buffer
      for (int32_t i = 0; i < pFillInfo->numOfCols; ++i) {
        SFillColInfo* pCol = &pFillInfo->pFillCol[i];
        if (TSDB_COL_IS_TAG(pCol->flag)/* || IS_VAR_DATA_TYPE(pCol->col.type)*/) {
          continue;
        }

        char* output = elePtrAt(data[i], pCol->col.bytes, pFillInfo->numOfCurrent);
        char* src = elePtrAt(srcData[i], pCol->col.bytes, pFillInfo->index);

        if (i == 0 || (pCol->functionId != FUNCTION_COUNT && !isNull(src, pCol->col.type)) ||
            (pCol->functionId == FUNCTION_COUNT && GET_INT64_VAL(src) != 0)) {
          assignVal(output, src, pCol->col.bytes, pCol->col.type);
232
          memcpy(*prev + pCol->offset, src, pCol->col.bytes);
233 234
        } else {  // i > 0 and data is null , do interpolation
          if (pFillInfo->type == TSDB_FILL_PREV) {
235
            assignVal(output, *prev + pCol->offset, pCol->col.bytes, pCol->col.type);
236 237
          } else if (pFillInfo->type == TSDB_FILL_LINEAR) {
            assignVal(output, src, pCol->col.bytes, pCol->col.type);
238
            memcpy(*prev + pCol->offset, src, pCol->col.bytes);
239 240
          } else if (pFillInfo->type == TSDB_FILL_NEXT) {
            if (*next) {
241
              assignVal(output, *next + pCol->offset, pCol->col.bytes, pCol->col.type);
242 243 244 245
            } else {
              setNull(output, pCol->col.type, pCol->col.bytes);
            }
          } else {
246
            assignVal(output, (char*)&pCol->val, pCol->col.bytes, pCol->col.type);
247 248 249 250 251 252 253 254
          }
        }
      }

      // set the tag value for final result
      setTagsValue(pFillInfo, data, pFillInfo->numOfCurrent);

      pFillInfo->currentKey = taosTimeAdd(pFillInfo->currentKey, pFillInfo->interval.sliding * step,
255
                                          pFillInfo->interval.slidingUnit, pFillInfo->interval.precision);
256 257 258 259 260 261 262
      pFillInfo->index += 1;
      pFillInfo->numOfCurrent += 1;
    }

    if (pFillInfo->index >= pFillInfo->numOfRows || pFillInfo->numOfCurrent >= outputRows) {
      /* the raw data block is exhausted, next value does not exists */
      if (pFillInfo->index >= pFillInfo->numOfRows) {
wafwerar's avatar
wafwerar 已提交
263
        taosMemoryFreeClear(*next);
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
      }

      pFillInfo->numOfTotal += pFillInfo->numOfCurrent;
      return pFillInfo->numOfCurrent;
    }
  }

  return pFillInfo->numOfCurrent;
}

static int64_t appendFilledResult(SFillInfo* pFillInfo, void** output, int64_t resultCapacity) {
  /*
   * 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);
  }

  pFillInfo->numOfTotal += pFillInfo->numOfCurrent;

  assert(pFillInfo->numOfCurrent == resultCapacity);
  return resultCapacity;
}

// 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;
  int32_t numOfTags = 0;

  int32_t k = 0;
  for (int32_t i = 0; i < numOfCols; ++i) {
    SFillColInfo* pColInfo = &pFillInfo->pFillCol[i];
    pFillInfo->pData[i] = NULL;

    if (TSDB_COL_IS_TAG(pColInfo->flag) || pColInfo->col.type == TSDB_DATA_TYPE_BINARY) {
      numOfTags += 1;

      bool exists = false;
      int32_t index = -1;
      for (int32_t j = 0; j < k; ++j) {
306
        if (pFillInfo->pTags[j].col.colId == pColInfo->col.slotId) {
307 308 309 310 311 312 313 314
          exists = true;
          index = j;
          break;
        }
      }

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

wafwerar's avatar
wafwerar 已提交
319
        pFillInfo->pTags[k].tagVal = taosMemoryCalloc(1, pColInfo->col.bytes);
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
        pColInfo->tagIndex = k;

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

    rowsize += pColInfo->col.bytes;
  }

  pFillInfo->numOfTags = numOfTags;

  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;
}

345
struct SFillInfo* taosCreateFillInfo(int32_t order, TSKEY skey, int32_t numOfTags, int32_t capacity, int32_t numOfCols,
346
                            SInterval* pInterval, int32_t fillType, struct SFillColInfo* pCol, const char* id) {
347 348 349 350
  if (fillType == TSDB_FILL_NONE) {
    return NULL;
  }

wafwerar's avatar
wafwerar 已提交
351
  SFillInfo* pFillInfo = taosMemoryCalloc(1, sizeof(SFillInfo));
352 353 354 355 356
  if (pFillInfo == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

357 358
  taosResetFillInfo(pFillInfo, skey);

359 360 361 362 363 364 365 366 367 368 369 370 371
  pFillInfo->order = order;

  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;
    default:
      terrno = TSDB_CODE_INVALID_PARA;
      return NULL;
  }

372 373 374 375 376
  pFillInfo->type      = fillType;
  pFillInfo->pFillCol  = pCol;
  pFillInfo->numOfTags = numOfTags;
  pFillInfo->numOfCols = numOfCols;
  pFillInfo->alloc     = capacity;
H
Haojun Liao 已提交
377
  pFillInfo->id        = id;
378 379
  pFillInfo->interval  = *pInterval;
  pFillInfo->pData     = taosMemoryMalloc(POINTER_BYTES * numOfCols);
380 381

//  if (numOfTags > 0) {
wafwerar's avatar
wafwerar 已提交
382
    pFillInfo->pTags = taosMemoryCalloc(numOfCols, sizeof(SFillTagColInfo));
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
    for (int32_t i = 0; i < numOfCols; ++i) {
      pFillInfo->pTags[i].col.colId = -2;  // TODO
    }
//  }

  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;
  pFillInfo->end          = startTimestamp;
  pFillInfo->index        = -1;
  pFillInfo->numOfRows    = 0;
  pFillInfo->numOfCurrent = 0;
  pFillInfo->numOfTotal   = 0;
}

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

wafwerar's avatar
wafwerar 已提交
408 409
  taosMemoryFreeClear(pFillInfo->prevValues);
  taosMemoryFreeClear(pFillInfo->nextValues);
410 411

  for(int32_t i = 0; i < pFillInfo->numOfTags; ++i) {
wafwerar's avatar
wafwerar 已提交
412
    taosMemoryFreeClear(pFillInfo->pTags[i].tagVal);
413 414
  }

wafwerar's avatar
wafwerar 已提交
415
  taosMemoryFreeClear(pFillInfo->pTags);
416
  
wafwerar's avatar
wafwerar 已提交
417 418
  taosMemoryFreeClear(pFillInfo->pData);
  taosMemoryFreeClear(pFillInfo->pFillCol);
419
  
wafwerar's avatar
wafwerar 已提交
420
  taosMemoryFreeClear(pFillInfo);
421 422 423 424 425 426 427 428 429 430
  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)) {
431
    pFillInfo->end = taosTimeTruncate(endKey, &pFillInfo->interval, pFillInfo->interval.precision);
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
  }

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

void taosFillSetInputDataBlock(SFillInfo* pFillInfo, const SSDataBlock* pInput) {
  for (int32_t i = 0; i < pFillInfo->numOfCols; ++i) {
    SFillColInfo* pCol = &pFillInfo->pFillCol[i];

    SColumnInfoData* pColData = taosArrayGet(pInput->pDataBlock, i);
    pFillInfo->pData[i] = pColData->pData;

    if (TSDB_COL_IS_TAG(pCol->flag)) {  // copy the tag value to tag value buffer
      SFillTagColInfo* pTag = &pFillInfo->pTags[pCol->tagIndex];
447
      assert (pTag->col.colId == pCol->col.slotId);
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
      memcpy(pTag->tagVal, pColData->pData, pCol->col.bytes);  // TODO not memcpy??
    }
  }
}

bool taosFillHasMoreResults(SFillInfo* pFillInfo) {
  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;
}

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)) {
474
    pFillInfo->end = taosTimeTruncate(ekey, &pFillInfo->interval, pFillInfo->interval.precision);
475 476 477 478 479 480 481 482 483 484
  }

  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,
485
      pFillInfo->interval.precision);
486 487 488 489 490 491 492 493 494 495 496 497
    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,
498
      pFillInfo->interval.precision);
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
    numOfRes += 1;
  }

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

int32_t taosGetLinearInterpolationVal(SPoint* point, int32_t outputType, SPoint* point1, SPoint* point2, int32_t inputType) {
  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;
}

int64_t taosFillResultDataBlock(SFillInfo* pFillInfo, void** output, int32_t capacity) {
  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) {
    appendFilledResult(pFillInfo, output, numOfRes);
  } else {
    fillResultImpl(pFillInfo, output, (int32_t) numOfRes);
    assert(numOfRes == pFillInfo->numOfCurrent);
  }

//  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);

  return numOfRes;
}
536 537 538 539 540

int64_t getFillInfoStart(struct SFillInfo *pFillInfo) {
  return pFillInfo->start;
}

541
struct SFillColInfo* createFillColInfo(SExprInfo* pExpr, int32_t numOfOutput, const struct SValueNode* val) {
542 543
  int32_t offset = 0;

wafwerar's avatar
wafwerar 已提交
544
  struct SFillColInfo* pFillCol = taosMemoryCalloc(numOfOutput, sizeof(SFillColInfo));
545 546 547 548 549 550 551
  if (pFillCol == NULL) {
    return NULL;
  }

  for(int32_t i = 0; i < numOfOutput; ++i) {
    SExprInfo* pExprInfo   = &pExpr[i];

552 553
    pFillCol[i].col        = pExprInfo->base.resSchema;
    pFillCol[i].offset     = offset;
554
    pFillCol[i].tagIndex   = -2;
555 556 557 558

    if (pExprInfo->base.numOfParams > 0) {
      pFillCol[i].flag = pExprInfo->base.pParam[0].pCol->flag;    // always be the normal column for table query
    }
559
//    pFillCol[i].functionId = pExprInfo->pExpr->_function.functionId;
560
//    pFillCol[i].val.d      = *val;
561 562 563 564 565 566

    offset += pExprInfo->base.resSchema.bytes;
  }

  return pFillCol;
}