clientStmt.c 32.7 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:
D
dapan1121 已提交
86
      code = TSDB_CODE_TSC_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) {
D
dapan1121 已提交
121 122
    STMT_ERR_RET(TSDB_CODE_TSC_OUT_OF_MEMORY);
  }
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 139 140 141 142 143 144 145 146 147 148 149 150
  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) {
      STMT_ERR_RET(TSDB_CODE_TSC_OUT_OF_MEMORY);
    }
    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) {
      STMT_ERR_RET(TSDB_CODE_TSC_OUT_OF_MEMORY);
    }
    memcpy(pStmt->exec.pRequest->body.resInfo.userFields, pRes->userFields, size);
  }
D
dapan1121 已提交
151 152 153 154

  return TSDB_CODE_SUCCESS;
}

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

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

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

  return TSDB_CODE_SUCCESS;
}

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

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

  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
dapan 已提交
189
  STMT_ERR_RET(stmtUpdateExecInfo(stmt, pVgHash, pBlockHash, autoCreateTbl));
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 244
int32_t stmtParseSql(STscStmt* pStmt) {
  SStmtCallback stmtCb = {
245 246 247 248
      .pStmt = pStmt,
      .getTbNameFn = stmtGetTbName,
      .setInfoFn = stmtUpdateInfo,
      .getExecInfoFn = stmtGetExecInfo,
D
stmt  
dapan1121 已提交
249
  };
D
stmt  
dapan1121 已提交
250

251
  STMT_ERR_RET(stmtCreateRequest(pStmt));
H
Hongze Cheng 已提交
252

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

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

D
dapan1121 已提交
257 258 259 260 261 262
  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 已提交
263 264
  return TSDB_CODE_SUCCESS;
}
D
stmt  
dapan1121 已提交
265

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

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

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

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

296
/*
D
dapan 已提交
297
    if (keepTable && (strlen(pStmt->bInfo.tbFName) == keyLen) && strncmp(pStmt->bInfo.tbFName, key, keyLen) == 0) {
D
dapan1121 已提交
298
      STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
X
Xiaoyu Wang 已提交
299

D
stmt  
dapan1121 已提交
300 301 302
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
      continue;
    }
303
*/
D
stmt  
dapan1121 已提交
304

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

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

D
dapan 已提交
311
  pStmt->exec.autoCreateTbl = false;
312

313 314 315
  if (!keepTable) {
    taosHashCleanup(pStmt->exec.pBlockHash);
    pStmt->exec.pBlockHash = NULL;
D
stmt  
dapan1121 已提交
316 317
  }

D
stmt  
dapan1121 已提交
318 319 320 321 322 323
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));

  return TSDB_CODE_SUCCESS;
}

int32_t stmtCleanSQLInfo(STscStmt* pStmt) {
D
dapan1121 已提交
324 325
  taosMemoryFree(pStmt->sql.queryRes.fields);
  taosMemoryFree(pStmt->sql.queryRes.userFields);
D
stmt  
dapan1121 已提交
326 327
  taosMemoryFree(pStmt->sql.sqlStr);
  qDestroyQuery(pStmt->sql.pQuery);
D
dapan1121 已提交
328
  taosArrayDestroy(pStmt->sql.nodeList);
D
dapan1121 已提交
329 330
  taosHashCleanup(pStmt->sql.pVgHash);
  pStmt->sql.pVgHash = NULL;
X
Xiaoyu Wang 已提交
331 332

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

D
dapan1121 已提交
336
    qDestroyStmtDataBlock(pCache->pDataCtx);
337
    qDestroyBoundColInfo(pCache->boundTags);
D
dapan1121 已提交
338
    taosMemoryFreeClear(pCache->boundTags);
X
Xiaoyu Wang 已提交
339

D
stmt  
dapan1121 已提交
340 341 342 343 344
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
  }
  taosHashCleanup(pStmt->sql.pTableCache);
  pStmt->sql.pTableCache = NULL;

D
dapan1121 已提交
345
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
D
stmt  
dapan1121 已提交
346
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
D
stmt  
dapan1121 已提交
347

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

D
stmt  
dapan1121 已提交
350 351 352
  return TSDB_CODE_SUCCESS;
}

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

D
dapan1121 已提交
361
  STMT_ERR_RET(catalogGetTableHashVgroup(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &vgInfo));
