tscAsync.c 18.0 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

16
#include "os.h"
H
hjxilinx 已提交
17 18 19
#include "tutil.h"

#include "tnote.h"
H
hzcheng 已提交
20
#include "trpc.h"
S
slguan 已提交
21
#include "tscLog.h"
H
hjxilinx 已提交
22
#include "tscSubquery.h"
23
#include "tscLocalMerge.h"
H
hzcheng 已提交
24
#include "tscUtil.h"
S
slguan 已提交
25
#include "tsched.h"
H
hjxilinx 已提交
26
#include "tschemautil.h"
H
hjxilinx 已提交
27
#include "tsclient.h"
H
hzcheng 已提交
28

29 30
static void tscProcessFetchRow(SSchedMsg *pMsg);
static void tscAsyncQueryRowsForNextVnode(void *param, TAOS_RES *tres, int numOfRows);
H
hzcheng 已提交
31 32 33 34

static void tscProcessAsyncRetrieveImpl(void *param, TAOS_RES *tres, int numOfRows, void (*fp)());

/*
35 36 37
 * Proxy function to perform sequentially query&retrieve operation.
 * If sql queries upon a super table and two-stage merge procedure is not involved (when employ the projection
 * query), it will sequentially query&retrieve data for all vnodes
H
hzcheng 已提交
38
 */
39 40
static void tscAsyncFetchRowsProxy(void *param, TAOS_RES *tres, int numOfRows);
static void tscAsyncFetchSingleRowProxy(void *param, TAOS_RES *tres, int numOfRows);
H
hzcheng 已提交
41

B
Bomin Zhang 已提交
42
void doAsyncQuery(STscObj* pObj, SSqlObj* pSql, void (*fp)(), void* param, const char* sqlstr, size_t sqlLen) {
H
hzcheng 已提交
43
  pSql->signature = pSql;
H
hjxilinx 已提交
44 45
  pSql->param     = param;
  pSql->pTscObj   = pObj;
46
  pSql->maxRetry  = TSDB_MAX_REPLICA;
47
  pSql->fp        = fp;
H
Haojun Liao 已提交
48
  pSql->fetchFp   = fp;
H
Haojun Liao 已提交
49

H
Haojun Liao 已提交
50
  pSql->sqlstr = calloc(1, sqlLen + 1);
H
hzcheng 已提交
51 52
  if (pSql->sqlstr == NULL) {
    tscError("%p failed to malloc sql string buffer", pSql);
53
    tscQueueAsyncError(pSql->fp, pSql->param, TSDB_CODE_TSC_OUT_OF_MEMORY);
H
hzcheng 已提交
54 55
    return;
  }
H
Haojun Liao 已提交
56

S
slguan 已提交
57
  strtolower(pSql->sqlstr, sqlstr);
H
Haojun Liao 已提交
58

S
Shengliang Guan 已提交
59
  tscDebugL("%p SQL: %s", pSql, pSql->sqlstr);
H
Haojun Liao 已提交
60 61 62
  pSql->cmd.curSql = pSql->sqlstr;

  int32_t code = tsParseSql(pSql, true);
63
  if (code == TSDB_CODE_TSC_ACTION_IN_PROGRESS) return;
64
  
H
hzcheng 已提交
65
  if (code != TSDB_CODE_SUCCESS) {
H
[td-99]  
hjxilinx 已提交
66
    pSql->res.code = code;
H
hzcheng 已提交
67 68 69
    tscQueueAsyncRes(pSql);
    return;
  }
70
  
H
hzcheng 已提交
71 72 73
  tscDoQuery(pSql);
}

