queryInfoUtil.c 10.8 KB
Newer Older
H
Haojun Liao 已提交
1
#include "os.h"
2
#include "queryInfoUtil.h"
3
#include "function.h"
4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include "parser.h"
#include "parserUtil.h"

static struct SSchema _s = {
    .colId = TSDB_TBNAME_COLUMN_INDEX,
    .type  = TSDB_DATA_TYPE_BINARY,
    .bytes = TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE,
    .name = "tbname",
};

SSchema* getTbnameColumnSchema() {
  return &_s;
}

18 19 20 21 22
SArray* getCurrentExprList(SQueryStmtInfo* pQueryInfo) {
  assert(pQueryInfo != NULL && pQueryInfo->exprListLevelIndex >= 0 && pQueryInfo->exprListLevelIndex < 10);
  return pQueryInfo->exprList[pQueryInfo->exprListLevelIndex];
}

23
size_t getNumOfExprs(SQueryStmtInfo* pQueryInfo) {
24 25
  SArray* pExprList = getCurrentExprList(pQueryInfo);
  return taosArrayGetSize(pExprList);
26 27 28
}

SSchema* getOneColumnSchema(const STableMeta* pTableMeta, int32_t colIndex) {
29
  assert(pTableMeta != NULL && pTableMeta->schema != NULL && colIndex >= 0 && colIndex < (getNumOfColumns(pTableMeta) + getNumOfTags(pTableMeta)));
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

  SSchema* pSchema = (SSchema*) pTableMeta->schema;
  return &pSchema[colIndex];
}

STableComInfo getTableInfo(const STableMeta* pTableMeta) {
  assert(pTableMeta != NULL);
  return pTableMeta->tableInfo;
}

int32_t getNumOfColumns(const STableMeta* pTableMeta) {
  assert(pTableMeta != NULL);
  // table created according to super table, use data from super table
  return getTableInfo(pTableMeta).numOfColumns;
}

int32_t getNumOfTags(const STableMeta* pTableMeta) {
  assert(pTableMeta != NULL);
  return getTableInfo(pTableMeta).numOfTags;
}

SSchema *getTableColumnSchema(const STableMeta *pTableMeta) {
  assert(pTableMeta != NULL);
  return (SSchema*) pTableMeta->schema;
}

SSchema* getTableTagSchema(const STableMeta* pTableMeta) {
  assert(pTableMeta != NULL && (pTableMeta->tableType == TSDB_SUPER_TABLE || pTableMeta->tableType == TSDB_CHILD_TABLE));
  return getOneColumnSchema(pTableMeta, getTableInfo(pTableMeta).numOfColumns);
}

61
static tExprNode* createFunctionExprNode(const char* funcName, struct SSourceParam *pParam) {
62 63 64 65 66 67
  tExprNode** p = malloc(pParam->num * POINTER_BYTES);

  if (pParam->pColumnList != NULL) {
    for(int32_t i = 0; i < pParam->num; ++i) {
      p[i] = calloc(1, sizeof(tExprNode));
      p[i]->nodeType = TEXPR_COL_NODE;
68 69 70 71 72 73 74 75 76

      SColumn* pSrc = taosArrayGetP(pParam->pColumnList, i);
      SSchema* pSchema = calloc(1, sizeof(SSchema));

      tstrncpy(pSchema->name, pSrc->name, tListLen(pSchema->name));
      pSchema->type  = pSrc->info.type;
      pSchema->bytes = pSrc->info.bytes;
      pSchema->colId = pSrc->info.colId;
      p[i]->pSchema = pSchema;
77
    }
78
  } else {
79 80 81 82
    assert(pParam->pColumnList == NULL);
    for(int32_t i = 0; i < pParam->num; ++i) {
      p[i] = taosArrayGetP(pParam->pExprNodeList, i);
    }
83
  }
84 85

  tExprNode* pNode = calloc(1, sizeof(tExprNode));
86

87
  pNode->nodeType = TEXPR_FUNCTION_NODE;
88
  tstrncpy(pNode->_function.functionName, funcName, tListLen(pNode->_function.functionName));
89 90
  pNode->_function.pChild = p;
  pNode->_function.num = pParam->num;
91 92 93 94

  return pNode;
}

