texpr.c 17.1 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/>.
 */

16
#include "function.h"
S
slguan 已提交
17
#include "os.h"
H
Haojun Liao 已提交
18 19

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

29 30 31 32 33 34 35 36 37 38 39 40
//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 已提交
41

H
Haojun Liao 已提交
42 43 44
static void doExprTreeDestroy(tExprNode **pExpr, void (*fp)(void *));

void tExprTreeDestroy(tExprNode *pNode, void (*fp)(void *)) {
H
Haojun Liao 已提交
45 46 47 48
  if (pNode == NULL) {
    return;
  }

49
  if (pNode->nodeType == TEXPR_BINARYEXPR_NODE || pNode->nodeType == TEXPR_UNARYEXPR_NODE) {
H
Haojun Liao 已提交
50
    doExprTreeDestroy(&pNode, fp);
51
  } else if (pNode->nodeType == TEXPR_VALUE_NODE) {
H
Haojun Liao 已提交
52
    taosVariantDestroy(pNode->pVal);
53
  } else if (pNode->nodeType == TEXPR_COL_NODE) {
W
wpan 已提交
54
    tfree(pNode->pSchema);
H
Haojun Liao 已提交
55 56 57 58 59
  }

  free(pNode);
}

H
Haojun Liao 已提交
60
static void doExprTreeDestroy(tExprNode **pExpr, void (*fp)(void *)) {
H
Haojun Liao 已提交
61 62 63
  if (*pExpr == NULL) {
    return;
  }
64 65 66

  int32_t type = (*pExpr)->nodeType;
  if (type == TEXPR_BINARYEXPR_NODE) {
H
Haojun Liao 已提交
67 68
    doExprTreeDestroy(&(*pExpr)->_node.pLeft, fp);
    doExprTreeDestroy(&(*pExpr)->_node.pRight, fp);
H
Haojun Liao 已提交
69 70 71 72
  
    if (fp != NULL) {
      fp((*pExpr)->_node.info);
    }
73 74 75 76 77 78 79 80
  } 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 已提交
81
    taosVariantDestroy((*pExpr)->pVal);
H
Haojun Liao 已提交
82
    free((*pExpr)->pVal);
83
  } else if (type == TEXPR_COL_NODE) {
H
Haojun Liao 已提交
84 85 86 87 88 89 90
    free((*pExpr)->pSchema);
  }

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

H
Haojun Liao 已提交
91
bool exprTreeApplyFilter(tExprNode *pExpr, const void *pItem, SExprTraverseSupp *param) {
H
Haojun Liao 已提交
92 93 94 95
  tExprNode *pLeft  = pExpr->_node.pLeft;
  tExprNode *pRight = pExpr->_node.pRight;

  //non-leaf nodes, recursively traverse the expression tree in the post-root order
96
  if (pLeft->nodeType == TEXPR_BINARYEXPR_NODE && pRight->nodeType == TEXPR_BINARYEXPR_NODE) {
H
Haojun Liao 已提交
97
    if (pExpr->_node.optr == TSDB_RELATION_OR) {  // or
H
Haojun Liao 已提交
98
      if (exprTreeApplyFilter(pLeft, pItem, param)) {
H
Haojun Liao 已提交
99 100 101 102
        return true;
      }

      // left child does not satisfy the query condition, try right child
H
Haojun Liao 已提交
103
      return exprTreeApplyFilter(pRight, pItem, param);
H
Haojun Liao 已提交
104
    } else {  // and
H
Haojun Liao 已提交
105
      if (!exprTreeApplyFilter(pLeft, pItem, param)) {
H
Haojun Liao 已提交
106 107 108
        return false;
      }

H
Haojun Liao 已提交
109
      return exprTreeApplyFilter(pRight, pItem, param);
H
Haojun Liao 已提交
110 111 112 113 114 115 116 117
    }
  }

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

H
hzcheng 已提交
118 119


120 121
static void exprTreeToBinaryImpl(SBufferWriter* bw, tExprNode* expr) {
  tbufWriteUint8(bw, expr->nodeType);
122
  
123
  if (expr->nodeType == TEXPR_VALUE_NODE) {
H
Haojun Liao 已提交
124
    SVariant* pVal = expr->pVal;
125
    
126
    tbufWriteUint32(bw, pVal->nType);
127
    if (pVal->nType == TSDB_DATA_TYPE_BINARY) {
128 129
      tbufWriteInt32(bw, pVal->nLen);
      tbufWrite(bw, pVal->pz, pVal->nLen);
130
    } else {
131
      tbufWriteInt64(bw, pVal->i);
132 133
    }
    
134
  } else if (expr->nodeType == TEXPR_COL_NODE) {
135 136 137 138 139
    SSchema* pSchema = expr->pSchema;
    tbufWriteInt16(bw, pSchema->colId);
    tbufWriteInt16(bw, pSchema->bytes);
    tbufWriteUint8(bw, pSchema->type);
    tbufWriteString(bw, pSchema->name);
140
    
141
  } else if (expr->nodeType == TEXPR_BINARYEXPR_NODE) {
142 143 144
    tbufWriteUint8(bw, expr->_node.optr);
    exprTreeToBinaryImpl(bw, expr->_node.pLeft);
    exprTreeToBinaryImpl(bw, expr->_node.pRight);
145 146 147
  }
}

148 149 150
void exprTreeToBinary(SBufferWriter* bw, tExprNode* expr) {
  if (expr != NULL) {
    exprTreeToBinaryImpl(bw, expr);
151
  }
152 153 154 155 156 157
}

// 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) {
158
    THROW(TSDB_CODE_QRY_OUT_OF_MEMORY);
159
  }
