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

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

S
Shengliang Guan 已提交
19
#include "texception.h"
20
#include "taosdef.h"
H
Hongze Cheng 已提交
21
#include "tmsg.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"
D
dapan1121 已提交
28
#include "tdef.h"
H
hzcheng 已提交
29

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

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

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

50
  if (pNode->nodeType == TEXPR_BINARYEXPR_NODE || pNode->nodeType == TEXPR_UNARYEXPR_NODE) {
H
Haojun Liao 已提交
51
    doExprTreeDestroy(&pNode, fp);
52
  } else if (pNode->nodeType == TEXPR_VALUE_NODE) {
H
Haojun Liao 已提交
53
    taosVariantDestroy(pNode->pVal);
54
  } else if (pNode->nodeType == TEXPR_COL_NODE) {
wafwerar's avatar
wafwerar 已提交
55
    taosMemoryFreeClear(pNode->pSchema);
H
Haojun Liao 已提交
56 57
  }

wafwerar's avatar
wafwerar 已提交
58
  taosMemoryFree(pNode);
H
Haojun Liao 已提交
59 60
}

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

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

wafwerar's avatar
wafwerar 已提交
88
  taosMemoryFree(*pExpr);
H
Haojun Liao 已提交
89 90 91
  *pExpr = NULL;
}

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

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

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

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

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

119 120
// TODO: these three functions should be made global
static void* exception_calloc(size_t nmemb, size_t size) {
wafwerar's avatar
wafwerar 已提交
121
  void* p = taosMemoryCalloc(nmemb, size);
122
  if (p == NULL) {
123
    THROW(TSDB_CODE_QRY_OUT_OF_MEMORY);
124
  }
125 126 127 128
  return p;
}

static void* exception_malloc(size_t size) {
wafwerar's avatar
wafwerar 已提交
129
  void* p = taosMemoryMalloc(size);
130
  if (p == NULL) {
131
    THROW(TSDB_CODE_QRY_OUT_OF_MEMORY);
132 133
  }
  return p;
134 135
}

136
static UNUSED_FUNC char* exception_strdup(const char* str) {
137 138
  char* p = strdup(str);
  if (p == NULL) {
139
    THROW(TSDB_CODE_QRY_OUT_OF_MEMORY);
140 141 142 143 144 145
  }
  return p;
}

static tExprNode* exprTreeFromBinaryImpl(SBufferReader* br) {
  int32_t anchor = CLEANUP_GET_ANCHOR();
dengyihao's avatar
TD-816  
dengyihao 已提交
146 147 148 149
  if (CLEANUP_EXCEED_LIMIT()) {
    THROW(TSDB_CODE_QRY_EXCEED_TAGS_LIMIT);
    return NULL;
  }
150 151

  tExprNode* pExpr = exception_calloc(1, sizeof(tExprNode));
H
Haojun Liao 已提交
152
  CLEANUP_PUSH_VOID_PTR_PTR(true, tExprTreeDestroy, pExpr, NULL);
153
  pExpr->nodeType = tbufReadUint8(br);
154
  
155
  if (pExpr->nodeType == TEXPR_VALUE_NODE) {
H
Haojun Liao 已提交
156
    SVariant* pVal = exception_calloc(1, sizeof(SVariant));
weixin_48148422's avatar
weixin_48148422 已提交
157
    pExpr->pVal = pVal;
158
  
159
    pVal->nType = tbufReadUint32(br);
160
    if (pVal->nType == TSDB_DATA_TYPE_BINARY) {
161
      tbufReadToBuffer(br, &pVal->nLen, sizeof(pVal->nLen));
wafwerar's avatar
wafwerar 已提交
162
      pVal->pz = taosMemoryCalloc(1, pVal->nLen + 1);
163
      tbufReadToBuffer(br, pVal->pz, pVal->nLen);
164
    } else {
165
      pVal->i = tbufReadInt64(br);
166 167
    }
    
168
  } else if (pExpr->nodeType == TEXPR_COL_NODE) {
169
    SSchema* pSchema = exception_calloc(1, sizeof(SSchema));
weixin_48148422's avatar
weixin_48148422 已提交
170 171
    pExpr->pSchema = pSchema;

172 173 174 175
    pSchema->colId = tbufReadInt16(br);
    pSchema->bytes = tbufReadInt16(br);
    pSchema->type = tbufReadUint8(br);
    tbufReadToString(br, pSchema->name, TSDB_COL_NAME_LEN);
176
    
177
  } else if (pExpr->nodeType == TEXPR_BINARYEXPR_NODE) {
178 179 180
    pExpr->_node.optr = tbufReadUint8(br);
    pExpr->_node.pLeft = exprTreeFromBinaryImpl(br);
    pExpr->_node.pRight = exprTreeFromBinaryImpl(br);
181 182 183
    assert(pExpr->_node.pLeft != NULL && pExpr->_node.pRight != NULL);
  }
  
184
  CLEANUP_EXECUTE_TO(anchor, false);
weixin_48148422's avatar
weixin_48148422 已提交
185
  return pExpr;
186 187
}