95
SExprInfo* createBinaryExprInfo(tExprNode* pNode, SSchema* pResSchema) {
96 97 98 99 100 101 102 103 104 105 106 107
  assert(pNode != NULL && pResSchema != NULL);

  SExprInfo* pExpr = calloc(1, sizeof(SExprInfo));
  if (pExpr == NULL) {
    return NULL;
  }

  pExpr->pExpr = pNode;
  memcpy(&pExpr->base.resSchema, pResSchema, sizeof(SSchema));
  return pExpr;
}

108
SExprInfo* createExprInfo(STableMetaInfo* pTableMetaInfo, const char* funcName, SSourceParam* pSourceParam, SSchema* pResSchema, int16_t interSize) {
109 110 111 112 113
  SExprInfo* pExpr = calloc(1, sizeof(SExprInfo));
  if (pExpr == NULL) {
    return NULL;
  }

114 115 116 117 118
  uint64_t uid = 0;
  if (pTableMetaInfo->pTableMeta) {
    uid = pTableMetaInfo->pTableMeta->uid;
  }

119 120
  SSqlExpr* p = &pExpr->base;

121 122 123 124 125 126
  p->pColumns   = calloc(pSourceParam->num, sizeof(SColumn));
  p->numOfCols  = pSourceParam->num;
  p->interBytes = interSize;
  memcpy(&p->resSchema, pResSchema, sizeof(SSchema));

  if (pSourceParam->pExprNodeList != NULL) {
127
    pExpr->pExpr = createFunctionExprNode(funcName, pSourceParam);
128 129 130 131 132 133
    return pExpr;
  }

  SColumn* pCol = taosArrayGetP(pSourceParam->pColumnList, 0);
  if (pCol->info.colId == TSDB_TBNAME_COLUMN_INDEX) {
    assert(pSourceParam->num == 1);
134

135
    SSchema* s = getTbnameColumnSchema();
136 137
    setColumn(p->pColumns, uid, pTableMetaInfo->aliasName, TSDB_COL_TAG, s);

138 139
    pExpr->pExpr = createFunctionExprNode(funcName, pSourceParam);
  } else if (TSDB_COL_IS_UD_COL(pCol->flag) || strcmp(funcName, "block_dist") == 0) {
140
    setColumn(p->pColumns, uid, pTableMetaInfo->aliasName, TSDB_COL_UDC, pResSchema);
141
    pExpr->pExpr = createFunctionExprNode(funcName, pSourceParam);
142
  } else {
143 144 145
    for(int32_t i = 0; i < pSourceParam->num; ++i) {
      SColumn* c = taosArrayGetP(pSourceParam->pColumnList, i);
      p->pColumns[i] = *c;
146
    }
147
    pExpr->pExpr = createFunctionExprNode(funcName, pSourceParam);
148 149 150 151 152
  }

  return pExpr;
}

153 154
void addExprInfo(SArray* pExprList, int32_t index, SExprInfo* pExprInfo, int32_t level) {
  assert(pExprList != NULL );
155

156
  int32_t num = (int32_t) taosArrayGetSize(pExprList);
157
  if (index == num) {
158
    taosArrayPush(pExprList, &pExprInfo);
159
  } else {
160
    taosArrayInsert(pExprList, index, &pExprInfo);
161
  }
162

163
#if 0
164 165 166 167 168
  if (pExprInfo->pExpr->nodeType == TEXPR_FUNCTION_NODE) {
    printf("add function: %s, level:%d, total:%ld\n", pExprInfo->pExpr->_function.functionName, level, taosArrayGetSize(pExprList));
  } else {
    printf("add operator: %s, level:%d, total:%ld\n", pExprInfo->base.resSchema.name, level, taosArrayGetSize(pExprList));
  }
169 170
#endif

171 172 173 174 175 176
}

