qast.c 37.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
  SQueryCond cond = {0};
weixin_48148422's avatar
weixin_48148422 已提交
544 545 546
  setQueryCond(pQueryInfo, &cond);

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

H
hjxilinx 已提交
559
        int32_t ret = pQueryInfo->compare(SL_GET_NODE_KEY(pSkipList, pNode), cond.start->v);
560 561 562 563 564 565 566 567 568 569 570 571 572 573
        if (ret == 0) {
          taosArrayPush(result, SL_GET_NODE_DATA(pNode));
        } else {
          break;
        }
      }
    } else if (optr == TSDB_RELATION_GREATER || optr == TSDB_RELATION_GREATER_EQUAL) {
      bool comp = true;
      int32_t ret = 0;
      
      while(tSkipListIterNext(iter)) {
        SSkipListNode* pNode = tSkipListIterGet(iter);
    
        if (comp) {
H
hjxilinx 已提交
574
          ret = pQueryInfo->compare(SL_GET_NODE_KEY(pSkipList, pNode), cond.start->v);
575
          assert(ret >= 0);
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
        }
        
        if (ret == 0 && optr == TSDB_RELATION_GREATER) {
          continue;
        } else {
          taosArrayPush(result, SL_GET_NODE_DATA(pNode));
          comp = false;
        }
      }
    } else if (optr == TSDB_RELATION_NOT_EQUAL) {
      assert(0);
    } else {
      assert(0);
    }
  } else {
weixin_48148422's avatar
weixin_48148422 已提交
591
    int32_t optr = cond.end->optr;
592 593 594 595 596 597 598 599 600
    
    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 已提交
601
          ret = pQueryInfo->compare(SL_GET_NODE_KEY(pSkipList, pNode), cond.end->v);
602
          assert(ret <= 0);
603 604 605 606 607 608 609 610 611 612 613 614
        }
        
        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 已提交
615

H
hjxilinx 已提交
616
int32_t merge(SArray *pLeft, SArray *pRight, SArray *pFinalRes) {
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
//  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 已提交
654
  return 0;
H
hzcheng 已提交
655 656
}

H
hjxilinx 已提交
657
int32_t intersect(SArray *pLeft, SArray *pRight, SArray *pFinalRes) {
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
//  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 已提交
689
 return 0;
H
hzcheng 已提交
690 691 692
}

/*
H
hjxilinx 已提交
693
 * traverse the result and apply the function to each item to check if the item is qualified or not
H
hzcheng 已提交
694
 */
H
hjxilinx 已提交
695 696 697 698 699 700 701 702 703 704 705 706
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 已提交
707 708
}

H
hjxilinx 已提交
709
static bool filterItem(tExprNode *pExpr, const void *pItem, SExprTraverseSupp *param) {
H
hjxilinx 已提交
710 711
  tExprNode *pLeft = pExpr->_node.pLeft;
  tExprNode *pRight = pExpr->_node.pRight;
S
slguan 已提交
712

713
  //non-leaf nodes, recursively traverse the expression tree in the post-root order
S
slguan 已提交
714
  if (pLeft->nodeType == TSQL_NODE_EXPR && pRight->nodeType == TSQL_NODE_EXPR) {
H
[td-32]  
hjxilinx 已提交
715 716
    if (pExpr->_node.optr == TSDB_RELATION_OR) {  // or
      if (filterItem(pLeft, pItem, param)) {
S
slguan 已提交
717 718 719 720
        return true;
      }

      // left child does not satisfy the query condition, try right child
H
[td-32]  
hjxilinx 已提交
721
      return filterItem(pRight, pItem, param);
S
slguan 已提交
722
    } else {  // and
H
[td-32]  
hjxilinx 已提交
723
      if (!filterItem(pLeft, pItem, param)) {
S
slguan 已提交
724 725 726
        return false;
      }

H
[td-32]  
hjxilinx 已提交
727
      return filterItem(pRight, pItem, param);
S
slguan 已提交
728 729 730 731 732 733 734
    }
  }

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

H
[td-32]  
hjxilinx 已提交
735
  return param->fp(pItem, pExpr->_node.info);
S
slguan 已提交
736 737 738 739 740 741 742 743 744 745 746 747
}

