tminmax.c 36.0 KB
Newer Older
H
Haojun Liao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * 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 "builtinsimpl.h"
#include "function.h"
#include "tdatablock.h"
#include "tfunctionInt.h"
#include "tglobal.h"

H
Haojun Liao 已提交
22
static int32_t i32VectorCmpAVX2(const int32_t* pData, int32_t numOfRows, bool isMinFunc) {
H
Haojun Liao 已提交
23 24 25
  int32_t        v = 0;
  const int32_t  bitWidth = 256;
  const int32_t* p = pData;
H
Haojun Liao 已提交
26

H
Haojun Liao 已提交
27 28 29
  int32_t width = (bitWidth>>3u) / sizeof(int32_t);
  int32_t remain = numOfRows % width;
  int32_t rounds = numOfRows / width;
H
Haojun Liao 已提交
30

H
Haojun Liao 已提交
31
#if __AVX2__
H
Haojun Liao 已提交
32
  __m256i next;
33
  __m256i initialVal = _mm256_lddqu_si256((__m256i*)p);
H
Haojun Liao 已提交
34
  p += width;
H
Haojun Liao 已提交
35 36 37

  if (!isMinFunc) {  // max function
    for (int32_t i = 0; i < rounds; ++i) {
H
Haojun Liao 已提交
38
      next = _mm256_lddqu_si256((__m256i*)p);
H
Haojun Liao 已提交
39
      initialVal = _mm256_max_epi32(initialVal, next);
H
Haojun Liao 已提交
40
      p += width;
H
Haojun Liao 已提交
41 42
    }

43
    // let compare  the final results
H
Haojun Liao 已提交
44 45
    const int32_t* q = (const int32_t*)&initialVal;
    v = TMAX(q[0], q[1]);
H
Haojun Liao 已提交
46 47 48
    for (int32_t k = 1; k < width; ++k) {
      v = TMAX(v, q[k]);
    }
H
Haojun Liao 已提交
49 50

    // calculate the front and the reminder items in array list
H
Haojun Liao 已提交
51
    int32_t start = rounds * width;
H
Haojun Liao 已提交
52
    for (int32_t j = 0; j < remain; ++j) {
H
Haojun Liao 已提交
53 54
      if (v < p[j + start]) {
        v = p[j + start];
H
Haojun Liao 已提交
55 56 57 58
      }
    }
  } else {  // min function
    for (int32_t i = 0; i < rounds; ++i) {
H
Haojun Liao 已提交
59
      next = _mm256_lddqu_si256((__m256i*)p);
H
Haojun Liao 已提交
60
      initialVal = _mm256_min_epi32(initialVal, next);
H
Haojun Liao 已提交
61
      p += width;
H
Haojun Liao 已提交
62 63 64 65 66
    }

    // let sum up the final results
    const int32_t* q = (const int32_t*)&initialVal;
    v = TMIN(q[0], q[1]);
H
Haojun Liao 已提交
67 68 69
    for (int32_t k = 1; k < width; ++k) {
      v = TMIN(v, q[k]);
    }
H
Haojun Liao 已提交
70 71

    // calculate the front and the remainder items in array list
H
Haojun Liao 已提交
72
    int32_t start = rounds * width;
H
Haojun Liao 已提交
73
    for (int32_t j = 0; j < remain; ++j) {
H
Haojun Liao 已提交
74 75
      if (v > p[j + start]) {
        v = p[j + start];
H
Haojun Liao 已提交
76 77 78 79 80 81 82 83 84 85
      }
    }
  }
#endif

  return v;
}

static float floatVectorCmpAVX(const float* pData, int32_t numOfRows, bool isMinFunc) {
  float v = 0;
H
Haojun Liao 已提交
86 87
  const int32_t bitWidth = 256;
  const float* p = pData;
H
Haojun Liao 已提交
88

H
Haojun Liao 已提交
89 90 91
  int32_t width = (bitWidth>>3u) / sizeof(float);
  int32_t remain = numOfRows % width;
  int32_t rounds = numOfRows / width;
H
Haojun Liao 已提交
92

H
Haojun Liao 已提交
93
#if __AVX__
H
Haojun Liao 已提交
94 95 96

  __m256 next;
  __m256 initialVal = _mm256_loadu_ps(p);
H
Haojun Liao 已提交
97
  p += width;
H
Haojun Liao 已提交
98 99

  if (!isMinFunc) {  // max function
H
Haojun Liao 已提交
100
    for (int32_t i = 1; i < rounds; ++i) {
H
Haojun Liao 已提交
101 102
      next = _mm256_loadu_ps(p);
      initialVal = _mm256_max_ps(initialVal, next);
H
Haojun Liao 已提交
103
      p += width;
H
Haojun Liao 已提交
104 105 106 107 108
    }

    // let sum up the final results
    const float* q = (const float*)&initialVal;
    v = TMAX(q[0], q[1]);
H
Haojun Liao 已提交
109 110 111
    for (int32_t k = 1; k < width; ++k) {
      v = TMAX(v, q[k]);
    }
H
Haojun Liao 已提交
112 113

    // calculate the front and the reminder items in array list
H
Haojun Liao 已提交
114
    int32_t start = rounds * width;
H
Haojun Liao 已提交
115
    for (int32_t j = 0; j < remain; ++j) {
H
Haojun Liao 已提交
116 117
      if (v < p[j + width]) {
        v = p[j + width];
H
Haojun Liao 已提交
118 119 120
      }
    }
  } else {  // min function
H
Haojun Liao 已提交
121
    for (int32_t i = 1; i < rounds; ++i) {
H
Haojun Liao 已提交
122 123
      next = _mm256_loadu_ps(p);
      initialVal = _mm256_min_ps(initialVal, next);
H
Haojun Liao 已提交
124
      p += width;
H
Haojun Liao 已提交
125 126 127 128
    }

    // let sum up the final results
    const float* q = (const float*)&initialVal;
H
Haojun Liao 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    v = TMIN(q[0], q[1]);
    for (int32_t k = 1; k < width; ++k) {
      v = TMIN(v, q[k]);
    }

    // calculate the front and the reminder items in array list
    int32_t start = rounds * bitWidth;
    for (int32_t j = 0; j < remain; ++j) {
      if (v > p[j + start]) {
        v = p[j + start];
      }
    }
  }
#endif

  return v;
}

