clientStmt.c 33.0 KB
Newer Older
D
dapan1121 已提交
1 2 3 4 5

#include "clientInt.h"
#include "clientLog.h"
#include "tdef.h"

X
Xiaoyu Wang 已提交
6 7
#include "clientStmt.h"

dengyihao's avatar
dengyihao 已提交
8 9
char* gStmtStatusStr[] = {"unknown",     "init", "prepare", "settbname", "settags",
                          "fetchFields", "bind", "bindCol", "addBatch",  "exec"};
10

11
static int32_t stmtCreateRequest(STscStmt* pStmt) {
D
dapan1121 已提交
12
  int32_t code = 0;
H
Hongze Cheng 已提交
13 14

  if (pStmt->exec.pRequest == NULL) {
dengyihao's avatar
dengyihao 已提交
15 16
    code = buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false, &pStmt->exec.pRequest,
                        pStmt->reqid);
dengyihao's avatar
dengyihao 已提交
17
    if (pStmt->reqid != 0) {
dengyihao's avatar
dengyihao 已提交
18 19
      pStmt->reqid++;
    }
D
dapan1121 已提交
20 21 22
    if (TSDB_CODE_SUCCESS == code) {
      pStmt->exec.pRequest->syncQuery = true;
    }
23
  }
D
dapan1121 已提交
24 25

  return code;
26 27
}

D
stmt  
dapan1121 已提交
28
int32_t stmtSwitchStatus(STscStmt* pStmt, STMT_STATUS newStatus) {
D
dapan1121 已提交
29
  int32_t code = 0;
X
Xiaoyu Wang 已提交
30

31 32 33 34
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
    STMT_LOG_SEQ(newStatus);
  }

D
stmt  
dapan1121 已提交
35
  switch (newStatus) {
D
dapan1121 已提交
36 37
    case STMT_PREPARE:
      break;
D
stmt  
dapan1121 已提交
38
    case STMT_SETTBNAME:
D
dapan1121 已提交
39
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL)) {
D
dapan1121 已提交
40 41 42 43
        code = TSDB_CODE_TSC_STMT_API_ERROR;
      }
      break;
    case STMT_SETTAGS:
D
dapan1121 已提交
44
      if (STMT_STATUS_NE(SETTBNAME) && STMT_STATUS_NE(FETCH_FIELDS)) {
D
dapan1121 已提交
45 46 47 48 49 50 51 52 53 54 55 56
        code = TSDB_CODE_TSC_STMT_API_ERROR;
      }
      break;
    case STMT_FETCH_FIELDS:
      if (STMT_STATUS_EQ(INIT)) {
        code = TSDB_CODE_TSC_STMT_API_ERROR;
      }
      break;
    case STMT_BIND:
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
        code = TSDB_CODE_TSC_STMT_API_ERROR;
      }
X
Xiaoyu Wang 已提交
57 58 59 60 61
      /*
            if ((pStmt->sql.type == STMT_TYPE_MULTI_INSERT) && ()) {
              code = TSDB_CODE_TSC_STMT_API_ERROR;
            }
      */
D
dapan1121 已提交
62 63 64 65 66 67 68 69 70 71
      break;
    case STMT_BIND_COL:
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND)) {
        code = TSDB_CODE_TSC_STMT_API_ERROR;
      }
      break;
    case STMT_ADD_BATCH:
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
        code = TSDB_CODE_TSC_STMT_API_ERROR;
      }
D
stmt  
dapan1121 已提交
72
      break;
D
dapan1121 已提交
73
    case STMT_EXECUTE:
D
dapan1121 已提交
74
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
X
Xiaoyu Wang 已提交
75 76
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
            STMT_STATUS_NE(BIND_COL)) {
D
dapan1121 已提交
77 78 79 80 81 82
          code = TSDB_CODE_TSC_STMT_API_ERROR;
        }
      } else {
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS)) {
          code = TSDB_CODE_TSC_STMT_API_ERROR;
        }
D
dapan1121 已提交
83
      }
D
dapan1121 已提交
84
      break;
D
stmt  
dapan1121 已提交
85
    default:
S
Shengliang Guan 已提交
86
      code = TSDB_CODE_APP_ERROR;
D
stmt  
dapan1121 已提交
87 88 89
      break;
  }

D
dapan1121 已提交
90
  STMT_ERR_RET(code);
D
stmt  
dapan1121 已提交
91 92 93 94 95 96

  pStmt->sql.status = newStatus;

  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
97
int32_t stmtGetTbName(TAOS_STMT* stmt, char** tbName) {
D
stmt  
dapan1121 已提交
98 99
  STscStmt* pStmt = (STscStmt*)stmt;

D
stmt  
dapan1121 已提交
100
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
101

D
dapan 已提交
102
  if ('\0' == pStmt->bInfo.tbName[0]) {
D
stmt  
dapan1121 已提交
103 104 105 106
    tscError("no table name set");
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_TBNAME_ERROR);
  }

D
stmt  
dapan1121 已提交
107
  *tbName = pStmt->bInfo.tbName;
D
stmt  
dapan1121 已提交
108 109 110 111

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
112
int32_t stmtBackupQueryFields(STscStmt* pStmt) {
X
Xiaoyu Wang 已提交
113
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
D
dapan1121 已提交
114 115
  pRes->numOfCols = pStmt->exec.pRequest->body.resInfo.numOfCols;
  pRes->precision = pStmt->exec.pRequest->body.resInfo.precision;
X
Xiaoyu Wang 已提交
116

D
dapan1121 已提交
117 118 119 120
  int32_t size = pRes->numOfCols * sizeof(TAOS_FIELD);
  pRes->fields = taosMemoryMalloc(size);
  pRes->userFields = taosMemoryMalloc(size);
  if (NULL == pRes->fields || NULL == pRes->userFields) {
S
Shengliang Guan 已提交
121
    STMT_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
122
  }
D
dapan1121 已提交
123 124 125 126 127 128 129
  memcpy(pRes->fields, pStmt->exec.pRequest->body.resInfo.fields, size);
  memcpy(pRes->userFields, pStmt->exec.pRequest->body.resInfo.userFields, size);

  return TSDB_CODE_SUCCESS;
}

int32_t stmtRestoreQueryFields(STscStmt* pStmt) {
X
Xiaoyu Wang 已提交
130 131 132
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
  int32_t            size = pRes->numOfCols * sizeof(TAOS_FIELD);

D
dapan1121 已提交
133 134 135 136 137 138
  pStmt->exec.pRequest->body.resInfo.numOfCols = pRes->numOfCols;
  pStmt->exec.pRequest->body.resInfo.precision = pRes->precision;

  if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
    pStmt->exec.pRequest->body.resInfo.fields = taosMemoryMalloc(size);
    if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
S
Shengliang Guan 已提交
139
      STMT_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
140 141 142 143 144 145 146
    }
    memcpy(pStmt->exec.pRequest->body.resInfo.fields, pRes->fields, size);
  }

  if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
    pStmt->exec.pRequest->body.resInfo.userFields = taosMemoryMalloc(size);
    if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
