texpr.c 24.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"
H
Haojun Liao 已提交
17 18

#include "exception.h"
19
#include "taosdef.h"
H
hzcheng 已提交
20
#include "taosmsg.h"
H
Haojun Liao 已提交
21 22 23
#include "tarray.h"
#include "tbuffer.h"
#include "tcompare.h"
24
#include "tname.h"
H
Haojun Liao 已提交
25
#include "thash.h"
H
Haojun Liao 已提交
26
#include "tskiplist.h"
H
Haojun Liao 已提交
27
#include "texpr.h"
28
//#include "tarithoperator.h"
H
Haojun Liao 已提交
29
#include "tvariant.h"
H
hzcheng 已提交
30

31 32 33 34 35 36 37 38 39 40 41 42
//static uint8_t UNUSED_FUNC isQueryOnPrimaryKey(const char *primaryColumnName, const tExprNode *pLeft, const tExprNode *pRight) {
//  if (pLeft->nodeType == TEXPR_COL_NODE) {
//    // if left node is the primary column,return true
//    return (strcmp(primaryColumnName, pLeft->pSchema->name) == 0) ? 1 : 0;
//  } else {
//    // if any children have query on primary key, their parents are also keep this value
//    return ((pLeft->nodeType == TEXPR_BINARYEXPR_NODE && pLeft->_node.hasPK == 1) ||
//            (pRight->nodeType == TEXPR_BINARYEXPR_NODE && pRight->_node.hasPK == 1)) == true
//               ? 1
//               : 0;
//  }
//}
H
hzcheng 已提交
43

44 45
static void reverseCopy(char* dest, const char* src, int16_t type, int32_t numOfRows) {
  switch(type) {
46 47
    case TSDB_DATA_TYPE_TINYINT:
    case TSDB_DATA_TYPE_UTINYINT:{
48 49 50 51 52 53
      int8_t* p = (int8_t*) dest;
      int8_t* pSrc = (int8_t*) src;

      for(int32_t i = 0; i < numOfRows; ++i) {
        p[i] = pSrc[numOfRows - i - 1];
      }
54
      return;
55
    }
56 57 58

    case TSDB_DATA_TYPE_SMALLINT:
    case TSDB_DATA_TYPE_USMALLINT:{
59 60 61 62 63 64
      int16_t* p = (int16_t*) dest;
      int16_t* pSrc = (int16_t*) src;

      for(int32_t i = 0; i < numOfRows; ++i) {
        p[i] = pSrc[numOfRows - i - 1];
      }
65
      return;
66
    }
67 68
    case TSDB_DATA_TYPE_INT:
    case TSDB_DATA_TYPE_UINT: {
69 70 71 72 73 74
      int32_t* p = (int32_t*) dest;
      int32_t* pSrc = (int32_t*) src;

      for(int32_t i = 0; i < numOfRows; ++i) {
        p[i] = pSrc[numOfRows - i - 1];
      }
75
      return;
76
    }
77 78
    case TSDB_DATA_TYPE_BIGINT:
    case TSDB_DATA_TYPE_UBIGINT: {
79 80 81 82 83 84
      int64_t* p = (int64_t*) dest;
      int64_t* pSrc = (int64_t*) src;

      for(int32_t i = 0; i < numOfRows; ++i) {
        p[i] = pSrc[numOfRows - i - 1];
      }
85
      return;
86 87 88 89 90 91 92 93
    }
    case TSDB_DATA_TYPE_FLOAT: {
      float* p = (float*) dest;
      float* pSrc = (float*) src;

      for(int32_t i = 0; i < numOfRows; ++i) {
        p[i] = pSrc[numOfRows - i - 1];
      }
94
      return;
95 96 97 98 99 100 101 102
    }
    case TSDB_DATA_TYPE_DOUBLE: {
      double* p = (double*) dest;
      double* pSrc = (double*) src;

      for(int32_t i = 0; i < numOfRows; ++i) {
        p[i] = pSrc[numOfRows - i - 1];
      }
103
      return;
104 105 106 107 108
    }
    default: assert(0);
  }
}

H
Haojun Liao 已提交
109 110 111
static void doExprTreeDestroy(tExprNode **pExpr, void (*fp)(void *));

void tExprTreeDestroy(tExprNode *pNode, void (*fp)(void *)) {
H
Haojun Liao 已提交
112 113 114 115
  if (pNode == NULL) {
    return;
  }

116
  if (pNode->nodeType == TEXPR_BINARYEXPR_NODE || pNode->nodeType == TEXPR_UNARYEXPR_NODE) {
H
Haojun Liao 已提交
117
    doExprTreeDestroy(&pNode, fp);
118
  } else if (pNode->nodeType == TEXPR_VALUE_NODE) {
H
Haojun Liao 已提交
119
    taosVariantDestroy(pNode->pVal);
120
  } else if (pNode->nodeType == TEXPR_COL_NODE) {
W
wpan 已提交
121
    tfree(pNode->pSchema);
H
Haojun Liao 已提交
122 123 124 125 126
  }

  free(pNode);
}