74
// TODO return the correct error code to client in tscQueueAsyncError
H
hjxilinx 已提交
75
void taos_query_a(TAOS *taos, const char *sqlstr, __async_cb_func_t fp, void *param) {
76 77 78
  STscObj *pObj = (STscObj *)taos;
  if (pObj == NULL || pObj->signature != pObj) {
    tscError("bug!!! pObj:%p", pObj);
79 80
    terrno = TSDB_CODE_TSC_DISCONNECTED;
    tscQueueAsyncError(fp, param, TSDB_CODE_TSC_DISCONNECTED);
81 82 83 84 85
    return;
  }
  
  int32_t sqlLen = strlen(sqlstr);
  if (sqlLen > tsMaxSQLStringLen) {
H
Haojun Liao 已提交
86
    tscError("sql string exceeds max length:%d", tsMaxSQLStringLen);
87 88
    terrno = TSDB_CODE_TSC_INVALID_SQL;
    tscQueueAsyncError(fp, param, TSDB_CODE_TSC_INVALID_SQL);
89 90 91 92 93 94 95 96
    return;
  }
  
  taosNotePrintTsc(sqlstr);
  
  SSqlObj *pSql = (SSqlObj *)calloc(1, sizeof(SSqlObj));
  if (pSql == NULL) {
    tscError("failed to malloc sqlObj");
97 98
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    tscQueueAsyncError(fp, param, TSDB_CODE_TSC_OUT_OF_MEMORY);
99 100 101 102 103 104
    return;
  }
  
  doAsyncQuery(pObj, pSql, fp, param, sqlstr, sqlLen);
}

105
static void tscAsyncFetchRowsProxy(void *param, TAOS_RES *tres, int numOfRows) {
H
hzcheng 已提交
106 107 108 109 110 111 112 113
  if (tres == NULL) {
    return;
  }

  SSqlObj *pSql = (SSqlObj *)tres;
  SSqlRes *pRes = &pSql->res;
  SSqlCmd *pCmd = &pSql->cmd;

114 115 116 117
  if (numOfRows == 0) {
    if (hasMoreVnodesToTry(pSql)) { // sequentially retrieve data from remain vnodes.
      tscTryQueryNextVnode(pSql, tscAsyncQueryRowsForNextVnode);
    } else {
118 119 120 121 122 123 124 125 126
      /*
       * all available virtual node has been checked already, now we need to check
       * for the next subclause queries
       */
      if (pCmd->clauseIndex < pCmd->numOfClause - 1) {
        tscTryQueryNextClause(pSql, tscAsyncQueryRowsForNextVnode);
        return;
      }

127 128 129 130 131
      /*
       * 1. has reach the limitation
       * 2. no remain virtual nodes to be retrieved anymore
       */
      (*pSql->fetchFp)(param, pSql, 0);
H
hzcheng 已提交
132
    }
133 134 135 136
    
    return;
  }
  
137
  // local merge has handle this situation during super table non-projection query.
H
hjxilinx 已提交
138
  if (pCmd->command != TSDB_SQL_RETRIEVE_LOCALMERGE) {
H
Haojun Liao 已提交
139
    pRes->numOfClauseTotal += pRes->numOfRows;
H
hzcheng 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
  }

  (*pSql->fetchFp)(param, tres, numOfRows);
}

// actual continue retrieve function with user-specified callback function
static void tscProcessAsyncRetrieveImpl(void *param, TAOS_RES *tres, int numOfRows, void (*fp)()) {
  SSqlObj *pSql = (SSqlObj *)tres;
  if (pSql == NULL) {  // error
    tscError("sql object is NULL");
    return;
  }

  SSqlCmd *pCmd = &pSql->cmd;
  SSqlRes *pRes = &pSql->res;

156
  if ((pRes->qhandle == 0 || numOfRows != 0) && pCmd->command < TSDB_SQL_LOCAL) {
157
    if (pRes->qhandle == 0 && numOfRows != 0) {
H
hzcheng 已提交
158 159 160 161 162
      tscError("qhandle is NULL");
    } else {
      pRes->code = numOfRows;
    }

H
Haojun Liao 已提交
163
    tscQueueAsyncRes(pSql);
H
hzcheng 已提交
164 165 166 167
    return;
  }

  pSql->fp = fp;
H
hjxilinx 已提交
168
  if (pCmd->command != TSDB_SQL_RETRIEVE_LOCALMERGE && pCmd->command < TSDB_SQL_LOCAL) {
H
hzcheng 已提交
169 170
    pCmd->command = (pCmd->command > TSDB_SQL_MGMT) ? TSDB_SQL_RETRIEVE : TSDB_SQL_FETCH;
  }
H
Haojun Liao 已提交
171

H
hzcheng 已提交
172 173 174 175
  tscProcessSql(pSql);
}

/*
176 177 178
 * retrieve callback for fetch rows proxy.
 * The below two functions both serve as the callback function of query virtual node.
 * query callback first, and then followed by retrieve callback
H
hzcheng 已提交
179
 */
