tscUtil.c 64.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 17
#include "tscUtil.h"
#include "hash.h"
18
#include "os.h"
H
hjxilinx 已提交
19
#include "qast.h"
H
hzcheng 已提交
20 21 22 23 24 25
#include "taosmsg.h"
#include "tcache.h"
#include "tkey.h"
#include "tmd5.h"
#include "tscProfile.h"
#include "tscSecondaryMerge.h"
H
hjxilinx 已提交
26
#include "tscSubquery.h"
H
hzcheng 已提交
27 28 29
#include "tschemautil.h"
#include "tsclient.h"
#include "ttimer.h"
30
#include "ttokendef.h"
S
slguan 已提交
31
#include "tscLog.h"
H
hzcheng 已提交
32

33 34 35 36
static void freeQueryInfoImpl(SQueryInfo* pQueryInfo);
static void clearAllTableMetaInfo(SQueryInfo* pQueryInfo, const char* address, bool removeFromCache);
  
  SCond* tsGetSTableQueryCond(STagCond* pTagCond, uint64_t uid) {
37 38 39 40 41 42 43 44 45 46
  if (pTagCond->pCond == NULL) {
    return NULL;
  }
  
  size_t size = taosArrayGetSize(pTagCond->pCond);
  for (int32_t i = 0; i < size; ++i) {
    SCond* pCond = taosArrayGet(pTagCond->pCond, i);
    
    if (uid == pCond->uid) {
      return pCond;
S
slguan 已提交
47 48 49 50 51 52
    }
  }

  return NULL;
}

53 54
void tsSetSTableQueryCond(STagCond* pTagCond, uint64_t uid, SBufferWriter* bw) {
  if (tbufTell(bw) == 0) {
S
slguan 已提交
55
    return;
H
hzcheng 已提交
56
  }
57 58 59
  
  SCond cond = {
    .uid = uid,
60
    .len = tbufTell(bw),
61 62 63
    .cond = NULL,
  };
  
64
  cond.cond = tbufGetData(bw, true);
65 66 67 68 69 70
  
  if (pTagCond->pCond == NULL) {
    pTagCond->pCond = taosArrayInit(3, sizeof(SCond));
  }
  
  taosArrayPush(pTagCond->pCond, &cond);
S
slguan 已提交
71 72
}

73
bool tscQueryOnSTable(SSqlCmd* pCmd) {
74 75 76
  SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0);

  return ((pQueryInfo->type & TSDB_QUERY_TYPE_STABLE_QUERY) == TSDB_QUERY_TYPE_STABLE_QUERY) &&
S
slguan 已提交
77
         (pCmd->msgType == TSDB_MSG_TYPE_QUERY);
S
slguan 已提交
78 79
}

H
hjxilinx 已提交
80
bool tscQueryTags(SQueryInfo* pQueryInfo) {
H
hjxilinx 已提交
81
  for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutput; ++i) {
82 83 84
    int32_t functId = tscSqlExprGet(pQueryInfo, i)->functionId;
    
    if (functId != TSDB_FUNC_TAGPRJ && functId != TSDB_FUNC_TID_TAG) {
S
slguan 已提交
85 86 87
      return false;
    }
  }
H
hzcheng 已提交
88

S
slguan 已提交
89
  return true;
H
hzcheng 已提交
90 91
}

S
slguan 已提交
92 93 94 95
bool tscIsSelectivityWithTagQuery(SSqlCmd* pCmd) {
  bool    hasTags = false;
  int32_t numOfSelectivity = 0;

96 97
  SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0);

H
hjxilinx 已提交
98
  for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutput; ++i) {
99
    int32_t functId = tscSqlExprGet(pQueryInfo, i)->functionId;
S
slguan 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    if (functId == TSDB_FUNC_TAG_DUMMY) {
      hasTags = true;
      continue;
    }

    if ((aAggs[functId].nStatus & TSDB_FUNCSTATE_SELECTIVITY) != 0) {
      numOfSelectivity++;
    }
  }

  if (numOfSelectivity > 0 && hasTags) {
    return true;
  }

  return false;
}
H
hzcheng 已提交
116

S
slguan 已提交
117 118
void tscGetDBInfoFromMeterId(char* tableId, char* db) {
  char* st = strstr(tableId, TS_PATH_DELIMITER);
H
hzcheng 已提交
119 120 121
  if (st != NULL) {
    char* end = strstr(st + 1, TS_PATH_DELIMITER);
    if (end != NULL) {
S
slguan 已提交
122 123
      memcpy(db, tableId, (end - tableId));
      db[end - tableId] = 0;
H
hzcheng 已提交
124 125 126 127 128 129 130
      return;
    }
  }

  db[0] = 0;
}

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
//STableIdInfo* tscGetMeterSidInfo(SVnodeSidList* pSidList, int32_t idx) {
//  if (pSidList == NULL) {
//    tscError("illegal sidlist");
//    return 0;
//  }
//
//  if (idx < 0 || idx >= pSidList->numOfSids) {
//    int32_t sidRange = (pSidList->numOfSids > 0) ? (pSidList->numOfSids - 1) : 0;
//
//    tscError("illegal sidIdx:%d, reset to 0, sidIdx range:%d-%d", idx, 0, sidRange);
//    idx = 0;
//  }
//
//  assert(pSidList->pSidExtInfoList[idx] >= 0);
//
//  return (STableIdInfo*)(pSidList->pSidExtInfoList[idx] + (char*)pSidList);
//}
H
hzcheng 已提交
148

H
hjxilinx 已提交
149
bool tscIsTwoStageSTableQuery(SQueryInfo* pQueryInfo, int32_t tableIndex) {
150 151 152
  if (pQueryInfo == NULL) {
    return false;
  }
153

H
hjxilinx 已提交
154
  STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, tableIndex);
H
hjxilinx 已提交
155
  if (pTableMetaInfo == NULL) {
156 157 158
    return false;
  }
  
H
hjxilinx 已提交
159
  // for select query super table, the super table vgroup list can not be null in any cases.
weixin_48148422's avatar
weixin_48148422 已提交
160
  if (pQueryInfo->command == TSDB_SQL_SELECT && UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo)) {
H
hjxilinx 已提交
161
    assert(pTableMetaInfo->vgroupList != NULL);
162 163
  }
  
H
hjxilinx 已提交
164 165
  if ((pQueryInfo->type & TSDB_QUERY_TYPE_FREE_RESOURCE) == TSDB_QUERY_TYPE_FREE_RESOURCE) {
    return false;
S
slguan 已提交
166 167
  }

168 169
  // for ordered projection query, iterate all qualified vnodes sequentially
  if (tscNonOrderedProjectionQueryOnSTable(pQueryInfo, tableIndex)) {
H
hzcheng 已提交
170 171 172
    return false;
  }

173
  if (((pQueryInfo->type & TSDB_QUERY_TYPE_STABLE_SUBQUERY) != TSDB_QUERY_TYPE_STABLE_SUBQUERY) &&
174
      pQueryInfo->command == TSDB_SQL_SELECT) {
weixin_48148422's avatar
weixin_48148422 已提交
175
    return UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo);
H
hzcheng 已提交
176 177 178 179 180
  }

  return false;
}

181
bool tscIsProjectionQueryOnSTable(SQueryInfo* pQueryInfo, int32_t tableIndex) {
H
hjxilinx 已提交
182
  STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, tableIndex);
183
  
H
hzcheng 已提交
184
  /*
185 186 187
   * In following cases, return false for non ordered project query on super table
   * 1. failed to get metermeta from server; 2. not a super table; 3. limitation is 0;
   * 4. show queries, instead of a select query
H
hzcheng 已提交
188
   */
H
hjxilinx 已提交
189
  size_t numOfExprs = tscSqlExprNumOfExprs(pQueryInfo);
weixin_48148422's avatar
weixin_48148422 已提交
190
  if (pTableMetaInfo == NULL || !UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo) ||
H
hjxilinx 已提交
191
      pQueryInfo->command == TSDB_SQL_RETRIEVE_EMPTY_RESULT || numOfExprs == 0) {
H
hzcheng 已提交
192 193
    return false;
  }
194
  
H
hjxilinx 已提交
195
  // only query on tag, a project query
H
hjxilinx 已提交
196
  if (tscQueryTags(pQueryInfo)) {
H
hjxilinx 已提交
197
    return true;
S
slguan 已提交
198
  }
199
  
H
hjxilinx 已提交
200
  for (int32_t i = 0; i < numOfExprs; ++i) {
201
    int32_t functionId = tscSqlExprGet(pQueryInfo, i)->functionId;
H
hjxilinx 已提交
202 203
    if (functionId != TSDB_FUNC_PRJ && functionId != TSDB_FUNC_TAGPRJ && functionId != TSDB_FUNC_TAG &&
        functionId != TSDB_FUNC_TS && functionId != TSDB_FUNC_ARITHM) {
S
slguan 已提交
204
      return false;
H
hzcheng 已提交
205 206
    }
  }
207
  
S
slguan 已提交
208
  return true;
H
hzcheng 已提交
209 210
}

211 212 213 214 215
bool tscNonOrderedProjectionQueryOnSTable(SQueryInfo* pQueryInfo, int32_t tableIndex) {
  if (!tscIsProjectionQueryOnSTable(pQueryInfo, tableIndex)) {
    return false;
  }
  
216
  // order by columnIndex exists, not a non-ordered projection query
217 218 219 220 221 222 223 224
  return pQueryInfo->order.orderColId < 0;
}

bool tscOrderedProjectionQueryOnSTable(SQueryInfo* pQueryInfo, int32_t tableIndex) {
  if (!tscIsProjectionQueryOnSTable(pQueryInfo, tableIndex)) {
    return false;
  }
  
225
  // order by columnIndex exists, a non-ordered projection query
226 227 228
  return pQueryInfo->order.orderColId >= 0;
}

229
bool tscProjectionQueryOnTable(SQueryInfo* pQueryInfo) {
H
hjxilinx 已提交
230
  for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutput; ++i) {
231
    int32_t functionId = tscSqlExprGet(pQueryInfo, i)->functionId;
H
hjxilinx 已提交
232 233 234 235
    if (functionId != TSDB_FUNC_PRJ && functionId != TSDB_FUNC_TS) {
      return false;
    }
  }
H
hjxilinx 已提交
236

H
hjxilinx 已提交
237 238 239
  return true;
}

240
bool tscIsPointInterpQuery(SQueryInfo* pQueryInfo) {
H
hjxilinx 已提交
241 242 243
  size_t size = tscSqlExprNumOfExprs(pQueryInfo);
  
  for (int32_t i = 0; i < size; ++i) {
244
    SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i);
H
hzcheng 已提交
245 246 247 248
    if (pExpr == NULL) {
      return false;
    }

S
slguan 已提交
249
    int32_t functionId = pExpr->functionId;
H
hzcheng 已提交
250 251 252 253 254 255 256 257 258 259 260
    if (functionId == TSDB_FUNC_TAG) {
      continue;
    }

    if (functionId != TSDB_FUNC_INTERP) {
      return false;
    }
  }
  return true;
}

261
bool tscIsTWAQuery(SQueryInfo* pQueryInfo) {
H
hjxilinx 已提交
262 263
  size_t numOfExprs = tscSqlExprNumOfExprs(pQueryInfo);
  for (int32_t i = 0; i < numOfExprs; ++i) {
264
    SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i);
S
slguan 已提交
265 266 267 268 269 270 271 272 273 274 275
    if (pExpr == NULL) {
      continue;
    }

    int32_t functionId = pExpr->functionId;
    if (functionId == TSDB_FUNC_TWA) {
      return true;
    }
  }

  return false;
H
hzcheng 已提交
276 277
}

278 279
void tscClearInterpInfo(SQueryInfo* pQueryInfo) {
  if (!tscIsPointInterpQuery(pQueryInfo)) {
H
hzcheng 已提交
280 281 282
    return;
  }

283
  pQueryInfo->fillType = TSDB_FILL_NONE;
284
  tfree(pQueryInfo->fillVal);
H
hzcheng 已提交
285 286
}

287
int32_t tscCreateResPointerInfo(SSqlRes* pRes, SQueryInfo* pQueryInfo) {
H
hzcheng 已提交
288
  if (pRes->tsrow == NULL) {
H
hjxilinx 已提交
289 290
    int32_t numOfOutput = pQueryInfo->fieldsInfo.numOfOutput;
    pRes->numOfCols = numOfOutput;
291
  
292 293 294
    pRes->tsrow  = calloc(numOfOutput, POINTER_BYTES);
    pRes->length = calloc(numOfOutput, sizeof(int32_t));  // todo refactor
    pRes->buffer = calloc(numOfOutput, POINTER_BYTES);
295 296
  
    // not enough memory
H
hjxilinx 已提交
297
    if (pRes->tsrow == NULL || (pRes->buffer == NULL && pRes->numOfCols > 0)) {
298 299
      tfree(pRes->tsrow);
      tfree(pRes->buffer);
300
      tfree(pRes->length);
301
    
H
hzcheng 已提交
302 303 304 305 306 307 308 309 310
      pRes->code = TSDB_CODE_CLI_OUT_OF_MEMORY;
      return pRes->code;
    }
  }

  return TSDB_CODE_SUCCESS;
}

void tscDestroyResPointerInfo(SSqlRes* pRes) {
H
hjxilinx 已提交
311
  if (pRes->buffer != NULL) { // free all buffers containing the multibyte string
H
hjxilinx 已提交
312
    for (int i = 0; i < pRes->numOfCols; i++) {
H
hzcheng 已提交
313 314
      tfree(pRes->buffer[i]);
    }
315
    
H
hjxilinx 已提交
316
    pRes->numOfCols = 0;
H
hzcheng 已提交
317
  }
318 319
  
  tfree(pRes->pRsp);
H
hzcheng 已提交
320
  tfree(pRes->tsrow);
321
  tfree(pRes->length);
322 323 324 325 326
  
  tfree(pRes->pGroupRec);
  tfree(pRes->pColumnIndex);
  tfree(pRes->buffer);
  
327 328 329 330 331
  if (pRes->pArithSup != NULL) {
    tfree(pRes->pArithSup->data);
    tfree(pRes->pArithSup);
  }
  
332
  pRes->data = NULL;  // pRes->data points to the buffer of pRsp, no need to free
H
hzcheng 已提交
333 334
}

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
static void tscFreeQueryInfo(SSqlCmd* pCmd) {
  if (pCmd == NULL || pCmd->numOfClause == 0) {
    return;
  }
  
  for (int32_t i = 0; i < pCmd->numOfClause; ++i) {
    char* addr = (char*)pCmd - offsetof(SSqlObj, cmd);
    SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, i);
    
    freeQueryInfoImpl(pQueryInfo);
    clearAllTableMetaInfo(pQueryInfo, (const char*)addr, false);
    tfree(pQueryInfo);
  }
  
  pCmd->numOfClause = 0;
  tfree(pCmd->pQueryInfo);
}