H
Haojun Liao 已提交
127
static void doExprTreeDestroy(tExprNode **pExpr, void (*fp)(void *)) {
H
Haojun Liao 已提交
128 129 130
  if (*pExpr == NULL) {
    return;
  }
131 132 133

  int32_t type = (*pExpr)->nodeType;
  if (type == TEXPR_BINARYEXPR_NODE) {
H
Haojun Liao 已提交
134 135
    doExprTreeDestroy(&(*pExpr)->_node.pLeft, fp);
    doExprTreeDestroy(&(*pExpr)->_node.pRight, fp);
H
Haojun Liao 已提交
136 137 138 139
  
    if (fp != NULL) {
      fp((*pExpr)->_node.info);
    }
140 141 142 143 144 145 146 147
  } else if (type == TEXPR_UNARYEXPR_NODE) {
    doExprTreeDestroy(&(*pExpr)->_node.pLeft, fp);
    if (fp != NULL) {
      fp((*pExpr)->_node.info);
    }

    assert((*pExpr)->_node.pRight == NULL);
  } else if (type == TEXPR_VALUE_NODE) {
H
Haojun Liao 已提交
148
    taosVariantDestroy((*pExpr)->pVal);
H
Haojun Liao 已提交
149
    free((*pExpr)->pVal);
150
  } else if (type == TEXPR_COL_NODE) {
H
Haojun Liao 已提交
151 152 153 154 155 156 157
    free((*pExpr)->pSchema);
  }

  free(*pExpr);
  *pExpr = NULL;
}

H
Haojun Liao 已提交
158
bool exprTreeApplyFilter(tExprNode *pExpr, const void *pItem, SExprTraverseSupp *param) {
H
Haojun Liao 已提交
159 160 161 162
  tExprNode *pLeft  = pExpr->_node.pLeft;
  tExprNode *pRight = pExpr->_node.pRight;

  //non-leaf nodes, recursively traverse the expression tree in the post-root order
163
  if (pLeft->nodeType == TEXPR_BINARYEXPR_NODE && pRight->nodeType == TEXPR_BINARYEXPR_NODE) {
H
Haojun Liao 已提交
164
    if (pExpr->_node.optr == TSDB_RELATION_OR) {  // or
H
Haojun Liao 已提交
165
      if (exprTreeApplyFilter(pLeft, pItem, param)) {
H
Haojun Liao 已提交
166 167 168 169
        return true;
      }

      // left child does not satisfy the query condition, try right child
H
Haojun Liao 已提交
170
      return exprTreeApplyFilter(pRight, pItem, param);
H
Haojun Liao 已提交
171
    } else {  // and
H
Haojun Liao 已提交
172
      if (!exprTreeApplyFilter(pLeft, pItem, param)) {
H
Haojun Liao 已提交
173 174 175
        return false;
      }

H
Haojun Liao 已提交
176
      return exprTreeApplyFilter(pRight, pItem, param);
H
Haojun Liao 已提交
177 178 179 180 181 182 183 184 185
    }
  }

  // handle the leaf node
  param->setupInfoFn(pExpr, param->pExtInfo);
  return param->nodeFilterFn(pItem, pExpr->_node.info);
}