180 181
static void tscAsyncQueryRowsForNextVnode(void *param, TAOS_RES *tres, int numOfRows) {
  // query completed, continue to retrieve
182
  tscProcessAsyncRetrieveImpl(param, tres, numOfRows, tscAsyncFetchRowsProxy);
H
hzcheng 已提交
183 184
}

185 186
void tscAsyncQuerySingleRowForNextVnode(void *param, TAOS_RES *tres, int numOfRows) {
  // query completed, continue to retrieve
187
  tscProcessAsyncRetrieveImpl(param, tres, numOfRows, tscAsyncFetchSingleRowProxy);
H
hzcheng 已提交
188 189 190 191 192 193
}

void taos_fetch_rows_a(TAOS_RES *taosa, void (*fp)(void *, TAOS_RES *, int), void *param) {
  SSqlObj *pSql = (SSqlObj *)taosa;
  if (pSql == NULL || pSql->signature != pSql) {
    tscError("sql object is NULL");
194
    tscQueueAsyncError(fp, param, TSDB_CODE_TSC_DISCONNECTED);
H
hzcheng 已提交
195 196 197 198 199 200
    return;
  }

  SSqlRes *pRes = &pSql->res;
  SSqlCmd *pCmd = &pSql->cmd;

201 202 203 204
  // user-defined callback function is stored in fetchFp
  pSql->fetchFp = fp;
  pSql->fp = tscAsyncFetchRowsProxy;

H
hzcheng 已提交
205 206
  if (pRes->qhandle == 0) {
    tscError("qhandle is NULL");
H
Haojun Liao 已提交
207 208
    pRes->code = TSDB_CODE_TSC_INVALID_QHANDLE;
    tscQueueAsyncRes(pSql);
H
hzcheng 已提交
209 210 211 212
    return;
  }

  pSql->param = param;
S
slguan 已提交
213
  tscResetForNextRetrieve(pRes);
H
hjxilinx 已提交
214 215
  
  // handle the sub queries of join query
216
  if (pCmd->command == TSDB_SQL_TABLE_JOIN_RETRIEVE) {
H
hjxilinx 已提交
217
    tscFetchDatablockFromSubquery(pSql);
218 219 220 221 222 223 224
  } else if (pRes->completed) {
    if(pCmd->command == TSDB_SQL_FETCH) {
      if (hasMoreVnodesToTry(pSql)) {  // sequentially retrieve data from remain vnodes.
        tscTryQueryNextVnode(pSql, tscAsyncQueryRowsForNextVnode);
        return;
      } else {
        /*
225 226
       * all available virtual node has been checked already, now we need to check
       * for the next subclause queries
227 228 229 230 231 232 233
         */
        if (pCmd->clauseIndex < pCmd->numOfClause - 1) {
          tscTryQueryNextClause(pSql, tscAsyncQueryRowsForNextVnode);
          return;
        }

        /*
234 235
       * 1. has reach the limitation
       * 2. no remain virtual nodes to be retrieved anymore
236 237 238 239
         */
        (*pSql->fetchFp)(param, pSql, 0);
      }
      return;
B
Bomin Zhang 已提交
240
    } else if (pCmd->command == TSDB_SQL_RETRIEVE || pCmd->command == TSDB_SQL_RETRIEVE_LOCALMERGE) {
241
      // in case of show command, return no data
242
      (*pSql->fetchFp)(param, pSql, 0);
243 244
    } else {
      assert(0);
245 246
    }
  } else { // current query is not completed, continue retrieve from node
H
hjxilinx 已提交
247
    if (pCmd->command != TSDB_SQL_RETRIEVE_LOCALMERGE && pCmd->command < TSDB_SQL_LOCAL) {
H
hjxilinx 已提交
248 249 250 251
      pCmd->command = (pCmd->command > TSDB_SQL_MGMT) ? TSDB_SQL_RETRIEVE : TSDB_SQL_FETCH;
    }
  
    tscProcessSql(pSql);
H
hzcheng 已提交
252 253 254 255 256 257 258
  }
}

