filter.c 101.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * 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 "os.h"
#include <tlog.h>
#include "thash.h"
//#include "queryLog.h"
#include "tcompare.h"
D
dapan1121 已提交
20
#include "filterInt.h"
D
dapan1121 已提交
21
#include "filter.h"
22 23

OptrStr gOptrStr[] = {
D
dapan1121 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
  {0,                                      "invalid"},
  {OP_TYPE_ADD,                            "+"},
  {OP_TYPE_SUB,                            "-"},
  {OP_TYPE_MULTI,                          "*"},
  {OP_TYPE_DIV,                            "/"},
  {OP_TYPE_MOD,                            "%"},

  // bit operator
  {OP_TYPE_BIT_AND,                        "&"},
  {OP_TYPE_BIT_OR,                         "|"},

  // comparison operator
  {OP_TYPE_GREATER_THAN,                   ">"},
  {OP_TYPE_GREATER_EQUAL,                  ">="},
  {OP_TYPE_LOWER_THAN,                     "<"},
  {OP_TYPE_LOWER_EQUAL,                    "<="},
  {OP_TYPE_EQUAL,                          "=="},
  {OP_TYPE_NOT_EQUAL,                      "!="},
  {OP_TYPE_IN,                             "in"},
  {OP_TYPE_NOT_IN,                         "not in"},
  {OP_TYPE_LIKE,                           "like"},
  {OP_TYPE_NOT_LIKE,                       "not like"},
  {OP_TYPE_MATCH,                          "match"},
  {OP_TYPE_NMATCH,                         "nmatch"},
  {OP_TYPE_IS_NULL,                        "is null"},
  {OP_TYPE_IS_NOT_NULL,                    "not null"},
  {OP_TYPE_IS_TRUE,                        "is true"},
  {OP_TYPE_IS_FALSE,                       "is false"},
  {OP_TYPE_IS_UNKNOWN,                     "is unknown"},
  {OP_TYPE_IS_NOT_TRUE,                    "not true"},
  {OP_TYPE_IS_NOT_FALSE,                   "not false"},
  {OP_TYPE_IS_NOT_UNKNOWN,                 "not unknown"},

  // json operator
  {OP_TYPE_JSON_GET_VALUE,                 "json get"},
  {OP_TYPE_JSON_CONTAINS,                  "json contains"}
60 61 62
};

bool filterRangeCompGi (const void *minv, const void *maxv, const void *minr, const void *maxr, __compar_fn_t cfunc) {
D
dapan1121 已提交
63
  int32_t result = cfunc(maxv, minr);
D
dapan1121 已提交
64
  //if (result == TSDB_DATA_JSON_CAN_NOT_COMPARE) return false;
D
dapan1121 已提交
65
  return result >= 0;
66 67
}
bool filterRangeCompGe (const void *minv, const void *maxv, const void *minr, const void *maxr, __compar_fn_t cfunc) {
D
dapan1121 已提交
68
  int32_t result = cfunc(maxv, minr);
D
dapan1121 已提交
69
  //if (result == TSDB_DATA_JSON_CAN_NOT_COMPARE) return false;
D
dapan1121 已提交
70
  return result > 0;
71 72
}
bool filterRangeCompLi (const void *minv, const void *maxv, const void *minr, const void *maxr, __compar_fn_t cfunc) {
D
dapan1121 已提交
73
  int32_t result = cfunc(minv, maxr);
D
dapan1121 已提交
74
  //if (result == TSDB_DATA_JSON_CAN_NOT_COMPARE) return false;
D
dapan1121 已提交
75
  return result <= 0;
76 77
}
bool filterRangeCompLe (const void *minv, const void *maxv, const void *minr, const void *maxr, __compar_fn_t cfunc) {
D
dapan1121 已提交
78
  int32_t result = cfunc(minv, maxr);
D
dapan1121 已提交
79
  //if (result == TSDB_DATA_JSON_CAN_NOT_COMPARE) return false;
D
dapan1121 已提交
80
  return result < 0;
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
}
bool filterRangeCompii (const void *minv, const void *maxv, const void *minr, const void *maxr, __compar_fn_t cfunc) {
  return cfunc(maxv, minr) >= 0 && cfunc(minv, maxr) <= 0;
}
bool filterRangeCompee (const void *minv, const void *maxv, const void *minr, const void *maxr, __compar_fn_t cfunc) {
  return cfunc(maxv, minr) > 0 && cfunc(minv, maxr) < 0;
}
bool filterRangeCompei (const void *minv, const void *maxv, const void *minr, const void *maxr, __compar_fn_t cfunc) {
  return cfunc(maxv, minr) > 0 && cfunc(minv, maxr) <= 0;
}
bool filterRangeCompie (const void *minv, const void *maxv, const void *minr, const void *maxr, __compar_fn_t cfunc) {
  return cfunc(maxv, minr) >= 0 && cfunc(minv, maxr) < 0;
}

rangeCompFunc filterGetRangeCompFunc(char sflag, char eflag) {
  if (FILTER_GET_FLAG(sflag, RANGE_FLG_NULL)) {
    if (FILTER_GET_FLAG(eflag, RANGE_FLG_EXCLUDE)) {
      return filterRangeCompLe;
    }
    
    return filterRangeCompLi;
  }

  if (FILTER_GET_FLAG(eflag, RANGE_FLG_NULL)) {
    if (FILTER_GET_FLAG(sflag, RANGE_FLG_EXCLUDE)) {
      return filterRangeCompGe;
    }
    
    return filterRangeCompGi;
  }

  if (FILTER_GET_FLAG(sflag, RANGE_FLG_EXCLUDE)) {
    if (FILTER_GET_FLAG(eflag, RANGE_FLG_EXCLUDE)) {
      return filterRangeCompee;
    }

    return filterRangeCompei;
  }

  if (FILTER_GET_FLAG(eflag, RANGE_FLG_EXCLUDE)) {
    return filterRangeCompie;
  }

  return filterRangeCompii;
}

rangeCompFunc gRangeCompare[] = {filterRangeCompee, filterRangeCompei, filterRangeCompie, filterRangeCompii, filterRangeCompGe,
 filterRangeCompGi, filterRangeCompLe, filterRangeCompLi};


int8_t filterGetRangeCompFuncFromOptrs(uint8_t optr, uint8_t optr2) {
  if (optr2) {
D
dapan1121 已提交
133
    assert(optr2 == OP_TYPE_LOWER_THAN || optr2 == OP_TYPE_LOWER_EQUAL);  
134

D
dapan1121 已提交
135 136
    if (optr == OP_TYPE_GREATER_THAN) {
      if (optr2 == OP_TYPE_LOWER_THAN) {
137 138 139 140 141 142
        return 0;
      }

      return 1;
    }

D
dapan1121 已提交
143
    if (optr2 == OP_TYPE_LOWER_THAN) {
144 145 146 147 148 149
      return 2;
    }

    return 3;
  } else {
    switch (optr) {
D
dapan1121 已提交
150
     case OP_TYPE_GREATER_THAN:
151
      return 4;
D
dapan1121 已提交
152
     case OP_TYPE_GREATER_EQUAL:
153
      return 5;
D
dapan1121 已提交
154
     case OP_TYPE_LOWER_THAN:
155
      return 6;
D
dapan1121 已提交
156
     case OP_TYPE_LOWER_EQUAL:
157 158 159 160 161 162 163 164 165 166
      return 7;
     default:
      break;
    }
  }

  return -1;
}

__compar_fn_t gDataCompare[] = {compareInt32Val, compareInt8Val, compareInt16Val, compareInt64Val, compareFloatVal,
D
dapan1121 已提交
167
  compareDoubleVal, compareLenPrefixedStr, compareStrPatternMatch, compareChkInString, compareWStrPatternMatch, 
168
  compareLenPrefixedWStr, compareUint8Val, compareUint16Val, compareUint32Val, compareUint64Val,
D
dapan1121 已提交
169 170 171
  setChkInBytes1, setChkInBytes2, setChkInBytes4, setChkInBytes8, compareStrRegexCompMatch, 
  compareStrRegexCompNMatch, setChkNotInBytes1, setChkNotInBytes2, setChkNotInBytes4, setChkNotInBytes8,
  compareChkNotInString, compareStrPatternNotMatch, compareWStrPatternNotMatch
172 173 174 175 176
};

int8_t filterGetCompFuncIdx(int32_t type, int32_t optr) {
  int8_t comparFn = 0;

D
dapan1121 已提交
177
  if (optr == OP_TYPE_IN && (type != TSDB_DATA_TYPE_BINARY && type != TSDB_DATA_TYPE_NCHAR)) {
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
    switch (type) {
      case TSDB_DATA_TYPE_BOOL:
      case TSDB_DATA_TYPE_TINYINT:  
      case TSDB_DATA_TYPE_UTINYINT:  
        return 15;
      case TSDB_DATA_TYPE_SMALLINT:
      case TSDB_DATA_TYPE_USMALLINT:
        return 16;
      case TSDB_DATA_TYPE_INT:
      case TSDB_DATA_TYPE_UINT:
      case TSDB_DATA_TYPE_FLOAT:        
        return 17;
      case TSDB_DATA_TYPE_BIGINT:        
      case TSDB_DATA_TYPE_UBIGINT:        
      case TSDB_DATA_TYPE_DOUBLE:        
      case TSDB_DATA_TYPE_TIMESTAMP:        
        return 18;
      default:
        assert(0);
    }
  }
D
dapan1121 已提交
199

D
dapan1121 已提交
200
  if (optr == OP_TYPE_NOT_IN && (type != TSDB_DATA_TYPE_BINARY && type != TSDB_DATA_TYPE_NCHAR)) {
D
dapan1121 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
    switch (type) {
      case TSDB_DATA_TYPE_BOOL:
      case TSDB_DATA_TYPE_TINYINT:  
      case TSDB_DATA_TYPE_UTINYINT:  
        return 21;
      case TSDB_DATA_TYPE_SMALLINT:
      case TSDB_DATA_TYPE_USMALLINT:
        return 22;
      case TSDB_DATA_TYPE_INT:
      case TSDB_DATA_TYPE_UINT:
      case TSDB_DATA_TYPE_FLOAT:        
        return 23;
      case TSDB_DATA_TYPE_BIGINT:        
      case TSDB_DATA_TYPE_UBIGINT:        
      case TSDB_DATA_TYPE_DOUBLE:        
      case TSDB_DATA_TYPE_TIMESTAMP:        
        return 24;
      default:
        assert(0);
    }
  }
222 223 224 225 226 227 228 229 230 231 232
  
  switch (type) {
    case TSDB_DATA_TYPE_BOOL:
    case TSDB_DATA_TYPE_TINYINT:   comparFn = 1;   break;
    case TSDB_DATA_TYPE_SMALLINT:  comparFn = 2;  break;
    case TSDB_DATA_TYPE_INT:       comparFn = 0;  break;
    case TSDB_DATA_TYPE_BIGINT:
    case TSDB_DATA_TYPE_TIMESTAMP: comparFn = 3;  break;
    case TSDB_DATA_TYPE_FLOAT:     comparFn = 4;  break;
    case TSDB_DATA_TYPE_DOUBLE:    comparFn = 5; break;
    case TSDB_DATA_TYPE_BINARY: {
D
dapan1121 已提交
233
      if (optr == OP_TYPE_MATCH) {
234
        comparFn = 19;
D
dapan1121 已提交
235
      } else if (optr == OP_TYPE_NMATCH) {
236
        comparFn = 20;
D
dapan1121 已提交
237
      } else if (optr == OP_TYPE_LIKE) { /* wildcard query using like operator */
238
        comparFn = 7;
D
dapan1121 已提交
239
      } else if (optr == OP_TYPE_NOT_LIKE) { /* wildcard query using like operator */
D
dapan1121 已提交
240
        comparFn = 26;
D
dapan1121 已提交
241
      } else if (optr == OP_TYPE_IN) {
242
        comparFn = 8;
D
dapan1121 已提交
243
      } else if (optr == OP_TYPE_NOT_IN) {
D
dapan1121 已提交
244
        comparFn = 25;
245 246 247 248 249 250 251 252
      } else { /* normal relational comparFn */
        comparFn = 6;
      }
    
      break;
    }
  
    case TSDB_DATA_TYPE_NCHAR: {
D
dapan1121 已提交
253
      if (optr == OP_TYPE_MATCH) {
254
        comparFn = 19;
D
dapan1121 已提交
255
      } else if (optr == OP_TYPE_NMATCH) {
256
        comparFn = 20;
D
dapan1121 已提交
257
      } else if (optr == OP_TYPE_LIKE) {
258
        comparFn = 9;
D
dapan1121 已提交
259
      } else if (optr == OP_TYPE_LIKE) {
D
dapan1121 已提交
260
        comparFn = 27;
D
dapan1121 已提交
261
      } else if (optr == OP_TYPE_IN) {
262
        comparFn = 8;
D
dapan1121 已提交
263
      } else if (optr == OP_TYPE_NOT_IN) {
D
dapan1121 已提交
264
        comparFn = 25;
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
      } else {
        comparFn = 10;
      }
      break;
    }

    case TSDB_DATA_TYPE_UTINYINT:  comparFn = 11; break;
    case TSDB_DATA_TYPE_USMALLINT: comparFn = 12;break;
    case TSDB_DATA_TYPE_UINT:      comparFn = 13;break;
    case TSDB_DATA_TYPE_UBIGINT:   comparFn = 14;break;

    default:
      comparFn = 0;
      break;
  }
  
  return comparFn;
}

D
dapan1121 已提交
284 285 286 287
__compar_fn_t filterGetCompFunc(int32_t type, int32_t optr) {
  return gDataCompare[filterGetCompFuncIdx(type, optr)];
}

288 289 290 291 292 293 294 295 296 297 298 299 300 301

static FORCE_INLINE int32_t filterCompareGroupCtx(const void *pLeft, const void *pRight) {
  SFilterGroupCtx *left = *((SFilterGroupCtx**)pLeft), *right = *((SFilterGroupCtx**)pRight);
  if (left->colNum > right->colNum) return 1;
  if (left->colNum < right->colNum) return -1;
  return 0;
}

int32_t filterInitUnitsFields(SFilterInfo *info) {
  info->unitSize = FILTER_DEFAULT_UNIT_SIZE;
  info->units = calloc(info->unitSize, sizeof(SFilterUnit));
  
  info->fields[FLD_TYPE_COLUMN].num = 0;
  info->fields[FLD_TYPE_COLUMN].size = FILTER_DEFAULT_FIELD_SIZE;
D
dapan1121 已提交
302
  info->fields[FLD_TYPE_COLUMN].fields = calloc(info->fields[FLD_TYPE_COLUMN].size, sizeof(SFilterField));
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
  info->fields[FLD_TYPE_VALUE].num = 0;
  info->fields[FLD_TYPE_VALUE].size = FILTER_DEFAULT_FIELD_SIZE;
  info->fields[FLD_TYPE_VALUE].fields = calloc(info->fields[FLD_TYPE_VALUE].size, sizeof(SFilterField));

  return TSDB_CODE_SUCCESS;
}

static FORCE_INLINE SFilterRangeNode* filterNewRange(SFilterRangeCtx *ctx, SFilterRange* ra) {
  SFilterRangeNode *r = NULL;
  
  if (ctx->rf) {
    r = ctx->rf;
    ctx->rf = ctx->rf->next;
    r->prev = NULL;
    r->next = NULL;
  } else {
    r = calloc(1, sizeof(SFilterRangeNode)); 
  }

  FILTER_COPY_RA(&r->ra, ra);

  return r;
}

void* filterInitRangeCtx(int32_t type, int32_t options) {
  if (type > TSDB_DATA_TYPE_UBIGINT || type < TSDB_DATA_TYPE_BOOL || type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_NCHAR) {
D
dapan1121 已提交
329
    qError("not supported range type:%d", type);
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
    return NULL;
  }
  
  SFilterRangeCtx *ctx = calloc(1, sizeof(SFilterRangeCtx));

  ctx->type = type;
  ctx->options = options;
  ctx->pCompareFunc = getComparFunc(type, 0);

  return ctx;
}


int32_t filterResetRangeCtx(SFilterRangeCtx *ctx) {
  ctx->status = 0;

  if (ctx->rf == NULL) {
    ctx->rf = ctx->rs;
    ctx->rs = NULL;
    return TSDB_CODE_SUCCESS;
  }

  ctx->isnull = false;
  ctx->notnull = false;
  ctx->isrange = false;

  SFilterRangeNode *r = ctx->rf;
  
  while (r && r->next) {
    r = r->next;
  }

  r->next = ctx->rs;
  ctx->rs = NULL;
  return TSDB_CODE_SUCCESS;
}

int32_t filterReuseRangeCtx(SFilterRangeCtx *ctx, int32_t type, int32_t options) {
  filterResetRangeCtx(ctx);

  ctx->type = type;
  ctx->options = options;
  ctx->pCompareFunc = getComparFunc(type, 0);

  return TSDB_CODE_SUCCESS;
}


int32_t filterConvertRange(SFilterRangeCtx *cur, SFilterRange *ra, bool *notNull) {
  if (!FILTER_GET_FLAG(ra->sflag, RANGE_FLG_NULL)) {
    int32_t sr = cur->pCompareFunc(&ra->s, getDataMin(cur->type));
    if (sr == 0) {
      FILTER_SET_FLAG(ra->sflag, RANGE_FLG_NULL);
    }
  }

  if (!FILTER_GET_FLAG(ra->eflag, RANGE_FLG_NULL)) {
    int32_t er = cur->pCompareFunc(&ra->e, getDataMax(cur->type));
    if (er == 0) {
      FILTER_SET_FLAG(ra->eflag, RANGE_FLG_NULL);
    }
  }

  
  if (FILTER_GET_FLAG(ra->sflag, RANGE_FLG_NULL) && FILTER_GET_FLAG(ra->eflag, RANGE_FLG_NULL)) {
    *notNull = true;
  } else {
    *notNull = false;
  }

  return TSDB_CODE_SUCCESS;
}

int32_t filterAddRangeOptr(void* h, uint8_t raOptr, int32_t optr, bool *empty, bool *all) {
  SFilterRangeCtx *ctx = (SFilterRangeCtx *)h;

D
dapan1121 已提交
406
  if (optr == LOGIC_COND_TYPE_AND) {
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
    SET_AND_OPTR(ctx, raOptr);
    if (CHK_AND_OPTR(ctx) || (raOptr == FILTER_DUMMY_EMPTY_OPTR)) {
      FILTER_SET_FLAG(ctx->status, MR_ST_EMPTY);
      *empty = true;
    }
  } else {
    SET_OR_OPTR(ctx, raOptr);
    if (CHK_OR_OPTR(ctx)) {
      FILTER_SET_FLAG(ctx->status, MR_ST_ALL);
      *all = true;
    }
  }

  return TSDB_CODE_SUCCESS;
}