S
Shengliang Guan 已提交
147
      STMT_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
148 149 150
    }
    memcpy(pStmt->exec.pRequest->body.resInfo.userFields, pRes->userFields, size);
  }
D
dapan1121 已提交
151 152 153 154

  return TSDB_CODE_SUCCESS;
}

155 156
int32_t stmtUpdateBindInfo(TAOS_STMT* stmt, STableMeta* pTableMeta, void* tags, SName* tbName, const char* sTableName,
                           bool autoCreateTbl) {
D
stmt  
dapan1121 已提交
157
  STscStmt* pStmt = (STscStmt*)stmt;
158
  char      tbFName[TSDB_TABLE_FNAME_LEN];
159
  tNameExtractFullName(tbName, tbFName);
D
stmt  
dapan1121 已提交
160

161
  memcpy(&pStmt->bInfo.sname, tbName, sizeof(*tbName));
D
dapan 已提交
162 163
  strncpy(pStmt->bInfo.tbFName, tbFName, sizeof(pStmt->bInfo.tbFName) - 1);
  pStmt->bInfo.tbFName[sizeof(pStmt->bInfo.tbFName) - 1] = 0;
164

165
  pStmt->bInfo.tbUid = autoCreateTbl ? 0 : pTableMeta->uid;
D
stmt  
dapan1121 已提交
166 167 168
  pStmt->bInfo.tbSuid = pTableMeta->suid;
  pStmt->bInfo.tbType = pTableMeta->tableType;
  pStmt->bInfo.boundTags = tags;
D
dapan1121 已提交
169
  pStmt->bInfo.tagsCached = false;
D
dapan1121 已提交
170
  tstrncpy(pStmt->bInfo.stbFName, sTableName, sizeof(pStmt->bInfo.stbFName));
D
stmt  
dapan1121 已提交
171 172 173 174

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
175
int32_t stmtUpdateExecInfo(TAOS_STMT* stmt, SHashObj* pVgHash, SHashObj* pBlockHash) {
D
stmt  
dapan1121 已提交
176 177
  STscStmt* pStmt = (STscStmt*)stmt;

D
dapan1121 已提交
178
  pStmt->sql.pVgHash = pVgHash;
D
stmt  
dapan1121 已提交
179 180 181 182 183
  pStmt->exec.pBlockHash = pBlockHash;

  return TSDB_CODE_SUCCESS;
}

184
int32_t stmtUpdateInfo(TAOS_STMT* stmt, STableMeta* pTableMeta, void* tags, SName* tbName, bool autoCreateTbl,
wmmhello's avatar
wmmhello 已提交
185
                       SHashObj* pVgHash, SHashObj* pBlockHash, const char* sTableName) {
D
dapan 已提交
186 187
  STscStmt* pStmt = (STscStmt*)stmt;

188
  STMT_ERR_RET(stmtUpdateBindInfo(stmt, pTableMeta, tags, tbName, sTableName, autoCreateTbl));
D
dapan1121 已提交
189
  STMT_ERR_RET(stmtUpdateExecInfo(stmt, pVgHash, pBlockHash));
D
dapan 已提交
190

D
dapan 已提交
191
  pStmt->sql.autoCreateTbl = autoCreateTbl;
192

D
dapan 已提交
193 194 195
  return TSDB_CODE_SUCCESS;
}

D
stmt  
dapan1121 已提交
196 197 198
int32_t stmtGetExecInfo(TAOS_STMT* stmt, SHashObj** pVgHash, SHashObj** pBlockHash) {
  STscStmt* pStmt = (STscStmt*)stmt;

D
dapan1121 已提交
199
  *pVgHash = pStmt->sql.pVgHash;
D
stmt  
dapan1121 已提交
200
  *pBlockHash = pStmt->exec.pBlockHash;
D
stmt  
dapan1121 已提交
201 202 203 204

  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
205
int32_t stmtCacheBlock(STscStmt* pStmt) {
D
stmt  
dapan1121 已提交
206 207 208
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
    return TSDB_CODE_SUCCESS;
  }
D
stmt  
dapan1121 已提交
209

210
  uint64_t uid = pStmt->bInfo.tbUid;
D
dapan 已提交
211
  uint64_t cacheUid = (TSDB_CHILD_TABLE == pStmt->bInfo.tbType) ? pStmt->bInfo.tbSuid : uid;
D
stmt  
dapan1121 已提交
212

D
dapan 已提交
213
  if (taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid))) {
D
stmt  
dapan1121 已提交
214 215 216
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
217
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
dengyihao's avatar
dengyihao 已提交
218
  if (!pSrc) {
wmmhello's avatar
wmmhello 已提交
219 220
    return TSDB_CODE_OUT_OF_MEMORY;
  }
D
dapan1121 已提交
221
  STableDataCxt* pDst = NULL;
222

D
dapan1121 已提交
223
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
D
stmt  
dapan1121 已提交
224 225

  SStmtTableCache cache = {
D
dapan1121 已提交
226
      .pDataCtx = pDst,
X
Xiaoyu Wang 已提交
227
      .boundTags = pStmt->bInfo.boundTags,
D
stmt  
dapan1121 已提交
228 229
  };

D
dapan 已提交
230
  if (taosHashPut(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid), &cache, sizeof(cache))) {
D
stmt  
dapan1121 已提交
231 232
    return TSDB_CODE_OUT_OF_MEMORY;
  }
D
stmt  
dapan1121 已提交
233

D
dapan 已提交
234 235 236 237 238
  if (pStmt->sql.autoCreateTbl) {
    pStmt->bInfo.tagsCached = true;
  } else {
    pStmt->bInfo.boundTags = NULL;
  }
239

D
stmt  
dapan1121 已提交
240 241 242
  return TSDB_CODE_SUCCESS;
}

D
stmt  
dapan1121 已提交
243
int32_t stmtParseSql(STscStmt* pStmt) {
D
dapan1121 已提交
244 245
  pStmt->exec.pCurrBlock = NULL;

D
stmt  
dapan1121 已提交
246
  SStmtCallback stmtCb = {
247 248 249 250
      .pStmt = pStmt,
      .getTbNameFn = stmtGetTbName,
      .setInfoFn = stmtUpdateInfo,
      .getExecInfoFn = stmtGetExecInfo,
D
stmt  
dapan1121 已提交
251
  };
D
stmt  
dapan1121 已提交
252

253
  STMT_ERR_RET(stmtCreateRequest(pStmt));
H
Hongze Cheng 已提交
254

D
stmt  
dapan1121 已提交
255 256
  STMT_ERR_RET(parseSql(pStmt->exec.pRequest, false, &pStmt->sql.pQuery, &stmtCb));

D
stmt  
dapan1121 已提交
257
  pStmt->bInfo.needParse = false;
X
Xiaoyu Wang 已提交
258

D
dapan1121 已提交
259 260 261 262 263 264
  if (pStmt->sql.pQuery->pRoot && 0 == pStmt->sql.type) {
    pStmt->sql.type = STMT_TYPE_INSERT;
  } else if (pStmt->sql.pQuery->pPrepareRoot) {
    pStmt->sql.type = STMT_TYPE_QUERY;
  }

D
stmt  
dapan1121 已提交
265 266
  return TSDB_CODE_SUCCESS;
}
D
stmt  
dapan1121 已提交
267

