qast.c 38.0 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

S
slguan 已提交
16
#include "os.h"
S
slguan 已提交
17
#include "tulog.h"
H
hjxilinx 已提交
18
#include "tutil.h"
19 20
#include "tbuffer.h"
#include "qast.h"
21
#include "tcompare.h"
22 23
#include "qsqlparser.h"
#include "qsyntaxtreefunction.h"
24
#include "taosdef.h"
H
hzcheng 已提交
25 26
#include "taosmsg.h"
#include "tsqlfunction.h"
H
hjxilinx 已提交
27
#include "tstoken.h"
28
#include "ttokendef.h"
S
slguan 已提交
29
#include "tschemautil.h"
H
hjxilinx 已提交
30 31
#include "tarray.h"
#include "tskiplist.h"
S
slguan 已提交
32
#include "queryLog.h"
weixin_48148422's avatar
weixin_48148422 已提交
33
#include "tsdbMain.h"
34
#include "exception.h"
H
hzcheng 已提交
35 36 37 38 39 40 41 42 43 44 45

/*
 *
 * @date 2018-2-15
 * @version 0.2  operation for column filter
 *
 * @Description parse tag query expression to build ast
 * ver 0.2, filter the result on first column with high priority to limit the candidate set
 * ver 0.3, pipeline filter in the form of: (a+2)/9 > 14
 *
 */
H
hjxilinx 已提交
46
static tExprNode *tExprNodeCreate(SSchema *pSchema, int32_t numOfCols, SSQLToken *pToken);
H
hzcheng 已提交
47

H
hjxilinx 已提交
48 49
static tExprNode *createSyntaxTree(SSchema *pSchema, int32_t numOfCols, char *str, int32_t *i);
static void       destroySyntaxTree(tExprNode *);
H
hzcheng 已提交
50

H
hjxilinx 已提交
51
static uint8_t isQueryOnPrimaryKey(const char *primaryColumnName, const tExprNode *pLeft, const tExprNode *pRight);
H
hzcheng 已提交
52 53 54 55 56 57

/*
 * Check the filter value type on the right hand side based on the column id on the left hand side,
 * the filter value type must be identical to field type for relational operation
 * As for binary arithmetic operation, it is not necessary to do so.
 */
H
hjxilinx 已提交
58
static void reviseBinaryExprIfNecessary(tExprNode **pLeft, tExprNode **pRight, uint8_t *optr) {
H
hzcheng 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
  if (*optr >= TSDB_RELATION_LESS && *optr <= TSDB_RELATION_LIKE) {
    // make sure that the type of data on both sides of relational comparision are identical
    if ((*pLeft)->nodeType == TSQL_NODE_VALUE) {
      tVariantTypeSetType((*pLeft)->pVal, (*pRight)->pSchema->type);
    } else if ((*pRight)->nodeType == TSQL_NODE_VALUE) {
      tVariantTypeSetType((*pRight)->pVal, (*pLeft)->pSchema->type);
    }

  } else if (*optr >= TSDB_BINARY_OP_ADD && *optr <= TSDB_BINARY_OP_REMAINDER) {
    if ((*pLeft)->nodeType == TSQL_NODE_VALUE) {
      /* convert to int/bigint may cause the precision loss */
      tVariantTypeSetType((*pLeft)->pVal, TSDB_DATA_TYPE_DOUBLE);
    } else if ((*pRight)->nodeType == TSQL_NODE_VALUE) {
      /* convert to int/bigint may cause the precision loss */
      tVariantTypeSetType((*pRight)->pVal, TSDB_DATA_TYPE_DOUBLE);
    }
  }

S
slguan 已提交
77 78 79 80
  /*
   * for expressions that are suitable for switch principle,
   * switch left and left and right hand side in expr if possible
   */
H
hzcheng 已提交
81
  if ((*pLeft)->nodeType == TSQL_NODE_VALUE && (*pRight)->nodeType == TSQL_NODE_COL) {
82
    if (*optr >= TSDB_RELATION_GREATER && *optr <= TSDB_RELATION_GREATER_EQUAL && *optr != TSDB_RELATION_EQUAL) {
H
hjxilinx 已提交
83
      SWAP(*pLeft, *pRight, tExprNode *);
S
slguan 已提交
84
    }
H
hzcheng 已提交
85 86

    switch (*optr) {
87
      case TSDB_RELATION_GREATER:
H
hzcheng 已提交
88 89 90
        (*optr) = TSDB_RELATION_LESS;
        break;
      case TSDB_RELATION_LESS:
91
        (*optr) = TSDB_RELATION_GREATER;
H
hzcheng 已提交
92
        break;
93
      case TSDB_RELATION_GREATER_EQUAL:
H
hzcheng 已提交
94 95 96
        (*optr) = TSDB_RELATION_LESS_EQUAL;
        break;
      case TSDB_RELATION_LESS_EQUAL:
97
        (*optr) = TSDB_RELATION_GREATER_EQUAL;
H
hzcheng 已提交
98 99 100 101 102 103 104
        break;
      default:;
        // for other type of operations, do nothing
    }
  }
}

H
hjxilinx 已提交
105
static tExprNode *tExprNodeCreate(SSchema *pSchema, int32_t numOfCols, SSQLToken *pToken) {
H
hzcheng 已提交
106 107 108 109 110 111
  /* if the token is not a value, return false */
  if (pToken->type == TK_RP || (pToken->type != TK_INTEGER && pToken->type != TK_FLOAT && pToken->type != TK_ID &&
                                pToken->type != TK_TBNAME && pToken->type != TK_STRING && pToken->type != TK_BOOL)) {
    return NULL;
  }

H
hjxilinx 已提交
112 113
  size_t nodeSize = sizeof(tExprNode);
  tExprNode *pNode = NULL;
H
hzcheng 已提交
114 115

  if (pToken->type == TK_ID || pToken->type == TK_TBNAME) {
H
hjxilinx 已提交
116
    int32_t i = 0;
H
hzcheng 已提交
117 118
    if (pToken->type == TK_ID) {
      do {
119 120
        SSQLToken tableToken = {0};
        extractTableNameFromToken(pToken, &tableToken);
121

H
hzcheng 已提交
122 123 124 125
        size_t len = strlen(pSchema[i].name);
        if (strncmp(pToken->z, pSchema[i].name, pToken->n) == 0 && pToken->n == len) break;
      } while (++i < numOfCols);

S
slguan 已提交
126
      if (i == numOfCols) {  // column name is not valid, parse the expression failed
H
hzcheng 已提交
127 128 129 130
        return NULL;
      }
    }

S
slguan 已提交
131
    nodeSize += sizeof(SSchema);
H
hzcheng 已提交
132

S
slguan 已提交
133
    pNode = calloc(1, nodeSize);
H
hjxilinx 已提交
134
    pNode->pSchema = (struct SSchema *)((char *)pNode + sizeof(tExprNode));
H
hzcheng 已提交
135 136 137
    pNode->nodeType = TSQL_NODE_COL;

    if (pToken->type == TK_ID) {
S
slguan 已提交
138
      memcpy(pNode->pSchema, &pSchema[i], sizeof(SSchema));
H
hzcheng 已提交
139 140
    } else {
      pNode->pSchema->type = TSDB_DATA_TYPE_BINARY;
S
slguan 已提交
141
      pNode->pSchema->bytes = TSDB_TABLE_NAME_LEN;
H
hzcheng 已提交
142 143 144 145 146 147
      strcpy(pNode->pSchema->name, TSQL_TBNAME_L);
      pNode->pSchema->colId = -1;
    }

  } else {
    nodeSize += sizeof(tVariant);
S
slguan 已提交
148
    pNode = calloc(1, nodeSize);
H
hjxilinx 已提交
149
    pNode->pVal = (tVariant *)((char *)pNode + sizeof(tExprNode));
H
hzcheng 已提交
150 151 152 153 154 155 156 157 158

    toTSDBType(pToken->type);
    tVariantCreate(pNode->pVal, pToken);
    pNode->nodeType = TSQL_NODE_VALUE;
  }

  return pNode;
}