362
  STMT_ERR_RET(
D
dapan1121 已提交
363
      taosHashPut(pStmt->sql.pVgHash, (const char*)&vgInfo.vgId, sizeof(vgInfo.vgId), (char*)&vgInfo, sizeof(vgInfo)));
364

D
dapan 已提交
365 366 367 368 369
  STMT_ERR_RET(qRebuildStmtDataBlock(newBlock, pDataBlock, uid, vgInfo.vgId));

  return TSDB_CODE_SUCCESS;
}

D
stmt  
dapan1121 已提交
370
int32_t stmtGetFromCache(STscStmt* pStmt) {
D
stmt  
dapan1121 已提交
371
  pStmt->bInfo.needParse = true;
D
dapan 已提交
372
  pStmt->bInfo.inExecCache = false;
373

D
dapan1121 已提交
374
  STableDataCxt* pCxtInExec =
375
      taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
D
dapan1121 已提交
376
  if (pCxtInExec) {
D
dapan1121 已提交
377
    pStmt->bInfo.needParse = false;
D
dapan 已提交
378 379 380
    pStmt->bInfo.inExecCache = true;

    if (pStmt->sql.autoCreateTbl) {
D
dapan1121 已提交
381
      tscDebug("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
D
dapan 已提交
382 383 384
      return TSDB_CODE_SUCCESS;
    }
  }
D
stmt  
dapan1121 已提交
385

D
stmt  
dapan1121 已提交
386
  if (NULL == pStmt->sql.pTableCache || taosHashGetSize(pStmt->sql.pTableCache) <= 0) {
D
dapan 已提交
387 388 389
    if (pStmt->bInfo.inExecCache) {
      ASSERT(taosHashGetSize(pStmt->exec.pBlockHash) == 1);
      pStmt->bInfo.needParse = false;
D
dapan1121 已提交
390
      tscDebug("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
D
dapan 已提交
391 392
      return TSDB_CODE_SUCCESS;
    }
393

D
dapan1121 已提交
394
    tscDebug("no stmt block cache for tb %s", pStmt->bInfo.tbFName);
D
stmt  
dapan1121 已提交
395 396 397
    return TSDB_CODE_SUCCESS;
  }

D
dapan 已提交
398 399 400 401
  if (NULL == pStmt->pCatalog) {
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
  }

D
dapan 已提交
402 403 404 405
  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 已提交
406 407 408
      pStmt->exec.autoCreateTbl = true;

      pStmt->bInfo.tbUid = 0;
409

D
dapan1121 已提交
410 411
      STableDataCxt* pNewBlock = NULL;
      STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, 0));
412 413 414

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

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

D
dapan 已提交
420 421
      return TSDB_CODE_SUCCESS;
    }
422

D
dapan 已提交
423 424
    STMT_RET(stmtCleanBindInfo(pStmt));
  }
D
stmt  
dapan1121 已提交
425

426 427
  STableMeta*      pTableMeta = NULL;
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
D
dapan1121 已提交
428 429 430
                           .requestId = pStmt->exec.pRequest->requestId,
                           .requestObjRefId = pStmt->exec.pRequest->self,
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
431
  int32_t          code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
D
dapan1121 已提交
432 433
  if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
    STMT_ERR_RET(stmtCleanBindInfo(pStmt));
X
Xiaoyu Wang 已提交
434

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

D
dapan1121 已提交
437 438 439 440
    return TSDB_CODE_SUCCESS;
  }

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

D
dapan1121 已提交
442 443
  uint64_t uid = pTableMeta->uid;
  uint64_t suid = pTableMeta->suid;
X
Xiaoyu Wang 已提交
444
  int8_t   tableType = pTableMeta->tableType;
D
dapan1121 已提交
445
  taosMemoryFree(pTableMeta);
D
dapan 已提交
446
  uint64_t cacheUid = (TSDB_CHILD_TABLE == tableType) ? suid : uid;
447

D
dapan1121 已提交
448
  if (uid == pStmt->bInfo.tbUid) {
D
stmt  
dapan1121 已提交
449
    pStmt->bInfo.needParse = false;
D
dapan1121 已提交
450

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

D
stmt  
dapan1121 已提交
453 454 455
    return TSDB_CODE_SUCCESS;
  }