D
stmt  
dapan1121 已提交
268
int32_t stmtCleanBindInfo(STscStmt* pStmt) {
D
stmt  
dapan1121 已提交
269 270 271 272
  pStmt->bInfo.tbUid = 0;
  pStmt->bInfo.tbSuid = 0;
  pStmt->bInfo.tbType = 0;
  pStmt->bInfo.needParse = true;
D
dapan 已提交
273
  pStmt->bInfo.inExecCache = false;
D
stmt  
dapan1121 已提交
274

D
dapan 已提交
275 276
  pStmt->bInfo.tbName[0] = 0;
  pStmt->bInfo.tbFName[0] = 0;
D
dapan1121 已提交
277
  if (!pStmt->bInfo.tagsCached) {
278
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
D
dapan1121 已提交
279 280
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
  }
wmmhello's avatar
wmmhello 已提交
281
  memset(pStmt->bInfo.stbFName, 0, TSDB_TABLE_FNAME_LEN);
D
stmt  
dapan1121 已提交
282
  return TSDB_CODE_SUCCESS;
D
stmt  
dapan1121 已提交
283 284
}

D
dapan1121 已提交
285 286
int32_t stmtCleanExecInfo(STscStmt* pStmt, bool keepTable, bool deepClean) {
  if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
D
dapan1121 已提交
287 288 289
    taos_free_result(pStmt->exec.pRequest);
    pStmt->exec.pRequest = NULL;
  }
D
stmt  
dapan1121 已提交
290

D
dapan 已提交
291
  size_t keyLen = 0;
292
  void*  pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
D
stmt  
dapan1121 已提交
293
  while (pIter) {
D
dapan1121 已提交
294
    STableDataCxt* pBlocks = *(STableDataCxt**)pIter;
D
dapan1121 已提交
295 296
    char*          key = taosHashGetKey(pIter, &keyLen);
    STableMeta*    pMeta = qGetTableMetaInDataBlock(pBlocks);
297

D
dapan1121 已提交
298 299 300
    if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
      ASSERT(NULL == pBlocks->pData);
      TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
D
dapan1121 已提交
301
      STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
X
Xiaoyu Wang 已提交
302

D
stmt  
dapan1121 已提交
303 304 305 306
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
      continue;
    }

D
dapan1121 已提交
307
    qDestroyStmtDataBlock(pBlocks);
D
dapan 已提交
308
    taosHashRemove(pStmt->exec.pBlockHash, key, keyLen);
D
stmt  
dapan1121 已提交
309 310 311 312

    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
  }

D
dapan1121 已提交
313 314
  if (keepTable) {
    return TSDB_CODE_SUCCESS;
D
stmt  
dapan1121 已提交
315 316
  }

D
dapan1121 已提交
317 318
  taosHashCleanup(pStmt->exec.pBlockHash);
  pStmt->exec.pBlockHash = NULL;
D
stmt  
dapan1121 已提交
319

D
dapan1121 已提交
320 321
  tDestroySSubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
  taosMemoryFreeClear(pStmt->exec.pCurrTbData);
D
dapan1121 已提交
322

D
stmt  
dapan1121 已提交
323 324 325 326 327 328
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));

  return TSDB_CODE_SUCCESS;
}

int32_t stmtCleanSQLInfo(STscStmt* pStmt) {
D
dapan1121 已提交
329 330
  taosMemoryFree(pStmt->sql.queryRes.fields);
  taosMemoryFree(pStmt->sql.queryRes.userFields);
D
stmt  
dapan1121 已提交
331 332
  taosMemoryFree(pStmt->sql.sqlStr);
  qDestroyQuery(pStmt->sql.pQuery);
D
dapan1121 已提交
333
  taosArrayDestroy(pStmt->sql.nodeList);
D
dapan1121 已提交
334 335
  taosHashCleanup(pStmt->sql.pVgHash);
  pStmt->sql.pVgHash = NULL;
X
Xiaoyu Wang 已提交
336 337

  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
D
stmt  
dapan1121 已提交
338
  while (pIter) {
X
Xiaoyu Wang 已提交
339
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
D
stmt  
dapan1121 已提交
340

D
dapan1121 已提交
341
    qDestroyStmtDataBlock(pCache->pDataCtx);
342
    qDestroyBoundColInfo(pCache->boundTags);
D
dapan1121 已提交
343
    taosMemoryFreeClear(pCache->boundTags);
X
Xiaoyu Wang 已提交
344

D
stmt  
dapan1121 已提交
345 346 347 348 349
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
  }
  taosHashCleanup(pStmt->sql.pTableCache);
  pStmt->sql.pTableCache = NULL;

D
dapan1121 已提交
350
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
D
stmt  
dapan1121 已提交
351
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
D
stmt  
dapan1121 已提交
352

D
dapan1121 已提交
353 354
  memset(&pStmt->sql, 0, sizeof(pStmt->sql));

D
stmt  
dapan1121 已提交
355 356 357
  return TSDB_CODE_SUCCESS;
}

358 359
int32_t stmtRebuildDataBlock(STscStmt* pStmt, STableDataCxt* pDataBlock, STableDataCxt** newBlock, uint64_t uid,
                             uint64_t suid) {
360 361 362
  SEpSet           ep = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
  SVgroupInfo      vgInfo = {0};
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
D
dapan1121 已提交
363 364 365
                           .requestId = pStmt->exec.pRequest->requestId,
                           .requestObjRefId = pStmt->exec.pRequest->self,
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
366

D
dapan1121 已提交
367
  STMT_ERR_RET(catalogGetTableHashVgroup(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &vgInfo));
368
  STMT_ERR_RET(
D
dapan1121 已提交
369
      taosHashPut(pStmt->sql.pVgHash, (const char*)&vgInfo.vgId, sizeof(vgInfo.vgId), (char*)&vgInfo, sizeof(vgInfo)));
370

D
dapan1121 已提交
371 372 373
  STMT_ERR_RET(qRebuildStmtDataBlock(newBlock, pDataBlock, uid, suid, vgInfo.vgId, pStmt->sql.autoCreateTbl));

  STMT_DLOG("tableDataCxt rebuilt, uid:%" PRId64 ", vgId:%d", uid, vgInfo.vgId);
D
dapan 已提交
374 375 376 377

  return TSDB_CODE_SUCCESS;
}