static int8_t i8VectorCmpAVX2(const int8_t* pData, int32_t numOfRows, bool isMinFunc) {
  int8_t        v = 0;
  const int32_t  bitWidth = 256;
  const int8_t* p = pData;

  int32_t width = (bitWidth>>3u) / sizeof(int8_t);
  int32_t remain = numOfRows % width;
  int32_t rounds = numOfRows / width;

#if __AVX2__
  __m256i next;
158
  __m256i initialVal = _mm256_lddqu_si256((__m256i*)p);
H
Haojun Liao 已提交
159 160 161 162 163 164 165 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
  p += width;

  if (!isMinFunc) {  // max function
    for (int32_t i = 0; i < rounds; ++i) {
      next = _mm256_lddqu_si256((__m256i*)p);
      initialVal = _mm256_max_epi8(initialVal, next);
      p += width;
    }

    // let sum up the final results
    const int8_t* q = (const int8_t*)&initialVal;
    v = TMAX(q[0], q[1]);
    for (int32_t k = 1; k < width; ++k) {
      v = TMAX(v, q[k]);
    }

    // calculate the front and the reminder items in array list
    int32_t start = rounds * width;
    for (int32_t j = 0; j < remain; ++j) {
      if (v < p[j + start]) {
        v = p[j + start];
      }
    }
  } else {  // min function
    for (int32_t i = 0; i < rounds; ++i) {
      next = _mm256_lddqu_si256((__m256i*)p);
      initialVal = _mm256_min_epi8(initialVal, next);
      p += width;
    }

    // let sum up the final results
    const int8_t* q = (const int8_t*)&initialVal;
H
Haojun Liao 已提交
191 192

    v = TMIN(q[0], q[1]);
H
Haojun Liao 已提交
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
    for(int32_t k = 1; k < width; ++k) {
      v = TMIN(v, q[k]);
    }

    // calculate the front and the remainder items in array list
    int32_t start = rounds * width;
    for (int32_t j = 0; j < remain; ++j) {
      if (v > p[j + start]) {
        v = p[j + start];
      }
    }
  }
#endif

  return v;
}

static int16_t i16VectorCmpAVX2(const int16_t* pData, int32_t numOfRows, bool isMinFunc) {
  int16_t        v = 0;
  const int32_t  bitWidth = 256;
  const int16_t* p = pData;

  int32_t width = (bitWidth>>3u) / sizeof(int16_t);
  int32_t remain = numOfRows % width;
  int32_t rounds = numOfRows / width;

#if __AVX2__
  __m256i next;
221
  __m256i initialVal = _mm256_lddqu_si256((__m256i*)p);
H
Haojun Liao 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
  p += width;

  if (!isMinFunc) {  // max function
    for (int32_t i = 0; i < rounds; ++i) {
      next = _mm256_lddqu_si256((__m256i*)p);
      initialVal = _mm256_max_epi16(initialVal, next);
      p += width;
    }

    // let sum up the final results
    const int16_t* q = (const int16_t*)&initialVal;

    v = TMAX(q[0], q[1]);
    for(int32_t k = 1; k < width; ++k) {
      v = TMAX(v, q[k]);
    }
H
Haojun Liao 已提交
238 239

    // calculate the front and the reminder items in array list
H
Haojun Liao 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
    int32_t start = rounds * width;
    for (int32_t j = 0; j < remain; ++j) {
      if (v < p[j + start]) {
        v = p[j + start];
      }
    }
  } else {  // min function
    for (int32_t i = 0; i < rounds; ++i) {
      next = _mm256_lddqu_si256((__m256i*)p);
      initialVal = _mm256_min_epi16(initialVal, next);
      p += width;
    }

    // let sum up the final results
    const int16_t* q = (const int16_t*)&initialVal;

    v = TMIN(q[0], q[1]);
    for(int32_t k = 1; k < width; ++k) {
      v = TMIN(v, q[k]);
    }

    // calculate the front and the remainder items in array list
    int32_t start = rounds * width;
H
Haojun Liao 已提交
263
    for (int32_t j = 0; j < remain; ++j) {
H
Haojun Liao 已提交
264 265
      if (v > p[j + start]) {
        v = p[j + start];
H
Haojun Liao 已提交
266 267 268 269 270 271 272 273
      }
    }
  }
#endif

  return v;
}

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 306 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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 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 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
static int32_t handleInt8Col(SColumnInfoData* pCol, int32_t start, int32_t numOfRows, SqlFunctionCtx* pCtx,
                             SMinmaxResInfo* pBuf, bool isMinFunc) {
  int8_t* pData = (int8_t*)pCol->pData;
  int8_t* val = (int8_t*)&pBuf->v;

  int32_t numOfElems = 0;
  if (pCol->hasNull || numOfRows <= 8 || pCtx->subsidiaries.num > 0) {
    int32_t i = start;
    while (i < (start + numOfRows)) {
      if (!colDataIsNull_f(pCol->nullbitmap, i)) {
        break;
      }
      i += 1;
    }

    if ((i < (start + numOfRows)) && (!pBuf->assign)) {
      *val = pData[i];
      if (pCtx->subsidiaries.num > 0) {
        pBuf->tuplePos = saveTupleData(pCtx, i, pCtx->pSrcBlock, NULL);
      }
      pBuf->assign = true;
      numOfElems += 1;
    }

    if (isMinFunc) {  // min
      for (; i < start + numOfRows; ++i) {
        if (colDataIsNull_f(pCol->nullbitmap, i)) {
          continue;
        }

        if (*val > pData[i]) {
          *val = pData[i];
          if (pCtx->subsidiaries.num > 0) {
            updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
          }
        }
        numOfElems += 1;
      }

    } else {  // max function
      for (; i < start + numOfRows; ++i) {
        if (colDataIsNull_f(pCol->nullbitmap, i)) {
          continue;
        }
        // ignore the equivalent data value
        // NOTE: An faster version to avoid one additional comparison with FPU.
        if (*val < pData[i]) {
          *val = pData[i];
          if (pCtx->subsidiaries.num > 0) {
            updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
          }
        }
        numOfElems += 1;
      }

    }
  } else {  // not has null value
    // AVX2 version to speedup the loop
    if (tsAVX2Enable && tsSIMDEnable) {
      *val = i8VectorCmpAVX2(pData, numOfRows, isMinFunc);
    } else {
      if (!pBuf->assign) {
        *val = pData[0];
        pBuf->assign = true;
      }

      if (isMinFunc) {  // min
        for (int32_t i = start; i < start + numOfRows; ++i) {
          if (*val > pData[i]) {
            *val = pData[i];
          }
        }
      } else {  // max
        for (int32_t i = start; i < start + numOfRows; ++i) {
          if (*val < pData[i]) {
            *val = pData[i];
          }
        }
      }
    }

    numOfElems = numOfRows;
  }

  return numOfElems;
}