H
hjxilinx 已提交
159
uint8_t getBinaryExprOptr(SSQLToken *pToken) {
H
hzcheng 已提交
160 161 162 163 164 165
  switch (pToken->type) {
    case TK_LT:
      return TSDB_RELATION_LESS;
    case TK_LE:
      return TSDB_RELATION_LESS_EQUAL;
    case TK_GT:
166
      return TSDB_RELATION_GREATER;
H
hzcheng 已提交
167
    case TK_GE:
168
      return TSDB_RELATION_GREATER_EQUAL;
H
hzcheng 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    case TK_NE:
      return TSDB_RELATION_NOT_EQUAL;
    case TK_AND:
      return TSDB_RELATION_AND;
    case TK_OR:
      return TSDB_RELATION_OR;
    case TK_EQ:
      return TSDB_RELATION_EQUAL;
    case TK_PLUS:
      return TSDB_BINARY_OP_ADD;
    case TK_MINUS:
      return TSDB_BINARY_OP_SUBTRACT;
    case TK_STAR:
      return TSDB_BINARY_OP_MULTIPLY;
    case TK_SLASH:
H
hjxilinx 已提交
184
    case TK_DIVIDE:
H
hzcheng 已提交
185 186 187 188 189 190 191 192 193 194
      return TSDB_BINARY_OP_DIVIDE;
    case TK_REM:
      return TSDB_BINARY_OP_REMAINDER;
    case TK_LIKE:
      return TSDB_RELATION_LIKE;
    default: { return 0; }
  }
}

// previous generated expr is reduced as the left child
H
hjxilinx 已提交
195
static tExprNode *parseRemainStr(char *pstr, tExprNode *pExpr, SSchema *pSchema, int32_t optr,
H
hzcheng 已提交
196 197
                                      int32_t numOfCols, int32_t *i) {
  // set the previous generated node as the left child of new root
H
[td-32]  
hjxilinx 已提交
198
  pExpr->nodeType = TSQL_NODE_EXPR;
H
hzcheng 已提交
199 200

  // remain is the right child
H
hjxilinx 已提交
201
  tExprNode *pRight = createSyntaxTree(pSchema, numOfCols, pstr, i);
H
[td-32]  
hjxilinx 已提交
202 203
  if (pRight == NULL || (pRight->nodeType == TSQL_NODE_COL && pExpr->nodeType != TSQL_NODE_VALUE) ||
      (pExpr->nodeType == TSQL_NODE_VALUE && pRight->nodeType != TSQL_NODE_COL)) {
H
hjxilinx 已提交
204 205
    tExprNodeDestroy(pExpr, NULL);
    tExprNodeDestroy(pRight, NULL);
H
hzcheng 已提交
206 207 208
    return NULL;
  }

H
hjxilinx 已提交
209
  tExprNode *pNewExpr = (tExprNode *)calloc(1, sizeof(tExprNode));
H
hzcheng 已提交
210
  uint8_t         k = optr;
H
[td-32]  
hjxilinx 已提交
211 212 213 214
  reviseBinaryExprIfNecessary(&pExpr, &pRight, &k);
  pNewExpr->_node.pLeft = pExpr;
  pNewExpr->_node.pRight = pRight;
  pNewExpr->_node.optr = k;
H
hzcheng 已提交
215

H
[td-32]  
hjxilinx 已提交
216 217
  pNewExpr->_node.hasPK = isQueryOnPrimaryKey(pSchema[0].name, pExpr, pRight);
  pNewExpr->nodeType = TSQL_NODE_EXPR;
H
hzcheng 已提交
218

H
[td-32]  
hjxilinx 已提交
219
  return pNewExpr;
H
hzcheng 已提交
220 221
}

H
hjxilinx 已提交
222
uint8_t isQueryOnPrimaryKey(const char *primaryColumnName, const tExprNode *pLeft, const tExprNode *pRight) {
H
hzcheng 已提交
223 224 225 226
  if (pLeft->nodeType == TSQL_NODE_COL) {
    // if left node is the primary column,return true
    return (strcmp(primaryColumnName, pLeft->pSchema->name) == 0) ? 1 : 0;
  } else {
S
slguan 已提交
227
    // if any children have query on primary key, their parents are also keep this value
H
[td-32]  
hjxilinx 已提交
228 229
    return ((pLeft->nodeType == TSQL_NODE_EXPR && pLeft->_node.hasPK == 1) ||
            (pRight->nodeType == TSQL_NODE_EXPR && pRight->_node.hasPK == 1)) == true
H
hzcheng 已提交
230 231 232 233 234
               ? 1
               : 0;
  }
}

H
hjxilinx 已提交
235
static tExprNode *createSyntaxTree(SSchema *pSchema, int32_t numOfCols, char *str, int32_t *i) {
H
hjxilinx 已提交
236
  SSQLToken t0 = tStrGetToken(str, i, false, 0, NULL);
H
hzcheng 已提交
237 238 239 240
  if (t0.n == 0) {
    return NULL;
  }

H
hjxilinx 已提交
241
  tExprNode *pLeft = NULL;
H
hzcheng 已提交
242 243 244 245 246 247
  if (t0.type == TK_LP) {  // start new left child branch
    pLeft = createSyntaxTree(pSchema, numOfCols, str, i);
  } else {
    if (t0.type == TK_RP) {
      return NULL;
    }
248
    
H
hjxilinx 已提交
249
    pLeft = tExprNodeCreate(pSchema, numOfCols, &t0);
H
hzcheng 已提交
250 251 252 253 254 255
  }

  if (pLeft == NULL) {
    return NULL;
  }

S
slguan 已提交
256
  t0 = tStrGetToken(str, i, false, 0, NULL);
H
hzcheng 已提交
257
  if (t0.n == 0 || t0.type == TK_RP) {
H
hjxilinx 已提交
258
    if (pLeft->nodeType != TSQL_NODE_EXPR) {  // if left is not the expr, it is not a legal expr
H
hjxilinx 已提交
259
      tExprNodeDestroy(pLeft, NULL);
H
hzcheng 已提交
260 261 262 263 264 265 266
      return NULL;
    }

    return pLeft;
  }

  // get the operator of expr
267
  uint8_t optr = getBinaryExprOptr(&t0);
S
slguan 已提交
268
  if (optr == 0) {
S
slguan 已提交
269
    uError("not support binary operator:%d", t0.type);
H
hjxilinx 已提交
270
    tExprNodeDestroy(pLeft, NULL);
H
hzcheng 已提交
271 272 273 274
    return NULL;
  }

  assert(pLeft != NULL);
H
hjxilinx 已提交
275
  tExprNode *pRight = NULL;
H
hzcheng 已提交
276 277 278 279 280 281 282 283 284 285

  if (t0.type == TK_AND || t0.type == TK_OR || t0.type == TK_LP) {
    pRight = createSyntaxTree(pSchema, numOfCols, str, i);
  } else {
    /*
     * In case that pLeft is a field identification,
     * we parse the value in expression according to queried field type,
     * if we do not get the information, in case of value of field presented first,
     * we revised the value after the binary expression is completed.
     */
S
slguan 已提交
286
    t0 = tStrGetToken(str, i, true, 0, NULL);
H
hzcheng 已提交
287
    if (t0.n == 0) {
H
hjxilinx 已提交
288
      tExprNodeDestroy(pLeft, NULL);  // illegal expression
H
hzcheng 已提交
289 290 291 292 293 294
      return NULL;
    }

    if (t0.type == TK_LP) {
      pRight = createSyntaxTree(pSchema, numOfCols, str, i);
    } else {
H
hjxilinx 已提交
295
      pRight = tExprNodeCreate(pSchema, numOfCols, &t0);
H
hzcheng 已提交
296 297 298 299
    }
  }

  if (pRight == NULL) {
H
hjxilinx 已提交
300
    tExprNodeDestroy(pLeft, NULL);
H
hzcheng 已提交
301 302 303 304
    return NULL;
  }

  /* create binary expr as the child of new parent node */
H
hjxilinx 已提交
305
  tExprNode *pExpr = (tExprNode *)calloc(1, sizeof(tExprNode));
H
hzcheng 已提交
306 307
  reviseBinaryExprIfNecessary(&pLeft, &pRight, &optr);

H
hjxilinx 已提交
308 309 310 311
  pExpr->_node.hasPK = isQueryOnPrimaryKey(pSchema[0].name, pLeft, pRight);
  pExpr->_node.pLeft = pLeft;
  pExpr->_node.pRight = pRight;
  pExpr->_node.optr = optr;
H
hzcheng 已提交
312

S
slguan 已提交
313
  t0 = tStrGetToken(str, i, true, 0, NULL);
H
hzcheng 已提交
314 315

  if (t0.n == 0 || t0.type == TK_RP) {
H
hjxilinx 已提交
316 317
    pExpr->nodeType = TSQL_NODE_EXPR;
    return pExpr;
H
hzcheng 已提交
318
  } else {
319
    uint8_t localOptr = getBinaryExprOptr(&t0);
S
slguan 已提交
320
    if (localOptr == 0) {
S
slguan 已提交
321
      uError("not support binary operator:%d", t0.type);
H
hjxilinx 已提交
322
      free(pExpr);
H
hzcheng 已提交
323 324 325
      return NULL;
    }

H
hjxilinx 已提交
326
    return parseRemainStr(str, pExpr, pSchema, localOptr, numOfCols, i);
H
hzcheng 已提交
327 328 329
  }
}