D
stmt  
dapan1121 已提交
378
int32_t stmtGetFromCache(STscStmt* pStmt) {
D
stmt  
dapan1121 已提交
379
  pStmt->bInfo.needParse = true;
D
dapan 已提交
380
  pStmt->bInfo.inExecCache = false;
381

382
  STableDataCxt** pCxtInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
D
dapan1121 已提交
383
  if (pCxtInExec) {
D
dapan1121 已提交
384
    pStmt->bInfo.needParse = false;
D
dapan 已提交
385 386
    pStmt->bInfo.inExecCache = true;

D
dapan1121 已提交
387
    pStmt->exec.pCurrBlock = *pCxtInExec;
D
dapan 已提交
388 389

    if (pStmt->sql.autoCreateTbl) {
D
dapan1121 已提交
390
      tscDebug("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
D
dapan 已提交
391 392 393
      return TSDB_CODE_SUCCESS;
    }
  }
D
stmt  
dapan1121 已提交
394

D
stmt  
dapan1121 已提交
395
  if (NULL == pStmt->sql.pTableCache || taosHashGetSize(pStmt->sql.pTableCache) <= 0) {
D
dapan 已提交
396 397 398
    if (pStmt->bInfo.inExecCache) {
      ASSERT(taosHashGetSize(pStmt->exec.pBlockHash) == 1);
      pStmt->bInfo.needParse = false;
D
dapan1121 已提交
399
      tscDebug("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
D
dapan 已提交
400 401
      return TSDB_CODE_SUCCESS;
    }
402

D
dapan1121 已提交
403
    tscDebug("no stmt block cache for tb %s", pStmt->bInfo.tbFName);
D
stmt  
dapan1121 已提交
404 405 406
    return TSDB_CODE_SUCCESS;
  }

D
dapan 已提交
407 408 409 410
  if (NULL == pStmt->pCatalog) {
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
  }

D
dapan 已提交
411 412 413 414
  if (pStmt->sql.autoCreateTbl) {
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &pStmt->bInfo.tbSuid, sizeof(pStmt->bInfo.tbSuid));
    if (pCache) {
      pStmt->bInfo.needParse = false;
D
dapan1121 已提交
415
      pStmt->bInfo.tbUid = 0;
416

D
dapan1121 已提交
417
      STableDataCxt* pNewBlock = NULL;
D
dapan1121 已提交
418
      STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, 0, pStmt->bInfo.tbSuid));
419 420 421

      if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
                      POINTER_BYTES)) {
D
dapan 已提交
422 423
        STMT_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
      }
424

D
dapan1121 已提交
425 426
      pStmt->exec.pCurrBlock = pNewBlock;

H
Hongze Cheng 已提交
427
      tscDebug("reuse stmt block for tb %s in sqlBlock, suid:0x%" PRIx64, pStmt->bInfo.tbFName, pStmt->bInfo.tbSuid);
D
dapan1121 已提交
428

D
dapan 已提交
429 430
      return TSDB_CODE_SUCCESS;
    }
431

D
dapan 已提交
432 433
    STMT_RET(stmtCleanBindInfo(pStmt));
  }
D
stmt  
dapan1121 已提交
434

435 436
  STableMeta*      pTableMeta = NULL;
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
D
dapan1121 已提交
437 438 439
                           .requestId = pStmt->exec.pRequest->requestId,
                           .requestObjRefId = pStmt->exec.pRequest->self,
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
440
  int32_t          code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
D
dapan1121 已提交
441 442
  if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
    STMT_ERR_RET(stmtCleanBindInfo(pStmt));
X
Xiaoyu Wang 已提交
443

D
dapan1121 已提交
444 445
    tscDebug("tb %s not exist", pStmt->bInfo.tbFName);

D
dapan1121 已提交
446 447 448 449
    return TSDB_CODE_SUCCESS;
  }

  STMT_ERR_RET(code);
X
Xiaoyu Wang 已提交
450

D
dapan1121 已提交
451 452
  uint64_t uid = pTableMeta->uid;
  uint64_t suid = pTableMeta->suid;
X
Xiaoyu Wang 已提交
453
  int8_t   tableType = pTableMeta->tableType;
D
dapan1121 已提交
454
  taosMemoryFree(pTableMeta);
D
dapan 已提交
455
  uint64_t cacheUid = (TSDB_CHILD_TABLE == tableType) ? suid : uid;
456

D
dapan1121 已提交
457
  if (uid == pStmt->bInfo.tbUid) {
D
stmt  
dapan1121 已提交
458
    pStmt->bInfo.needParse = false;
D
dapan1121 已提交
459

D
dapan1121 已提交
460 461
    tscDebug("tb %s is current table", pStmt->bInfo.tbFName);

D
stmt  
dapan1121 已提交
462 463 464
    return TSDB_CODE_SUCCESS;
  }

D
dapan 已提交
465
  if (pStmt->bInfo.inExecCache) {
D
dapan 已提交
466
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
D
stmt  
dapan1121 已提交
467
    if (NULL == pCache) {
468 469 470
      tscError("table [%s, %" PRIx64 ", %" PRIx64 "] found in exec blockHash, but not in sql blockHash",
               pStmt->bInfo.tbFName, uid, cacheUid);

S
Shengliang Guan 已提交
471
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
D
stmt  
dapan1121 已提交
472
    }
X
Xiaoyu Wang 已提交
473

D
stmt  
dapan1121 已提交
474
    pStmt->bInfo.needParse = false;
X
Xiaoyu Wang 已提交
475

D
dapan1121 已提交
476 477 478
    pStmt->bInfo.tbUid = uid;
    pStmt->bInfo.tbSuid = suid;
    pStmt->bInfo.tbType = tableType;
D
stmt  
dapan1121 已提交
479
    pStmt->bInfo.boundTags = pCache->boundTags;
D
dapan1121 已提交
480
    pStmt->bInfo.tagsCached = true;
D
dapan1121 已提交
481

D
dapan1121 已提交
482 483
    tscDebug("tb %s in execBlock list, set to current", pStmt->bInfo.tbFName);

D
stmt  
dapan1121 已提交
484 485 486
    return TSDB_CODE_SUCCESS;
  }

D
dapan 已提交
487
  SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
D
stmt  
dapan1121 已提交
488
  if (pCache) {
D
stmt  
dapan1121 已提交
489
    pStmt->bInfo.needParse = false;
D
stmt  
dapan1121 已提交
490

D
dapan1121 已提交
491 492 493
    pStmt->bInfo.tbUid = uid;
    pStmt->bInfo.tbSuid = suid;
    pStmt->bInfo.tbType = tableType;
D
stmt  
dapan1121 已提交
494
    pStmt->bInfo.boundTags = pCache->boundTags;
D
dapan1121 已提交
495
    pStmt->bInfo.tagsCached = true;
D
stmt  
dapan1121 已提交
496

D
dapan1121 已提交
497
    STableDataCxt* pNewBlock = NULL;
D
dapan1121 已提交
498
    STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, uid, suid));
