texpr.c 9.7 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

  int32_t type = (*pExpr)->nodeType;
67
  if (type == TEXPR_VALUE_NODE) {
H
Haojun Liao 已提交
68
    taosVariantDestroy((*pExpr)->pVal);
wafwerar's avatar
wafwerar 已提交
69
    taosMemoryFree((*pExpr)->pVal);
70
  } else if (type == TEXPR_COL_NODE) {
wafwerar's avatar
wafwerar 已提交
71
    taosMemoryFree((*pExpr)->pSchema);
H
Haojun Liao 已提交
72 73
  }

wafwerar's avatar
wafwerar 已提交
74
  taosMemoryFree(*pExpr);
H
Haojun Liao 已提交
75 76 77
  *pExpr = NULL;
}

H
Haojun Liao 已提交
78
bool exprTreeApplyFilter(tExprNode *pExpr, const void *pItem, SExprTraverseSupp *param) {
79
#if 0
H
Haojun Liao 已提交
80
  //non-leaf nodes, recursively traverse the expression tree in the post-root order
81
  if (pLeft->nodeType == TEXPR_BINARYEXPR_NODE && pRight->nodeType == TEXPR_BINARYEXPR_NODE) {
D
dapan1121 已提交
82
    if (pExpr->_node.optr == LOGIC_COND_TYPE_OR) {  // or
H
Haojun Liao 已提交
83
      if (exprTreeApplyFilter(pLeft, pItem, param)) {
H
Haojun Liao 已提交
84 85 86 87
        return true;
      }

      // left child does not satisfy the query condition, try right child
H
Haojun Liao 已提交
88
      return exprTreeApplyFilter(pRight, pItem, param);
H
Haojun Liao 已提交
89
    } else {  // and
H
Haojun Liao 已提交
90
      if (!exprTreeApplyFilter(pLeft, pItem, param)) {
H
Haojun Liao 已提交
91 92 93
        return false;
      }

H
Haojun Liao 已提交
94
      return exprTreeApplyFilter(pRight, pItem, param);
H
Haojun Liao 已提交
95 96 97 98 99 100
    }
  }

  // handle the leaf node
  param->setupInfoFn(pExpr, param->pExtInfo);
  return param->nodeFilterFn(pItem, pExpr->_node.info);
101 102 103
#endif

  return 0;
H
Haojun Liao 已提交
104 105
}

106 107
// TODO: these three functions should be made global
static void* exception_calloc(size_t nmemb, size_t size) {
wafwerar's avatar
wafwerar 已提交
108
  void* p = taosMemoryCalloc(nmemb, size);
109
  if (p == NULL) {
110
    THROW(TSDB_CODE_QRY_OUT_OF_MEMORY);
111
  }
112 113 114 115
  return p;
}

static void* exception_malloc(size_t size) {
wafwerar's avatar
wafwerar 已提交
116
  void* p = taosMemoryMalloc(size);
117
  if (p == NULL) {
118
    THROW(TSDB_CODE_QRY_OUT_OF_MEMORY);
119 120
  }
  return p;
121 122
}

123
static UNUSED_FUNC char* exception_strdup(const char* str) {
124 125
  char* p = strdup(str);
  if (p == NULL) {
126
    THROW(TSDB_CODE_QRY_OUT_OF_MEMORY);
127 128 129 130
  }
  return p;
}

Y
yihaoDeng 已提交
131 132 133 134
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);
135
  
H
Haojun Liao 已提交
136 137
//  taosHashSetEqualFp(pObj, taosGetDefaultEqualFunction(type));