int32_t filterAddRangeImpl(void* h, SFilterRange* ra, int32_t optr) {
  SFilterRangeCtx *ctx = (SFilterRangeCtx *)h;

  if (ctx->rs == NULL) {
    if ((FILTER_GET_FLAG(ctx->status, MR_ST_START) == 0) 
D
dapan1121 已提交
430 431
      || (FILTER_GET_FLAG(ctx->status, MR_ST_ALL) && (optr == LOGIC_COND_TYPE_AND))
      || ((!FILTER_GET_FLAG(ctx->status, MR_ST_ALL)) && (optr == LOGIC_COND_TYPE_OR))) {
432 433 434 435 436 437 438 439 440 441 442
      APPEND_RANGE(ctx, ctx->rs, ra);
      FILTER_SET_FLAG(ctx->status, MR_ST_START);
    }

    return TSDB_CODE_SUCCESS;
  }

  SFilterRangeNode *r = ctx->rs;
  SFilterRangeNode *rn = NULL;
  int32_t cr = 0;

D
dapan1121 已提交
443
  if (optr == LOGIC_COND_TYPE_AND) {
444 445 446 447 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 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 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 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
    while (r != NULL) {
      cr = ctx->pCompareFunc(&r->ra.s, &ra->e);
      if (FILTER_GREATER(cr, r->ra.sflag, ra->eflag)) {
        FREE_FROM_RANGE(ctx, r);
        break;
      }

      cr = ctx->pCompareFunc(&ra->s, &r->ra.e);
      if (FILTER_GREATER(cr, ra->sflag, r->ra.eflag)) {
        rn = r->next;
        FREE_RANGE(ctx, r);
        r = rn;
        continue;
      }

      cr = ctx->pCompareFunc(&ra->s, &r->ra.s);
      if (FILTER_GREATER(cr, ra->sflag, r->ra.sflag)) {
        SIMPLE_COPY_VALUES((char *)&r->ra.s, &ra->s);
        cr == 0 ? (r->ra.sflag |= ra->sflag) : (r->ra.sflag = ra->sflag);
      }

      cr = ctx->pCompareFunc(&r->ra.e, &ra->e);
      if (FILTER_GREATER(cr, r->ra.eflag, ra->eflag)) {
        SIMPLE_COPY_VALUES((char *)&r->ra.e, &ra->e);
        cr == 0 ? (r->ra.eflag |= ra->eflag) : (r->ra.eflag = ra->eflag);
        break;
      }

      r = r->next;
    }

    return TSDB_CODE_SUCCESS;
  }


  //TSDB_RELATION_OR
  
  bool smerged = false;
  bool emerged = false;

  while (r != NULL) {
    cr = ctx->pCompareFunc(&r->ra.s, &ra->e);
    if (FILTER_GREATER(cr, r->ra.sflag, ra->eflag)) {    
      if (emerged == false) {
        INSERT_RANGE(ctx, r, ra);
      }
      
      break;
    }

    if (smerged == false) {
      cr = ctx->pCompareFunc(&ra->s, &r->ra.e);
      if (FILTER_GREATER(cr, ra->sflag, r->ra.eflag)) {   
        if (r->next) {
          r= r->next;
          continue;
        }

        APPEND_RANGE(ctx, r, ra);
        break;
      }

      cr = ctx->pCompareFunc(&r->ra.s, &ra->s);
      if (FILTER_GREATER(cr, r->ra.sflag, ra->sflag)) {         
        SIMPLE_COPY_VALUES((char *)&r->ra.s, &ra->s);        
        cr == 0 ? (r->ra.sflag &= ra->sflag) : (r->ra.sflag = ra->sflag);
      }

      smerged = true;
    }
    
    if (emerged == false) {
      cr = ctx->pCompareFunc(&ra->e, &r->ra.e);
      if (FILTER_GREATER(cr, ra->eflag, r->ra.eflag)) {
        SIMPLE_COPY_VALUES((char *)&r->ra.e, &ra->e);
        if (cr == 0) { 
          r->ra.eflag &= ra->eflag;
          break;
        }
        
        r->ra.eflag = ra->eflag;
        emerged = true;
        r = r->next;
        continue;
      }

      break;
    }

    cr = ctx->pCompareFunc(&ra->e, &r->ra.e);
    if (FILTER_GREATER(cr, ra->eflag, r->ra.eflag)) {
      rn = r->next;
      FREE_RANGE(ctx, r);
      r = rn;

      continue;
    } else {
      SIMPLE_COPY_VALUES(&r->prev->ra.e, (char *)&r->ra.e);
      cr == 0 ? (r->prev->ra.eflag &= r->ra.eflag) : (r->prev->ra.eflag = r->ra.eflag);
      FREE_RANGE(ctx, r);
      
      break;
    }
  }

  if (ctx->rs && ctx->rs->next == NULL) {
    bool notnull;
    filterConvertRange(ctx, &ctx->rs->ra, &notnull);
    if (notnull) {
      bool all = false;
      FREE_FROM_RANGE(ctx, ctx->rs);
D
dapan1121 已提交
555
      filterAddRangeOptr(h, OP_TYPE_IS_NOT_NULL, optr, NULL, &all);
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
      if (all) {
        FILTER_SET_FLAG(ctx->status, MR_ST_ALL);
      }
    }
  }

  return TSDB_CODE_SUCCESS;  
}

int32_t filterAddRange(void* h, SFilterRange* ra, int32_t optr) {
  SFilterRangeCtx *ctx = (SFilterRangeCtx *)h;
  
  if (FILTER_GET_FLAG(ra->sflag, RANGE_FLG_NULL)) {
    SIMPLE_COPY_VALUES(&ra->s, getDataMin(ctx->type));
    //FILTER_CLR_FLAG(ra->sflag, RA_NULL);
  }

  if (FILTER_GET_FLAG(ra->eflag, RANGE_FLG_NULL)) {
    SIMPLE_COPY_VALUES(&ra->e, getDataMax(ctx->type));
    //FILTER_CLR_FLAG(ra->eflag, RA_NULL);
  }

  return filterAddRangeImpl(h, ra, optr);
}


int32_t filterAddRangeCtx(void *dst, void *src, int32_t optr) {
  SFilterRangeCtx *dctx = (SFilterRangeCtx *)dst;
  SFilterRangeCtx *sctx = (SFilterRangeCtx *)src;

D
dapan1121 已提交
586
  assert(optr == LOGIC_COND_TYPE_OR);
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 628 629 630 631 632 633 634 635 636

  if (sctx->rs == NULL) {
    return TSDB_CODE_SUCCESS;
  }

  SFilterRangeNode *r = sctx->rs;
  
  while (r) {
    filterAddRange(dctx, &r->ra, optr);
    r = r->next;
  }

  return TSDB_CODE_SUCCESS;
}

int32_t filterCopyRangeCtx(void *dst, void *src) {
  SFilterRangeCtx *dctx = (SFilterRangeCtx *)dst;
  SFilterRangeCtx *sctx = (SFilterRangeCtx *)src;

  dctx->status = sctx->status;
  
  dctx->isnull = sctx->isnull;
  dctx->notnull = sctx->notnull;
  dctx->isrange = sctx->isrange;

  SFilterRangeNode *r = sctx->rs;
  SFilterRangeNode *dr = dctx->rs;
  
  while (r) {
    APPEND_RANGE(dctx, dr, &r->ra);
    if (dr == NULL) {
      dr = dctx->rs;
    } else {
      dr = dr->next;
    }
    r = r->next;
  }

  return TSDB_CODE_SUCCESS;
}