160 161 162 163 164 165
  return p;
}

static void* exception_malloc(size_t size) {
  void* p = malloc(size);
  if (p == NULL) {
166
    THROW(TSDB_CODE_QRY_OUT_OF_MEMORY);
167 168
  }
  return p;
169 170
}

171
static UNUSED_FUNC char* exception_strdup(const char* str) {
172 173
  char* p = strdup(str);
  if (p == NULL) {
174
    THROW(TSDB_CODE_QRY_OUT_OF_MEMORY);
175 176 177 178 179 180
  }
  return p;
}

static tExprNode* exprTreeFromBinaryImpl(SBufferReader* br) {
  int32_t anchor = CLEANUP_GET_ANCHOR();
dengyihao's avatar
TD-816  
dengyihao 已提交
181 182 183 184
  if (CLEANUP_EXCEED_LIMIT()) {
    THROW(TSDB_CODE_QRY_EXCEED_TAGS_LIMIT);
    return NULL;
  }
185 186

  tExprNode* pExpr = exception_calloc(1, sizeof(tExprNode));
H
Haojun Liao 已提交
187
  CLEANUP_PUSH_VOID_PTR_PTR(true, tExprTreeDestroy, pExpr, NULL);
188
  pExpr->nodeType = tbufReadUint8(br);
189
  
190
  if (pExpr->nodeType == TEXPR_VALUE_NODE) {
H
Haojun Liao 已提交
191
    SVariant* pVal = exception_calloc(1, sizeof(SVariant));
weixin_48148422's avatar
weixin_48148422 已提交
192
    pExpr->pVal = pVal;
193
  
194
    pVal->nType = tbufReadUint32(br);
195
    if (pVal->nType == TSDB_DATA_TYPE_BINARY) {
196
      tbufReadToBuffer(br, &pVal->nLen, sizeof(pVal->nLen));
197
      pVal->pz = calloc(1, pVal->nLen + 1);
198
      tbufReadToBuffer(br, pVal->pz, pVal->nLen);
199
    } else {
200
      pVal->i = tbufReadInt64(br);
201 202
    }
    
203
  } else if (pExpr->nodeType == TEXPR_COL_NODE) {
204
    SSchema* pSchema = exception_calloc(1, sizeof(SSchema));
weixin_48148422's avatar
weixin_48148422 已提交
205 206
    pExpr->pSchema = pSchema;

207 208 209 210
    pSchema->colId = tbufReadInt16(br);
    pSchema->bytes = tbufReadInt16(br);
    pSchema->type = tbufReadUint8(br);
    tbufReadToString(br, pSchema->name, TSDB_COL_NAME_LEN);
211
    
212
  } else if (pExpr->nodeType == TEXPR_BINARYEXPR_NODE) {
213 214 215
    pExpr->_node.optr = tbufReadUint8(br);
    pExpr->_node.pLeft = exprTreeFromBinaryImpl(br);
    pExpr->_node.pRight = exprTreeFromBinaryImpl(br);
216 217 218
    assert(pExpr->_node.pLeft != NULL && pExpr->_node.pRight != NULL);
  }
  
219
  CLEANUP_EXECUTE_TO(anchor, false);
weixin_48148422's avatar
weixin_48148422 已提交
220
  return pExpr;
221 222
}