D
dapan 已提交
456
  if (pStmt->bInfo.inExecCache) {
D
dapan 已提交
457
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
D
stmt  
dapan1121 已提交
458
    if (NULL == pCache) {
459 460 461
      tscError("table [%s, %" PRIx64 ", %" PRIx64 "] found in exec blockHash, but not in sql blockHash",
               pStmt->bInfo.tbFName, uid, cacheUid);

D
stmt  
dapan1121 已提交
462 463
      STMT_ERR_RET(TSDB_CODE_TSC_APP_ERROR);
    }
X
Xiaoyu Wang 已提交
464

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

D
dapan1121 已提交
467 468 469
    pStmt->bInfo.tbUid = uid;
    pStmt->bInfo.tbSuid = suid;
    pStmt->bInfo.tbType = tableType;
D
stmt  
dapan1121 已提交
470
    pStmt->bInfo.boundTags = pCache->boundTags;
D
dapan1121 已提交
471
    pStmt->bInfo.tagsCached = true;
D
dapan1121 已提交
472

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

D
stmt  
dapan1121 已提交
475 476 477
    return TSDB_CODE_SUCCESS;
  }

D
dapan 已提交
478
  SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
D
stmt  
dapan1121 已提交
479
  if (pCache) {
D
stmt  
dapan1121 已提交
480
    pStmt->bInfo.needParse = false;
D
stmt  
dapan1121 已提交
481

D
dapan1121 已提交
482 483 484
    pStmt->bInfo.tbUid = uid;
    pStmt->bInfo.tbSuid = suid;
    pStmt->bInfo.tbType = tableType;
D
stmt  
dapan1121 已提交
485
    pStmt->bInfo.boundTags = pCache->boundTags;
D
dapan1121 已提交
486
    pStmt->bInfo.tagsCached = true;
D
stmt  
dapan1121 已提交
487

D
dapan1121 已提交
488 489
    STableDataCxt* pNewBlock = NULL;
    STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, uid));
D
stmt  
dapan1121 已提交
490

491 492
    if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
                    POINTER_BYTES)) {
D
stmt  
dapan1121 已提交
493 494
      STMT_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
X
Xiaoyu Wang 已提交
495

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

D
stmt  
dapan1121 已提交
498 499 500
    return TSDB_CODE_SUCCESS;
  }

D
stmt  
dapan1121 已提交
501 502
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));

D
stmt  
dapan1121 已提交
503 504 505
  return TSDB_CODE_SUCCESS;
}

D
stmt  
dapan1121 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518 519
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) {
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    STMT_ERR_RET(terrno);
  }

  pStmt->sql.status = STMT_INIT;

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
520
TAOS_STMT* stmtInit(STscObj* taos, int64_t reqid) {
X
Xiaoyu Wang 已提交
521
  STscObj*  pObj = (STscObj*)taos;
D
dapan1121 已提交
522 523 524
  STscStmt* pStmt = NULL;

  pStmt = taosMemoryCalloc(1, sizeof(STscStmt));
D
stmt  
dapan1121 已提交
525
  if (NULL == pStmt) {
D
dapan1121 已提交
526 527 528
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    return NULL;
  }
D
stmt  
dapan1121 已提交
529

D
stmt  
dapan1121 已提交
530 531
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
  if (NULL == pStmt->sql.pTableCache) {
D
stmt  
dapan1121 已提交
532 533 534 535
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    taosMemoryFree(pStmt);
    return NULL;
  }
D
stmt  
dapan1121 已提交
536

D
dapan1121 已提交
537
  pStmt->taos = pObj;
D
stmt  
dapan1121 已提交
538
  pStmt->bInfo.needParse = true;
D
stmt  
dapan1121 已提交
539
  pStmt->sql.status = STMT_INIT;
dengyihao's avatar
dengyihao 已提交
540
  pStmt->reqid = reqid;
X
Xiaoyu Wang 已提交
541

542
  STMT_LOG_SEQ(STMT_INIT);
dengyihao's avatar
dengyihao 已提交
543

544 545
  tscDebug("stmt:%p initialized", pStmt);

D
stmt  
dapan1121 已提交
546 547
  return pStmt;
}
D
dapan1121 已提交
548

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

552
  STMT_DLOG_E("start to prepare");
D
dapan1121 已提交
553