353 354
void tscResetSqlCmdObj(SSqlCmd* pCmd) {
  pCmd->command   = 0;
355
  pCmd->numOfCols = 0;
356 357 358 359
  pCmd->count     = 0;
  pCmd->curSql    = NULL;
  pCmd->msgType   = 0;
  pCmd->parseFinished = 0;
360
  pCmd->autoCreated = 0;
361 362
  
  taosHashCleanup(pCmd->pTableList);
363
  pCmd->pTableList = NULL;
364
  
S
slguan 已提交
365
  pCmd->pDataBlocks = tscDestroyBlockArrayList(pCmd->pDataBlocks);
366
  
367
  tscFreeQueryInfo(pCmd);
H
hzcheng 已提交
368 369
}

H
hjxilinx 已提交
370
void tscFreeSqlResult(SSqlObj* pSql) {
371 372
  tscDestroyLocalReducer(pSql);
  
H
hjxilinx 已提交
373
  SSqlRes* pRes = &pSql->res;
374
  tscDestroyResPointerInfo(pRes);
375
  
H
hjxilinx 已提交
376
  memset(&pSql->res, 0, sizeof(SSqlRes));
377 378
}

H
hjxilinx 已提交
379
void tscPartiallyFreeSqlObj(SSqlObj* pSql) {
S
slguan 已提交
380 381 382
  if (pSql == NULL || pSql->signature != pSql) {
    return;
  }
H
hzcheng 已提交
383 384 385 386 387

  SSqlCmd* pCmd = &pSql->cmd;
  STscObj* pObj = pSql->pTscObj;

  int32_t cmd = pCmd->command;
H
hjxilinx 已提交
388
  if (cmd < TSDB_SQL_INSERT || cmd == TSDB_SQL_RETRIEVE_LOCALMERGE || cmd == TSDB_SQL_RETRIEVE_EMPTY_RESULT ||
389
      cmd == TSDB_SQL_TABLE_JOIN_RETRIEVE) {
H
hzcheng 已提交
390 391
    tscRemoveFromSqlList(pSql);
  }
392
  
H
hzcheng 已提交
393
  // pSql->sqlstr will be used by tscBuildQueryStreamDesc
L
lihui 已提交
394 395 396 397 398
  if (pObj->signature == pObj) {
    pthread_mutex_lock(&pObj->mutex);
    tfree(pSql->sqlstr);
    pthread_mutex_unlock(&pObj->mutex);
  }
399
  
weixin_48148422's avatar
weixin_48148422 已提交
400
  tscFreeSqlResult(pSql);
H
[TD-98]  
hjxilinx 已提交
401
  
402
  tfree(pSql->pSubs);
403
  pSql->freed = 0;
H
[TD-98]  
hjxilinx 已提交
404 405
  pSql->numOfSubs = 0;
  
406
  tscResetSqlCmdObj(pCmd);
407
  
408
  tscTrace("%p partially free sqlObj completed", pSql);
H
hzcheng 已提交
409 410 411
}

void tscFreeSqlObj(SSqlObj* pSql) {
H
Haojun Liao 已提交
412 413 414 415
  if (pSql == NULL || pSql->signature != pSql) {
    return;
  }
  
H
hzcheng 已提交
416
  tscTrace("%p start to free sql object", pSql);
H
hjxilinx 已提交
417
  tscPartiallyFreeSqlObj(pSql);
H
hzcheng 已提交
418 419 420

  pSql->signature = NULL;
  pSql->fp = NULL;
421
  
H
hzcheng 已提交
422 423
  SSqlCmd* pCmd = &pSql->cmd;

S
slguan 已提交
424
  memset(pCmd->payload, 0, (size_t)pCmd->allocSize);
H
hzcheng 已提交
425 426
  tfree(pCmd->payload);
  pCmd->allocSize = 0;
427 428
  
  tfree(pSql->sqlstr);
H
hzcheng 已提交
429 430 431
  free(pSql);
}

S
slguan 已提交
432 433
void tscDestroyDataBlock(STableDataBlocks* pDataBlock) {
  if (pDataBlock == NULL) {
H
hzcheng 已提交
434 435 436
    return;
  }

S
slguan 已提交
437
  tfree(pDataBlock->pData);
S
slguan 已提交
438
  tfree(pDataBlock->params);
H
hjxilinx 已提交
439

H
hjxilinx 已提交
440
  // free the refcount for metermeta
H
hjxilinx 已提交
441
  taosCacheRelease(tscCacheHandle, (void**)&(pDataBlock->pTableMeta), false);
S
slguan 已提交
442
  tfree(pDataBlock);
H
hzcheng 已提交
443 444
}

445 446
SParamInfo* tscAddParamToDataBlock(STableDataBlocks* pDataBlock, char type, uint8_t timePrec, short bytes,
                                   uint32_t offset) {
S
slguan 已提交
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
  uint32_t needed = pDataBlock->numOfParams + 1;
  if (needed > pDataBlock->numOfAllocedParams) {
    needed *= 2;
    void* tmp = realloc(pDataBlock->params, needed * sizeof(SParamInfo));
    if (tmp == NULL) {
      return NULL;
    }
    pDataBlock->params = (SParamInfo*)tmp;
    pDataBlock->numOfAllocedParams = needed;
  }

  SParamInfo* param = pDataBlock->params + pDataBlock->numOfParams;
  param->idx = -1;
  param->type = type;
  param->timePrec = timePrec;
  param->bytes = bytes;
  param->offset = offset;

  ++pDataBlock->numOfParams;
  return param;
}

H
hzcheng 已提交
469 470 471 472
SDataBlockList* tscCreateBlockArrayList() {
  const int32_t DEFAULT_INITIAL_NUM_OF_BLOCK = 16;

  SDataBlockList* pDataBlockArrayList = calloc(1, sizeof(SDataBlockList));
S
slguan 已提交
473 474 475
  if (pDataBlockArrayList == NULL) {
    return NULL;
  }
H
hjxilinx 已提交
476
  
H
hzcheng 已提交
477 478
  pDataBlockArrayList->nAlloc = DEFAULT_INITIAL_NUM_OF_BLOCK;
  pDataBlockArrayList->pData = calloc(1, POINTER_BYTES * pDataBlockArrayList->nAlloc);
S
slguan 已提交
479 480 481 482
  if (pDataBlockArrayList->pData == NULL) {
    free(pDataBlockArrayList);
    return NULL;
  }
H
hzcheng 已提交
483 484 485 486

  return pDataBlockArrayList;
}

487
void tscAppendDataBlock(SDataBlockList* pList, STableDataBlocks* pBlocks) {
S
slguan 已提交
488
  if (pList->nSize >= pList->nAlloc) {
H
hjxilinx 已提交
489 490
    pList->nAlloc = (pList->nAlloc) << 1U;
    pList->pData = realloc(pList->pData, POINTER_BYTES * (size_t)pList->nAlloc);
S
slguan 已提交
491 492

    // reset allocated memory
H
hjxilinx 已提交
493
    memset(pList->pData + pList->nSize, 0, POINTER_BYTES * (pList->nAlloc - pList->nSize));
S
slguan 已提交
494 495 496 497 498
  }

  pList->pData[pList->nSize++] = pBlocks;
}

S
slguan 已提交
499 500 501
void* tscDestroyBlockArrayList(SDataBlockList* pList) {
  if (pList == NULL) {
    return NULL;
H
hzcheng 已提交
502 503
  }

S
slguan 已提交
504 505
  for (int32_t i = 0; i < pList->nSize; i++) {
    tscDestroyDataBlock(pList->pData[i]);
H
hzcheng 已提交
506 507
  }

S
slguan 已提交
508 509 510 511
  tfree(pList->pData);
  tfree(pList);

  return NULL;
H
hzcheng 已提交
512 513
}

S
slguan 已提交
514
int32_t tscCopyDataBlockToPayload(SSqlObj* pSql, STableDataBlocks* pDataBlock) {
H
hjxilinx 已提交
515
  SSqlCmd* pCmd = &pSql->cmd;
H
hjxilinx 已提交
516
  assert(pDataBlock->pTableMeta != NULL);
H
hjxilinx 已提交
517

S
slguan 已提交
518
  pCmd->numOfTablesInSubmit = pDataBlock->numOfTables;
519

520
  assert(pCmd->numOfClause == 1);
521
  STableMetaInfo* pTableMetaInfo = tscGetTableMetaInfoFromCmd(pCmd, pCmd->clauseIndex, 0);
H
hjxilinx 已提交
522

523
  // set the correct table meta object, the table meta has been locked in pDataBlocks, so it must be in the cache
H
hjxilinx 已提交
524 525 526
  if (pTableMetaInfo->pTableMeta != pDataBlock->pTableMeta) {
    strcpy(pTableMetaInfo->name, pDataBlock->tableId);
    taosCacheRelease(tscCacheHandle, (void**)&(pTableMetaInfo->pTableMeta), false);
527

H
hjxilinx 已提交
528
    pTableMetaInfo->pTableMeta = taosCacheTransfer(tscCacheHandle, (void**)&pDataBlock->pTableMeta);
H
hjxilinx 已提交
529
  } else {
H
hjxilinx 已提交
530
    assert(strncmp(pTableMetaInfo->name, pDataBlock->tableId, tListLen(pDataBlock->tableId)) == 0);
H
hjxilinx 已提交
531
  }
H
hjxilinx 已提交
532

533 534
  /*
   * the submit message consists of : [RPC header|message body|digest]
535
   * the dataBlock only includes the RPC Header buffer and actual submit message body, space for digest needs
536 537
   * additional space.
   */
S
slguan 已提交
538
  int ret = tscAllocPayload(pCmd, pDataBlock->nAllocSize + 100);
H
hjxilinx 已提交
539 540 541
  if (TSDB_CODE_SUCCESS != ret) {
    return ret;
  }
H
hjxilinx 已提交
542

H
hzcheng 已提交
543
  memcpy(pCmd->payload, pDataBlock->pData, pDataBlock->nAllocSize);
H
hjxilinx 已提交
544

545 546 547 548
  /*
   * the payloadLen should be actual message body size
   * the old value of payloadLen is the allocated payload size
   */
549
  pCmd->payloadLen = pDataBlock->nAllocSize - tsRpcHeadSize;
H
hjxilinx 已提交
550

H
hjxilinx 已提交
551
  assert(pCmd->allocSize >= pCmd->payloadLen + tsRpcHeadSize + 100 && pCmd->payloadLen > 0);
H
hjxilinx 已提交
552
  return TSDB_CODE_SUCCESS;
H
hzcheng 已提交
553 554 555 556 557
}

void tscFreeUnusedDataBlocks(SDataBlockList* pList) {
  /* release additional memory consumption */
  for (int32_t i = 0; i < pList->nSize; ++i) {
S
slguan 已提交
558 559 560
    STableDataBlocks* pDataBlock = pList->pData[i];
    pDataBlock->pData = realloc(pDataBlock->pData, pDataBlock->size);
    pDataBlock->nAllocSize = (uint32_t)pDataBlock->size;
H
hzcheng 已提交
561 562 563
  }
}

H
hjxilinx 已提交
564 565 566 567 568 569
/**
 * create the in-memory buffer for each table to keep the submitted data block
 * @param initialSize
 * @param rowSize
 * @param startOffset
 * @param name
H
hjxilinx 已提交
570
 * @param dataBlocks
H
hjxilinx 已提交
571 572
 * @return
 */