Y
yihaoDeng 已提交
138 139 140
  int dummy = -1;
  int32_t sz = tbufReadInt32(&br);
  for (int32_t i = 0; i < sz; i++) {
141
    if (type == TSDB_DATA_TYPE_BOOL || IS_SIGNED_NUMERIC_TYPE(type)) {
142 143
      int64_t val = tbufReadInt64(&br); 
      taosHashPut(pObj, (char *)&val, sizeof(val),  &dummy, sizeof(dummy));
144 145 146 147 148
    } 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 已提交
149 150 151 152 153
      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 已提交
154
    } else if (type == TSDB_DATA_TYPE_BINARY) {
Y
yihaoDeng 已提交
155 156 157
      size_t  t = 0;
      const char *val = tbufReadBinary(&br, &t);
      taosHashPut(pObj, (char *)val, t, &dummy, sizeof(dummy));
Y
yihaoDeng 已提交
158
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
Y
yihaoDeng 已提交
159 160 161
      size_t  t = 0;
      const char *val = tbufReadBinary(&br, &t);      
      taosHashPut(pObj, (char *)val, t, &dummy, sizeof(dummy));
Y
yihaoDeng 已提交
162 163 164 165 166
    }
  } 
  *q = (void *)pObj;
}

W
wpan 已提交
167 168 169 170 171
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 已提交
172
//  taosHashSetEqualFp(pObj, taosGetDefaultEqualFunction(tType));
W
wpan 已提交
173 174
  
  int dummy = -1;
H
Haojun Liao 已提交
175
  SVariant tmpVar = {0};  
W
wpan 已提交
176 177
  size_t  t = 0;
  int32_t sz = tbufReadInt32(&br);
W
wpan 已提交
178 179
  void *pvar = NULL;  
  int64_t val = 0;
W
wpan 已提交
180 181 182 183 184 185 186
  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 已提交
187
  char *tmp = taosMemoryCalloc(1, bufLen * TSDB_NCHAR_SIZE);
W
wpan 已提交
188 189 190 191
    
  for (int32_t i = 0; i < sz; i++) {
    switch (sType) {
    case TSDB_DATA_TYPE_BOOL:
W
wpan 已提交
192
    case TSDB_DATA_TYPE_UTINYINT:
W
wpan 已提交
193
    case TSDB_DATA_TYPE_TINYINT: {
W
wpan 已提交
194
      *(uint8_t *)&val = (uint8_t)tbufReadInt64(&br); 
W
wpan 已提交
195 196 197 198
      t = sizeof(val);
      pvar = &val;
      break;
    }
W
wpan 已提交
199
    case TSDB_DATA_TYPE_USMALLINT:
W
wpan 已提交
200
    case TSDB_DATA_TYPE_SMALLINT: {
W
wpan 已提交
201
      *(uint16_t *)&val = (uint16_t)tbufReadInt64(&br); 
W
wpan 已提交
202 203 204 205
      t = sizeof(val);
      pvar = &val;
      break;
    }
W
wpan 已提交
206
    case TSDB_DATA_TYPE_UINT:
W
wpan 已提交
207
    case TSDB_DATA_TYPE_INT: {
W
wpan 已提交
208
      *(uint32_t *)&val = (uint32_t)tbufReadInt64(&br); 
W
wpan 已提交
209 210 211 212
      t = sizeof(val);
      pvar = &val;
      break;
    }
W
wpan 已提交
213
    case TSDB_DATA_TYPE_TIMESTAMP:
W
wpan 已提交
214
    case TSDB_DATA_TYPE_UBIGINT:
W
wpan 已提交
215
    case TSDB_DATA_TYPE_BIGINT: {
W
wpan 已提交
216
      *(uint64_t *)&val = (uint64_t)tbufReadInt64(&br); 
W
wpan 已提交
217 218 219 220 221
      t = sizeof(val);
      pvar = &val;
      break;
    }
    case TSDB_DATA_TYPE_DOUBLE: {
W
wpan 已提交
222
      *(double *)&val = tbufReadDouble(&br);
W
wpan 已提交
223 224 225 226 227
      t = sizeof(val);
      pvar = &val;
      break;
    }
    case TSDB_DATA_TYPE_FLOAT: {
W
wpan 已提交
228
      *(float *)&val = (float)tbufReadDouble(&br);
W
wpan 已提交
229 230 231 232 233
      t = sizeof(val);
      pvar = &val;
      break;
    }
    case TSDB_DATA_TYPE_BINARY: {
W
wpan 已提交
234
      pvar = (char *)tbufReadBinary(&br, &t);
W
wpan 已提交
235 236 237
      break;
    }
    case TSDB_DATA_TYPE_NCHAR: {
W
wpan 已提交
238
      pvar = (char *)tbufReadBinary(&br, &t);      
W
wpan 已提交
239 240 241 242 243 244 245 246
      break;
    }
    default:
      taosHashCleanup(pObj);
      *q = NULL;
      return;
    }
    
H
Haojun Liao 已提交
247
    taosVariantCreateFromBinary(&tmpVar, (char *)pvar, t, sType);
W
wpan 已提交
248 249

    if (bufLen < t) {
wafwerar's avatar
wafwerar 已提交
250
      tmp = taosMemoryRealloc(tmp, t * TSDB_NCHAR_SIZE);
W
wpan 已提交
251
      bufLen = (int32_t)t;
W
wpan 已提交
252 253 254 255
    }

    switch (tType) {
      case TSDB_DATA_TYPE_BOOL:
W
wpan 已提交
256
      case TSDB_DATA_TYPE_UTINYINT:
W
wpan 已提交
257
      case TSDB_DATA_TYPE_TINYINT: {
H
Haojun Liao 已提交
258
        if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) {
W
wpan 已提交
259 260 261 262 263 264
          goto err_ret;
        }
        pvar = &val;
        t = sizeof(val);
        break;
      }
W
wpan 已提交
265
      case TSDB_DATA_TYPE_USMALLINT:
W
wpan 已提交
266
      case TSDB_DATA_TYPE_SMALLINT: {
H
Haojun Liao 已提交
267
        if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) {
W
wpan 已提交
268 269 270 271 272 273
          goto err_ret;
        }
        pvar = &val;
        t = sizeof(val);
        break;
      }