D
stmt  
dapan1121 已提交
554
  if (pStmt->sql.status >= STMT_PREPARE) {
D
stmt  
dapan1121 已提交
555
    STMT_ERR_RET(stmtResetStmt(pStmt));
D
stmt  
dapan1121 已提交
556 557
  }

D
stmt  
dapan1121 已提交
558 559 560 561 562
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));

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

D
stmt  
dapan1121 已提交
564 565
  pStmt->sql.sqlStr = strndup(sql, length);
  pStmt->sql.sqlLen = length;
D
stmt  
dapan1121 已提交
566 567 568 569

  return TSDB_CODE_SUCCESS;
}

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

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

D
stmt  
dapan1121 已提交
575
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
D
stmt  
dapan1121 已提交
576

D
dapan1121 已提交
577 578 579 580 581 582 583
  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);
  }

584
  STMT_ERR_RET(stmtCreateRequest(pStmt));
585 586 587

  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 已提交
588
  tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName);
589

D
stmt  
dapan1121 已提交
590
  STMT_ERR_RET(stmtGetFromCache(pStmt));
D
stmt  
dapan1121 已提交
591

D
stmt  
dapan1121 已提交
592
  if (pStmt->bInfo.needParse) {
D
dapan 已提交
593 594
    strncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName) - 1);
    pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
D
dapan1121 已提交
595 596

    STMT_ERR_RET(stmtParseSql(pStmt));
D
stmt  
dapan1121 已提交
597 598
  }

D
stmt  
dapan1121 已提交
599 600 601
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
602
int stmtSetTbTags(TAOS_STMT* stmt, TAOS_MULTI_BIND* tags) {
D
stmt  
dapan1121 已提交
603
  STscStmt* pStmt = (STscStmt*)stmt;
D
dapan1121 已提交
604

605
  STMT_DLOG_E("start to set tbTags");
D
dapan1121 已提交
606

D
stmt  
dapan1121 已提交
607
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
D
stmt  
dapan1121 已提交
608

D
dapan 已提交
609 610 611
  if (pStmt->bInfo.inExecCache) {
    return TSDB_CODE_SUCCESS;
  }
D
dapan 已提交
612

D
dapan1121 已提交
613 614
  STableDataCxt** pDataBlock =
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
D
stmt  
dapan1121 已提交
615
  if (NULL == pDataBlock) {
D
dapan 已提交
616
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
D
stmt  
dapan1121 已提交
617 618
    STMT_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
  }
X
Xiaoyu Wang 已提交
619

D
dapan1121 已提交
620
  tscDebug("start to bind stmt tag values");
H
Hongze Cheng 已提交
621 622 623
  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 已提交
624

625 626
  pStmt->exec.autoCreateTbl = true;

D
stmt  
dapan1121 已提交
627 628 629
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
630
int stmtFetchTagFields(STscStmt* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
D
stmt  
dapan1121 已提交
631 632 633 634 635
  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 已提交
636 637
  STableDataCxt** pDataBlock =
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
D
stmt  
dapan1121 已提交
638
  if (NULL == pDataBlock) {
D
dapan 已提交
639
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
D
stmt  
dapan1121 已提交
640 641 642
    STMT_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
  }

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

D
stmt  
dapan1121 已提交
645 646
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
647

D
dapan1121 已提交
648
int stmtFetchColFields(STscStmt* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
D
stmt  
dapan1121 已提交
649 650 651 652 653
  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 已提交
654 655
  STableDataCxt** pDataBlock =
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
D
stmt  
dapan1121 已提交
656
  if (NULL == pDataBlock) {
D
dapan 已提交
657
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
D
stmt  
dapan1121 已提交
658 659 660
    STMT_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
  }

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

X
Xiaoyu Wang 已提交
663
  return TSDB_CODE_SUCCESS;
D
stmt  
dapan1121 已提交
664 665
}

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

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

D
dapan1121 已提交
671 672
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));

X
Xiaoyu Wang 已提交
673 674
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
D
stmt  
dapan1121 已提交
675
    pStmt->bInfo.needParse = false;
D
stmt  
dapan1121 已提交
676
  }
D
stmt  
dapan1121 已提交
677

D
dapan1121 已提交
678 679 680 681
  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 已提交
682

683
  STMT_ERR_RET(stmtCreateRequest(pStmt));