D
stmt  
dapan1121 已提交
499

500 501
    if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
                    POINTER_BYTES)) {
D
stmt  
dapan1121 已提交
502 503
      STMT_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
X
Xiaoyu Wang 已提交
504

D
dapan1121 已提交
505 506
    pStmt->exec.pCurrBlock = pNewBlock;

D
dapan1121 已提交
507 508
    tscDebug("tb %s in sqlBlock list, set to current", pStmt->bInfo.tbFName);

D
stmt  
dapan1121 已提交
509 510 511
    return TSDB_CODE_SUCCESS;
  }

D
stmt  
dapan1121 已提交
512 513
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));

D
stmt  
dapan1121 已提交
514 515 516
  return TSDB_CODE_SUCCESS;
}

D
stmt  
dapan1121 已提交
517 518 519 520 521
int32_t stmtResetStmt(STscStmt* pStmt) {
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));

  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
  if (NULL == pStmt->sql.pTableCache) {
S
Shengliang Guan 已提交
522
    terrno = TSDB_CODE_OUT_OF_MEMORY;
D
stmt  
dapan1121 已提交
523 524 525 526 527 528 529 530
    STMT_ERR_RET(terrno);
  }

  pStmt->sql.status = STMT_INIT;

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
531
TAOS_STMT* stmtInit(STscObj* taos, int64_t reqid) {
X
Xiaoyu Wang 已提交
532
  STscObj*  pObj = (STscObj*)taos;
D
dapan1121 已提交
533 534 535
  STscStmt* pStmt = NULL;

  pStmt = taosMemoryCalloc(1, sizeof(STscStmt));
D
stmt  
dapan1121 已提交
536
  if (NULL == pStmt) {
S
Shengliang Guan 已提交
537
    terrno = TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
538 539
    return NULL;
  }
D
stmt  
dapan1121 已提交
540

D
stmt  
dapan1121 已提交
541 542
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
  if (NULL == pStmt->sql.pTableCache) {
S
Shengliang Guan 已提交
543
    terrno = TSDB_CODE_OUT_OF_MEMORY;
D
stmt  
dapan1121 已提交
544 545 546
    taosMemoryFree(pStmt);
    return NULL;
  }
D
stmt  
dapan1121 已提交
547

D
dapan1121 已提交
548
  pStmt->taos = pObj;
D
stmt  
dapan1121 已提交
549
  pStmt->bInfo.needParse = true;
D
stmt  
dapan1121 已提交
550
  pStmt->sql.status = STMT_INIT;
dengyihao's avatar
dengyihao 已提交
551
  pStmt->reqid = reqid;
X
Xiaoyu Wang 已提交
552

553
  STMT_LOG_SEQ(STMT_INIT);
dengyihao's avatar
dengyihao 已提交
554

555 556
  tscDebug("stmt:%p initialized", pStmt);

D
stmt  
dapan1121 已提交
557 558
  return pStmt;
}
D
dapan1121 已提交
559

X
Xiaoyu Wang 已提交
560
int stmtPrepare(TAOS_STMT* stmt, const char* sql, unsigned long length) {
D
stmt  
dapan1121 已提交
561 562
  STscStmt* pStmt = (STscStmt*)stmt;

563
  STMT_DLOG_E("start to prepare");
D
dapan1121 已提交
564

D
stmt  
dapan1121 已提交
565
  if (pStmt->sql.status >= STMT_PREPARE) {
D
stmt  
dapan1121 已提交
566
    STMT_ERR_RET(stmtResetStmt(pStmt));
D
stmt  
dapan1121 已提交
567 568
  }

D
stmt  
dapan1121 已提交
569 570 571 572 573
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));

  if (length <= 0) {
    length = strlen(sql);
  }
X
Xiaoyu Wang 已提交
574

D
stmt  
dapan1121 已提交
575 576
  pStmt->sql.sqlStr = strndup(sql, length);
  pStmt->sql.sqlLen = length;
D
stmt  
dapan1121 已提交
577 578 579 580

  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
581
int stmtSetTbName(TAOS_STMT* stmt, const char* tbName) {
D
stmt  
dapan1121 已提交
582 583
  STscStmt* pStmt = (STscStmt*)stmt;

584
  STMT_DLOG("start to set tbName: %s", tbName);
D
dapan1121 已提交
585

D
stmt  
dapan1121 已提交
586
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
D
stmt  
dapan1121 已提交
587

D
dapan1121 已提交
588 589 590 591 592 593 594
  int32_t insert = 0;
  stmtIsInsert(stmt, &insert);
  if (0 == insert) {
    tscError("set tb name not available for none insert statement");
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
  }

595
  STMT_ERR_RET(stmtCreateRequest(pStmt));
596 597 598

  STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
                            pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
D
dapan 已提交
599
  tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName);
600

D
stmt  
dapan1121 已提交
601
  STMT_ERR_RET(stmtGetFromCache(pStmt));
D
stmt  
dapan1121 已提交
602

D
stmt  
dapan1121 已提交
603
  if (pStmt->bInfo.needParse) {
D
dapan 已提交
604 605
    strncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName) - 1);
    pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
D
dapan1121 已提交
606 607

    STMT_ERR_RET(stmtParseSql(pStmt));
D
stmt  
dapan1121 已提交
608 609
  }

D
stmt  
dapan1121 已提交
610 611 612
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
613
int stmtSetTbTags(TAOS_STMT* stmt, TAOS_MULTI_BIND* tags) {
D
stmt  
dapan1121 已提交
614
  STscStmt* pStmt = (STscStmt*)stmt;
D
dapan1121 已提交
615

616
  STMT_DLOG_E("start to set tbTags");
D
dapan1121 已提交
617

D
stmt  
dapan1121 已提交
618
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
D
stmt  
dapan1121 已提交
619

D
dapan 已提交
620 621 622
  if (pStmt->bInfo.inExecCache) {
    return TSDB_CODE_SUCCESS;
  }
D
dapan 已提交
623

D
dapan1121 已提交
624 625
  STableDataCxt** pDataBlock =
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
D
stmt  
dapan1121 已提交
626
  if (NULL == pDataBlock) {
D
dapan 已提交
627
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
S
Shengliang Guan 已提交
628
    STMT_ERR_RET(TSDB_CODE_APP_ERROR);
D
stmt  
dapan1121 已提交
629
  }
X
Xiaoyu Wang 已提交
630

D
dapan1121 已提交
631
  tscDebug("start to bind stmt tag values");
H
Hongze Cheng 已提交
632 633 634
  STMT_ERR_RET(qBindStmtTagsValue(*pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.tbSuid, pStmt->bInfo.stbFName,
                                  pStmt->bInfo.sname.tname, tags, pStmt->exec.pRequest->msgBuf,
                                  pStmt->exec.pRequest->msgBufLen));
D
stmt  
dapan1121 已提交
635

D
stmt  
dapan1121 已提交
636 637 638
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
639
int stmtFetchTagFields(STscStmt* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
D
stmt  
dapan1121 已提交
640 641 642 643 644
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
    tscError("invalid operation to get query tag fileds");
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
  }

D
dapan1121 已提交
645 646
  STableDataCxt** pDataBlock =
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
D
stmt  
dapan1121 已提交
647
  if (NULL == pDataBlock) {
D
dapan 已提交
648
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
S
Shengliang Guan 已提交
649
    STMT_ERR_RET(TSDB_CODE_APP_ERROR);
D
stmt  
dapan1121 已提交
650 651
  }

D
stmt  
dapan1121 已提交
652
  STMT_ERR_RET(qBuildStmtTagFields(*pDataBlock, pStmt->bInfo.boundTags, fieldNum, fields));
D
stmt  
dapan1121 已提交
653

D
stmt  
dapan1121 已提交
654 655
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
656

D
dapan1121 已提交
657
int stmtFetchColFields(STscStmt* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
D
stmt  
dapan1121 已提交
658 659 660 661 662
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
    tscError("invalid operation to get query column fileds");
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
  }

D
dapan1121 已提交
663 664
  STableDataCxt** pDataBlock =
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
D
stmt  
dapan1121 已提交
665
  if (NULL == pDataBlock) {
D
dapan 已提交
666
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
S
Shengliang Guan 已提交
667
    STMT_ERR_RET(TSDB_CODE_APP_ERROR);
D
stmt  
dapan1121 已提交
668 669
  }

D
stmt  
dapan1121 已提交
670
  STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields));