223
tExprNode* exprTreeFromBinary(const void* data, size_t size) {
weixin_48148422's avatar
weixin_48148422 已提交
224 225 226
  if (size == 0) {
    return NULL;
  }
H
Haojun Liao 已提交
227

228 229
  SBufferReader br = tbufInitReader(data, size, false);
  return exprTreeFromBinaryImpl(&br);
230 231
}

weixin_48148422's avatar
weixin_48148422 已提交
232 233 234 235 236
tExprNode* exprTreeFromTableName(const char* tbnameCond) {
  if (!tbnameCond) {
    return NULL;
  }

237 238 239
  int32_t anchor = CLEANUP_GET_ANCHOR();

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

242
  expr->nodeType = TEXPR_BINARYEXPR_NODE;
weixin_48148422's avatar
weixin_48148422 已提交
243

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

247
  left->nodeType = TEXPR_COL_NODE;
248
  SSchema* pSchema = exception_calloc(1, sizeof(SSchema));
weixin_48148422's avatar
weixin_48148422 已提交
249 250
  left->pSchema = pSchema;

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

253
  tExprNode* right = exception_calloc(1, sizeof(tExprNode));
weixin_48148422's avatar
weixin_48148422 已提交
254 255 256
  expr->_node.pRight = right;

  if (strncmp(tbnameCond, QUERY_COND_REL_PREFIX_LIKE, QUERY_COND_REL_PREFIX_LIKE_LEN) == 0) {
257
    right->nodeType = TEXPR_VALUE_NODE;
weixin_48148422's avatar
weixin_48148422 已提交
258
    expr->_node.optr = TSDB_RELATION_LIKE;
H
Haojun Liao 已提交
259
    SVariant* pVal = exception_calloc(1, sizeof(SVariant));
weixin_48148422's avatar
weixin_48148422 已提交
260 261
    right->pVal = pVal;
    size_t len = strlen(tbnameCond + QUERY_COND_REL_PREFIX_LIKE_LEN) + 1;
262
    pVal->pz = exception_malloc(len);
weixin_48148422's avatar
weixin_48148422 已提交
263
    memcpy(pVal->pz, tbnameCond + QUERY_COND_REL_PREFIX_LIKE_LEN, len);
264
    pVal->nType = TSDB_DATA_TYPE_BINARY;
265
    pVal->nLen = (int32_t)len;
weixin_48148422's avatar
weixin_48148422 已提交
266

267
  } else if (strncmp(tbnameCond, QUERY_COND_REL_PREFIX_MATCH, QUERY_COND_REL_PREFIX_MATCH_LEN) == 0) {
268
    right->nodeType = TEXPR_VALUE_NODE;
269
    expr->_node.optr = TSDB_RELATION_MATCH;
H
Haojun Liao 已提交
270
    SVariant* pVal = exception_calloc(1, sizeof(SVariant));
271 272 273 274 275 276
    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;
277
  } else if (strncmp(tbnameCond, QUERY_COND_REL_PREFIX_NMATCH, QUERY_COND_REL_PREFIX_NMATCH_LEN) == 0) {
278
    right->nodeType = TEXPR_VALUE_NODE;
279
    expr->_node.optr = TSDB_RELATION_NMATCH;
H
Haojun Liao 已提交
280
    SVariant* pVal = exception_calloc(1, sizeof(SVariant));
281 282 283 284 285 286
    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 已提交
287
  } else if (strncmp(tbnameCond, QUERY_COND_REL_PREFIX_IN, QUERY_COND_REL_PREFIX_IN_LEN) == 0) {
288
    right->nodeType = TEXPR_VALUE_NODE;
weixin_48148422's avatar
weixin_48148422 已提交
289
    expr->_node.optr = TSDB_RELATION_IN;
H
Haojun Liao 已提交
290
    SVariant* pVal = exception_calloc(1, sizeof(SVariant));
weixin_48148422's avatar
weixin_48148422 已提交
291
    right->pVal = pVal;
W
wpan 已提交
292
    pVal->nType = TSDB_DATA_TYPE_POINTER_ARRAY;
293
    pVal->arr = taosArrayInit(2, POINTER_BYTES);
weixin_48148422's avatar
weixin_48148422 已提交
294 295 296

    const char* cond = tbnameCond + QUERY_COND_REL_PREFIX_IN_LEN;
    for (const char *e = cond; *e != 0; e++) {
297 298 299
      if (*e == TS_PATH_DELIMITER[0]) {
        cond = e + 1;
      } else if (*e == ',') {
300 301
        size_t len = e - cond;
        char* p = exception_malloc(len + VARSTR_HEADER_SIZE);
S
TD-1057  
Shengliang Guan 已提交
302
        STR_WITH_SIZE_TO_VARSTR(p, cond, (VarDataLenT)len);
weixin_48148422's avatar
weixin_48148422 已提交
303
        cond += len;
weixin_48148422's avatar
weixin_48148422 已提交
304
        taosArrayPush(pVal->arr, &p);
weixin_48148422's avatar
weixin_48148422 已提交
305 306 307 308
      }
    }

    if (*cond != 0) {
309 310 311
      size_t len = strlen(cond) + VARSTR_HEADER_SIZE;
      
      char* p = exception_malloc(len);
S
TD-1057  
Shengliang Guan 已提交
312
      STR_WITH_SIZE_TO_VARSTR(p, cond, (VarDataLenT)(len - VARSTR_HEADER_SIZE));
313
      taosArrayPush(pVal->arr, &p);
weixin_48148422's avatar
weixin_48148422 已提交
314 315
    }

316
    taosArraySortString(pVal->arr, taosArrayCompareString);
weixin_48148422's avatar
weixin_48148422 已提交
317 318
  }

319
  CLEANUP_EXECUTE_TO(anchor, false);
weixin_48148422's avatar
weixin_48148422 已提交
320
  return expr;
dengyihao's avatar
dengyihao 已提交
321
}
H
Haojun Liao 已提交
322

