astToMsg.c 13.9 KB
Newer Older
H
Haojun Liao 已提交
1
#include "parserInt.h"
H
Haojun Liao 已提交
2
#include "astGenerator.h"
H
Haojun Liao 已提交
3
#include "parserUtil.h"
H
Haojun Liao 已提交
4

S
Shengliang Guan 已提交
5 6
SCreateUserReq* buildUserManipulationMsg(SSqlInfo* pInfo, int32_t* outputLen, int64_t id, char* msgBuf, int32_t msgLen) {
  SCreateUserReq* pMsg = (SCreateUserReq*)calloc(1, sizeof(SCreateUserReq));
H
Haojun Liao 已提交
7
  if (pMsg == NULL) {
H
Haojun Liao 已提交
8
    //    tscError("0x%" PRIx64 " failed to malloc for query msg", id);
H
Haojun Liao 已提交
9 10 11 12
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    return NULL;
  }

H
Haojun Liao 已提交
13
  SUserInfo* pUser = &pInfo->pMiscInfo->user;
H
Haojun Liao 已提交
14 15 16 17 18
  strncpy(pMsg->user, pUser->user.z, pUser->user.n);
  pMsg->type = pUser->type;
  pMsg->superUser = (int8_t)pUser->type;

  if (pUser->type == TSDB_ALTER_USER_PRIVILEGES) {
H
Haojun Liao 已提交
19
    //    pMsg->privilege = (char)pCmd->count;
H
Haojun Liao 已提交
20 21 22 23
  } else {
    strncpy(pMsg->pass, pUser->passwd.z, pUser->passwd.n);
  }

H
Haojun Liao 已提交
24
  *outputLen = sizeof(SUserInfo);
H
Haojun Liao 已提交
25
  return pMsg;
H
Haojun Liao 已提交
26 27
}

S
Shengliang Guan 已提交
28 29
SCreateAcctReq* buildAcctManipulationMsg(SSqlInfo* pInfo, int32_t* outputLen, int64_t id, char* msgBuf, int32_t msgLen) {
  SCreateAcctReq *pCreateMsg = (SCreateAcctReq *) calloc(1, sizeof(SCreateAcctReq));
30 31 32
  if (pCreateMsg == NULL) {
    qError("0x%" PRIx64 " failed to malloc for query msg", id);
    terrno = TSDB_CODE_QRY_OUT_OF_MEMORY;
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    return NULL;
  }

  SToken *pName = &pInfo->pMiscInfo->user.user;
  SToken *pPwd = &pInfo->pMiscInfo->user.passwd;

  strncpy(pCreateMsg->user, pName->z, pName->n);
  strncpy(pCreateMsg->pass, pPwd->z, pPwd->n);

  SCreateAcctInfo *pAcctOpt = &pInfo->pMiscInfo->acctOpt;

  pCreateMsg->maxUsers = htonl(pAcctOpt->maxUsers);
  pCreateMsg->maxDbs = htonl(pAcctOpt->maxDbs);
  pCreateMsg->maxTimeSeries = htonl(pAcctOpt->maxTimeSeries);
  pCreateMsg->maxStreams = htonl(pAcctOpt->maxStreams);
//  pCreateMsg->maxPointsPerSecond = htonl(pAcctOpt->maxPointsPerSecond);
  pCreateMsg->maxStorage = htobe64(pAcctOpt->maxStorage);
//  pCreateMsg->maxQueryTime = htobe64(pAcctOpt->maxQueryTime);
//  pCreateMsg->maxConnections = htonl(pAcctOpt->maxConnections);

  if (pAcctOpt->stat.n == 0) {
    pCreateMsg->accessState = -1;
  } else {
    if (pAcctOpt->stat.z[0] == 'r' && pAcctOpt->stat.n == 1) {
      pCreateMsg->accessState = TSDB_VN_READ_ACCCESS;
    } else if (pAcctOpt->stat.z[0] == 'w' && pAcctOpt->stat.n == 1) {
      pCreateMsg->accessState = TSDB_VN_WRITE_ACCCESS;
    } else if (strncmp(pAcctOpt->stat.z, "all", 3) == 0 && pAcctOpt->stat.n == 3) {
      pCreateMsg->accessState = TSDB_VN_ALL_ACCCESS;
    } else if (strncmp(pAcctOpt->stat.z, "no", 2) == 0 && pAcctOpt->stat.n == 2) {
      pCreateMsg->accessState = 0;
    }
  }

S
Shengliang Guan 已提交
67
  *outputLen = sizeof(SCreateAcctReq);
68
  return pCreateMsg;
69
}
70