void taos_fetch_row_a(TAOS_RES *taosa, void (*fp)(void *, TAOS_RES *, TAOS_ROW), void *param) {
  SSqlObj *pSql = (SSqlObj *)taosa;
  if (pSql == NULL || pSql->signature != pSql) {
    tscError("sql object is NULL");
259
    tscQueueAsyncError(fp, param, TSDB_CODE_TSC_DISCONNECTED);
H
hzcheng 已提交
260 261 262 263 264 265 266 267
    return;
  }

  SSqlRes *pRes = &pSql->res;
  SSqlCmd *pCmd = &pSql->cmd;

  if (pRes->qhandle == 0) {
    tscError("qhandle is NULL");
268
    tscQueueAsyncError(fp, param, TSDB_CODE_TSC_INVALID_QHANDLE);
H
hzcheng 已提交
269 270 271 272 273
    return;
  }

  pSql->fetchFp = fp;
  pSql->param = param;
274
  
H
hzcheng 已提交
275
  if (pRes->row >= pRes->numOfRows) {
S
slguan 已提交
276
    tscResetForNextRetrieve(pRes);
277
    pSql->fp = tscAsyncFetchSingleRowProxy;
278
    
H
hjxilinx 已提交
279
    if (pCmd->command != TSDB_SQL_RETRIEVE_LOCALMERGE && pCmd->command < TSDB_SQL_LOCAL) {
280 281 282
      pCmd->command = (pCmd->command > TSDB_SQL_MGMT) ? TSDB_SQL_RETRIEVE : TSDB_SQL_FETCH;
    }
    
H
hzcheng 已提交
283 284
    tscProcessSql(pSql);
  } else {
B
Bomin Zhang 已提交
285
    SSchedMsg schedMsg = { 0 };
H
hzcheng 已提交
286 287 288 289 290 291 292 293
    schedMsg.fp = tscProcessFetchRow;
    schedMsg.ahandle = pSql;
    schedMsg.thandle = pRes->tsrow;
    schedMsg.msg = NULL;
    taosScheduleTask(tscQhandle, &schedMsg);
  }
}

294
void tscAsyncFetchSingleRowProxy(void *param, TAOS_RES *tres, int numOfRows) {
H
hzcheng 已提交
295 296 297 298
  SSqlObj *pSql = (SSqlObj *)tres;
  SSqlRes *pRes = &pSql->res;
  SSqlCmd *pCmd = &pSql->cmd;

299
  SQueryInfo *pQueryInfo = tscGetQueryInfoDetail(pCmd, pCmd->clauseIndex);
300
  
H
hzcheng 已提交
301
  if (numOfRows == 0) {
302 303 304
    if (hasMoreVnodesToTry(pSql)) {     // sequentially retrieve data from remain vnodes.
      tscTryQueryNextVnode(pSql, tscAsyncQuerySingleRowForNextVnode);
    } else {
H
hzcheng 已提交
305
      /*
306 307
       * 1. has reach the limitation
       * 2. no remain virtual nodes to be retrieved anymore
H
hzcheng 已提交
308 309 310
       */
      (*pSql->fetchFp)(pSql->param, pSql, NULL);
    }
311
    return;
H
hzcheng 已提交
312
  }
313
  
314
  for (int i = 0; i < pCmd->numOfCols; ++i){
H
hjxilinx 已提交
315 316 317
    SFieldSupInfo* pSup = taosArrayGet(pQueryInfo->fieldsInfo.pSupportInfo, i);
    if (pSup->pSqlExpr != NULL) {
//      pRes->tsrow[i] = TSC_GET_RESPTR_BASE(pRes, pQueryInfo, i) + pSup->pSqlExpr->resBytes * pRes->row;
318 319 320 321 322
    } else {
      //todo add
    }
  }
  
323 324 325
  pRes->row++;

  (*pSql->fetchFp)(pSql->param, pSql, pSql->res.tsrow);
H
hzcheng 已提交
326 327 328 329 330 331
}

void tscProcessFetchRow(SSchedMsg *pMsg) {
  SSqlObj *pSql = (SSqlObj *)pMsg->ahandle;
  SSqlRes *pRes = &pSql->res;
  SSqlCmd *pCmd = &pSql->cmd;
332
  
H
hjxilinx 已提交
333
  SQueryInfo *pQueryInfo = tscGetQueryInfoDetail(pCmd, pCmd->clauseIndex);
H
hzcheng 已提交
334

335
  for (int i = 0; i < pCmd->numOfCols; ++i) {
H
hjxilinx 已提交
336 337 338
    SFieldSupInfo* pSup = taosArrayGet(pQueryInfo->fieldsInfo.pSupportInfo, i);

    if (pSup->pSqlExpr != NULL) {
339
      tscGetResultColumnChr(pRes, &pQueryInfo->fieldsInfo, i);
340
    } else {
H
hjxilinx 已提交
341
//      todo add
342
    }
343 344
  }
  
H
hzcheng 已提交
345 346 347 348 349 350
  pRes->row++;
  (*pSql->fetchFp)(pSql->param, pSql, pRes->tsrow);
}