188
tExprNode* exprTreeFromBinary(const void* data, size_t size) {
weixin_48148422's avatar
weixin_48148422 已提交
189 190 191
  if (size == 0) {
    return NULL;
  }
H
Haojun Liao 已提交
192

193 194
  SBufferReader br = tbufInitReader(data, size, false);
  return exprTreeFromBinaryImpl(&br);
195 196
}

Y
yihaoDeng 已提交
197 198 199 200
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);
201
  
H
Haojun Liao 已提交
202 203
//  taosHashSetEqualFp(pObj, taosGetDefaultEqualFunction(type));

Y
yihaoDeng 已提交
204 205 206
  int dummy = -1;
  int32_t sz = tbufReadInt32(&br);
  for (int32_t i = 0; i < sz; i++) {
207
    if (type == TSDB_DATA_TYPE_BOOL || IS_SIGNED_NUMERIC_TYPE(type)) {
208 209
      int64_t val = tbufReadInt64(&br); 
      taosHashPut(pObj, (char *)&val, sizeof(val),  &dummy, sizeof(dummy));
210 211 212 213 214
    } 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 已提交
215 216 217 218 219
      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 已提交
220
    } else if (type == TSDB_DATA_TYPE_BINARY) {
Y
yihaoDeng 已提交
221 222 223
      size_t  t = 0;
      const char *val = tbufReadBinary(&br, &t);
      taosHashPut(pObj, (char *)val, t, &dummy, sizeof(dummy));
Y
yihaoDeng 已提交
224
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
Y
yihaoDeng 已提交
225 226 227
      size_t  t = 0;
      const char *val = tbufReadBinary(&br, &t);      
      taosHashPut(pObj, (char *)val, t, &dummy, sizeof(dummy));
Y
yihaoDeng 已提交
228 229 230 231 232
    }
  } 
  *q = (void *)pObj;
}

W
wpan 已提交
233 234 235 236 237
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 已提交
238
//  taosHashSetEqualFp(pObj, taosGetDefaultEqualFunction(tType));
W
wpan 已提交
239 240
  
  int dummy = -1;
H
Haojun Liao 已提交
241
  SVariant tmpVar = {0};  
W
wpan 已提交
242 243
  size_t  t = 0;
  int32_t sz = tbufReadInt32(&br);
W
wpan 已提交
244 245
  void *pvar = NULL;  
  int64_t val = 0;
W
wpan 已提交
246 247 248 249 250 251 252
  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;
  }

wafwerar's avatar
wafwerar 已提交
253
  char *tmp = taosMemoryCalloc(1, bufLen * TSDB_NCHAR_SIZE);