D
stmt  
dapan1121 已提交
671

X
Xiaoyu Wang 已提交
672
  return TSDB_CODE_SUCCESS;
D
stmt  
dapan1121 已提交
673 674
}

X
Xiaoyu Wang 已提交
675
int stmtBindBatch(TAOS_STMT* stmt, TAOS_MULTI_BIND* bind, int32_t colIdx) {
D
stmt  
dapan1121 已提交
676 677
  STscStmt* pStmt = (STscStmt*)stmt;

678
  STMT_DLOG("start to bind stmt data, colIdx: %d", colIdx);
D
dapan1121 已提交
679

D
dapan1121 已提交
680 681
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));

X
Xiaoyu Wang 已提交
682 683
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
D
stmt  
dapan1121 已提交
684
    pStmt->bInfo.needParse = false;
D
stmt  
dapan1121 已提交
685
  }
D
stmt  
dapan1121 已提交
686

D
dapan1121 已提交
687 688 689 690
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
    taos_free_result(pStmt->exec.pRequest);
    pStmt->exec.pRequest = NULL;
  }
X
Xiaoyu Wang 已提交
691

692
  STMT_ERR_RET(stmtCreateRequest(pStmt));
D
stmt  
dapan1121 已提交
693

D
stmt  
dapan1121 已提交
694
  if (pStmt->bInfo.needParse) {
D
stmt  
dapan1121 已提交
695 696
    STMT_ERR_RET(stmtParseSql(pStmt));
  }
D
stmt  
dapan1121 已提交
697

D
dapan1121 已提交
698
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
699 700
    STMT_ERR_RET(qStmtBindParams(pStmt->sql.pQuery, bind, colIdx));

D
dapan1121 已提交
701 702 703 704 705 706 707 708 709 710 711
    SParseContext ctx = {.requestId = pStmt->exec.pRequest->requestId,
                         .acctId = pStmt->taos->acctId,
                         .db = pStmt->exec.pRequest->pDb,
                         .topicQuery = false,
                         .pSql = pStmt->sql.sqlStr,
                         .sqlLen = pStmt->sql.sqlLen,
                         .pMsg = pStmt->exec.pRequest->msgBuf,
                         .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
                         .pTransporter = pStmt->taos->pAppInfo->pTransporter,
                         .pStmtCb = NULL,
                         .pUser = pStmt->taos->user};
D
dapan1121 已提交
712 713
    ctx.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &ctx.pCatalog));
714

D
dapan1121 已提交
715
    STMT_ERR_RET(qStmtParseQuerySql(&ctx, pStmt->sql.pQuery));
X
Xiaoyu Wang 已提交
716

D
dapan1121 已提交
717
    if (pStmt->sql.pQuery->haveResultSet) {
718 719
      setResSchemaInfo(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->pResSchema,
                       pStmt->sql.pQuery->numOfResCols);
D
dapan1121 已提交
720
      taosMemoryFreeClear(pStmt->sql.pQuery->pResSchema);
D
dapan1121 已提交
721 722
      setResPrecision(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->precision);
    }
723

D
dapan1121 已提交
724
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
725
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);
D
dapan1121 已提交
726
    TSWAP(pStmt->exec.pRequest->targetTableList, pStmt->sql.pQuery->pTargetTableList);
D
dapan1121 已提交
727

728 729 730
    // if (STMT_TYPE_QUERY == pStmt->sql.queryRes) {
    //   STMT_ERR_RET(stmtRestoreQueryFields(pStmt));
    // }
D
dapan1121 已提交
731

732
    // STMT_ERR_RET(stmtBackupQueryFields(pStmt));
D
dapan1121 已提交
733

D
dapan1121 已提交
734
    return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
735
  }
736

D
dapan1121 已提交
737 738 739 740 741
  STableDataCxt** pDataBlock = NULL;

  if (pStmt->exec.pCurrBlock) {
    pDataBlock = &pStmt->exec.pCurrBlock;
  } else {
742 743
    pDataBlock =
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
D
dapan1121 已提交
744 745 746 747 748
    if (NULL == pDataBlock) {
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
      STMT_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
    }
    pStmt->exec.pCurrBlock = *pDataBlock;
D
stmt  
dapan1121 已提交
749
  }
D
stmt  
dapan1121 已提交
750 751

  if (colIdx < 0) {
D
dapan1121 已提交
752 753 754 755 756
    int32_t code = qBindStmtColsValue(*pDataBlock, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen);
    if (code) {
      tscError("qBindStmtColsValue failed, error:%s", tstrerror(code));
      STMT_ERR_RET(code);
    }
D
stmt  
dapan1121 已提交
757 758 759
  } else {
    if (colIdx != (pStmt->bInfo.sBindLastIdx + 1) && colIdx != 0) {
      tscError("bind column index not in sequence");
S
Shengliang Guan 已提交
760
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
D
stmt  
dapan1121 已提交
761 762 763
    }

    pStmt->bInfo.sBindLastIdx = colIdx;
X
Xiaoyu Wang 已提交
764

D
stmt  
dapan1121 已提交
765 766 767
    if (0 == colIdx) {
      pStmt->bInfo.sBindRowNum = bind->num;
    }
X
Xiaoyu Wang 已提交
768 769 770

    qBindStmtSingleColValue(*pDataBlock, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen, colIdx,
                            pStmt->bInfo.sBindRowNum);
D
stmt  
dapan1121 已提交
771
  }