H
hjxilinx 已提交
330
void tSQLBinaryExprFromString(tExprNode **pExpr, SSchema *pSchema, int32_t numOfCols, char *src, int32_t len) {
H
hzcheng 已提交
331
  *pExpr = NULL;
H
hjxilinx 已提交
332
  
H
hzcheng 已提交
333 334 335 336
  if (len <= 0 || src == NULL || pSchema == NULL || numOfCols <= 0) {
    return;
  }

H
hjxilinx 已提交
337 338
  int32_t pos = 0;
  
H
[td-32]  
hjxilinx 已提交
339 340 341
  *pExpr = createSyntaxTree(pSchema, numOfCols, src, &pos);
  if (*pExpr != NULL) {
    assert((*pExpr)->nodeType == TSQL_NODE_EXPR);
H
hzcheng 已提交
342 343 344
  }
}

H
hjxilinx 已提交
345
int32_t tSQLBinaryExprToStringImpl(tExprNode *pNode, char *dst, uint8_t type) {
H
hzcheng 已提交
346 347 348
  int32_t len = 0;
  if (type == TSQL_NODE_EXPR) {
    *dst = '(';
H
[td-32]  
hjxilinx 已提交
349
    tSQLBinaryExprToString(pNode, dst + 1, &len);
H
hzcheng 已提交
350 351 352 353 354 355 356 357 358 359
    len += 2;
    *(dst + len - 1) = ')';
  } else if (type == TSQL_NODE_COL) {
    len = sprintf(dst, "%s", pNode->pSchema->name);
  } else {
    len = tVariantToString(pNode->pVal, dst);
  }
  return len;
}

S
slguan 已提交
360
// TODO REFACTOR WITH SQL PARSER
H
hzcheng 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
static char *tSQLOptrToString(uint8_t optr, char *dst) {
  switch (optr) {
    case TSDB_RELATION_LESS: {
      *dst = '<';
      dst += 1;
      break;
    }
    case TSDB_RELATION_LESS_EQUAL: {
      *dst = '<';
      *(dst + 1) = '=';
      dst += 2;
      break;
    }
    case TSDB_RELATION_EQUAL: {
      *dst = '=';
      dst += 1;
      break;
    }
379
    case TSDB_RELATION_GREATER: {
H
hzcheng 已提交
380 381 382 383
      *dst = '>';
      dst += 1;
      break;
    }
384
    case TSDB_RELATION_GREATER_EQUAL: {
H
hzcheng 已提交
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
      *dst = '>';
      *(dst + 1) = '=';
      dst += 2;
      break;
    }
    case TSDB_RELATION_NOT_EQUAL: {
      *dst = '<';
      *(dst + 1) = '>';
      dst += 2;
      break;
    }
    case TSDB_RELATION_OR: {
      memcpy(dst, "or", 2);
      dst += 2;
      break;
    }
    case TSDB_RELATION_AND: {
      memcpy(dst, "and", 3);
      dst += 3;
      break;
    }
    default:;
  }
  return dst;
}

H
hjxilinx 已提交
411
void tSQLBinaryExprToString(tExprNode *pExpr, char *dst, int32_t *len) {
H
hzcheng 已提交
412 413 414
  if (pExpr == NULL) {
    *dst = 0;
    *len = 0;
415
    return;
H
hzcheng 已提交
416 417
  }

H
[td-32]  
hjxilinx 已提交
418
  int32_t lhs = tSQLBinaryExprToStringImpl(pExpr->_node.pLeft, dst, pExpr->_node.pLeft->nodeType);
H
hzcheng 已提交
419 420 421
  dst += lhs;
  *len = lhs;

H
[td-32]  
hjxilinx 已提交
422
  char *start = tSQLOptrToString(pExpr->_node.optr, dst);
H
hzcheng 已提交
423 424
  *len += (start - dst);

H
[td-32]  
hjxilinx 已提交
425
  *len += tSQLBinaryExprToStringImpl(pExpr->_node.pRight, start, pExpr->_node.pRight->nodeType);
H
hzcheng 已提交
426 427
}

H
hjxilinx 已提交
428
static void UNUSED_FUNC destroySyntaxTree(tExprNode *pNode) { tExprNodeDestroy(pNode, NULL); }
H
hzcheng 已提交
429

430
void tExprNodeDestroy(tExprNode *pNode, void (*fp)(void *)) {
S
slguan 已提交
431 432 433
  if (pNode == NULL) {
    return;
  }
H
hzcheng 已提交
434 435

  if (pNode->nodeType == TSQL_NODE_EXPR) {
436
    tExprTreeDestroy(&pNode, fp);
H
hzcheng 已提交
437 438
  } else if (pNode->nodeType == TSQL_NODE_VALUE) {
    tVariantDestroy(pNode->pVal);
439 440
  } else if (pNode->nodeType == TSQL_NODE_COL) {
    free(pNode->pSchema);
H
hzcheng 已提交
441 442 443 444 445
  }

  free(pNode);
}

446
void tExprTreeDestroy(tExprNode **pExpr, void (*fp)(void *)) {
S
slguan 已提交
447 448
  if (*pExpr == NULL) {
    return;
H
hzcheng 已提交
449
  }
450 451 452 453 454 455 456 457 458 459 460 461 462
  
  if ((*pExpr)->nodeType == TSQL_NODE_EXPR) {
    tExprTreeDestroy(&(*pExpr)->_node.pLeft, fp);
    tExprTreeDestroy(&(*pExpr)->_node.pRight, fp);
  
    if (fp != NULL) {
      fp((*pExpr)->_node.info);
    }
  } else if ((*pExpr)->nodeType == TSQL_NODE_VALUE) {
    tVariantDestroy((*pExpr)->pVal);
    free((*pExpr)->pVal);
  } else if ((*pExpr)->nodeType == TSQL_NODE_COL) {
    free((*pExpr)->pSchema);
H
hzcheng 已提交
463 464
  }

S
slguan 已提交
465 466
  free(*pExpr);
  *pExpr = NULL;
H
hzcheng 已提交
467 468
}

469
typedef struct {
H
hjxilinx 已提交
470
  char*    v;
471 472 473 474 475 476 477 478
  int32_t  optr;
} SEndPoint;

typedef struct {
  SEndPoint* start;
  SEndPoint* end;
} SQueryCond;