D
stmt  
dapan1121 已提交
684

D
stmt  
dapan1121 已提交
685
  if (pStmt->bInfo.needParse) {
D
stmt  
dapan1121 已提交
686 687
    STMT_ERR_RET(stmtParseSql(pStmt));
  }
D
stmt  
dapan1121 已提交
688

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

D
dapan1121 已提交
692 693 694 695 696 697 698 699 700 701 702
    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 已提交
703 704
    ctx.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &ctx.pCatalog));
705

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

D
dapan1121 已提交
708
    if (pStmt->sql.pQuery->haveResultSet) {
709 710
      setResSchemaInfo(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->pResSchema,
                       pStmt->sql.pQuery->numOfResCols);
D
dapan1121 已提交
711
      taosMemoryFreeClear(pStmt->sql.pQuery->pResSchema);
D
dapan1121 已提交
712 713
      setResPrecision(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->precision);
    }
714

D
dapan1121 已提交
715
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
716
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);
D
dapan1121 已提交
717
    TSWAP(pStmt->exec.pRequest->targetTableList, pStmt->sql.pQuery->pTargetTableList);
D
dapan1121 已提交
718

719 720 721
    // if (STMT_TYPE_QUERY == pStmt->sql.queryRes) {
    //   STMT_ERR_RET(stmtRestoreQueryFields(pStmt));
    // }
D
dapan1121 已提交
722

723
    // STMT_ERR_RET(stmtBackupQueryFields(pStmt));
D
dapan1121 已提交
724

D
dapan1121 已提交
725
    return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
726
  }
727

D
dapan1121 已提交
728 729
  STableDataCxt** pDataBlock =
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
D
stmt  
dapan1121 已提交
730
  if (NULL == pDataBlock) {
D
dapan 已提交
731
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
D
stmt  
dapan1121 已提交
732 733
    STMT_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
  }
D
stmt  
dapan1121 已提交
734 735

  if (colIdx < 0) {
D
dapan1121 已提交
736 737 738 739 740
    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 已提交
741 742 743 744 745 746 747
  } else {
    if (colIdx != (pStmt->bInfo.sBindLastIdx + 1) && colIdx != 0) {
      tscError("bind column index not in sequence");
      STMT_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
    }

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

D
stmt  
dapan1121 已提交
749 750 751
    if (0 == colIdx) {
      pStmt->bInfo.sBindRowNum = bind->num;
    }
X
Xiaoyu Wang 已提交
752 753 754

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

D
stmt  
dapan1121 已提交
757
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
758 759
}

X
Xiaoyu Wang 已提交
760
int stmtAddBatch(TAOS_STMT* stmt) {
D
stmt  
dapan1121 已提交
761 762
  STscStmt* pStmt = (STscStmt*)stmt;

763
  STMT_DLOG_E("start to add batch");
D
dapan1121 已提交
764

D
stmt  
dapan1121 已提交
765
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
D
stmt  
dapan1121 已提交
766

D
dapan1121 已提交
767
  STMT_ERR_RET(stmtCacheBlock(pStmt));
X
Xiaoyu Wang 已提交
768

D
stmt  
dapan1121 已提交
769 770 771
  return TSDB_CODE_SUCCESS;
}

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

775 776 777
  int32_t code = 0;
  int32_t finalCode = 0;
  size_t  keyLen = 0;
D
dapan1121 已提交
778
  void* pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
D
dapan 已提交
779
  while (pIter) {
D
dapan1121 已提交
780
    STableDataCxt* pBlock = *(STableDataCxt**)pIter;
781 782 783
    char*             key = taosHashGetKey(pIter, &keyLen);

    STableMeta* pMeta = qGetTableMetaInDataBlock(pBlock);
D
dapan 已提交
784 785 786 787 788
    if (pMeta->uid) {
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
      continue;
    }

789 790
    SSubmitBlkRsp* blkRsp = NULL;
    int32_t        i = 0;
D
dapan 已提交
791 792 793 794 795
    for (; i < pRsp->nBlocks; ++i) {
      blkRsp = pRsp->pBlocks + i;
      if (strlen(blkRsp->tblFName) != keyLen) {
        continue;
      }
796

D
dapan 已提交
797 798 799
      if (strncmp(blkRsp->tblFName, key, keyLen)) {
        continue;
      }
800

D
dapan 已提交
801 802 803 804
      break;
    }

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

D
dapan 已提交
808 809 810
      pMeta->uid = blkRsp->uid;
      pStmt->bInfo.tbUid = blkRsp->uid;
    } else {
811 812
      tscDebug("table %s not found in submit rsp, will update from catalog", pStmt->bInfo.tbFName);
      if (NULL == pStmt->pCatalog) {
813 814 815 816 817 818
        code = catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog);
        if (code) {
          pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
          finalCode = code;
          continue;
        }
819 820
      }