void tscProcessAsyncRes(SSchedMsg *pMsg) {
  SSqlObj *pSql = (SSqlObj *)pMsg->ahandle;
H
Haojun Liao 已提交
351
//  SSqlCmd *pCmd = &pSql->cmd;
H
hzcheng 已提交
352 353
  SSqlRes *pRes = &pSql->res;

H
Haojun Liao 已提交
354
//  void *taosres = pSql;
H
hzcheng 已提交
355 356

  // pCmd may be released, so cache pCmd->command
H
Haojun Liao 已提交
357 358
//  int cmd = pCmd->command;
//  int code = pRes->code;
H
hzcheng 已提交
359 360

  // in case of async insert, restore the user specified callback function
H
Haojun Liao 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
//  bool shouldFree = tscShouldBeFreed(pSql);

//  if (pCmd->command == TSDB_SQL_INSERT) {
//    assert(pSql->fp != NULL);
  assert(pSql->fp != NULL && pSql->fetchFp != NULL);
//  }

//  if (pSql->fp) {
  pSql->fp = pSql->fetchFp;
  (*pSql->fp)(pSql->param, pSql, pRes->code);
//  }

//  if (shouldFree) {
//    tscDebug("%p sqlObj is automatically freed in async res", pSql);
//    tscFreeSqlObj(pSql);
//  }
H
hzcheng 已提交
377 378
}

H
[td-99]  
hjxilinx 已提交
379
static void tscProcessAsyncError(SSchedMsg *pMsg) {
H
hzcheng 已提交
380
  void (*fp)() = pMsg->ahandle;
H
[td-99]  
hjxilinx 已提交
381
  (*fp)(pMsg->thandle, NULL, *(int32_t*)pMsg->msg);
H
hzcheng 已提交
382 383
}

H
[td-99]  
hjxilinx 已提交
384 385 386 387
void tscQueueAsyncError(void(*fp), void *param, int32_t code) {
  int32_t* c = malloc(sizeof(int32_t));
  *c = code;
  
B
Bomin Zhang 已提交
388
  SSchedMsg schedMsg = { 0 };
H
hzcheng 已提交
389 390 391
  schedMsg.fp = tscProcessAsyncError;
  schedMsg.ahandle = fp;
  schedMsg.thandle = param;
H
[td-99]  
hjxilinx 已提交
392
  schedMsg.msg = c;
H
hzcheng 已提交
393 394 395 396 397
  taosScheduleTask(tscQhandle, &schedMsg);
}

void tscQueueAsyncRes(SSqlObj *pSql) {
  if (pSql == NULL || pSql->signature != pSql) {
398
    tscDebug("%p SqlObj is freed, not add into queue async res", pSql);
H
hzcheng 已提交
399 400
    return;
  } else {
H
[td-32]  
hjxilinx 已提交
401
    tscError("%p add into queued async res, code:%s", pSql, tstrerror(pSql->res.code));
H
hzcheng 已提交
402 403
  }

B
Bomin Zhang 已提交
404
  SSchedMsg schedMsg = { 0 };
H
hzcheng 已提交
405 406 407 408 409 410 411 412 413
  schedMsg.fp = tscProcessAsyncRes;
  schedMsg.ahandle = pSql;
  schedMsg.thandle = (void *)1;
  schedMsg.msg = NULL;
  taosScheduleTask(tscQhandle, &schedMsg);
}

void tscProcessAsyncFree(SSchedMsg *pMsg) {
  SSqlObj *pSql = (SSqlObj *)pMsg->ahandle;
414
  tscDebug("%p sql is freed", pSql);
H
hzcheng 已提交
415 416 417 418 419
  taos_free_result(pSql);
}

int tscSendMsgToServer(SSqlObj *pSql);

