indexoperator.c 18.6 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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/>.
 */

#include "indexoperator.h"
#include "executorimpl.h"
dengyihao's avatar
dengyihao 已提交
18
#include "index.h"
dengyihao's avatar
dengyihao 已提交
19
#include "nodes.h"
dengyihao's avatar
dengyihao 已提交
20
#include "tdatablock.h"
dengyihao's avatar
dengyihao 已提交
21 22 23

typedef struct SIFCtx {
  int32_t   code;
dengyihao's avatar
dengyihao 已提交
24 25 26
  SHashObj *pRes;    /* element is SScalarParam */
  bool      noExec;  // true: just iterate condition tree, and add hint to executor plan
  // SIdxFltStatus st;
dengyihao's avatar
dengyihao 已提交
27 28
} SIFCtx;

dengyihao's avatar
dengyihao 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#define SIF_ERR_RET(c)                \
  do {                                \
    int32_t _code = c;                \
    if (_code != TSDB_CODE_SUCCESS) { \
      terrno = _code;                 \
      return _code;                   \
    }                                 \
  } while (0)
#define SIF_RET(c)                    \
  do {                                \
    int32_t _code = c;                \
    if (_code != TSDB_CODE_SUCCESS) { \
      terrno = _code;                 \
    }                                 \
    return _code;                     \
  } while (0)
#define SIF_ERR_JRET(c)              \
  do {                               \
    code = c;                        \
    if (code != TSDB_CODE_SUCCESS) { \
      terrno = code;                 \
      goto _return;                  \
    }                                \
  } while (0)

dengyihao's avatar
dengyihao 已提交
54 55
typedef struct SIFParam {
  SHashObj *pFilter;
dengyihao's avatar
dengyihao 已提交
56 57 58 59

  SArray *result;
  char *  condValue;

dengyihao's avatar
dengyihao 已提交
60 61 62 63 64 65
  SIdxFltStatus status;
  uint8_t       colValType;
  col_id_t      colId;
  int64_t       suid;  // add later
  char          dbName[TSDB_DB_NAME_LEN];
  char          colName[TSDB_COL_NAME_LEN];
dengyihao's avatar
dengyihao 已提交
66
} SIFParam;
dengyihao's avatar
dengyihao 已提交
67