821 822 823 824 825 826
      code = stmtCreateRequest(pStmt);
      if (code) {
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
        finalCode = code;
        continue;
      }
827 828 829 830 831 832 833 834 835 836

      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;
837 838 839 840

      if (code || NULL == pTableMeta) {
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
        finalCode = code;
D
dapan1121 已提交
841
        taosMemoryFree(pTableMeta);
842
        continue;
843 844 845 846
      }

      pMeta->uid = pTableMeta->uid;
      pStmt->bInfo.tbUid = pTableMeta->uid;
D
dapan1121 已提交
847
      taosMemoryFree(pTableMeta);      
D
dapan 已提交
848 849 850 851 852
    }

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

853
  return finalCode;
D
dapan 已提交
854 855
}

856 857 858 859 860
int stmtExec(TAOS_STMT* stmt) {
  STscStmt*   pStmt = (STscStmt*)stmt;
  int32_t     code = 0;
  SSubmitRsp* pRsp = NULL;
  bool        autoCreateTbl = pStmt->exec.autoCreateTbl;
D
stmt  
dapan1121 已提交
861

862
  STMT_DLOG_E("start to exec");
D
dapan1121 已提交
863

D
stmt  
dapan1121 已提交
864
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
D
stmt  
dapan1121 已提交
865

D
dapan1121 已提交
866
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
867
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
D
dapan1121 已提交
868
  } else {
D
dapan1121 已提交
869
    STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
870
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, (autoCreateTbl ? (void**)&pRsp : NULL));
D
dapan1121 已提交
871
  }
D
fix bug  
dapan1121 已提交
872 873 874 875 876 877

  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 已提交
878
      tFreeSSubmitRsp(pRsp);
D
fix bug  
dapan1121 已提交
879 880 881 882
      STMT_ERR_RET(stmtResetStmt(pStmt));
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
    }
  }
X
Xiaoyu Wang 已提交
883

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

D
dapan1121 已提交
886 887
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
  pStmt->affectedRows += pStmt->exec.affectedRows;
D
stmt  
dapan1121 已提交
888

D
stmt  
dapan1121 已提交
889 890
_return:

D
dapan1121 已提交
891
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
D
dapan 已提交
892 893 894 895

  if (TSDB_CODE_SUCCESS == code && autoCreateTbl) {
    if (NULL == pRsp) {
      tscError("no submit resp got for auto create table");
D
dapan1121 已提交
896 897 898
      code = TSDB_CODE_TSC_APP_ERROR;
    } else {
      code = stmtUpdateTableUid(pStmt, pRsp);
D
dapan 已提交
899 900
    }
  }
X
Xiaoyu Wang 已提交
901

D
dapan1121 已提交
902
  tFreeSSubmitRsp(pRsp);
903

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

D
stmt  
dapan1121 已提交
906
  STMT_RET(code);
D
stmt  
dapan1121 已提交
907 908
}

X
Xiaoyu Wang 已提交
909
int stmtClose(TAOS_STMT* stmt) {
D
stmt  
dapan1121 已提交
910 911
  STscStmt* pStmt = (STscStmt*)stmt;

D
dapan1121 已提交
912
  stmtCleanSQLInfo(pStmt);
D
dapan1121 已提交
913
  taosMemoryFree(stmt);
D
dapan1121 已提交
914 915

  return TSDB_CODE_SUCCESS;
D
stmt  
dapan1121 已提交
916 917
}

X
Xiaoyu Wang 已提交
918
const char* stmtErrstr(TAOS_STMT* stmt) {
D
stmt  
dapan1121 已提交
919
  STscStmt* pStmt = (STscStmt*)stmt;
D
stmt  
dapan1121 已提交
920

D
fix bug  
dapan1121 已提交
921
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
X
Xiaoyu Wang 已提交
922
    return (char*)tstrerror(terrno);
D
stmt  
dapan1121 已提交
923 924
  }

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