void updateExprInfo(SExprInfo* pExprInfo, int16_t functionId, int32_t colId, int16_t srcColumnIndex, int16_t resType, int16_t resSize) {
  assert(pExprInfo != NULL);

  SSqlExpr* pse = &pExprInfo->base;
177
  assert(0);
178

179 180
  pse->resSchema.type  = resType;
  pse->resSchema.bytes = resSize;
181 182 183 184
}

SExprInfo* getExprInfo(SQueryStmtInfo* pQueryInfo, int32_t index) {
  assert(pQueryInfo != NULL && pQueryInfo->exprList && index >= 0);
185
  return taosArrayGetP(getCurrentExprList(pQueryInfo), index);
186 187
}

188 189
void destroyExprInfo(SExprInfo* pExprInfo) {
  tExprTreeDestroy(pExprInfo->pExpr, NULL);
190 191 192 193

  for(int32_t i = 0; i < pExprInfo->base.numOfParams; ++i) {
    taosVariantDestroy(&pExprInfo->base.param[i]);
  }
194 195

  tfree(pExprInfo->base.pColumns);
196 197 198
  tfree(pExprInfo);
}

199
void dropOneLevelExprInfo(SArray* pExprInfo) {
200 201
  size_t size = taosArrayGetSize(pExprInfo);

202
  for (int32_t i = 0; i < size; ++i) {
203
    SExprInfo* pExpr = taosArrayGetP(pExprInfo, i);
204
    destroyExprInfo(pExpr);
205 206 207 208 209
  }

  taosArrayDestroy(pExprInfo);
}

210 211 212 213 214 215
void dropAllExprInfo(SArray** pExprInfo, int32_t numOfLevel) {
  for(int32_t i = 0; i < numOfLevel; ++i) {
    dropOneLevelExprInfo(pExprInfo[i]);
  }
}

216
void addExprInfoParam(SSqlExpr* pExpr, char* argument, int32_t type, int32_t bytes) {
217 218 219 220 221 222 223 224 225 226
  assert (pExpr != NULL || argument != NULL || bytes != 0);

  // set parameter value
  // transfer to tVariant from byte data/no ascii data
  taosVariantCreateFromBinary(&pExpr->param[pExpr->numOfParams], argument, bytes, type);
  pExpr->numOfParams += 1;

  assert(pExpr->numOfParams <= 3);
}

227
int32_t getExprFunctionId(SExprInfo *pExprInfo) {
H
Haojun Liao 已提交
228
  assert(pExprInfo != NULL && pExprInfo->pExpr != NULL && pExprInfo->pExpr->nodeType == TEXPR_FUNCTION_NODE);
229
  return 0;
230 231
}

232
void assignExprInfo(SExprInfo* dst, const SExprInfo* src) {
H
Haojun Liao 已提交
233
  assert(dst != NULL && src != NULL/* && src->base.numOfCols > 0*/);
234 235

  *dst = *src;
236
#if 0
237 238 239 240
  if (src->base.flist.numOfFilters > 0) {
    dst->base.flist.filterInfo = calloc(src->base.flist.numOfFilters, sizeof(SColumnFilterInfo));
    memcpy(dst->base.flist.filterInfo, src->base.flist.filterInfo, sizeof(SColumnFilterInfo) * src->base.flist.numOfFilters);
  }
241
#endif
242

H
Haojun Liao 已提交
243
  dst->pExpr = exprdup(src->pExpr);
H
Haojun Liao 已提交
244 245 246
  if (src->base.numOfCols > 0) {
    dst->base.pColumns = calloc(src->base.numOfCols, sizeof(SColumn));
    memcpy(dst->base.pColumns, src->base.pColumns, sizeof(SColumn) * src->base.numOfCols);
H
Haojun Liao 已提交
247 248
  } else {
    dst->base.pColumns = NULL;
H
Haojun Liao 已提交
249
  }
250

251 252 253 254 255 256
  memset(dst->base.param, 0, sizeof(SVariant) * tListLen(dst->base.param));
  for (int32_t j = 0; j < src->base.numOfParams; ++j) {
    taosVariantAssign(&dst->base.param[j], &src->base.param[j]);
  }
}