Y
yihaoDeng 已提交
323 324 325 326
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);
327
  
H
Haojun Liao 已提交
328 329
//  taosHashSetEqualFp(pObj, taosGetDefaultEqualFunction(type));

Y
yihaoDeng 已提交
330 331 332
  int dummy = -1;
  int32_t sz = tbufReadInt32(&br);
  for (int32_t i = 0; i < sz; i++) {
333
    if (type == TSDB_DATA_TYPE_BOOL || IS_SIGNED_NUMERIC_TYPE(type)) {
334 335
      int64_t val = tbufReadInt64(&br); 
      taosHashPut(pObj, (char *)&val, sizeof(val),  &dummy, sizeof(dummy));
336 337 338 339 340
    } 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 已提交
341 342 343 344 345
      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 已提交
346
    } else if (type == TSDB_DATA_TYPE_BINARY) {
Y
yihaoDeng 已提交
347 348 349
      size_t  t = 0;
      const char *val = tbufReadBinary(&br, &t);
      taosHashPut(pObj, (char *)val, t, &dummy, sizeof(dummy));
Y
yihaoDeng 已提交
350
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
Y
yihaoDeng 已提交
351 352 353
      size_t  t = 0;
      const char *val = tbufReadBinary(&br, &t);      
      taosHashPut(pObj, (char *)val, t, &dummy, sizeof(dummy));
Y
yihaoDeng 已提交
354 355 356 357 358
    }
  } 
  *q = (void *)pObj;
}

W
wpan 已提交
359 360 361 362 363
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 已提交
364
//  taosHashSetEqualFp(pObj, taosGetDefaultEqualFunction(tType));
W
wpan 已提交
365 366
  
  int dummy = -1;
H
Haojun Liao 已提交
367
  SVariant tmpVar = {0};  
W
wpan 已提交
368 369
  size_t  t = 0;
  int32_t sz = tbufReadInt32(&br);
W
wpan 已提交
370 371
  void *pvar = NULL;  
  int64_t val = 0;