static int32_t handleInt16Col(SColumnInfoData* pCol, int32_t start, int32_t numOfRows, SqlFunctionCtx* pCtx,
                             SMinmaxResInfo* pBuf, bool isMinFunc) {
  int16_t* pData = (int16_t*)pCol->pData;
  int16_t* val = (int16_t*)&pBuf->v;

  int32_t numOfElems = 0;
  if (pCol->hasNull || numOfRows <= 8 || pCtx->subsidiaries.num > 0) {
    int32_t i = start;
    while (i < (start + numOfRows)) {
      if (!colDataIsNull_f(pCol->nullbitmap, i)) {
        break;
      }
      i += 1;
    }

    if ((i < (start + numOfRows)) && (!pBuf->assign)) {
      *val = pData[i];
      if (pCtx->subsidiaries.num > 0) {
        pBuf->tuplePos = saveTupleData(pCtx, i, pCtx->pSrcBlock, NULL);
      }
      pBuf->assign = true;
      numOfElems += 1;
    }

    if (isMinFunc) {  // min
      for (; i < start + numOfRows; ++i) {
        if (colDataIsNull_f(pCol->nullbitmap, i)) {
          continue;
        }

        if (*val > pData[i]) {
          *val = pData[i];
          if (pCtx->subsidiaries.num > 0) {
            updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
          }
        }
        numOfElems += 1;
      }

    } else {  // max function
      for (; i < start + numOfRows; ++i) {
        if (colDataIsNull_f(pCol->nullbitmap, i)) {
          continue;
        }
        // ignore the equivalent data value
        // NOTE: An faster version to avoid one additional comparison with FPU.
        if (*val < pData[i]) {
          *val = pData[i];
          if (pCtx->subsidiaries.num > 0) {
            updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
          }
        }
        numOfElems += 1;
      }

    }
  } else {  // not has null value
    // AVX2 version to speedup the loop
    if (tsAVX2Enable && tsSIMDEnable) {
      *val = i16VectorCmpAVX2(pData, numOfRows, isMinFunc);
    } else {
      if (!pBuf->assign) {
        *val = pData[0];
        pBuf->assign = true;
      }

      if (isMinFunc) {  // min
        for (int32_t i = start; i < start + numOfRows; ++i) {
          if (*val > pData[i]) {
            *val = pData[i];
          }
        }
      } else {  // max
        for (int32_t i = start; i < start + numOfRows; ++i) {
          if (*val < pData[i]) {
            *val = pData[i];
          }
        }
      }
    }

    numOfElems = numOfRows;
  }

  return numOfElems;
}
H
Haojun Liao 已提交
447