/**
 * 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 已提交
748
static void exprTreeTraverseImpl(tExprNode *pExpr, SArray *pResult, SExprTraverseSupp *param) {
749 750 751 752 753
  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 已提交
754 755

    if (filterItem(pExpr, pItem, param)) {
756
      taosArrayPush(array, &pItem);
S
slguan 已提交
757 758
    }
  }
759 760
  
  taosArrayCopy(pResult, array);
S
slguan 已提交
761 762
}

763

H
hjxilinx 已提交
764
static void tSQLBinaryTraverseOnSkipList(tExprNode *pExpr, SArray *pResult, SSkipList *pSkipList, SExprTraverseSupp *param ) {
765 766 767 768 769 770 771 772
  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 已提交
773
  tSkipListDestroyIter(iter);
774
}
S
slguan 已提交
775

weixin_48148422's avatar
weixin_48148422 已提交
776 777 778 779 780 781 782 783 784


static void tQueryIndexlessColumn(SSkipList* pSkipList, tQueryInfo* pQueryInfo, SArray* result) {
  SSkipListIterator* iter = tSkipListCreateIter(pSkipList);

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

    SSkipListNode *pNode = tSkipListIterGet(iter);
H
hjxilinx 已提交
785 786 787 788 789 790
    char* pTable = SL_GET_NODE_DATA(pNode);
    
    //todo refactor:
    char* name = (*(STable**) pTable)->name;
//    char* name = NULL;
//    tsdbGetTableName(tsdb, pTable, &name);
weixin_48148422's avatar
weixin_48148422 已提交
791

H
hjxilinx 已提交
792
    // todo speed up by using hash
weixin_48148422's avatar
weixin_48148422 已提交
793 794
    if (pQueryInfo->colIndex == TSDB_TBNAME_COLUMN_INDEX) {
      if (pQueryInfo->optr == TSDB_RELATION_IN) {
H
hjxilinx 已提交
795
        addToResult = pQueryInfo->compare(name, pQueryInfo->q);
weixin_48148422's avatar
weixin_48148422 已提交
796
      } else if(pQueryInfo->optr == TSDB_RELATION_LIKE) {
H
hjxilinx 已提交
797
        addToResult = !pQueryInfo->compare(name, pQueryInfo->q);
weixin_48148422's avatar
weixin_48148422 已提交
798 799 800 801 802 803
      }
    } else {
      // TODO: other columns
    }

    if (addToResult) {
H
hjxilinx 已提交
804
      taosArrayPush(result, pTable);
weixin_48148422's avatar
weixin_48148422 已提交
805 806
    }
  }
weixin_48148422's avatar
weixin_48148422 已提交
807 808

  tSkipListDestroyIter(iter);
weixin_48148422's avatar
weixin_48148422 已提交
809 810 811 812
}



H
hzcheng 已提交
813
// post-root order traverse syntax tree
H
hjxilinx 已提交
814
void tExprTreeTraverse(tExprNode *pExpr, SSkipList *pSkipList, SArray *result, SExprTraverseSupp *param) {
815 816 817 818
  if (pExpr == NULL) {
    return;
  }

819
  tExprNode *pLeft  = pExpr->_node.pLeft;
H
hjxilinx 已提交
820
  tExprNode *pRight = pExpr->_node.pRight;
821

weixin_48148422's avatar
weixin_48148422 已提交
822 823 824
  // column project
  if (pLeft->nodeType != TSQL_NODE_EXPR && pRight->nodeType != TSQL_NODE_EXPR) {
    assert(pLeft->nodeType == TSQL_NODE_COL && pRight->nodeType == TSQL_NODE_VALUE);
825

weixin_48148422's avatar
weixin_48148422 已提交
826 827
    param->setupInfoFn(pExpr, param->pExtInfo);
    if (pSkipList == NULL) {
H
hjxilinx 已提交
828
      tArrayTraverse(pExpr, param->fp, result);
weixin_48148422's avatar
weixin_48148422 已提交
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
      return;
    }

    tQueryInfo *pQueryInfo = pExpr->_node.info;
    if (pQueryInfo->colIndex == 0 && pQueryInfo->optr != TSDB_RELATION_LIKE) {
      tQueryIndexColumn(pSkipList, pQueryInfo, result);
    } else {
      tQueryIndexlessColumn(pSkipList, pQueryInfo, result);
    }

    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.
     */