257
int32_t copyExprInfoList(SArray* dst, const SArray* src, uint64_t uid, bool deepcopy) {
258 259 260 261 262
  assert(src != NULL && dst != NULL);

  size_t size = taosArrayGetSize(src);
  for (int32_t i = 0; i < size; ++i) {
    SExprInfo* pExpr = taosArrayGetP(src, i);
263
    uint64_t exprUid = pExpr->base.pColumns->uid;
264

265
    if (exprUid == uid) {
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
      if (deepcopy) {
        SExprInfo* p1 = calloc(1, sizeof(SExprInfo));
        assignExprInfo(p1, pExpr);

        taosArrayPush(dst, &p1);
      } else {
        taosArrayPush(dst, &pExpr);
      }
    }
  }

  return 0;
}

int32_t copyAllExprInfo(SArray* dst, const SArray* src, bool deepcopy) {
  assert(src != NULL && dst != NULL);

  size_t size = taosArrayGetSize(src);
  for (int32_t i = 0; i < size; ++i) {
    SExprInfo* pExpr = taosArrayGetP(src, i);

287 288 289
    SExprInfo* p1 = calloc(1, sizeof(SExprInfo));
    assignExprInfo(p1, pExpr);
    taosArrayPush(dst, &p1);
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
  }

  return 0;
}

//void* tSqlExprDestroy(SExprInfo* pExpr) {
//  if (pExpr == NULL) {
//    return NULL;
//  }
//
//  SSqlExpr* p = &pExpr->base;
//  for(int32_t i = 0; i < tListLen(p->param); ++i) {
//    taosVariantDestroy(&p->param[i]);
//  }
//
//  if (p->flist.numOfFilters > 0) {
//    tfree(p->flist.filterInfo);
//  }
//
//  if (pExpr->pExpr != NULL) {
//    tExprTreeDestroy(pExpr->pExpr, NULL);
//  }
//
//  tfree(pExpr);
//  return NULL;
//}

int32_t getResRowLength(SArray* pExprList) {
  size_t num = taosArrayGetSize(pExprList);
  if (num == 0) {
    return 0;
  }

  int32_t size = 0;
  for(int32_t i = 0; i < num; ++i) {
    SExprInfo* pExpr = taosArrayGetP(pExprList, i);
326
    size += pExpr->base.resSchema.bytes;
327 328 329 330 331
  }

  return size;
}

332
SArray* extractFunctionList(SArray* pExprInfoList) {
333 334 335
  assert(pExprInfoList != NULL);

  size_t len = taosArrayGetSize(pExprInfoList);
336
  SArray* p = taosArrayInit(len, POINTER_BYTES);
337

338 339
  for(int32_t i = 0; i < len; ++i) {
    SExprInfo* pExprInfo = taosArrayGetP(pExprInfoList, i);
340
    if (pExprInfo->pExpr->nodeType == TEXPR_FUNCTION_NODE) {
341 342
      char* name = strdup(pExprInfo->pExpr->_function.functionName);
      taosArrayPush(p, &name);
343
    } else {
344 345
      char* name = strdup("project");
      taosArrayPush(p, &name);
346
    }
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
  }

  return p;
}

bool tscHasColumnFilter(SQueryStmtInfo* pQueryInfo) {
  // filter on primary timestamp column
  if (pQueryInfo->window.skey != INT64_MIN || pQueryInfo->window.ekey != INT64_MAX) {
    return true;
  }

  size_t size = taosArrayGetSize(pQueryInfo->colList);
  for (int32_t i = 0; i < size; ++i) {
    SColumn* pCol = taosArrayGetP(pQueryInfo->colList, i);
    if (pCol->info.flist.numOfFilters > 0) {
      return true;
    }
  }

  return false;
}

X
Xiaoyu Wang 已提交
369
int32_t getExprFunctionLevel(const SQueryStmtInfo* pQueryInfo) {
H
Haojun Liao 已提交
370 371 372 373 374 375 376 377 378 379 380 381
  int32_t n = 10;

  int32_t level = 0;
  for(int32_t i = 0; i < n; ++i) {
    SArray* pList = pQueryInfo->exprList[i];
    if (taosArrayGetSize(pList) > 0) {
      level += 1;
    }
  }

  return level;
}