H
hjxilinx 已提交
573
int32_t tscCreateDataBlock(size_t initialSize, int32_t rowSize, int32_t startOffset, const char* name,
H
hjxilinx 已提交
574
                           STableMeta* pTableMeta, STableDataBlocks** dataBlocks) {
H
hjxilinx 已提交
575
  STableDataBlocks* dataBuf = (STableDataBlocks*)calloc(1, sizeof(STableDataBlocks));
H
hjxilinx 已提交
576 577 578 579 580 581
  if (dataBuf == NULL) {
    tscError("failed to allocated memory, reason:%s", strerror(errno));
    return TSDB_CODE_CLI_OUT_OF_MEMORY;
  }

  dataBuf->nAllocSize = (uint32_t)initialSize;
L
[#1102]  
lihui 已提交
582
  dataBuf->headerSize = startOffset; // the header size will always be the startOffset value, reserved for the subumit block header
H
hjxilinx 已提交
583 584 585 586
  if (dataBuf->nAllocSize <= dataBuf->headerSize) {
    dataBuf->nAllocSize = dataBuf->headerSize*2;
  }
  
H
hjxilinx 已提交
587 588 589
  dataBuf->pData = calloc(1, dataBuf->nAllocSize);
  dataBuf->ordered = true;
  dataBuf->prevTS = INT64_MIN;
S
slguan 已提交
590 591 592

  dataBuf->rowSize = rowSize;
  dataBuf->size = startOffset;
S
slguan 已提交
593 594
  dataBuf->tsSource = -1;

S
slguan 已提交
595
  strncpy(dataBuf->tableId, name, TSDB_TABLE_ID_LEN);
H
hjxilinx 已提交
596 597

  /*
598
   * The table meta may be released since the table meta cache are completed clean by other thread
599 600
   * due to operation such as drop database. So here we add the reference count directly instead of invoke
   * taosGetDataFromCache, which may return NULL value.
H
hjxilinx 已提交
601
   */
H
hjxilinx 已提交
602 603
  dataBuf->pTableMeta = taosCacheAcquireByData(tscCacheHandle, pTableMeta);
  assert(initialSize > 0 && pTableMeta != NULL && dataBuf->pTableMeta != NULL);
604

605 606
  *dataBlocks = dataBuf;
  return TSDB_CODE_SUCCESS;
S
slguan 已提交
607 608
}

H
hjxilinx 已提交
609
int32_t tscGetDataBlockFromList(void* pHashList, SDataBlockList* pDataBlockList, int64_t id, int32_t size,
H
hjxilinx 已提交
610
                                int32_t startOffset, int32_t rowSize, const char* tableId, STableMeta* pTableMeta,
H
hjxilinx 已提交
611 612
                                STableDataBlocks** dataBlocks) {
  *dataBlocks = NULL;
S
slguan 已提交
613

H
hjxilinx 已提交
614
  STableDataBlocks** t1 = (STableDataBlocks**)taosHashGet(pHashList, (const char*)&id, sizeof(id));
S
slguan 已提交
615
  if (t1 != NULL) {
H
hjxilinx 已提交
616
    *dataBlocks = *t1;
S
slguan 已提交
617 618
  }

H
hjxilinx 已提交
619
  if (*dataBlocks == NULL) {
H
hjxilinx 已提交
620
    int32_t ret = tscCreateDataBlock((size_t)size, rowSize, startOffset, tableId, pTableMeta, dataBlocks);
H
hjxilinx 已提交
621 622 623 624
    if (ret != TSDB_CODE_SUCCESS) {
      return ret;
    }

H
hjxilinx 已提交
625
    taosHashPut(pHashList, (const char*)&id, sizeof(int64_t), (char*)dataBlocks, POINTER_BYTES);
H
hjxilinx 已提交
626
    tscAppendDataBlock(pDataBlockList, *dataBlocks);
S
slguan 已提交
627 628
  }

H
hjxilinx 已提交
629
  return TSDB_CODE_SUCCESS;
S
slguan 已提交
630 631
}

H
TD-100  
hzcheng 已提交
632
static int trimDataBlock(void* pDataBlock, STableDataBlocks* pTableDataBlock) {
633
  // TODO: optimize this function, handle the case while binary is not presented
H
TD-100  
hzcheng 已提交
634
  int len = 0;
H
TD-166  
hzcheng 已提交
635 636

  STableMeta*   pTableMeta = pTableDataBlock->pTableMeta;
637
  STableComInfo tinfo = tscGetTableInfo(pTableMeta);
H
TD-166  
hzcheng 已提交
638 639 640
  SSchema*      pSchema = tscGetTableSchema(pTableMeta);

  SSubmitBlk* pBlock = pDataBlock;
641 642
  memcpy(pDataBlock, pTableDataBlock->pData, sizeof(SSubmitBlk));
  pDataBlock += sizeof(SSubmitBlk);
H
TD-166  
hzcheng 已提交
643

H
hjxilinx 已提交
644
  int32_t flen = 0;  // original total length of row
H
TD-166  
hzcheng 已提交
645 646
  for (int32_t i = 0; i < tinfo.numOfColumns; ++i) {
    flen += TYPE_BYTES[pSchema[i].type];
647
  }
H
TD-166  
hzcheng 已提交
648

649
  char* p = pTableDataBlock->pData + sizeof(SSubmitBlk);
H
TD-166  
hzcheng 已提交
650
  pBlock->len = 0;
H
hjxilinx 已提交
651 652 653
  int32_t numOfRows = htons(pBlock->numOfRows);
  
  for (int32_t i = 0; i < numOfRows; ++i) {
H
TD-166  
hzcheng 已提交
654 655 656 657 658
    SDataRow trow = (SDataRow)pDataBlock;
    dataRowSetLen(trow, TD_DATA_ROW_HEAD_SIZE + flen);

    int toffset = 0;
    for (int32_t j = 0; j < tinfo.numOfColumns; j++) {
H
TD-166  
hzcheng 已提交
659
      tdAppendColVal(trow, p, pSchema[j].type, pSchema[j].bytes, toffset);
H
TD-166  
hzcheng 已提交
660 661 662 663 664 665 666
      toffset += TYPE_BYTES[pSchema[j].type];
      p += pSchema[j].bytes;
    }

    // p += pTableDataBlock->rowSize;
    pDataBlock += dataRowLen(trow);
    pBlock->len += dataRowLen(trow);
667
  }
H
TD-166  
hzcheng 已提交
668

H
TD-100  
hzcheng 已提交
669
  len = pBlock->len;
H
TD-166  
hzcheng 已提交
670
  pBlock->len = htonl(pBlock->len);
H
TD-100  
hzcheng 已提交
671
  return len;
672 673
}

S
slguan 已提交
674 675 676
int32_t tscMergeTableDataBlocks(SSqlObj* pSql, SDataBlockList* pTableDataBlockList) {
  SSqlCmd* pCmd = &pSql->cmd;

H
hjxilinx 已提交
677
  void* pVnodeDataBlockHashList = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false);
S
slguan 已提交
678 679 680 681
  SDataBlockList* pVnodeDataBlockList = tscCreateBlockArrayList();

  for (int32_t i = 0; i < pTableDataBlockList->nSize; ++i) {
    STableDataBlocks* pOneTableBlock = pTableDataBlockList->pData[i];
S
slguan 已提交
682

H
hjxilinx 已提交
683
    STableDataBlocks* dataBuf = NULL;
H
hjxilinx 已提交
684 685 686
    
    int32_t ret =
        tscGetDataBlockFromList(pVnodeDataBlockHashList, pVnodeDataBlockList, pOneTableBlock->vgId, TSDB_PAYLOAD_SIZE,
H
hjxilinx 已提交
687
                                tsInsertHeadSize, 0, pOneTableBlock->tableId, pOneTableBlock->pTableMeta, &dataBuf);
H
hjxilinx 已提交
688
    if (ret != TSDB_CODE_SUCCESS) {
689
      tscError("%p failed to prepare the data block buffer for merging table data, code:%d", pSql, ret);
H
hjxilinx 已提交
690
      taosHashCleanup(pVnodeDataBlockHashList);
691
      tscDestroyBlockArrayList(pVnodeDataBlockList);
H
hjxilinx 已提交
692 693
      return ret;
    }
S
slguan 已提交
694

695
    int64_t destSize = dataBuf->size + pOneTableBlock->size + pOneTableBlock->size*sizeof(int32_t)*2;
S
slguan 已提交
696 697 698 699 700 701 702 703 704
    if (dataBuf->nAllocSize < destSize) {
      while (dataBuf->nAllocSize < destSize) {
        dataBuf->nAllocSize = dataBuf->nAllocSize * 1.5;
      }

      char* tmp = realloc(dataBuf->pData, dataBuf->nAllocSize);
      if (tmp != NULL) {
        dataBuf->pData = tmp;
        memset(dataBuf->pData + dataBuf->size, 0, dataBuf->nAllocSize - dataBuf->size);
705
      } else {  // failed to allocate memory, free already allocated memory and return error code
S
slguan 已提交
706 707
        tscError("%p failed to allocate memory for merging submit block, size:%d", pSql, dataBuf->nAllocSize);

H
hjxilinx 已提交
708
        taosHashCleanup(pVnodeDataBlockHashList);
S
slguan 已提交
709
        tscDestroyBlockArrayList(pVnodeDataBlockList);
710
        tfree(dataBuf->pData);
S
slguan 已提交
711 712

        return TSDB_CODE_CLI_OUT_OF_MEMORY;
S
slguan 已提交
713 714 715
      }
    }

716
    SSubmitBlk* pBlocks = (SSubmitBlk*) pOneTableBlock->pData;
H
hjxilinx 已提交
717
    tscSortRemoveDataBlockDupRows(pOneTableBlock);
S
slguan 已提交
718

719
    char* e = (char*)pBlocks->data + pOneTableBlock->rowSize*(pBlocks->numOfRows-1);
L
lihui 已提交
720
    
721 722
    tscTrace("%p tableId:%s, sid:%d rows:%d sversion:%d skey:%" PRId64 ", ekey:%" PRId64, pSql, pOneTableBlock->tableId,
        pBlocks->tid, pBlocks->numOfRows, pBlocks->sversion, GET_INT64_VAL(pBlocks->data), GET_INT64_VAL(e));
S
slguan 已提交
723

724 725 726
    int32_t len = pBlocks->numOfRows * (pOneTableBlock->rowSize + sizeof(int32_t) * 2);
    
    pBlocks->tid = htonl(pBlocks->tid);
S
slguan 已提交
727 728 729
    pBlocks->uid = htobe64(pBlocks->uid);
    pBlocks->sversion = htonl(pBlocks->sversion);
    pBlocks->numOfRows = htons(pBlocks->numOfRows);
730 731 732 733
    
    pBlocks->len = htonl(len);
    
    // erase the empty space reserved for binary data
H
TD-100  
hzcheng 已提交
734
    len = trimDataBlock(dataBuf->pData + dataBuf->size, pOneTableBlock);
735
    dataBuf->size += (len + sizeof(SSubmitBlk));
S
slguan 已提交
736
    dataBuf->numOfTables += 1;
S
slguan 已提交
737 738 739 740 741 742 743 744
  }

  tscDestroyBlockArrayList(pTableDataBlockList);

  // free the table data blocks;
  pCmd->pDataBlocks = pVnodeDataBlockList;

  tscFreeUnusedDataBlocks(pCmd->pDataBlocks);
H
hjxilinx 已提交
745
  taosHashCleanup(pVnodeDataBlockHashList);
S
slguan 已提交
746 747

  return TSDB_CODE_SUCCESS;
S
slguan 已提交
748 749
}

H
hzcheng 已提交
750
void tscCloseTscObj(STscObj* pObj) {
H
hjxilinx 已提交
751 752
  assert(pObj != NULL);
  
H
hzcheng 已提交
753 754 755
  pObj->signature = NULL;
  taosTmrStopA(&(pObj->pTimer));
  pthread_mutex_destroy(&pObj->mutex);
756
  
757 758 759 760
  if (pObj->pDnodeConn != NULL) {
    rpcClose(pObj->pDnodeConn);
  }
  
761
  tscTrace("%p DB connection is closed, dnodeConn:%p", pObj, pObj->pDnodeConn);
H
hzcheng 已提交
762 763 764
  tfree(pObj);
}

H
hjxilinx 已提交
765
bool tscIsInsertData(char* sqlstr) {
766 767 768 769 770 771 772 773
  int32_t index = 0;

  do {
    SSQLToken t0 = tStrGetToken(sqlstr, &index, false, 0, NULL);
    if (t0.type != TK_LP) {
      return t0.type == TK_INSERT || t0.type == TK_IMPORT;
    }
  } while (1);
H
hzcheng 已提交
774 775
}

S
slguan 已提交
776
int tscAllocPayload(SSqlCmd* pCmd, int size) {
H
hzcheng 已提交
777 778 779 780 781
  assert(size > 0);

  if (pCmd->payload == NULL) {
    assert(pCmd->allocSize == 0);

782
    pCmd->payload = (char*)calloc(1, size);
H
hzcheng 已提交
783 784 785 786
    if (pCmd->payload == NULL) return TSDB_CODE_CLI_OUT_OF_MEMORY;
    pCmd->allocSize = size;
  } else {
    if (pCmd->allocSize < size) {
787
      char* b = realloc(pCmd->payload, size);
S
slguan 已提交
788 789
      if (b == NULL) return TSDB_CODE_CLI_OUT_OF_MEMORY;
      pCmd->payload = b;
H
hzcheng 已提交
790 791
      pCmd->allocSize = size;
    }
792
    
H
hjxilinx 已提交
793
    memset(pCmd->payload, 0, pCmd->allocSize);
H
hzcheng 已提交
794 795 796 797 798 799
  }

  assert(pCmd->allocSize >= size);
  return TSDB_CODE_SUCCESS;
}

H
hjxilinx 已提交
800 801 802 803
TAOS_FIELD tscCreateField(int8_t type, const char* name, int16_t bytes) {
  TAOS_FIELD f = { .type = type, .bytes = bytes, };
  strncpy(f.name, name, TSDB_COL_NAME_LEN);
  return f;
H
hzcheng 已提交
804 805
}

H
hjxilinx 已提交
806 807 808 809
SFieldSupInfo* tscFieldInfoAppend(SFieldInfo* pFieldInfo, TAOS_FIELD* pField) {
  assert(pFieldInfo != NULL);
  taosArrayPush(pFieldInfo->pFields, pField);
  pFieldInfo->numOfOutput++;
H
hjxilinx 已提交
810
  
H
hjxilinx 已提交
811 812 813 814 815 816 817
  struct SFieldSupInfo info = {
    .pSqlExpr = NULL,
    .pArithExprInfo = NULL,
    .visible = true,
  };
  
  return taosArrayPush(pFieldInfo->pSupportInfo, &info);
H
hzcheng 已提交
818 819
}

H
hjxilinx 已提交
820 821
SFieldSupInfo* tscFieldInfoGetSupp(SFieldInfo* pFieldInfo, int32_t index) {
  return taosArrayGet(pFieldInfo->pSupportInfo, index);
H
hjxilinx 已提交
822 823
}

H
hjxilinx 已提交
824 825 826 827 828 829 830 831 832 833 834
SFieldSupInfo* tscFieldInfoInsert(SFieldInfo* pFieldInfo, int32_t index, TAOS_FIELD* field) {
  taosArrayInsert(pFieldInfo->pFields, index, field);
  pFieldInfo->numOfOutput++;
  
  struct SFieldSupInfo info = {
      .pSqlExpr = NULL,
      .pArithExprInfo = NULL,
      .visible = true,
  };
  
  return taosArrayInsert(pFieldInfo->pSupportInfo, index, &info);
H
hjxilinx 已提交
835
}
H
hzcheng 已提交
836

H
hjxilinx 已提交
837
void tscFieldInfoUpdateOffset(SQueryInfo* pQueryInfo) {
H
hjxilinx 已提交
838 839
  size_t numOfExprs = tscSqlExprNumOfExprs(pQueryInfo);
  
H
hjxilinx 已提交
840
  SSqlExpr* pExpr = taosArrayGetP(pQueryInfo->exprList, 0);
H
hjxilinx 已提交
841
  pExpr->offset = 0;
H
hjxilinx 已提交
842
  
H
hjxilinx 已提交
843
  for (int32_t i = 1; i < numOfExprs; ++i) {
H
hjxilinx 已提交
844 845
    SSqlExpr* prev = taosArrayGetP(pQueryInfo->exprList, i - 1);
    SSqlExpr* p = taosArrayGetP(pQueryInfo->exprList, i);
H
hjxilinx 已提交
846 847
  
    p->offset = prev->offset + prev->resBytes;
H
hzcheng 已提交
848 849 850
  }
}