W
wpan 已提交
274
      case TSDB_DATA_TYPE_UINT:
W
wpan 已提交
275
      case TSDB_DATA_TYPE_INT: {
H
Haojun Liao 已提交
276
        if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) {
W
wpan 已提交
277 278 279 280 281 282
          goto err_ret;
        }
        pvar = &val;
        t = sizeof(val);
        break;
      }
W
wpan 已提交
283
      case TSDB_DATA_TYPE_TIMESTAMP:
W
wpan 已提交
284
      case TSDB_DATA_TYPE_UBIGINT:
W
wpan 已提交
285
      case TSDB_DATA_TYPE_BIGINT: {
H
Haojun Liao 已提交
286
        if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) {
W
wpan 已提交
287 288 289 290 291 292 293
          goto err_ret;
        }
        pvar = &val;
        t = sizeof(val);
        break;
      }
      case TSDB_DATA_TYPE_DOUBLE: {
H
Haojun Liao 已提交
294
        if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) {
W
wpan 已提交
295 296 297 298 299 300 301
          goto err_ret;
        }
        pvar = &val;
        t = sizeof(val);
        break;
      }
      case TSDB_DATA_TYPE_FLOAT: {
H
Haojun Liao 已提交
302
        if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) {
W
wpan 已提交
303 304 305 306 307 308 309
          goto err_ret;
        }
        pvar = &val;
        t = sizeof(val);
        break;
      }
      case TSDB_DATA_TYPE_BINARY: {
H
Haojun Liao 已提交
310
        if (taosVariantDump(&tmpVar, tmp, tType, true)) {
W
wpan 已提交
311 312 313 314 315 316 317
          goto err_ret;
        }
        t = varDataLen(tmp);
        pvar = varDataVal(tmp);
        break;
      }
      case TSDB_DATA_TYPE_NCHAR: {
H
Haojun Liao 已提交
318
        if (taosVariantDump(&tmpVar, tmp, tType, true)) {
W
wpan 已提交
319 320 321 322 323 324 325 326 327 328 329
          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 已提交
330
    taosVariantDestroy(&tmpVar);
W
wpan 已提交
331
    memset(&tmpVar, 0, sizeof(tmpVar));
W
wpan 已提交
332 333 334 335 336 337
  } 

  *q = (void *)pObj;
  pObj = NULL;
  
err_ret:  
H
Haojun Liao 已提交
338
  taosVariantDestroy(&tmpVar);
W
wpan 已提交
339
  taosHashCleanup(pObj);
wafwerar's avatar
wafwerar 已提交
340
  taosMemoryFreeClear(tmp);
W
wpan 已提交
341
}