479
//static void setInitialValueForRangeQueryCondition(tSKipListQueryCond *q, int8_t type) {
480
//  q->lowerBndRelOptr = TSDB_RELATION_GREATER;
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
//  q->upperBndRelOptr = TSDB_RELATION_LESS;
//
//  switch (type) {
//    case TSDB_DATA_TYPE_BOOL:
//    case TSDB_DATA_TYPE_TINYINT:
//    case TSDB_DATA_TYPE_SMALLINT:
//    case TSDB_DATA_TYPE_INT:
//    case TSDB_DATA_TYPE_BIGINT: {
//      q->upperBnd.nType = TSDB_DATA_TYPE_BIGINT;
//      q->lowerBnd.nType = TSDB_DATA_TYPE_BIGINT;
//
//      q->upperBnd.i64Key = INT64_MAX;
//      q->lowerBnd.i64Key = INT64_MIN;
//      break;
//    };
//    case TSDB_DATA_TYPE_FLOAT:
//    case TSDB_DATA_TYPE_DOUBLE: {
//      q->upperBnd.nType = TSDB_DATA_TYPE_DOUBLE;
//      q->lowerBnd.nType = TSDB_DATA_TYPE_DOUBLE;
//      q->upperBnd.dKey = DBL_MAX;
//      q->lowerBnd.dKey = -DBL_MIN;
//      break;
//    };
//    case TSDB_DATA_TYPE_NCHAR:
//    case TSDB_DATA_TYPE_BINARY: {
//      q->upperBnd.nType = type;
//      q->upperBnd.pz = NULL;
//      q->upperBnd.nLen = -1;
//
//      q->lowerBnd.nType = type;
//      q->lowerBnd.pz = NULL;
//      q->lowerBnd.nLen = -1;
//    }
//  }
//}

517 518 519 520 521 522
// todo check for malloc failure
static int32_t setQueryCond(tQueryInfo *queryColInfo, SQueryCond* pCond) {
  int32_t optr = queryColInfo->optr;
  
  if (optr == TSDB_RELATION_GREATER || optr == TSDB_RELATION_GREATER_EQUAL ||
      optr == TSDB_RELATION_EQUAL || optr == TSDB_RELATION_NOT_EQUAL) {
H
hjxilinx 已提交
523
    pCond->start = calloc(1, sizeof(SEndPoint));
524
    pCond->start->optr = queryColInfo->optr;
H
hjxilinx 已提交
525
    pCond->start->v = queryColInfo->q;
526
  } else if (optr == TSDB_RELATION_LESS || optr == TSDB_RELATION_LESS_EQUAL) {
H
hjxilinx 已提交
527
    pCond->end = calloc(1, sizeof(SEndPoint));
528
    pCond->end->optr = queryColInfo->optr;
H
hjxilinx 已提交
529
    pCond->end->v = queryColInfo->q;
weixin_48148422's avatar
weixin_48148422 已提交
530 531
  } else if (optr == TSDB_RELATION_IN) {
    printf("relation is in\n");
H
hjxilinx 已提交
532
    assert(0);
weixin_48148422's avatar
weixin_48148422 已提交
533 534
  } else if (optr == TSDB_RELATION_LIKE) {
    printf("relation is like\n");
H
hjxilinx 已提交
535
    assert(0);
536 537 538 539 540
  }
  
  return TSDB_CODE_SUCCESS;
}

weixin_48148422's avatar
weixin_48148422 已提交
541
static void tQueryIndexColumn(SSkipList* pSkipList, tQueryInfo* pQueryInfo, SArray* result) {
542
  SSkipListIterator* iter = NULL;
H
hjxilinx 已提交
543
  
H
hjxilinx 已提交
544
  SQueryCond cond = {0};
H
hjxilinx 已提交
545 546 547
  if (setQueryCond(pQueryInfo, &cond) != TSDB_CODE_SUCCESS) {
    //todo handle error
  }
weixin_48148422's avatar
weixin_48148422 已提交
548 549

  if (cond.start != NULL) {
H
hjxilinx 已提交
550
    iter = tSkipListCreateIterFromVal(pSkipList, (char*) cond.start->v, pSkipList->keyInfo.type, TSDB_ORDER_ASC);
551
  } else {
H
hjxilinx 已提交
552
    iter = tSkipListCreateIterFromVal(pSkipList, (char*) cond.end->v, pSkipList->keyInfo.type, TSDB_ORDER_DESC);
553 554
  }
  
weixin_48148422's avatar
weixin_48148422 已提交
555 556
  if (cond.start != NULL) {
    int32_t optr = cond.start->optr;
557
    
H
hjxilinx 已提交
558
    if (optr == TSDB_RELATION_EQUAL) {   // equals
559 560 561
      while(tSkipListIterNext(iter)) {
        SSkipListNode* pNode = tSkipListIterGet(iter);

H
hjxilinx 已提交
562
        int32_t ret = pQueryInfo->compare(SL_GET_NODE_KEY(pSkipList, pNode), cond.start->v);
H
hjxilinx 已提交
563
        if (ret != 0) {
564 565
          break;
        }
H
hjxilinx 已提交
566 567
        
        taosArrayPush(result, SL_GET_NODE_DATA(pNode));
568
      }
H
hjxilinx 已提交
569
    } else if (optr == TSDB_RELATION_GREATER || optr == TSDB_RELATION_GREATER_EQUAL) { // greater equal
570 571 572 573 574 575 576
      bool comp = true;
      int32_t ret = 0;
      
      while(tSkipListIterNext(iter)) {
        SSkipListNode* pNode = tSkipListIterGet(iter);
    
        if (comp) {
H
hjxilinx 已提交
577
          ret = pQueryInfo->compare(SL_GET_NODE_KEY(pSkipList, pNode), cond.start->v);
578
          assert(ret >= 0);
579 580 581 582 583 584 585 586 587
        }
        
        if (ret == 0 && optr == TSDB_RELATION_GREATER) {
          continue;
        } else {
          taosArrayPush(result, SL_GET_NODE_DATA(pNode));
          comp = false;
        }
      }
H
hjxilinx 已提交
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
    } else if (optr == TSDB_RELATION_NOT_EQUAL) {   // not equal
      bool comp = true;
      
      while(tSkipListIterNext(iter)) {
        SSkipListNode* pNode = tSkipListIterGet(iter);
        comp = comp && (pQueryInfo->compare(SL_GET_NODE_KEY(pSkipList, pNode), cond.start->v) == 0);
        if (comp) {
          continue;
        }
        
        taosArrayPush(result, SL_GET_NODE_DATA(pNode));
      }
      
      tSkipListDestroyIter(iter);
  
      comp = true;
      iter = tSkipListCreateIterFromVal(pSkipList, (char*) cond.start->v, pSkipList->keyInfo.type, TSDB_ORDER_DESC);
      while(tSkipListIterNext(iter)) {
        SSkipListNode* pNode = tSkipListIterGet(iter);
        comp = comp && (pQueryInfo->compare(SL_GET_NODE_KEY(pSkipList, pNode), cond.start->v) == 0);
        if (comp) {
          continue;
        }
  
        taosArrayPush(result, SL_GET_NODE_DATA(pNode));
      }
  
615 616 617 618
    } else {
      assert(0);
    }
  } else {
weixin_48148422's avatar
weixin_48148422 已提交
619
    int32_t optr = cond.end->optr;
620 621 622 623 624 625 626 627 628
    
    if (optr == TSDB_RELATION_LESS || optr == TSDB_RELATION_LESS_EQUAL) {
      bool comp = true;
      int32_t ret = 0;
      
      while(tSkipListIterNext(iter)) {
        SSkipListNode* pNode = tSkipListIterGet(iter);
      
        if (comp) {
H
hjxilinx 已提交
629
          ret = pQueryInfo->compare(SL_GET_NODE_KEY(pSkipList, pNode), cond.end->v);
630
          assert(ret <= 0);
631 632 633 634 635 636 637 638 639 640 641 642
        }
        
        if (ret == 0 && optr == TSDB_RELATION_LESS) {
          continue;
        } else {
          taosArrayPush(result, SL_GET_NODE_DATA(pNode));
          comp = false;  // no need to compare anymore
        }
      }
    }
  }
}
H
hzcheng 已提交
643