851
void tscFieldInfoUpdateOffsetForInterResult(SQueryInfo* pQueryInfo) {
H
hjxilinx 已提交
852
  if (tscSqlExprNumOfExprs(pQueryInfo) == 0) {
H
hzcheng 已提交
853 854
    return;
  }
H
hjxilinx 已提交
855
  
H
hjxilinx 已提交
856
  SSqlExpr* pExpr = taosArrayGetP(pQueryInfo->exprList, 0);
H
hjxilinx 已提交
857
  pExpr->offset = 0;
H
hjxilinx 已提交
858
  
H
hjxilinx 已提交
859 860
  size_t numOfExprs = tscSqlExprNumOfExprs(pQueryInfo);
  for (int32_t i = 1; i < numOfExprs; ++i) {
H
hjxilinx 已提交
861 862
    SSqlExpr* prev = taosArrayGetP(pQueryInfo->exprList, i - 1);
    SSqlExpr* p = taosArrayGetP(pQueryInfo->exprList, i);
H
hjxilinx 已提交
863 864
    
    p->offset = prev->offset + prev->resBytes;
H
hzcheng 已提交
865 866 867
  }
}

H
hjxilinx 已提交
868
void tscFieldInfoCopy(SFieldInfo* dst, const SFieldInfo* src) {
H
hjxilinx 已提交
869
  dst->numOfOutput = src->numOfOutput;
H
hzcheng 已提交
870

H
hjxilinx 已提交
871 872 873 874 875 876 877 878 879 880 881
  if (dst->pFields == NULL) {
    dst->pFields = taosArrayClone(src->pFields);
  } else {
    taosArrayCopy(dst->pFields, src->pFields);
  }
  
  if (dst->pSupportInfo == NULL) {
    dst->pSupportInfo = taosArrayClone(src->pSupportInfo);
  } else {
    taosArrayCopy(dst->pSupportInfo, src->pSupportInfo);
  }
H
hzcheng 已提交
882 883
}

H
hjxilinx 已提交
884 885
TAOS_FIELD* tscFieldInfoGetField(SFieldInfo* pFieldInfo, int32_t index) {
  return taosArrayGet(pFieldInfo->pFields, index);
886 887
}

H
hjxilinx 已提交
888
int32_t tscNumOfFields(SQueryInfo* pQueryInfo) { return pQueryInfo->fieldsInfo.numOfOutput; }
H
hzcheng 已提交
889

890
int16_t tscFieldInfoGetOffset(SQueryInfo* pQueryInfo, int32_t index) {
H
hjxilinx 已提交
891 892
  SFieldSupInfo* pInfo = tscFieldInfoGetSupp(&pQueryInfo->fieldsInfo, index);
  assert(pInfo != NULL);
H
hzcheng 已提交
893

H
hjxilinx 已提交
894
  return pInfo->pSqlExpr->offset;
H
hzcheng 已提交
895 896
}

H
hjxilinx 已提交
897
int32_t tscFieldInfoCompare(const SFieldInfo* pFieldInfo1, const SFieldInfo* pFieldInfo2) {
898
  assert(pFieldInfo1 != NULL && pFieldInfo2 != NULL);
899

H
hjxilinx 已提交
900 901
  if (pFieldInfo1->numOfOutput != pFieldInfo2->numOfOutput) {
    return pFieldInfo1->numOfOutput - pFieldInfo2->numOfOutput;
902 903
  }

H
hjxilinx 已提交
904 905 906
  for (int32_t i = 0; i < pFieldInfo1->numOfOutput; ++i) {
    TAOS_FIELD* pField1 = tscFieldInfoGetField((SFieldInfo*) pFieldInfo1, i);
    TAOS_FIELD* pField2 = tscFieldInfoGetField((SFieldInfo*) pFieldInfo2, i);
907

H
hjxilinx 已提交
908 909
    if (pField1->type != pField2->type ||
        pField1->bytes != pField2->bytes ||
910 911 912 913 914 915 916 917
        strcasecmp(pField1->name, pField2->name) != 0) {
      return 1;
    }
  }

  return 0;
}

H
hjxilinx 已提交
918 919 920
int32_t tscGetResRowLength(SArray* pExprList) {
  size_t num = taosArrayGetSize(pExprList);
  if (num == 0) {
H
hzcheng 已提交
921 922
    return 0;
  }
H
hjxilinx 已提交
923 924
  
  int32_t size = 0;
H
hjxilinx 已提交
925 926 927
  for(int32_t i = 0; i < num; ++i) {
    SSqlExpr* pExpr = taosArrayGetP(pExprList, i);
    size += pExpr->resBytes;
H
hjxilinx 已提交
928 929 930
  }
  
  return size;
H
hzcheng 已提交
931 932
}

H
hjxilinx 已提交
933
void tscFieldInfoClear(SFieldInfo* pFieldInfo) {
S
slguan 已提交
934
  if (pFieldInfo == NULL) {
H
hzcheng 已提交
935 936 937
    return;
  }

H
hjxilinx 已提交
938
  taosArrayDestroy(pFieldInfo->pFields);
H
hjxilinx 已提交
939
  
H
hjxilinx 已提交
940 941 942 943
  for(int32_t i = 0; i < pFieldInfo->numOfOutput; ++i) {
    SFieldSupInfo* pInfo = taosArrayGet(pFieldInfo->pSupportInfo, i);
    
    if (pInfo->pArithExprInfo != NULL) {
944
      tExprTreeDestroy(&pInfo->pArithExprInfo->pExpr, NULL);
945
      tfree(pInfo->pArithExprInfo);
H
hjxilinx 已提交
946 947 948
    }
  }
  
H
hjxilinx 已提交
949
  taosArrayDestroy(pFieldInfo->pSupportInfo);
S
slguan 已提交
950
  memset(pFieldInfo, 0, sizeof(SFieldInfo));
H
hzcheng 已提交
951 952
}

H
hjxilinx 已提交
953
static SSqlExpr* doBuildSqlExpr(SQueryInfo* pQueryInfo, int16_t functionId, SColumnIndex* pColIndex, int16_t type,
954
    int16_t size, int16_t interSize, bool isTagCol) {
H
hjxilinx 已提交
955
  STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, pColIndex->tableIndex);
H
hjxilinx 已提交
956
  
H
hjxilinx 已提交
957
  SSqlExpr* pExpr = calloc(1, sizeof(SSqlExpr));
S
slguan 已提交
958
  pExpr->functionId = functionId;
959
  
960
  // set the correct columnIndex index
S
slguan 已提交
961 962 963
  if (pColIndex->columnIndex == TSDB_TBNAME_COLUMN_INDEX) {
    pExpr->colInfo.colId = TSDB_TBNAME_COLUMN_INDEX;
  } else {
964 965 966 967
    if (isTagCol) {
      SSchema* pSchema = tscGetTableTagSchema(pTableMetaInfo->pTableMeta);
      pExpr->colInfo.colId = pSchema[pColIndex->columnIndex].colId;
      strncpy(pExpr->colInfo.name, pSchema[pColIndex->columnIndex].name, TSDB_COL_NAME_LEN);
S
slguan 已提交
968
    } else {
969 970 971
      SSchema* pSchema = tscGetTableColumnSchema(pTableMetaInfo->pTableMeta, pColIndex->columnIndex);
      pExpr->colInfo.colId = pSchema->colId;
      strncpy(pExpr->colInfo.name, pSchema->name, TSDB_COL_NAME_LEN);
S
slguan 已提交
972
    }
H
hzcheng 已提交
973
  }
H
hjxilinx 已提交
974
  
975 976
  pExpr->colInfo.flag = isTagCol? TSDB_COL_TAG:TSDB_COL_NORMAL;
  
977
  pExpr->colInfo.colIndex = pColIndex->columnIndex;
H
hjxilinx 已提交
978 979
  pExpr->resType       = type;
  pExpr->resBytes      = size;
980
  pExpr->interBytes = interSize;
H
hjxilinx 已提交
981
  pExpr->uid           = pTableMetaInfo->pTableMeta->uid;
H
hjxilinx 已提交
982
  
H
hjxilinx 已提交
983 984 985 986
  return pExpr;
}

SSqlExpr* tscSqlExprInsert(SQueryInfo* pQueryInfo, int32_t index, int16_t functionId, SColumnIndex* pColIndex, int16_t type,
987
                           int16_t size, int16_t interSize, bool isTagCol) {
H
hjxilinx 已提交
988
  int32_t num = taosArrayGetSize(pQueryInfo->exprList);
H
hjxilinx 已提交
989
  if (index == num) {
990
    return tscSqlExprAppend(pQueryInfo, functionId, pColIndex, type, size, interSize, isTagCol);
H
hjxilinx 已提交
991 992
  }
  
993
  SSqlExpr* pExpr = doBuildSqlExpr(pQueryInfo, functionId, pColIndex, type, size, interSize, isTagCol);
H
hjxilinx 已提交
994
  taosArrayInsert(pQueryInfo->exprList, index, &pExpr);
H
hjxilinx 已提交
995 996 997 998
  return pExpr;
}

SSqlExpr* tscSqlExprAppend(SQueryInfo* pQueryInfo, int16_t functionId, SColumnIndex* pColIndex, int16_t type,
999 1000
    int16_t size, int16_t interSize, bool isTagCol) {
  SSqlExpr* pExpr = doBuildSqlExpr(pQueryInfo, functionId, pColIndex, type, size, interSize, isTagCol);
H
hjxilinx 已提交
1001
  taosArrayPush(pQueryInfo->exprList, &pExpr);
H
hzcheng 已提交
1002 1003 1004
  return pExpr;
}

1005 1006
SSqlExpr* tscSqlExprUpdate(SQueryInfo* pQueryInfo, int32_t index, int16_t functionId, int16_t srcColumnIndex,
                           int16_t type, int16_t size) {
H
hjxilinx 已提交
1007
  STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0);
H
hjxilinx 已提交
1008 1009
  SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, index);
  if (pExpr == NULL) {
H
hzcheng 已提交
1010 1011 1012
    return NULL;
  }

S
slguan 已提交
1013
  pExpr->functionId = functionId;
H
hzcheng 已提交
1014

1015
  pExpr->colInfo.colIndex = srcColumnIndex;
H
hjxilinx 已提交
1016
  pExpr->colInfo.colId = tscGetTableColumnSchema(pTableMetaInfo->pTableMeta, srcColumnIndex)->colId;
H
hzcheng 已提交
1017 1018 1019 1020 1021 1022 1023

  pExpr->resType = type;
  pExpr->resBytes = size;

  return pExpr;
}

1024
size_t tscSqlExprNumOfExprs(SQueryInfo* pQueryInfo) {
H
hjxilinx 已提交
1025
  return taosArrayGetSize(pQueryInfo->exprList);
H
hjxilinx 已提交
1026 1027
}