D
stmt  
dapan1121 已提交
927
  return taos_errstr(pStmt->exec.pRequest);
D
stmt  
dapan1121 已提交
928 929
}

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

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

X
Xiaoyu Wang 已提交
934
int stmtIsInsert(TAOS_STMT* stmt, int* insert) {
D
stmt  
dapan1121 已提交
935 936
  STscStmt* pStmt = (STscStmt*)stmt;

937 938
  STMT_DLOG_E("start is insert");

D
stmt  
dapan1121 已提交
939 940 941
  if (pStmt->sql.type) {
    *insert = (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
  } else {
942
    *insert = qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
D
stmt  
dapan1121 已提交
943
  }
X
Xiaoyu Wang 已提交
944

D
stmt  
dapan1121 已提交
945 946 947
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
948 949 950
int stmtGetTagFields(TAOS_STMT* stmt, int* nums, TAOS_FIELD_E** fields) {
  STscStmt* pStmt = (STscStmt*)stmt;

951 952
  STMT_DLOG_E("start to get tag fields");

D
dapan1121 已提交
953 954 955
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
    STMT_RET(TSDB_CODE_TSC_STMT_API_ERROR);
  }
956

D
dapan1121 已提交
957 958 959 960 961 962 963 964 965 966 967 968
  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;
  }

969
  STMT_ERR_RET(stmtCreateRequest(pStmt));
D
dapan1121 已提交
970 971 972 973 974 975 976 977 978 979 980 981 982

  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;

983 984
  STMT_DLOG_E("start to get col fields");

D
dapan1121 已提交
985 986 987
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
    STMT_RET(TSDB_CODE_TSC_STMT_API_ERROR);
  }
988

D
dapan1121 已提交
989 990 991 992 993 994 995 996 997 998 999 1000
  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;
  }

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

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

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

  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
1012
int stmtGetParamNum(TAOS_STMT* stmt, int* nums) {
D
dapan1121 已提交
1013 1014
  STscStmt* pStmt = (STscStmt*)stmt;

1015 1016
  STMT_DLOG_E("start to get param num");

D
dapan1121 已提交
1017 1018
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));

X
Xiaoyu Wang 已提交
1019 1020
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
D
dapan1121 已提交
1021 1022 1023 1024 1025 1026 1027
    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 已提交
1028

1029
  STMT_ERR_RET(stmtCreateRequest(pStmt));
D
dapan1121 已提交
1030 1031 1032 1033 1034
  if (pStmt->bInfo.needParse) {
    STMT_ERR_RET(stmtParseSql(pStmt));
  }

  if (STMT_TYPE_QUERY == pStmt->sql.type) {
D
dapan1121 已提交
1035
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
D
dapan1121 已提交
1036 1037 1038
  } else {
    STMT_ERR_RET(stmtFetchColFields(stmt, nums, NULL));
  }
X
Xiaoyu Wang 已提交
1039

D
stmt  
dapan1121 已提交
1040 1041 1042
  return TSDB_CODE_SUCCESS;
}

1043
int stmtGetParam(TAOS_STMT* stmt, int idx, int* type, int* bytes) {
D
dapan1121 已提交
1044 1045
  STscStmt* pStmt = (STscStmt*)stmt;

1046 1047
  STMT_DLOG_E("start to get param");

D
dapan1121 已提交
1048 1049 1050
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
    STMT_RET(TSDB_CODE_TSC_STMT_API_ERROR);
  }
1051

D
dapan1121 已提交
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
  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;
  }

1064
  STMT_ERR_RET(stmtCreateRequest(pStmt));
D
dapan1121 已提交
1065 1066 1067 1068 1069

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

1070 1071
  int32_t       nums = 0;
  TAOS_FIELD_E* pField = NULL;
D
dapan1121 已提交
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
  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 已提交
1087
TAOS_RES* stmtUseResult(TAOS_STMT* stmt) {
D
dapan1121 已提交
1088 1089
  STscStmt* pStmt = (STscStmt*)stmt;

1090 1091
  STMT_DLOG_E("start to use result");

D
dapan1121 已提交
1092 1093 1094 1095 1096 1097
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
    tscError("useResult only for query statement");
    return NULL;
  }

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