tminmax.c 30.9 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 33
  __m256i next;
  __m256i initialVal = _mm256_loadu_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 44 45
    }

    // let sum up the final results
    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 158 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
    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;
  __m256i initialVal = _mm256_loadu_si256((__m256i*)p);
  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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    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;
  __m256i initialVal = _mm256_loadu_si256((__m256i*)p);
  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;
}

H
Haojun Liao 已提交
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
//static int64_t i64VectorCmpAVX2(const int64_t* pData, int32_t numOfRows, bool isMinFunc) {
//  int64_t        v = 0;
//  const int32_t  bitWidth = 256;
//  const int64_t* p = pData;
//
//  int32_t width = (bitWidth>>3u) / sizeof(int64_t);
//  int32_t remain = numOfRows % width;
//  int32_t rounds = numOfRows / width;
//
//#if __AVX2__
//  __m256i next;
//  __m256i initialVal = _mm256_loadu_si256((__m256i*)p);
//  p += width;
//
//  if (!isMinFunc) {  // max function
//    for (int32_t i = 0; i < rounds; ++i) {
//      next = _mm256_lddqu_si256((__m256i*)p);
//      initialVal = _mm256_max_epi64(initialVal, next);
//      p += width;
//    }
//
//    // let sum up the final results
//    const int64_t* q = (const int64_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_epi64(initialVal, next);
//      p += width;
//    }
//
//    // let sum up the final results
//    const int64_t* q = (const int64_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;
//    for (int32_t j = 0; j < remain; ++j) {
//      if (v > p[j + start]) {
//        v = p[j + start];
//      }
//    }
//  }
//#endif
//
//  return v;
//}

H
Haojun Liao 已提交
336 337 338 339 340 341
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 已提交
342
  if (pCol->hasNull || numOfRows <= 8 || pCtx->subsidiaries.num > 0) {
H
Haojun Liao 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
    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 已提交
360
    if (isMinFunc) {  // min
H
Haojun Liao 已提交
361
      for (; i < start + numOfRows; ++i) {
H
Haojun Liao 已提交
362 363 364 365
        if (colDataIsNull_f(pCol->nullbitmap, i)) {
          continue;
        }

H
Haojun Liao 已提交
366
        if (*val > pData[i]) {
H
Haojun Liao 已提交
367 368
          *val = pData[i];
          if (pCtx->subsidiaries.num > 0) {
H
Haojun Liao 已提交
369
            updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
H
Haojun Liao 已提交
370 371 372 373
          }
        }
        numOfElems += 1;
      }
H
Haojun Liao 已提交
374

H
Haojun Liao 已提交
375
    } else {  // max function
H
Haojun Liao 已提交
376
      for (; i < start + numOfRows; ++i) {
H
Haojun Liao 已提交
377 378 379
        if (colDataIsNull_f(pCol->nullbitmap, i)) {
          continue;
        }
H
Haojun Liao 已提交
380 381 382
        // ignore the equivalent data value
        // NOTE: An faster version to avoid one additional comparison with FPU.
        if (*val < pData[i]) {
H
Haojun Liao 已提交
383 384
          *val = pData[i];
          if (pCtx->subsidiaries.num > 0) {
H
Haojun Liao 已提交
385
            updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
H
Haojun Liao 已提交
386 387 388 389
          }
        }
        numOfElems += 1;
      }
H
Haojun Liao 已提交
390

H
Haojun Liao 已提交
391
    }
H
Haojun Liao 已提交
392
  } else {  // not has null value
H
Haojun Liao 已提交
393 394 395 396 397 398 399
    // 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 已提交
400 401
      }