S
slguan 已提交
1028
void addExprParams(SSqlExpr* pExpr, char* argument, int32_t type, int32_t bytes, int16_t tableIndex) {
H
hzcheng 已提交
1029 1030 1031 1032 1033 1034
  if (pExpr == NULL || argument == NULL || bytes == 0) {
    return;
  }

  // set parameter value
  // transfer to tVariant from byte data/no ascii data
S
slguan 已提交
1035
  tVariantCreateFromBinary(&pExpr->param[pExpr->numOfParams], argument, bytes, type);
H
hzcheng 已提交
1036 1037 1038 1039 1040

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

1041
SSqlExpr* tscSqlExprGet(SQueryInfo* pQueryInfo, int32_t index) {
H
hjxilinx 已提交
1042
  return taosArrayGetP(pQueryInfo->exprList, index);
H
hzcheng 已提交
1043 1044
}

H
hjxilinx 已提交
1045
void* sqlExprDestroy(SSqlExpr* pExpr) {
H
hjxilinx 已提交
1046 1047 1048 1049 1050 1051 1052 1053
  if (pExpr == NULL) {
    return NULL;
  }
  
  for(int32_t i = 0; i < tListLen(pExpr->param); ++i) {
    tVariantDestroy(&pExpr->param[i]);
  }
  
H
hjxilinx 已提交
1054 1055
  tfree(pExpr);
  
H
hjxilinx 已提交
1056
  return NULL;
H
hzcheng 已提交
1057 1058
}

H
hjxilinx 已提交
1059 1060 1061
/*
 * NOTE: Does not release SSqlExprInfo here.
 */
H
hjxilinx 已提交
1062 1063
void tscSqlExprInfoDestroy(SArray* pExprInfo) {
  size_t size = taosArrayGetSize(pExprInfo);
H
hjxilinx 已提交
1064
  
H
hjxilinx 已提交
1065 1066 1067
  for(int32_t i = 0; i < size; ++i) {
    SSqlExpr* pExpr = taosArrayGetP(pExprInfo, i);
    sqlExprDestroy(pExpr);
H
hjxilinx 已提交
1068 1069
  }
  
H
hjxilinx 已提交
1070
  taosArrayDestroy(pExprInfo);
H
hjxilinx 已提交
1071 1072
}

H
hjxilinx 已提交
1073 1074
void tscSqlExprCopy(SArray* dst, const SArray* src, uint64_t uid, bool deepcopy) {
  assert(src != NULL && dst != NULL);
H
hjxilinx 已提交
1075
  
H
hjxilinx 已提交
1076 1077 1078 1079 1080
  size_t size = taosArrayGetSize(src);
  for (int32_t i = 0; i < size; ++i) {
    SSqlExpr* pExpr = taosArrayGetP(src, i);
    
    if (pExpr->uid == uid) {
H
hjxilinx 已提交
1081 1082
      
      if (deepcopy) {
H
hjxilinx 已提交
1083 1084 1085 1086 1087 1088 1089 1090
        SSqlExpr* p1 = calloc(1, sizeof(SSqlExpr));
        *p1 = *pExpr;
  
        for (int32_t j = 0; j < pExpr->numOfParams; ++j) {
          tVariantAssign(&p1->param[j], &pExpr->param[j]);
        }
        
        taosArrayPush(dst, &p1);
H
hjxilinx 已提交
1091
      } else {
H
hjxilinx 已提交
1092
        taosArrayPush(dst, &pExpr);
H
hjxilinx 已提交
1093
      }
S
slguan 已提交
1094 1095
    }
  }
H
hzcheng 已提交
1096 1097
}

1098
SColumn* tscColumnListInsert(SArray* pColumnList, SColumnIndex* pColIndex) {
1099
  // ignore the tbname columnIndex to be inserted into source list
S
slguan 已提交
1100
  if (pColIndex->columnIndex < 0) {
H
hzcheng 已提交
1101 1102
    return NULL;
  }
1103 1104
  
  size_t numOfCols = taosArrayGetSize(pColumnList);
S
slguan 已提交
1105 1106
  int16_t col = pColIndex->columnIndex;

H
hzcheng 已提交
1107
  int32_t i = 0;
1108 1109 1110
  while (i < numOfCols) {
    SColumn* pCol = taosArrayGetP(pColumnList, i);
    if (pCol->colIndex.columnIndex < col) {
S
slguan 已提交
1111
      i++;
1112
    } else if (pCol->colIndex.tableIndex < pColIndex->tableIndex) {
S
slguan 已提交
1113 1114 1115 1116
      i++;
    } else {
      break;
    }
H
hzcheng 已提交
1117 1118
  }

1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
  if (i >= numOfCols || numOfCols == 0) {
    SColumn* b = calloc(1, sizeof(SColumn));
    b->colIndex = *pColIndex;
    
    taosArrayInsert(pColumnList, i, &b);
  } else {
    SColumn* pCol = taosArrayGetP(pColumnList, i);
  
    if (i < numOfCols && (pCol->colIndex.columnIndex > col || pCol->colIndex.tableIndex != pColIndex->tableIndex)) {
      SColumn* b = calloc(1, sizeof(SColumn));
      b->colIndex = *pColIndex;
      
      taosArrayInsert(pColumnList, i, &b);
    }
H
hzcheng 已提交
1133 1134
  }

1135
  return taosArrayGetP(pColumnList, i);
H
hzcheng 已提交
1136 1137
}

1138
SColumnFilterInfo* tscFilterInfoClone(const SColumnFilterInfo* src, int32_t numOfFilters) {
1139
  if (numOfFilters == 0) {
1140 1141
    assert(src == NULL);
    return NULL;
S
slguan 已提交
1142
  }
1143
  
1144 1145
  SColumnFilterInfo* pFilter = calloc(1, numOfFilters * sizeof(SColumnFilterInfo));
  
1146 1147
  memcpy(pFilter, src, sizeof(SColumnFilterInfo) * numOfFilters);
  for (int32_t j = 0; j < numOfFilters; ++j) {
1148
    
1149 1150
    if (pFilter[j].filterstr) {
      size_t len = (size_t) pFilter[j].len + 1;
1151
      pFilter[j].pz = (int64_t) calloc(1, len);
1152
      
1153
      memcpy((char*)pFilter[j].pz, (char*)src[j].pz, (size_t)len);
1154
    }
S
slguan 已提交
1155
  }
1156 1157 1158 1159 1160
  
  assert(src->filterstr == 0 || src->filterstr == 1);
  assert(!(src->lowerRelOptr == TSDB_RELATION_INVALID && src->upperRelOptr == TSDB_RELATION_INVALID));
  
  return pFilter;
S
slguan 已提交
1161 1162
}

1163 1164 1165 1166
static void destroyFilterInfo(SColumnFilterInfo* pFilterInfo, int32_t numOfFilters) {
  for(int32_t i = 0; i < numOfFilters; ++i) {
    if (pFilterInfo[i].filterstr) {
      tfree(pFilterInfo[i].pz);
S
slguan 已提交
1167 1168
    }
  }
1169 1170
  
  tfree(pFilterInfo);
S
slguan 已提交
1171 1172
}

1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
SColumn* tscColumnClone(const SColumn* src) {
  assert(src != NULL);
  
  SColumn* dst = calloc(1, sizeof(SColumn));
  
  dst->colIndex     = src->colIndex;
  dst->numOfFilters = src->numOfFilters;
  dst->filterInfo   = tscFilterInfoClone(src->filterInfo, src->numOfFilters);
  
  return dst;
}

static void tscColumnDestroy(SColumn* pCol) {
  destroyFilterInfo(pCol->filterInfo, pCol->numOfFilters);
  free(pCol);
}

H
hjxilinx 已提交
1190
void tscColumnListCopy(SArray* dst, const SArray* src, int16_t tableIndex) {
H
hjxilinx 已提交
1191
  assert(src != NULL && dst != NULL);
1192 1193 1194 1195
  
  size_t num = taosArrayGetSize(src);
  for (int32_t i = 0; i < num; ++i) {
    SColumn* pCol = taosArrayGetP(src, i);
H
hzcheng 已提交
1196

1197 1198 1199
    if (pCol->colIndex.tableIndex == tableIndex || tableIndex < 0) {
      SColumn* p = tscColumnClone(pCol);
      taosArrayPush(dst, &p);
S
slguan 已提交
1200 1201
    }
  }
H
hzcheng 已提交
1202 1203
}

1204 1205
void tscColumnListDestroy(SArray* pColumnList) {
  if (pColumnList == NULL) {
S
slguan 已提交
1206 1207 1208
    return;
  }

1209
  size_t num = taosArrayGetSize(pColumnList);
1210
  for (int32_t i = 0; i < num; ++i) {
1211
    SColumn* pCol = taosArrayGetP(pColumnList, i);
1212
    tscColumnDestroy(pCol);
S
slguan 已提交
1213 1214
  }

1215
  taosArrayDestroy(pColumnList);
1216
}
H
hzcheng 已提交
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232

/*
 * 1. normal name, not a keyword or number
 * 2. name with quote
 * 3. string with only one delimiter '.'.
 *
 * only_one_part
 * 'only_one_part'
 * first_part.second_part
 * first_part.'second_part'
 * 'first_part'.second_part
 * 'first_part'.'second_part'
 * 'first_part.second_part'
 *
 */
static int32_t validateQuoteToken(SSQLToken* pToken) {
H
huili 已提交
1233
  pToken->n = strdequote(pToken->z);
H
hzcheng 已提交
1234 1235 1236 1237
  strtrim(pToken->z);
  pToken->n = (uint32_t)strlen(pToken->z);

  int32_t k = tSQLGetToken(pToken->z, &pToken->type);
1238

H
huili 已提交
1239 1240
  if (pToken->type == TK_STRING) {
    return tscValidateName(pToken);
S
slguan 已提交
1241
  }
H
hzcheng 已提交
1242

H
huili 已提交
1243 1244 1245
  if (k != pToken->n || pToken->type != TK_ID) {
    return TSDB_CODE_INVALID_SQL;
  }
H
hzcheng 已提交
1246 1247 1248 1249 1250 1251 1252 1253
  return TSDB_CODE_SUCCESS;
}

int32_t tscValidateName(SSQLToken* pToken) {
  if (pToken->type != TK_STRING && pToken->type != TK_ID) {
    return TSDB_CODE_INVALID_SQL;
  }

S
slguan 已提交
1254
  char* sep = strnchr(pToken->z, TS_PATH_DELIMITER[0], pToken->n, true);
H
hzcheng 已提交
1255 1256
  if (sep == NULL) {  // single part
    if (pToken->type == TK_STRING) {
H
huili 已提交
1257 1258 1259
      pToken->n = strdequote(pToken->z);
      strtrim(pToken->z);
      pToken->n = (uint32_t)strlen(pToken->z);
S
slguan 已提交
1260 1261 1262 1263

      int len = tSQLGetToken(pToken->z, &pToken->type);

      // single token, validate it
1264
      if (len == pToken->n) {
H
huili 已提交
1265
        return validateQuoteToken(pToken);
S
slguan 已提交
1266
      } else {
1267 1268 1269 1270
        sep = strnchr(pToken->z, TS_PATH_DELIMITER[0], pToken->n, true);
        if (sep == NULL) {
          return TSDB_CODE_INVALID_SQL;
        }
S
slguan 已提交
1271

H
huili 已提交
1272
        return tscValidateName(pToken);
1273
      }
H
hzcheng 已提交
1274 1275 1276 1277 1278 1279 1280 1281 1282
    } else {
      if (isNumber(pToken)) {
        return TSDB_CODE_INVALID_SQL;
      }
    }
  } else {  // two part
    int32_t oldLen = pToken->n;
    char*   pStr = pToken->z;

H
huili 已提交
1283 1284
    if (pToken->type == TK_SPACE) {
      strtrim(pToken->z);
S
slguan 已提交
1285
      pToken->n = (uint32_t)strlen(pToken->z);
H
huili 已提交
1286 1287
    }

H
hzcheng 已提交
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
    pToken->n = tSQLGetToken(pToken->z, &pToken->type);
    if (pToken->z[pToken->n] != TS_PATH_DELIMITER[0]) {
      return TSDB_CODE_INVALID_SQL;
    }

    if (pToken->type != TK_STRING && pToken->type != TK_ID) {
      return TSDB_CODE_INVALID_SQL;
    }

    if (pToken->type == TK_STRING && validateQuoteToken(pToken) != TSDB_CODE_SUCCESS) {
      return TSDB_CODE_INVALID_SQL;
    }

    int32_t firstPartLen = pToken->n;

    pToken->z = sep + 1;
    pToken->n = oldLen - (sep - pStr) - 1;
    int32_t len = tSQLGetToken(pToken->z, &pToken->type);
    if (len != pToken->n || (pToken->type != TK_STRING && pToken->type != TK_ID)) {
      return TSDB_CODE_INVALID_SQL;
    }

    if (pToken->type == TK_STRING && validateQuoteToken(pToken) != TSDB_CODE_SUCCESS) {
      return TSDB_CODE_INVALID_SQL;
    }

    // re-build the whole name string
    if (pStr[firstPartLen] == TS_PATH_DELIMITER[0]) {
H
hjxilinx 已提交
1316
      // first part do not have quote do nothing
H
hzcheng 已提交
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
    } else {
      pStr[firstPartLen] = TS_PATH_DELIMITER[0];
      memmove(&pStr[firstPartLen + 1], pToken->z, pToken->n);
      pStr[firstPartLen + sizeof(TS_PATH_DELIMITER[0]) + pToken->n] = 0;
    }
    pToken->n += (firstPartLen + sizeof(TS_PATH_DELIMITER[0]));
    pToken->z = pStr;
  }

  return TSDB_CODE_SUCCESS;
}

void tscIncStreamExecutionCount(void* pStream) {
  if (pStream == NULL) {
    return;
  }

  SSqlStream* ps = (SSqlStream*)pStream;
  ps->num += 1;
}

H
hjxilinx 已提交
1338 1339
bool tscValidateColumnId(STableMetaInfo* pTableMetaInfo, int32_t colId) {
  if (pTableMetaInfo->pTableMeta == NULL) {
H
hzcheng 已提交
1340 1341 1342
    return false;
  }

1343
  if (colId == TSDB_TBNAME_COLUMN_INDEX) {
H
hzcheng 已提交
1344 1345 1346
    return true;
  }

H
hjxilinx 已提交
1347
  SSchema* pSchema = tscGetTableSchema(pTableMetaInfo->pTableMeta);
H
hjxilinx 已提交
1348
  STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta);
H
hjxilinx 已提交
1349 1350
  
  int32_t  numOfTotal = tinfo.numOfTags + tinfo.numOfColumns;
H
hzcheng 已提交
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360

  for (int32_t i = 0; i < numOfTotal; ++i) {
    if (pSchema[i].colId == colId) {
      return true;
    }
  }

  return false;
}

S
slguan 已提交
1361 1362
void tscTagCondCopy(STagCond* dest, const STagCond* src) {
  memset(dest, 0, sizeof(STagCond));
H
hjxilinx 已提交
1363

H
hjxilinx 已提交
1364 1365 1366
  if (src->tbnameCond.cond != NULL) {
    dest->tbnameCond.cond = strdup(src->tbnameCond.cond);
  }
S
slguan 已提交
1367 1368 1369 1370

  dest->tbnameCond.uid = src->tbnameCond.uid;

  memcpy(&dest->joinInfo, &src->joinInfo, sizeof(SJoinInfo));
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
  dest->relType = src->relType;
  
  if (src->pCond == NULL) {
    return;
  }
  
  size_t s = taosArrayGetSize(src->pCond);
  dest->pCond = taosArrayInit(s, sizeof(SCond));
  
  for (int32_t i = 0; i < s; ++i) {
    SCond* pCond = taosArrayGet(src->pCond, i);
    
    SCond c = {0};
    c.len = pCond->len;
    c.uid = pCond->uid;
    
    if (pCond->len > 0) {
      assert(pCond->cond != NULL);
      c.cond = malloc(c.len);
      memcpy(c.cond, pCond->cond, c.len);
H
hjxilinx 已提交
1391
    }
1392 1393
    
    taosArrayPush(dest->pCond, &c);
H
hzcheng 已提交
1394 1395 1396
  }
}

1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
void tscTagCondRelease(STagCond* pTagCond) {
  free(pTagCond->tbnameCond.cond);
  
  if (pTagCond->pCond != NULL) {
    size_t s = taosArrayGetSize(pTagCond->pCond);
    for (int32_t i = 0; i < s; ++i) {
      SCond* p = taosArrayGet(pTagCond->pCond, i);
      tfree(p->cond);
    }
  
    taosArrayDestroy(pTagCond->pCond);
H
hzcheng 已提交
1408 1409
  }

1410
  memset(pTagCond, 0, sizeof(STagCond));
H
hzcheng 已提交
1411 1412
}

1413
void tscGetSrcColumnInfo(SSrcColumnInfo* pColInfo, SQueryInfo* pQueryInfo) {
H
hjxilinx 已提交
1414
  STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0);
H
hjxilinx 已提交
1415
  SSchema*        pSchema = tscGetTableSchema(pTableMetaInfo->pTableMeta);
H
hjxilinx 已提交
1416 1417 1418
  
  size_t numOfExprs = tscSqlExprNumOfExprs(pQueryInfo);
  for (int32_t i = 0; i < numOfExprs; ++i) {
1419
    SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i);
S
slguan 已提交
1420
    pColInfo[i].functionId = pExpr->functionId;
H
hzcheng 已提交
1421

S
slguan 已提交
1422
    if (TSDB_COL_IS_TAG(pExpr->colInfo.flag)) {
H
hjxilinx 已提交
1423
      SSchema* pTagSchema = tscGetTableTagSchema(pTableMetaInfo->pTableMeta);
1424 1425 1426
      
      int16_t index = pExpr->colInfo.colIndex;
      pColInfo[i].type = (index != -1) ? pTagSchema[index].type : TSDB_DATA_TYPE_BINARY;
H
hzcheng 已提交
1427
    } else {
1428
      pColInfo[i].type = pSchema[pExpr->colInfo.colIndex].type;
H
hzcheng 已提交
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
    }
  }
}