void arithmeticTreeTraverse(tExprNode *pExprs, int32_t numOfRows, char *pOutput, void *param, int32_t order,
186
                                char *(*getSourceDataBlock)(void *, const char*, int32_t)) {
H
hzcheng 已提交
187 188 189
  if (pExprs == NULL) {
    return;
  }
190
#if 0
H
hjxilinx 已提交
191 192
  tExprNode *pLeft = pExprs->_node.pLeft;
  tExprNode *pRight = pExprs->_node.pRight;
H
hzcheng 已提交
193 194

  /* the left output has result from the left child syntax tree */
L
lihui 已提交
195
  char *pLeftOutput = (char*)malloc(sizeof(int64_t) * numOfRows);
196
  if (pLeft->nodeType == TEXPR_BINARYEXPR_NODE) {
H
Haojun Liao 已提交
197
    arithmeticTreeTraverse(pLeft, numOfRows, pLeftOutput, param, order, getSourceDataBlock);
H
hzcheng 已提交
198 199
  }

200
  // the right output has result from the right child syntax tree
H
hzcheng 已提交
201
  char *pRightOutput = malloc(sizeof(int64_t) * numOfRows);
202 203
  char *pdata = malloc(sizeof(int64_t) * numOfRows);

204
  if (pRight->nodeType == TEXPR_BINARYEXPR_NODE) {
H
Haojun Liao 已提交
205
    arithmeticTreeTraverse(pRight, numOfRows, pRightOutput, param, order, getSourceDataBlock);
H
hzcheng 已提交
206 207
  }

208 209
  if (pLeft->nodeType == TEXPR_BINARYEXPR_NODE) {
    if (pRight->nodeType == TEXPR_BINARYEXPR_NODE) {
S
slguan 已提交
210 211 212 213
      /*
       * exprLeft + exprRight
       * the type of returned value of one expression is always double float precious
       */
214 215
      _arithmetic_operator_fn_t OperatorFn = getArithmeticOperatorFn(pExprs->_node.optr);
      OperatorFn(pLeftOutput, numOfRows, TSDB_DATA_TYPE_DOUBLE, pRightOutput, numOfRows, TSDB_DATA_TYPE_DOUBLE, pOutput, TSDB_ORDER_ASC);
H
hzcheng 已提交
216

217
    } else if (pRight->nodeType == TEXPR_COL_NODE) {  // exprLeft + columnRight
218
      _arithmetic_operator_fn_t OperatorFn = getArithmeticOperatorFn(pExprs->_node.optr);
219

H
hzcheng 已提交
220
      // set input buffer
H
[td-32]  
hjxilinx 已提交
221
      char *pInputData = getSourceDataBlock(param, pRight->pSchema->name, pRight->pSchema->colId);
222 223
      if (order == TSDB_ORDER_DESC) {
        reverseCopy(pdata, pInputData, pRight->pSchema->type, numOfRows);
224
        OperatorFn(pLeftOutput, numOfRows, TSDB_DATA_TYPE_DOUBLE, pdata, numOfRows, pRight->pSchema->type, pOutput, TSDB_ORDER_ASC);
225
      } else {
226
        OperatorFn(pLeftOutput, numOfRows, TSDB_DATA_TYPE_DOUBLE, pInputData, numOfRows, pRight->pSchema->type, pOutput, TSDB_ORDER_ASC);
227
      }
H
hzcheng 已提交
228

229
    } else if (pRight->nodeType == TEXPR_VALUE_NODE) {  // exprLeft + 12
230
      _arithmetic_operator_fn_t OperatorFn = getArithmeticOperatorFn(pExprs->_node.optr);
231
      OperatorFn(pLeftOutput, numOfRows, TSDB_DATA_TYPE_DOUBLE, &pRight->pVal->i, 1, pRight->pVal->nType, pOutput, TSDB_ORDER_ASC);
H
hzcheng 已提交
232
    }
233
  } else if (pLeft->nodeType == TEXPR_COL_NODE) {
H
hzcheng 已提交
234
    // column data specified on left-hand-side
H
[td-32]  
hjxilinx 已提交
235
    char *pLeftInputData = getSourceDataBlock(param, pLeft->pSchema->name, pLeft->pSchema->colId);
236
    if (pRight->nodeType == TEXPR_BINARYEXPR_NODE) {  // columnLeft + expr2
237
      _arithmetic_operator_fn_t OperatorFn = getArithmeticOperatorFn(pExprs->_node.optr);
238 239 240

      if (order == TSDB_ORDER_DESC) {
        reverseCopy(pdata, pLeftInputData, pLeft->pSchema->type, numOfRows);
241
        OperatorFn(pdata, numOfRows, pLeft->pSchema->type, pRightOutput, numOfRows, TSDB_DATA_TYPE_DOUBLE, pOutput, TSDB_ORDER_ASC);
242
      } else {
243
        OperatorFn(pLeftInputData, numOfRows, pLeft->pSchema->type, pRightOutput, numOfRows, TSDB_DATA_TYPE_DOUBLE, pOutput, TSDB_ORDER_ASC);
244
      }
H
hzcheng 已提交
245

246
    } else if (pRight->nodeType == TEXPR_COL_NODE) {  // columnLeft + columnRight
H
hzcheng 已提交
247
      // column data specified on right-hand-side
H
[td-32]  
hjxilinx 已提交
248
      char *pRightInputData = getSourceDataBlock(param, pRight->pSchema->name, pRight->pSchema->colId);
249
      _arithmetic_operator_fn_t OperatorFn = getArithmeticOperatorFn(pExprs->_node.optr);
H
hzcheng 已提交
250

251
      // both columns are descending order, do not reverse the source data
252
      OperatorFn(pLeftInputData, numOfRows, pLeft->pSchema->type, pRightInputData, numOfRows, pRight->pSchema->type, pOutput, order);
253
    } else if (pRight->nodeType == TEXPR_VALUE_NODE) {  // columnLeft + 12
254
      _arithmetic_operator_fn_t OperatorFn = getArithmeticOperatorFn(pExprs->_node.optr);
255 256 257

      if (order == TSDB_ORDER_DESC) {
        reverseCopy(pdata, pLeftInputData, pLeft->pSchema->type, numOfRows);
258
        OperatorFn(pdata, numOfRows, pLeft->pSchema->type, &pRight->pVal->i, 1, pRight->pVal->nType, pOutput, TSDB_ORDER_ASC);
259
      } else {
260
        OperatorFn(pLeftInputData, numOfRows, pLeft->pSchema->type, &pRight->pVal->i, 1, pRight->pVal->nType, pOutput, TSDB_ORDER_ASC);
261
      }
H
hzcheng 已提交
262 263 264
    }
  } else {
    // column data specified on left-hand-side
265
    if (pRight->nodeType == TEXPR_BINARYEXPR_NODE) {  // 12 + expr2
266
      _arithmetic_operator_fn_t OperatorFn = getArithmeticOperatorFn(pExprs->_node.optr);
267
      OperatorFn(&pLeft->pVal->i, 1, pLeft->pVal->nType, pRightOutput, numOfRows, TSDB_DATA_TYPE_DOUBLE, pOutput, TSDB_ORDER_ASC);
H
hzcheng 已提交
268

269
    } else if (pRight->nodeType == TEXPR_COL_NODE) {  // 12 + columnRight
H
hzcheng 已提交
270
      // column data specified on right-hand-side
271
      char *pRightInputData = getSourceDataBlock(param, pRight->pSchema->name, pRight->pSchema->colId);
272
      _arithmetic_operator_fn_t OperatorFn = getArithmeticOperatorFn(pExprs->_node.optr);
273 274 275

      if (order == TSDB_ORDER_DESC) {
        reverseCopy(pdata, pRightInputData, pRight->pSchema->type, numOfRows);
276
        OperatorFn(&pLeft->pVal->i, 1, pLeft->pVal->nType, pdata, numOfRows, pRight->pSchema->type, pOutput, TSDB_ORDER_ASC);
277
      } else {
278
        OperatorFn(&pLeft->pVal->i, 1, pLeft->pVal->nType, pRightInputData, numOfRows, pRight->pSchema->type, pOutput, TSDB_ORDER_ASC);
279
      }
H
hzcheng 已提交
280

281
    } else if (pRight->nodeType == TEXPR_VALUE_NODE) {  // 12 + 12
282
      _arithmetic_operator_fn_t OperatorFn = getArithmeticOperatorFn(pExprs->_node.optr);
283
      OperatorFn(&pLeft->pVal->i, 1, pLeft->pVal->nType, &pRight->pVal->i, 1, pRight->pVal->nType, pOutput, TSDB_ORDER_ASC);
H
hzcheng 已提交
284 285 286
    }
  }

H
Haojun Liao 已提交
287 288 289
  tfree(pdata);
  tfree(pLeftOutput);
  tfree(pRightOutput);
290 291
#endif

H
hzcheng 已提交
292 293
}