X
Xiaoyu Wang 已提交
772

D
stmt  
dapan1121 已提交
773
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
774 775
}

X
Xiaoyu Wang 已提交
776
int stmtAddBatch(TAOS_STMT* stmt) {
D
stmt  
dapan1121 已提交
777 778
  STscStmt* pStmt = (STscStmt*)stmt;

779
  STMT_DLOG_E("start to add batch");
D
dapan1121 已提交
780

D
stmt  
dapan1121 已提交
781
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
D
stmt  
dapan1121 已提交
782

D
dapan1121 已提交
783
  STMT_ERR_RET(stmtCacheBlock(pStmt));
X
Xiaoyu Wang 已提交
784

D
stmt  
dapan1121 已提交
785 786 787
  return TSDB_CODE_SUCCESS;
}

788
int stmtUpdateTableUid(STscStmt* pStmt, SSubmitRsp* pRsp) {
D
dapan1121 已提交
789
  tscDebug("stmt start to update tbUid, blockNum: %d", pRsp->nBlocks);
D
dapan1121 已提交
790

791 792 793
  int32_t code = 0;
  int32_t finalCode = 0;
  size_t  keyLen = 0;
794
  void*   pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
D
dapan 已提交
795
  while (pIter) {
D
dapan1121 已提交
796
    STableDataCxt* pBlock = *(STableDataCxt**)pIter;
797
    char*          key = taosHashGetKey(pIter, &keyLen);
798 799

    STableMeta* pMeta = qGetTableMetaInDataBlock(pBlock);
D
dapan 已提交
800 801 802 803 804
    if (pMeta->uid) {
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
      continue;
    }

805 806
    SSubmitBlkRsp* blkRsp = NULL;
    int32_t        i = 0;
D
dapan 已提交
807 808 809 810 811
    for (; i < pRsp->nBlocks; ++i) {
      blkRsp = pRsp->pBlocks + i;
      if (strlen(blkRsp->tblFName) != keyLen) {
        continue;
      }
812

D
dapan 已提交
813 814 815
      if (strncmp(blkRsp->tblFName, key, keyLen)) {
        continue;
      }
816

D
dapan 已提交
817 818 819 820
      break;
    }

    if (i < pRsp->nBlocks) {
821 822 823
      tscDebug("auto created table %s uid updated from %" PRIx64 " to %" PRIx64, blkRsp->tblFName, pMeta->uid,
               blkRsp->uid);

D
dapan 已提交
824 825 826
      pMeta->uid = blkRsp->uid;
      pStmt->bInfo.tbUid = blkRsp->uid;
    } else {
827 828
      tscDebug("table %s not found in submit rsp, will update from catalog", pStmt->bInfo.tbFName);
      if (NULL == pStmt->pCatalog) {
829 830 831 832 833 834
        code = catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog);
        if (code) {
          pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
          finalCode = code;
          continue;
        }
835 836
      }

837 838 839 840 841 842
      code = stmtCreateRequest(pStmt);
      if (code) {
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
        finalCode = code;
        continue;
      }
843 844 845 846 847 848 849 850 851 852

      STableMeta*      pTableMeta = NULL;
      SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
                               .requestId = pStmt->exec.pRequest->requestId,
                               .requestObjRefId = pStmt->exec.pRequest->self,
                               .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
      int32_t          code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);

      taos_free_result(pStmt->exec.pRequest);
      pStmt->exec.pRequest = NULL;
853 854 855 856

      if (code || NULL == pTableMeta) {
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
        finalCode = code;
D
dapan1121 已提交
857
        taosMemoryFree(pTableMeta);
858
        continue;
859 860 861 862
      }

      pMeta->uid = pTableMeta->uid;
      pStmt->bInfo.tbUid = pTableMeta->uid;
863
      taosMemoryFree(pTableMeta);
D
dapan 已提交
864 865 866 867 868
    }

    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
  }

869
  return finalCode;
D
dapan 已提交
870 871
}

872 873 874 875
int stmtExec(TAOS_STMT* stmt) {
  STscStmt*   pStmt = (STscStmt*)stmt;
  int32_t     code = 0;
  SSubmitRsp* pRsp = NULL;
D
stmt  
dapan1121 已提交
876

877
  STMT_DLOG_E("start to exec");
D
dapan1121 已提交
878

D
stmt  
dapan1121 已提交
879
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
D
stmt  
dapan1121 已提交
880

D
dapan1121 已提交
881
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
882
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
D
dapan1121 已提交
883
  } else {
D
dapan1121 已提交
884 885
    tDestroySSubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
    taosMemoryFreeClear(pStmt->exec.pCurrTbData);
886

D
dapan1121 已提交
887
    STMT_ERR_RET(qCloneCurrentTbData(pStmt->exec.pCurrBlock, &pStmt->exec.pCurrTbData));
888

D
dapan1121 已提交
889
    STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
D
dapan1121 已提交
890
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
D
dapan1121 已提交
891
  }
D
fix bug  
dapan1121 已提交
892 893 894 895 896 897

  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
    if (code) {
      pStmt->exec.pRequest->code = code;
    } else {
D
dapan1121 已提交
898
      tFreeSSubmitRsp(pRsp);
D
fix bug  
dapan1121 已提交
899 900 901 902
      STMT_ERR_RET(stmtResetStmt(pStmt));
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
    }
  }
X
Xiaoyu Wang 已提交
903

D
stmt  
dapan1121 已提交
904
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
D
stmt  
dapan1121 已提交
905

D
dapan1121 已提交
906 907
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
  pStmt->affectedRows += pStmt->exec.affectedRows;
D
stmt  
dapan1121 已提交
908

D
stmt  
dapan1121 已提交
909 910
_return:

D
dapan1121 已提交
911
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
D
dapan 已提交
912

D
dapan1121 已提交
913
  tFreeSSubmitRsp(pRsp);
914

D
stmt  
dapan1121 已提交
915
  ++pStmt->sql.runTimes;
X
Xiaoyu Wang 已提交
916

D
stmt  
dapan1121 已提交
917
  STMT_RET(code);
D
stmt  
dapan1121 已提交
918 919
}

X
Xiaoyu Wang 已提交
920
int stmtClose(TAOS_STMT* stmt) {
D
stmt  
dapan1121 已提交
921 922
  STscStmt* pStmt = (STscStmt*)stmt;

D
dapan1121 已提交
923
  stmtCleanSQLInfo(pStmt);
D
dapan1121 已提交
924
  taosMemoryFree(stmt);
D
dapan1121 已提交
925 926

  return TSDB_CODE_SUCCESS;
D
stmt  
dapan1121 已提交
927 928
}