W
wpan 已提交
372 373 374 375 376 377 378 379 380 381 382 383
  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 已提交
384
    case TSDB_DATA_TYPE_UTINYINT:
W
wpan 已提交
385
    case TSDB_DATA_TYPE_TINYINT: {
W
wpan 已提交
386
      *(uint8_t *)&val = (uint8_t)tbufReadInt64(&br); 
W
wpan 已提交
387 388 389 390
      t = sizeof(val);
      pvar = &val;
      break;
    }
W
wpan 已提交
391
    case TSDB_DATA_TYPE_USMALLINT:
W
wpan 已提交
392
    case TSDB_DATA_TYPE_SMALLINT: {
W
wpan 已提交
393
      *(uint16_t *)&val = (uint16_t)tbufReadInt64(&br); 
W
wpan 已提交
394 395 396 397
      t = sizeof(val);
      pvar = &val;
      break;
    }
W
wpan 已提交
398
    case TSDB_DATA_TYPE_UINT:
W
wpan 已提交
399
    case TSDB_DATA_TYPE_INT: {
W
wpan 已提交
400
      *(uint32_t *)&val = (uint32_t)tbufReadInt64(&br); 
W
wpan 已提交
401 402 403 404
      t = sizeof(val);
      pvar = &val;
      break;
    }
W
wpan 已提交
405
    case TSDB_DATA_TYPE_TIMESTAMP:
W
wpan 已提交
406
    case TSDB_DATA_TYPE_UBIGINT:
W
wpan 已提交
407
    case TSDB_DATA_TYPE_BIGINT: {
W
wpan 已提交
408
      *(uint64_t *)&val = (uint64_t)tbufReadInt64(&br); 
W
wpan 已提交
409 410 411 412 413
      t = sizeof(val);
      pvar = &val;
      break;
    }
    case TSDB_DATA_TYPE_DOUBLE: {
W
wpan 已提交
414
      *(double *)&val = tbufReadDouble(&br);
W
wpan 已提交
415 416 417 418 419
      t = sizeof(val);
      pvar = &val;
      break;
    }
    case TSDB_DATA_TYPE_FLOAT: {
W
wpan 已提交
420
      *(float *)&val = (float)tbufReadDouble(&br);
W
wpan 已提交
421 422 423 424 425
      t = sizeof(val);
      pvar = &val;
      break;
    }
    case TSDB_DATA_TYPE_BINARY: {
W
wpan 已提交
426
      pvar = (char *)tbufReadBinary(&br, &t);
W
wpan 已提交
427 428 429
      break;
    }
    case TSDB_DATA_TYPE_NCHAR: {
W
wpan 已提交
430
      pvar = (char *)tbufReadBinary(&br, &t);      
W
wpan 已提交
431 432 433 434 435 436 437 438
      break;
    }
    default:
      taosHashCleanup(pObj);
      *q = NULL;
      return;
    }
    
H
Haojun Liao 已提交
439
    taosVariantCreateFromBinary(&tmpVar, (char *)pvar, t, sType);
W
wpan 已提交
440 441 442

    if (bufLen < t) {
      tmp = realloc(tmp, t * TSDB_NCHAR_SIZE);
W
wpan 已提交
443
      bufLen = (int32_t)t;
W
wpan 已提交
444 445 446 447
    }

    switch (tType) {
      case TSDB_DATA_TYPE_BOOL:
W
wpan 已提交
448
      case TSDB_DATA_TYPE_UTINYINT:
W
wpan 已提交
449
      case TSDB_DATA_TYPE_TINYINT: {
H
Haojun Liao 已提交
450
        if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) {
W
wpan 已提交
451 452 453 454 455 456
          goto err_ret;
        }
        pvar = &val;
        t = sizeof(val);
        break;
      }
W
wpan 已提交
457
      case TSDB_DATA_TYPE_USMALLINT:
W
wpan 已提交
458
      case TSDB_DATA_TYPE_SMALLINT: {
H
Haojun Liao 已提交
459
        if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) {
W
wpan 已提交
460 461 462 463 464 465
          goto err_ret;
        }
        pvar = &val;
        t = sizeof(val);
        break;
      }
W
wpan 已提交
466
      case TSDB_DATA_TYPE_UINT:
W
wpan 已提交
467
      case TSDB_DATA_TYPE_INT: {
H
Haojun Liao 已提交
468
        if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) {
W
wpan 已提交
469 470 471 472 473 474
          goto err_ret;
        }
        pvar = &val;
        t = sizeof(val);
        break;
      }
W
wpan 已提交
475
      case TSDB_DATA_TYPE_TIMESTAMP:
W
wpan 已提交
476
      case TSDB_DATA_TYPE_UBIGINT:
W
wpan 已提交
477
      case TSDB_DATA_TYPE_BIGINT: {
H
Haojun Liao 已提交
478
        if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) {
W
wpan 已提交
479 480 481 482 483 484 485
          goto err_ret;
        }
        pvar = &val;
        t = sizeof(val);
        break;
      }
      case TSDB_DATA_TYPE_DOUBLE: {
H
Haojun Liao 已提交
486
        if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) {
W
wpan 已提交
487 488 489 490 491 492 493
          goto err_ret;
        }
        pvar = &val;
        t = sizeof(val);
        break;
      }
      case TSDB_DATA_TYPE_FLOAT: {
H
Haojun Liao 已提交
494
        if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) {
W
wpan 已提交
495 496 497 498 499 500 501
          goto err_ret;
        }
        pvar = &val;
        t = sizeof(val);
        break;
      }
      case TSDB_DATA_TYPE_BINARY: {
H
Haojun Liao 已提交
502
        if (taosVariantDump(&tmpVar, tmp, tType, true)) {
W
wpan 已提交
503 504 505 506 507 508 509
          goto err_ret;
        }
        t = varDataLen(tmp);
        pvar = varDataVal(tmp);
        break;
      }
      case TSDB_DATA_TYPE_NCHAR: {
H
Haojun Liao 已提交
510
        if (taosVariantDump(&tmpVar, tmp, tType, true)) {
W
wpan 已提交
511 512 513 514 515 516 517 518 519 520 521
          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 已提交
522
    taosVariantDestroy(&tmpVar);
W
wpan 已提交
523
    memset(&tmpVar, 0, sizeof(tmpVar));
W
wpan 已提交
524 525 526 527 528 529
  } 

  *q = (void *)pObj;
  pObj = NULL;
  
err_ret:  
H
Haojun Liao 已提交
530
  taosVariantDestroy(&tmpVar);
W
wpan 已提交
531 532 533 534
  taosHashCleanup(pObj);
  tfree(tmp);
}

H
Haojun Liao 已提交
535 536
tExprNode* exprdup(tExprNode* pNode) {
  if (pNode == NULL) {
H
Haojun Liao 已提交
537 538 539
    return NULL;
  }

H
Haojun Liao 已提交
540
  tExprNode* pCloned = calloc(1, sizeof(tExprNode));
541
  if (pNode->nodeType == TEXPR_BINARYEXPR_NODE) {
H
Haojun Liao 已提交
542 543 544 545 546 547
    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;
548
  } else if (pNode->nodeType == TEXPR_VALUE_NODE) {
H
Haojun Liao 已提交
549
    pCloned->pVal = calloc(1, sizeof(SVariant));
H
Haojun Liao 已提交
550
    taosVariantAssign(pCloned->pVal, pNode->pVal);
551
  } else if (pNode->nodeType == TEXPR_COL_NODE) {
H
Haojun Liao 已提交
552 553
    pCloned->pSchema = calloc(1, sizeof(SSchema));
    *pCloned->pSchema = *pNode->pSchema;
554 555 556 557 558 559 560 561 562
  } else if (pNode->nodeType == TEXPR_FUNCTION_NODE) {
    strcpy(pCloned->_function.functionName, pNode->_function.functionName);

    int32_t num = pNode->_function.num;
    pCloned->_function.num = num;
    pCloned->_function.pChild = calloc(num, POINTER_BYTES);
    for(int32_t i = 0; i < num; ++i) {
      pCloned->_function.pChild[i] = exprdup(pNode->_function.pChild[i]);
    }
H
Haojun Liao 已提交
563 564
  }

H
Haojun Liao 已提交
565 566
  pCloned->nodeType = pNode->nodeType;
  return pCloned;
H
Haojun Liao 已提交
567 568
}