S
Shengliang Guan 已提交
71
SDropUserReq* buildDropUserMsg(SSqlInfo* pInfo, int32_t *msgLen, int64_t id, char* msgBuf, int32_t msgBufLen) {
H
Haojun Liao 已提交
72 73
  SToken* pName = taosArrayGet(pInfo->pMiscInfo->a, 0);
  if (pName->n >= TSDB_USER_LEN) {
H
Haojun Liao 已提交
74 75 76
    return NULL;
  }

S
Shengliang Guan 已提交
77
  SDropUserReq* pMsg = calloc(1, sizeof(SDropUserReq));
H
Haojun Liao 已提交
78
  if (pMsg == NULL) {
79
    terrno = TSDB_CODE_QRY_OUT_OF_MEMORY;
H
Haojun Liao 已提交
80 81 82 83
    return NULL;
  }

  strncpy(pMsg->user, pName->z, pName->n);
S
Shengliang Guan 已提交
84
  *msgLen = sizeof(SDropUserReq);
H
Haojun Liao 已提交
85
  return pMsg;
H
Haojun Liao 已提交
86 87
}

88
SShowReq* buildShowMsg(SShowInfo* pShowInfo, SParseContext *pCtx, SMsgBuf* pMsgBuf) {
S
Shengliang Guan 已提交
89
  SShowReq* pShowMsg = calloc(1, sizeof(SShowReq));
90 91 92 93
  if (pShowMsg == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return pShowMsg;
  }
H
Haojun Liao 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

  pShowMsg->type = pShowInfo->showType;
  if (pShowInfo->showType != TSDB_MGMT_TABLE_VNODES) {
    SToken* pPattern = &pShowInfo->pattern;
    if (pPattern->type > 0) {  // only show tables support wildcard query
      strncpy(pShowMsg->payload, pPattern->z, pPattern->n);
      pShowMsg->payloadLen = htons(pPattern->n);
    }
  } else {
    SToken* pEpAddr = &pShowInfo->prefix;
    assert(pEpAddr->n > 0 && pEpAddr->type > 0);

    strncpy(pShowMsg->payload, pEpAddr->z, pEpAddr->n);
    pShowMsg->payloadLen = htons(pEpAddr->n);
  }

110 111
  if (pShowInfo->showType == TSDB_MGMT_TABLE_STB || pShowInfo->showType == TSDB_MGMT_TABLE_VGROUP) {
    SName n = {0};
112 113 114 115 116 117 118 119 120 121 122 123

    if (pShowInfo->prefix.n > 0) {
      if (pShowInfo->prefix.n >= TSDB_DB_FNAME_LEN) {
        terrno = buildInvalidOperationMsg(pMsgBuf, "prefix name is too long");
        tfree(pShowMsg);
        return NULL;
      }
      tNameSetDbName(&n, pCtx->acctId, pShowInfo->prefix.z, pShowInfo->prefix.n);
    } else {
      tNameSetDbName(&n, pCtx->acctId, pCtx->db, strlen(pCtx->db));
    }

124 125 126
    tNameGetFullDbName(&n, pShowMsg->db);
  }

H
Haojun Liao 已提交
127 128 129
  return pShowMsg;
}