H
hjxilinx 已提交
644
int32_t merge(SArray *pLeft, SArray *pRight, SArray *pFinalRes) {
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
//  assert(pFinalRes->pRes == 0);
//
//  pFinalRes->pRes = calloc((size_t)(pLeft->num + pRight->num), POINTER_BYTES);
//  pFinalRes->num = 0;
//
//  // sort according to address
//  tSkipListNode **pLeftNodes = (tSkipListNode **)pLeft->pRes;
//  qsort(pLeftNodes, pLeft->num, sizeof(pLeft->pRes[0]), compareByAddr);
//
//  tSkipListNode **pRightNodes = (tSkipListNode **)pRight->pRes;
//  qsort(pRightNodes, pRight->num, sizeof(pRight->pRes[0]), compareByAddr);
//
//  int32_t i = 0, j = 0;
//
//  // merge two sorted arrays in O(n) time
//  while (i < pLeft->num && j < pRight->num) {
//    int64_t ret = (int64_t)pLeftNodes[i] - (int64_t)pRightNodes[j];
//
//    if (ret < 0) {
//      pFinalRes->pRes[pFinalRes->num++] = pLeftNodes[i++];
//    } else if (ret > 0) {
//      pFinalRes->pRes[pFinalRes->num++] = pRightNodes[j++];
//    } else {  // pNode->key > pkey[i]
//      pFinalRes->pRes[pFinalRes->num++] = pRightNodes[j++];
//      i++;
//    }
//  }
//
//  while (i < pLeft->num) {
//    pFinalRes->pRes[pFinalRes->num++] = pLeftNodes[i++];
//  }
//
//  while (j < pRight->num) {
//    pFinalRes->pRes[pFinalRes->num++] = pRightNodes[j++];
//  }
//
//  return pFinalRes->num;
H
hjxilinx 已提交
682
  return 0;
H
hzcheng 已提交
683 684
}

H
hjxilinx 已提交
685
int32_t intersect(SArray *pLeft, SArray *pRight, SArray *pFinalRes) {
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
//  int64_t num = MIN(pLeft->num, pRight->num);
//
//  assert(pFinalRes->pRes == 0);
//
//  pFinalRes->pRes = calloc(num, POINTER_BYTES);
//  pFinalRes->num = 0;
//
//  // sort according to address
//  tSkipListNode **pLeftNodes = (tSkipListNode **)pLeft->pRes;
//  qsort(pLeftNodes, pLeft->num, sizeof(pLeft->pRes[0]), compareByAddr);
//
//  tSkipListNode **pRightNodes = (tSkipListNode **)pRight->pRes;
//  qsort(pRightNodes, pRight->num, sizeof(pRight->pRes[0]), compareByAddr);
//
//  int32_t i = 0, j = 0;
//  // merge two sorted arrays in O(n) time
//  while (i < pLeft->num && j < pRight->num) {
//    int64_t ret = (int64_t)pLeftNodes[i] - (int64_t)pRightNodes[j];
//
//    if (ret < 0) {
//      i++;
//    } else if (ret > 0) {
//      j++;
//    } else {  // pNode->key > pkey[i]
//      pFinalRes->pRes[pFinalRes->num++] = pRightNodes[j];
//      i++;
//      j++;
//    }
//  }
//
//  return pFinalRes->num;
H
hjxilinx 已提交
717
 return 0;
H
hzcheng 已提交
718 719 720
}

/*
H
hjxilinx 已提交
721
 * traverse the result and apply the function to each item to check if the item is qualified or not
H
hzcheng 已提交
722
 */
H
hjxilinx 已提交
723 724 725 726 727 728 729 730 731 732 733 734
static void tArrayTraverse(tExprNode *pExpr, __result_filter_fn_t fp, SArray *pResult) {
  assert(pExpr->_node.pLeft->nodeType == TSQL_NODE_COL && pExpr->_node.pRight->nodeType == TSQL_NODE_VALUE && fp != NULL);

  //  scan the result array list and check for each item in the list
  for (int32_t i = 0; i < taosArrayGetSize(pResult); ++i) {
    void* item = taosArrayGet(pResult, i);
    if (fp(item, pExpr->_node.info)) {
      i++;
    } else {
      taosArrayRemove(pResult, i);
    }
  }
H
hzcheng 已提交
735 736
}

H
hjxilinx 已提交
737
static bool filterItem(tExprNode *pExpr, const void *pItem, SExprTraverseSupp *param) {
H
hjxilinx 已提交
738 739
  tExprNode *pLeft = pExpr->_node.pLeft;
  tExprNode *pRight = pExpr->_node.pRight;
S
slguan 已提交
740

741
  //non-leaf nodes, recursively traverse the expression tree in the post-root order
S
slguan 已提交
742
  if (pLeft->nodeType == TSQL_NODE_EXPR && pRight->nodeType == TSQL_NODE_EXPR) {
H
[td-32]  
hjxilinx 已提交
743 744
    if (pExpr->_node.optr == TSDB_RELATION_OR) {  // or
      if (filterItem(pLeft, pItem, param)) {
S
slguan 已提交
745 746 747 748
        return true;
      }

      // left child does not satisfy the query condition, try right child
H
[td-32]  
hjxilinx 已提交
749
      return filterItem(pRight, pItem, param);
S
slguan 已提交
750
    } else {  // and
H
[td-32]  
hjxilinx 已提交
751
      if (!filterItem(pLeft, pItem, param)) {
S
slguan 已提交
752 753 754
        return false;
      }

H
[td-32]  
hjxilinx 已提交
755
      return filterItem(pRight, pItem, param);
S
slguan 已提交
756 757 758 759 760 761 762
    }
  }

  // handle the leaf node
  assert(pLeft->nodeType == TSQL_NODE_COL && pRight->nodeType == TSQL_NODE_VALUE);
  param->setupInfoFn(pExpr, param->pExtInfo);

763
  return param->nodeFilterFn(pItem, pExpr->_node.info);
S
slguan 已提交
764 765 766 767 768 769 770 771 772 773 774 775
}

/**
 * Apply the filter expression on non-indexed tag columns to each element in the result list, which is generated
 * by filtering on indexed tag column. So the whole result set only needs to be iterated once to generate
 * result that is satisfied to the filter expression, no matter how the filter expression consisting of.
 *
 * @param pExpr     filter expression on non-indexed tag columns.
 * @param pResult   results from filter on the indexed tag column, which is usually the first tag column
 * @param pSchema   tag schemas
 * @param fp        filter callback function
 */
H
hjxilinx 已提交
776
static void exprTreeTraverseImpl(tExprNode *pExpr, SArray *pResult, SExprTraverseSupp *param) {
777 778 779 780 781
  size_t size = taosArrayGetSize(pResult);
  
  SArray* array = taosArrayInit(size, POINTER_BYTES);
  for (int32_t i = 0; i < size; ++i) {
    void *pItem = taosArrayGetP(pResult, i);
S
slguan 已提交
782 783

    if (filterItem(pExpr, pItem, param)) {
784
      taosArrayPush(array, &pItem);
S
slguan 已提交
785 786
    }
  }
787 788
  
  taosArrayCopy(pResult, array);
S
slguan 已提交
789 790
}

791

H
hjxilinx 已提交
792
static void tSQLBinaryTraverseOnSkipList(tExprNode *pExpr, SArray *pResult, SSkipList *pSkipList, SExprTraverseSupp *param ) {
793 794 795 796 797 798 799 800
  SSkipListIterator* iter = tSkipListCreateIter(pSkipList);

  while (tSkipListIterNext(iter)) {
    SSkipListNode *pNode = tSkipListIterGet(iter);
    if (filterItem(pExpr, pNode, param)) {
      taosArrayPush(pResult, SL_GET_NODE_DATA(pNode));
    }
  }
weixin_48148422's avatar
weixin_48148422 已提交
801
  tSkipListDestroyIter(iter);
802
}
S
slguan 已提交
803

804
static void tQueryIndexlessColumn(SSkipList* pSkipList, tQueryInfo* pQueryInfo, SArray* res, __result_filter_fn_t filterFp) {
weixin_48148422's avatar
weixin_48148422 已提交
805 806 807 808 809 810
  SSkipListIterator* iter = tSkipListCreateIter(pSkipList);

  while (tSkipListIterNext(iter)) {
    bool addToResult = false;

    SSkipListNode *pNode = tSkipListIterGet(iter);
811 812 813 814 815 816
    char *         pData = SL_GET_NODE_DATA(pNode);

    // todo refactor:
    tstr *name = ((STableIndexElem *)pData)->pTable->name;
    //    char* name = NULL;
//        tsdbGetTableName(pQueryInfo->, pTable, &name);
weixin_48148422's avatar
weixin_48148422 已提交
817

H
hjxilinx 已提交
818
    // todo speed up by using hash
weixin_48148422's avatar
weixin_48148422 已提交
819 820
    if (pQueryInfo->colIndex == TSDB_TBNAME_COLUMN_INDEX) {
      if (pQueryInfo->optr == TSDB_RELATION_IN) {
H
hjxilinx 已提交
821
        addToResult = pQueryInfo->compare(name, pQueryInfo->q);
822
      } else if (pQueryInfo->optr == TSDB_RELATION_LIKE) {
H
hjxilinx 已提交
823
        addToResult = !pQueryInfo->compare(name, pQueryInfo->q);
weixin_48148422's avatar
weixin_48148422 已提交
824 825
      }
    } else {
826
      addToResult = filterFp(pNode, pQueryInfo);
weixin_48148422's avatar
weixin_48148422 已提交
827 828 829
    }

    if (addToResult) {
830
      taosArrayPush(res, pData);
weixin_48148422's avatar
weixin_48148422 已提交
831 832
    }
  }
weixin_48148422's avatar
weixin_48148422 已提交
833 834

  tSkipListDestroyIter(iter);
weixin_48148422's avatar
weixin_48148422 已提交
835 836 837 838
}