int32_t filterFinishRange(void* h) {
  SFilterRangeCtx *ctx = (SFilterRangeCtx *)h;

  if (FILTER_GET_FLAG(ctx->status, MR_ST_FIN)) {
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
637
  if (FILTER_GET_FLAG(ctx->options, FLT_OPTION_TIMESTAMP)) {
638 639 640 641 642
    SFilterRangeNode *r = ctx->rs;
    SFilterRangeNode *rn = NULL;
    
    while (r && r->next) {
      int64_t tmp = 1;
D
dapan1121 已提交
643
      operateVal(&tmp, &r->ra.e, &tmp, OP_TYPE_ADD, ctx->type);
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
      if (ctx->pCompareFunc(&tmp, &r->next->ra.s) == 0) {
        rn = r->next;
        SIMPLE_COPY_VALUES((char *)&r->next->ra.s, (char *)&r->ra.s);
        FREE_RANGE(ctx, r);
        r = rn;
      
        continue;
      }
      
      r = r->next;
    }
  }

  FILTER_SET_FLAG(ctx->status, MR_ST_FIN);

  return TSDB_CODE_SUCCESS;
}

int32_t filterGetRangeNum(void* h, int32_t* num) {
  filterFinishRange(h);
  
  SFilterRangeCtx *ctx = (SFilterRangeCtx *)h;

  *num = 0;

  SFilterRangeNode *r = ctx->rs;
  
  while (r) {
    ++(*num);
    r = r->next;
  }

  return TSDB_CODE_SUCCESS;
}


int32_t filterGetRangeRes(void* h, SFilterRange *ra) {
  filterFinishRange(h);

  SFilterRangeCtx *ctx = (SFilterRangeCtx *)h;
  uint32_t num = 0;
  SFilterRangeNode* r = ctx->rs;
  
  while (r) {
    FILTER_COPY_RA(ra, &r->ra);

    ++num;
    r = r->next;
    ++ra;
  }

  if (num == 0) {
D
dapan1121 已提交
696
    qError("no range result");
697 698 699 700 701 702 703 704 705 706 707
    return TSDB_CODE_QRY_APP_ERROR;
  }
  
  return TSDB_CODE_SUCCESS;
}


int32_t filterSourceRangeFromCtx(SFilterRangeCtx *ctx, void *sctx, int32_t optr, bool *empty, bool *all) {
  SFilterRangeCtx *src = (SFilterRangeCtx *)sctx;

  if (src->isnull){
D
dapan1121 已提交
708
    filterAddRangeOptr(ctx, OP_TYPE_IS_NULL, optr, empty, all);
709 710 711 712 713 714
    if (FILTER_GET_FLAG(ctx->status, MR_ST_ALL)) {
      *all = true;
    }
  }

  if (src->notnull) {
D
dapan1121 已提交
715
    filterAddRangeOptr(ctx, OP_TYPE_IS_NOT_NULL, optr, empty, all);
716 717 718 719 720 721 722 723
    if (FILTER_GET_FLAG(ctx->status, MR_ST_ALL)) {
      *all = true;
    }
  }

  if (src->isrange) {
    filterAddRangeOptr(ctx, 0, optr, empty, all);

D
dapan1121 已提交
724
    if (!(optr == LOGIC_COND_TYPE_OR && ctx->notnull)) {
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
      filterAddRangeCtx(ctx, src, optr);
    }
    
    if (FILTER_GET_FLAG(ctx->status, MR_ST_ALL)) {
      *all = true;
    }
  }

  return TSDB_CODE_SUCCESS;
}



int32_t filterFreeRangeCtx(void* h) {
  if (h == NULL) {
    return TSDB_CODE_SUCCESS;
  }
  
  SFilterRangeCtx *ctx = (SFilterRangeCtx *)h;
  SFilterRangeNode *r = ctx->rs;
  SFilterRangeNode *rn = NULL;
  
  while (r) {
    rn = r->next;
    free(r);
    r = rn;
  }

  r = ctx->rf;
  while (r) {
    rn = r->next;
    free(r);
    r = rn;
  }

  free(ctx);

  return TSDB_CODE_SUCCESS;
}


int32_t filterDetachCnfGroup(SFilterGroup *gp1, SFilterGroup *gp2, SArray* group) {
  SFilterGroup gp = {0};

  gp.unitNum = gp1->unitNum + gp2->unitNum;
  gp.unitIdxs = calloc(gp.unitNum, sizeof(*gp.unitIdxs));
  memcpy(gp.unitIdxs, gp1->unitIdxs, gp1->unitNum * sizeof(*gp.unitIdxs));
  memcpy(gp.unitIdxs + gp1->unitNum, gp2->unitIdxs, gp2->unitNum * sizeof(*gp.unitIdxs));    

  gp.unitFlags = NULL;
  
  taosArrayPush(group, &gp);

  return TSDB_CODE_SUCCESS;
}


int32_t filterDetachCnfGroups(SArray* group, SArray* left, SArray* right) {
  int32_t leftSize = (int32_t)taosArrayGetSize(left);
  int32_t rightSize = (int32_t)taosArrayGetSize(right);

D
dapan1121 已提交
786
  if (taosArrayGetSize(left) <= 0) {
D
dapan1121 已提交
787 788 789 790 791 792 793 794 795 796 797
    if (taosArrayGetSize(right) <= 0) {
      fltError("both groups are empty");
      FLT_ERR_RET(TSDB_CODE_QRY_APP_ERROR);      
    }

    SFilterGroup *gp = NULL;
    while (gp = (SFilterGroup *)taosArrayPop(right)) {
      taosArrayPush(group, gp);
    }

    return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
798 799 800
  }

  if (taosArrayGetSize(right) <= 0) { 
D
dapan1121 已提交
801 802 803 804 805 806
    SFilterGroup *gp = NULL;
    while (gp = (SFilterGroup *)taosArrayPop(left)) {
      taosArrayPush(group, gp);
    }

    return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
807
  }
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
  
  for (int32_t l = 0; l < leftSize; ++l) {
    SFilterGroup *gp1 = taosArrayGet(left, l);
    
    for (int32_t r = 0; r < rightSize; ++r) {
      SFilterGroup *gp2 = taosArrayGet(right, r);

      filterDetachCnfGroup(gp1, gp2, group);
    }
  }


  return TSDB_CODE_SUCCESS;
}

int32_t filterGetFiledByDesc(SFilterFields* fields, int32_t type, void *v) {
D
dapan1121 已提交
824
  for (uint32_t i = 0; i < fields->num; ++i) {
D
dapan 已提交
825
    if (nodesEqualNode(fields->fields[i].desc, v)) {
826 827 828 829 830 831 832 833 834 835 836
      return i;
    }
  }

  return -1;
}


int32_t filterGetFiledByData(SFilterInfo *info, int32_t type, void *v, int32_t dataLen) {
  if (type == FLD_TYPE_VALUE) {
    if (info->pctx.valHash == false) {
D
dapan1121 已提交
837
      qError("value hash is empty");
838 839 840 841 842 843 844 845 846 847 848 849
      return -1;
    }

    void *hv = taosHashGet(info->pctx.valHash, v, dataLen);
    if (hv) {
      return *(int32_t *)hv;
    }
  }

  return -1;
}

D
dapan1121 已提交
850 851
// In the params, we should use void *data instead of void **data, there is no need to use tfree(*data) to set *data = 0
// Besides, fields data value is a pointer, so dataLen should be POINTER_BYTES for better.
852 853
int32_t filterAddField(SFilterInfo *info, void *desc, void **data, int32_t type, SFilterFieldId *fid, int32_t dataLen, bool freeIfExists) {
  int32_t idx = -1;
D
dapan1121 已提交
854
  uint32_t *num;
855 856 857 858 859 860

  num = &info->fields[type].num;

  if (*num > 0) {
    if (type == FLD_TYPE_COLUMN) {
      idx = filterGetFiledByDesc(&info->fields[type], type, desc);
D
dapan1121 已提交
861
    } else if (data && (*data) && dataLen > 0 && FILTER_GET_FLAG(info->options, FLT_OPTION_NEED_UNIQE)) {
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
      idx = filterGetFiledByData(info, type, *data, dataLen);
    }
  }
  
  if (idx < 0) {
    idx = *num;
    if (idx >= info->fields[type].size) {
      info->fields[type].size += FILTER_DEFAULT_FIELD_SIZE;
      info->fields[type].fields = realloc(info->fields[type].fields, info->fields[type].size * sizeof(SFilterField));
    }
    
    info->fields[type].fields[idx].flag = type;  
    info->fields[type].fields[idx].desc = desc;
    info->fields[type].fields[idx].data = data ? *data : NULL;

    if (type == FLD_TYPE_COLUMN) {
      FILTER_SET_FLAG(info->fields[type].fields[idx].flag, FLD_DATA_NO_FREE);
    }

    ++(*num);

D
dapan1121 已提交
883
    if (data && (*data) && dataLen > 0 && FILTER_GET_FLAG(info->options, FLT_OPTION_NEED_UNIQE)) {
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
      if (info->pctx.valHash == NULL) {
        info->pctx.valHash = taosHashInit(FILTER_DEFAULT_GROUP_SIZE * FILTER_DEFAULT_VALUE_SIZE, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, false);
      }
      
      taosHashPut(info->pctx.valHash, *data, dataLen, &idx, sizeof(idx));
    }
  } else {
    if (data && freeIfExists) {
      tfree(*data);
    }
  }

  fid->type = type;
  fid->idx = idx;
  
  return TSDB_CODE_SUCCESS;
}

static FORCE_INLINE int32_t filterAddColFieldFromField(SFilterInfo *info, SFilterField *field, SFilterFieldId *fid) {
  filterAddField(info, field->desc, &field->data, FILTER_GET_TYPE(field->flag), fid, 0, false);

  FILTER_SET_FLAG(field->flag, FLD_DATA_NO_FREE);

  return TSDB_CODE_SUCCESS;
}


D
dapan 已提交
911
int32_t filterAddFieldFromNode(SFilterInfo *info, SNode *node, SFilterFieldId *fid) {
D
dapan1121 已提交
912 913 914 915 916
  if (node == NULL) {
    fltError("empty node");
    FLT_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
  }
  
X
Xiaoyu Wang 已提交
917
  if (nodeType(node) != QUERY_NODE_COLUMN && nodeType(node) != QUERY_NODE_VALUE && nodeType(node) != QUERY_NODE_NODE_LIST) {
D
dapan1121 已提交
918 919
    FLT_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
  }
920 921 922 923
  
  int32_t type;
  void *v;

X
Xiaoyu Wang 已提交
924
  if (nodeType(node) == QUERY_NODE_COLUMN) {
925
    type = FLD_TYPE_COLUMN;
D
dapan 已提交
926
    v = node;
927 928
  } else {
    type = FLD_TYPE_VALUE;
D
dapan 已提交
929
    v = node;
930 931 932 933 934 935 936
  }

  filterAddField(info, v, NULL, type, fid, 0, true);
  
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
937
int32_t filterAddUnit(SFilterInfo *info, uint8_t optr, SFilterFieldId *left, SFilterFieldId *right, uint32_t *uidx) {
D
dapan1121 已提交
938
  if (FILTER_GET_FLAG(info->options, FLT_OPTION_NEED_UNIQE)) {
939 940 941 942 943 944 945
    if (info->pctx.unitHash == NULL) {
      info->pctx.unitHash = taosHashInit(FILTER_DEFAULT_GROUP_SIZE * FILTER_DEFAULT_UNIT_SIZE, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, false);
    } else {
      int64_t v = 0;
      FILTER_PACKAGE_UNIT_HASH_KEY(&v, optr, left->idx, right ? right->idx : -1);
      void *hu = taosHashGet(info->pctx.unitHash, &v, sizeof(v));
      if (hu) {
D
dapan1121 已提交
946
        *uidx = *(uint32_t *)hu;
947 948 949 950 951 952
        return TSDB_CODE_SUCCESS;
      }
    }
  }

  if (info->unitNum >= info->unitSize) {
D
dapan1121 已提交
953
    uint32_t psize = info->unitSize;
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
    info->unitSize += FILTER_DEFAULT_UNIT_SIZE;
    info->units = realloc(info->units, info->unitSize * sizeof(SFilterUnit));
    memset(info->units + psize, 0, sizeof(*info->units) * FILTER_DEFAULT_UNIT_SIZE);
  }

  SFilterUnit *u = &info->units[info->unitNum];
  
  u->compare.optr = optr;
  u->left = *left;
  if (right) {
    u->right = *right;
  }

  if (u->right.type == FLD_TYPE_VALUE) {
    SFilterField *val = FILTER_UNIT_RIGHT_FIELD(info, u);  
    assert(FILTER_GET_FLAG(val->flag, FLD_TYPE_VALUE));
  } else {
D
dapan1121 已提交
971 972 973 974
    int32_t paramNum = scalarGetOperatorParamNum(optr);
    if (1 != paramNum) {
      fltError("invalid right field in unit, operator:%s, rightType:%d", gOptrStr[optr].str, u->right.type);
      return TSDB_CODE_QRY_APP_ERROR;
D
dapan1121 已提交
975
    }
976 977 978 979 980 981 982 983 984
  }
  
  SFilterField *col = FILTER_UNIT_LEFT_FIELD(info, u);
  assert(FILTER_GET_FLAG(col->flag, FLD_TYPE_COLUMN));
  
  info->units[info->unitNum].compare.type = FILTER_GET_COL_FIELD_TYPE(col);

  *uidx = info->unitNum;

D
dapan1121 已提交
985
  if (FILTER_GET_FLAG(info->options, FLT_OPTION_NEED_UNIQE)) {
986 987 988 989 990 991 992 993 994 995 996 997
    int64_t v = 0;
    FILTER_PACKAGE_UNIT_HASH_KEY(&v, optr, left->idx, right ? right->idx : -1);  
    taosHashPut(info->pctx.unitHash, &v, sizeof(v), uidx, sizeof(*uidx));
  }
  
  ++info->unitNum;
  
  return TSDB_CODE_SUCCESS;
}



D
dapan1121 已提交
998
int32_t filterAddUnitToGroup(SFilterGroup *group, uint32_t unitIdx) {
999 1000 1001 1002 1003 1004 1005 1006 1007 1008
  if (group->unitNum >= group->unitSize) {
    group->unitSize += FILTER_DEFAULT_UNIT_SIZE;
    group->unitIdxs = realloc(group->unitIdxs, group->unitSize * sizeof(*group->unitIdxs));
  }
  
  group->unitIdxs[group->unitNum++] = unitIdx;

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1009
int32_t fltAddGroupUnitFromNode(SFilterInfo *info, SNode* tree, SArray *group) {
D
dapan 已提交
1010
  SOperatorNode *node = (SOperatorNode *)tree;
D
dapan1121 已提交
1011
  int32_t ret = TSDB_CODE_SUCCESS;
1012
  SFilterFieldId left = {0}, right = {0};
D
dapan 已提交
1013
  filterAddFieldFromNode(info, node->pLeft, &left);
D
dapan1121 已提交
1014 1015 1016
  uint8_t type = FILTER_GET_COL_FIELD_TYPE(FILTER_GET_FIELD(info, left));
  int32_t len = 0;
  uint32_t uidx = 0;
D
dapan 已提交
1017
  int32_t code = 0;
1018

D
dapan 已提交
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
  if (node->opType == OP_TYPE_IN && (!IS_VAR_DATA_TYPE(type))) {
    SNodeListNode *listNode = (SNodeListNode *)node->pRight;
    SListCell *cell = listNode->pNodeList->pHead;
    SScalarParam in = {.num = 1}, out = {.num = 1, .type = type};
    
    for (int32_t i = 0; i < listNode->pNodeList->length; ++i) {
      SValueNode *valueNode = (SValueNode *)cell->pNode;
      in.type = valueNode->node.resType.type;
      in.bytes = valueNode->node.resType.bytes;
      in.data = nodesGetValueFromNode(valueNode);
      out.data = malloc(sizeof(int64_t));

      code = vectorConvertImpl(&in, &out);
      if (code) {
        fltError("convert from %d to %d failed", in.type, out.type);
        tfree(out.data);
        FLT_ERR_RET(code);
      }
1037
      
D
dapan1121 已提交
1038 1039
      len = tDataTypes[type].bytes;

D
dapan1121 已提交
1040
      filterAddField(info, NULL, &out.data, FLD_TYPE_VALUE, &right, len, true);
1041

D
dapan1121 已提交
1042
      filterAddUnit(info, OP_TYPE_EQUAL, &left, &right, &uidx);
1043 1044 1045 1046 1047
      
      SFilterGroup fgroup = {0};
      filterAddUnitToGroup(&fgroup, uidx);
      
      taosArrayPush(group, &fgroup);
D
dapan1121 已提交
1048 1049

      cell = cell->pNext;
1050 1051
    }
  } else {
D
dapan 已提交
1052
    filterAddFieldFromNode(info, node->pRight, &right);
1053
    
D
dapan 已提交
1054
    FLT_ERR_RET(filterAddUnit(info, node->opType, &left, &right, &uidx));
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065

    SFilterGroup fgroup = {0};
    filterAddUnitToGroup(&fgroup, uidx);
    
    taosArrayPush(group, &fgroup);
  }
  
  return TSDB_CODE_SUCCESS;
}


D
dapan1121 已提交
1066
int32_t filterAddUnitFromUnit(SFilterInfo *dst, SFilterInfo *src, SFilterUnit* u, uint32_t *uidx) {
1067 1068
  SFilterFieldId left, right, *pright = &right;
  int32_t type = FILTER_UNIT_DATA_TYPE(u);
D
dapan1121 已提交
1069
  uint16_t flag = 0;
1070 1071 1072 1073 1074 1075 1076

  filterAddField(dst, FILTER_UNIT_COL_DESC(src, u), NULL, FLD_TYPE_COLUMN, &left, 0, false);
  SFilterField *t = FILTER_UNIT_LEFT_FIELD(src, u);
  
  if (u->right.type == FLD_TYPE_VALUE) {
    void *data = FILTER_UNIT_VAL_DATA(src, u);
    if (IS_VAR_DATA_TYPE(type)) {
D
dapan1121 已提交
1077
      if (FILTER_UNIT_OPTR(u) ==  OP_TYPE_IN) {
D
dapan1121 已提交
1078
        filterAddField(dst, NULL, &data, FLD_TYPE_VALUE, &right, POINTER_BYTES, false); // POINTER_BYTES should be sizeof(SHashObj), but POINTER_BYTES is also right.
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099

        t = FILTER_GET_FIELD(dst, right);
        
        FILTER_SET_FLAG(t->flag, FLD_DATA_IS_HASH);
      } else {
        filterAddField(dst, NULL, &data, FLD_TYPE_VALUE, &right, varDataTLen(data), false);
      }
    } else {
      filterAddField(dst, NULL, &data, FLD_TYPE_VALUE, &right, tDataTypes[type].bytes, false);
    }

    flag = FLD_DATA_NO_FREE;    
    t = FILTER_UNIT_RIGHT_FIELD(src, u);
    FILTER_SET_FLAG(t->flag, flag);
  } else {
    pright = NULL;
  }

  return filterAddUnit(dst, FILTER_UNIT_OPTR(u), &left, pright, uidx);
}

D
dapan1121 已提交
1100
int32_t filterAddUnitRight(SFilterInfo *info, uint8_t optr, SFilterFieldId *right, uint32_t uidx) {
1101 1102 1103 1104 1105 1106 1107 1108 1109
  SFilterUnit *u = &info->units[uidx];
  
  u->compare.optr2 = optr;
  u->right2 = *right;

  return TSDB_CODE_SUCCESS;
}


D
dapan1121 已提交
1110
int32_t filterAddGroupUnitFromCtx(SFilterInfo *dst, SFilterInfo *src, SFilterRangeCtx *ctx, uint32_t cidx, SFilterGroup *g, int32_t optr, SArray *res) {
1111
  SFilterFieldId left, right, right2;
D
dapan1121 已提交
1112
  uint32_t uidx = 0;
1113 1114 1115 1116 1117 1118 1119

  SFilterField *col = FILTER_GET_COL_FIELD(src, cidx);

  filterAddColFieldFromField(dst, col, &left);

  int32_t type = FILTER_GET_COL_FIELD_TYPE(FILTER_GET_FIELD(dst, left));

D
dapan1121 已提交
1120
  if (optr == LOGIC_COND_TYPE_AND) {
1121 1122
    if (ctx->isnull) {
      assert(ctx->notnull == false && ctx->isrange == false);
D
dapan1121 已提交
1123
      filterAddUnit(dst, OP_TYPE_IS_NULL, &left, NULL, &uidx);
1124 1125 1126 1127 1128 1129
      filterAddUnitToGroup(g, uidx);
      return TSDB_CODE_SUCCESS;
    }

    if (ctx->notnull) {      
      assert(ctx->isnull == false && ctx->isrange == false);
D
dapan1121 已提交
1130
      filterAddUnit(dst, OP_TYPE_IS_NOT_NULL, &left, NULL, &uidx);
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
      filterAddUnitToGroup(g, uidx);
      return TSDB_CODE_SUCCESS;
    }

    if (!ctx->isrange) {
      assert(ctx->isnull || ctx->notnull);
      return TSDB_CODE_SUCCESS;
    }

    assert(ctx->rs && ctx->rs->next == NULL);

    SFilterRange *ra = &ctx->rs->ra;
    
    assert(!((FILTER_GET_FLAG(ra->sflag, RANGE_FLG_NULL)) && (FILTER_GET_FLAG(ra->eflag, RANGE_FLG_NULL))));

    if ((!FILTER_GET_FLAG(ra->sflag, RANGE_FLG_NULL)) && (!FILTER_GET_FLAG(ra->eflag, RANGE_FLG_NULL))) {
      __compar_fn_t func = getComparFunc(type, 0);
      if (func(&ra->s, &ra->e) == 0) {
        void *data = malloc(sizeof(int64_t));
        SIMPLE_COPY_VALUES(data, &ra->s);
        filterAddField(dst, NULL, &data, FLD_TYPE_VALUE, &right, tDataTypes[type].bytes, true);
D
dapan1121 已提交
1152
        filterAddUnit(dst, OP_TYPE_EQUAL, &left, &right, &uidx);
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
        filterAddUnitToGroup(g, uidx);
        return TSDB_CODE_SUCCESS;              
      } else {
        void *data = malloc(sizeof(int64_t));
        SIMPLE_COPY_VALUES(data, &ra->s);
        filterAddField(dst, NULL, &data, FLD_TYPE_VALUE, &right, tDataTypes[type].bytes, true);
        void *data2 = malloc(sizeof(int64_t));
        SIMPLE_COPY_VALUES(data2, &ra->e);
        filterAddField(dst, NULL, &data2, FLD_TYPE_VALUE, &right2, tDataTypes[type].bytes, true);
        
D
dapan1121 已提交
1163 1164
        filterAddUnit(dst, FILTER_GET_FLAG(ra->sflag, RANGE_FLG_EXCLUDE) ? OP_TYPE_GREATER_THAN : OP_TYPE_GREATER_EQUAL, &left, &right, &uidx);
        filterAddUnitRight(dst, FILTER_GET_FLAG(ra->eflag, RANGE_FLG_EXCLUDE) ? OP_TYPE_LOWER_THAN : OP_TYPE_LOWER_EQUAL, &right2, uidx);
1165 1166 1167 1168 1169 1170 1171 1172 1173
        filterAddUnitToGroup(g, uidx);
        return TSDB_CODE_SUCCESS;              
      }
    }
    
    if (!FILTER_GET_FLAG(ra->sflag, RANGE_FLG_NULL)) {
      void *data = malloc(sizeof(int64_t));
      SIMPLE_COPY_VALUES(data, &ra->s);
      filterAddField(dst, NULL, &data, FLD_TYPE_VALUE, &right, tDataTypes[type].bytes, true);
D
dapan1121 已提交
1174
      filterAddUnit(dst, FILTER_GET_FLAG(ra->sflag, RANGE_FLG_EXCLUDE) ? OP_TYPE_GREATER_THAN : OP_TYPE_GREATER_EQUAL, &left, &right, &uidx);
1175 1176 1177 1178 1179 1180 1181
      filterAddUnitToGroup(g, uidx);
    }

    if (!FILTER_GET_FLAG(ra->eflag, RANGE_FLG_NULL)) {
      void *data = malloc(sizeof(int64_t));
      SIMPLE_COPY_VALUES(data, &ra->e);
      filterAddField(dst, NULL, &data, FLD_TYPE_VALUE, &right, tDataTypes[type].bytes, true);
D
dapan1121 已提交
1182
      filterAddUnit(dst, FILTER_GET_FLAG(ra->eflag, RANGE_FLG_EXCLUDE) ? OP_TYPE_LOWER_THAN : OP_TYPE_LOWER_EQUAL, &left, &right, &uidx);
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
      filterAddUnitToGroup(g, uidx);
    }    

    return TSDB_CODE_SUCCESS;      
  } 

  // OR PROCESS
  
  SFilterGroup ng = {0};
  g = &ng;

  assert(ctx->isnull || ctx->notnull || ctx->isrange);
  
  if (ctx->isnull) {
D
dapan1121 已提交
1197
    filterAddUnit(dst, OP_TYPE_IS_NULL, &left, NULL, &uidx);
1198 1199 1200 1201 1202 1203 1204 1205
    filterAddUnitToGroup(g, uidx);    
    taosArrayPush(res, g);
  }
  
  if (ctx->notnull) {
    assert(!ctx->isrange);
    memset(g, 0, sizeof(*g));
    
D
dapan1121 已提交
1206
    filterAddUnit(dst, OP_TYPE_IS_NOT_NULL, &left, NULL, &uidx);
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
    filterAddUnitToGroup(g, uidx);
    taosArrayPush(res, g);
  }

  if (!ctx->isrange) {
    assert(ctx->isnull || ctx->notnull);
    g->unitNum = 0;
    return TSDB_CODE_SUCCESS;
  }

  SFilterRangeNode *r = ctx->rs;
  
  while (r) {
    memset(g, 0, sizeof(*g));

    if ((!FILTER_GET_FLAG(r->ra.sflag, RANGE_FLG_NULL)) &&(!FILTER_GET_FLAG(r->ra.eflag, RANGE_FLG_NULL))) {
      __compar_fn_t func = getComparFunc(type, 0);
      if (func(&r->ra.s, &r->ra.e) == 0) {
        void *data = malloc(sizeof(int64_t));
        SIMPLE_COPY_VALUES(data, &r->ra.s);
        filterAddField(dst, NULL, &data, FLD_TYPE_VALUE, &right, tDataTypes[type].bytes, true);
D
dapan1121 已提交
1228
        filterAddUnit(dst, OP_TYPE_EQUAL, &left, &right, &uidx);
1229 1230 1231 1232 1233 1234 1235 1236 1237
        filterAddUnitToGroup(g, uidx);
      } else {
        void *data = malloc(sizeof(int64_t));
        SIMPLE_COPY_VALUES(data, &r->ra.s);
        filterAddField(dst, NULL, &data, FLD_TYPE_VALUE, &right, tDataTypes[type].bytes, true);
        void *data2 = malloc(sizeof(int64_t));
        SIMPLE_COPY_VALUES(data2, &r->ra.e);
        filterAddField(dst, NULL, &data2, FLD_TYPE_VALUE, &right2, tDataTypes[type].bytes, true);
        
D
dapan1121 已提交
1238 1239
        filterAddUnit(dst, FILTER_GET_FLAG(r->ra.sflag, RANGE_FLG_EXCLUDE) ? OP_TYPE_GREATER_THAN : OP_TYPE_GREATER_EQUAL, &left, &right, &uidx);
        filterAddUnitRight(dst, FILTER_GET_FLAG(r->ra.eflag, RANGE_FLG_EXCLUDE) ? OP_TYPE_LOWER_THAN : OP_TYPE_LOWER_EQUAL, &right2, uidx);
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
        filterAddUnitToGroup(g, uidx);
      }

      taosArrayPush(res, g);
      
      r = r->next;
      
      continue;
    }
    
    if (!FILTER_GET_FLAG(r->ra.sflag, RANGE_FLG_NULL)) {
      void *data = malloc(sizeof(int64_t));
      SIMPLE_COPY_VALUES(data, &r->ra.s);
      filterAddField(dst, NULL, &data, FLD_TYPE_VALUE, &right, tDataTypes[type].bytes, true);
D
dapan1121 已提交
1254
      filterAddUnit(dst, FILTER_GET_FLAG(r->ra.sflag, RANGE_FLG_EXCLUDE) ? OP_TYPE_GREATER_THAN : OP_TYPE_GREATER_EQUAL, &left, &right, &uidx);
1255 1256 1257 1258 1259 1260 1261
      filterAddUnitToGroup(g, uidx);
    }
    
    if (!FILTER_GET_FLAG(r->ra.eflag, RANGE_FLG_NULL)) {
      void *data = malloc(sizeof(int64_t));
      SIMPLE_COPY_VALUES(data, &r->ra.e);    
      filterAddField(dst, NULL, &data, FLD_TYPE_VALUE, &right, tDataTypes[type].bytes, true);
D
dapan1121 已提交
1262
      filterAddUnit(dst, FILTER_GET_FLAG(r->ra.eflag, RANGE_FLG_EXCLUDE) ? OP_TYPE_LOWER_THAN : OP_TYPE_LOWER_EQUAL, &left, &right, &uidx);
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
      filterAddUnitToGroup(g, uidx);
    }

    assert (g->unitNum > 0);

    taosArrayPush(res, g);

    r = r->next;
  }

  g->unitNum = 0;

  return TSDB_CODE_SUCCESS;
}


static void filterFreeGroup(void *pItem) {
  if (pItem == NULL) {
    return;
  }
  
  SFilterGroup* p = (SFilterGroup*) pItem;
  tfree(p->unitIdxs);
  tfree(p->unitFlags);
}


D
dapan1121 已提交
1290
EDealRes fltTreeToGroup(SNode* pNode, void* pContext) {
1291
  int32_t code = TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1292 1293 1294
  SArray* preGroup = NULL;
  SArray* newGroup = NULL;
  SArray* resGroup = NULL;
D
dapan1121 已提交
1295
  ENodeType nType = nodeType(pNode);
D
dapan1121 已提交
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
  SFltBuildGroupCtx *ctx = (SFltBuildGroupCtx *)pContext;

  if (QUERY_NODE_LOGIC_CONDITION == nodeType(pNode)) {
    SLogicConditionNode *node = (SLogicConditionNode *)pNode;
    if (LOGIC_COND_TYPE_AND == node->condType) {
      SListCell *cell = node->pParameterList->pHead;
      for (int32_t i = 0; i < node->pParameterList->length; ++i) {
        newGroup = taosArrayInit(4, sizeof(SFilterGroup));
        resGroup = taosArrayInit(4, sizeof(SFilterGroup));
        
        SFltBuildGroupCtx tctx = {.info = ctx->info, .group = newGroup};
        nodesWalkNode(cell->pNode, fltTreeToGroup, (void *)&tctx);
        FLT_ERR_JRET(tctx.code);
        
        FLT_ERR_JRET(filterDetachCnfGroups(resGroup, preGroup, newGroup));
        
        taosArrayDestroyEx(newGroup, filterFreeGroup);
D
dapan1121 已提交
1313
        newGroup = NULL;
D
dapan1121 已提交
1314 1315 1316
        taosArrayDestroyEx(preGroup, filterFreeGroup);
        
        preGroup = resGroup;
D
dapan1121 已提交
1317
        resGroup = NULL;
D
dapan1121 已提交
1318 1319

        cell = cell->pNext;
D
dapan1121 已提交
1320
      }
1321

D
dapan1121 已提交
1322
      taosArrayAddAll(ctx->group, preGroup);
1323

D
dapan1121 已提交
1324
      taosArrayDestroy(preGroup);
D
dapan1121 已提交
1325 1326 1327 1328 1329 1330 1331 1332
      
      return DEAL_RES_IGNORE_CHILD;
    }

    if (LOGIC_COND_TYPE_OR == node->condType) {
      SListCell *cell = node->pParameterList->pHead;
      for (int32_t i = 0; i < node->pParameterList->length; ++i) {
        nodesWalkNode(cell->pNode, fltTreeToGroup, (void *)pContext);
D
dapan1121 已提交
1333
        FLT_ERR_JRET(ctx->code);
D
dapan1121 已提交
1334 1335
        
        cell = cell->pNext;
D
dapan1121 已提交
1336 1337 1338 1339
      }
      
      return DEAL_RES_IGNORE_CHILD;
    }
1340

D
dapan1121 已提交
1341 1342
    ctx->code = TSDB_CODE_QRY_APP_ERROR;
    
D
dapan1121 已提交
1343
    fltError("invalid condition type, type:%d", node->condType);
1344

D
dapan1121 已提交
1345
    return DEAL_RES_ERROR;
1346 1347
  }

D
dapan1121 已提交
1348 1349 1350
  if (QUERY_NODE_OPERATOR == nType) {
    FLT_ERR_JRET(fltAddGroupUnitFromNode(ctx->info, pNode, ctx->group));  
    
D
dapan1121 已提交
1351
    return DEAL_RES_IGNORE_CHILD;
D
dapan1121 已提交
1352
  }
1353

D
dapan1121 已提交
1354
  fltError("invalid node type for filter, type:%d", nodeType(pNode));
D
dapan1121 已提交
1355 1356

  code = TSDB_CODE_QRY_INVALID_INPUT;
1357 1358 1359

_return:

D
dapan1121 已提交
1360 1361 1362
  taosArrayDestroyEx(newGroup, filterFreeGroup);
  taosArrayDestroyEx(preGroup, filterFreeGroup);
  taosArrayDestroyEx(resGroup, filterFreeGroup);
1363

D
dapan1121 已提交
1364
  ctx->code = code;
1365

D
dapan1121 已提交
1366
  return DEAL_RES_ERROR;
1367 1368
}

D
dapan1121 已提交
1369
int32_t fltConverToStr(char *str, int type, void *buf, int32_t bufSize, int32_t *len) {
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
  int32_t n = 0;

  switch (type) {
    case TSDB_DATA_TYPE_NULL:
      n = sprintf(str, "null");
      break;

    case TSDB_DATA_TYPE_BOOL:
      n = sprintf(str, (*(int8_t*)buf) ? "true" : "false");
      break;

    case TSDB_DATA_TYPE_TINYINT:
      n = sprintf(str, "%d", *(int8_t*)buf);
      break;

    case TSDB_DATA_TYPE_SMALLINT:
      n = sprintf(str, "%d", *(int16_t*)buf);
      break;

    case TSDB_DATA_TYPE_INT:
      n = sprintf(str, "%d", *(int32_t*)buf);
      break;

    case TSDB_DATA_TYPE_BIGINT:
    case TSDB_DATA_TYPE_TIMESTAMP:
      n = sprintf(str, "%" PRId64, *(int64_t*)buf);
      break;

    case TSDB_DATA_TYPE_FLOAT:
      n = sprintf(str, "%e", GET_FLOAT_VAL(buf));
      break;

    case TSDB_DATA_TYPE_DOUBLE:
      n = sprintf(str, "%e", GET_DOUBLE_VAL(buf));
      break;

    case TSDB_DATA_TYPE_BINARY:
    case TSDB_DATA_TYPE_NCHAR:
      if (bufSize < 0) {
//        tscError("invalid buf size");
        return TSDB_CODE_TSC_INVALID_VALUE;
      }

      *str = '"';
      memcpy(str + 1, buf, bufSize);
      *(str + bufSize + 1) = '"';
      n = bufSize + 2;
      break;

    case TSDB_DATA_TYPE_UTINYINT:
      n = sprintf(str, "%d", *(uint8_t*)buf);
      break;

    case TSDB_DATA_TYPE_USMALLINT:
      n = sprintf(str, "%d", *(uint16_t*)buf);
      break;

    case TSDB_DATA_TYPE_UINT:
      n = sprintf(str, "%u", *(uint32_t*)buf);
      break;

    case TSDB_DATA_TYPE_UBIGINT:
      n = sprintf(str, "%" PRIu64, *(uint64_t*)buf);
      break;

    default:
//      tscError("unsupported type:%d", type);
      return TSDB_CODE_TSC_INVALID_VALUE;
  }

  *len = n;

  return TSDB_CODE_SUCCESS;
}

void filterDumpInfoToString(SFilterInfo *info, const char *msg, int32_t options) {
  if (qDebugFlag & DEBUG_DEBUG) {
D
dapan1121 已提交
1447 1448 1449 1450
    if (info == NULL) {
      fltDebug("%s - FilterInfo: EMPTY", msg);
      return;
    }
1451 1452

    if (options == 0) {
D
dapan1121 已提交
1453 1454 1455
      qDebug("%s - FilterInfo:", msg);
      qDebug("COLUMN Field Num:%u", info->fields[FLD_TYPE_COLUMN].num);
      for (uint32_t i = 0; i < info->fields[FLD_TYPE_COLUMN].num; ++i) {
1456
        SFilterField *field = &info->fields[FLD_TYPE_COLUMN].fields[i];
X
Xiaoyu Wang 已提交
1457
        SColumnNode *refNode = (SColumnNode *)field->desc;
X
Xiaoyu Wang 已提交
1458
        qDebug("COL%d => [%d][%d]", i, refNode->dataBlockId, refNode->slotId);
1459 1460
      }

D
dapan1121 已提交
1461 1462
      qDebug("VALUE Field Num:%u", info->fields[FLD_TYPE_VALUE].num);
      for (uint32_t i = 0; i < info->fields[FLD_TYPE_VALUE].num; ++i) {
1463 1464
        SFilterField *field = &info->fields[FLD_TYPE_VALUE].fields[i];
        if (field->desc) {
D
dapan1121 已提交
1465 1466 1467
          SValueNode *var = (SValueNode *)field->desc;
          SDataType *dType = &var->node.resType;
          if (dType->type == TSDB_DATA_TYPE_VALUE_ARRAY) {
D
dapan1121 已提交
1468
            qDebug("VAL%d => [type:TS][val:[%" PRIi64"] - [%" PRId64 "]]", i, *(int64_t *)field->data, *(((int64_t *)field->data) + 1)); 
1469
          } else {
D
dapan1121 已提交
1470
            qDebug("VAL%d => [type:%d][val:%" PRIx64"]", i, dType->type, var->datum.i); //TODO
1471 1472
          }
        } else if (field->data) {
D
dapan1121 已提交
1473
          qDebug("VAL%d => [type:NIL][val:NIL]", i); //TODO
1474 1475 1476
        }
      }

D
dapan1121 已提交
1477 1478
      qDebug("UNIT  Num:%u", info->unitNum);
      for (uint32_t i = 0; i < info->unitNum; ++i) {
1479 1480 1481 1482 1483 1484 1485
        SFilterUnit *unit = &info->units[i];
        int32_t type = FILTER_UNIT_DATA_TYPE(unit);
        int32_t len = 0;
        int32_t tlen = 0;
        char str[512] = {0};
        
        SFilterField *left = FILTER_UNIT_LEFT_FIELD(info, unit);
X
Xiaoyu Wang 已提交
1486
        SColumnNode *refNode = (SColumnNode *)left->desc;
D
dapan1121 已提交
1487
        if (unit->compare.optr >= 0 && unit->compare.optr <= OP_TYPE_JSON_CONTAINS){
X
Xiaoyu Wang 已提交
1488
          len = sprintf(str, "UNIT[%d] => [%d][%d]  %s  [", i, refNode->dataBlockId, refNode->slotId, gOptrStr[unit->compare.optr].str);
D
dapan1121 已提交
1489
        }
1490

D
dapan1121 已提交
1491
        if (unit->right.type == FLD_TYPE_VALUE && FILTER_UNIT_OPTR(unit) != OP_TYPE_IN) {
1492 1493 1494 1495 1496 1497
          SFilterField *right = FILTER_UNIT_RIGHT_FIELD(info, unit);
          char *data = right->data;
          if (IS_VAR_DATA_TYPE(type)) {
            tlen = varDataLen(data);
            data += VARSTR_HEADER_SIZE;
          }
D
dapan1121 已提交
1498
          if (data) fltConverToStr(str + len, type, data, tlen > 32 ? 32 : tlen, &tlen);
1499 1500 1501 1502 1503 1504 1505
        } else {
          strcat(str, "NULL");
        }
        strcat(str, "]");

        if (unit->compare.optr2) {
          strcat(str, " && ");
D
dapan1121 已提交
1506
          if (unit->compare.optr2 >= 0 && unit->compare.optr2 <= OP_TYPE_JSON_CONTAINS){
X
Xiaoyu Wang 已提交
1507
            sprintf(str + strlen(str), "[%d][%d]  %s  [", refNode->dataBlockId, refNode->slotId, gOptrStr[unit->compare.optr2].str);
D
dapan1121 已提交
1508
          }
1509
          
D
dapan1121 已提交
1510
          if (unit->right2.type == FLD_TYPE_VALUE && FILTER_UNIT_OPTR(unit) != OP_TYPE_IN) {
1511 1512 1513 1514 1515 1516
            SFilterField *right = FILTER_UNIT_RIGHT2_FIELD(info, unit);
            char *data = right->data;
            if (IS_VAR_DATA_TYPE(type)) {
              tlen = varDataLen(data);
              data += VARSTR_HEADER_SIZE;
            }
D
dapan1121 已提交
1517
            fltConverToStr(str + strlen(str), type, data, tlen > 32 ? 32 : tlen, &tlen);
1518 1519 1520 1521 1522 1523
          } else {
            strcat(str, "NULL");
          }
          strcat(str, "]");
        }
        
D
dapan1121 已提交
1524
        qDebug("%s", str); //TODO
1525 1526
      }

D
dapan1121 已提交
1527 1528
      qDebug("GROUP Num:%u", info->groupNum);
      for (uint32_t i = 0; i < info->groupNum; ++i) {
1529
        SFilterGroup *group = &info->groups[i];
D
dapan1121 已提交
1530
        qDebug("Group%d : unit num[%u]", i, group->unitNum);
1531

D
dapan1121 已提交
1532 1533
        for (uint32_t u = 0; u < group->unitNum; ++u) {
          qDebug("unit id:%u", group->unitIdxs[u]);
1534 1535 1536 1537 1538 1539 1540
        }
      }

      return;
    }

    if (options == 1) {
D
dapan1121 已提交
1541
      qDebug("%s - RANGE info:", msg);
1542

D
dapan1121 已提交
1543 1544
      qDebug("RANGE Num:%u", info->colRangeNum);
      for (uint32_t i = 0; i < info->colRangeNum; ++i) {
1545
        SFilterRangeCtx *ctx = info->colRange[i];
D
dapan1121 已提交
1546
        qDebug("Column ID[%d] RANGE: isnull[%d],notnull[%d],range[%d]", ctx->colId, ctx->isnull, ctx->notnull, ctx->isrange);
1547 1548 1549 1550 1551 1552 1553 1554 1555
        if (ctx->isrange) {      
          SFilterRangeNode *r = ctx->rs;
          while (r) {
            char str[256] = {0};        
            int32_t tlen = 0;
            if (FILTER_GET_FLAG(r->ra.sflag, RANGE_FLG_NULL)) {
              strcat(str,"(NULL)");
            } else {
              FILTER_GET_FLAG(r->ra.sflag, RANGE_FLG_EXCLUDE) ? strcat(str,"(") : strcat(str,"[");
D
dapan1121 已提交
1556
              fltConverToStr(str + strlen(str), ctx->type, &r->ra.s, tlen > 32 ? 32 : tlen, &tlen);
1557 1558 1559 1560 1561 1562 1563
              FILTER_GET_FLAG(r->ra.sflag, RANGE_FLG_EXCLUDE) ? strcat(str,")") : strcat(str,"]");
            }
            strcat(str, " - ");
            if (FILTER_GET_FLAG(r->ra.eflag, RANGE_FLG_NULL)) {
              strcat(str, "(NULL)");
            } else {
              FILTER_GET_FLAG(r->ra.eflag, RANGE_FLG_EXCLUDE) ? strcat(str,"(") : strcat(str,"[");
D
dapan1121 已提交
1564
              fltConverToStr(str + strlen(str), ctx->type, &r->ra.e, tlen > 32 ? 32 : tlen, &tlen);
1565 1566
              FILTER_GET_FLAG(r->ra.eflag, RANGE_FLG_EXCLUDE) ? strcat(str,")") : strcat(str,"]");
            }
D
dapan1121 已提交
1567
            qDebug("range: %s", str);        
1568 1569 1570 1571 1572 1573 1574 1575 1576
            
            r = r->next;
          }
        }
      }

      return;
    }

D
dapan1121 已提交
1577
    qDebug("%s - Block Filter info:", msg);
1578 1579

    if (FILTER_GET_FLAG(info->blkFlag, FI_STATUS_BLK_ALL)) {
D
dapan1121 已提交
1580
      qDebug("Flag:%s", "ALL");
1581 1582
      return;
    } else if (FILTER_GET_FLAG(info->blkFlag, FI_STATUS_BLK_EMPTY)) {
D
dapan1121 已提交
1583
      qDebug("Flag:%s", "EMPTY");
1584 1585
      return;
    } else if (FILTER_GET_FLAG(info->blkFlag, FI_STATUS_BLK_ACTIVE)){
D
dapan1121 已提交
1586
      qDebug("Flag:%s", "ACTIVE");
1587 1588
    }

D
dapan1121 已提交
1589 1590 1591 1592 1593 1594 1595
    qDebug("GroupNum:%d", info->blkGroupNum);
    uint32_t *unitIdx = info->blkUnits;
    for (uint32_t i = 0; i < info->blkGroupNum; ++i) {
      qDebug("Group[%d] UnitNum: %d:", i, *unitIdx);
      uint32_t unitNum = *(unitIdx++);
      for (uint32_t m = 0; m < unitNum; ++m) {
        qDebug("uidx[%d]", *(unitIdx++));
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
      }
    }
  }
}

void filterFreeColInfo(void *data) {
  SFilterColInfo* info = (SFilterColInfo *)data;

  if (info->info == NULL) {
    return;
  }

  if (info->type == RANGE_TYPE_VAR_HASH) {
    //TODO
  } else if (info->type == RANGE_TYPE_MR_CTX) {
    filterFreeRangeCtx(info->info);  
  } else if (info->type == RANGE_TYPE_UNIT) {
    taosArrayDestroy((SArray *)info->info);
  }

  //NO NEED TO FREE UNIT

  info->type = 0;
  info->info = NULL;
}

void filterFreeColCtx(void *data) {
  SFilterColCtx* ctx = (SFilterColCtx *)data;

  if (ctx->ctx) {
    filterFreeRangeCtx(ctx->ctx);
  }
}


void filterFreeGroupCtx(SFilterGroupCtx* gRes) {
  if (gRes == NULL) {
    return;
  }

  tfree(gRes->colIdx);

  int16_t i = 0, j = 0;

  while (i < gRes->colNum) {
    if (gRes->colInfo[j].info) {
      filterFreeColInfo(&gRes->colInfo[j]);
      ++i;
    }

    ++j;
  }

  tfree(gRes->colInfo);
  tfree(gRes);
}

void filterFreeField(SFilterField* field, int32_t type) {
  if (field == NULL) {
    return;
  }

  if (!FILTER_GET_FLAG(field->flag, FLD_DATA_NO_FREE)) {
    if (FILTER_GET_FLAG(field->flag, FLD_DATA_IS_HASH)) {
      taosHashCleanup(field->data);
    } else {
      tfree(field->data);
    }
  }
}

void filterFreePCtx(SFilterPCtx *pctx) {
  taosHashCleanup(pctx->valHash);
  taosHashCleanup(pctx->unitHash);
}

void filterFreeInfo(SFilterInfo *info) {
D
dapan1121 已提交
1673 1674 1675
  if (info == NULL) {
    return;
  }
1676 1677 1678 1679 1680 1681

  tfree(info->cunits);
  tfree(info->blkUnitRes);
  tfree(info->blkUnits);
  
  for (int32_t i = 0; i < FLD_TYPE_MAX; ++i) {
D
dapan1121 已提交
1682
    for (uint32_t f = 0; f < info->fields[i].num; ++f) {
1683 1684 1685 1686 1687 1688
      filterFreeField(&info->fields[i].fields[f], i);
    }
    
    tfree(info->fields[i].fields);
  }

D
dapan1121 已提交
1689
  for (uint32_t i = 0; i < info->groupNum; ++i) {
1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700
    filterFreeGroup(&info->groups[i]);    
  }
  
  tfree(info->groups);

  tfree(info->units);

  tfree(info->unitRes);

  tfree(info->unitFlags);

D
dapan1121 已提交
1701
  for (uint32_t i = 0; i < info->colRangeNum; ++i) {
1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718
    filterFreeRangeCtx(info->colRange[i]);
  }

  tfree(info->colRange);

  filterFreePCtx(&info->pctx);

  if (!FILTER_GET_FLAG(info->status, FI_STATUS_CLONED)) {
    tfree(info);
  }
}

int32_t filterHandleValueExtInfo(SFilterUnit* unit, char extInfo) {
  assert(extInfo > 0 || extInfo < 0);
  
  uint8_t optr = FILTER_UNIT_OPTR(unit);
  switch (optr) {
D
dapan1121 已提交
1719 1720 1721
    case OP_TYPE_GREATER_THAN:
    case OP_TYPE_GREATER_EQUAL:
      unit->compare.optr = (extInfo > 0) ? FILTER_DUMMY_EMPTY_OPTR : OP_TYPE_IS_NOT_NULL;
1722
      break;
D
dapan1121 已提交
1723 1724 1725
    case OP_TYPE_LOWER_THAN:
    case OP_TYPE_LOWER_EQUAL:
      unit->compare.optr = (extInfo > 0) ? OP_TYPE_IS_NOT_NULL : FILTER_DUMMY_EMPTY_OPTR;
1726
      break;
D
dapan1121 已提交
1727
    case OP_TYPE_EQUAL:
1728 1729 1730 1731 1732 1733 1734 1735 1736 1737
      unit->compare.optr = FILTER_DUMMY_EMPTY_OPTR;
      break;
    default:
      assert(0);
  }

  return TSDB_CODE_SUCCESS;
}


D
dapan1121 已提交
1738
int32_t fltInitValFieldData(SFilterInfo *info) {
D
dapan1121 已提交
1739
  for (uint32_t i = 0; i < info->unitNum; ++i) {
1740 1741
    SFilterUnit* unit = &info->units[i];
    if (unit->right.type != FLD_TYPE_VALUE) {
D
dapan1121 已提交
1742
      assert(unit->compare.optr == FILTER_DUMMY_EMPTY_OPTR || scalarGetOperatorParamNum(unit->compare.optr) == 1);
1743 1744 1745 1746 1747 1748 1749 1750 1751 1752
      continue;
    }
    
    SFilterField* right = FILTER_UNIT_RIGHT_FIELD(info, unit);

    assert(FILTER_GET_FLAG(right->flag, FLD_TYPE_VALUE));

    uint32_t type = FILTER_UNIT_DATA_TYPE(unit);
    SFilterField* fi = right;
    
D
dapan1121 已提交
1753
    SValueNode* var = (SValueNode *)fi->desc;
1754 1755 1756 1757 1758 1759

    if (var == NULL) {
      assert(fi->data != NULL);
      continue;
    }

D
dapan1121 已提交
1760
    if (unit->compare.optr == OP_TYPE_IN) {
D
dapan1121 已提交
1761
      FLT_ERR_RET(scalarGenerateSetFromList((void **)&fi->data, fi->desc, type));
D
dapan1121 已提交
1762 1763 1764 1765
      if (fi->data == NULL) {
        fltError("failed to convert in param");
        FLT_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
      }
1766 1767 1768 1769 1770 1771

      FILTER_SET_FLAG(fi->flag, FLD_DATA_IS_HASH);
      
      continue;
    }

D
dapan1121 已提交
1772 1773
    SDataType *dType = &var->node.resType;

1774
    if (type == TSDB_DATA_TYPE_BINARY) {
D
dapan1121 已提交
1775
      size_t len = (dType->type == TSDB_DATA_TYPE_BINARY || dType->type == TSDB_DATA_TYPE_NCHAR) ? dType->bytes : MAX_NUM_STR_SIZE;
1776 1777
      fi->data = calloc(1, len + 1 + VARSTR_HEADER_SIZE);
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
D
dapan1121 已提交
1778
      size_t len = (dType->type == TSDB_DATA_TYPE_BINARY || dType->type == TSDB_DATA_TYPE_NCHAR) ? dType->bytes : MAX_NUM_STR_SIZE;    
1779
      fi->data = calloc(1, (len + 1) * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE);
D
dapan1121 已提交
1780
    } else if (type != TSDB_DATA_TYPE_JSON){
D
dapan1121 已提交
1781 1782 1783 1784
      if (dType->type == TSDB_DATA_TYPE_VALUE_ARRAY) {  //TIME RANGE
/*      
        fi->data = calloc(dType->bytes, tDataTypes[type].bytes);
        for (int32_t a = 0; a < dType->bytes; ++a) {
1785 1786 1787
          int64_t *v = taosArrayGet(var->arr, a);
          assignVal((char *)fi->data + a * tDataTypes[type].bytes, (char *)v, 0, type);
        }
D
dapan1121 已提交
1788
*/
1789 1790 1791 1792
        continue;
      } else {
        fi->data = calloc(1, sizeof(int64_t));
      }
D
dapan1121 已提交
1793 1794 1795 1796 1797
    } else{     // type == TSDB_DATA_TYPE_JSON
      // fi->data = null;  use fi->desc as data, because json value is variable, so use tVariant (fi->desc)
    }

    if(type != TSDB_DATA_TYPE_JSON){
D
dapan1121 已提交
1798 1799 1800 1801 1802 1803 1804 1805 1806
      if (dType->type == type) {
        assignVal(fi->data, nodesGetValueFromNode(var), dType->bytes, type);
      } else {
        SScalarParam in = {.data = nodesGetValueFromNode(var), .num = 1, .type = dType->type, .bytes = dType->bytes};
        SScalarParam out = {.data = fi->data, .num = 1, .type = type};
        if (vectorConvertImpl(&in, &out)) {
          qError("convert value to type[%d] failed", type);
          return TSDB_CODE_TSC_INVALID_OPERATION;
        }
D
dapan1121 已提交
1807 1808 1809 1810 1811
      }
    }

    // match/nmatch for nchar type need convert from ucs4 to mbs
    if(type == TSDB_DATA_TYPE_NCHAR &&
D
dapan1121 已提交
1812
        (unit->compare.optr == OP_TYPE_MATCH || unit->compare.optr == OP_TYPE_NMATCH)){
D
dapan1121 已提交
1813 1814 1815 1816
      char newValData[TSDB_REGEX_STRING_DEFAULT_LEN * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE] = {0};
      int32_t len = taosUcs4ToMbs(varDataVal(fi->data), varDataLen(fi->data), varDataVal(newValData));
      if (len < 0){
        qError("filterInitValFieldData taosUcs4ToMbs error 1");
D
dapan1121 已提交
1817
        return TSDB_CODE_QRY_APP_ERROR;
D
dapan1121 已提交
1818 1819 1820
      }
      varDataSetLen(newValData, len);
      varDataCopy(fi->data, newValData);
1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831
    }
  }

  return TSDB_CODE_SUCCESS;
}


bool filterDoCompare(__compar_fn_t func, uint8_t optr, void *left, void *right) {
  int32_t ret = func(left, right);

  switch (optr) {
D
dapan1121 已提交
1832
    case OP_TYPE_EQUAL: {
1833 1834
      return ret == 0;
    }
D
dapan1121 已提交
1835
    case OP_TYPE_NOT_EQUAL: {
1836 1837
      return ret != 0;
    }
D
dapan1121 已提交
1838
    case OP_TYPE_GREATER_EQUAL: {
1839 1840
      return ret >= 0;
    }
D
dapan1121 已提交
1841
    case OP_TYPE_GREATER_THAN: {
1842 1843
      return ret > 0;
    }
D
dapan1121 已提交
1844
    case OP_TYPE_LOWER_EQUAL: {
1845 1846
      return ret <= 0;
    }
D
dapan1121 已提交
1847
    case OP_TYPE_LOWER_THAN: {
1848 1849
      return ret < 0;
    }
D
dapan1121 已提交
1850
    case OP_TYPE_LIKE: {
1851 1852
      return ret == 0;
    }
D
dapan1121 已提交
1853
    case OP_TYPE_NOT_LIKE: {
1854 1855
      return ret == 0;
    }
D
dapan1121 已提交
1856
    case OP_TYPE_MATCH: {
1857 1858
      return ret == 0;
    }
D
dapan1121 已提交
1859
    case OP_TYPE_NMATCH: {
1860 1861
      return ret == 0;
    }
D
dapan1121 已提交
1862
    case OP_TYPE_IN: {
1863 1864
      return ret == 1;
    }
D
dapan1121 已提交
1865
    case OP_TYPE_NOT_IN: {
1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884
      return ret == 1;
    }

    default:
      assert(false);
  }

  return true;
}


int32_t filterAddUnitRange(SFilterInfo *info, SFilterUnit* u, SFilterRangeCtx *ctx, int32_t optr) {
  int32_t type = FILTER_UNIT_DATA_TYPE(u);
  uint8_t uoptr = FILTER_UNIT_OPTR(u);
  void *val = FILTER_UNIT_VAL_DATA(info, u);
  SFilterRange ra = {0};
  int64_t tmp = 0;

  switch (uoptr) {
D
dapan1121 已提交
1885
    case OP_TYPE_GREATER_THAN:
1886 1887 1888 1889
      SIMPLE_COPY_VALUES(&ra.s, val);
      FILTER_SET_FLAG(ra.sflag, RANGE_FLG_EXCLUDE);
      FILTER_SET_FLAG(ra.eflag, RANGE_FLG_NULL);
      break;
D
dapan1121 已提交
1890
    case OP_TYPE_GREATER_EQUAL:
1891 1892 1893
      SIMPLE_COPY_VALUES(&ra.s, val);
      FILTER_SET_FLAG(ra.eflag, RANGE_FLG_NULL);
      break;
D
dapan1121 已提交
1894
    case OP_TYPE_LOWER_THAN:
1895 1896 1897 1898
      SIMPLE_COPY_VALUES(&ra.e, val);
      FILTER_SET_FLAG(ra.eflag, RANGE_FLG_EXCLUDE);
      FILTER_SET_FLAG(ra.sflag, RANGE_FLG_NULL);
      break;
D
dapan1121 已提交
1899
    case OP_TYPE_LOWER_EQUAL:
1900 1901 1902
      SIMPLE_COPY_VALUES(&ra.e, val);
      FILTER_SET_FLAG(ra.sflag, RANGE_FLG_NULL);
      break;
D
dapan1121 已提交
1903
    case OP_TYPE_NOT_EQUAL:
1904 1905 1906 1907 1908 1909 1910 1911 1912 1913
      assert(type == TSDB_DATA_TYPE_BOOL);
      if (GET_INT8_VAL(val)) {
        SIMPLE_COPY_VALUES(&ra.s, &tmp);
        SIMPLE_COPY_VALUES(&ra.e, &tmp);        
      } else {
        *(bool *)&tmp = true;
        SIMPLE_COPY_VALUES(&ra.s, &tmp);
        SIMPLE_COPY_VALUES(&ra.e, &tmp);        
      }
      break;
D
dapan1121 已提交
1914
    case OP_TYPE_EQUAL:
1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927
      SIMPLE_COPY_VALUES(&ra.s, val);
      SIMPLE_COPY_VALUES(&ra.e, val);
      break;
    default:
      assert(0);
  }
  
  filterAddRange(ctx, &ra, optr);

  return TSDB_CODE_SUCCESS;
}

int32_t filterCompareRangeCtx(SFilterRangeCtx *ctx1, SFilterRangeCtx *ctx2, bool *equal) {
D
dapan1121 已提交
1928 1929 1930 1931
  FLT_CHK_JMP(ctx1->status != ctx2->status);
  FLT_CHK_JMP(ctx1->isnull != ctx2->isnull);
  FLT_CHK_JMP(ctx1->notnull != ctx2->notnull);
  FLT_CHK_JMP(ctx1->isrange != ctx2->isrange);
1932 1933 1934 1935 1936

  SFilterRangeNode *r1 = ctx1->rs;
  SFilterRangeNode *r2 = ctx2->rs;
  
  while (r1 && r2) {
D
dapan1121 已提交
1937 1938 1939 1940
    FLT_CHK_JMP(r1->ra.sflag != r2->ra.sflag);
    FLT_CHK_JMP(r1->ra.eflag != r2->ra.eflag);
    FLT_CHK_JMP(r1->ra.s != r2->ra.s);
    FLT_CHK_JMP(r1->ra.e != r2->ra.e);
1941 1942 1943 1944 1945
    
    r1 = r1->next;
    r2 = r2->next;
  }

D
dapan1121 已提交
1946
  FLT_CHK_JMP(r1 != r2);
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957

  *equal = true;

  return TSDB_CODE_SUCCESS;

_return:
  *equal = false;
  return TSDB_CODE_SUCCESS;
}


D
dapan1121 已提交
1958
int32_t filterMergeUnits(SFilterInfo *info, SFilterGroupCtx* gRes, uint32_t colIdx, bool *empty) {
1959 1960 1961 1962 1963 1964 1965 1966 1967
  SArray* colArray = (SArray *)gRes->colInfo[colIdx].info;
  int32_t size = (int32_t)taosArrayGetSize(colArray);
  int32_t type = gRes->colInfo[colIdx].dataType;
  SFilterRangeCtx* ctx = filterInitRangeCtx(type, 0);
  
  for (uint32_t i = 0; i < size; ++i) {
    SFilterUnit* u = taosArrayGetP(colArray, i);
    uint8_t optr = FILTER_UNIT_OPTR(u);

D
dapan1121 已提交
1968
    filterAddRangeOptr(ctx, optr, LOGIC_COND_TYPE_AND, empty, NULL);
D
dapan1121 已提交
1969
    FLT_CHK_JMP(*empty);
1970 1971

    if (!FILTER_NO_MERGE_OPTR(optr)) {
D
dapan1121 已提交
1972
      filterAddUnitRange(info, u, ctx, LOGIC_COND_TYPE_AND);
D
dapan1121 已提交
1973
      FLT_CHK_JMP(MR_EMPTY_RES(ctx));
1974
    }
D
dapan1121 已提交
1975 1976
    if(FILTER_UNIT_OPTR(u) == OP_TYPE_EQUAL && !FILTER_NO_MERGE_DATA_TYPE(FILTER_UNIT_DATA_TYPE(u))){
      gRes->colInfo[colIdx].optr = OP_TYPE_EQUAL;
D
dapan1121 已提交
1977 1978
      SIMPLE_COPY_VALUES(&gRes->colInfo[colIdx].value, FILTER_UNIT_VAL_DATA(info, u));
    }
1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998
  }

  taosArrayDestroy(colArray);

  FILTER_PUSH_CTX(gRes->colInfo[colIdx], ctx);

  return TSDB_CODE_SUCCESS;

_return:

  *empty = true;

  filterFreeRangeCtx(ctx);

  return TSDB_CODE_SUCCESS;
}


int32_t filterMergeGroupUnits(SFilterInfo *info, SFilterGroupCtx** gRes, int32_t* gResNum) {
  bool empty = false;
D
dapan1121 已提交
1999 2000 2001
  uint32_t *colIdx = malloc(info->fields[FLD_TYPE_COLUMN].num * sizeof(uint32_t));
  uint32_t colIdxi = 0;
  uint32_t gResIdx = 0;
2002
  
D
dapan1121 已提交
2003
  for (uint32_t i = 0; i < info->groupNum; ++i) {
2004 2005 2006 2007 2008 2009 2010
    SFilterGroup* g = info->groups + i;

    gRes[gResIdx] = calloc(1, sizeof(SFilterGroupCtx));
    gRes[gResIdx]->colInfo = calloc(info->fields[FLD_TYPE_COLUMN].num, sizeof(SFilterColInfo));
    colIdxi = 0;
    empty = false;
    
D
dapan1121 已提交
2011
    for (uint32_t j = 0; j < g->unitNum; ++j) {
2012
      SFilterUnit* u = FILTER_GROUP_UNIT(info, g, j);
D
dapan1121 已提交
2013
      uint32_t cidx = FILTER_UNIT_COL_IDX(u);
2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028

      if (gRes[gResIdx]->colInfo[cidx].info == NULL) {
        gRes[gResIdx]->colInfo[cidx].info = (SArray *)taosArrayInit(4, POINTER_BYTES);
        colIdx[colIdxi++] = cidx;
        ++gRes[gResIdx]->colNum;
      } else {
        if (!FILTER_NO_MERGE_DATA_TYPE(FILTER_UNIT_DATA_TYPE(u))) {
          FILTER_SET_FLAG(info->status, FI_STATUS_REWRITE);
        }
      }
      
      FILTER_PUSH_UNIT(gRes[gResIdx]->colInfo[cidx], u);
    }

    if (colIdxi > 1) {
D
dapan1121 已提交
2029
      qsort(colIdx, colIdxi, sizeof(uint32_t), getComparFunc(TSDB_DATA_TYPE_USMALLINT, 0));
2030 2031
    }

D
dapan1121 已提交
2032
    for (uint32_t l = 0; l < colIdxi; ++l) {
2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070
      int32_t type = gRes[gResIdx]->colInfo[colIdx[l]].dataType;

      if (FILTER_NO_MERGE_DATA_TYPE(type)) {
        continue;
      }

      filterMergeUnits(info, gRes[gResIdx], colIdx[l], &empty);

      if (empty) {
        break;
      }
    }

    if (empty) {
      FILTER_SET_FLAG(info->status, FI_STATUS_REWRITE);
      filterFreeGroupCtx(gRes[gResIdx]);
      gRes[gResIdx] = NULL;
    
      continue;
    }
    
    gRes[gResIdx]->colNum = colIdxi;
    FILTER_COPY_IDX(&gRes[gResIdx]->colIdx, colIdx, colIdxi);
    ++gResIdx;
  }

  tfree(colIdx);

  *gResNum = gResIdx;
  
  if (gResIdx == 0) {
    FILTER_SET_FLAG(info->status, FI_STATUS_EMPTY);
  }

  return TSDB_CODE_SUCCESS;
}

void filterCheckColConflict(SFilterGroupCtx* gRes1, SFilterGroupCtx* gRes2, bool *conflict) {
D
dapan1121 已提交
2071
  uint32_t idx1 = 0, idx2 = 0, m = 0, n = 0;
2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093
  bool equal = false;
  
  for (; m < gRes1->colNum; ++m) {
    idx1 = gRes1->colIdx[m];

    equal = false;
  
    for (; n < gRes2->colNum; ++n) {
      idx2 = gRes2->colIdx[n];
      if (idx1 < idx2) {
        *conflict = true;
        return;
      }

      if (idx1 > idx2) {
        continue;
      }

      if (FILTER_NO_MERGE_DATA_TYPE(gRes1->colInfo[idx1].dataType)) {
        *conflict = true;
        return;
      }
D
dapan1121 已提交
2094 2095

      // for long in operation
D
dapan1121 已提交
2096
      if (gRes1->colInfo[idx1].optr == OP_TYPE_EQUAL && gRes2->colInfo[idx2].optr == OP_TYPE_EQUAL) {
D
dapan1121 已提交
2097 2098 2099 2100 2101 2102
        SFilterRangeCtx* ctx = gRes1->colInfo[idx1].info;
        if (ctx->pCompareFunc(&gRes1->colInfo[idx1].value, &gRes2->colInfo[idx2].value)){
          *conflict = true;
          return;
        }
      }
2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119
      
      ++n;
      equal = true;
      break;
    }

    if (!equal) {
      *conflict = true;
      return;
    }
  }

  *conflict = false;
  return;
}


D
dapan1121 已提交
2120
int32_t filterMergeTwoGroupsImpl(SFilterInfo *info, SFilterRangeCtx **ctx, int32_t optr, uint32_t cidx, SFilterGroupCtx* gRes1, SFilterGroupCtx* gRes2, bool *empty, bool *all) {
2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149
  SFilterField *fi = FILTER_GET_COL_FIELD(info, cidx);
  int32_t type = FILTER_GET_COL_FIELD_TYPE(fi);

  if ((*ctx) == NULL) {
    *ctx = filterInitRangeCtx(type, 0);
  } else {
    filterReuseRangeCtx(*ctx, type, 0);
  }

  assert(gRes2->colInfo[cidx].type == RANGE_TYPE_MR_CTX);
  assert(gRes1->colInfo[cidx].type == RANGE_TYPE_MR_CTX);

  filterCopyRangeCtx(*ctx, gRes2->colInfo[cidx].info);
  filterSourceRangeFromCtx(*ctx, gRes1->colInfo[cidx].info, optr, empty, all);

  return TSDB_CODE_SUCCESS;
}


int32_t filterMergeTwoGroups(SFilterInfo *info, SFilterGroupCtx** gRes1, SFilterGroupCtx** gRes2, bool *all) {
  bool conflict = false;
  
  filterCheckColConflict(*gRes1, *gRes2, &conflict);
  if (conflict) {
    return TSDB_CODE_SUCCESS;
  }

  FILTER_SET_FLAG(info->status, FI_STATUS_REWRITE);

D
dapan1121 已提交
2150
  uint32_t idx1 = 0, idx2 = 0, m = 0, n = 0;
2151 2152
  bool numEqual = (*gRes1)->colNum == (*gRes2)->colNum;
  bool equal = false;
D
dapan1121 已提交
2153
  uint32_t equal1 = 0, equal2 = 0, merNum = 0;
2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171
  SFilterRangeCtx *ctx = NULL;
  SFilterColCtx colCtx = {0};
  SArray* colCtxs = taosArrayInit((*gRes2)->colNum, sizeof(SFilterColCtx));

  for (; m < (*gRes1)->colNum; ++m) {
    idx1 = (*gRes1)->colIdx[m];
  
    for (; n < (*gRes2)->colNum; ++n) {
      idx2 = (*gRes2)->colIdx[n];

      if (idx1 > idx2) {
        continue;
      }

      assert(idx1 == idx2);
      
      ++merNum;
      
D
dapan1121 已提交
2172
      filterMergeTwoGroupsImpl(info, &ctx, LOGIC_COND_TYPE_OR, idx1, *gRes1, *gRes2, NULL, all);
2173

D
dapan1121 已提交
2174
      FLT_CHK_JMP(*all);
2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193

      if (numEqual) {
        if ((*gRes1)->colNum == 1) {
          ++equal1;
          colCtx.colIdx = idx1;
          colCtx.ctx = ctx;
          taosArrayPush(colCtxs, &colCtx);
          break;
        } else {
          filterCompareRangeCtx(ctx, (*gRes1)->colInfo[idx1].info, &equal);
          if (equal) {
            ++equal1;
          }
          
          filterCompareRangeCtx(ctx, (*gRes2)->colInfo[idx2].info, &equal);
          if (equal) {
            ++equal2;
          }

D
dapan1121 已提交
2194
          FLT_CHK_JMP(equal1 != merNum && equal2 != merNum);
2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205
          colCtx.colIdx = idx1;
          colCtx.ctx = ctx;
          ctx = NULL;
          taosArrayPush(colCtxs, &colCtx);
        }
      } else {
        filterCompareRangeCtx(ctx, (*gRes1)->colInfo[idx1].info, &equal);
        if (equal) {
          ++equal1;
        }

D
dapan1121 已提交
2206
        FLT_CHK_JMP(equal1 != merNum);
2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266
        colCtx.colIdx = idx1;
        colCtx.ctx = ctx;        
        ctx = NULL;
        taosArrayPush(colCtxs, &colCtx);
      }

      ++n;
      break;
    }
  }

  assert(merNum > 0);

  SFilterColInfo *colInfo = NULL;
  assert (merNum == equal1 || merNum == equal2);

  filterFreeGroupCtx(*gRes2);
  *gRes2 = NULL;

  assert(colCtxs && taosArrayGetSize(colCtxs) > 0);

  int32_t ctxSize = (int32_t)taosArrayGetSize(colCtxs);
  SFilterColCtx *pctx = NULL;
  
  for (int32_t i = 0; i < ctxSize; ++i) {
    pctx = taosArrayGet(colCtxs, i);
    colInfo = &(*gRes1)->colInfo[pctx->colIdx];
    
    filterFreeColInfo(colInfo);
    FILTER_PUSH_CTX((*gRes1)->colInfo[pctx->colIdx], pctx->ctx);
  }

  taosArrayDestroy(colCtxs);
  
  return TSDB_CODE_SUCCESS;

_return:

  if (colCtxs) {
    if (taosArrayGetSize(colCtxs) > 0) {
      taosArrayDestroyEx(colCtxs, filterFreeColCtx);
    } else {
      taosArrayDestroy(colCtxs);
    }
  }

  filterFreeRangeCtx(ctx);

  return TSDB_CODE_SUCCESS;
}


int32_t filterMergeGroups(SFilterInfo *info, SFilterGroupCtx** gRes, int32_t *gResNum) {
  if (*gResNum <= 1) {
    return TSDB_CODE_SUCCESS;
  }

  qsort(gRes, *gResNum, POINTER_BYTES, filterCompareGroupCtx);

  int32_t pEnd = 0, cStart = 0, cEnd = 0;
D
dapan1121 已提交
2267
  uint32_t pColNum = 0, cColNum = 0; 
2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286
  int32_t movedNum = 0;
  bool all = false;

  cColNum = gRes[0]->colNum;

  for (int32_t i = 1; i <= *gResNum; ++i) {
    if (i < (*gResNum) && gRes[i]->colNum == cColNum) {
      continue;
    }

    cEnd = i - 1;

    movedNum = 0;
    if (pColNum > 0) {
      for (int32_t m = 0; m <= pEnd; ++m) {
        for (int32_t n = cStart; n <= cEnd; ++n) {
          assert(m < n);
          filterMergeTwoGroups(info, &gRes[m], &gRes[n], &all);

D
dapan1121 已提交
2287
          FLT_CHK_JMP(all);
2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307

          if (gRes[n] == NULL) {
            if (n < ((*gResNum) - 1)) {
              memmove(&gRes[n], &gRes[n+1], (*gResNum-n-1) * POINTER_BYTES);
            }
            
            --cEnd;
            --(*gResNum);
            ++movedNum;
            --n;
          }
        }
      }
    }

    for (int32_t m = cStart; m < cEnd; ++m) {
      for (int32_t n = m + 1; n <= cEnd; ++n) {
        assert(m < n);
        filterMergeTwoGroups(info, &gRes[m], &gRes[n], &all);

D
dapan1121 已提交
2308
        FLT_CHK_JMP(all);
2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347
        
        if (gRes[n] == NULL) {
          if (n < ((*gResNum) - 1)) {
            memmove(&gRes[n], &gRes[n+1], (*gResNum-n-1) * POINTER_BYTES);
          }
          
          --cEnd;
          --(*gResNum);
          ++movedNum;
          --n;
        }
      }
    }

    pColNum = cColNum;
    pEnd = cEnd;

    i -= movedNum;

    if (i >= (*gResNum)) {
      break;
    }
    
    cStart = i;
    cColNum = gRes[i]->colNum;    
  }

  return TSDB_CODE_SUCCESS;
  
_return:

  FILTER_SET_FLAG(info->status, FI_STATUS_ALL);

  return TSDB_CODE_SUCCESS;
}

int32_t filterConvertGroupFromArray(SFilterInfo *info, SArray* group) {
  size_t groupSize = taosArrayGetSize(group);

D
dapan1121 已提交
2348
  info->groupNum = (uint32_t)groupSize;
2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364

  if (info->groupNum > 0) {
    info->groups = calloc(info->groupNum, sizeof(*info->groups));
  }

  for (size_t i = 0; i < groupSize; ++i) {
    SFilterGroup *pg = taosArrayGet(group, i);
    pg->unitFlags = calloc(pg->unitNum, sizeof(*pg->unitFlags));
    info->groups[i] = *pg;
  }

  return TSDB_CODE_SUCCESS;
}

int32_t filterRewrite(SFilterInfo *info, SFilterGroupCtx** gRes, int32_t gResNum) {
  if (!FILTER_GET_FLAG(info->status, FI_STATUS_REWRITE)) {
D
dapan1121 已提交
2365
    qDebug("no need rewrite");
2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376
    return TSDB_CODE_SUCCESS;
  }
  
  SFilterInfo oinfo = *info;

  FILTER_SET_FLAG(oinfo.status, FI_STATUS_CLONED);
  
  SArray* group = taosArrayInit(FILTER_DEFAULT_GROUP_SIZE, sizeof(SFilterGroup));
  SFilterGroupCtx *res = NULL;
  SFilterColInfo *colInfo = NULL;
  int32_t optr = 0;
D
dapan1121 已提交
2377
  uint32_t uidx = 0;
2378 2379 2380 2381 2382 2383 2384 2385

  memset(info, 0, sizeof(*info));
  
  info->colRangeNum = oinfo.colRangeNum;
  info->colRange = oinfo.colRange;
  oinfo.colRangeNum = 0;
  oinfo.colRange = NULL;

D
dapan1121 已提交
2386
  FILTER_SET_FLAG(info->options, FLT_OPTION_NEED_UNIQE);
2387 2388 2389 2390 2391 2392

  filterInitUnitsFields(info);

  for (int32_t i = 0; i < gResNum; ++i) {
    res = gRes[i];

D
dapan1121 已提交
2393
    optr = (res->colNum > 1) ? LOGIC_COND_TYPE_AND : LOGIC_COND_TYPE_OR;
2394 2395 2396

    SFilterGroup ng = {0};
    
D
dapan1121 已提交
2397
    for (uint32_t m = 0; m < res->colNum; ++m) {
2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432
      colInfo = &res->colInfo[res->colIdx[m]];
      if (FILTER_NO_MERGE_DATA_TYPE(colInfo->dataType)) {
        assert(colInfo->type == RANGE_TYPE_UNIT);
        int32_t usize = (int32_t)taosArrayGetSize((SArray *)colInfo->info);
        
        for (int32_t n = 0; n < usize; ++n) {
          SFilterUnit* u = taosArrayGetP((SArray *)colInfo->info, n);
          
          filterAddUnitFromUnit(info, &oinfo, u, &uidx);
          filterAddUnitToGroup(&ng, uidx);
        }
        
        continue;
      }
      
      assert(colInfo->type == RANGE_TYPE_MR_CTX);
      
      filterAddGroupUnitFromCtx(info, &oinfo, colInfo->info, res->colIdx[m], &ng, optr, group);
    }

    if (ng.unitNum > 0) {
      taosArrayPush(group, &ng);
    }
  }

  filterConvertGroupFromArray(info, group);

  taosArrayDestroy(group);

  filterFreeInfo(&oinfo);

  return TSDB_CODE_SUCCESS;
}

int32_t filterGenerateColRange(SFilterInfo *info, SFilterGroupCtx** gRes, int32_t gResNum) {
D
dapan1121 已提交
2433 2434
  uint32_t *idxs = NULL;
  uint32_t colNum = 0;
2435
  SFilterGroupCtx *res = NULL;
D
dapan1121 已提交
2436
  uint32_t *idxNum = calloc(info->fields[FLD_TYPE_COLUMN].num, sizeof(*idxNum));
2437 2438

  for (int32_t i = 0; i < gResNum; ++i) {
D
dapan1121 已提交
2439
    for (uint32_t m = 0; m < gRes[i]->colNum; ++m) {
2440 2441 2442 2443 2444 2445 2446 2447 2448
      SFilterColInfo  *colInfo = &gRes[i]->colInfo[gRes[i]->colIdx[m]];
      if (FILTER_NO_MERGE_DATA_TYPE(colInfo->dataType)) {
        continue;
      }

      ++idxNum[gRes[i]->colIdx[m]];
    }
  }

D
dapan1121 已提交
2449
  for (uint32_t i = 0; i < info->fields[FLD_TYPE_COLUMN].num; ++i) {
2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462
    if (idxNum[i] < gResNum) {
      continue;
    }

    assert(idxNum[i] == gResNum);
    
    if (idxs == NULL) {
      idxs = calloc(info->fields[FLD_TYPE_COLUMN].num, sizeof(*idxs));
    }

    idxs[colNum++] = i;
  }

D
dapan1121 已提交
2463
  FLT_CHK_JMP(colNum <= 0);
2464 2465 2466 2467 2468 2469

  info->colRangeNum = colNum;
  info->colRange = calloc(colNum, POINTER_BYTES);

  for (int32_t i = 0; i < gResNum; ++i) {
    res = gRes[i];
D
dapan1121 已提交
2470
    uint32_t n = 0;
2471

D
dapan1121 已提交
2472
    for (uint32_t m = 0; m < info->colRangeNum; ++m) {
2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483
      for (; n < res->colNum; ++n) {
        if (res->colIdx[n] < idxs[m]) {
          continue;
        }

        assert(res->colIdx[n] == idxs[m]);

        SFilterColInfo * colInfo = &res->colInfo[res->colIdx[n]];
        if (info->colRange[m] == NULL) {
          info->colRange[m] = filterInitRangeCtx(colInfo->dataType, 0);
          SFilterField* fi = FILTER_GET_COL_FIELD(info, res->colIdx[n]);
D
dapan 已提交
2484
          info->colRange[m]->colId = FILTER_GET_COL_FIELD_ID(fi);
2485 2486 2487 2488 2489
        }

        assert(colInfo->type == RANGE_TYPE_MR_CTX);

        bool all = false;
D
dapan1121 已提交
2490
        filterSourceRangeFromCtx(info->colRange[m], colInfo->info, LOGIC_COND_TYPE_OR, NULL, &all);
2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502
        if (all) {
          filterFreeRangeCtx(info->colRange[m]);
          info->colRange[m] = NULL;
          
          if (m < (info->colRangeNum - 1)) {
            memmove(&info->colRange[m], &info->colRange[m + 1], (info->colRangeNum - m - 1) * POINTER_BYTES);
            memmove(&idxs[m], &idxs[m + 1], (info->colRangeNum - m - 1) * sizeof(*idxs));
          }

          --info->colRangeNum;
          --m;

D
dapan1121 已提交
2503
          FLT_CHK_JMP(info->colRangeNum <= 0);          
2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519
        }

        ++n; 
        break;
      }
    }
  }

_return:
  tfree(idxNum);
  tfree(idxs);

  return TSDB_CODE_SUCCESS;
}

int32_t filterPostProcessRange(SFilterInfo *info) {
D
dapan1121 已提交
2520
  for (uint32_t i = 0; i < info->colRangeNum; ++i) {
2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537
    SFilterRangeCtx* ctx = info->colRange[i];
    SFilterRangeNode *r = ctx->rs;
    while (r) {
      r->rc.func = filterGetRangeCompFunc(r->ra.sflag, r->ra.eflag);
      r = r->next;
    }
  }

  return TSDB_CODE_SUCCESS;
}


int32_t filterGenerateComInfo(SFilterInfo *info) {
  info->cunits = malloc(info->unitNum * sizeof(*info->cunits));
  info->blkUnitRes = malloc(sizeof(*info->blkUnitRes) * info->unitNum);
  info->blkUnits = malloc(sizeof(*info->blkUnits) * (info->unitNum + 1) * info->groupNum);

D
dapan1121 已提交
2538
  for (uint32_t i = 0; i < info->unitNum; ++i) {
2539 2540 2541 2542 2543 2544 2545 2546 2547
    SFilterUnit *unit = &info->units[i];

    info->cunits[i].func = filterGetCompFuncIdx(FILTER_UNIT_DATA_TYPE(unit), unit->compare.optr);
    info->cunits[i].rfunc = filterGetRangeCompFuncFromOptrs(unit->compare.optr, unit->compare.optr2);
    info->cunits[i].optr = FILTER_UNIT_OPTR(unit);
    info->cunits[i].colData = NULL;
    info->cunits[i].colId = FILTER_UNIT_COL_ID(info, unit);
    
    if (unit->right.type == FLD_TYPE_VALUE) {
D
dapan1121 已提交
2548 2549 2550 2551 2552
      if(FILTER_UNIT_DATA_TYPE(unit) == TSDB_DATA_TYPE_JSON){   // json value is tVariant
        info->cunits[i].valData = FILTER_UNIT_JSON_VAL_DATA(info, unit);
      }else{
        info->cunits[i].valData = FILTER_UNIT_VAL_DATA(info, unit);
      }
2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569
    } else {
      info->cunits[i].valData = NULL;
    }
    if (unit->right2.type == FLD_TYPE_VALUE) {
      info->cunits[i].valData2 = FILTER_GET_VAL_FIELD_DATA(FILTER_GET_FIELD(info, unit->right2));
    } else {
      info->cunits[i].valData2 = info->cunits[i].valData;
    }
    
    info->cunits[i].dataSize = FILTER_UNIT_COL_SIZE(info, unit);
    info->cunits[i].dataType = FILTER_UNIT_DATA_TYPE(unit);
  }
  
  return TSDB_CODE_SUCCESS;
}

int32_t filterUpdateComUnits(SFilterInfo *info) {
D
dapan1121 已提交
2570
  for (uint32_t i = 0; i < info->unitNum; ++i) {
2571 2572
    SFilterUnit *unit = &info->units[i];

D
dapan1121 已提交
2573 2574
    SFilterField *col = FILTER_UNIT_LEFT_FIELD(info, unit);
    info->cunits[i].colData = col->data;
2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585
  }

  return TSDB_CODE_SUCCESS;
}


int32_t filterRmUnitByRange(SFilterInfo *info, SColumnDataAgg *pDataStatis, int32_t numOfCols, int32_t numOfRows) {
  int32_t rmUnit = 0;

  memset(info->blkUnitRes, 0, sizeof(*info->blkUnitRes) * info->unitNum);
  
D
dapan1121 已提交
2586
  for (uint32_t k = 0; k < info->unitNum; ++k) {
2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605
    int32_t index = -1;
    SFilterComUnit *cunit = &info->cunits[k];

    if (FILTER_NO_MERGE_DATA_TYPE(cunit->dataType)) {
      continue;
    }

    for(int32_t i = 0; i < numOfCols; ++i) {
      if (pDataStatis[i].colId == cunit->colId) {
        index = i;
        break;
      }
    }

    if (index == -1) {
      continue;
    }

    if (pDataStatis[index].numOfNull <= 0) {
D
dapan1121 已提交
2606
      if (cunit->optr == OP_TYPE_IS_NULL) {
2607 2608 2609 2610 2611
        info->blkUnitRes[k] = -1;
        rmUnit = 1;
        continue;
      }

D
dapan1121 已提交
2612
      if (cunit->optr == OP_TYPE_IS_NOT_NULL) {
2613 2614 2615 2616 2617 2618
        info->blkUnitRes[k] = 1;
        rmUnit = 1;
        continue;
      }
    } else {
      if (pDataStatis[index].numOfNull == numOfRows) {
D
dapan1121 已提交
2619
        if (cunit->optr == OP_TYPE_IS_NULL) {
2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630
          info->blkUnitRes[k] = 1;
          rmUnit = 1;
          continue;
        }
        
        info->blkUnitRes[k] = -1;
        rmUnit = 1;
        continue;
      }
    }

D
dapan1121 已提交
2631 2632 2633
    if (cunit->optr == OP_TYPE_IS_NULL || cunit->optr == OP_TYPE_IS_NOT_NULL
     || cunit->optr == OP_TYPE_IN || cunit->optr == OP_TYPE_LIKE || cunit->optr == OP_TYPE_MATCH
     || cunit->optr == OP_TYPE_NOT_EQUAL) {
2634 2635 2636 2637 2638
      continue;
    }

    SColumnDataAgg* pDataBlockst = &pDataStatis[index];
    void *minVal, *maxVal;
D
dapan1121 已提交
2639 2640
    float minv = 0;
    float maxv = 0;
2641 2642

    if (cunit->dataType == TSDB_DATA_TYPE_FLOAT) {
D
dapan1121 已提交
2643 2644
      minv = (float)(*(double *)(&pDataBlockst->min));
      maxv = (float)(*(double *)(&pDataBlockst->max));
2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662
       
      minVal = &minv;
      maxVal = &maxv;
    } else {
      minVal = &pDataBlockst->min;
      maxVal = &pDataBlockst->max;
    }

    bool minRes = false, maxRes = false;

    if (cunit->rfunc >= 0) {
      minRes = (*gRangeCompare[cunit->rfunc])(minVal, minVal, cunit->valData, cunit->valData2, gDataCompare[cunit->func]);
      maxRes = (*gRangeCompare[cunit->rfunc])(maxVal, maxVal, cunit->valData, cunit->valData2, gDataCompare[cunit->func]);

      if (minRes && maxRes) {
        info->blkUnitRes[k] = 1;
        rmUnit = 1;
      } else if ((!minRes) && (!maxRes)) {
D
dapan1121 已提交
2663 2664
        minRes = filterDoCompare(gDataCompare[cunit->func], OP_TYPE_LOWER_EQUAL, minVal, cunit->valData);
        maxRes = filterDoCompare(gDataCompare[cunit->func], OP_TYPE_GREATER_EQUAL, maxVal, cunit->valData2);
2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680

        if (minRes && maxRes) {
          continue;
        }
        
        info->blkUnitRes[k] = -1;
        rmUnit = 1;
      }
    } else {
      minRes = filterDoCompare(gDataCompare[cunit->func], cunit->optr, minVal, cunit->valData);
      maxRes = filterDoCompare(gDataCompare[cunit->func], cunit->optr, maxVal, cunit->valData);

      if (minRes && maxRes) {
        info->blkUnitRes[k] = 1;
        rmUnit = 1;
      } else if ((!minRes) && (!maxRes)) {
D
dapan1121 已提交
2681 2682 2683
        if (cunit->optr == OP_TYPE_EQUAL) {
          minRes = filterDoCompare(gDataCompare[cunit->func], OP_TYPE_GREATER_THAN, minVal, cunit->valData);
          maxRes = filterDoCompare(gDataCompare[cunit->func], OP_TYPE_LOWER_THAN, maxVal, cunit->valData);
2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698
          if (minRes || maxRes) {
            info->blkUnitRes[k] = -1;
            rmUnit = 1;
          }
          
          continue;
        }
        
        info->blkUnitRes[k] = -1;
        rmUnit = 1;
      }
    }

  }

D
dapan1121 已提交
2699 2700 2701 2702
  if (rmUnit == 0) {
    fltDebug("NO Block Filter APPLY");
    FLT_RET(TSDB_CODE_SUCCESS);
  }
2703 2704 2705

  info->blkGroupNum = info->groupNum;
  
D
dapan1121 已提交
2706 2707
  uint32_t *unitNum = info->blkUnits;
  uint32_t *unitIdx = unitNum + 1;
2708 2709 2710 2711 2712 2713 2714 2715 2716
  int32_t all = 0, empty = 0;
  
  for (uint32_t g = 0; g < info->groupNum; ++g) {
    SFilterGroup *group = &info->groups[g];
    *unitNum = group->unitNum;
    all = 0; 
    empty = 0;
    
    for (uint32_t u = 0; u < group->unitNum; ++u) {
D
dapan1121 已提交
2717
      uint32_t uidx = group->unitIdxs[u];
2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763
      if (info->blkUnitRes[uidx] == 1) {
        --(*unitNum);
        all = 1;
        continue;
      } else if (info->blkUnitRes[uidx] == -1) {
        *unitNum = 0;
        empty = 1;
        break;
      }

      *(unitIdx++) = uidx;
    }

    if (*unitNum == 0) {
      --info->blkGroupNum;
      assert(empty || all);
      
      if (empty) {
        FILTER_SET_FLAG(info->blkFlag, FI_STATUS_BLK_EMPTY);
      } else {
        FILTER_SET_FLAG(info->blkFlag, FI_STATUS_BLK_ALL);
        goto _return;
      }
      
      continue;
    }

    unitNum = unitIdx;
    ++unitIdx;
  }

  if (info->blkGroupNum) {
    FILTER_CLR_FLAG(info->blkFlag, FI_STATUS_BLK_EMPTY);
    FILTER_SET_FLAG(info->blkFlag, FI_STATUS_BLK_ACTIVE);
  }

_return:

  filterDumpInfoToString(info, "Block Filter", 2);

  return TSDB_CODE_SUCCESS;
}

bool filterExecuteBasedOnStatisImpl(void *pinfo, int32_t numOfRows, int8_t** p, SColumnDataAgg *statis, int16_t numOfCols) {
  SFilterInfo *info = (SFilterInfo *)pinfo;
  bool all = true;
D
dapan1121 已提交
2764
  uint32_t *unitIdx = NULL;
2765

D
dapan1121 已提交
2766 2767 2768 2769
  if (*p == NULL) {
    *p = calloc(numOfRows, sizeof(int8_t));
  }
  
2770 2771 2772 2773 2774 2775
  for (int32_t i = 0; i < numOfRows; ++i) {
    //FILTER_UNIT_CLR_F(info);

    unitIdx = info->blkUnits;
    
    for (uint32_t g = 0; g < info->blkGroupNum; ++g) {
D
dapan1121 已提交
2776
      uint32_t unitNum = *(unitIdx++);
2777 2778
      for (uint32_t u = 0; u < unitNum; ++u) {
        SFilterComUnit *cunit = &info->cunits[*(unitIdx + u)];
D
dapan1121 已提交
2779
        void *colData = colDataGet((SColumnInfoData *)cunit->colData, i);
2780 2781 2782 2783 2784 2785 2786
      
        //if (FILTER_UNIT_GET_F(info, uidx)) {
        //  p[i] = FILTER_UNIT_GET_R(info, uidx);
        //} else {
          uint8_t optr = cunit->optr;

          if (isNull(colData, cunit->dataType)) {
D
dapan1121 已提交
2787
            (*p)[i] = optr == OP_TYPE_IS_NULL ? true : false;
2788
          } else {
D
dapan1121 已提交
2789
            if (optr == OP_TYPE_IS_NOT_NULL) {
2790
              (*p)[i] = 1;
D
dapan1121 已提交
2791
            } else if (optr == OP_TYPE_IS_NULL) {
2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870
              (*p)[i] = 0;
            } else if (cunit->rfunc >= 0) {
              (*p)[i] = (*gRangeCompare[cunit->rfunc])(colData, colData, cunit->valData, cunit->valData2, gDataCompare[cunit->func]);
            } else {
              (*p)[i] = filterDoCompare(gDataCompare[cunit->func], cunit->optr, colData, cunit->valData);
            }
          
          //FILTER_UNIT_SET_R(info, uidx, p[i]);
          //FILTER_UNIT_SET_F(info, uidx);
          }

        if ((*p)[i] == 0) {
          break;
        }
      }

      if ((*p)[i]) {
        break;
      }

      unitIdx += unitNum;
    }

    if ((*p)[i] == 0) {
      all = false;
    }    
  }

  return all;
}



int32_t filterExecuteBasedOnStatis(SFilterInfo *info, int32_t numOfRows, int8_t** p, SColumnDataAgg *statis, int16_t numOfCols, bool* all) {
  if (statis && numOfRows >= FILTER_RM_UNIT_MIN_ROWS) {    
    info->blkFlag = 0;
    
    filterRmUnitByRange(info, statis, numOfCols, numOfRows);
    
    if (info->blkFlag) {
      if (FILTER_GET_FLAG(info->blkFlag, FI_STATUS_BLK_ALL)) {
        *all = true;
        goto _return;
      } else if (FILTER_GET_FLAG(info->blkFlag, FI_STATUS_BLK_EMPTY)) {
        *all = false;
        goto _return;
      }

      assert(info->unitNum > 1);
      
      *all = filterExecuteBasedOnStatisImpl(info, numOfRows, p, statis, numOfCols);

      goto _return;
    }
  }

  return 1;

_return:
  info->blkFlag = 0;
  
  return TSDB_CODE_SUCCESS;
}


static FORCE_INLINE bool filterExecuteImplAll(void *info, int32_t numOfRows, int8_t** p, SColumnDataAgg *statis, int16_t numOfCols) {
  return true;
}
static FORCE_INLINE bool filterExecuteImplEmpty(void *info, int32_t numOfRows, int8_t** p, SColumnDataAgg *statis, int16_t numOfCols) {
  return false;
}
static FORCE_INLINE bool filterExecuteImplIsNull(void *pinfo, int32_t numOfRows, int8_t** p, SColumnDataAgg *statis, int16_t numOfCols) {
  SFilterInfo *info = (SFilterInfo *)pinfo;
  bool all = true;

  if (filterExecuteBasedOnStatis(info, numOfRows, p, statis, numOfCols, &all) == 0) {
    return all;
  }

D
dapan1121 已提交
2871 2872 2873
  if (*p == NULL) {
    *p = calloc(numOfRows, sizeof(int8_t));
  }
2874 2875
  
  for (int32_t i = 0; i < numOfRows; ++i) {
D
dapan1121 已提交
2876
    uint32_t uidx = info->groups[0].unitIdxs[0];
D
dapan1121 已提交
2877
    void *colData = colDataGet((SColumnInfoData *)info->cunits[uidx].colData, i);
D
dapan1121 已提交
2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889
    if(info->cunits[uidx].dataType == TSDB_DATA_TYPE_JSON){
      if (!colData){  // for json->'key' is null
        (*p)[i] = 1;
      }else if( *(char*)colData == TSDB_DATA_TYPE_JSON){  // for json is null
        colData = POINTER_SHIFT(colData, CHAR_BYTES);
        (*p)[i] = isNull(colData, info->cunits[uidx].dataType);
      }else{
        (*p)[i] = 0;
      }
    }else{
      (*p)[i] = ((colData == NULL) || isNull(colData, info->cunits[uidx].dataType));
    }
2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904
    if ((*p)[i] == 0) {
      all = false;
    }    
  }

  return all;
}
static FORCE_INLINE bool filterExecuteImplNotNull(void *pinfo, int32_t numOfRows, int8_t** p, SColumnDataAgg *statis, int16_t numOfCols) {
  SFilterInfo *info = (SFilterInfo *)pinfo;
  bool all = true;

  if (filterExecuteBasedOnStatis(info, numOfRows, p, statis, numOfCols, &all) == 0) {
    return all;
  }

D
dapan1121 已提交
2905 2906 2907 2908
  if (*p == NULL) {
    *p = calloc(numOfRows, sizeof(int8_t));
  }
  
2909
  for (int32_t i = 0; i < numOfRows; ++i) {
D
dapan1121 已提交
2910
    uint32_t uidx = info->groups[0].unitIdxs[0];
D
dapan1121 已提交
2911
    void *colData = colDataGet((SColumnInfoData *)info->cunits[uidx].colData, i);
D
dapan1121 已提交
2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925

    if(info->cunits[uidx].dataType == TSDB_DATA_TYPE_JSON){
      if (!colData) {   // for json->'key' is not null
        (*p)[i] = 0;
      }else if( *(char*)colData == TSDB_DATA_TYPE_JSON){   // for json is not null
        colData = POINTER_SHIFT(colData, CHAR_BYTES);
        (*p)[i] = !isNull(colData, info->cunits[uidx].dataType);
      }else{    // for json->'key' is not null
        (*p)[i] = 1;
      }
    }else {
      (*p)[i] = ((colData != NULL) && !isNull(colData, info->cunits[uidx].dataType));
    }

2926 2927 2928 2929 2930 2931 2932 2933
    if ((*p)[i] == 0) {
      all = false;
    }
  }

  return all;
}

D
dapan1121 已提交
2934
bool filterExecuteImplRange(void *pinfo, int32_t numOfRows, int8_t** p, SColumnDataAgg *statis, int16_t numOfCols) {
2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946
  SFilterInfo *info = (SFilterInfo *)pinfo;
  bool all = true;
  uint16_t dataSize = info->cunits[0].dataSize;
  rangeCompFunc rfunc = gRangeCompare[info->cunits[0].rfunc];
  void *valData = info->cunits[0].valData;
  void *valData2 = info->cunits[0].valData2;
  __compar_fn_t func = gDataCompare[info->cunits[0].func];

  if (filterExecuteBasedOnStatis(info, numOfRows, p, statis, numOfCols, &all) == 0) {
    return all;
  }

D
dapan1121 已提交
2947 2948 2949
  if (*p == NULL) {
    *p = calloc(numOfRows, sizeof(int8_t));
  }
2950
  
D
dapan1121 已提交
2951 2952 2953
  for (int32_t i = 0; i < numOfRows; ++i) {    
    void *colData = colDataGet((SColumnInfoData *)info->cunits[0].colData, i);

D
dapan1121 已提交
2954
    if (colData == NULL || isNull(colData, info->cunits[0].dataType)) {
2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975
      all = false;
      continue;
    }

    (*p)[i] = (*rfunc)(colData, colData, valData, valData2, func);
            
    if ((*p)[i] == 0) {
      all = false;
    }
  }

  return all;
}

bool filterExecuteImplMisc(void *pinfo, int32_t numOfRows, int8_t** p, SColumnDataAgg *statis, int16_t numOfCols) {
  SFilterInfo *info = (SFilterInfo *)pinfo;
  bool all = true;

  if (filterExecuteBasedOnStatis(info, numOfRows, p, statis, numOfCols, &all) == 0) {
    return all;
  }
D
dapan1121 已提交
2976 2977 2978 2979
  
  if (*p == NULL) {
    *p = calloc(numOfRows, sizeof(int8_t));
  }
2980 2981
  
  for (int32_t i = 0; i < numOfRows; ++i) {
D
dapan1121 已提交
2982
    uint32_t uidx = info->groups[0].unitIdxs[0];
D
dapan1121 已提交
2983
    void *colData = colDataGet((SColumnInfoData *)info->cunits[uidx].colData, i);
D
dapan1121 已提交
2984 2985
    if (colData == NULL || isNull(colData, info->cunits[uidx].dataType)) {
      (*p)[i] = 0;
2986 2987 2988
      all = false;
      continue;
    }
D
dapan1121 已提交
2989 2990
    // match/nmatch for nchar type need convert from ucs4 to mbs

D
dapan1121 已提交
2991
    if(info->cunits[uidx].dataType == TSDB_DATA_TYPE_NCHAR && (info->cunits[uidx].optr == OP_TYPE_MATCH || info->cunits[uidx].optr == OP_TYPE_NMATCH)){
D
dapan1121 已提交
2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003
      char *newColData = calloc(info->cunits[uidx].dataSize * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE, 1);
      int32_t len = taosUcs4ToMbs(varDataVal(colData), varDataLen(colData), varDataVal(newColData));
      if (len < 0){
        qError("castConvert1 taosUcs4ToMbs error");
      }else{
        varDataSetLen(newColData, len);
        (*p)[i] = filterDoCompare(gDataCompare[info->cunits[uidx].func], info->cunits[uidx].optr, newColData, info->cunits[uidx].valData);
      }
      tfree(newColData);
    }else{
      (*p)[i] = filterDoCompare(gDataCompare[info->cunits[uidx].func], info->cunits[uidx].optr, colData, info->cunits[uidx].valData);
    }
3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021

    if ((*p)[i] == 0) {
      all = false;
    }
  }

  return all;
}


bool filterExecuteImpl(void *pinfo, int32_t numOfRows, int8_t** p, SColumnDataAgg *statis, int16_t numOfCols) {
  SFilterInfo *info = (SFilterInfo *)pinfo;
  bool all = true;

  if (filterExecuteBasedOnStatis(info, numOfRows, p, statis, numOfCols, &all) == 0) {
    return all;
  }

D
dapan1121 已提交
3022 3023 3024 3025
  if (*p == NULL) {
    *p = calloc(numOfRows, sizeof(int8_t));
  }
  
3026 3027 3028 3029 3030 3031
  for (int32_t i = 0; i < numOfRows; ++i) {
    //FILTER_UNIT_CLR_F(info);
  
    for (uint32_t g = 0; g < info->groupNum; ++g) {
      SFilterGroup *group = &info->groups[g];
      for (uint32_t u = 0; u < group->unitNum; ++u) {
D
dapan1121 已提交
3032
        uint32_t uidx = group->unitIdxs[u];
3033
        SFilterComUnit *cunit = &info->cunits[uidx];
D
dapan1121 已提交
3034
        void *colData = colDataGet((SColumnInfoData *)(cunit->colData), i);
3035 3036 3037 3038 3039 3040
      
        //if (FILTER_UNIT_GET_F(info, uidx)) {
        //  p[i] = FILTER_UNIT_GET_R(info, uidx);
        //} else {
          uint8_t optr = cunit->optr;

D
dapan1121 已提交
3041
          if (colData == NULL || isNull(colData, cunit->dataType)) {
D
dapan1121 已提交
3042
            (*p)[i] = optr == OP_TYPE_IS_NULL ? true : false;
3043
          } else {
D
dapan1121 已提交
3044
            if (optr == OP_TYPE_IS_NOT_NULL) {
3045
              (*p)[i] = 1;
D
dapan1121 已提交
3046
            } else if (optr == OP_TYPE_IS_NULL) {
3047 3048 3049 3050
              (*p)[i] = 0;
            } else if (cunit->rfunc >= 0) {
              (*p)[i] = (*gRangeCompare[cunit->rfunc])(colData, colData, cunit->valData, cunit->valData2, gDataCompare[cunit->func]);
            } else {
D
dapan1121 已提交
3051
              if(cunit->dataType == TSDB_DATA_TYPE_NCHAR && (cunit->optr == OP_TYPE_MATCH || cunit->optr == OP_TYPE_NMATCH)){
D
dapan1121 已提交
3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063
                char *newColData = calloc(cunit->dataSize * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE, 1);
                int32_t len = taosUcs4ToMbs(varDataVal(colData), varDataLen(colData), varDataVal(newColData));
                if (len < 0){
                  qError("castConvert1 taosUcs4ToMbs error");
                }else{
                  varDataSetLen(newColData, len);
                  (*p)[i] = filterDoCompare(gDataCompare[cunit->func], cunit->optr, newColData, cunit->valData);
                }
                tfree(newColData);
              }else{
                (*p)[i] = filterDoCompare(gDataCompare[cunit->func], cunit->optr, colData, cunit->valData);
              }
3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103
            }
          
          //FILTER_UNIT_SET_R(info, uidx, p[i]);
          //FILTER_UNIT_SET_F(info, uidx);
          }

        if ((*p)[i] == 0) {
          break;
        }
      }

      if ((*p)[i]) {
        break;
      }
    }

    if ((*p)[i] == 0) {
      all = false;
    }    
  }

  return all;
}

int32_t filterSetExecFunc(SFilterInfo *info) {
  if (FILTER_ALL_RES(info)) {
    info->func = filterExecuteImplAll;
    return TSDB_CODE_SUCCESS;
  }

  if (FILTER_EMPTY_RES(info)) {
    info->func = filterExecuteImplEmpty;
    return TSDB_CODE_SUCCESS;
  }

  if (info->unitNum > 1) {
    info->func = filterExecuteImpl;
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
3104
  if (info->units[0].compare.optr == OP_TYPE_IS_NULL) {
3105 3106 3107 3108
    info->func = filterExecuteImplIsNull;
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
3109
  if (info->units[0].compare.optr == OP_TYPE_IS_NOT_NULL) {
3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133
    info->func = filterExecuteImplNotNull;
    return TSDB_CODE_SUCCESS;
  }

  if (info->cunits[0].rfunc >= 0) {
    info->func = filterExecuteImplRange;
    return TSDB_CODE_SUCCESS;  
  }

  info->func = filterExecuteImplMisc;
  return TSDB_CODE_SUCCESS;  
}



int32_t filterPreprocess(SFilterInfo *info) {
  SFilterGroupCtx** gRes = calloc(info->groupNum, sizeof(SFilterGroupCtx *));
  int32_t gResNum = 0;
  
  filterMergeGroupUnits(info, gRes, &gResNum);

  filterMergeGroups(info, gRes, &gResNum);

  if (FILTER_GET_FLAG(info->status, FI_STATUS_ALL)) {
D
dapan1121 已提交
3134
    fltInfo("Final - FilterInfo: [ALL]");
3135 3136 3137 3138 3139
    goto _return;
  }

  
  if (FILTER_GET_FLAG(info->status, FI_STATUS_EMPTY)) {
D
dapan1121 已提交
3140
    fltInfo("Final - FilterInfo: [EMPTY]");
3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166
    goto _return;
  }  

  filterGenerateColRange(info, gRes, gResNum);

  filterDumpInfoToString(info, "Final", 1);

  filterPostProcessRange(info);

  filterRewrite(info, gRes, gResNum);

  filterGenerateComInfo(info);

_return:

  filterSetExecFunc(info);

  for (int32_t i = 0; i < gResNum; ++i) {
    filterFreeGroupCtx(gRes[i]);
  }

  tfree(gRes);
  
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
3167 3168

int32_t fltSetColFieldDataImpl(SFilterInfo *info, void *param, filer_get_col_from_id fp, bool fromColId) {
3169 3170 3171 3172
  if (FILTER_ALL_RES(info) || FILTER_EMPTY_RES(info)) {
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
3173
  for (uint32_t i = 0; i < info->fields[FLD_TYPE_COLUMN].num; ++i) {
3174
    SFilterField* fi = &info->fields[FLD_TYPE_COLUMN].fields[i];
D
dapan1121 已提交
3175

D
dapan1121 已提交
3176 3177 3178 3179 3180
    if (fromColId) {
      (*fp)(param, FILTER_GET_COL_FIELD_ID(fi), &fi->data);
    } else {
      (*fp)(param, FILTER_GET_COL_FIELD_SLOT_ID(fi), &fi->data);
    }
3181 3182 3183 3184 3185 3186 3187
  }

  filterUpdateComUnits(info);

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
3188 3189 3190

int32_t fltInitFromNode(SNode* tree, SFilterInfo *info, uint32_t options) {
  int32_t code = TSDB_CODE_SUCCESS;
3191 3192 3193 3194 3195
  
  SArray* group = taosArrayInit(FILTER_DEFAULT_GROUP_SIZE, sizeof(SFilterGroup));

  filterInitUnitsFields(info);

D
dapan1121 已提交
3196 3197 3198
  SFltBuildGroupCtx tctx = {.info = info, .group = group};
  nodesWalkNode(tree, fltTreeToGroup, (void *)&tctx);
  FLT_ERR_JRET(tctx.code);
3199 3200

  filterConvertGroupFromArray(info, group);
D
dapan1121 已提交
3201
  taosArrayDestroy(group);
3202

D
dapan1121 已提交
3203
  FLT_ERR_JRET(fltInitValFieldData(info));
3204

D
dapan1121 已提交
3205
  if (!FILTER_GET_FLAG(info->options, FLT_OPTION_NO_REWRITE)) {
3206 3207
    filterDumpInfoToString(info, "Before preprocess", 0);

D
dapan1121 已提交
3208
    FLT_ERR_JRET(filterPreprocess(info));
3209
    
D
dapan1121 已提交
3210
    FLT_CHK_JMP(FILTER_GET_FLAG(info->status, FI_STATUS_ALL));
3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225

    if (FILTER_GET_FLAG(info->status, FI_STATUS_EMPTY)) {
      return code;
    }
  }  

  info->unitRes = malloc(info->unitNum * sizeof(*info->unitRes));
  info->unitFlags = malloc(info->unitNum * sizeof(*info->unitFlags));

  filterDumpInfoToString(info, "Final", 0);

  return code;

_return:

D
dapan1121 已提交
3226
  qInfo("init from node failed, code:%d", code);
D
dapan1121 已提交
3227
  
3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245
  return code;
}




bool filterRangeExecute(SFilterInfo *info, SColumnDataAgg *pDataStatis, int32_t numOfCols, int32_t numOfRows) {
  if (FILTER_EMPTY_RES(info)) {
    return false;
  }

  if (FILTER_ALL_RES(info)) {
    return true;
  }
  
  bool ret = true;
  void *minVal, *maxVal;
  
D
dapan1121 已提交
3246
  for (uint32_t k = 0; k < info->colRangeNum; ++k) {
3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265
    int32_t index = -1;
    SFilterRangeCtx *ctx = info->colRange[k];
    for(int32_t i = 0; i < numOfCols; ++i) {
      if (pDataStatis[i].colId == ctx->colId) {
        index = i;
        break;
      }
    }

    // no statistics data, load the true data block
    if (index == -1) {
      break;
    }

    // not support pre-filter operation on binary/nchar data type
    if (FILTER_NO_MERGE_DATA_TYPE(ctx->type)) {
      break;
    }

D
dapan1121 已提交
3266 3267
    if (pDataStatis[index].numOfNull <= 0) {
      if (ctx->isnull && !ctx->notnull && !ctx->isrange) {
3268 3269 3270
        ret = false;
        break;
      }
D
dapan1121 已提交
3271 3272 3273 3274 3275 3276
    } else if (pDataStatis[index].numOfNull > 0) {
      if (pDataStatis[index].numOfNull == numOfRows) {
        if ((ctx->notnull || ctx->isrange) && (!ctx->isnull)) {
          ret = false;
          break;
        }
3277

D
dapan1121 已提交
3278 3279 3280 3281 3282 3283
        continue;
      } else {
        if (ctx->isnull) {
          continue;
        }
      }
3284 3285 3286 3287 3288
    }

    SColumnDataAgg* pDataBlockst = &pDataStatis[index];

    SFilterRangeNode *r = ctx->rs;
D
dapan1121 已提交
3289 3290
    float minv = 0;
    float maxv = 0;
3291 3292

    if (ctx->type == TSDB_DATA_TYPE_FLOAT) {
D
dapan1121 已提交
3293 3294
      minv = (float)(*(double *)(&pDataBlockst->min));
      maxv = (float)(*(double *)(&pDataBlockst->max));
3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310
       
      minVal = &minv;
      maxVal = &maxv;
    } else {
      minVal = &pDataBlockst->min;
      maxVal = &pDataBlockst->max;
    }

    while (r) {
      ret = r->rc.func(minVal, maxVal, &r->rc.s, &r->rc.e, ctx->pCompareFunc);
      if (ret) {
        break;
      }
      r = r->next;
    }
    
D
dapan1121 已提交
3311 3312 3313
    if (!ret) {
      return ret;
    }
3314 3315 3316 3317 3318 3319 3320 3321 3322
  }

  return ret;
}



int32_t filterGetTimeRange(SFilterInfo *info, STimeWindow       *win) {
  SFilterRange ra = {0};
D
dapan1121 已提交
3323 3324
  SFilterRangeCtx *prev = filterInitRangeCtx(TSDB_DATA_TYPE_TIMESTAMP, FLT_OPTION_TIMESTAMP);
  SFilterRangeCtx *tmpc = filterInitRangeCtx(TSDB_DATA_TYPE_TIMESTAMP, FLT_OPTION_TIMESTAMP);
3325 3326 3327 3328 3329 3330
  SFilterRangeCtx *cur = NULL;
  int32_t num = 0;
  int32_t optr = 0;
  int32_t code = 0;
  bool empty = false, all = false;

D
dapan1121 已提交
3331
  for (uint32_t i = 0; i < info->groupNum; ++i) {
3332 3333 3334
    SFilterGroup *group = &info->groups[i];
    if (group->unitNum > 1) {
      cur = tmpc;
D
dapan1121 已提交
3335
      optr = LOGIC_COND_TYPE_AND;
3336 3337
    } else {
      cur = prev;
D
dapan1121 已提交
3338
      optr = LOGIC_COND_TYPE_OR;
3339 3340
    }

D
dapan1121 已提交
3341 3342
    for (uint32_t u = 0; u < group->unitNum; ++u) {
      uint32_t uidx = group->unitIdxs[u];
3343 3344 3345 3346
      SFilterUnit *unit = &info->units[uidx];

      uint8_t raOptr = FILTER_UNIT_OPTR(unit);
      
D
dapan1121 已提交
3347
      filterAddRangeOptr(cur, raOptr, LOGIC_COND_TYPE_AND, &empty, NULL);
D
dapan1121 已提交
3348
      FLT_CHK_JMP(empty);
3349 3350 3351 3352 3353
      
      if (FILTER_NO_MERGE_OPTR(raOptr)) {
        continue;
      }

D
dapan1121 已提交
3354
      filterAddUnitRange(info, unit, cur, optr);
3355 3356 3357 3358 3359 3360 3361 3362
    }

    if (cur->notnull) {
      prev->notnull = true;
      break;
    }

    if (group->unitNum > 1) {
D
dapan1121 已提交
3363
      filterSourceRangeFromCtx(prev, cur, LOGIC_COND_TYPE_OR, &empty, &all);
3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375
      filterResetRangeCtx(cur);
      if (all) {
        break;
      }
    }
  }

  if (prev->notnull) {
    *win = TSWINDOW_INITIALIZER;
  } else {
    filterGetRangeNum(prev, &num);
    if (num > 1) {
D
dapan1121 已提交
3376 3377
      qError("only one time range accepted, num:%d", num);
      FLT_ERR_JRET(TSDB_CODE_QRY_INVALID_TIME_CONDITION);
3378 3379
    }

D
dapan1121 已提交
3380
    FLT_CHK_JMP(num < 1);
3381 3382 3383 3384 3385 3386 3387 3388 3389 3390

    SFilterRange tra;
    filterGetRangeRes(prev, &tra);
    win->skey = tra.s; 
    win->ekey = tra.e;
  }

  filterFreeRangeCtx(prev);
  filterFreeRangeCtx(tmpc);

D
dapan1121 已提交
3391
  qDebug("qFilter time range:[%"PRId64 "]-[%"PRId64 "]", win->skey, win->ekey);
3392 3393 3394 3395 3396 3397 3398 3399 3400
  return TSDB_CODE_SUCCESS;

_return:

  *win = TSWINDOW_DESC_INITIALIZER;

  filterFreeRangeCtx(prev);
  filterFreeRangeCtx(tmpc);

D
dapan1121 已提交
3401
  qDebug("qFilter time range:[%"PRId64 "]-[%"PRId64 "]", win->skey, win->ekey);
3402 3403 3404 3405 3406 3407

  return code;
}


int32_t filterConverNcharColumns(SFilterInfo* info, int32_t rows, bool *gotNchar) {
D
dapan1121 已提交
3408 3409 3410 3411 3412
  if (FILTER_EMPTY_RES(info) || FILTER_ALL_RES(info)) {
    return TSDB_CODE_SUCCESS;
  }
 
  for (uint32_t i = 0; i < info->fields[FLD_TYPE_COLUMN].num; ++i) {
3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424
    SFilterField* fi = &info->fields[FLD_TYPE_COLUMN].fields[i];
    int32_t type = FILTER_GET_COL_FIELD_TYPE(fi);
    if (type == TSDB_DATA_TYPE_NCHAR) {
      SFilterField nfi = {0};
      nfi.desc = fi->desc;
      int32_t bytes = FILTER_GET_COL_FIELD_SIZE(fi);
      nfi.data = malloc(rows * bytes);
      int32_t bufSize = bytes - VARSTR_HEADER_SIZE;
      for (int32_t j = 0; j < rows; ++j) {
        char *src = FILTER_GET_COL_FIELD_DATA(fi, j);
        char *dst = FILTER_GET_COL_FIELD_DATA(&nfi, j);
        int32_t len = 0;
D
dapan1121 已提交
3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438
        char *varSrc = varDataVal(src);
        size_t k = 0, varSrcLen = varDataLen(src);
        while (k < varSrcLen && varSrc[k++] == -1) {}
        if (k == varSrcLen) {
          /* NULL */
          varDataLen(dst) = (VarDataLenT) varSrcLen;
          varDataCopy(dst, src);
          continue;
        }
        bool ret = taosMbsToUcs4(varDataVal(src), varDataLen(src), varDataVal(dst), bufSize, &len);
        if(!ret) {
          qError("filterConverNcharColumns taosMbsToUcs4 error");
          return TSDB_CODE_FAILED;
        }
3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455
        varDataLen(dst) = len;
      }

      fi->data = nfi.data;
      
      *gotNchar = true;
    }
  }

  if (*gotNchar) {
    filterUpdateComUnits(info);
  }

  return TSDB_CODE_SUCCESS;
}

int32_t filterFreeNcharColumns(SFilterInfo* info) {
D
dapan1121 已提交
3456
  for (uint32_t i = 0; i < info->fields[FLD_TYPE_COLUMN].num; ++i) {
3457 3458 3459 3460 3461 3462 3463 3464 3465 3466
    SFilterField* fi = &info->fields[FLD_TYPE_COLUMN].fields[i];
    int32_t type = FILTER_GET_COL_FIELD_TYPE(fi);
    if (type == TSDB_DATA_TYPE_NCHAR) {
      tfree(fi->data);
    }
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
3467 3468
EDealRes fltReviseRewriter(SNode** pNode, void* pContext) {
  SFltTreeStat *stat = (SFltTreeStat *)pContext;
D
dapan 已提交
3469 3470 3471 3472 3473 3474

  if (QUERY_NODE_LOGIC_CONDITION == nodeType(*pNode)) {
    SLogicConditionNode *node = (SLogicConditionNode *)*pNode;
    SListCell *cell = node->pParameterList->pHead;
    for (int32_t i = 0; i < node->pParameterList->length; ++i) {
      if (NULL == cell || NULL == cell->pNode) {
D
dapan1121 已提交
3475
        fltError("invalid cell, cell:%p, pNode:%p", cell, cell->pNode);
D
dapan 已提交
3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489
        stat->code = TSDB_CODE_QRY_INVALID_INPUT;
        return DEAL_RES_ERROR;
      }
    
      if ((QUERY_NODE_OPERATOR != nodeType(cell->pNode)) && (QUERY_NODE_LOGIC_CONDITION != nodeType(cell->pNode))) {
        stat->scalarMode = true;
      }
      
      cell = cell->pNext;
    }

    return DEAL_RES_CONTINUE;
  }

X
Xiaoyu Wang 已提交
3490
  if (QUERY_NODE_VALUE == nodeType(*pNode) || QUERY_NODE_NODE_LIST == nodeType(*pNode) || QUERY_NODE_COLUMN == nodeType(*pNode)) {
D
dapan 已提交
3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506
    return DEAL_RES_CONTINUE;
  }

  if (QUERY_NODE_FUNCTION == nodeType(*pNode)) {
    stat->scalarMode = true;
    return DEAL_RES_CONTINUE;
  }

  if (QUERY_NODE_OPERATOR == nodeType(*pNode)) {
    SOperatorNode *node = (SOperatorNode *)*pNode;
    if (!FLT_IS_COMPARISON_OPERATOR(node->opType)) {
      stat->scalarMode = true;
      return DEAL_RES_CONTINUE;
    }

    if (NULL == node->pRight) {
D
dapan1121 已提交
3507
      if (scalarGetOperatorParamNum(node->opType) > 1) {
D
dapan1121 已提交
3508
        fltError("invalid operator, pRight:%p, nodeType:%d, opType:%d", node->pRight, nodeType(node), node->opType);
D
dapan 已提交
3509 3510 3511 3512
        stat->code = TSDB_CODE_QRY_APP_ERROR;
        return DEAL_RES_ERROR;
      }
      
X
Xiaoyu Wang 已提交
3513
      if (QUERY_NODE_COLUMN != nodeType(node->pLeft)) {
D
dapan 已提交
3514 3515 3516
        stat->scalarMode = true;
        return DEAL_RES_CONTINUE;
      }
D
dapan1121 已提交
3517 3518 3519 3520 3521 3522

      if (OP_TYPE_IS_TRUE == node->opType || OP_TYPE_IS_FALSE == node->opType || OP_TYPE_IS_UNKNOWN == node->opType
       || OP_TYPE_IS_NOT_TRUE == node->opType || OP_TYPE_IS_NOT_FALSE == node->opType || OP_TYPE_IS_NOT_UNKNOWN == node->opType) {
        stat->scalarMode = true;
        return DEAL_RES_CONTINUE;
      }
D
dapan 已提交
3523
    } else {
X
Xiaoyu Wang 已提交
3524
      if ((QUERY_NODE_COLUMN != nodeType(node->pLeft)) && (QUERY_NODE_VALUE != nodeType(node->pLeft))) {
D
dapan 已提交
3525 3526 3527 3528
        stat->scalarMode = true;
        return DEAL_RES_CONTINUE;
      }

X
Xiaoyu Wang 已提交
3529
      if ((QUERY_NODE_COLUMN != nodeType(node->pRight)) && (QUERY_NODE_VALUE != nodeType(node->pRight))) {
D
dapan 已提交
3530 3531 3532 3533 3534 3535 3536 3537 3538
        stat->scalarMode = true;
        return DEAL_RES_CONTINUE;
      }      

      if (nodeType(node->pLeft) == nodeType(node->pRight)) {
        stat->scalarMode = true;
        return DEAL_RES_CONTINUE;
      }

X
Xiaoyu Wang 已提交
3539
      if (QUERY_NODE_COLUMN != nodeType(node->pLeft)) {
D
dapan 已提交
3540 3541 3542 3543 3544
        SNode *t = node->pLeft;
        node->pLeft = node->pRight;
        node->pRight = t;
      }

D
dapan1121 已提交
3545 3546
      if (OP_TYPE_IN == node->opType && QUERY_NODE_NODE_LIST != nodeType(node->pRight)) {
        fltError("invalid IN operator node, rightType:%d", nodeType(node->pRight));
D
dapan 已提交
3547 3548
        stat->code = TSDB_CODE_QRY_APP_ERROR;
        return DEAL_RES_ERROR;
D
dapan1121 已提交
3549 3550 3551
      }

      if (OP_TYPE_IN != node->opType) {
X
Xiaoyu Wang 已提交
3552
        SColumnNode *refNode = (SColumnNode *)node->pLeft;
D
dapan1121 已提交
3553
        SValueNode *valueNode = (SValueNode *)node->pRight;
X
Xiaoyu Wang 已提交
3554 3555
        int32_t type = vectorGetConvertType(refNode->node.resType.type, valueNode->node.resType.type);
        if (0 != type && type != refNode->node.resType.type) {
D
dapan1121 已提交
3556 3557 3558 3559
          stat->scalarMode = true;
          return DEAL_RES_CONTINUE;
        }
      }
D
dapan 已提交
3560 3561 3562 3563 3564
    }

    return DEAL_RES_CONTINUE;
  }  
  
D
dapan1121 已提交
3565
  fltError("invalid node type for filter, type:%d", nodeType(*pNode));
D
dapan 已提交
3566 3567 3568 3569
  
  stat->code = TSDB_CODE_QRY_INVALID_INPUT;
  
  return DEAL_RES_ERROR;
D
dapan1121 已提交
3570 3571 3572 3573 3574 3575 3576 3577 3578
}

int32_t fltReviseNodes(SFilterInfo *pInfo, SNode** pNode, SFltTreeStat *pStat) {
  nodesRewriteNodePostOrder(pNode, fltReviseRewriter, (void *)pStat);

  FLT_RET(pStat->code);
}

int32_t fltOptimizeNodes(SFilterInfo *pInfo, SNode** pNode, SFltTreeStat *pStat) {
D
dapan1121 已提交
3579 3580
  //TODO
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
3581 3582 3583
}


D
dapan1121 已提交
3584
int32_t fltGetDataFromColId(void *param, int32_t id, void **data) {
D
dapan1121 已提交
3585 3586 3587 3588 3589 3590
  int32_t numOfCols = ((SFilterColumnParam *)param)->numOfCols;
  SArray* pDataBlock = ((SFilterColumnParam *)param)->pDataBlock;
  
  for (int32_t j = 0; j < numOfCols; ++j) {
    SColumnInfoData* pColInfo = taosArrayGet(pDataBlock, j);
    if (id == pColInfo->info.colId) {
D
dapan1121 已提交
3591
      *data = pColInfo;
D
dapan1121 已提交
3592 3593 3594 3595 3596 3597 3598
      break;
    }
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
3599 3600 3601 3602 3603 3604 3605 3606 3607 3608
int32_t fltGetDataFromSlotId(void *param, int32_t id, void **data) {
  int32_t numOfCols = ((SFilterColumnParam *)param)->numOfCols;
  SArray* pDataBlock = ((SFilterColumnParam *)param)->pDataBlock;
  if (id < 0 || id >= numOfCols || id >= taosArrayGetSize(pDataBlock)) {
    fltError("invalid slot id, id:%d, numOfCols:%d, arraySize:%d", id, numOfCols, (int32_t)taosArrayGetSize(pDataBlock));
    return TSDB_CODE_QRY_APP_ERROR;
  }
  
  SColumnInfoData* pColInfo = taosArrayGet(pDataBlock, id);
  *data = pColInfo;
D
dapan1121 已提交
3609

D
dapan1121 已提交
3610
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
3611 3612
}

D
dapan1121 已提交
3613

D
dapan1121 已提交
3614

D
dapan1121 已提交
3615 3616
int32_t filterSetDataFromSlotId(SFilterInfo *info, void *param) {
  return fltSetColFieldDataImpl(info, param, fltGetDataFromSlotId, false);
D
dapan1121 已提交
3617 3618
}

D
dapan1121 已提交
3619 3620
int32_t filterSetDataFromColId(SFilterInfo *info, void *param) {
  return fltSetColFieldDataImpl(info, param, fltGetDataFromColId, true);
D
dapan1121 已提交
3621 3622 3623 3624
}



D
dapan1121 已提交
3625 3626 3627 3628 3629 3630 3631 3632
int32_t filterInitFromNode(SNode* pNode, SFilterInfo **pInfo, uint32_t options) {
  int32_t code = 0;
  SFilterInfo *info = NULL;
  
  if (pNode == NULL || pInfo == NULL) {
    fltError("invalid param");
    FLT_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
  }
3633

D
dapan1121 已提交
3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647
  if (*pInfo == NULL) {
    *pInfo = calloc(1, sizeof(SFilterInfo));
    if (NULL == *pInfo) {
      fltError("calloc %d failed", (int32_t)sizeof(SFilterInfo));
      FLT_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
    }
  }

  info = *pInfo;
  info->options = options;

  SFltTreeStat stat = {0};
  FLT_ERR_JRET(fltReviseNodes(info, &pNode, &stat));

D
dapan1121 已提交
3648 3649
  info->scalarMode = stat.scalarMode;

D
dapan1121 已提交
3650 3651
  if (!info->scalarMode) {
    FLT_ERR_JRET(fltInitFromNode(pNode, info, options));
D
dapan 已提交
3652 3653 3654
  } else {
    info->sclCtx.node = pNode;
    FLT_ERR_JRET(fltOptimizeNodes(info, &info->sclCtx.node, &stat));
D
dapan1121 已提交
3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667
  }
  
  return code;

_return:
  
  filterFreeInfo(*pInfo);

  *pInfo = NULL;

  FLT_RET(code);
}

D
dapan1121 已提交
3668
bool filterExecute(SFilterInfo *info, SSDataBlock *pSrc, int8_t** p, SColumnDataAgg *statis, int16_t numOfCols) {
D
dapan1121 已提交
3669 3670 3671 3672 3673
  if (info->scalarMode) {
    SScalarParam output = {0};
    FLT_ERR_RET(scalarCalculate(info->sclCtx.node, pSrc, &output));

    *p = output.data;
D
dapan1121 已提交
3674 3675 3676 3677 3678 3679 3680 3681 3682

    int8_t *r = output.data;
    for (int32_t i = 0; i < output.num; ++i) {
      if (0 == *(r+i)) {
        return false;
      }
    }
    
    return true;
D
dapan1121 已提交
3683 3684 3685 3686
  }

  return (*info->func)(info, pSrc->info.rows, p, statis, numOfCols);
}
3687 3688 3689