W
wpan 已提交
254 255 256 257
    
  for (int32_t i = 0; i < sz; i++) {
    switch (sType) {
    case TSDB_DATA_TYPE_BOOL:
W
wpan 已提交
258
    case TSDB_DATA_TYPE_UTINYINT:
W
wpan 已提交
259
    case TSDB_DATA_TYPE_TINYINT: {
W
wpan 已提交
260
      *(uint8_t *)&val = (uint8_t)tbufReadInt64(&br); 
W
wpan 已提交
261 262 263 264
      t = sizeof(val);
      pvar = &val;
      break;
    }
W
wpan 已提交
265
    case TSDB_DATA_TYPE_USMALLINT:
W
wpan 已提交
266
    case TSDB_DATA_TYPE_SMALLINT: {
W
wpan 已提交
267
      *(uint16_t *)&val = (uint16_t)tbufReadInt64(&br); 
W
wpan 已提交
268 269 270 271
      t = sizeof(val);
      pvar = &val;
      break;
    }
W
wpan 已提交
272
    case TSDB_DATA_TYPE_UINT:
W
wpan 已提交
273
    case TSDB_DATA_TYPE_INT: {
W
wpan 已提交
274
      *(uint32_t *)&val = (uint32_t)tbufReadInt64(&br); 
W
wpan 已提交
275 276 277 278
      t = sizeof(val);
      pvar = &val;
      break;
    }
W
wpan 已提交
279
    case TSDB_DATA_TYPE_TIMESTAMP:
W
wpan 已提交
280
    case TSDB_DATA_TYPE_UBIGINT:
W
wpan 已提交
281
    case TSDB_DATA_TYPE_BIGINT: {
W
wpan 已提交
282
      *(uint64_t *)&val = (uint64_t)tbufReadInt64(&br); 
W
wpan 已提交
283 284 285 286 287
      t = sizeof(val);
      pvar = &val;
      break;
    }
    case TSDB_DATA_TYPE_DOUBLE: {
W
wpan 已提交
288
      *(double *)&val = tbufReadDouble(&br);
W
wpan 已提交
289 290 291 292 293
      t = sizeof(val);
      pvar = &val;
      break;
    }
    case TSDB_DATA_TYPE_FLOAT: {
W
wpan 已提交
294
      *(float *)&val = (float)tbufReadDouble(&br);
W
wpan 已提交
295 296 297 298 299
      t = sizeof(val);
      pvar = &val;
      break;
    }
    case TSDB_DATA_TYPE_BINARY: {
W
wpan 已提交
300
      pvar = (char *)tbufReadBinary(&br, &t);
W
wpan 已提交
301 302 303
      break;
    }
    case TSDB_DATA_TYPE_NCHAR: {
W
wpan 已提交
304
      pvar = (char *)tbufReadBinary(&br, &t);      
W
wpan 已提交
305 306 307 308 309 310 311 312
      break;
    }
    default:
      taosHashCleanup(pObj);
      *q = NULL;
      return;
    }
    
H
Haojun Liao 已提交
313
    taosVariantCreateFromBinary(&tmpVar, (char *)pvar, t, sType);
W
wpan 已提交
314 315

    if (bufLen < t) {
wafwerar's avatar
wafwerar 已提交
316
      tmp = taosMemoryRealloc(tmp, t * TSDB_NCHAR_SIZE);
W
wpan 已提交
317
      bufLen = (int32_t)t;
W
wpan 已提交
318 319 320 321
    }

    switch (tType) {
      case TSDB_DATA_TYPE_BOOL:
W
wpan 已提交
322
      case TSDB_DATA_TYPE_UTINYINT:
W
wpan 已提交
323
      case TSDB_DATA_TYPE_TINYINT: {
H
Haojun Liao 已提交
324
        if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) {
W
wpan 已提交
325 326 327 328 329 330
          goto err_ret;
        }
        pvar = &val;
        t = sizeof(val);
        break;
      }
W
wpan 已提交
331
      case TSDB_DATA_TYPE_USMALLINT:
W
wpan 已提交
332
      case TSDB_DATA_TYPE_SMALLINT: {
H
Haojun Liao 已提交
333
        if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) {
W
wpan 已提交
334 335 336 337 338 339
          goto err_ret;
        }
        pvar = &val;
        t = sizeof(val);
        break;
      }
W
wpan 已提交
340
      case TSDB_DATA_TYPE_UINT:
W
wpan 已提交
341
      case TSDB_DATA_TYPE_INT: {
H
Haojun Liao 已提交
342
        if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) {
W
wpan 已提交
343 344 345 346 347 348
          goto err_ret;
        }
        pvar = &val;
        t = sizeof(val);
        break;
      }
W
wpan 已提交
349
      case TSDB_DATA_TYPE_TIMESTAMP:
W
wpan 已提交
350
      case TSDB_DATA_TYPE_UBIGINT:
W
wpan 已提交
351
      case TSDB_DATA_TYPE_BIGINT: {
H
Haojun Liao 已提交
352
        if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) {
W
wpan 已提交
353 354 355 356 357 358 359
          goto err_ret;
        }
        pvar = &val;
        t = sizeof(val);
        break;
      }
      case TSDB_DATA_TYPE_DOUBLE: {
H
Haojun Liao 已提交
360
        if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) {
W
wpan 已提交
361 362 363 364 365 366 367
          goto err_ret;
        }
        pvar = &val;
        t = sizeof(val);
        break;
      }
      case TSDB_DATA_TYPE_FLOAT: {
H
Haojun Liao 已提交
368
        if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) {
W
wpan 已提交
369 370 371 372 373 374 375
          goto err_ret;
        }
        pvar = &val;
        t = sizeof(val);
        break;
      }
      case TSDB_DATA_TYPE_BINARY: {
H
Haojun Liao 已提交
376
        if (taosVariantDump(&tmpVar, tmp, tType, true)) {
W
wpan 已提交
377 378 379 380 381 382 383
          goto err_ret;
        }
        t = varDataLen(tmp);
        pvar = varDataVal(tmp);
        break;
      }
      case TSDB_DATA_TYPE_NCHAR: {
H
Haojun Liao 已提交
384
        if (taosVariantDump(&tmpVar, tmp, tType, true)) {
W
wpan 已提交
385 386 387 388 389 390 391 392 393 394 395
          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 已提交
396
    taosVariantDestroy(&tmpVar);
W
wpan 已提交
397
    memset(&tmpVar, 0, sizeof(tmpVar));
W
wpan 已提交
398 399 400 401 402 403
  } 

  *q = (void *)pObj;
  pObj = NULL;
  
err_ret:  
H
Haojun Liao 已提交
404
  taosVariantDestroy(&tmpVar);
W
wpan 已提交
405
  taosHashCleanup(pObj);
wafwerar's avatar
wafwerar 已提交
406
  taosMemoryFreeClear(tmp);
W
wpan 已提交
407 408
}

H
Haojun Liao 已提交
409 410
tExprNode* exprdup(tExprNode* pNode) {
  if (pNode == NULL) {
H
Haojun Liao 已提交
411 412 413
    return NULL;
  }

wafwerar's avatar
wafwerar 已提交
414
  tExprNode* pCloned = taosMemoryCalloc(1, sizeof(tExprNode));
415
  if (pNode->nodeType == TEXPR_BINARYEXPR_NODE) {
H
Haojun Liao 已提交
416 417 418 419 420 421
    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;
422
  } else if (pNode->nodeType == TEXPR_VALUE_NODE) {
wafwerar's avatar
wafwerar 已提交
423
    pCloned->pVal = taosMemoryCalloc(1, sizeof(SVariant));
H
Haojun Liao 已提交
424
    taosVariantAssign(pCloned->pVal, pNode->pVal);
425
  } else if (pNode->nodeType == TEXPR_COL_NODE) {
wafwerar's avatar
wafwerar 已提交
426
    pCloned->pSchema = taosMemoryCalloc(1, sizeof(SSchema));
H
Haojun Liao 已提交
427
    *pCloned->pSchema = *pNode->pSchema;
428 429 430 431 432
  } else if (pNode->nodeType == TEXPR_FUNCTION_NODE) {
    strcpy(pCloned->_function.functionName, pNode->_function.functionName);

    int32_t num = pNode->_function.num;
    pCloned->_function.num = num;
wafwerar's avatar
wafwerar 已提交
433
    pCloned->_function.pChild = taosMemoryCalloc(num, POINTER_BYTES);
434 435 436
    for(int32_t i = 0; i < num; ++i) {
      pCloned->_function.pChild[i] = exprdup(pNode->_function.pChild[i]);
    }
H
Haojun Liao 已提交
437 438
  }

H
Haojun Liao 已提交
439 440
  pCloned->nodeType = pNode->nodeType;
  return pCloned;
H
Haojun Liao 已提交
441 442
}