853
      exprTreeTraverseImpl(pExpr, result, param);
weixin_48148422's avatar
weixin_48148422 已提交
854
    } else {
855 856 857 858 859
      /**
       * 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 已提交
860 861 862 863 864 865 866
    }
    return;
  }
  
  if (weight == 2 || (weight == 1 && pExpr->_node.optr == TSDB_RELATION_OR)) {
    SArray* rLeft  = taosArrayInit(10, POINTER_BYTES);
    SArray* rRight = taosArrayInit(10, POINTER_BYTES);
867

weixin_48148422's avatar
weixin_48148422 已提交
868 869
    tExprTreeTraverse(pLeft, pSkipList, rLeft, param);
    tExprTreeTraverse(pRight, pSkipList, rRight, param);
870

weixin_48148422's avatar
weixin_48148422 已提交
871 872 873 874
    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);
875
    } else {
weixin_48148422's avatar
weixin_48148422 已提交
876 877
      assert(false);
    }
878

weixin_48148422's avatar
weixin_48148422 已提交
879 880 881 882
    taosArrayDestroy(rLeft);
    taosArrayDestroy(rRight);
    return;
  }
883

weixin_48148422's avatar
weixin_48148422 已提交
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
  /*
    * (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;
  }
901

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

weixin_48148422's avatar
weixin_48148422 已提交
904 905 906 907 908 909 910 911
  // 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);
912
}
H
hzcheng 已提交
913

914 915
void tExprTreeCalcTraverse(tExprNode *pExprs, int32_t numOfRows, char *pOutput, void *param, int32_t order,
                                char *(*getSourceDataBlock)(void *, const char*, int32_t)) {
H
hzcheng 已提交
916 917 918 919
  if (pExprs == NULL) {
    return;
  }

H
hjxilinx 已提交
920 921
  tExprNode *pLeft = pExprs->_node.pLeft;
  tExprNode *pRight = pExprs->_node.pRight;
H
hzcheng 已提交
922 923

  /* the left output has result from the left child syntax tree */
L
lihui 已提交
924
  char *pLeftOutput = (char*)malloc(sizeof(int64_t) * numOfRows);
H
hzcheng 已提交
925
  if (pLeft->nodeType == TSQL_NODE_EXPR) {
926
    tExprTreeCalcTraverse(pLeft, numOfRows, pLeftOutput, param, order, getSourceDataBlock);
H
hzcheng 已提交
927 928 929 930 931
  }

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

  if (pLeft->nodeType == TSQL_NODE_EXPR) {
S
slguan 已提交
936 937 938 939 940
    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 已提交
941
      _bi_consumer_fn_t fp = tGetBiConsumerFn(TSDB_DATA_TYPE_DOUBLE, TSDB_DATA_TYPE_DOUBLE, pExprs->_node.optr);
H
hzcheng 已提交
942 943 944
      fp(pLeftOutput, pRightOutput, numOfRows, numOfRows, pOutput, order);

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

    } else if (pRight->nodeType == TSQL_NODE_VALUE) {  // exprLeft + 12
H
[td-32]  
hjxilinx 已提交
951
      _bi_consumer_fn_t fp = tGetBiConsumerFn(TSDB_DATA_TYPE_DOUBLE, pRight->pVal->nType, pExprs->_node.optr);
H
hzcheng 已提交
952 953 954 955
      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 已提交
956
    char *pLeftInputData = getSourceDataBlock(param, pLeft->pSchema->name, pLeft->pSchema->colId);
H
hzcheng 已提交
957
    if (pRight->nodeType == TSQL_NODE_EXPR) {  // columnLeft + expr2
H
[td-32]  
hjxilinx 已提交
958
      _bi_consumer_fn_t fp = tGetBiConsumerFn(pLeft->pSchema->type, TSDB_DATA_TYPE_DOUBLE, pExprs->_node.optr);
H
hzcheng 已提交
959 960 961 962
      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 已提交
963
      char *pRightInputData = getSourceDataBlock(param, pRight->pSchema->name, pRight->pSchema->colId);
H
hzcheng 已提交
964

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

    } else if (pRight->nodeType == TSQL_NODE_VALUE) {  // columnLeft + 12
H
[td-32]  
hjxilinx 已提交
969
      _bi_consumer_fn_t fp = tGetBiConsumerFn(pLeft->pSchema->type, pRight->pVal->nType, pExprs->_node.optr);
H
hzcheng 已提交
970 971 972 973 974
      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 已提交
975
      _bi_consumer_fn_t fp = tGetBiConsumerFn(pLeft->pVal->nType, TSDB_DATA_TYPE_DOUBLE, pExprs->_node.optr);
H
hzcheng 已提交
976 977 978 979
      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
980 981
      char *pRightInputData = getSourceDataBlock(param, pRight->pSchema->name, pRight->pSchema->colId);
      
H
[td-32]  
hjxilinx 已提交
982
      _bi_consumer_fn_t fp = tGetBiConsumerFn(pLeft->pVal->nType, pRight->pSchema->type, pExprs->_node.optr);
H
hzcheng 已提交
983 984 985
      fp(&pLeft->pVal->i64Key, pRightInputData, 1, numOfRows, pOutput, order);

    } else if (pRight->nodeType == TSQL_NODE_VALUE) {  // 12 + 12
H
[td-32]  
hjxilinx 已提交
986
      _bi_consumer_fn_t fp = tGetBiConsumerFn(pLeft->pVal->nType, pRight->pVal->nType, pExprs->_node.optr);
H
hzcheng 已提交
987 988 989 990 991 992 993 994
      fp(&pLeft->pVal->i64Key, &pRight->pVal->i64Key, 1, 1, pOutput, order);
    }
  }

  free(pLeftOutput);
  free(pRightOutput);
}