294 295
static void exprTreeToBinaryImpl(SBufferWriter* bw, tExprNode* expr) {
  tbufWriteUint8(bw, expr->nodeType);
296
  
297
  if (expr->nodeType == TEXPR_VALUE_NODE) {
H
Haojun Liao 已提交
298
    SVariant* pVal = expr->pVal;
299
    
300
    tbufWriteUint32(bw, pVal->nType);
301
    if (pVal->nType == TSDB_DATA_TYPE_BINARY) {
302 303
      tbufWriteInt32(bw, pVal->nLen);
      tbufWrite(bw, pVal->pz, pVal->nLen);
304
    } else {
305
      tbufWriteInt64(bw, pVal->i);
306 307
    }
    
308
  } else if (expr->nodeType == TEXPR_COL_NODE) {
309 310 311 312 313
    SSchema* pSchema = expr->pSchema;
    tbufWriteInt16(bw, pSchema->colId);
    tbufWriteInt16(bw, pSchema->bytes);
    tbufWriteUint8(bw, pSchema->type);
    tbufWriteString(bw, pSchema->name);
314
    
315
  } else if (expr->nodeType == TEXPR_BINARYEXPR_NODE) {
316 317 318
    tbufWriteUint8(bw, expr->_node.optr);
    exprTreeToBinaryImpl(bw, expr->_node.pLeft);
    exprTreeToBinaryImpl(bw, expr->_node.pRight);
319 320 321
  }
}

322 323 324
void exprTreeToBinary(SBufferWriter* bw, tExprNode* expr) {
  if (expr != NULL) {
    exprTreeToBinaryImpl(bw, expr);
325
  }
326 327 328 329 330 331
}

// 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) {
332
    THROW(TSDB_CODE_QRY_OUT_OF_MEMORY);
333
  }
334 335 336 337 338 339
  return p;
}

static void* exception_malloc(size_t size) {
  void* p = malloc(size);
  if (p == NULL) {
340
    THROW(TSDB_CODE_QRY_OUT_OF_MEMORY);
341 342
  }
  return p;
343 344
}

345
static UNUSED_FUNC char* exception_strdup(const char* str) {
346 347
  char* p = strdup(str);
  if (p == NULL) {
348
    THROW(TSDB_CODE_QRY_OUT_OF_MEMORY);
349 350 351 352 353 354
  }
  return p;
}

static tExprNode* exprTreeFromBinaryImpl(SBufferReader* br) {
  int32_t anchor = CLEANUP_GET_ANCHOR();
dengyihao's avatar
TD-816  
dengyihao 已提交
355 356 357 358
  if (CLEANUP_EXCEED_LIMIT()) {
    THROW(TSDB_CODE_QRY_EXCEED_TAGS_LIMIT);
    return NULL;
  }
359 360

  tExprNode* pExpr = exception_calloc(1, sizeof(tExprNode));
H
Haojun Liao 已提交
361
  CLEANUP_PUSH_VOID_PTR_PTR(true, tExprTreeDestroy, pExpr, NULL);
362
  pExpr->nodeType = tbufReadUint8(br);
363
  
364
  if (pExpr->nodeType == TEXPR_VALUE_NODE) {
H
Haojun Liao 已提交
365
    SVariant* pVal = exception_calloc(1, sizeof(SVariant));
weixin_48148422's avatar
weixin_48148422 已提交
366
    pExpr->pVal = pVal;
367
  
368
    pVal->nType = tbufReadUint32(br);
369
    if (pVal->nType == TSDB_DATA_TYPE_BINARY) {
370
      tbufReadToBuffer(br, &pVal->nLen, sizeof(pVal->nLen));
371
      pVal->pz = calloc(1, pVal->nLen + 1);
372
      tbufReadToBuffer(br, pVal->pz, pVal->nLen);
373
    } else {
374
      pVal->i = tbufReadInt64(br);
375 376
    }
    
377
  } else if (pExpr->nodeType == TEXPR_COL_NODE) {
378
    SSchema* pSchema = exception_calloc(1, sizeof(SSchema));
weixin_48148422's avatar
weixin_48148422 已提交
379 380
    pExpr->pSchema = pSchema;

381 382 383 384
    pSchema->colId = tbufReadInt16(br);
    pSchema->bytes = tbufReadInt16(br);
    pSchema->type = tbufReadUint8(br);
    tbufReadToString(br, pSchema->name, TSDB_COL_NAME_LEN);
385
    
386
  } else if (pExpr->nodeType == TEXPR_BINARYEXPR_NODE) {
387 388 389
    pExpr->_node.optr = tbufReadUint8(br);
    pExpr->_node.pLeft = exprTreeFromBinaryImpl(br);
    pExpr->_node.pRight = exprTreeFromBinaryImpl(br);
390 391 392
    assert(pExpr->_node.pLeft != NULL && pExpr->_node.pRight != NULL);
  }
  
393
  CLEANUP_EXECUTE_TO(anchor, false);
weixin_48148422's avatar
weixin_48148422 已提交
394
  return pExpr;
395 396
}