H
hzcheng 已提交
839
// post-root order traverse syntax tree
H
hjxilinx 已提交
840
void tExprTreeTraverse(tExprNode *pExpr, SSkipList *pSkipList, SArray *result, SExprTraverseSupp *param) {
841 842 843 844
  if (pExpr == NULL) {
    return;
  }

845
  tExprNode *pLeft  = pExpr->_node.pLeft;
H
hjxilinx 已提交
846
  tExprNode *pRight = pExpr->_node.pRight;
847

weixin_48148422's avatar
weixin_48148422 已提交
848 849 850
  // column project
  if (pLeft->nodeType != TSQL_NODE_EXPR && pRight->nodeType != TSQL_NODE_EXPR) {
    assert(pLeft->nodeType == TSQL_NODE_COL && pRight->nodeType == TSQL_NODE_VALUE);
851

weixin_48148422's avatar
weixin_48148422 已提交
852 853
    param->setupInfoFn(pExpr, param->pExtInfo);
    if (pSkipList == NULL) {
854
      tArrayTraverse(pExpr, param->nodeFilterFn, result);
weixin_48148422's avatar
weixin_48148422 已提交
855 856 857 858 859 860 861
      return;
    }

    tQueryInfo *pQueryInfo = pExpr->_node.info;
    if (pQueryInfo->colIndex == 0 && pQueryInfo->optr != TSDB_RELATION_LIKE) {
      tQueryIndexColumn(pSkipList, pQueryInfo, result);
    } else {
862
      tQueryIndexlessColumn(pSkipList, pQueryInfo, result, param->nodeFilterFn);
weixin_48148422's avatar
weixin_48148422 已提交
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
    }

    return;
  }

  // recursive traverse left child branch
  uint8_t weight = pLeft->_node.hasPK + pRight->_node.hasPK;

  if (weight == 0 ) {
    if (taosArrayGetSize(result) > 0 && pSkipList == NULL) {
    /**
     * Perform the filter operation based on the initial filter result, which is obtained from filtering from index.
     * Since no index presented, the filter operation is done by scan all elements in the result set.
     *
     * if the query is a high selectivity filter, only small portion of meters are retrieved.
     */
879
      exprTreeTraverseImpl(pExpr, result, param);
weixin_48148422's avatar
weixin_48148422 已提交
880
    } else {
881 882 883 884 885
      /**
       * apply the hierarchical expression to every node in skiplist for find the qualified nodes
       */
      assert(taosArrayGetSize(result) == 0);
      tSQLBinaryTraverseOnSkipList(pExpr, result, pSkipList, param);
weixin_48148422's avatar
weixin_48148422 已提交
886 887 888 889 890 891 892
    }
    return;
  }
  
  if (weight == 2 || (weight == 1 && pExpr->_node.optr == TSDB_RELATION_OR)) {
    SArray* rLeft  = taosArrayInit(10, POINTER_BYTES);
    SArray* rRight = taosArrayInit(10, POINTER_BYTES);
893

weixin_48148422's avatar
weixin_48148422 已提交
894 895
    tExprTreeTraverse(pLeft, pSkipList, rLeft, param);
    tExprTreeTraverse(pRight, pSkipList, rRight, param);
896

weixin_48148422's avatar
weixin_48148422 已提交
897 898 899 900
    if (pExpr->_node.optr == TSDB_RELATION_AND) {  // CROSS
      intersect(rLeft, rRight, result);
    } else if (pExpr->_node.optr == TSDB_RELATION_OR) {  // or
      merge(rLeft, rRight, result);
901
    } else {
weixin_48148422's avatar
weixin_48148422 已提交
902 903
      assert(false);
    }
904

weixin_48148422's avatar
weixin_48148422 已提交
905 906 907 908
    taosArrayDestroy(rLeft);
    taosArrayDestroy(rRight);
    return;
  }
909

weixin_48148422's avatar
weixin_48148422 已提交
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
  /*
    * (weight == 1 && pExpr->nSQLBinaryOptr == TSDB_RELATION_AND) is handled here
    *
    * first, we filter results based on the skiplist index, which is the initial filter stage,
    * then, we conduct the secondary filter operation based on the result from the initial filter stage.
    */
  assert(pExpr->_node.optr == TSDB_RELATION_AND);

  tExprNode *pFirst = NULL;
  tExprNode *pSecond = NULL;
  if (pLeft->_node.hasPK == 1) {
    pFirst = pLeft;
    pSecond = pRight;
  } else {
    pFirst = pRight;
    pSecond = pLeft;
  }
927

weixin_48148422's avatar
weixin_48148422 已提交
928
  assert(pFirst != pSecond && pFirst != NULL && pSecond != NULL);
929

weixin_48148422's avatar
weixin_48148422 已提交
930 931 932 933 934 935 936 937
  // we filter the result based on the skiplist index in the first place
  tExprTreeTraverse(pFirst, pSkipList, result, param);

  /*
    * recursively perform the filter operation based on the initial results,
    * So, we do not set the skip list index as a parameter
    */
  tExprTreeTraverse(pSecond, NULL, result, param);
938
}
H
hzcheng 已提交
939