995
void tSQLBinaryExprTrv(tExprNode *pExprs, SArray* res) {
H
hzcheng 已提交
996 997 998 999
  if (pExprs == NULL) {
    return;
  }

H
hjxilinx 已提交
1000 1001
  tExprNode *pLeft = pExprs->_node.pLeft;
  tExprNode *pRight = pExprs->_node.pRight;
H
hzcheng 已提交
1002 1003 1004

  // recursive traverse left child branch
  if (pLeft->nodeType == TSQL_NODE_EXPR) {
1005
    tSQLBinaryExprTrv(pLeft, res);
H
hzcheng 已提交
1006
  } else if (pLeft->nodeType == TSQL_NODE_COL) {
1007
    taosArrayPush(res, &pLeft->pSchema->colId);
H
hzcheng 已提交
1008 1009 1010
  }

  if (pRight->nodeType == TSQL_NODE_EXPR) {
1011
    tSQLBinaryExprTrv(pRight, res);
H
hzcheng 已提交
1012
  } else if (pRight->nodeType == TSQL_NODE_COL) {
1013
    taosArrayPush(res, &pRight->pSchema->colId);
H
hzcheng 已提交
1014
  }
S
slguan 已提交
1015
}
1016

1017 1018
static void exprTreeToBinaryImpl(SBufferWriter* bw, tExprNode* expr) {
  tbufWriteUint8(bw, expr->nodeType);
1019
  
1020 1021
  if (expr->nodeType == TSQL_NODE_VALUE) {
    tVariant* pVal = expr->pVal;
1022
    
1023
    tbufWriteUint32(bw, pVal->nType);
1024
    if (pVal->nType == TSDB_DATA_TYPE_BINARY) {
1025 1026
      tbufWriteInt32(bw, pVal->nLen);
      tbufWrite(bw, pVal->pz, pVal->nLen);
1027
    } else {
1028
      tbufWriteInt64(bw, pVal->i64Key);
1029 1030
    }
    
1031 1032 1033 1034 1035 1036
  } 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);