X
Xiaoyu Wang 已提交
929
const char* stmtErrstr(TAOS_STMT* stmt) {
D
stmt  
dapan1121 已提交
930
  STscStmt* pStmt = (STscStmt*)stmt;
D
stmt  
dapan1121 已提交
931

D
fix bug  
dapan1121 已提交
932
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
X
Xiaoyu Wang 已提交
933
    return (char*)tstrerror(terrno);
D
stmt  
dapan1121 已提交
934 935
  }

D
fix bug  
dapan1121 已提交
936
  pStmt->exec.pRequest->code = terrno;
D
stmt  
dapan1121 已提交
937

D
stmt  
dapan1121 已提交
938
  return taos_errstr(pStmt->exec.pRequest);
D
stmt  
dapan1121 已提交
939 940
}

X
Xiaoyu Wang 已提交
941
int stmtAffectedRows(TAOS_STMT* stmt) { return ((STscStmt*)stmt)->affectedRows; }
D
stmt  
dapan1121 已提交
942

X
Xiaoyu Wang 已提交
943
int stmtAffectedRowsOnce(TAOS_STMT* stmt) { return ((STscStmt*)stmt)->exec.affectedRows; }
D
dapan1121 已提交
944

X
Xiaoyu Wang 已提交
945
int stmtIsInsert(TAOS_STMT* stmt, int* insert) {
D
stmt  
dapan1121 已提交
946 947
  STscStmt* pStmt = (STscStmt*)stmt;

948 949
  STMT_DLOG_E("start is insert");

D
stmt  
dapan1121 已提交
950 951 952
  if (pStmt->sql.type) {
    *insert = (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
  } else {
953
    *insert = qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
D
stmt  
dapan1121 已提交
954
  }
X
Xiaoyu Wang 已提交
955

D
stmt  
dapan1121 已提交
956 957 958
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
959 960 961
int stmtGetTagFields(TAOS_STMT* stmt, int* nums, TAOS_FIELD_E** fields) {
  STscStmt* pStmt = (STscStmt*)stmt;

962 963
  STMT_DLOG_E("start to get tag fields");

D
dapan1121 已提交
964 965 966
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
    STMT_RET(TSDB_CODE_TSC_STMT_API_ERROR);
  }
967

D
dapan1121 已提交
968 969 970 971 972 973 974 975 976 977 978 979
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));

  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
    pStmt->bInfo.needParse = false;
  }

  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
    taos_free_result(pStmt->exec.pRequest);
    pStmt->exec.pRequest = NULL;
  }

980
  STMT_ERR_RET(stmtCreateRequest(pStmt));
D
dapan1121 已提交
981 982 983 984 985 986 987 988 989 990 991 992 993

  if (pStmt->bInfo.needParse) {
    STMT_ERR_RET(stmtParseSql(pStmt));
  }

  STMT_ERR_RET(stmtFetchTagFields(stmt, nums, fields));

  return TSDB_CODE_SUCCESS;
}

int stmtGetColFields(TAOS_STMT* stmt, int* nums, TAOS_FIELD_E** fields) {
  STscStmt* pStmt = (STscStmt*)stmt;

994 995
  STMT_DLOG_E("start to get col fields");

D
dapan1121 已提交
996 997 998
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
    STMT_RET(TSDB_CODE_TSC_STMT_API_ERROR);
  }
999

D
dapan1121 已提交
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));

  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
    pStmt->bInfo.needParse = false;
  }

  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
    taos_free_result(pStmt->exec.pRequest);
    pStmt->exec.pRequest = NULL;
  }

1012
  STMT_ERR_RET(stmtCreateRequest(pStmt));
D
dapan1121 已提交
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022

  if (pStmt->bInfo.needParse) {
    STMT_ERR_RET(stmtParseSql(pStmt));
  }

  STMT_ERR_RET(stmtFetchColFields(stmt, nums, fields));

  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
1023
int stmtGetParamNum(TAOS_STMT* stmt, int* nums) {
D
dapan1121 已提交
1024 1025
  STscStmt* pStmt = (STscStmt*)stmt;

1026 1027
  STMT_DLOG_E("start to get param num");

D
dapan1121 已提交
1028 1029
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));

X
Xiaoyu Wang 已提交
1030 1031
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
D
dapan1121 已提交
1032 1033 1034 1035 1036 1037 1038
    pStmt->bInfo.needParse = false;
  }

  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
    taos_free_result(pStmt->exec.pRequest);
    pStmt->exec.pRequest = NULL;
  }
X
Xiaoyu Wang 已提交
1039

1040
  STMT_ERR_RET(stmtCreateRequest(pStmt));
D
dapan1121 已提交
1041 1042 1043 1044 1045
  if (pStmt->bInfo.needParse) {
    STMT_ERR_RET(stmtParseSql(pStmt));
  }

  if (STMT_TYPE_QUERY == pStmt->sql.type) {
D
dapan1121 已提交
1046
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
D
dapan1121 已提交
1047 1048 1049
  } else {
    STMT_ERR_RET(stmtFetchColFields(stmt, nums, NULL));
  }
X
Xiaoyu Wang 已提交
1050

D
stmt  
dapan1121 已提交
1051 1052 1053
  return TSDB_CODE_SUCCESS;
}

1054
int stmtGetParam(TAOS_STMT* stmt, int idx, int* type, int* bytes) {
D
dapan1121 已提交
1055 1056
  STscStmt* pStmt = (STscStmt*)stmt;

1057 1058
  STMT_DLOG_E("start to get param");

D
dapan1121 已提交
1059 1060 1061
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
    STMT_RET(TSDB_CODE_TSC_STMT_API_ERROR);
  }
1062

D
dapan1121 已提交
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));

  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
    pStmt->bInfo.needParse = false;
  }

  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
    taos_free_result(pStmt->exec.pRequest);
    pStmt->exec.pRequest = NULL;
  }

1075
  STMT_ERR_RET(stmtCreateRequest(pStmt));
D
dapan1121 已提交
1076 1077 1078 1079 1080

  if (pStmt->bInfo.needParse) {
    STMT_ERR_RET(stmtParseSql(pStmt));
  }

1081 1082
  int32_t       nums = 0;
  TAOS_FIELD_E* pField = NULL;
D
dapan1121 已提交
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
  STMT_ERR_RET(stmtFetchColFields(stmt, &nums, &pField));
  if (idx >= nums) {
    tscError("idx %d is too big", idx);
    taosMemoryFree(pField);
    STMT_ERR_RET(TSDB_CODE_INVALID_PARA);
  }

  *type = pField[idx].type;
  *bytes = pField[idx].bytes;

  taosMemoryFree(pField);

  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
1098
TAOS_RES* stmtUseResult(TAOS_STMT* stmt) {
D
dapan1121 已提交
1099 1100
  STscStmt* pStmt = (STscStmt*)stmt;

1101 1102
  STMT_DLOG_E("start to use result");

D
dapan1121 已提交
1103 1104 1105 1106 1107 1108
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
    tscError("useResult only for query statement");
    return NULL;
  }

  return pStmt->exec.pRequest;
D
stmt  
dapan1121 已提交
1109
}