H
hjxilinx 已提交
420
void tscTableMetaCallBack(void *param, TAOS_RES *res, int code) {
H
hzcheng 已提交
421 422 423 424 425
  SSqlObj *pSql = (SSqlObj *)param;
  if (pSql == NULL || pSql->signature != pSql) return;

  SSqlCmd *pCmd = &pSql->cmd;
  SSqlRes *pRes = &pSql->res;
H
Haojun Liao 已提交
426
  pRes->code = code;
H
hzcheng 已提交
427

H
[td-32]  
hjxilinx 已提交
428
  if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
429 430 431 432
    tscError("%p ge tableMeta failed, code:%s", pSql, tstrerror(code));
    goto _error;
  } else {
    tscDebug("%p get tableMeta successfully", pSql);
H
hzcheng 已提交
433 434 435
  }

  if (pSql->pStream == NULL) {
H
hjxilinx 已提交
436
    SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, pCmd->clauseIndex);
H
Haojun Liao 已提交
437 438 439

    // check if it is a sub-query of super table query first, if true, enter another routine
    if (TSDB_QUERY_HAS_TYPE(pQueryInfo->type, TSDB_QUERY_TYPE_STABLE_SUBQUERY)) {
440
      tscDebug("%p update table meta in local cache, continue to process sql and send corresponding subquery", pSql);
H
Haojun Liao 已提交
441

H
hjxilinx 已提交
442
      STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0);
dengyihao's avatar
dengyihao 已提交
443 444 445 446 447
      code = tscGetTableMeta(pSql, pTableMetaInfo);
      if (code == TSDB_CODE_TSC_ACTION_IN_PROGRESS) {
        return;
      } else {
        assert(code == TSDB_CODE_SUCCESS);      
H
Haojun Liao 已提交
448
      }
dengyihao's avatar
dengyihao 已提交
449
     
450
      assert((tscGetNumOfTags(pTableMetaInfo->pTableMeta) != 0) && pSql->param != NULL);
H
hzcheng 已提交
451 452

      SRetrieveSupport *trs = (SRetrieveSupport *)pSql->param;
453
      SSqlObj *         pParObj = trs->pParentSql;
454
      
455
      // NOTE: the vgroupInfo for the queried super table must be existed here.
H
hjxilinx 已提交
456
      assert(pParObj->signature == pParObj && trs->subqueryIndex == pTableMetaInfo->vgroupIndex &&
457
          pTableMetaInfo->vgroupIndex >= 0 && pTableMetaInfo->vgroupList != NULL);
S
slguan 已提交
458

H
Haojun Liao 已提交
459 460 461
      // tscProcessSql can add error into async res
      tscProcessSql(pSql);
      return;