S
Shengliang Guan 已提交
130
static int32_t setKeepOption(SCreateDbReq* pMsg, const SCreateDbInfo* pCreateDb, SMsgBuf* pMsgBuf) {
H
Haojun Liao 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
  const char* msg1 = "invalid number of keep options";
  const char* msg2 = "invalid keep value";
  const char* msg3 = "invalid keep value, should be keep0 <= keep1 <= keep2";

  pMsg->daysToKeep0 = htonl(-1);
  pMsg->daysToKeep1 = htonl(-1);
  pMsg->daysToKeep2 = htonl(-1);

  SArray* pKeep = pCreateDb->keep;
  if (pKeep != NULL) {
    size_t s = taosArrayGetSize(pKeep);
#ifdef _STORAGE
    if (s >= 4 ||s <= 0) {
#else
    if (s != 1) {
#endif
      return buildInvalidOperationMsg(pMsgBuf, msg1);
    }

//    tListI* p0 = taosArrayGet(pKeep, 0);
//    tVariantListItem* p1 = (s > 1) ? taosArrayGet(pKeep, 1) : p0;
//    tVariantListItem* p2 = (s > 2) ? taosArrayGet(pKeep, 2) : p1;
//
//    if ((int32_t)p0->pVar.i64 <= 0 || (int32_t)p1->pVar.i64 <= 0 || (int32_t)p2->pVar.i64 <= 0) {
//      return buildInvalidOperationMsg(pMsgBuf, msg2);
//    }
//    if (!(((int32_t)p0->pVar.i64 <= (int32_t)p1->pVar.i64) && ((int32_t)p1->pVar.i64 <= (int32_t)p2->pVar.i64))) {
//      return buildInvalidOperationMsg(pMsgBuf, msg3);
//    }
//
//    pMsg->daysToKeep0 = htonl((int32_t)p0->pVar.i64);
//    pMsg->daysToKeep1 = htonl((int32_t)p1->pVar.i64);
//    pMsg->daysToKeep2 = htonl((int32_t)p2->pVar.i64);
  }

  return TSDB_CODE_SUCCESS;
}

S
Shengliang Guan 已提交
169
static int32_t setTimePrecision(SCreateDbReq* pMsg, const SCreateDbInfo* pCreateDbInfo, SMsgBuf* pMsgBuf) {
H
Haojun Liao 已提交
170 171 172 173
  const char* msg = "invalid time precision";

  pMsg->precision = TSDB_TIME_PRECISION_MILLI;  // millisecond by default

H
Haojun Liao 已提交
174
  SToken* pToken = (SToken*) &pCreateDbInfo->precision;
H
Haojun Liao 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
  if (pToken->n > 0) {
    pToken->n = strdequote(pToken->z);

    if (strncmp(pToken->z, TSDB_TIME_PRECISION_MILLI_STR, pToken->n) == 0 &&
        strlen(TSDB_TIME_PRECISION_MILLI_STR) == pToken->n) {
      // time precision for this db: million second
      pMsg->precision = TSDB_TIME_PRECISION_MILLI;
    } else if (strncmp(pToken->z, TSDB_TIME_PRECISION_MICRO_STR, pToken->n) == 0 &&
               strlen(TSDB_TIME_PRECISION_MICRO_STR) == pToken->n) {
      pMsg->precision = TSDB_TIME_PRECISION_MICRO;
    } else if (strncmp(pToken->z, TSDB_TIME_PRECISION_NANO_STR, pToken->n) == 0 &&
               strlen(TSDB_TIME_PRECISION_NANO_STR) == pToken->n) {
      pMsg->precision = TSDB_TIME_PRECISION_NANO;
    } else {
      return buildInvalidOperationMsg(pMsgBuf, msg);
    }
  }

  return TSDB_CODE_SUCCESS;
}

S
Shengliang Guan 已提交
196
static void doSetDbOptions(SCreateDbReq* pMsg, const SCreateDbInfo* pCreateDb) {
H
Haojun Liao 已提交
197
  pMsg->cacheBlockSize = htonl(pCreateDb->cacheBlockSize);
H
Haojun Liao 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211
  pMsg->totalBlocks    = htonl(pCreateDb->numOfBlocks);
  pMsg->daysPerFile    = htonl(pCreateDb->daysPerFile);
  pMsg->commitTime     = htonl((int32_t)pCreateDb->commitTime);
  pMsg->minRows        = htonl(pCreateDb->minRowsPerBlock);
  pMsg->maxRows        = htonl(pCreateDb->maxRowsPerBlock);
  pMsg->fsyncPeriod    = htonl(pCreateDb->fsyncPeriod);
  pMsg->compression    = (int8_t) pCreateDb->compressionLevel;
  pMsg->walLevel       = (char)pCreateDb->walLevel;
  pMsg->replications   = pCreateDb->replica;
  pMsg->quorum         = pCreateDb->quorum;
  pMsg->ignoreExist    = pCreateDb->ignoreExists;
  pMsg->update         = pCreateDb->update;
  pMsg->cacheLastRow   = pCreateDb->cachelast;
  pMsg->numOfVgroups   = htonl(pCreateDb->numOfVgroups);
H
Haojun Liao 已提交
212 213
}

S
Shengliang Guan 已提交
214
int32_t setDbOptions(SCreateDbReq* pCreateDbMsg, const SCreateDbInfo* pCreateDbSql, SMsgBuf* pMsgBuf) {
H
Haojun Liao 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227
  doSetDbOptions(pCreateDbMsg, pCreateDbSql);

  if (setKeepOption(pCreateDbMsg, pCreateDbSql, pMsgBuf) != TSDB_CODE_SUCCESS) {
    return TSDB_CODE_TSC_INVALID_OPERATION;
  }

  if (setTimePrecision(pCreateDbMsg, pCreateDbSql, pMsgBuf) != TSDB_CODE_SUCCESS) {
    return TSDB_CODE_TSC_INVALID_OPERATION;
  }

  return TSDB_CODE_SUCCESS;
}

H
Haojun Liao 已提交
228
SCreateDbReq* buildCreateDbMsg(SCreateDbInfo* pCreateDbInfo, SParseContext *pCtx, SMsgBuf* pMsgBuf) {
S
Shengliang Guan 已提交
229
  SCreateDbReq* pCreateMsg = calloc(1, sizeof(SCreateDbReq));
230
  if (setDbOptions(pCreateMsg, pCreateDbInfo, pMsgBuf) != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
231
    tfree(pCreateMsg);
H
Haojun Liao 已提交
232 233 234
    terrno = TSDB_CODE_TSC_INVALID_OPERATION;

    return NULL;
H
Haojun Liao 已提交
235 236
  }

237 238 239 240 241 242 243 244
  SName   name = {0};
  int32_t ret = tNameSetDbName(&name, pCtx->acctId, pCreateDbInfo->dbname.z, pCreateDbInfo->dbname.n);
  if (ret != TSDB_CODE_SUCCESS) {
    terrno = ret;
    return NULL;
  }

  tNameGetFullDbName(&name, pCreateMsg->db);
H
Haojun Liao 已提交
245 246
  return pCreateMsg;
}
247

H
Haojun Liao 已提交
248
SMCreateStbReq* buildCreateStbMsg(SCreateTableSql* pCreateTableSql, int32_t* len, SParseContext* pParseCtx, SMsgBuf* pMsgBuf) {
249 250
  SSchema* pSchema;

251
  int32_t numOfTags = 0;
252
  int32_t numOfCols = (int32_t) taosArrayGetSize(pCreateTableSql->colInfo.pColumns);
253 254 255
  if (pCreateTableSql->colInfo.pTagColumns != NULL) {
    numOfTags = (int32_t) taosArrayGetSize(pCreateTableSql->colInfo.pTagColumns);
  }
256

S
Shengliang Guan 已提交
257
  SMCreateStbReq* pCreateStbMsg = (SMCreateStbReq*)calloc(1, sizeof(SMCreateStbReq) + (numOfCols + numOfTags) * sizeof(SSchema));
258 259 260
  if (pCreateStbMsg == NULL) {
    return NULL;
  }
261 262

  char* pMsg = NULL;
263
#if 0
264 265
  int32_t tableType = pCreateTableSql->type;
  if (tableType != TSQL_CREATE_TABLE && tableType != TSQL_CREATE_STABLE) {  // create by using super table, tags value
266 267 268
    SArray* list = pInfo->pCreateTableInfo->childTableInfo;

    int32_t numOfTables = (int32_t)taosArrayGetSize(list);
269
    pCreateStbMsg->numOfTables = htonl(numOfTables);
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288

    pMsg = (char*)pCreateMsg;
    for (int32_t i = 0; i < numOfTables; ++i) {
      SCreateTableMsg* pCreate = (SCreateTableMsg*)pMsg;

      pCreate->numOfColumns = htons(pCmd->numOfCols);
      pCreate->numOfTags = htons(pCmd->count);
      pMsg += sizeof(SCreateTableMsg);

      SCreatedTableInfo* p = taosArrayGet(list, i);
      strcpy(pCreate->tableName, p->fullname);
      pCreate->igExists = (p->igExist) ? 1 : 0;

      // use dbinfo from table id without modifying current db info
      pMsg = serializeTagData(&p->tagdata, pMsg);

      int32_t len = (int32_t)(pMsg - (char*)pCreate);
      pCreate->len = htonl(len);
    }
289 290

  } else {
291
#endif
292
    // create (super) table
293 294 295 296 297 298
    SName n = {0};
    int32_t code = createSName(&n, &pCreateTableSql->name, pParseCtx, pMsgBuf);
    if (code != 0) {
      return NULL;
    }

299
    code = tNameExtractFullName(&n, pCreateStbMsg->name);
300 301 302 303 304
    if (code != 0) {
      buildInvalidOperationMsg(pMsgBuf, "invalid table name or database not specified");
      return NULL;
    }

305 306 307
    pCreateStbMsg->igExists     = pCreateTableSql->existCheck ? 1 : 0;
    pCreateStbMsg->numOfColumns = htonl(numOfCols);
    pCreateStbMsg->numOfTags    = htonl(numOfTags);
308

309
    pSchema = (SSchema*)pCreateStbMsg->pSchema;
310
    for (int i = 0; i < numOfCols; ++i) {
311
      SField* pField = taosArrayGet(pCreateTableSql->colInfo.pColumns, i);
312 313 314 315 316 317 318 319
      pSchema->type  = pField->type;
      pSchema->bytes = htonl(pField->bytes);
      strcpy(pSchema->name, pField->name);

      pSchema++;
    }

    for(int32_t i = 0; i < numOfTags; ++i) {
320
      SField* pField = taosArrayGet(pCreateTableSql->colInfo.pTagColumns, i);
321 322 323 324 325 326 327 328 329
      pSchema->type  = pField->type;
      pSchema->bytes = htonl(pField->bytes);
      strcpy(pSchema->name, pField->name);

      pSchema++;
    }

    pMsg = (char*)pSchema;

330
  int32_t msgLen = (int32_t)(pMsg - (char*)pCreateStbMsg);
331 332
  *len = msgLen;

333
  return pCreateStbMsg;
334
}
335

H
Haojun Liao 已提交
336
SMDropStbReq* buildDropStableMsg(SSqlInfo* pInfo, int32_t* len, SParseContext* pParseCtx, SMsgBuf* pMsgBuf) {
337 338 339 340 341 342 343 344 345
  SToken* tableName = taosArrayGet(pInfo->pMiscInfo->a, 0);

  SName name = {0};
  int32_t code = createSName(&name, tableName, pParseCtx, pMsgBuf);
  if (code != TSDB_CODE_SUCCESS) {
    terrno = buildInvalidOperationMsg(pMsgBuf, "invalid table name");
    return NULL;
  }

S
Shengliang Guan 已提交
346
  SMDropStbReq *pDropTableMsg = (SMDropStbReq*) calloc(1, sizeof(SMDropStbReq));
347 348 349 350

  code = tNameExtractFullName(&name, pDropTableMsg->name);
  assert(code == TSDB_CODE_SUCCESS && name.type == TSDB_TABLE_NAME_T);

H
Haojun Liao 已提交
351
  pDropTableMsg->igNotExists = pInfo->pMiscInfo->existsCheck ? 1 : 0;
S
Shengliang Guan 已提交
352
  *len = sizeof(SMDropStbReq);
353 354 355
  return pDropTableMsg;
}

S
Shengliang Guan 已提交
356
SCreateDnodeReq *buildCreateDnodeMsg(SSqlInfo* pInfo, int32_t* len, SMsgBuf* pMsgBuf) {
357 358
  const char* msg1 = "invalid host name (name too long, maximum length 128)";
  const char* msg2 = "dnode name can not be string";
359
  const char* msg3 = "port should be an integer that is less than 65535 and greater than 0";
360
  const char* msg4 = "failed prepare create dnode message";
361

362
  if (taosArrayGetSize(pInfo->pMiscInfo->a) != 2) {
363 364 365 366 367
    buildInvalidOperationMsg(pMsgBuf, msg1);
    return NULL;
  }

  SToken* id = taosArrayGet(pInfo->pMiscInfo->a, 0);
368
  if (id->type != TK_ID && id->type != TK_IPTOKEN) {
369 370 371 372
    buildInvalidOperationMsg(pMsgBuf, msg2);
    return NULL;
  }

373 374 375 376 377 378 379 380 381 382
  SToken* port = taosArrayGet(pInfo->pMiscInfo->a, 1);
  if (port->type != TK_INTEGER) {
    buildInvalidOperationMsg(pMsgBuf, msg3);
    return NULL;
  }

  bool    isSign = false;
  int64_t val = 0;

  toInteger(port->z, port->n, 10, &val, &isSign);
383
  if (val >= UINT16_MAX || val <= 0) {
384 385 386 387
    buildInvalidOperationMsg(pMsgBuf, msg3);
    return NULL;
  }

S
Shengliang Guan 已提交
388
  SCreateDnodeReq *pCreate = (SCreateDnodeReq *) calloc(1, sizeof(SCreateDnodeReq));
389 390 391 392 393
  if (pCreate == NULL) {
    buildInvalidOperationMsg(pMsgBuf, msg4);
    return NULL;
  }

S
Shengliang Guan 已提交
394 395
  strncpy(pCreate->fqdn, id->z, id->n);
  pCreate->port = htonl(val);
396

S
Shengliang Guan 已提交
397
  *len = sizeof(SCreateDnodeReq);
398 399 400
  return pCreate;
}

S
Shengliang Guan 已提交
401
SDropDnodeReq *buildDropDnodeMsg(SSqlInfo* pInfo, int32_t* len, SMsgBuf* pMsgBuf) {
402 403 404
  SToken* pzName = taosArrayGet(pInfo->pMiscInfo->a, 0);

  char* end = NULL;
S
Shengliang Guan 已提交
405
  SDropDnodeReq * pDrop = (SDropDnodeReq *)calloc(1, sizeof(SDropDnodeReq));
406
  pDrop->dnodeId = strtoll(pzName->z, &end, 10);
S
Shengliang Guan 已提交
407
  pDrop->dnodeId = htonl(pDrop->dnodeId);
S
Shengliang Guan 已提交
408
  *len = sizeof(SDropDnodeReq);
409 410 411 412 413 414 415 416

  if (end - pzName->z != pzName->n) {
    buildInvalidOperationMsg(pMsgBuf, "invalid dnode id");
    tfree(pDrop);
    return NULL;
  }

  return pDrop;
417
}