397
tExprNode* exprTreeFromBinary(const void* data, size_t size) {
weixin_48148422's avatar
weixin_48148422 已提交
398 399 400
  if (size == 0) {
    return NULL;
  }
H
Haojun Liao 已提交
401

402 403
  SBufferReader br = tbufInitReader(data, size, false);
  return exprTreeFromBinaryImpl(&br);
404 405
}

weixin_48148422's avatar
weixin_48148422 已提交
406 407 408 409 410
tExprNode* exprTreeFromTableName(const char* tbnameCond) {
  if (!tbnameCond) {
    return NULL;
  }

411 412 413
  int32_t anchor = CLEANUP_GET_ANCHOR();

  tExprNode* expr = exception_calloc(1, sizeof(tExprNode));
H
Haojun Liao 已提交
414
  CLEANUP_PUSH_VOID_PTR_PTR(true, tExprTreeDestroy, expr, NULL);
415

416
  expr->nodeType = TEXPR_BINARYEXPR_NODE;
weixin_48148422's avatar
weixin_48148422 已提交
417

418
  tExprNode* left = exception_calloc(1, sizeof(tExprNode));
weixin_48148422's avatar
weixin_48148422 已提交
419 420
  expr->_node.pLeft = left;

421
  left->nodeType = TEXPR_COL_NODE;
422
  SSchema* pSchema = exception_calloc(1, sizeof(SSchema));
weixin_48148422's avatar
weixin_48148422 已提交
423 424
  left->pSchema = pSchema;

H
Haojun Liao 已提交
425
//  *pSchema = NULL;//*tGetTbnameColumnSchema();
weixin_48148422's avatar
weixin_48148422 已提交
426

427
  tExprNode* right = exception_calloc(1, sizeof(tExprNode));
weixin_48148422's avatar
weixin_48148422 已提交
428 429 430
  expr->_node.pRight = right;

  if (strncmp(tbnameCond, QUERY_COND_REL_PREFIX_LIKE, QUERY_COND_REL_PREFIX_LIKE_LEN) == 0) {
431
    right->nodeType = TEXPR_VALUE_NODE;
weixin_48148422's avatar
weixin_48148422 已提交
432
    expr->_node.optr = TSDB_RELATION_LIKE;
H
Haojun Liao 已提交
433
    SVariant* pVal = exception_calloc(1, sizeof(SVariant));
weixin_48148422's avatar
weixin_48148422 已提交
434 435
    right->pVal = pVal;
    size_t len = strlen(tbnameCond + QUERY_COND_REL_PREFIX_LIKE_LEN) + 1;
436
    pVal->pz = exception_malloc(len);
weixin_48148422's avatar
weixin_48148422 已提交
437
    memcpy(pVal->pz, tbnameCond + QUERY_COND_REL_PREFIX_LIKE_LEN, len);
438
    pVal->nType = TSDB_DATA_TYPE_BINARY;
439
    pVal->nLen = (int32_t)len;
weixin_48148422's avatar
weixin_48148422 已提交
440

441
  } else if (strncmp(tbnameCond, QUERY_COND_REL_PREFIX_MATCH, QUERY_COND_REL_PREFIX_MATCH_LEN) == 0) {
442
    right->nodeType = TEXPR_VALUE_NODE;
443
    expr->_node.optr = TSDB_RELATION_MATCH;
H
Haojun Liao 已提交
444
    SVariant* pVal = exception_calloc(1, sizeof(SVariant));
445 446 447 448 449 450
    right->pVal = pVal;
    size_t len = strlen(tbnameCond + QUERY_COND_REL_PREFIX_MATCH_LEN) + 1;
    pVal->pz = exception_malloc(len);
    memcpy(pVal->pz, tbnameCond + QUERY_COND_REL_PREFIX_MATCH_LEN, len);
    pVal->nType = TSDB_DATA_TYPE_BINARY;
    pVal->nLen = (int32_t)len;
451
  } else if (strncmp(tbnameCond, QUERY_COND_REL_PREFIX_NMATCH, QUERY_COND_REL_PREFIX_NMATCH_LEN) == 0) {
452
    right->nodeType = TEXPR_VALUE_NODE;
453
    expr->_node.optr = TSDB_RELATION_NMATCH;
H
Haojun Liao 已提交
454
    SVariant* pVal = exception_calloc(1, sizeof(SVariant));
455 456 457 458 459 460
    right->pVal = pVal;
    size_t len = strlen(tbnameCond + QUERY_COND_REL_PREFIX_NMATCH_LEN) + 1;
    pVal->pz = exception_malloc(len);
    memcpy(pVal->pz, tbnameCond + QUERY_COND_REL_PREFIX_NMATCH_LEN, len);
    pVal->nType = TSDB_DATA_TYPE_BINARY;
    pVal->nLen = (int32_t)len;
weixin_48148422's avatar
weixin_48148422 已提交
461
  } else if (strncmp(tbnameCond, QUERY_COND_REL_PREFIX_IN, QUERY_COND_REL_PREFIX_IN_LEN) == 0) {
462
    right->nodeType = TEXPR_VALUE_NODE;
weixin_48148422's avatar
weixin_48148422 已提交
463
    expr->_node.optr = TSDB_RELATION_IN;
H
Haojun Liao 已提交
464
    SVariant* pVal = exception_calloc(1, sizeof(SVariant));
weixin_48148422's avatar
weixin_48148422 已提交
465
    right->pVal = pVal;
W
wpan 已提交
466
    pVal->nType = TSDB_DATA_TYPE_POINTER_ARRAY;
467
    pVal->arr = taosArrayInit(2, POINTER_BYTES);
weixin_48148422's avatar
weixin_48148422 已提交
468 469 470

    const char* cond = tbnameCond + QUERY_COND_REL_PREFIX_IN_LEN;
    for (const char *e = cond; *e != 0; e++) {
471 472 473
      if (*e == TS_PATH_DELIMITER[0]) {
        cond = e + 1;
      } else if (*e == ',') {
474 475
        size_t len = e - cond;
        char* p = exception_malloc(len + VARSTR_HEADER_SIZE);
S
TD-1057  
Shengliang Guan 已提交
476
        STR_WITH_SIZE_TO_VARSTR(p, cond, (VarDataLenT)len);
weixin_48148422's avatar
weixin_48148422 已提交
477
        cond += len;
weixin_48148422's avatar
weixin_48148422 已提交
478
        taosArrayPush(pVal->arr, &p);
weixin_48148422's avatar
weixin_48148422 已提交
479 480 481 482
      }
    }

    if (*cond != 0) {
483 484 485
      size_t len = strlen(cond) + VARSTR_HEADER_SIZE;
      
      char* p = exception_malloc(len);
S
TD-1057  
Shengliang Guan 已提交
486
      STR_WITH_SIZE_TO_VARSTR(p, cond, (VarDataLenT)(len - VARSTR_HEADER_SIZE));
487
      taosArrayPush(pVal->arr, &p);
weixin_48148422's avatar
weixin_48148422 已提交
488 489
    }

490
    taosArraySortString(pVal->arr, taosArrayCompareString);
weixin_48148422's avatar
weixin_48148422 已提交
491 492
  }

493
  CLEANUP_EXECUTE_TO(anchor, false);
weixin_48148422's avatar
weixin_48148422 已提交
494
  return expr;
dengyihao's avatar
dengyihao 已提交
495
}
H
Haojun Liao 已提交
496