void tscSetFreeHeatBeat(STscObj* pObj) {
  if (pObj == NULL || pObj->signature != pObj || pObj->pHb == NULL) {
    return;
  }

  SSqlObj* pHeatBeat = pObj->pHb;
  assert(pHeatBeat == pHeatBeat->signature);

S
slguan 已提交
1441
  // to denote the heart-beat timer close connection and free all allocated resources
1442 1443
  SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pHeatBeat->cmd, 0);
  pQueryInfo->type = TSDB_QUERY_TYPE_FREE_RESOURCE;
H
hzcheng 已提交
1444 1445 1446 1447
}

bool tscShouldFreeHeatBeat(SSqlObj* pHb) {
  assert(pHb == pHb->signature);
1448 1449 1450

  SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pHb->cmd, 0);
  return pQueryInfo->type == TSDB_QUERY_TYPE_FREE_RESOURCE;
H
hzcheng 已提交
1451 1452 1453
}

/*
weixin_48148422's avatar
weixin_48148422 已提交
1454
 * the following four kinds of SqlObj should not be freed
H
hzcheng 已提交
1455 1456 1457
 * 1. SqlObj for stream computing
 * 2. main SqlObj
 * 3. heartbeat SqlObj
weixin_48148422's avatar
weixin_48148422 已提交
1458
 * 4. SqlObj for subscription
H
hzcheng 已提交
1459 1460 1461 1462 1463 1464
 *
 * If res code is error and SqlObj does not belong to above types, it should be
 * automatically freed for async query, ignoring that connection should be kept.
 *
 * If connection need to be recycled, the SqlObj also should be freed.
 */
H
hjxilinx 已提交
1465
bool tscShouldBeFreed(SSqlObj* pSql) {
H
Haojun Liao 已提交
1466
  if (pSql == NULL || pSql->signature != pSql) {
H
hzcheng 已提交
1467 1468
    return false;
  }
H
Haojun Liao 已提交
1469 1470 1471
  
  assert(pSql->fp != NULL);
  
H
hzcheng 已提交
1472
  STscObj* pTscObj = pSql->pTscObj;
H
Haojun Liao 已提交
1473
  if (pSql->pStream != NULL || pTscObj->pHb == pSql || pSql->pSubscription != NULL) {
H
hzcheng 已提交
1474 1475 1476 1477
    return false;
  }

  int32_t command = pSql->cmd.command;
H
Haojun Liao 已提交
1478
  if (command == TSDB_SQL_META || command == TSDB_SQL_STABLEVGROUP) {//TODO subquery should be freed here
1479
    return true;
H
hzcheng 已提交
1480
  }
H
Haojun Liao 已提交
1481 1482 1483 1484 1485 1486
  
  // all subqueries should be automatically freed
//  if (pSql->cmd.pQueryInfo != NULL && pSql->cmd.pQueryInfo[0]->type & TSDB_QUERY_TYPE_SUBQUERY) {
//    return true;
//  }
  return false;
H
hzcheng 已提交
1487 1488
}

1489 1490 1491
/**
 *
 * @param pCmd
1492
 * @param clauseIndex denote the index of the union sub clause, usually are 0, if no union query exists.
1493 1494 1495
 * @param tableIndex  denote the table index for join query, where more than one table exists
 * @return
 */
1496
STableMetaInfo* tscGetTableMetaInfoFromCmd(SSqlCmd* pCmd, int32_t clauseIndex, int32_t tableIndex) {
1497
  if (pCmd == NULL || pCmd->numOfClause == 0) {
S
slguan 已提交
1498 1499 1500
    return NULL;
  }

1501
  assert(clauseIndex >= 0 && clauseIndex < pCmd->numOfClause);
1502

1503
  SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, clauseIndex);
H
hjxilinx 已提交
1504
  return tscGetMetaInfo(pQueryInfo, tableIndex);
S
slguan 已提交
1505 1506
}

H
hjxilinx 已提交
1507
STableMetaInfo* tscGetMetaInfo(SQueryInfo* pQueryInfo, int32_t tableIndex) {
H
hjxilinx 已提交
1508
  assert(pQueryInfo != NULL);
1509

H
hjxilinx 已提交
1510
  if (pQueryInfo->pTableMetaInfo == NULL) {
1511 1512 1513 1514
    assert(pQueryInfo->numOfTables == 0);
    return NULL;
  }

H
hjxilinx 已提交
1515
  assert(tableIndex >= 0 && tableIndex <= pQueryInfo->numOfTables && pQueryInfo->pTableMetaInfo != NULL);
1516

H
hjxilinx 已提交
1517
  return pQueryInfo->pTableMetaInfo[tableIndex];
1518 1519 1520
}

SQueryInfo* tscGetQueryInfoDetail(SSqlCmd* pCmd, int32_t subClauseIndex) {
1521
  assert(pCmd != NULL && subClauseIndex >= 0 && subClauseIndex < TSDB_MAX_UNION_CLAUSE);
1522

1523
  if (pCmd->pQueryInfo == NULL || subClauseIndex >= pCmd->numOfClause) {
1524 1525 1526 1527 1528 1529
    return NULL;
  }

  return pCmd->pQueryInfo[subClauseIndex];
}

1530
int32_t tscGetQueryInfoDetailSafely(SSqlCmd* pCmd, int32_t subClauseIndex, SQueryInfo** pQueryInfo) {
1531
  int32_t ret = TSDB_CODE_SUCCESS;
1532

1533
  *pQueryInfo = tscGetQueryInfoDetail(pCmd, subClauseIndex);
1534

1535 1536 1537 1538
  while ((*pQueryInfo) == NULL) {
    if ((ret = tscAddSubqueryInfo(pCmd)) != TSDB_CODE_SUCCESS) {
      return ret;
    }
1539

1540 1541
    (*pQueryInfo) = tscGetQueryInfoDetail(pCmd, subClauseIndex);
  }
1542

1543 1544 1545
  return TSDB_CODE_SUCCESS;
}

H
hjxilinx 已提交
1546
STableMetaInfo* tscGetTableMetaInfoByUid(SQueryInfo* pQueryInfo, uint64_t uid, int32_t* index) {
S
slguan 已提交
1547
  int32_t k = -1;
1548 1549

  for (int32_t i = 0; i < pQueryInfo->numOfTables; ++i) {
H
hjxilinx 已提交
1550
    if (pQueryInfo->pTableMetaInfo[i]->pTableMeta->uid == uid) {
S
slguan 已提交
1551 1552 1553 1554 1555 1556 1557 1558 1559
      k = i;
      break;
    }
  }

  if (index != NULL) {
    *index = k;
  }

H
hjxilinx 已提交
1560
  assert(k != -1);
H
hjxilinx 已提交
1561
  return tscGetMetaInfo(pQueryInfo, k);
S
slguan 已提交
1562 1563
}

H
hjxilinx 已提交
1564 1565 1566 1567 1568 1569 1570 1571 1572
void tscInitQueryInfo(SQueryInfo* pQueryInfo) {
  assert(pQueryInfo->fieldsInfo.pFields == NULL);
  pQueryInfo->fieldsInfo.pFields = taosArrayInit(4, sizeof(TAOS_FIELD));
  
  assert(pQueryInfo->fieldsInfo.pSupportInfo == NULL);
  pQueryInfo->fieldsInfo.pSupportInfo = taosArrayInit(4, sizeof(SFieldSupInfo));
  
  assert(pQueryInfo->exprList == NULL);
  pQueryInfo->exprList = taosArrayInit(4, POINTER_BYTES);
1573
  pQueryInfo->colList  = taosArrayInit(4, POINTER_BYTES);
H
hjxilinx 已提交
1574 1575
}

1576
int32_t tscAddSubqueryInfo(SSqlCmd* pCmd) {
1577
  assert(pCmd != NULL);
1578

H
hjxilinx 已提交
1579
  // todo refactor: remove this structure
1580 1581
  size_t s = pCmd->numOfClause + 1;
  char*  tmp = realloc(pCmd->pQueryInfo, s * POINTER_BYTES);
1582 1583 1584
  if (tmp == NULL) {
    return TSDB_CODE_CLI_OUT_OF_MEMORY;
  }
1585 1586 1587 1588

  pCmd->pQueryInfo = (SQueryInfo**)tmp;

  SQueryInfo* pQueryInfo = calloc(1, sizeof(SQueryInfo));
H
hjxilinx 已提交
1589
  tscInitQueryInfo(pQueryInfo);
H
hjxilinx 已提交
1590
  
1591 1592 1593
  pQueryInfo->msg = pCmd->payload;  // pointer to the parent error message buffer

  pCmd->pQueryInfo[pCmd->numOfClause++] = pQueryInfo;
1594 1595 1596
  return TSDB_CODE_SUCCESS;
}

1597
static void freeQueryInfoImpl(SQueryInfo* pQueryInfo) {
1598
  tscTagCondRelease(&pQueryInfo->tagCond);
H
hjxilinx 已提交
1599
  tscFieldInfoClear(&pQueryInfo->fieldsInfo);
1600

H
hjxilinx 已提交
1601 1602
  tscSqlExprInfoDestroy(pQueryInfo->exprList);
  pQueryInfo->exprList = NULL;
1603

1604
  tscColumnListDestroy(pQueryInfo->colList);
H
hjxilinx 已提交
1605
  pQueryInfo->colList = NULL;
1606

1607 1608 1609 1610 1611
  if (pQueryInfo->groupbyExpr.columnInfo != NULL) {
    taosArrayDestroy(pQueryInfo->groupbyExpr.columnInfo);
    pQueryInfo->groupbyExpr.columnInfo = NULL;
  }
  
1612
  pQueryInfo->tsBuf = tsBufDestory(pQueryInfo->tsBuf);
1613

1614
  tfree(pQueryInfo->fillVal);
1615
}
1616

1617
void tscClearSubqueryInfo(SSqlCmd* pCmd) {
1618
  for (int32_t i = 0; i < pCmd->numOfClause; ++i) {
1619
    SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, i);
1620
    freeQueryInfoImpl(pQueryInfo);
1621
  }
1622 1623
}

1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644
void doRemoveTableMetaInfo(SQueryInfo* pQueryInfo, int32_t index, bool removeFromCache) {
  if (index < 0 || index >= pQueryInfo->numOfTables) {
    return;
  }
  
  STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, index);
  
  tscClearTableMetaInfo(pTableMetaInfo, removeFromCache);
  free(pTableMetaInfo);
  
  int32_t after = pQueryInfo->numOfTables - index - 1;
  if (after > 0) {
    memmove(&pQueryInfo->pTableMetaInfo[index], &pQueryInfo->pTableMetaInfo[index + 1], after * POINTER_BYTES);
  }
  
  pQueryInfo->numOfTables -= 1;
}

void clearAllTableMetaInfo(SQueryInfo* pQueryInfo, const char* address, bool removeFromCache) {
  tscTrace("%p deref the table meta in cache, numOfTables:%d", address, pQueryInfo->numOfTables);
  
1645 1646 1647 1648 1649
  for(int32_t i = 0; i < pQueryInfo->numOfTables; ++i) {
    STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, i);
  
    tscClearTableMetaInfo(pTableMetaInfo, removeFromCache);
    free(pTableMetaInfo);
1650 1651 1652 1653 1654
  }
  
  tfree(pQueryInfo->pTableMetaInfo);
}

1655
STableMetaInfo* tscAddTableMetaInfo(SQueryInfo* pQueryInfo, const char* name, STableMeta* pTableMeta,
1656
                                    SVgroupsInfo* vgroupList, SArray* pTagCols) {
H
hjxilinx 已提交
1657
  void* pAlloc = realloc(pQueryInfo->pTableMetaInfo, (pQueryInfo->numOfTables + 1) * POINTER_BYTES);
S
slguan 已提交
1658 1659 1660 1661
  if (pAlloc == NULL) {
    return NULL;
  }

H
hjxilinx 已提交
1662 1663
  pQueryInfo->pTableMetaInfo = pAlloc;
  pQueryInfo->pTableMetaInfo[pQueryInfo->numOfTables] = calloc(1, sizeof(STableMetaInfo));
S
slguan 已提交
1664

H
hjxilinx 已提交
1665
  STableMetaInfo* pTableMetaInfo = pQueryInfo->pTableMetaInfo[pQueryInfo->numOfTables];
H
hjxilinx 已提交
1666
  assert(pTableMetaInfo != NULL);
S
slguan 已提交
1667 1668

  if (name != NULL) {
1669
    strncpy(pTableMetaInfo->name, name, TSDB_TABLE_ID_LEN);
S
slguan 已提交
1670 1671
  }

H
hjxilinx 已提交
1672
  pTableMetaInfo->pTableMeta = pTableMeta;
1673 1674
  
  if (vgroupList != NULL) {
H
hjxilinx 已提交
1675
    size_t size = sizeof(SVgroupsInfo) + sizeof(SCMVgroupInfo) * vgroupList->numOfVgroups;
1676 1677
    pTableMetaInfo->vgroupList = malloc(size);
    memcpy(pTableMetaInfo->vgroupList, vgroupList, size);
1678
  }
S
slguan 已提交
1679

1680 1681 1682
  pTableMetaInfo->tagColList = taosArrayInit(4, POINTER_BYTES);
  if (pTagCols != NULL) {
    tscColumnListCopy(pTableMetaInfo->tagColList, pTagCols, -1);
S
slguan 已提交
1683
  }
1684
  
1685
  pQueryInfo->numOfTables += 1;
H
hjxilinx 已提交
1686
  return pTableMetaInfo;
S
slguan 已提交
1687 1688
}

H
hjxilinx 已提交
1689
STableMetaInfo* tscAddEmptyMetaInfo(SQueryInfo* pQueryInfo) {
1690
  return tscAddTableMetaInfo(pQueryInfo, NULL, NULL, NULL, NULL);
1691
}
S
slguan 已提交
1692

1693
void tscClearTableMetaInfo(STableMetaInfo* pTableMetaInfo, bool removeFromCache) {
H
hjxilinx 已提交
1694
  if (pTableMetaInfo == NULL) {
S
slguan 已提交
1695 1696 1697
    return;
  }

H
hjxilinx 已提交
1698
  taosCacheRelease(tscCacheHandle, (void**)&(pTableMetaInfo->pTableMeta), removeFromCache);
1699
  tfree(pTableMetaInfo->vgroupList);
1700
  
1701 1702
  tscColumnListDestroy(pTableMetaInfo->tagColList);
  pTableMetaInfo->tagColList = NULL;
S
slguan 已提交
1703 1704 1705
}

void tscResetForNextRetrieve(SSqlRes* pRes) {
H
hjxilinx 已提交
1706 1707 1708
  if (pRes == NULL) {
    return;
  }
1709

S
slguan 已提交
1710 1711 1712 1713
  pRes->row = 0;
  pRes->numOfRows = 0;
}