H
Haojun Liao 已提交
448 449 450 451 452 453
static int32_t handleInt32Col(SColumnInfoData* pCol, int32_t start, int32_t numOfRows, SqlFunctionCtx* pCtx,
                              SMinmaxResInfo* pBuf, bool isMinFunc) {
  int32_t* pData = (int32_t*)pCol->pData;
  int32_t* val = (int32_t*)&pBuf->v;

  int32_t numOfElems = 0;
H
Haojun Liao 已提交
454
  if (pCol->hasNull || numOfRows <= 8 || pCtx->subsidiaries.num > 0) {
H
Haojun Liao 已提交
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
    int32_t i = start;
    while (i < (start + numOfRows)) {
      if (!colDataIsNull_f(pCol->nullbitmap, i)) {
        break;
      }
      i += 1;
    }

    if ((i < (start + numOfRows)) && (!pBuf->assign)) {
      *val = pData[i];
      if (pCtx->subsidiaries.num > 0) {
        pBuf->tuplePos = saveTupleData(pCtx, i, pCtx->pSrcBlock, NULL);
      }
      pBuf->assign = true;
      numOfElems += 1;
    }

H
Haojun Liao 已提交
472
    if (isMinFunc) {  // min
H
Haojun Liao 已提交
473
      for (; i < start + numOfRows; ++i) {
H
Haojun Liao 已提交
474 475 476 477
        if (colDataIsNull_f(pCol->nullbitmap, i)) {
          continue;
        }

H
Haojun Liao 已提交
478
        if (*val > pData[i]) {
H
Haojun Liao 已提交
479 480
          *val = pData[i];
          if (pCtx->subsidiaries.num > 0) {
H
Haojun Liao 已提交
481
            updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
H
Haojun Liao 已提交
482 483 484 485
          }
        }
        numOfElems += 1;
      }
H
Haojun Liao 已提交
486

H
Haojun Liao 已提交
487
    } else {  // max function
H
Haojun Liao 已提交
488
      for (; i < start + numOfRows; ++i) {
H
Haojun Liao 已提交
489 490 491
        if (colDataIsNull_f(pCol->nullbitmap, i)) {
          continue;
        }
H
Haojun Liao 已提交
492 493 494
        // ignore the equivalent data value
        // NOTE: An faster version to avoid one additional comparison with FPU.
        if (*val < pData[i]) {
H
Haojun Liao 已提交
495 496
          *val = pData[i];
          if (pCtx->subsidiaries.num > 0) {
H
Haojun Liao 已提交
497
            updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
H
Haojun Liao 已提交
498 499 500 501
          }
        }
        numOfElems += 1;
      }
H
Haojun Liao 已提交
502

H
Haojun Liao 已提交
503
    }
H
Haojun Liao 已提交
504
  } else {  // not has null value
H
Haojun Liao 已提交
505 506 507 508 509 510 511
    // AVX2 version to speedup the loop
    if (tsAVX2Enable && tsSIMDEnable) {
      *val = i32VectorCmpAVX2(pData, numOfRows, isMinFunc);
    } else {
      if (!pBuf->assign) {
        *val = pData[0];
        pBuf->assign = true;
H
Haojun Liao 已提交
512 513
      }

H
Haojun Liao 已提交
514 515 516 517 518
      if (isMinFunc) {  // min
        for (int32_t i = start; i < start + numOfRows; ++i) {
          if (*val > pData[i]) {
            *val = pData[i];
          }
H
Haojun Liao 已提交
519
        }
H
Haojun Liao 已提交
520 521 522 523 524
      } else {  // max
        for (int32_t i = start; i < start + numOfRows; ++i) {
          if (*val < pData[i]) {
            *val = pData[i];
          }
H
Haojun Liao 已提交
525 526 527 528 529 530 531 532 533 534
        }
      }
    }

    numOfElems = numOfRows;
  }

  return numOfElems;
}

535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
static int32_t handleInt64Col(SColumnInfoData* pCol, int32_t start, int32_t numOfRows, SqlFunctionCtx* pCtx,
                              SMinmaxResInfo* pBuf, bool isMinFunc) {
  int32_t* pData = (int32_t*)pCol->pData;
  int32_t* val = (int32_t*)&pBuf->v;

  int32_t numOfElems = 0;
  if (pCol->hasNull || pCtx->subsidiaries.num > 0) {
    int32_t i = start;
    while (i < (start + numOfRows)) {
      if (!colDataIsNull_f(pCol->nullbitmap, i)) {
        break;
      }
      i += 1;
    }

    if ((i < (start + numOfRows)) && (!pBuf->assign)) {
      *val = pData[i];
      if (pCtx->subsidiaries.num > 0) {
        pBuf->tuplePos = saveTupleData(pCtx, i, pCtx->pSrcBlock, NULL);
      }
      pBuf->assign = true;
      numOfElems += 1;
    }

    if (isMinFunc) {  // min
      for (; i < start + numOfRows; ++i) {
        if (colDataIsNull_f(pCol->nullbitmap, i)) {
          continue;
        }

        if (*val > pData[i]) {
          *val = pData[i];
          if (pCtx->subsidiaries.num > 0) {
            updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
          }
        }
        numOfElems += 1;
      }

    } else {  // max function
      for (; i < start + numOfRows; ++i) {
        if (colDataIsNull_f(pCol->nullbitmap, i)) {
          continue;
        }
        // ignore the equivalent data value
        // NOTE: An faster version to avoid one additional comparison with FPU.
        if (*val < pData[i]) {
          *val = pData[i];
          if (pCtx->subsidiaries.num > 0) {
            updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
          }
        }
        numOfElems += 1;
      }
    }
  } else {  // not has null value
            // AVX2 version to speedup the loop
    if (!pBuf->assign) {
      *val = pData[0];
      pBuf->assign = true;
    }

    if (isMinFunc) {  // min
      for (int32_t i = start; i < start + numOfRows; ++i) {
        if (*val > pData[i]) {
          *val = pData[i];
        }
      }
    } else {  // max
      for (int32_t i = start; i < start + numOfRows; ++i) {
        if (*val < pData[i]) {
          *val = pData[i];
        }
      }
    }

    numOfElems = numOfRows;
  }
  return numOfElems;
}