Y
yihaoDeng 已提交
497 498 499 500
void buildFilterSetFromBinary(void **q, const char *buf, int32_t len) {
  SBufferReader br = tbufInitReader(buf, len, false); 
  uint32_t type  = tbufReadUint32(&br);     
  SHashObj *pObj = taosHashInit(256, taosGetDefaultHashFunction(type), true, false);
501
  
H
Haojun Liao 已提交
502 503
//  taosHashSetEqualFp(pObj, taosGetDefaultEqualFunction(type));

Y
yihaoDeng 已提交
504 505 506
  int dummy = -1;
  int32_t sz = tbufReadInt32(&br);
  for (int32_t i = 0; i < sz; i++) {
507
    if (type == TSDB_DATA_TYPE_BOOL || IS_SIGNED_NUMERIC_TYPE(type)) {
508 509
      int64_t val = tbufReadInt64(&br); 
      taosHashPut(pObj, (char *)&val, sizeof(val),  &dummy, sizeof(dummy));
510 511 512 513 514
    } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
      uint64_t val = tbufReadUint64(&br); 
      taosHashPut(pObj, (char *)&val, sizeof(val),  &dummy, sizeof(dummy));
    }
    else if (type == TSDB_DATA_TYPE_TIMESTAMP) {
Y
yihaoDeng 已提交
515 516 517 518 519
      int64_t val = tbufReadInt64(&br); 
      taosHashPut(pObj, (char *)&val, sizeof(val),  &dummy, sizeof(dummy));
    } else if (type == TSDB_DATA_TYPE_DOUBLE || type == TSDB_DATA_TYPE_FLOAT) {
      double  val = tbufReadDouble(&br);
      taosHashPut(pObj, (char *)&val, sizeof(val), &dummy, sizeof(dummy));
Y
yihaoDeng 已提交
520
    } else if (type == TSDB_DATA_TYPE_BINARY) {
Y
yihaoDeng 已提交
521 522 523
      size_t  t = 0;
      const char *val = tbufReadBinary(&br, &t);
      taosHashPut(pObj, (char *)val, t, &dummy, sizeof(dummy));
Y
yihaoDeng 已提交
524
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
Y
yihaoDeng 已提交
525 526 527
      size_t  t = 0;
      const char *val = tbufReadBinary(&br, &t);      
      taosHashPut(pObj, (char *)val, t, &dummy, sizeof(dummy));
Y
yihaoDeng 已提交
528 529 530 531 532
    }
  } 
  *q = (void *)pObj;
}