1037
    
1038 1039 1040 1041 1042
  } 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);
1043 1044 1045
  }
}

1046 1047 1048
void exprTreeToBinary(SBufferWriter* bw, tExprNode* expr) {
  if (expr != NULL) {
    exprTreeToBinaryImpl(bw, expr);
1049
  }
1050 1051 1052 1053 1054 1055 1056
}

// 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);
1057
  }
1058 1059 1060 1061 1062 1063 1064 1065 1066
  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;
1067 1068
}

1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
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);
1085 1086
  
  if (pExpr->nodeType == TSQL_NODE_VALUE) {
1087
    tVariant* pVal = exception_calloc(1, sizeof(tVariant));
weixin_48148422's avatar
weixin_48148422 已提交
1088
    pExpr->pVal = pVal;
1089
  
1090
    pVal->nType = tbufReadUint32(br);
1091
    if (pVal->nType == TSDB_DATA_TYPE_BINARY) {
1092
      tbufReadToBuffer(br, &pVal->nLen, sizeof(pVal->nLen));
1093
      pVal->pz = calloc(1, pVal->nLen + 1);
1094
      tbufReadToBuffer(br, pVal->pz, pVal->nLen);
1095
    } else {
1096
      pVal->i64Key = tbufReadInt64(br);
1097 1098 1099
    }
    
  } else if (pExpr->nodeType == TSQL_NODE_COL) {
1100
    SSchema* pSchema = exception_calloc(1, sizeof(SSchema));
weixin_48148422's avatar
weixin_48148422 已提交
1101 1102
    pExpr->pSchema = pSchema;

1103 1104 1105 1106
    pSchema->colId = tbufReadInt16(br);
    pSchema->bytes = tbufReadInt16(br);
    pSchema->type = tbufReadUint8(br);
    tbufReadToString(br, pSchema->name, TSDB_COL_NAME_LEN);
1107 1108
    
  } else if (pExpr->nodeType == TSQL_NODE_EXPR) {
1109 1110 1111 1112
    pExpr->_node.optr = tbufReadUint8(br);
    pExpr->_node.hasPK = tbufReadUint8(br);
    pExpr->_node.pLeft = exprTreeFromBinaryImpl(br);
    pExpr->_node.pRight = exprTreeFromBinaryImpl(br);
1113 1114 1115 1116
    
    assert(pExpr->_node.pLeft != NULL && pExpr->_node.pRight != NULL);
  }
  
1117
  CLEANUP_EXECUTE_TO(anchor, false);
weixin_48148422's avatar
weixin_48148422 已提交
1118
  return pExpr;
1119 1120
}

1121
tExprNode* exprTreeFromBinary(const void* data, size_t size) {
weixin_48148422's avatar
weixin_48148422 已提交
1122 1123 1124
  if (size == 0) {
    return NULL;
  }
1125 1126
  SBufferReader br = tbufInitReader(data, size, false);
  return exprTreeFromBinaryImpl(&br);
1127 1128
}