dengyihao's avatar
dengyihao 已提交
68
static int32_t sifGetFuncFromSql(EOperatorType src, EIndexQueryType *dst) {
dengyihao's avatar
dengyihao 已提交
69 70 71 72 73 74 75 76
  if (src == OP_TYPE_GREATER_THAN) {
    *dst = QUERY_GREATER_THAN;
  } else if (src == OP_TYPE_GREATER_EQUAL) {
    *dst = QUERY_GREATER_EQUAL;
  } else if (src == OP_TYPE_LOWER_THAN) {
    *dst = QUERY_LESS_THAN;
  } else if (src == OP_TYPE_LOWER_EQUAL) {
    *dst = QUERY_LESS_EQUAL;
dengyihao's avatar
dengyihao 已提交
77 78 79 80 81 82 83 84 85 86
  } else if (src == OP_TYPE_EQUAL) {
    *dst = QUERY_TERM;
  } else if (src == OP_TYPE_LIKE || src == OP_TYPE_MATCH || src == OP_TYPE_NMATCH) {
    *dst = QUERY_REGEX;
  } else {
    return TSDB_CODE_QRY_INVALID_INPUT;
  }
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
87
typedef int32_t (*sif_func_t)(SIFParam *left, SIFParam *rigth, SIFParam *output);
dengyihao's avatar
dengyihao 已提交
88 89 90

static sif_func_t sifNullFunc = NULL;
// typedef struct SIFWalkParm
dengyihao's avatar
dengyihao 已提交
91
// construct tag filter operator later
dengyihao's avatar
dengyihao 已提交
92 93 94
static void destroyTagFilterOperatorInfo(void *param) {
  STagFilterOperatorInfo *pInfo = (STagFilterOperatorInfo *)param;
}
dengyihao's avatar
dengyihao 已提交
95 96 97

static void sifFreeParam(SIFParam *param) {
  if (param == NULL) return;
dengyihao's avatar
dengyihao 已提交
98

dengyihao's avatar
dengyihao 已提交
99
  taosArrayDestroy(param->result);
dengyihao's avatar
dengyihao 已提交
100 101
  taosMemoryFree(param->condValue);
  taosHashCleanup(param->pFilter);
dengyihao's avatar
dengyihao 已提交
102 103
}

dengyihao's avatar
dengyihao 已提交
104
static int32_t sifGetOperParamNum(EOperatorType ty) {
dengyihao's avatar
dengyihao 已提交
105 106 107
  if (OP_TYPE_IS_NULL == ty || OP_TYPE_IS_NOT_NULL == ty || OP_TYPE_IS_TRUE == ty || OP_TYPE_IS_NOT_TRUE == ty ||
      OP_TYPE_IS_FALSE == ty || OP_TYPE_IS_NOT_FALSE == ty || OP_TYPE_IS_UNKNOWN == ty ||
      OP_TYPE_IS_NOT_UNKNOWN == ty || OP_TYPE_MINUS == ty) {
dengyihao's avatar
dengyihao 已提交
108 109 110 111
    return 1;
  }
  return 2;
}
dengyihao's avatar
dengyihao 已提交
112 113 114 115 116 117 118 119 120 121 122
static int32_t sifValidateColumn(SColumnNode *cn) {
  // add more check
  if (cn == NULL) {
    return TSDB_CODE_QRY_INVALID_INPUT;
  }
  if (cn->colType != COLUMN_TYPE_TAG) {
    return TSDB_CODE_QRY_INVALID_INPUT;
  }
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
static SIdxFltStatus sifMergeCond(ELogicConditionType type, SIdxFltStatus ls, SIdxFltStatus rs) {
  // enh rule later
  if (type == LOGIC_COND_TYPE_AND) {
    if (ls == SFLT_NOT_INDEX || rs == SFLT_NOT_INDEX) {
      if (ls == SFLT_NOT_INDEX)
        return rs;
      else
        return ls;
    }
    return SFLT_COARSE_INDEX;
  } else if (type == LOGIC_COND_TYPE_OR) {
    return SFLT_COARSE_INDEX;
  } else if (type == LOGIC_COND_TYPE_NOT) {
    return SFLT_NOT_INDEX;
  }
  return SFLT_NOT_INDEX;
}

dengyihao's avatar
dengyihao 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
static int32_t sifGetValueFromNode(SNode *node, char **value) {
  // covert data From snode;
  SValueNode *vn = (SValueNode *)node;

  char *     pData = nodesGetValueFromNode(vn);
  SDataType *pType = &vn->node.resType;
  int32_t    type = pType->type;
  int32_t    valLen = 0;

  if (IS_VAR_DATA_TYPE(type)) {
    int32_t dataLen = varDataTLen(pData);
    if (type == TSDB_DATA_TYPE_JSON) {
      if (*pData == TSDB_DATA_TYPE_NULL) {
        dataLen = 0;
      } else if (*pData == TSDB_DATA_TYPE_NCHAR) {
        dataLen = varDataTLen(pData + CHAR_BYTES);
      } else if (*pData == TSDB_DATA_TYPE_BIGINT || *pData == TSDB_DATA_TYPE_DOUBLE) {
        dataLen = LONG_BYTES;
      } else if (*pData == TSDB_DATA_TYPE_BOOL) {
        dataLen = CHAR_BYTES;
      }
      dataLen += CHAR_BYTES;
    }
    valLen = dataLen;
  } else {
    valLen = pType->bytes;
  }
  char *tv = taosMemoryCalloc(1, valLen + 1);
  if (tv == NULL) {
    return TSDB_CODE_QRY_OUT_OF_MEMORY;
  }

  memcpy(tv, pData, valLen);
  *value = tv;

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
179 180 181 182
static int32_t sifInitParam(SNode *node, SIFParam *param, SIFCtx *ctx) {
  switch (nodeType(node)) {
    case QUERY_NODE_VALUE: {
      SValueNode *vn = (SValueNode *)node;
dengyihao's avatar
dengyihao 已提交
183 184
      SIF_ERR_RET(sifGetValueFromNode(node, &param->condValue));
      param->colId = -1;
dengyihao's avatar
dengyihao 已提交
185 186 187 188
      break;
    }
    case QUERY_NODE_COLUMN: {
      SColumnNode *cn = (SColumnNode *)node;
dengyihao's avatar
dengyihao 已提交
189 190
      /*only support tag column*/
      SIF_ERR_RET(sifValidateColumn(cn));
dengyihao's avatar
dengyihao 已提交
191

dengyihao's avatar
dengyihao 已提交
192
      param->colId = cn->colId;
dengyihao's avatar
dengyihao 已提交
193
      param->colValType = cn->node.resType.type;
dengyihao's avatar
dengyihao 已提交
194 195
      memcpy(param->dbName, cn->dbName, sizeof(cn->dbName));
      memcpy(param->colName, cn->colName, sizeof(cn->colName));
dengyihao's avatar
dengyihao 已提交
196 197 198 199 200 201 202 203
      break;
    }
    case QUERY_NODE_NODE_LIST: {
      SNodeListNode *nl = (SNodeListNode *)node;
      if (LIST_LENGTH(nl->pNodeList) <= 0) {
        qError("invalid length for node:%p, length: %d", node, LIST_LENGTH(nl->pNodeList));
        SIF_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
      }
dengyihao's avatar
dengyihao 已提交
204
      SIF_ERR_RET(scalarGenerateSetFromList((void **)&param->pFilter, node, nl->dataType.type));
dengyihao's avatar
dengyihao 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
      if (taosHashPut(ctx->pRes, &node, POINTER_BYTES, param, sizeof(*param))) {
        taosHashCleanup(param->pFilter);
        qError("taosHashPut nodeList failed, size:%d", (int32_t)sizeof(*param));
        SIF_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
      }
      break;
    }
    case QUERY_NODE_FUNCTION:
    case QUERY_NODE_OPERATOR:
    case QUERY_NODE_LOGIC_CONDITION: {
      SIFParam *res = (SIFParam *)taosHashGet(ctx->pRes, &node, POINTER_BYTES);
      if (NULL == res) {
        qError("no result for node, type:%d, node:%p", nodeType(node), node);
        SIF_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
      }
      *param = *res;
      break;
    }
    default:
      break;
  }
  return TSDB_CODE_SUCCESS;
}

static int32_t sifInitOperParams(SIFParam **params, SOperatorNode *node, SIFCtx *ctx) {
dengyihao's avatar
dengyihao 已提交
230
  int32_t code = 0;
dengyihao's avatar
dengyihao 已提交
231 232 233 234 235 236 237 238 239 240 241 242
  int32_t nParam = sifGetOperParamNum(node->opType);
  if (NULL == node->pLeft || (nParam == 2 && NULL == node->pRight)) {
    qError("invalid operation node, left: %p, rigth: %p", node->pLeft, node->pRight);
    SIF_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }
  SIFParam *paramList = taosMemoryCalloc(nParam, sizeof(SIFParam));
  if (NULL == paramList) {
    SIF_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

  SIF_ERR_JRET(sifInitParam(node->pLeft, &paramList[0], ctx));
  if (nParam > 1) {
dengyihao's avatar
dengyihao 已提交
243
    SIF_ERR_JRET(sifInitParam(node->pRight, &paramList[1], ctx));
dengyihao's avatar
dengyihao 已提交
244 245
  }
  *params = paramList;
dengyihao's avatar
dengyihao 已提交
246
  return TSDB_CODE_SUCCESS;
dengyihao's avatar
dengyihao 已提交
247 248 249
_return:
  taosMemoryFree(paramList);
  SIF_RET(code);
dengyihao's avatar
dengyihao 已提交
250
}
dengyihao's avatar
dengyihao 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
static int32_t sifInitParamList(SIFParam **params, SNodeList *nodeList, SIFCtx *ctx) {
  int32_t   code = 0;
  SIFParam *tParams = taosMemoryCalloc(nodeList->length, sizeof(SIFParam));
  if (tParams == NULL) {
    qError("failed to calloc, nodeList: %p", nodeList);
    SIF_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

  SListCell *cell = nodeList->pHead;
  for (int32_t i = 0; i < nodeList->length; i++) {
    if (NULL == cell || NULL == cell->pNode) {
      SIF_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT);
    }
    SIF_ERR_JRET(sifInitParam(cell->pNode, &tParams[i], ctx));
    cell = cell->pNext;
  }
  *params = tParams;
  return TSDB_CODE_SUCCESS;

_return:
  taosMemoryFree(tParams);
  SIF_RET(code);
}
dengyihao's avatar
dengyihao 已提交
274 275
static int32_t sifExecFunction(SFunctionNode *node, SIFCtx *ctx, SIFParam *output) {
  qError("index-filter not support buildin function");
dengyihao's avatar
dengyihao 已提交
276 277
  return TSDB_CODE_QRY_INVALID_INPUT;
}
dengyihao's avatar
dengyihao 已提交
278
static int32_t sifDoIndex(SIFParam *left, SIFParam *right, int8_t operType, SIFParam *output) {
dengyihao's avatar
dengyihao 已提交
279 280
  SIndexTerm *tm = indexTermCreate(left->suid, DEFAULT, left->colValType, left->colName, strlen(left->colName),
                                   right->condValue, strlen(right->condValue));
dengyihao's avatar
dengyihao 已提交
281 282 283
  if (tm == NULL) {
    return TSDB_CODE_QRY_OUT_OF_MEMORY;
  }
dengyihao's avatar
dengyihao 已提交
284 285 286 287

  EIndexQueryType qtype = 0;
  SIF_ERR_RET(sifGetFuncFromSql(operType, &qtype));

dengyihao's avatar
dengyihao 已提交
288
  SIndexMultiTermQuery *mtm = indexMultiTermQueryCreate(MUST);
dengyihao's avatar
dengyihao 已提交
289
  indexMultiTermQueryAdd(mtm, tm, qtype);
dengyihao's avatar
dengyihao 已提交
290 291 292
  int ret = indexSearch(NULL, mtm, output->result);
  indexMultiTermQueryDestroy(mtm);
  return ret;
dengyihao's avatar
dengyihao 已提交
293
}
dengyihao's avatar
dengyihao 已提交
294 295 296

static int32_t sifLessThanFunc(SIFParam *left, SIFParam *right, SIFParam *output) {
  int id = OP_TYPE_LOWER_THAN;
dengyihao's avatar
dengyihao 已提交
297
  return sifDoIndex(left, right, id, output);
dengyihao's avatar
dengyihao 已提交
298
}
dengyihao's avatar
dengyihao 已提交
299 300
static int32_t sifLessEqualFunc(SIFParam *left, SIFParam *right, SIFParam *output) {
  int id = OP_TYPE_LOWER_EQUAL;
dengyihao's avatar
dengyihao 已提交
301
  return sifDoIndex(left, right, id, output);
dengyihao's avatar
dengyihao 已提交
302
}
dengyihao's avatar
dengyihao 已提交
303 304 305

static int32_t sifGreaterThanFunc(SIFParam *left, SIFParam *right, SIFParam *output) {
  int id = OP_TYPE_GREATER_THAN;
dengyihao's avatar
dengyihao 已提交
306
  return sifDoIndex(left, right, id, output);
dengyihao's avatar
dengyihao 已提交
307 308 309
}
static int32_t sifGreaterEqualFunc(SIFParam *left, SIFParam *right, SIFParam *output) {
  int id = OP_TYPE_GREATER_EQUAL;
dengyihao's avatar
dengyihao 已提交
310
  return sifDoIndex(left, right, id, output);
dengyihao's avatar
dengyihao 已提交
311 312
}

dengyihao's avatar
dengyihao 已提交
313 314
static int32_t sifEqualFunc(SIFParam *left, SIFParam *right, SIFParam *output) {
  int id = OP_TYPE_EQUAL;
dengyihao's avatar
dengyihao 已提交
315
  return sifDoIndex(left, right, id, output);
dengyihao's avatar
dengyihao 已提交
316
}
dengyihao's avatar
dengyihao 已提交
317 318
static int32_t sifNotEqualFunc(SIFParam *left, SIFParam *right, SIFParam *output) {
  int id = OP_TYPE_NOT_EQUAL;
dengyihao's avatar
dengyihao 已提交
319
  return sifDoIndex(left, right, id, output);
dengyihao's avatar
dengyihao 已提交
320
}
dengyihao's avatar
dengyihao 已提交
321 322
static int32_t sifInFunc(SIFParam *left, SIFParam *right, SIFParam *output) {
  int id = OP_TYPE_IN;
dengyihao's avatar
dengyihao 已提交
323
  return sifDoIndex(left, right, id, output);
dengyihao's avatar
dengyihao 已提交
324
}
dengyihao's avatar
dengyihao 已提交
325 326
static int32_t sifNotInFunc(SIFParam *left, SIFParam *right, SIFParam *output) {
  int id = OP_TYPE_NOT_IN;
dengyihao's avatar
dengyihao 已提交
327
  return sifDoIndex(left, right, id, output);
dengyihao's avatar
dengyihao 已提交
328
}
dengyihao's avatar
dengyihao 已提交
329 330
static int32_t sifLikeFunc(SIFParam *left, SIFParam *right, SIFParam *output) {
  int id = OP_TYPE_LIKE;
dengyihao's avatar
dengyihao 已提交
331
  return sifDoIndex(left, right, id, output);
dengyihao's avatar
dengyihao 已提交
332
}
dengyihao's avatar
dengyihao 已提交
333 334
static int32_t sifNotLikeFunc(SIFParam *left, SIFParam *right, SIFParam *output) {
  int id = OP_TYPE_NOT_LIKE;
dengyihao's avatar
dengyihao 已提交
335
  return sifDoIndex(left, right, id, output);
dengyihao's avatar
dengyihao 已提交
336 337
}

dengyihao's avatar
dengyihao 已提交
338 339
static int32_t sifMatchFunc(SIFParam *left, SIFParam *right, SIFParam *output) {
  int id = OP_TYPE_MATCH;
dengyihao's avatar
dengyihao 已提交
340
  return sifDoIndex(left, right, id, output);
dengyihao's avatar
dengyihao 已提交
341
}
dengyihao's avatar
dengyihao 已提交
342 343
static int32_t sifNotMatchFunc(SIFParam *left, SIFParam *right, SIFParam *output) {
  int id = OP_TYPE_NMATCH;
dengyihao's avatar
dengyihao 已提交
344
  return sifDoIndex(left, right, id, output);
dengyihao's avatar
dengyihao 已提交
345
}
dengyihao's avatar
dengyihao 已提交
346

dengyihao's avatar
dengyihao 已提交
347
static int32_t sifDefaultFunc(SIFParam *left, SIFParam *right, SIFParam *output) {
dengyihao's avatar
dengyihao 已提交
348 349 350 351 352 353 354
  // add more except
  return TSDB_CODE_QRY_INVALID_INPUT;
}

static sif_func_t sifGetOperFn(int32_t funcId) {
  // impl later
  switch (funcId) {
dengyihao's avatar
dengyihao 已提交
355 356 357 358
    case OP_TYPE_GREATER_THAN:
      return sifGreaterThanFunc;
    case OP_TYPE_GREATER_EQUAL:
      return sifGreaterEqualFunc;
dengyihao's avatar
dengyihao 已提交
359 360
    case OP_TYPE_LOWER_THAN:
      return sifLessThanFunc;
dengyihao's avatar
dengyihao 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
    case OP_TYPE_LOWER_EQUAL:
      return sifLessEqualFunc;
    case OP_TYPE_EQUAL:
      return sifEqualFunc;
    case OP_TYPE_NOT_EQUAL:
      return sifNotEqualFunc;
    case OP_TYPE_IN:
      return sifInFunc;
    case OP_TYPE_NOT_IN:
      return sifNotInFunc;
    case OP_TYPE_LIKE:
      return sifLikeFunc;
    case OP_TYPE_NOT_LIKE:
      return sifNotLikeFunc;
    case OP_TYPE_MATCH:
      return sifMatchFunc;
    case OP_TYPE_NMATCH:
      return sifNotMatchFunc;
dengyihao's avatar
dengyihao 已提交
379
    default:
dengyihao's avatar
dengyihao 已提交
380
      return sifNullFunc;
dengyihao's avatar
dengyihao 已提交
381
  }
dengyihao's avatar
dengyihao 已提交
382
  return sifNullFunc;
dengyihao's avatar
dengyihao 已提交
383
}
dengyihao's avatar
dengyihao 已提交
384
static int32_t sifExecOper(SOperatorNode *node, SIFCtx *ctx, SIFParam *output) {
dengyihao's avatar
dengyihao 已提交
385
  int32_t code = 0;
dengyihao's avatar
dengyihao 已提交
386 387 388 389
  int32_t nParam = sifGetOperParamNum(node->opType);
  if (nParam <= 1) {
    SIF_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT);
  }
dengyihao's avatar
dengyihao 已提交
390 391 392 393

  SIFParam *params = NULL;
  SIF_ERR_RET(sifInitOperParams(&params, node, ctx));

dengyihao's avatar
dengyihao 已提交
394
  sif_func_t operFn = sifGetOperFn(node->opType);
dengyihao's avatar
dengyihao 已提交
395 396 397 398 399
  if (ctx->noExec && operFn == NULL) {
    output->status = SFLT_NOT_INDEX;
  } else {
    output->status = SFLT_ACCURATE_INDEX;
  }
dengyihao's avatar
dengyihao 已提交
400

dengyihao's avatar
dengyihao 已提交
401
  return operFn(&params[0], nParam > 1 ? &params[1] : NULL, output);
dengyihao's avatar
dengyihao 已提交
402 403 404
_return:
  taosMemoryFree(params);
  SIF_RET(code);
dengyihao's avatar
dengyihao 已提交
405 406
}

dengyihao's avatar
dengyihao 已提交
407 408
static int32_t sifExecLogic(SLogicConditionNode *node, SIFCtx *ctx, SIFParam *output) {
  if (NULL == node->pParameterList || node->pParameterList->length <= 0) {
dengyihao's avatar
dengyihao 已提交
409 410
    qError("invalid logic parameter list, list:%p, paramNum:%d", node->pParameterList,
           node->pParameterList ? node->pParameterList->length : 0);
dengyihao's avatar
dengyihao 已提交
411 412
    return TSDB_CODE_QRY_INVALID_INPUT;
  }
dengyihao's avatar
dengyihao 已提交
413 414 415 416 417

  int32_t   code;
  SIFParam *params = NULL;
  SIF_ERR_RET(sifInitParamList(&params, node->pParameterList, ctx));

dengyihao's avatar
dengyihao 已提交
418 419 420 421 422 423 424 425 426 427 428 429 430 431
  if (ctx->noExec == false) {
    for (int32_t m = 0; m < node->pParameterList->length; m++) {
      // add impl later
      if (node->condType == LOGIC_COND_TYPE_AND) {
        taosArrayAddAll(output->result, params[m].result);
      } else if (node->condType == LOGIC_COND_TYPE_OR) {
        taosArrayAddAll(output->result, params[m].result);
      } else if (node->condType == LOGIC_COND_TYPE_NOT) {
        taosArrayAddAll(output->result, params[m].result);
      }
    }
  } else {
    for (int32_t m = 0; m < node->pParameterList->length; m++) {
      output->status = sifMergeCond(node->condType, output->status, params[m].status);
dengyihao's avatar
dengyihao 已提交
432 433 434 435 436
    }
  }
_return:
  taosMemoryFree(params);
  SIF_RET(code);
dengyihao's avatar
dengyihao 已提交
437
}
dengyihao's avatar
dengyihao 已提交
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488

static EDealRes sifWalkFunction(SNode *pNode, void *context) {
  SFunctionNode *node = (SFunctionNode *)pNode;
  SIFParam       output = {0};

  SIFCtx *ctx = context;
  ctx->code = sifExecFunction(node, ctx, &output);
  if (ctx->code != TSDB_CODE_SUCCESS) {
    return DEAL_RES_ERROR;
  }

  if (taosHashPut(ctx->pRes, &pNode, POINTER_BYTES, &output, sizeof(output))) {
    ctx->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
    return DEAL_RES_ERROR;
  }
  return DEAL_RES_CONTINUE;
}
static EDealRes sifWalkLogic(SNode *pNode, void *context) {
  SLogicConditionNode *node = (SLogicConditionNode *)pNode;
  SIFParam             output = {0};

  SIFCtx *ctx = context;
  ctx->code = sifExecLogic(node, ctx, &output);
  if (ctx->code) {
    return DEAL_RES_ERROR;
  }

  if (taosHashPut(ctx->pRes, &pNode, POINTER_BYTES, &output, sizeof(output))) {
    ctx->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
    return DEAL_RES_ERROR;
  }
  return DEAL_RES_CONTINUE;
}
static EDealRes sifWalkOper(SNode *pNode, void *context) {
  SOperatorNode *node = (SOperatorNode *)pNode;
  SIFParam       output = {0};

  SIFCtx *ctx = context;
  ctx->code = sifExecOper(node, ctx, &output);
  if (ctx->code) {
    return DEAL_RES_ERROR;
  }
  if (taosHashPut(ctx->pRes, &pNode, POINTER_BYTES, &output, sizeof(output))) {
    ctx->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
    return DEAL_RES_ERROR;
  }

  return DEAL_RES_CONTINUE;
}

EDealRes sifCalcWalker(SNode *node, void *context) {
dengyihao's avatar
dengyihao 已提交
489 490
  if (QUERY_NODE_VALUE == nodeType(node) || QUERY_NODE_NODE_LIST == nodeType(node) ||
      QUERY_NODE_COLUMN == nodeType(node)) {
dengyihao's avatar
dengyihao 已提交
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 517 518 519 520 521 522 523 524
    return DEAL_RES_CONTINUE;
  }
  SIFCtx *ctx = (SIFCtx *)context;
  if (QUERY_NODE_FUNCTION == nodeType(node)) {
    return sifWalkFunction(node, ctx);
  }
  if (QUERY_NODE_LOGIC_CONDITION == nodeType(node)) {
    return sifWalkLogic(node, ctx);
  }
  if (QUERY_NODE_OPERATOR == nodeType(node)) {
    return sifWalkOper(node, ctx);
  }

  qError("invalid node type for index filter calculating, type:%d", nodeType(node));
  ctx->code = TSDB_CODE_QRY_INVALID_INPUT;
  return DEAL_RES_ERROR;
}

void sifFreeRes(SHashObj *res) {
  void *pIter = taosHashIterate(res, NULL);
  while (pIter) {
    SIFParam *p = pIter;
    if (p) {
      sifFreeParam(p);
    }
    pIter = taosHashIterate(res, pIter);
  }
  taosHashCleanup(res);
}
static int32_t sifCalculate(SNode *pNode, SIFParam *pDst) {
  if (pNode == NULL || pDst == NULL) {
    return TSDB_CODE_QRY_INVALID_INPUT;
  }
  int32_t code = 0;
dengyihao's avatar
dengyihao 已提交
525
  SIFCtx  ctx = {.code = 0, .noExec = false};
dengyihao's avatar
dengyihao 已提交
526 527 528 529 530
  ctx.pRes = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
  if (NULL == ctx.pRes) {
    qError("index-filter failed to taosHashInit");
    return TSDB_CODE_QRY_OUT_OF_MEMORY;
  }
dengyihao's avatar
dengyihao 已提交
531

dengyihao's avatar
dengyihao 已提交
532
  nodesWalkExprPostOrder(pNode, sifCalcWalker, &ctx);
dengyihao's avatar
dengyihao 已提交
533 534
  SIF_ERR_RET(ctx.code);

dengyihao's avatar
dengyihao 已提交
535 536 537 538
  if (pDst) {
    SIFParam *res = (SIFParam *)taosHashGet(ctx.pRes, (void *)&pNode, POINTER_BYTES);
    if (res == NULL) {
      qError("no valid res in hash, node:(%p), type(%d)", (void *)&pNode, nodeType(pNode));
dengyihao's avatar
dengyihao 已提交
539
      SIF_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
dengyihao's avatar
dengyihao 已提交
540 541 542 543 544 545
    }
    taosArrayAddAll(pDst->result, res->result);

    sifFreeParam(res);
    taosHashRemove(ctx.pRes, (void *)&pNode, POINTER_BYTES);
  }
dengyihao's avatar
dengyihao 已提交
546
  SIF_RET(code);
dengyihao's avatar
dengyihao 已提交
547 548
}

dengyihao's avatar
dengyihao 已提交
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
static int32_t sifGetFltHint(SNode *pNode, SIdxFltStatus *status) {
  int32_t code = TSDB_CODE_SUCCESS;
  if (pNode == NULL) {
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

  SIFCtx ctx = {.code = 0, .noExec = true};
  ctx.pRes = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
  if (NULL == ctx.pRes) {
    qError("index-filter failed to taosHashInit");
    return TSDB_CODE_QRY_OUT_OF_MEMORY;
  }

  nodesWalkExprPostOrder(pNode, sifCalcWalker, &ctx);

  SIF_ERR_RET(ctx.code);

  SIFParam *res = (SIFParam *)taosHashGet(ctx.pRes, (void *)&pNode, POINTER_BYTES);
  if (res == NULL) {
    qError("no valid res in hash, node:(%p), type(%d)", (void *)&pNode, nodeType(pNode));
    SIF_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
  }
  *status = res->status;

  sifFreeParam(res);
  taosHashRemove(ctx.pRes, (void *)&pNode, POINTER_BYTES);

  SIF_RET(code);
}

dengyihao's avatar
dengyihao 已提交
579
int32_t doFilterTag(const SNode *pFilterNode, SArray *result) {
dengyihao's avatar
dengyihao 已提交
580 581 582 583 584 585
  if (pFilterNode == NULL) {
    return TSDB_CODE_SUCCESS;
  }

  SFilterInfo *filter = NULL;
  // todo move to the initialization function
dengyihao's avatar
dengyihao 已提交
586
  SIF_ERR_RET(filterInitFromNode((SNode *)pFilterNode, &filter, 0));
dengyihao's avatar
dengyihao 已提交
587 588

  SIFParam param = {0};
dengyihao's avatar
dengyihao 已提交
589
  SIF_ERR_RET(sifCalculate((SNode *)pFilterNode, &param));
dengyihao's avatar
dengyihao 已提交
590 591 592

  taosArrayAddAll(result, param.result);
  sifFreeParam(&param);
dengyihao's avatar
dengyihao 已提交
593
  SIF_RET(TSDB_CODE_SUCCESS);
dengyihao's avatar
dengyihao 已提交
594
}
dengyihao's avatar
dengyihao 已提交
595 596

SIdxFltStatus idxGetFltStatus(SNode *pFilterNode) {
dengyihao's avatar
dengyihao 已提交
597
  SIdxFltStatus st = SFLT_NOT_INDEX;
dengyihao's avatar
dengyihao 已提交
598 599 600
  if (pFilterNode == NULL) {
    return SFLT_NOT_INDEX;
  }
dengyihao's avatar
dengyihao 已提交
601 602 603
  SFilterInfo *filter = NULL;
  // todo move to the initialization function
  SIF_ERR_RET(filterInitFromNode((SNode *)pFilterNode, &filter, 0));
dengyihao's avatar
dengyihao 已提交
604

dengyihao's avatar
dengyihao 已提交
605 606
  SIF_ERR_RET(sifGetFltHint((SNode *)pFilterNode, &st));
  return st;
dengyihao's avatar
dengyihao 已提交
607
}