940 941
void tExprTreeCalcTraverse(tExprNode *pExprs, int32_t numOfRows, char *pOutput, void *param, int32_t order,
                                char *(*getSourceDataBlock)(void *, const char*, int32_t)) {
H
hzcheng 已提交
942 943 944 945
  if (pExprs == NULL) {
    return;
  }

H
hjxilinx 已提交
946 947
  tExprNode *pLeft = pExprs->_node.pLeft;
  tExprNode *pRight = pExprs->_node.pRight;
H
hzcheng 已提交
948 949

  /* the left output has result from the left child syntax tree */
L
lihui 已提交
950
  char *pLeftOutput = (char*)malloc(sizeof(int64_t) * numOfRows);
H
hzcheng 已提交
951
  if (pLeft->nodeType == TSQL_NODE_EXPR) {
952
    tExprTreeCalcTraverse(pLeft, numOfRows, pLeftOutput, param, order, getSourceDataBlock);
H
hzcheng 已提交
953 954 955 956 957
  }

  /* the right output has result from the right child syntax tree */
  char *pRightOutput = malloc(sizeof(int64_t) * numOfRows);
  if (pRight->nodeType == TSQL_NODE_EXPR) {
958
    tExprTreeCalcTraverse(pRight, numOfRows, pRightOutput, param, order, getSourceDataBlock);
H
hzcheng 已提交
959 960 961
  }

  if (pLeft->nodeType == TSQL_NODE_EXPR) {
S
slguan 已提交
962 963 964 965 966
    if (pRight->nodeType == TSQL_NODE_EXPR) {
      /*
       * exprLeft + exprRight
       * the type of returned value of one expression is always double float precious
       */
H
[td-32]  
hjxilinx 已提交
967
      _bi_consumer_fn_t fp = tGetBiConsumerFn(TSDB_DATA_TYPE_DOUBLE, TSDB_DATA_TYPE_DOUBLE, pExprs->_node.optr);
H
hzcheng 已提交
968 969 970
      fp(pLeftOutput, pRightOutput, numOfRows, numOfRows, pOutput, order);

    } else if (pRight->nodeType == TSQL_NODE_COL) {  // exprLeft + columnRight
H
[td-32]  
hjxilinx 已提交
971
      _bi_consumer_fn_t fp = tGetBiConsumerFn(TSDB_DATA_TYPE_DOUBLE, pRight->pSchema->type, pExprs->_node.optr);
H
hzcheng 已提交
972
      // set input buffer
H
[td-32]  
hjxilinx 已提交
973
      char *pInputData = getSourceDataBlock(param, pRight->pSchema->name, pRight->pSchema->colId);
H
hzcheng 已提交
974 975 976
      fp(pLeftOutput, pInputData, numOfRows, numOfRows, pOutput, order);

    } else if (pRight->nodeType == TSQL_NODE_VALUE) {  // exprLeft + 12
H
[td-32]  
hjxilinx 已提交
977
      _bi_consumer_fn_t fp = tGetBiConsumerFn(TSDB_DATA_TYPE_DOUBLE, pRight->pVal->nType, pExprs->_node.optr);
H
hzcheng 已提交
978 979 980 981
      fp(pLeftOutput, &pRight->pVal->i64Key, numOfRows, 1, pOutput, order);
    }
  } else if (pLeft->nodeType == TSQL_NODE_COL) {
    // column data specified on left-hand-side
H
[td-32]  
hjxilinx 已提交
982
    char *pLeftInputData = getSourceDataBlock(param, pLeft->pSchema->name, pLeft->pSchema->colId);
H
hzcheng 已提交
983
    if (pRight->nodeType == TSQL_NODE_EXPR) {  // columnLeft + expr2
H
[td-32]  
hjxilinx 已提交
984
      _bi_consumer_fn_t fp = tGetBiConsumerFn(pLeft->pSchema->type, TSDB_DATA_TYPE_DOUBLE, pExprs->_node.optr);
H
hzcheng 已提交
985 986 987 988
      fp(pLeftInputData, pRightOutput, numOfRows, numOfRows, pOutput, order);

    } else if (pRight->nodeType == TSQL_NODE_COL) {  // columnLeft + columnRight
      // column data specified on right-hand-side
H
[td-32]  
hjxilinx 已提交
989
      char *pRightInputData = getSourceDataBlock(param, pRight->pSchema->name, pRight->pSchema->colId);
H
hzcheng 已提交
990

H
[td-32]  
hjxilinx 已提交
991
      _bi_consumer_fn_t fp = tGetBiConsumerFn(pLeft->pSchema->type, pRight->pSchema->type, pExprs->_node.optr);
H
hzcheng 已提交
992 993 994
      fp(pLeftInputData, pRightInputData, numOfRows, numOfRows, pOutput, order);

    } else if (pRight->nodeType == TSQL_NODE_VALUE) {  // columnLeft + 12
H
[td-32]  
hjxilinx 已提交
995
      _bi_consumer_fn_t fp = tGetBiConsumerFn(pLeft->pSchema->type, pRight->pVal->nType, pExprs->_node.optr);
H
hzcheng 已提交
996 997 998 999 1000
      fp(pLeftInputData, &pRight->pVal->i64Key, numOfRows, 1, pOutput, order);
    }
  } else {
    // column data specified on left-hand-side
    if (pRight->nodeType == TSQL_NODE_EXPR) {  // 12 + expr2
H
[td-32]  
hjxilinx 已提交
1001
      _bi_consumer_fn_t fp = tGetBiConsumerFn(pLeft->pVal->nType, TSDB_DATA_TYPE_DOUBLE, pExprs->_node.optr);
H
hzcheng 已提交
1002 1003 1004 1005
      fp(&pLeft->pVal->i64Key, pRightOutput, 1, numOfRows, pOutput, order);

    } else if (pRight->nodeType == TSQL_NODE_COL) {  // 12 + columnRight
      // column data specified on right-hand-side
1006 1007
      char *pRightInputData = getSourceDataBlock(param, pRight->pSchema->name, pRight->pSchema->colId);
      
H
[td-32]  
hjxilinx 已提交
1008
      _bi_consumer_fn_t fp = tGetBiConsumerFn(pLeft->pVal->nType, pRight->pSchema->type, pExprs->_node.optr);
H
hzcheng 已提交
1009 1010 1011
      fp(&pLeft->pVal->i64Key, pRightInputData, 1, numOfRows, pOutput, order);

    } else if (pRight->nodeType == TSQL_NODE_VALUE) {  // 12 + 12
H
[td-32]  
hjxilinx 已提交
1012
      _bi_consumer_fn_t fp = tGetBiConsumerFn(pLeft->pVal->nType, pRight->pVal->nType, pExprs->_node.optr);
H
hzcheng 已提交
1013 1014 1015 1016 1017 1018 1019 1020
      fp(&pLeft->pVal->i64Key, &pRight->pVal->i64Key, 1, 1, pOutput, order);
    }
  }

  free(pLeftOutput);
  free(pRightOutput);
}

1021
void tSQLBinaryExprTrv(tExprNode *pExprs, SArray* res) {
H
hzcheng 已提交
1022 1023 1024 1025
  if (pExprs == NULL) {
    return;
  }

H
hjxilinx 已提交
1026 1027
  tExprNode *pLeft = pExprs->_node.pLeft;
  tExprNode *pRight = pExprs->_node.pRight;
H
hzcheng 已提交
1028 1029 1030

  // recursive traverse left child branch
  if (pLeft->nodeType == TSQL_NODE_EXPR) {
1031
    tSQLBinaryExprTrv(pLeft, res);
H
hzcheng 已提交
1032
  } else if (pLeft->nodeType == TSQL_NODE_COL) {
1033
    taosArrayPush(res, &pLeft->pSchema->colId);
H
hzcheng 已提交
1034 1035 1036
  }

  if (pRight->nodeType == TSQL_NODE_EXPR) {
1037
    tSQLBinaryExprTrv(pRight, res);
H
hzcheng 已提交
1038
  } else if (pRight->nodeType == TSQL_NODE_COL) {
1039
    taosArrayPush(res, &pRight->pSchema->colId);
H
hzcheng 已提交
1040
  }
S
slguan 已提交
1041
}
1042

1043 1044
static void exprTreeToBinaryImpl(SBufferWriter* bw, tExprNode* expr) {
  tbufWriteUint8(bw, expr->nodeType);
1045
  
1046 1047
  if (expr->nodeType == TSQL_NODE_VALUE) {
    tVariant* pVal = expr->pVal;
1048
    
1049
    tbufWriteUint32(bw, pVal->nType);
1050
    if (pVal->nType == TSDB_DATA_TYPE_BINARY) {
1051 1052
      tbufWriteInt32(bw, pVal->nLen);
      tbufWrite(bw, pVal->pz, pVal->nLen);
1053
    } else {
1054
      tbufWriteInt64(bw, pVal->i64Key);
1055 1056
    }
    
1057 1058 1059 1060 1061 1062
  } else if (expr->nodeType == TSQL_NODE_COL) {
    SSchema* pSchema = expr->pSchema;
    tbufWriteInt16(bw, pSchema->colId);
    tbufWriteInt16(bw, pSchema->bytes);
    tbufWriteUint8(bw, pSchema->type);
    tbufWriteString(bw, pSchema->name);
1063
    
1064 1065 1066 1067 1068
  } else if (expr->nodeType == TSQL_NODE_EXPR) {
    tbufWriteUint8(bw, expr->_node.optr);
    tbufWriteUint8(bw, expr->_node.hasPK);
    exprTreeToBinaryImpl(bw, expr->_node.pLeft);
    exprTreeToBinaryImpl(bw, expr->_node.pRight);
1069 1070 1071
  }
}

1072 1073 1074
void exprTreeToBinary(SBufferWriter* bw, tExprNode* expr) {
  if (expr != NULL) {
    exprTreeToBinaryImpl(bw, expr);
1075
  }
1076 1077 1078 1079 1080 1081 1082
}

// TODO: these three functions should be made global
static void* exception_calloc(size_t nmemb, size_t size) {
  void* p = calloc(nmemb, size);
  if (p == NULL) {
    THROW(TSDB_CODE_SERV_OUT_OF_MEMORY);
1083
  }
1084 1085 1086 1087 1088 1089 1090 1091 1092
  return p;
}