H
Haojun Liao 已提交
616 617 618 619 620 621 622
static int32_t handleFloatCol(SColumnInfoData* pCol, int32_t start, int32_t numOfRows, SqlFunctionCtx* pCtx,
                              SMinmaxResInfo* pBuf, bool isMinFunc) {
  float* pData = (float*)pCol->pData;
  double* val = (double*)&pBuf->v;

  int32_t numOfElems = 0;
  if (pCol->hasNull || numOfRows < 8 || pCtx->subsidiaries.num > 0) {
H
Haojun Liao 已提交
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
    int32_t i = start;
    while (i < (start + numOfRows)) {
      if (!colDataIsNull_f(pCol->nullbitmap, i)) {
        break;
      }
      i += 1;
    }

    if ((i < (start + numOfRows)) && (!pBuf->assign)) {
      *val = pData[i];
      if (pCtx->subsidiaries.num > 0) {
        pBuf->tuplePos = saveTupleData(pCtx, i, pCtx->pSrcBlock, NULL);
      }
      pBuf->assign = true;
      numOfElems += 1;
    }

H
Haojun Liao 已提交
640
    if (isMinFunc) {  // min
H
Haojun Liao 已提交
641
      for (; i < start + numOfRows; ++i) {
H
Haojun Liao 已提交
642 643 644 645
        if (colDataIsNull_f(pCol->nullbitmap, i)) {
          continue;
        }

H
Haojun Liao 已提交
646
        if (*val > pData[i]) {
H
Haojun Liao 已提交
647 648
          *val = pData[i];
          if (pCtx->subsidiaries.num > 0) {
H
Haojun Liao 已提交
649
            updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
H
Haojun Liao 已提交
650 651 652 653 654
          }
        }
        numOfElems += 1;
      }
    } else {  // max function
H
Haojun Liao 已提交
655
      for (; i < start + numOfRows; ++i) {
H
Haojun Liao 已提交
656 657 658 659
        if (colDataIsNull_f(pCol->nullbitmap, i)) {
          continue;
        }

H
Haojun Liao 已提交
660 661 662
        // ignore the equivalent data value
        // NOTE: An faster version to avoid one additional comparison with FPU.
        if (*val < pData[i]) {
H
Haojun Liao 已提交
663 664
          *val = pData[i];
          if (pCtx->subsidiaries.num > 0) {
H
Haojun Liao 已提交
665
            updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
H
Haojun Liao 已提交
666
          }
H
Haojun Liao 已提交
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
        }
        numOfElems += 1;
      }
    }
  } else {  // not has null value
    // AVX version to speedup the loop
    if (tsAVXEnable && tsSIMDEnable) {
      *val = (double) floatVectorCmpAVX(pData, numOfRows, isMinFunc);
    } else {
      if (!pBuf->assign) {
        *val = pData[0];
        pBuf->assign = true;
      }

      if (isMinFunc) {  // min
        for (int32_t i = start; i < start + numOfRows; ++i) {
          if (*val > pData[i]) {
            *val = pData[i];
          }
        }
      } else {  // max
        for (int32_t i = start; i < start + numOfRows; ++i) {
H
Haojun Liao 已提交
689 690 691 692
          if (*val < pData[i]) {
            *val = pData[i];
          }
        }
H
Haojun Liao 已提交
693 694 695 696 697 698 699 700 701
      }
    }

    numOfElems = numOfRows;
  }

  return numOfElems;
}

702 703 704 705
static int32_t handleDoubleCol(SColumnInfoData* pCol, int32_t start, int32_t numOfRows, SqlFunctionCtx* pCtx,
                              SMinmaxResInfo* pBuf, bool isMinFunc) {
  float* pData = (float*)pCol->pData;
  double* val = (double*)&pBuf->v;
H
Haojun Liao 已提交
706

H
Haojun Liao 已提交
707
  int32_t numOfElems = 0;
708
  if (pCol->hasNull || numOfRows < 8 || pCtx->subsidiaries.num > 0) {
H
Haojun Liao 已提交
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
    int32_t i = start;
    while (i < (start + numOfRows)) {
      if (!colDataIsNull_f(pCol->nullbitmap, i)) {
        break;
      }
      i += 1;
    }

    if ((i < (start + numOfRows)) && (!pBuf->assign)) {
      *val = pData[i];
      if (pCtx->subsidiaries.num > 0) {
        pBuf->tuplePos = saveTupleData(pCtx, i, pCtx->pSrcBlock, NULL);
      }
      pBuf->assign = true;
      numOfElems += 1;
    }

    if (isMinFunc) {  // min
      for (; i < start + numOfRows; ++i) {
        if (colDataIsNull_f(pCol->nullbitmap, i)) {
          continue;
        }

        if (*val > pData[i]) {
          *val = pData[i];
          if (pCtx->subsidiaries.num > 0) {
            updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
          }
        }
H
Haojun Liao 已提交
738 739
        numOfElems += 1;
      }
H
Haojun Liao 已提交
740 741 742 743 744
    } else {  // max function
      for (; i < start + numOfRows; ++i) {
        if (colDataIsNull_f(pCol->nullbitmap, i)) {
          continue;
        }
745

H
Haojun Liao 已提交
746 747 748 749 750 751 752 753 754 755
        // ignore the equivalent data value
        // NOTE: An faster version to avoid one additional comparison with FPU.
        if (*val < pData[i]) {
          *val = pData[i];
          if (pCtx->subsidiaries.num > 0) {
            updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
          }
        }
        numOfElems += 1;
      }
H
Haojun Liao 已提交
756
    }
H
Haojun Liao 已提交
757
  } else {  // not has null value
758 759 760
    // AVX version to speedup the loop
    if (tsAVXEnable && tsSIMDEnable) {
      *val = (double) floatVectorCmpAVX(pData, numOfRows, isMinFunc);
H
Haojun Liao 已提交
761 762 763 764
    } else {
      if (!pBuf->assign) {
        *val = pData[0];
        pBuf->assign = true;
H
Haojun Liao 已提交
765 766
      }

H
Haojun Liao 已提交
767 768 769 770 771
      if (isMinFunc) {  // min
        for (int32_t i = start; i < start + numOfRows; ++i) {
          if (*val > pData[i]) {
            *val = pData[i];
          }
H
Haojun Liao 已提交
772
        }
H
Haojun Liao 已提交
773 774 775 776 777
      } else {  // max
        for (int32_t i = start; i < start + numOfRows; ++i) {
          if (*val < pData[i]) {
            *val = pData[i];
          }
H
Haojun Liao 已提交
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
        }
      }
    }

    numOfElems = numOfRows;
  }

  return numOfElems;
}