W
wpan 已提交
533 534 535 536 537
void convertFilterSetFromBinary(void **q, const char *buf, int32_t len, uint32_t tType) {
  SBufferReader br = tbufInitReader(buf, len, false); 
  uint32_t sType  = tbufReadUint32(&br);     
  SHashObj *pObj = taosHashInit(256, taosGetDefaultHashFunction(tType), true, false);
  
H
Haojun Liao 已提交
538
//  taosHashSetEqualFp(pObj, taosGetDefaultEqualFunction(tType));
W
wpan 已提交
539 540
  
  int dummy = -1;
H
Haojun Liao 已提交
541
  SVariant tmpVar = {0};  
W
wpan 已提交
542 543
  size_t  t = 0;
  int32_t sz = tbufReadInt32(&br);
W
wpan 已提交
544 545
  void *pvar = NULL;  
  int64_t val = 0;
W
wpan 已提交
546 547 548 549 550 551 552 553 554 555 556 557
  int32_t bufLen = 0;
  if (IS_NUMERIC_TYPE(sType)) {
    bufLen = 60;  // The maximum length of string that a number is converted to.
  } else {
    bufLen = 128;
  }

  char *tmp = calloc(1, bufLen * TSDB_NCHAR_SIZE);
    
  for (int32_t i = 0; i < sz; i++) {
    switch (sType) {
    case TSDB_DATA_TYPE_BOOL:
W
wpan 已提交
558
    case TSDB_DATA_TYPE_UTINYINT:
W
wpan 已提交
559
    case TSDB_DATA_TYPE_TINYINT: {
W
wpan 已提交
560
      *(uint8_t *)&val = (uint8_t)tbufReadInt64(&br); 
W
wpan 已提交
561 562 563 564
      t = sizeof(val);
      pvar = &val;
      break;
    }
W
wpan 已提交
565
    case TSDB_DATA_TYPE_USMALLINT:
W
wpan 已提交
566
    case TSDB_DATA_TYPE_SMALLINT: {
W
wpan 已提交
567
      *(uint16_t *)&val = (uint16_t)tbufReadInt64(&br); 
W
wpan 已提交
568 569 570 571
      t = sizeof(val);
      pvar = &val;
      break;
    }
W
wpan 已提交
572
    case TSDB_DATA_TYPE_UINT:
W
wpan 已提交
573
    case TSDB_DATA_TYPE_INT: {
W
wpan 已提交
574
      *(uint32_t *)&val = (uint32_t)tbufReadInt64(&br); 
W
wpan 已提交
575 576 577 578
      t = sizeof(val);
      pvar = &val;
      break;
    }
W
wpan 已提交
579
    case TSDB_DATA_TYPE_TIMESTAMP:
W
wpan 已提交
580
    case TSDB_DATA_TYPE_UBIGINT:
W
wpan 已提交
581
    case TSDB_DATA_TYPE_BIGINT: {
W
wpan 已提交
582
      *(uint64_t *)&val = (uint64_t)tbufReadInt64(&br); 
W
wpan 已提交
583 584 585 586 587
      t = sizeof(val);
      pvar = &val;
      break;
    }
    case TSDB_DATA_TYPE_DOUBLE: {
W
wpan 已提交
588
      *(double *)&val = tbufReadDouble(&br);
W
wpan 已提交
589 590 591 592 593
      t = sizeof(val);
      pvar = &val;
      break;
    }
    case TSDB_DATA_TYPE_FLOAT: {
W
wpan 已提交
594
      *(float *)&val = (float)tbufReadDouble(&br);
W
wpan 已提交
595 596 597 598 599
      t = sizeof(val);
      pvar = &val;
      break;
    }
    case TSDB_DATA_TYPE_BINARY: {
W
wpan 已提交
600
      pvar = (char *)tbufReadBinary(&br, &t);
W
wpan 已提交
601 602 603
      break;
    }
    case TSDB_DATA_TYPE_NCHAR: {
W
wpan 已提交
604
      pvar = (char *)tbufReadBinary(&br, &t);      
W
wpan 已提交
605 606 607 608 609 610 611 612
      break;
    }
    default:
      taosHashCleanup(pObj);
      *q = NULL;
      return;
    }
    
H
Haojun Liao 已提交
613
    taosVariantCreateFromBinary(&tmpVar, (char *)pvar, t, sType);
W
wpan 已提交
614 615 616

    if (bufLen < t) {
      tmp = realloc(tmp, t * TSDB_NCHAR_SIZE);
W
wpan 已提交
617
      bufLen = (int32_t)t;
W
wpan 已提交
618 619 620 621
    }

    switch (tType) {
      case TSDB_DATA_TYPE_BOOL:
W
wpan 已提交
622
      case TSDB_DATA_TYPE_UTINYINT:
W
wpan 已提交
623
      case TSDB_DATA_TYPE_TINYINT: {
H
Haojun Liao 已提交
624
        if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) {
W
wpan 已提交
625 626 627 628 629 630
          goto err_ret;
        }
        pvar = &val;
        t = sizeof(val);
        break;
      }
W
wpan 已提交
631
      case TSDB_DATA_TYPE_USMALLINT:
W
wpan 已提交
632
      case TSDB_DATA_TYPE_SMALLINT: {
H
Haojun Liao 已提交
633
        if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) {
W
wpan 已提交
634 635 636 637 638 639
          goto err_ret;
        }
        pvar = &val;
        t = sizeof(val);
        break;
      }
W
wpan 已提交
640
      case TSDB_DATA_TYPE_UINT:
W
wpan 已提交
641
      case TSDB_DATA_TYPE_INT: {
H
Haojun Liao 已提交
642
        if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) {
W
wpan 已提交
643 644 645 646 647 648
          goto err_ret;
        }
        pvar = &val;
        t = sizeof(val);
        break;
      }