weixin_48148422's avatar
weixin_48148422 已提交
1129 1130 1131 1132 1133
tExprNode* exprTreeFromTableName(const char* tbnameCond) {
  if (!tbnameCond) {
    return NULL;
  }

1134 1135 1136 1137 1138
  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 已提交
1139 1140
  expr->nodeType = TSQL_NODE_EXPR;

1141
  tExprNode* left = exception_calloc(1, sizeof(tExprNode));
weixin_48148422's avatar
weixin_48148422 已提交
1142 1143 1144
  expr->_node.pLeft = left;

  left->nodeType = TSQL_NODE_COL;
1145
  SSchema* pSchema = exception_calloc(1, sizeof(SSchema));
weixin_48148422's avatar
weixin_48148422 已提交
1146 1147 1148 1149 1150 1151 1152
  left->pSchema = pSchema;

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

1153
  tExprNode* right = exception_calloc(1, sizeof(tExprNode));
weixin_48148422's avatar
weixin_48148422 已提交
1154 1155 1156
  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 已提交
1157
    right->nodeType = TSQL_NODE_VALUE;
weixin_48148422's avatar
weixin_48148422 已提交
1158
    expr->_node.optr = TSDB_RELATION_LIKE;
1159
    tVariant* pVal = exception_calloc(1, sizeof(tVariant));
weixin_48148422's avatar
weixin_48148422 已提交
1160 1161
    right->pVal = pVal;
    size_t len = strlen(tbnameCond + QUERY_COND_REL_PREFIX_LIKE_LEN) + 1;
1162
    pVal->pz = exception_malloc(len);
weixin_48148422's avatar
weixin_48148422 已提交
1163
    memcpy(pVal->pz, tbnameCond + QUERY_COND_REL_PREFIX_LIKE_LEN, len);
1164
    pVal->nType = TSDB_DATA_TYPE_BINARY;
1165
    pVal->nLen = (int32_t)len;
weixin_48148422's avatar
weixin_48148422 已提交
1166 1167

  } else if (strncmp(tbnameCond, QUERY_COND_REL_PREFIX_IN, QUERY_COND_REL_PREFIX_IN_LEN) == 0) {
weixin_48148422's avatar
weixin_48148422 已提交
1168
    right->nodeType = TSQL_NODE_VALUE;
weixin_48148422's avatar
weixin_48148422 已提交
1169
    expr->_node.optr = TSDB_RELATION_IN;
1170
    tVariant* pVal = exception_calloc(1, sizeof(tVariant));
weixin_48148422's avatar
weixin_48148422 已提交
1171 1172 1173
    right->pVal = pVal;
    pVal->nType = TSDB_DATA_TYPE_ARRAY;
    pVal->arr = taosArrayInit(2, sizeof(char*));
weixin_48148422's avatar
weixin_48148422 已提交
1174 1175 1176

    const char* cond = tbnameCond + QUERY_COND_REL_PREFIX_IN_LEN;
    for (const char *e = cond; *e != 0; e++) {
1177 1178 1179
      if (*e == TS_PATH_DELIMITER[0]) {
        cond = e + 1;
      } else if (*e == ',') {
weixin_48148422's avatar
weixin_48148422 已提交
1180
        size_t len = e - cond + 1;
1181
        char* p = exception_malloc( len );
weixin_48148422's avatar
weixin_48148422 已提交
1182 1183 1184
        memcpy(p, cond, len);
        p[len - 1] = 0;
        cond += len;
weixin_48148422's avatar
weixin_48148422 已提交
1185
        taosArrayPush(pVal->arr, &p);
weixin_48148422's avatar
weixin_48148422 已提交
1186 1187 1188 1189
      }
    }

    if (*cond != 0) {
1190
        char* p = exception_strdup( cond );
weixin_48148422's avatar
weixin_48148422 已提交
1191
        taosArrayPush(pVal->arr, &p);
weixin_48148422's avatar
weixin_48148422 已提交
1192 1193
    }

weixin_48148422's avatar
weixin_48148422 已提交
1194
    taosArraySortString(pVal->arr);
weixin_48148422's avatar
weixin_48148422 已提交
1195 1196
  }

1197
  CLEANUP_EXECUTE_TO(anchor, false);
weixin_48148422's avatar
weixin_48148422 已提交
1198 1199
  return expr;
}