static void* exception_malloc(size_t size) {
  void* p = malloc(size);
  if (p == NULL) {
    THROW(TSDB_CODE_SERV_OUT_OF_MEMORY);
  }
  return p;
1093 1094
}

1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
static char* exception_strdup(const char* str) {
  char* p = strdup(str);
  if (p == NULL) {
    THROW(TSDB_CODE_SERV_OUT_OF_MEMORY);
  }
  return p;
}


static tExprNode* exprTreeFromBinaryImpl(SBufferReader* br) {
  int32_t anchor = CLEANUP_GET_ANCHOR();

  tExprNode* pExpr = exception_calloc(1, sizeof(tExprNode));
  CLEANUP_PUSH_VOID_PTR_PTR(true, tExprNodeDestroy, pExpr, NULL);

  pExpr->nodeType = tbufReadUint8(br);
1111 1112
  
  if (pExpr->nodeType == TSQL_NODE_VALUE) {
1113
    tVariant* pVal = exception_calloc(1, sizeof(tVariant));
weixin_48148422's avatar
weixin_48148422 已提交
1114
    pExpr->pVal = pVal;
1115
  
1116
    pVal->nType = tbufReadUint32(br);
1117
    if (pVal->nType == TSDB_DATA_TYPE_BINARY) {
1118
      tbufReadToBuffer(br, &pVal->nLen, sizeof(pVal->nLen));
1119
      pVal->pz = calloc(1, pVal->nLen + 1);
1120
      tbufReadToBuffer(br, pVal->pz, pVal->nLen);
1121
    } else {
1122
      pVal->i64Key = tbufReadInt64(br);
1123 1124 1125
    }
    
  } else if (pExpr->nodeType == TSQL_NODE_COL) {
1126
    SSchema* pSchema = exception_calloc(1, sizeof(SSchema));
weixin_48148422's avatar
weixin_48148422 已提交
1127 1128
    pExpr->pSchema = pSchema;

1129 1130 1131 1132
    pSchema->colId = tbufReadInt16(br);
    pSchema->bytes = tbufReadInt16(br);
    pSchema->type = tbufReadUint8(br);
    tbufReadToString(br, pSchema->name, TSDB_COL_NAME_LEN);
1133 1134
    
  } else if (pExpr->nodeType == TSQL_NODE_EXPR) {
1135 1136 1137 1138
    pExpr->_node.optr = tbufReadUint8(br);
    pExpr->_node.hasPK = tbufReadUint8(br);
    pExpr->_node.pLeft = exprTreeFromBinaryImpl(br);
    pExpr->_node.pRight = exprTreeFromBinaryImpl(br);
1139 1140 1141 1142
    
    assert(pExpr->_node.pLeft != NULL && pExpr->_node.pRight != NULL);
  }
  
1143
  CLEANUP_EXECUTE_TO(anchor, false);
weixin_48148422's avatar
weixin_48148422 已提交
1144
  return pExpr;
1145 1146
}

1147
tExprNode* exprTreeFromBinary(const void* data, size_t size) {
weixin_48148422's avatar
weixin_48148422 已提交
1148 1149 1150
  if (size == 0) {
    return NULL;
  }
1151 1152
  SBufferReader br = tbufInitReader(data, size, false);
  return exprTreeFromBinaryImpl(&br);
1153 1154
}

weixin_48148422's avatar
weixin_48148422 已提交
1155 1156 1157 1158 1159
tExprNode* exprTreeFromTableName(const char* tbnameCond) {
  if (!tbnameCond) {
    return NULL;
  }

1160 1161 1162 1163 1164
  int32_t anchor = CLEANUP_GET_ANCHOR();

  tExprNode* expr = exception_calloc(1, sizeof(tExprNode));
  CLEANUP_PUSH_VOID_PTR_PTR(true, tExprNodeDestroy, expr, NULL);

weixin_48148422's avatar
weixin_48148422 已提交
1165 1166
  expr->nodeType = TSQL_NODE_EXPR;

1167
  tExprNode* left = exception_calloc(1, sizeof(tExprNode));
weixin_48148422's avatar
weixin_48148422 已提交
1168 1169 1170
  expr->_node.pLeft = left;

  left->nodeType = TSQL_NODE_COL;
1171
  SSchema* pSchema = exception_calloc(1, sizeof(SSchema));
weixin_48148422's avatar
weixin_48148422 已提交
1172 1173 1174 1175 1176 1177 1178
  left->pSchema = pSchema;

  pSchema->type = TSDB_DATA_TYPE_BINARY;
  pSchema->bytes = TSDB_TABLE_NAME_LEN;
  strcpy(pSchema->name, TSQL_TBNAME_L);
  pSchema->colId = -1;

1179
  tExprNode* right = exception_calloc(1, sizeof(tExprNode));
weixin_48148422's avatar
weixin_48148422 已提交
1180 1181 1182
  expr->_node.pRight = right;

  if (strncmp(tbnameCond, QUERY_COND_REL_PREFIX_LIKE, QUERY_COND_REL_PREFIX_LIKE_LEN) == 0) {
weixin_48148422's avatar
weixin_48148422 已提交
1183
    right->nodeType = TSQL_NODE_VALUE;
weixin_48148422's avatar
weixin_48148422 已提交
1184
    expr->_node.optr = TSDB_RELATION_LIKE;
1185
    tVariant* pVal = exception_calloc(1, sizeof(tVariant));
weixin_48148422's avatar
weixin_48148422 已提交
1186 1187
    right->pVal = pVal;
    size_t len = strlen(tbnameCond + QUERY_COND_REL_PREFIX_LIKE_LEN) + 1;
1188
    pVal->pz = exception_malloc(len);
weixin_48148422's avatar
weixin_48148422 已提交
1189
    memcpy(pVal->pz, tbnameCond + QUERY_COND_REL_PREFIX_LIKE_LEN, len);
1190
    pVal->nType = TSDB_DATA_TYPE_BINARY;
1191
    pVal->nLen = (int32_t)len;
weixin_48148422's avatar
weixin_48148422 已提交
1192 1193

  } else if (strncmp(tbnameCond, QUERY_COND_REL_PREFIX_IN, QUERY_COND_REL_PREFIX_IN_LEN) == 0) {
weixin_48148422's avatar
weixin_48148422 已提交
1194
    right->nodeType = TSQL_NODE_VALUE;
weixin_48148422's avatar
weixin_48148422 已提交
1195
    expr->_node.optr = TSDB_RELATION_IN;
1196
    tVariant* pVal = exception_calloc(1, sizeof(tVariant));
weixin_48148422's avatar
weixin_48148422 已提交
1197 1198 1199
    right->pVal = pVal;
    pVal->nType = TSDB_DATA_TYPE_ARRAY;
    pVal->arr = taosArrayInit(2, sizeof(char*));
weixin_48148422's avatar
weixin_48148422 已提交
1200 1201 1202

    const char* cond = tbnameCond + QUERY_COND_REL_PREFIX_IN_LEN;
    for (const char *e = cond; *e != 0; e++) {
1203 1204 1205
      if (*e == TS_PATH_DELIMITER[0]) {
        cond = e + 1;
      } else if (*e == ',') {
weixin_48148422's avatar
weixin_48148422 已提交
1206
        size_t len = e - cond + 1;
1207
        char* p = exception_malloc( len );
weixin_48148422's avatar
weixin_48148422 已提交
1208 1209 1210
        memcpy(p, cond, len);
        p[len - 1] = 0;
        cond += len;
weixin_48148422's avatar
weixin_48148422 已提交
1211
        taosArrayPush(pVal->arr, &p);
weixin_48148422's avatar
weixin_48148422 已提交
1212 1213 1214 1215
      }
    }

    if (*cond != 0) {
1216
        char* p = exception_strdup( cond );
weixin_48148422's avatar
weixin_48148422 已提交
1217
        taosArrayPush(pVal->arr, &p);
weixin_48148422's avatar
weixin_48148422 已提交
1218 1219
    }

weixin_48148422's avatar
weixin_48148422 已提交
1220
    taosArraySortString(pVal->arr);
weixin_48148422's avatar
weixin_48148422 已提交
1221 1222
  }

1223
  CLEANUP_EXECUTE_TO(anchor, false);
weixin_48148422's avatar
weixin_48148422 已提交
1224 1225
  return expr;
}