1714
SSqlObj* createSubqueryObj(SSqlObj* pSql, int16_t tableIndex, void (*fp)(), void* param, int32_t cmd, SSqlObj* pPrevSql) {
1715
  SSqlCmd* pCmd = &pSql->cmd;
S
slguan 已提交
1716 1717
  SSqlObj* pNew = (SSqlObj*)calloc(1, sizeof(SSqlObj));
  if (pNew == NULL) {
1718
    tscError("%p new subquery failed, tableIndex:%d", pSql, tableIndex);
S
slguan 已提交
1719 1720
    return NULL;
  }
1721 1722
  
  STableMetaInfo* pTableMetaInfo = tscGetTableMetaInfoFromCmd(pCmd, pCmd->clauseIndex, tableIndex);
S
slguan 已提交
1723 1724 1725 1726 1727 1728

  pNew->pTscObj = pSql->pTscObj;
  pNew->signature = pNew;

  pNew->sqlstr = strdup(pSql->sqlstr);
  if (pNew->sqlstr == NULL) {
H
hjxilinx 已提交
1729
    tscError("%p new subquery failed, tableIndex:%d, vgroupIndex:%d", pSql, tableIndex, pTableMetaInfo->vgroupIndex);
S
slguan 已提交
1730 1731 1732 1733 1734

    free(pNew);
    return NULL;
  }

1735 1736 1737 1738 1739 1740
  SSqlCmd* pnCmd = &pNew->cmd;
  memcpy(pnCmd, pCmd, sizeof(SSqlCmd));
  
  pnCmd->command = cmd;
  pnCmd->payload = NULL;
  pnCmd->allocSize = 0;
S
slguan 已提交
1741

1742 1743 1744 1745
  pnCmd->pQueryInfo = NULL;
  pnCmd->numOfClause = 0;
  pnCmd->clauseIndex = 0;
  pnCmd->pDataBlocks = NULL;
1746

1747
  if (tscAddSubqueryInfo(pnCmd) != TSDB_CODE_SUCCESS) {
1748 1749 1750
    tscFreeSqlObj(pNew);
    return NULL;
  }
1751

1752
  SQueryInfo* pNewQueryInfo = tscGetQueryInfoDetail(pnCmd, 0);
1753
  SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, pCmd->clauseIndex);
1754

H
hjxilinx 已提交
1755 1756 1757
  pNewQueryInfo->command = pQueryInfo->command;
  pNewQueryInfo->slidingTimeUnit = pQueryInfo->slidingTimeUnit;
  pNewQueryInfo->intervalTime = pQueryInfo->intervalTime;
H
hjxilinx 已提交
1758 1759 1760 1761
  pNewQueryInfo->slidingTime  = pQueryInfo->slidingTime;
  pNewQueryInfo->type   = pQueryInfo->type;
  pNewQueryInfo->window = pQueryInfo->window;
  pNewQueryInfo->limit  = pQueryInfo->limit;
H
hjxilinx 已提交
1762
  pNewQueryInfo->slimit = pQueryInfo->slimit;
H
hjxilinx 已提交
1763
  pNewQueryInfo->order  = pQueryInfo->order;
H
Haojun Liao 已提交
1764 1765
  pNewQueryInfo->tsBuf  = NULL;
  pNewQueryInfo->fillType = pQueryInfo->fillType;
1766
  pNewQueryInfo->fillVal  = NULL;
H
Haojun Liao 已提交
1767
  pNewQueryInfo->clauseLimit = pQueryInfo->clauseLimit;
1768
  pNewQueryInfo->numOfTables = 0;
H
Haojun Liao 已提交
1769
  pNewQueryInfo->pTableMetaInfo = NULL;
H
hjxilinx 已提交
1770
  
H
hjxilinx 已提交
1771 1772 1773 1774 1775
  pNewQueryInfo->groupbyExpr = pQueryInfo->groupbyExpr;
  if (pQueryInfo->groupbyExpr.columnInfo != NULL) {
    pNewQueryInfo->groupbyExpr.columnInfo = taosArrayClone(pQueryInfo->groupbyExpr.columnInfo);
  }
  
1776
  tscTagCondCopy(&pNewQueryInfo->tagCond, &pQueryInfo->tagCond);
1777

1778
  if (pQueryInfo->fillType != TSDB_FILL_NONE) {
1779 1780
    pNewQueryInfo->fillVal = malloc(pQueryInfo->fieldsInfo.numOfOutput * sizeof(int64_t));
    memcpy(pNewQueryInfo->fillVal, pQueryInfo->fillVal, pQueryInfo->fieldsInfo.numOfOutput * sizeof(int64_t));
1781
  }
1782

1783
  if (tscAllocPayload(pnCmd, TSDB_DEFAULT_PAYLOAD_SIZE) != TSDB_CODE_SUCCESS) {
H
hjxilinx 已提交
1784
    tscError("%p new subquery failed, tableIndex:%d, vgroupIndex:%d", pSql, tableIndex, pTableMetaInfo->vgroupIndex);
S
slguan 已提交
1785 1786 1787
    tscFreeSqlObj(pNew);
    return NULL;
  }
1788
  
H
hjxilinx 已提交
1789
  tscColumnListCopy(pNewQueryInfo->colList, pQueryInfo->colList, (int16_t)tableIndex);
1790

S
slguan 已提交
1791 1792
  // set the correct query type
  if (pPrevSql != NULL) {
1793
    SQueryInfo* pPrevQueryInfo = tscGetQueryInfoDetail(&pPrevSql->cmd, pPrevSql->cmd.clauseIndex);
1794
    pNewQueryInfo->type = pPrevQueryInfo->type;
S
slguan 已提交
1795
  } else {
1796
    pNewQueryInfo->type |= TSDB_QUERY_TYPE_SUBQUERY;  // it must be the subquery
S
slguan 已提交
1797 1798
  }

H
hjxilinx 已提交
1799
  uint64_t uid = pTableMetaInfo->pTableMeta->uid;
H
hjxilinx 已提交
1800
  tscSqlExprCopy(pNewQueryInfo->exprList, pQueryInfo->exprList, uid, true);
S
slguan 已提交
1801

H
hjxilinx 已提交
1802
  int32_t numOfOutput = tscSqlExprNumOfExprs(pNewQueryInfo);
S
slguan 已提交
1803

H
hjxilinx 已提交
1804
  if (numOfOutput > 0) {  // todo refactor to extract method
H
hjxilinx 已提交
1805
    size_t numOfExprs = tscSqlExprNumOfExprs(pQueryInfo);
H
hjxilinx 已提交
1806 1807 1808
    SFieldInfo* pFieldInfo = &pQueryInfo->fieldsInfo;
    
    for (int32_t i = 0; i < numOfExprs; ++i) {
1809
      SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i);
H
hjxilinx 已提交
1810
      
S
slguan 已提交
1811
      if (pExpr->uid == uid) {
H
hjxilinx 已提交
1812 1813 1814 1815 1816
        TAOS_FIELD* p = tscFieldInfoGetField(pFieldInfo, i);
        SFieldSupInfo* pInfo = tscFieldInfoGetSupp(pFieldInfo, i);
  
        SFieldSupInfo* pInfo1 = tscFieldInfoAppend(&pNewQueryInfo->fieldsInfo, p);
        *pInfo1 = *pInfo;
S
slguan 已提交
1817 1818 1819
      }
    }

H
hjxilinx 已提交
1820
    // make sure the the sqlExpr for each fields is correct
H
hjxilinx 已提交
1821
// todo handle the agg arithmetic expression
H
hjxilinx 已提交
1822 1823
    for(int32_t f = 0; f < pNewQueryInfo->fieldsInfo.numOfOutput; ++f) {
      TAOS_FIELD* field = tscFieldInfoGetField(&pNewQueryInfo->fieldsInfo, f);
H
hjxilinx 已提交
1824 1825
      numOfExprs = tscSqlExprNumOfExprs(pNewQueryInfo);
      
H
hjxilinx 已提交
1826
      for(int32_t k1 = 0; k1 < numOfExprs; ++k1) {
H
hjxilinx 已提交
1827
        SSqlExpr* pExpr1 = tscSqlExprGet(pNewQueryInfo, k1);
H
hjxilinx 已提交
1828 1829
        
        if (strcmp(field->name, pExpr1->aliasName) == 0) {  // eatablish link according to the result field name
H
hjxilinx 已提交
1830 1831
          SFieldSupInfo* pInfo = tscFieldInfoGetSupp(&pNewQueryInfo->fieldsInfo, f);
          pInfo->pSqlExpr = pExpr1;
H
hjxilinx 已提交
1832 1833 1834
        }
      }
    }
H
hjxilinx 已提交
1835 1836
  
    tscFieldInfoUpdateOffset(pNewQueryInfo);
S
slguan 已提交
1837 1838 1839 1840
  }

  pNew->fp = fp;
  pNew->param = param;
1841
  pNew->maxRetry = TSDB_MAX_REPLICA_NUM;
H
hjxilinx 已提交
1842

1843
  char* name = pTableMetaInfo->name;
H
hjxilinx 已提交
1844
  STableMetaInfo* pFinalInfo = NULL;
S
slguan 已提交
1845 1846

  if (pPrevSql == NULL) {
1847
    STableMeta* pTableMeta = taosCacheAcquireByData(tscCacheHandle, pTableMetaInfo->pTableMeta);  // get by name may failed due to the cache cleanup
1848
    assert(pTableMeta != NULL);
1849
    pFinalInfo = tscAddTableMetaInfo(pNewQueryInfo, name, pTableMeta, pTableMetaInfo->vgroupList, pTableMetaInfo->tagColList);
1850 1851
  } else {  // transfer the ownership of pTableMeta to the newly create sql object.
    STableMetaInfo* pPrevInfo = tscGetTableMetaInfoFromCmd(&pPrevSql->cmd, pPrevSql->cmd.clauseIndex, 0);
1852

1853
    STableMeta*  pPrevTableMeta = taosCacheTransfer(tscCacheHandle, (void**)&pPrevInfo->pTableMeta);
1854
    
1855 1856
    SVgroupsInfo* pVgroupsInfo = pPrevInfo->vgroupList;
    pPrevInfo->vgroupList = NULL;
1857
    pFinalInfo = tscAddTableMetaInfo(pNewQueryInfo, name, pPrevTableMeta, pVgroupsInfo, pTableMetaInfo->tagColList);
S
slguan 已提交
1858 1859
  }

L
lihui 已提交
1860
  if (pFinalInfo->pTableMeta == NULL) {
H
Haojun Liao 已提交
1861
    tscError("%p new subquery failed for get tableMeta is NULL from cache", pSql);
L
lihui 已提交
1862 1863 1864 1865 1866 1867
    tscFreeSqlObj(pNew);
    return NULL;
  }
  
  assert(pNewQueryInfo->numOfTables == 1);
  
weixin_48148422's avatar
weixin_48148422 已提交
1868
  if (UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo)) {
1869
    assert(pFinalInfo->vgroupList != NULL);
S
slguan 已提交
1870
  }
H
hjxilinx 已提交
1871
  
1872
  if (cmd == TSDB_SQL_SELECT) {
1873 1874
    size_t size = taosArrayGetSize(pNewQueryInfo->colList);
    
1875
    tscTrace(
H
hjxilinx 已提交
1876
        "%p new subquery:%p, tableIndex:%d, vgroupIndex:%d, type:%d, exprInfo:%d, colList:%d,"
1877
        "fieldInfo:%d, name:%s, qrang:%" PRId64 " - %" PRId64 " order:%d, limit:%" PRId64,
H
hjxilinx 已提交
1878
        pSql, pNew, tableIndex, pTableMetaInfo->vgroupIndex, pNewQueryInfo->type, tscSqlExprNumOfExprs(pNewQueryInfo),
H
hjxilinx 已提交
1879 1880
        size, pNewQueryInfo->fieldsInfo.numOfOutput, pFinalInfo->name, pNewQueryInfo->window.skey,
        pNewQueryInfo->window.ekey, pNewQueryInfo->order.order, pNewQueryInfo->limit.limit);
1881 1882 1883
    
    tscPrintSelectClause(pNew, 0);
  } else {
H
hjxilinx 已提交
1884
    tscTrace("%p new sub insertion: %p, vnodeIdx:%d", pSql, pNew, pTableMetaInfo->vgroupIndex);
1885
  }
1886

S
slguan 已提交
1887 1888 1889
  return pNew;
}

H
hjxilinx 已提交
1890 1891 1892 1893 1894
/**
 * To decide if current is a two-stage super table query, join query, or insert. And invoke different
 * procedure accordingly
 * @param pSql
 */
H
hzcheng 已提交
1895 1896
void tscDoQuery(SSqlObj* pSql) {
  SSqlCmd* pCmd = &pSql->cmd;
H
hjxilinx 已提交
1897
  SSqlRes* pRes = &pSql->res;
H
hjxilinx 已提交
1898
  
H
hjxilinx 已提交
1899
  pRes->code = TSDB_CODE_SUCCESS;
H
hjxilinx 已提交
1900
  
H
hzcheng 已提交
1901 1902
  if (pCmd->command > TSDB_SQL_LOCAL) {
    tscProcessLocalCmd(pSql);
H
hjxilinx 已提交
1903 1904 1905 1906 1907 1908 1909 1910 1911
    return;
  }
  
  if (pCmd->command == TSDB_SQL_SELECT) {
    tscAddIntoSqlList(pSql);
  }

  if (pCmd->dataSourceType == DATA_FROM_DATA_FILE) {
    tscProcessMultiVnodesInsertFromFile(pSql);
H
hzcheng 已提交
1912
  } else {
H
hjxilinx 已提交
1913 1914 1915 1916 1917 1918
    SQueryInfo *pQueryInfo = tscGetQueryInfoDetail(pCmd, pCmd->clauseIndex);
    uint16_t type = pQueryInfo->type;
  
    if (pSql->fp == (void(*)())tscHandleMultivnodeInsert) {  // multi-vnodes insertion
      tscHandleMultivnodeInsert(pSql);
      return;
H
hzcheng 已提交
1919
    }
H
hjxilinx 已提交
1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934
  
    if (QUERY_IS_JOIN_QUERY(type)) {
      if ((pQueryInfo->type & TSDB_QUERY_TYPE_SUBQUERY) == 0) {
        tscHandleMasterJoinQuery(pSql);
        return;
      } else {
        // for first stage sub query, iterate all vnodes to get all timestamp
        if ((pQueryInfo->type & TSDB_QUERY_TYPE_JOIN_SEC_STAGE) != TSDB_QUERY_TYPE_JOIN_SEC_STAGE) {
//          doProcessSql(pSql);
          assert(0);
        }
      }
    } else if (tscIsTwoStageSTableQuery(pQueryInfo, 0)) {  // super table query
      tscHandleMasterSTableQuery(pSql);
      return;
S
slguan 已提交
1935
    }
H
hjxilinx 已提交
1936 1937
    
    tscProcessSql(pSql);
H
hzcheng 已提交
1938 1939
  }
}
S
slguan 已提交
1940