H
Haojun Liao 已提交
402 403 404 405 406
      if (isMinFunc) {  // min
        for (int32_t i = start; i < start + numOfRows; ++i) {
          if (*val > pData[i]) {
            *val = pData[i];
          }
H
Haojun Liao 已提交
407
        }
H
Haojun Liao 已提交
408 409 410 411 412
      } else {  // max
        for (int32_t i = start; i < start + numOfRows; ++i) {
          if (*val < pData[i]) {
            *val = pData[i];
          }
H
Haojun Liao 已提交
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
        }
      }
    }

    numOfElems = numOfRows;
  }

  return numOfElems;
}

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 已提交
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
    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 已提交
447
    if (isMinFunc) {  // min
H
Haojun Liao 已提交
448
      for (; i < start + numOfRows; ++i) {
H
Haojun Liao 已提交
449 450 451 452
        if (colDataIsNull_f(pCol->nullbitmap, i)) {
          continue;
        }

H
Haojun Liao 已提交
453
        if (*val > pData[i]) {
H
Haojun Liao 已提交
454 455
          *val = pData[i];
          if (pCtx->subsidiaries.num > 0) {
H
Haojun Liao 已提交
456
            updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
H
Haojun Liao 已提交
457 458 459 460 461
          }
        }
        numOfElems += 1;
      }
    } else {  // max function
H
Haojun Liao 已提交
462
      for (; i < start + numOfRows; ++i) {
H
Haojun Liao 已提交
463 464 465 466
        if (colDataIsNull_f(pCol->nullbitmap, i)) {
          continue;
        }

H
Haojun Liao 已提交
467 468 469
        // ignore the equivalent data value
        // NOTE: An faster version to avoid one additional comparison with FPU.
        if (*val < pData[i]) {
H
Haojun Liao 已提交
470 471
          *val = pData[i];
          if (pCtx->subsidiaries.num > 0) {
H
Haojun Liao 已提交
472
            updateTupleData(pCtx, i, pCtx->pSrcBlock, &pBuf->tuplePos);
H
Haojun Liao 已提交
473
          }
H
Haojun Liao 已提交
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
        }
        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 已提交
496 497 498 499
          if (*val < pData[i]) {
            *val = pData[i];
          }
        }
H
Haojun Liao 已提交
500 501 502 503 504 505 506 507 508 509 510 511 512
      }
    }

    numOfElems = numOfRows;
  }

  return numOfElems;
}

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;
H
Haojun Liao 已提交
513

H
Haojun Liao 已提交
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
  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);
          }
        }
H
Haojun Liao 已提交
545 546
        numOfElems += 1;
      }
H
Haojun Liao 已提交
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563

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

H
Haojun Liao 已提交
564
    }
H
Haojun Liao 已提交
565 566 567 568
  } else {  // not has null value
    // AVX2 version to speedup the loop
    if (tsAVX2Enable && tsSIMDEnable) {
      *val = i8VectorCmpAVX2(pData, numOfRows, isMinFunc);
H
Haojun Liao 已提交
569 570 571 572
    } else {
      if (!pBuf->assign) {
        *val = pData[0];
        pBuf->assign = true;
H
Haojun Liao 已提交
573 574
      }

H
Haojun Liao 已提交
575 576 577 578 579
      if (isMinFunc) {  // min
        for (int32_t i = start; i < start + numOfRows; ++i) {
          if (*val > pData[i]) {
            *val = pData[i];
          }
H
Haojun Liao 已提交
580
        }
H
Haojun Liao 已提交
581 582 583 584 585
      } else {  // max
        for (int32_t i = start; i < start + numOfRows; ++i) {
          if (*val < pData[i]) {
            *val = pData[i];
          }
H
Haojun Liao 已提交
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 616 617 618 619 620 621 622 623 624 625 626 627
        }
      }
    }

    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 已提交
628
  if (pInput->colDataSMAIsSet) {
H
Haojun Liao 已提交
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
    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 已提交
722
      numOfElems = handleInt8Col(pCol, start, numOfRows, pCtx, pBuf, isMinFunc);
H
Haojun Liao 已提交
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 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 820 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 914 915 916 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
    } else if (type == TSDB_DATA_TYPE_SMALLINT) {
      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;
}