H
Haojun Liao 已提交
462
    } else {  // continue to process normal async query
463
      if (pCmd->parseFinished) {
464
        tscDebug("%p update table meta in local cache, continue to process sql and send corresponding query", pSql);
H
Haojun Liao 已提交
465

466
        STableMetaInfo* pTableMetaInfo = tscGetTableMetaInfoFromCmd(pCmd, pCmd->clauseIndex, 0);
H
hjxilinx 已提交
467
        code = tscGetTableMeta(pSql, pTableMetaInfo);
dengyihao's avatar
dengyihao 已提交
468 469 470 471 472
        if (code == TSDB_CODE_TSC_ACTION_IN_PROGRESS) {
          return;
        } else {
          assert(code == TSDB_CODE_SUCCESS);      
        }
H
Haojun Liao 已提交
473

474
        // in case of insert, redo parsing the sql string and build new submit data block for two reasons:
475
        // 1. the table Id(tid & uid) may have been update, the submit block needs to be updated accordingly.
476 477 478 479 480
        // 2. vnode may need the schema information along with submit block to update its local table schema.
        if (pCmd->command == TSDB_SQL_INSERT) {
          tscDebug("%p redo parse sql string to build submit block", pSql);

          pCmd->parseFinished = false;
481 482
          tscResetSqlCmdObj(pCmd);
          
H
Haojun Liao 已提交
483 484 485 486
          code = tsParseSql(pSql, true);

          if (code == TSDB_CODE_TSC_ACTION_IN_PROGRESS) {
            return;
H
Haojun Liao 已提交
487 488
          } else if (code != TSDB_CODE_SUCCESS) {
            goto _error;
H
Haojun Liao 已提交
489 490
          }

H
Haojun Liao 已提交
491 492 493 494 495 496
          /*
           * Discard previous built submit blocks, and then parse the sql string again and build up all submit blocks,
           * and send the required submit block according to index value in supporter to server.
           */
          pSql->fp = pSql->fetchFp;  // restore the fp
          tscHandleInsertRetry(pSql);
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
        } else if (pCmd->command == TSDB_SQL_SELECT) {  // in case of other query type, continue
          tscDebug("%p redo parse sql string and proceed", pSql);
          //tscDebug("before  %p fp:%p, fetchFp:%p", pSql, pSql->fp, pSql->fetchFp);
          pCmd->parseFinished = false;
          tscResetSqlCmdObj(pCmd);

          //tscDebug("after %p fp:%p, fetchFp:%p", pSql, pSql->fp, pSql->fetchFp);
          code = tsParseSql(pSql, true);

          if (code == TSDB_CODE_TSC_ACTION_IN_PROGRESS) {
            return;
          } else if (code != TSDB_CODE_SUCCESS) {
            goto _error;
          }

          tscProcessSql(pSql);
        } else {  // in all other cases, simple retry
H
Haojun Liao 已提交
514
          tscProcessSql(pSql);
L
[#1083]  
lihui 已提交
515
        }
516

H
Haojun Liao 已提交
517
        return;
L
[#1083]  
lihui 已提交
518
      } else {
519
        tscDebug("%p continue parse sql after get table meta", pSql);
H
Haojun Liao 已提交
520

H
hjxilinx 已提交
521
        code = tsParseSql(pSql, false);
522 523 524 525 526 527
        if (code == TSDB_CODE_TSC_ACTION_IN_PROGRESS) {
          return;
        } else if (code != TSDB_CODE_SUCCESS) {
          goto _error;
        }

H
Haojun Liao 已提交
528
        if (TSDB_QUERY_HAS_TYPE(pQueryInfo->type, TSDB_QUERY_TYPE_STMT_INSERT)) {
H
Hui Li 已提交
529 530
          STableMetaInfo* pTableMetaInfo = tscGetTableMetaInfoFromCmd(pCmd, pCmd->clauseIndex, 0);
          code = tscGetTableMeta(pSql, pTableMetaInfo);
dengyihao's avatar
dengyihao 已提交
531 532 533 534 535
          if (code == TSDB_CODE_TSC_ACTION_IN_PROGRESS) {
            return;
          } else {
            assert(code == TSDB_CODE_SUCCESS);      
          }
536

H
Hui Li 已提交
537
          (*pSql->fp)(pSql->param, pSql, code);
H
Hui Li 已提交
538 539
          return;
        }
540 541

        // proceed to invoke the tscDoQuery();
L
[#1083]  
lihui 已提交
542
      }
H
hzcheng 已提交
543
    }
S
slguan 已提交
544

H
hzcheng 已提交
545
  } else {  // stream computing
546
    STableMetaInfo *pTableMetaInfo = tscGetTableMetaInfoFromCmd(pCmd, pCmd->clauseIndex, 0);
H
hzcheng 已提交
547

548 549 550 551 552 553
    code = tscGetTableMeta(pSql, pTableMetaInfo);
    if (code == TSDB_CODE_TSC_ACTION_IN_PROGRESS) {
      return;
    } else if (code != TSDB_CODE_SUCCESS) {
      goto _error;
    }
H
hzcheng 已提交
554

H
Haojun Liao 已提交
555
    if (UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo)) {
H
hjxilinx 已提交
556
      code = tscGetSTableVgroupInfo(pSql, pCmd->clauseIndex);
557 558 559 560 561
      if (code == TSDB_CODE_TSC_ACTION_IN_PROGRESS) {
        return;
      } else if (code != TSDB_CODE_SUCCESS) {
        goto _error;
      }
H
hzcheng 已提交
562 563
    }

564
    tscDebug("%p stream:%p meta is updated, start new query, command:%d", pSql, pSql->pStream, pSql->cmd.command);
565 566 567 568
    if (!pSql->cmd.parseFinished) {
      tsParseSql(pSql, false);
      sem_post(&pSql->rspSem);
    }
569

570
    return;
H
hzcheng 已提交
571 572 573
  }

  tscDoQuery(pSql);
574 575 576 577 578 579 580
  return;

  _error:
  if (code != TSDB_CODE_SUCCESS) {
    pSql->res.code = code;
    tscQueueAsyncRes(pSql);
  }
H
hzcheng 已提交
581
}