H
hjxilinx 已提交
1941
int16_t tscGetJoinTagColIndexByUid(STagCond* pTagCond, uint64_t uid) {
S
slguan 已提交
1942 1943 1944 1945 1946 1947
  if (pTagCond->joinInfo.left.uid == uid) {
    return pTagCond->joinInfo.left.tagCol;
  } else {
    return pTagCond->joinInfo.right.tagCol;
  }
}
1948

H
Haojun Liao 已提交
1949 1950
bool tscIsUpdateQuery(SSqlObj* pSql) {
  if (pSql == NULL || pSql->signature != pSql) {
H
hjxilinx 已提交
1951
    terrno = TSDB_CODE_DISCONNECTED;
1952 1953 1954
    return TSDB_CODE_DISCONNECTED;
  }

H
Haojun Liao 已提交
1955 1956
  SSqlCmd* pCmd = &pSql->cmd;
  return ((pCmd->command >= TSDB_SQL_INSERT && pCmd->command <= TSDB_SQL_DROP_DNODE) || TSDB_SQL_USE_DB == pCmd->command);
H
hjxilinx 已提交
1957
}
1958

H
hjxilinx 已提交
1959 1960 1961 1962 1963
int32_t tscInvalidSQLErrMsg(char* msg, const char* additionalInfo, const char* sql) {
  const char* msgFormat1 = "invalid SQL: %s";
  const char* msgFormat2 = "invalid SQL: syntax error near \"%s\" (%s)";
  const char* msgFormat3 = "invalid SQL: syntax error near \"%s\"";

H
hjxilinx 已提交
1964
  const int32_t BACKWARD_CHAR_STEP = 0;
H
hjxilinx 已提交
1965

H
hjxilinx 已提交
1966 1967 1968 1969 1970
  if (sql == NULL) {
    assert(additionalInfo != NULL);
    sprintf(msg, msgFormat1, additionalInfo);
    return TSDB_CODE_INVALID_SQL;
  }
H
hjxilinx 已提交
1971 1972

  char buf[64] = {0};  // only extract part of sql string
H
hjxilinx 已提交
1973
  strncpy(buf, (sql - BACKWARD_CHAR_STEP), tListLen(buf) - 1);
H
hjxilinx 已提交
1974

H
hjxilinx 已提交
1975 1976 1977
  if (additionalInfo != NULL) {
    sprintf(msg, msgFormat2, buf, additionalInfo);
  } else {
H
hjxilinx 已提交
1978
    sprintf(msg, msgFormat3, buf);  // no additional information for invalid sql error
H
hjxilinx 已提交
1979
  }
H
hjxilinx 已提交
1980

H
hjxilinx 已提交
1981
  return TSDB_CODE_INVALID_SQL;
1982
}
H
hjxilinx 已提交
1983

H
hjxilinx 已提交
1984 1985
bool tscHasReachLimitation(SQueryInfo* pQueryInfo, SSqlRes* pRes) {
  assert(pQueryInfo != NULL && pQueryInfo->clauseLimit != 0);
H
Haojun Liao 已提交
1986
  return (pQueryInfo->clauseLimit > 0 && pRes->numOfClauseTotal >= pQueryInfo->clauseLimit);
H
hjxilinx 已提交
1987
}
1988 1989

char* tscGetErrorMsgPayload(SSqlCmd* pCmd) { return pCmd->payload; }
1990 1991 1992

/**
 *  If current vnode query does not return results anymore (pRes->numOfRows == 0), try the next vnode if exists,
1993
 *  while multi-vnode super table projection query and the result does not reach the limitation.
1994
 */
1995
bool hasMoreVnodesToTry(SSqlObj* pSql) {
1996 1997 1998 1999 2000
  SSqlCmd* pCmd = &pSql->cmd;
  SSqlRes* pRes = &pSql->res;
  if (pCmd->command != TSDB_SQL_FETCH) {
    return false;
  }
2001

2002
  SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, pCmd->clauseIndex);
2003
  
2004 2005 2006
  STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0);
  assert(pRes->completed);
  
2007
  // for normal table, no need to try any more if results are all retrieved from one vnode
H
hjLiao 已提交
2008
  if (!UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo) || (pTableMetaInfo->vgroupList == NULL)) {
2009
    return false;
2010
  }
2011
  
2012 2013 2014
  int32_t numOfVgroups = pTableMetaInfo->vgroupList->numOfVgroups;
  return tscNonOrderedProjectionQueryOnSTable(pQueryInfo, 0) &&
         (!tscHasReachLimitation(pQueryInfo, pRes)) && (pTableMetaInfo->vgroupIndex < numOfVgroups - 1);
2015 2016
}

2017 2018 2019 2020 2021 2022
void tscTryQueryNextVnode(SSqlObj* pSql, __async_cb_func_t fp) {
  SSqlCmd* pCmd = &pSql->cmd;
  SSqlRes* pRes = &pSql->res;

  SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, pCmd->clauseIndex);

2023 2024 2025 2026
  /*
   * no result returned from the current virtual node anymore, try the next vnode if exists
   * if case of: multi-vnode super table projection query
   */
2027
  assert(pRes->numOfRows == 0 && tscNonOrderedProjectionQueryOnSTable(pQueryInfo, 0) && !tscHasReachLimitation(pQueryInfo, pRes));
2028

H
hjxilinx 已提交
2029
  STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0);
2030 2031 2032
  
  int32_t totalVgroups = pTableMetaInfo->vgroupList->numOfVgroups;
  while (++pTableMetaInfo->vgroupIndex < totalVgroups) {
2033
    tscTrace("%p results from vgroup index:%d completed, try next:%d. total vgroups:%d. current numOfRes:%d", pSql,
H
Haojun Liao 已提交
2034
             pTableMetaInfo->vgroupIndex - 1, pTableMetaInfo->vgroupIndex, totalVgroups, pRes->numOfClauseTotal);
2035

2036 2037 2038 2039 2040 2041
    /*
     * update the limit and offset value for the query on the next vnode,
     * according to current retrieval results
     *
     * NOTE:
     * if the pRes->offset is larger than 0, the start returned position has not reached yet.
H
Haojun Liao 已提交
2042
     * Therefore, the pRes->numOfRows, as well as pRes->numOfClauseTotal, must be 0.
2043 2044 2045
     * The pRes->offset value will be updated by virtual node, during query execution.
     */
    if (pQueryInfo->clauseLimit >= 0) {
H
Haojun Liao 已提交
2046
      pQueryInfo->limit.limit = pQueryInfo->clauseLimit - pRes->numOfClauseTotal;
2047
    }
2048

2049 2050
    pQueryInfo->limit.offset = pRes->offset;
    assert((pRes->offset >= 0 && pRes->numOfRows == 0) || (pRes->offset == 0 && pRes->numOfRows >= 0));
2051 2052 2053
    
    tscTrace("%p new query to next vgroup, index:%d, limit:%" PRId64 ", offset:%" PRId64 ", glimit:%" PRId64,
        pSql, pTableMetaInfo->vgroupIndex, pQueryInfo->limit.limit, pQueryInfo->limit.offset, pQueryInfo->clauseLimit);
2054

2055 2056 2057 2058 2059 2060 2061 2062
    /*
     * For project query with super table join, the numOfSub is equalled to the number of all subqueries.
     * Therefore, we need to reset the value of numOfSubs to be 0.
     *
     * For super table join with projection query, if anyone of the subquery is exhausted, the query completed.
     */
    pSql->numOfSubs = 0;
    pCmd->command = TSDB_SQL_SELECT;
2063

2064
    tscResetForNextRetrieve(pRes);
2065

2066
    // set the callback function
2067
    pSql->fp = fp;
2068 2069
    int32_t ret = tscProcessSql(pSql);
    if (ret == TSDB_CODE_SUCCESS) {
2070
      return;
2071
    } else {// todo check for failure
2072 2073 2074
    }
  }
}
2075 2076 2077 2078 2079 2080 2081 2082

void tscTryQueryNextClause(SSqlObj* pSql, void (*queryFp)()) {
  SSqlCmd* pCmd = &pSql->cmd;
  SSqlRes* pRes = &pSql->res;

  // current subclause is completed, try the next subclause
  assert(pCmd->clauseIndex < pCmd->numOfClause - 1);

H
hjxilinx 已提交
2083
  pCmd->clauseIndex++;
2084 2085 2086 2087
  SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, pCmd->clauseIndex);

  pSql->cmd.command = pQueryInfo->command;

2088
  //backup the total number of result first
H
Haojun Liao 已提交
2089
  int64_t num = pRes->numOfTotal + pRes->numOfClauseTotal;
H
hjxilinx 已提交
2090
  tscFreeSqlResult(pSql);
2091 2092 2093
  
  pRes->numOfTotal = num;
  
2094
  tfree(pSql->pSubs);
2095 2096
  pSql->numOfSubs = 0;
  
2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108
  if (pSql->fp != NULL) {
    pSql->fp = queryFp;
    assert(queryFp != NULL);
  }

  tscTrace("%p try data in the next subclause:%d, total subclause:%d", pSql, pCmd->clauseIndex, pCmd->numOfClause);
  if (pCmd->command > TSDB_SQL_LOCAL) {
    tscProcessLocalCmd(pSql);
  } else {
    tscProcessSql(pSql);
  }
}
H
hjxilinx 已提交
2109

2110 2111 2112 2113
void tscGetResultColumnChr(SSqlRes* pRes, SFieldInfo* pFieldInfo, int32_t columnIndex) {
  SFieldSupInfo* pInfo = tscFieldInfoGetSupp(pFieldInfo, columnIndex);
  assert(pInfo->pSqlExpr != NULL);

H
hjxilinx 已提交
2114
  int32_t type = pInfo->pSqlExpr->resType;
2115 2116
  int32_t bytes = pInfo->pSqlExpr->resBytes;
  
2117
  char* pData = pRes->data + pInfo->pSqlExpr->offset * pRes->numOfRows + bytes * pRes->row;
H
hjxilinx 已提交
2118
  
H
hjxilinx 已提交
2119
  if (type == TSDB_DATA_TYPE_NCHAR || type == TSDB_DATA_TYPE_BINARY) {
2120
    int32_t realLen = varDataLen(pData);
H
hjxilinx 已提交
2121 2122
    assert(realLen <= bytes - VARSTR_HEADER_SIZE);
    
H
Haojun Liao 已提交
2123 2124 2125 2126 2127 2128
    if (isNull(pData, type)) {
      pRes->tsrow[columnIndex] = NULL;
    } else {
      pRes->tsrow[columnIndex] = pData + VARSTR_HEADER_SIZE;
    }
  
2129
    if (realLen < pInfo->pSqlExpr->resBytes - VARSTR_HEADER_SIZE) { // todo refactor
2130
      *(pData + realLen + VARSTR_HEADER_SIZE) = 0;
H
hjxilinx 已提交
2131 2132
    }
    
2133
    pRes->length[columnIndex] = realLen;
H
hjxilinx 已提交
2134
  } else {
2135 2136
    assert(bytes == tDataTypeDesc[type].nSize);
    
H
Haojun Liao 已提交
2137 2138 2139 2140 2141 2142
    if (isNull(pData, type)) {
      pRes->tsrow[columnIndex] = NULL;
    } else {
      pRes->tsrow[columnIndex] = pData;
    }
    
2143
    pRes->length[columnIndex] = bytes;
H
hjxilinx 已提交
2144
  }
H
hjxilinx 已提交
2145 2146
}

weixin_48148422's avatar
weixin_48148422 已提交
2147 2148 2149 2150 2151 2152 2153 2154 2155
void* malloc_throw(size_t size) {
  void* p = malloc(size);
  if (p == NULL) {
    THROW(TSDB_CODE_CLI_OUT_OF_MEMORY);
  }
  return p;
}

void* calloc_throw(size_t nmemb, size_t size) {
weixin_48148422's avatar
weixin_48148422 已提交
2156
  void* p = calloc(nmemb, size);
weixin_48148422's avatar
weixin_48148422 已提交
2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169
  if (p == NULL) {
    THROW(TSDB_CODE_CLI_OUT_OF_MEMORY);
  }
  return p;
}

char* strdup_throw(const char* str) {
  char* p = strdup(str);
  if (p == NULL) {
    THROW(TSDB_CODE_CLI_OUT_OF_MEMORY);
  }
  return p;
}
2170 2171 2172 2173 2174 2175

int tscSetMgmtIpListFromCfg(const char *first, const char *second) {
  tscMgmtIpSet.numOfIps = 0;
  tscMgmtIpSet.inUse = 0;

  if (first && first[0] != 0) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
2176
    if (strlen(first) >= TSDB_EP_LEN) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
2177 2178 2179
      terrno = TSDB_CODE_INVALID_FQDN;
      return -1;
    }
2180 2181 2182 2183 2184
    taosGetFqdnPortFromEp(first, tscMgmtIpSet.fqdn[tscMgmtIpSet.numOfIps], &tscMgmtIpSet.port[tscMgmtIpSet.numOfIps]);
    tscMgmtIpSet.numOfIps++;
  }

  if (second && second[0] != 0) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
2185
    if (strlen(second) >= TSDB_EP_LEN) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
2186 2187 2188
      terrno = TSDB_CODE_INVALID_FQDN;
      return -1;
    }
2189 2190 2191 2192
    taosGetFqdnPortFromEp(second, tscMgmtIpSet.fqdn[tscMgmtIpSet.numOfIps], &tscMgmtIpSet.port[tscMgmtIpSet.numOfIps]);
    tscMgmtIpSet.numOfIps++;
  }

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
2193 2194 2195 2196
  if ( tscMgmtIpSet.numOfIps == 0) {
    terrno = TSDB_CODE_INVALID_FQDN;
    return -1;
  }
2197 2198 2199

  return 0;
}