static int32_t findRowIndex(int32_t start, int32_t num, SColumnInfoData* pCol, const char* tval) {
  // the data is loaded, not only the block SMA value
  for (int32_t i = start; i < num + start; ++i) {
    char* p = colDataGetData(pCol, i);
    if (memcmp((void*)tval, p, pCol->info.bytes) == 0) {
      return i;
    }
  }

  // if reach here means real data of block SMA is not set in pCtx->input.
  return -1;
}

int32_t doMinMaxHelper(SqlFunctionCtx* pCtx, int32_t isMinFunc) {
  int32_t numOfElems = 0;

  SInputColumnInfoData* pInput = &pCtx->input;
  SColumnDataAgg*       pAgg = pInput->pColumnDataAgg[0];

  SColumnInfoData* pCol = pInput->pData[0];
  int32_t          type = pCol->info.type;

  SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
  SMinmaxResInfo*      pBuf = GET_ROWCELL_INTERBUF(pResInfo);
  pBuf->type = type;

  if (IS_NULL_TYPE(type)) {
    numOfElems = 0;
    goto _min_max_over;
  }

  // data in current data block are qualified to the query
H
Haojun Liao 已提交
820
  if (pInput->colDataSMAIsSet) {
H
Haojun Liao 已提交
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
    numOfElems = pInput->numOfRows - pAgg->numOfNull;
    ASSERT(pInput->numOfRows == pInput->totalRows && numOfElems >= 0);
    if (numOfElems == 0) {
      return numOfElems;
    }

    void*   tval = NULL;
    int16_t index = 0;

    if (isMinFunc) {
      tval = &pInput->pColumnDataAgg[0]->min;
    } else {
      tval = &pInput->pColumnDataAgg[0]->max;
    }

    if (!pBuf->assign) {
      pBuf->v = *(int64_t*)tval;
      if (pCtx->subsidiaries.num > 0) {
        index = findRowIndex(pInput->startRowIndex, pInput->numOfRows, pCol, tval);
        if (index >= 0) {
          pBuf->tuplePos = saveTupleData(pCtx, index, pCtx->pSrcBlock, NULL);
        }
      }
    } else {
      if (IS_SIGNED_NUMERIC_TYPE(type)) {
        int64_t prev = 0;
        GET_TYPED_DATA(prev, int64_t, type, &pBuf->v);

        int64_t val = GET_INT64_VAL(tval);
        if ((prev < val) ^ isMinFunc) {
          *(int64_t*)&pBuf->v = val;
          if (pCtx->subsidiaries.num > 0) {
            index = findRowIndex(pInput->startRowIndex, pInput->numOfRows, pCol, tval);
            if (index >= 0) {
              pBuf->tuplePos = saveTupleData(pCtx, index, pCtx->pSrcBlock, NULL);
            }
          }
        }
      } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
        uint64_t prev = 0;
        GET_TYPED_DATA(prev, uint64_t, type, &pBuf->v);

        uint64_t val = GET_UINT64_VAL(tval);
        if ((prev < val) ^ isMinFunc) {
          *(uint64_t*)&pBuf->v = val;
          if (pCtx->subsidiaries.num > 0) {
            index = findRowIndex(pInput->startRowIndex, pInput->numOfRows, pCol, tval);
            if (index >= 0) {
              pBuf->tuplePos = saveTupleData(pCtx, index, pCtx->pSrcBlock, NULL);
            }
          }
        }
      } else if (type == TSDB_DATA_TYPE_DOUBLE) {
        double prev = 0;
        GET_TYPED_DATA(prev, double, type, &pBuf->v);

        double val = GET_DOUBLE_VAL(tval);
        if ((prev < val) ^ isMinFunc) {
          *(double*)&pBuf->v = val;
          if (pCtx->subsidiaries.num > 0) {
            index = findRowIndex(pInput->startRowIndex, pInput->numOfRows, pCol, tval);
            if (index >= 0) {
              pBuf->tuplePos = saveTupleData(pCtx, index, pCtx->pSrcBlock, NULL);
            }
          }
        }
      } else if (type == TSDB_DATA_TYPE_FLOAT) {
        float prev = 0;
        GET_TYPED_DATA(prev, float, type, &pBuf->v);

        float val = GET_DOUBLE_VAL(tval);
        if ((prev < val) ^ isMinFunc) {
          *(float*)&pBuf->v = val;
        }

        if (pCtx->subsidiaries.num > 0) {
          index = findRowIndex(pInput->startRowIndex, pInput->numOfRows, pCol, tval);
          if (index >= 0) {
            pBuf->tuplePos = saveTupleData(pCtx, index, pCtx->pSrcBlock, NULL);
          }
        }
      }
    }

    pBuf->assign = true;
    return numOfElems;
  }

  int32_t start = pInput->startRowIndex;
  int32_t numOfRows = pInput->numOfRows;

  if (IS_SIGNED_NUMERIC_TYPE(type) || type == TSDB_DATA_TYPE_BOOL) {
    if (type == TSDB_DATA_TYPE_TINYINT || type == TSDB_DATA_TYPE_BOOL) {
H
Haojun Liao 已提交
914
      numOfElems = handleInt8Col(pCol, start, numOfRows, pCtx, pBuf, isMinFunc);
H
Haojun Liao 已提交
915
    } else if (type == TSDB_DATA_TYPE_SMALLINT) {
916
      numOfElems = handleInt16Col(pCol, start, numOfRows, pCtx, pBuf, isMinFunc);
H
Haojun Liao 已提交
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
      int16_t* pData = (int16_t*)pCol->pData;
      int16_t* val = (int16_t*)&pBuf->v;

      for (int32_t i = start; i < start + numOfRows; ++i) {
        if ((pCol->hasNull) && colDataIsNull_f(pCol->nullbitmap, i)) {
          continue;
        }

        if (!pBuf->assign) {
          *val = pData[i];
          if (pCtx->subsidiaries.num > 0) {
            pBuf->tuplePos = saveTupleData(pCtx, i, pCtx->pSrcBlock, NULL);
          }
          pBuf->assign = true;
        } else {
          // ignore the equivalent data value
          // NOTE: An faster version to avoid one additional comparison with FPU.
          if (isMinFunc) {  // min
            if (*val > pData[i]) {
              *val = pData[i];
              if (pCtx->subsidiaries.num > 0) {
                updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
              }
            }
          } else {  // max
            if (*val < pData[i]) {
              *val = pData[i];
              if (pCtx->subsidiaries.num > 0) {
                updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
              }
            }
          }
        }

        numOfElems += 1;
      }
    } else if (type == TSDB_DATA_TYPE_INT) {
      numOfElems = handleInt32Col(pCol, start, numOfRows, pCtx, pBuf, isMinFunc);
#if 0
      for (int32_t i = start; i < start + numOfRows; ++i) {
        if ((pCol->hasNull) && colDataIsNull_f(pCol->nullbitmap, i)) {
          continue;
        }

        if (!pBuf->assign) {
          *val = pData[i];
          if (pCtx->subsidiaries.num > 0) {
            pBuf->tuplePos = saveTupleData(pCtx, i, pCtx->pSrcBlock, NULL);
          }
          pBuf->assign = true;
        } else {
          // ignore the equivalent data value
          // NOTE: An faster version to avoid one additional comparison with FPU.
          if (isMinFunc) {  // min
            if (*val > pData[i]) {
              *val = pData[i];
              if (pCtx->subsidiaries.num > 0) {
                updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
              }
            }
          } else {  // max
            if (*val < pData[i]) {
              *val = pData[i];
              if (pCtx->subsidiaries.num > 0) {
                updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
              }
            }
          }
        }

        numOfElems += 1;
      }
#endif

    } else if (type == TSDB_DATA_TYPE_BIGINT) {
      int64_t* pData = (int64_t*)pCol->pData;
      int64_t* val = (int64_t*)&pBuf->v;

      for (int32_t i = start; i < start + numOfRows; ++i) {
        if ((pCol->hasNull) && colDataIsNull_f(pCol->nullbitmap, i)) {
          continue;
        }

        if (!pBuf->assign) {
          *val = pData[i];
          if (pCtx->subsidiaries.num > 0) {
            pBuf->tuplePos = saveTupleData(pCtx, i, pCtx->pSrcBlock, NULL);
          }
          pBuf->assign = true;
        } else {
          // ignore the equivalent data value
          // NOTE: An faster version to avoid one additional comparison with FPU.
          if (isMinFunc) {  // min
            if (*val > pData[i]) {
              *val = pData[i];
              if (pCtx->subsidiaries.num > 0) {
                updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
              }
            }
          } else {  // max
            if (*val < pData[i]) {
              *val = pData[i];
              if (pCtx->subsidiaries.num > 0) {
                updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
              }
            }
          }
        }

        numOfElems += 1;
      }
    }
  } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
    if (type == TSDB_DATA_TYPE_UTINYINT) {
      uint8_t* pData = (uint8_t*)pCol->pData;
      uint8_t* val = (uint8_t*)&pBuf->v;

      for (int32_t i = start; i < start + numOfRows; ++i) {
        if ((pCol->hasNull) && colDataIsNull_f(pCol->nullbitmap, i)) {
          continue;
        }

        if (!pBuf->assign) {
          *val = pData[i];
          if (pCtx->subsidiaries.num > 0) {
            pBuf->tuplePos = saveTupleData(pCtx, i, pCtx->pSrcBlock, NULL);
          }
          pBuf->assign = true;
        } else {
          // ignore the equivalent data value
          // NOTE: An faster version to avoid one additional comparison with FPU.
          if (isMinFunc) {  // min
            if (*val > pData[i]) {
              *val = pData[i];
              if (pCtx->subsidiaries.num > 0) {
                updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
              }
            }
          } else {  // max
            if (*val < pData[i]) {
              *val = pData[i];
              if (pCtx->subsidiaries.num > 0) {
                updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
              }
            }
          }
        }

        numOfElems += 1;
      }
    } else if (type == TSDB_DATA_TYPE_USMALLINT) {
      uint16_t* pData = (uint16_t*)pCol->pData;
      uint16_t* val = (uint16_t*)&pBuf->v;

      for (int32_t i = start; i < start + numOfRows; ++i) {
        if ((pCol->hasNull) && colDataIsNull_f(pCol->nullbitmap, i)) {
          continue;
        }

        if (!pBuf->assign) {
          *val = pData[i];
          if (pCtx->subsidiaries.num > 0) {
            pBuf->tuplePos = saveTupleData(pCtx, i, pCtx->pSrcBlock, NULL);
          }
          pBuf->assign = true;
        } else {
          // ignore the equivalent data value
          // NOTE: An faster version to avoid one additional comparison with FPU.
          if (isMinFunc) {  // min
            if (*val > pData[i]) {
              *val = pData[i];
              if (pCtx->subsidiaries.num > 0) {
                updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
              }
            }
          } else {  // max
            if (*val < pData[i]) {
              *val = pData[i];
              if (pCtx->subsidiaries.num > 0) {
                updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
              }
            }
          }
        }

        numOfElems += 1;
      }
    } else if (type == TSDB_DATA_TYPE_UINT) {
      uint32_t* pData = (uint32_t*)pCol->pData;
      uint32_t* val = (uint32_t*)&pBuf->v;

      for (int32_t i = start; i < start + numOfRows; ++i) {
        if ((pCol->hasNull) && colDataIsNull_f(pCol->nullbitmap, i)) {
          continue;
        }

        if (!pBuf->assign) {
          *val = pData[i];
          if (pCtx->subsidiaries.num > 0) {
            pBuf->tuplePos = saveTupleData(pCtx, i, pCtx->pSrcBlock, NULL);
          }
          pBuf->assign = true;
        } else {
          // ignore the equivalent data value
          // NOTE: An faster version to avoid one additional comparison with FPU.
          if (isMinFunc) {  // min
            if (*val > pData[i]) {
              *val = pData[i];
              if (pCtx->subsidiaries.num > 0) {
                updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
              }
            }
          } else {  // max
            if (*val < pData[i]) {
              *val = pData[i];
              if (pCtx->subsidiaries.num > 0) {
                updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
              }
            }
          }
        }

        numOfElems += 1;
      }
    } else if (type == TSDB_DATA_TYPE_UBIGINT) {
      uint64_t* pData = (uint64_t*)pCol->pData;
      uint64_t* val = (uint64_t*)&pBuf->v;

      for (int32_t i = start; i < start + numOfRows; ++i) {
        if ((pCol->hasNull) && colDataIsNull_f(pCol->nullbitmap, i)) {
          continue;
        }

        if (!pBuf->assign) {
          *val = pData[i];
          if (pCtx->subsidiaries.num > 0) {
            pBuf->tuplePos = saveTupleData(pCtx, i, pCtx->pSrcBlock, NULL);
          }
          pBuf->assign = true;
        } else {
          // ignore the equivalent data value
          // NOTE: An faster version to avoid one additional comparison with FPU.
          if (isMinFunc) {  // min
            if (*val > pData[i]) {
              *val = pData[i];
              if (pCtx->subsidiaries.num > 0) {
                updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
              }
            }
          } else {  // max
            if (*val < pData[i]) {
              *val = pData[i];
              if (pCtx->subsidiaries.num > 0) {
                updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
              }
            }
          }
        }

        numOfElems += 1;
      }
    }
  } else if (type == TSDB_DATA_TYPE_DOUBLE) {
    double* pData = (double*)pCol->pData;
    double* val = (double*)&pBuf->v;

    for (int32_t i = start; i < start + numOfRows; ++i) {
      if ((pCol->hasNull) && colDataIsNull_f(pCol->nullbitmap, i)) {
        continue;
      }

      if (!pBuf->assign) {
        *val = pData[i];
        if (pCtx->subsidiaries.num > 0) {
          pBuf->tuplePos = saveTupleData(pCtx, i, pCtx->pSrcBlock, NULL);
        }
        pBuf->assign = true;
      } else {
        // ignore the equivalent data value
        // NOTE: An faster version to avoid one additional comparison with FPU.
        if (isMinFunc) {  // min
          if (*val > pData[i]) {
            *val = pData[i];
            if (pCtx->subsidiaries.num > 0) {
              updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
            }
          }
        } else {  // max
          if (*val < pData[i]) {
            *val = pData[i];
            if (pCtx->subsidiaries.num > 0) {
              updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
            }
          }
        }
      }

      numOfElems += 1;
    }
  } else if (type == TSDB_DATA_TYPE_FLOAT) {
    numOfElems = handleFloatCol(pCol, start, numOfRows, pCtx, pBuf, isMinFunc);
#if 0
    for (int32_t i = start; i < start + numOfRows; ++i) {
      if ((pCol->hasNull) && colDataIsNull_f(pCol->nullbitmap, i)) {
        continue;
      }

      if (!pBuf->assign) {
        *val = pData[i];
        if (pCtx->subsidiaries.num > 0) {
          pBuf->tuplePos = saveTupleData(pCtx, i, pCtx->pSrcBlock, NULL);
        }
        pBuf->assign = true;
      } else {
#if 0
        if ((*val) == pData[i]) {
          continue;
        }

        if ((*val < pData[i]) ^ isMinFunc) {
          *val = pData[i];
          if (pCtx->subsidiaries.num > 0) {
            updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
          }
        }
#endif
        // NOTE: An faster version to avoid one additional comparison with FPU.
        if (isMinFunc) {  // min
          if (*val > pData[i]) {
            *val = pData[i];
            if (pCtx->subsidiaries.num > 0) {
              updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
            }
          }
        } else {  // max
          if (*val < pData[i]) {
            *val = pData[i];
            if (pCtx->subsidiaries.num > 0) {
              updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
            }
          }
        }
      }

      numOfElems += 1;
    }
#endif

  }

_min_max_over:
  if (numOfElems == 0 && pCtx->subsidiaries.num > 0 && !pBuf->nullTupleSaved) {
    pBuf->nullTuplePos = saveTupleData(pCtx, pInput->startRowIndex, pCtx->pSrcBlock, NULL);
    pBuf->nullTupleSaved = true;
  }
  return numOfElems;
}