W
wpan 已提交
649
      case TSDB_DATA_TYPE_TIMESTAMP:
W
wpan 已提交
650
      case TSDB_DATA_TYPE_UBIGINT:
W
wpan 已提交
651
      case TSDB_DATA_TYPE_BIGINT: {
H
Haojun Liao 已提交
652
        if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) {
W
wpan 已提交
653 654 655 656 657 658 659
          goto err_ret;
        }
        pvar = &val;
        t = sizeof(val);
        break;
      }
      case TSDB_DATA_TYPE_DOUBLE: {
H
Haojun Liao 已提交
660
        if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) {
W
wpan 已提交
661 662 663 664 665 666 667
          goto err_ret;
        }
        pvar = &val;
        t = sizeof(val);
        break;
      }
      case TSDB_DATA_TYPE_FLOAT: {
H
Haojun Liao 已提交
668
        if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) {
W
wpan 已提交
669 670 671 672 673 674 675
          goto err_ret;
        }
        pvar = &val;
        t = sizeof(val);
        break;
      }
      case TSDB_DATA_TYPE_BINARY: {
H
Haojun Liao 已提交
676
        if (taosVariantDump(&tmpVar, tmp, tType, true)) {
W
wpan 已提交
677 678 679 680 681 682 683
          goto err_ret;
        }
        t = varDataLen(tmp);
        pvar = varDataVal(tmp);
        break;
      }
      case TSDB_DATA_TYPE_NCHAR: {
H
Haojun Liao 已提交
684
        if (taosVariantDump(&tmpVar, tmp, tType, true)) {
W
wpan 已提交
685 686 687 688 689 690 691 692 693 694 695
          goto err_ret;
        }
        t = varDataLen(tmp);
        pvar = varDataVal(tmp);        
        break;
      }
      default:
        goto err_ret;
    }
    
    taosHashPut(pObj, (char *)pvar, t,  &dummy, sizeof(dummy));
H
Haojun Liao 已提交
696
    taosVariantDestroy(&tmpVar);
W
wpan 已提交
697
    memset(&tmpVar, 0, sizeof(tmpVar));
W
wpan 已提交
698 699 700 701 702 703
  } 

  *q = (void *)pObj;
  pObj = NULL;
  
err_ret:  
H
Haojun Liao 已提交
704
  taosVariantDestroy(&tmpVar);
W
wpan 已提交
705 706 707 708
  taosHashCleanup(pObj);
  tfree(tmp);
}

H
Haojun Liao 已提交
709 710
tExprNode* exprdup(tExprNode* pNode) {
  if (pNode == NULL) {
H
Haojun Liao 已提交
711 712 713
    return NULL;
  }

H
Haojun Liao 已提交
714
  tExprNode* pCloned = calloc(1, sizeof(tExprNode));
715
  if (pNode->nodeType == TEXPR_BINARYEXPR_NODE) {
H
Haojun Liao 已提交
716 717 718 719 720 721
    tExprNode* pLeft  = exprdup(pNode->_node.pLeft);
    tExprNode* pRight = exprdup(pNode->_node.pRight);

    pCloned->_node.pLeft  = pLeft;
    pCloned->_node.pRight = pRight;
    pCloned->_node.optr  = pNode->_node.optr;
722
  } else if (pNode->nodeType == TEXPR_VALUE_NODE) {
H
Haojun Liao 已提交
723
    pCloned->pVal = calloc(1, sizeof(SVariant));
H
Haojun Liao 已提交
724
    taosVariantAssign(pCloned->pVal, pNode->pVal);
725
  } else if (pNode->nodeType == TEXPR_COL_NODE) {
H
Haojun Liao 已提交
726 727
    pCloned->pSchema = calloc(1, sizeof(SSchema));
    *pCloned->pSchema = *pNode->pSchema;
H
Haojun Liao 已提交
728 729
  }

H
Haojun Liao 已提交
730 731
  pCloned->nodeType = pNode->nodeType;
  return pCloned;
H
Haojun Liao 已提交
732 733
}