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

S
slguan 已提交
16
#include "os.h"
17
#include "qSqlparser.h"
H
[td-32]  
hjxilinx 已提交
18
#include "taosdef.h"
H
hjxilinx 已提交
19
#include "taosmsg.h"
H
Haojun Liao 已提交
20
#include "tcmdtype.h"
H
hzcheng 已提交
21
#include "tstoken.h"
H
Haojun Liao 已提交
22
#include "tstrbuild.h"
23
#include "ttokendef.h"
H
hzcheng 已提交
24 25
#include "tutil.h"

26
SSqlInfo qSQLParse(const char *pStr) {
H
hzcheng 已提交
27
  void *pParser = ParseAlloc(malloc);
28 29 30

  SSqlInfo sqlInfo = {0};
  sqlInfo.valid = true;
H
hzcheng 已提交
31 32 33

  int32_t i = 0;
  while (1) {
H
Haojun Liao 已提交
34
    SStrToken t0 = {0};
H
hzcheng 已提交
35 36

    if (pStr[i] == 0) {
37
      Parse(pParser, 0, t0, &sqlInfo);
H
hzcheng 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50
      goto abort_parse;
    }

    t0.n = tSQLGetToken((char *)&pStr[i], &t0.type);
    t0.z = (char *)(pStr + i);
    i += t0.n;

    switch (t0.type) {
      case TK_SPACE:
      case TK_COMMENT: {
        break;
      }
      case TK_SEMI: {
51
        Parse(pParser, 0, t0, &sqlInfo);
H
hzcheng 已提交
52 53
        goto abort_parse;
      }
H
hjxilinx 已提交
54 55
      
      case TK_QUESTION:
H
hzcheng 已提交
56
      case TK_ILLEGAL: {
H
Haojun Liao 已提交
57
        snprintf(sqlInfo.msg, tListLen(sqlInfo.msg), "unrecognized token: \"%s\"", t0.z);
58
        sqlInfo.valid = false;
H
hzcheng 已提交
59 60 61
        goto abort_parse;
      }
      default:
62 63
        Parse(pParser, t0.type, t0, &sqlInfo);
        if (sqlInfo.valid == false) {
H
hzcheng 已提交
64 65 66 67 68 69 70
          goto abort_parse;
        }
    }
  }

abort_parse:
  ParseFree(pParser, free);
71
  return sqlInfo;
H
hzcheng 已提交
72 73
}

dengyihao's avatar
TD-2571  
dengyihao 已提交
74
tSQLExprList *tSqlExprListAppend(tSQLExprList *pList, tSQLExpr *pNode, SStrToken *pDistinct, SStrToken *pToken) {
H
hzcheng 已提交
75 76 77 78
  if (pList == NULL) {
    pList = calloc(1, sizeof(tSQLExprList));
  }

79
  if (pList->nAlloc <= pList->nExpr) {
H
Haojun Liao 已提交
80
    pList->nAlloc = (pList->nAlloc << 1u) + 4;
H
hzcheng 已提交
81 82 83 84 85 86 87 88 89
    pList->a = realloc(pList->a, pList->nAlloc * sizeof(pList->a[0]));
    if (pList->a == 0) {
      pList->nExpr = pList->nAlloc = 0;
      return pList;
    }
  }
  assert(pList->a != 0);

  if (pNode || pToken) {
H
Haojun Liao 已提交
90
    struct tSqlExprItem *pItem = &pList->a[pList->nExpr++];
H
hzcheng 已提交
91 92 93 94 95 96 97 98 99
    memset(pItem, 0, sizeof(*pItem));
    pItem->pNode = pNode;
    if (pToken) {  // set the as clause
      pItem->aliasName = malloc(pToken->n + 1);
      strncpy(pItem->aliasName, pToken->z, pToken->n);
      pItem->aliasName[pToken->n] = 0;

      strdequote(pItem->aliasName);
    }
dengyihao's avatar
TD-2571  
dengyihao 已提交
100
    pItem->distinct = (pDistinct != NULL);
H
hzcheng 已提交
101 102 103 104
  }
  return pList;
}

H
Haojun Liao 已提交
105
void tSqlExprListDestroy(tSQLExprList *pList) {
H
hzcheng 已提交
106 107 108 109 110 111
  if (pList == NULL) return;

  for (int32_t i = 0; i < pList->nExpr; ++i) {
    if (pList->a[i].aliasName != NULL) {
      free(pList->a[i].aliasName);
    }
H
Haojun Liao 已提交
112
    tSqlExprDestroy(pList->a[i].pNode);
H
hzcheng 已提交
113 114 115 116 117 118
  }

  free(pList->a);
  free(pList);
}

H
Haojun Liao 已提交
119 120
tSQLExpr *tSqlExprIdValueCreate(SStrToken *pToken, int32_t optrType) {
  tSQLExpr *pSqlExpr = calloc(1, sizeof(tSQLExpr));
H
Haojun Liao 已提交
121 122

  if (pToken != NULL) {
H
Haojun Liao 已提交
123
    pSqlExpr->token = *pToken;
H
Haojun Liao 已提交
124
  }
H
hzcheng 已提交
125 126

  if (optrType == TK_INTEGER || optrType == TK_STRING || optrType == TK_FLOAT || optrType == TK_BOOL) {
H
Haojun Liao 已提交
127
    toTSDBType(pToken->type);
H
hzcheng 已提交
128

H
Haojun Liao 已提交
129 130
    tVariantCreate(&pSqlExpr->val, pToken);
    pSqlExpr->nSQLOptr = optrType;
H
hzcheng 已提交
131
  } else if (optrType == TK_NOW) {
H
Haojun Liao 已提交
132
    // use microsecond by default
133
    pSqlExpr->val.i64 = taosGetTimestamp(TSDB_TIME_PRECISION_MICRO);
H
Haojun Liao 已提交
134 135
    pSqlExpr->val.nType = TSDB_DATA_TYPE_BIGINT;
    pSqlExpr->nSQLOptr = TK_TIMESTAMP;  // TK_TIMESTAMP used to denote the time value is in microsecond
H
hzcheng 已提交
136
  } else if (optrType == TK_VARIABLE) {
137
    int32_t ret = parseAbsoluteDuration(pToken->z, pToken->n, &pSqlExpr->val.i64);
H
Haojun Liao 已提交
138
    if (ret != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
139
      terrno = TSDB_CODE_TSC_SQL_SYNTAX_ERROR;
H
Haojun Liao 已提交
140
    }
H
hzcheng 已提交
141

H
Haojun Liao 已提交
142 143
    pSqlExpr->val.nType = TSDB_DATA_TYPE_BIGINT;
    pSqlExpr->nSQLOptr = TK_TIMESTAMP;
S
slguan 已提交
144 145
  } else {  // it must be the column name (tk_id) if it is not the number
    assert(optrType == TK_ID || optrType == TK_ALL);
H
Haojun Liao 已提交
146
    if (pToken != NULL) {
H
Haojun Liao 已提交
147
      pSqlExpr->colInfo = *pToken;
H
hzcheng 已提交
148 149
    }

H
Haojun Liao 已提交
150
    pSqlExpr->nSQLOptr = optrType;
H
hzcheng 已提交
151
  }
H
Haojun Liao 已提交
152

H
Haojun Liao 已提交
153
  return pSqlExpr;
H
hzcheng 已提交
154 155 156 157 158 159
}

/*
 * pList is the parameters for function with id(optType)
 * function name is denoted by pFunctionToken
 */
H
Haojun Liao 已提交
160
tSQLExpr *tSqlExprCreateFunction(tSQLExprList *pList, SStrToken *pFuncToken, SStrToken *endToken, int32_t optType) {
H
hzcheng 已提交
161 162 163 164 165 166
  if (pFuncToken == NULL) return NULL;

  tSQLExpr *pExpr = calloc(1, sizeof(tSQLExpr));
  pExpr->nSQLOptr = optType;
  pExpr->pParam = pList;

S
TD-1057  
Shengliang Guan 已提交
167
  int32_t len = (int32_t)((endToken->z + endToken->n) - pFuncToken->z);
H
hzcheng 已提交
168 169 170 171
  pExpr->operand.z = pFuncToken->z;

  pExpr->operand.n = len;  // raw field name
  pExpr->operand.type = pFuncToken->type;
H
Haojun Liao 已提交
172 173

  pExpr->token = pExpr->operand;
H
hzcheng 已提交
174 175 176 177 178 179 180
  return pExpr;
}

/*
 * create binary expression in this procedure
 * if the expr is arithmetic, calculate the result and set it to tSQLExpr Object
 */
H
Haojun Liao 已提交
181
tSQLExpr *tSqlExprCreate(tSQLExpr *pLeft, tSQLExpr *pRight, int32_t optrType) {
H
hzcheng 已提交
182 183
  tSQLExpr *pExpr = calloc(1, sizeof(tSQLExpr));

H
Haojun Liao 已提交
184
  if (pLeft != NULL && pRight != NULL && (optrType != TK_IN)) {
185 186
    char* endPos = pRight->token.z + pRight->token.n;
    pExpr->token.z = pLeft->token.z;
187
    pExpr->token.n = (uint32_t)(endPos - pExpr->token.z);
188 189
    pExpr->token.type = pLeft->token.type;
  }
H
Haojun Liao 已提交
190

H
Haojun Liao 已提交
191 192
  if ((pLeft != NULL && pRight != NULL) &&
      (optrType == TK_PLUS || optrType == TK_MINUS || optrType == TK_STAR || optrType == TK_DIVIDE || optrType == TK_REM)) {
H
hzcheng 已提交
193 194 195 196 197 198 199 200 201 202 203
    /*
     * if a token is noted as the TK_TIMESTAMP, the time precision is microsecond
     * Otherwise, the time precision is adaptive, determined by the time precision from databases.
     */
    if ((pLeft->nSQLOptr == TK_INTEGER && pRight->nSQLOptr == TK_INTEGER) ||
        (pLeft->nSQLOptr == TK_TIMESTAMP && pRight->nSQLOptr == TK_TIMESTAMP)) {
      pExpr->val.nType = TSDB_DATA_TYPE_BIGINT;
      pExpr->nSQLOptr = pLeft->nSQLOptr;

      switch (optrType) {
        case TK_PLUS: {
204
          pExpr->val.i64 = pLeft->val.i64 + pRight->val.i64;
H
hzcheng 已提交
205 206 207
          break;
        }
        case TK_MINUS: {
208
          pExpr->val.i64 = pLeft->val.i64 - pRight->val.i64;
H
hzcheng 已提交
209 210 211
          break;
        }
        case TK_STAR: {
212
          pExpr->val.i64 = pLeft->val.i64 * pRight->val.i64;
H
hzcheng 已提交
213 214 215 216 217
          break;
        }
        case TK_DIVIDE: {
          pExpr->nSQLOptr = TK_FLOAT;
          pExpr->val.nType = TSDB_DATA_TYPE_DOUBLE;
218
          pExpr->val.dKey = (double)pLeft->val.i64 / pRight->val.i64;
H
hzcheng 已提交
219 220 221
          break;
        }
        case TK_REM: {
222
          pExpr->val.i64 = pLeft->val.i64 % pRight->val.i64;
H
hzcheng 已提交
223 224 225 226
          break;
        }
      }

H
Haojun Liao 已提交
227 228
      tSqlExprDestroy(pLeft);
      tSqlExprDestroy(pRight);
H
hzcheng 已提交
229

H
Haojun Liao 已提交
230 231
    } else if ((pLeft->nSQLOptr == TK_FLOAT && pRight->nSQLOptr == TK_INTEGER) || (pLeft->nSQLOptr == TK_INTEGER && pRight->nSQLOptr == TK_FLOAT) ||
        (pLeft->nSQLOptr == TK_FLOAT && pRight->nSQLOptr == TK_FLOAT)) {
H
hzcheng 已提交
232
      pExpr->val.nType = TSDB_DATA_TYPE_DOUBLE;
233
      pExpr->nSQLOptr  = TK_FLOAT;
H
hzcheng 已提交
234

235 236
      double left  = (pLeft->val.nType == TSDB_DATA_TYPE_DOUBLE) ? pLeft->val.dKey : pLeft->val.i64;
      double right = (pRight->val.nType == TSDB_DATA_TYPE_DOUBLE) ? pRight->val.dKey : pRight->val.i64;
H
hzcheng 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260

      switch (optrType) {
        case TK_PLUS: {
          pExpr->val.dKey = left + right;
          break;
        }
        case TK_MINUS: {
          pExpr->val.dKey = left - right;
          break;
        }
        case TK_STAR: {
          pExpr->val.dKey = left * right;
          break;
        }
        case TK_DIVIDE: {
          pExpr->val.dKey = left / right;
          break;
        }
        case TK_REM: {
          pExpr->val.dKey = left - ((int64_t)(left / right)) * right;
          break;
        }
      }

H
Haojun Liao 已提交
261 262
      tSqlExprDestroy(pLeft);
      tSqlExprDestroy(pRight);
H
hzcheng 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280

    } else {
      pExpr->nSQLOptr = optrType;
      pExpr->pLeft = pLeft;
      pExpr->pRight = pRight;
    }
  } else if (optrType == TK_IN) {
    pExpr->nSQLOptr = optrType;
    pExpr->pLeft = pLeft;

    tSQLExpr *pRSub = calloc(1, sizeof(tSQLExpr));
    pRSub->nSQLOptr = TK_SET;  // TODO refactor .....
    pRSub->pParam = (tSQLExprList *)pRight;

    pExpr->pRight = pRSub;
  } else {
    pExpr->nSQLOptr = optrType;
    pExpr->pLeft = pLeft;
H
Haojun Liao 已提交
281

Y
yihaoDeng 已提交
282
    if (pLeft != NULL && pRight == NULL) {
H
Haojun Liao 已提交
283 284 285
      pRight = calloc(1, sizeof(tSQLExpr));
    }

H
hzcheng 已提交
286 287 288 289 290 291
    pExpr->pRight = pRight;
  }

  return pExpr;
}

D
dapan1121 已提交
292

D
dapan1121 已提交
293 294 295 296 297 298
static FORCE_INLINE int32_t tStrTokenCompare(SStrToken* left, SStrToken* right) {
  return (left->type == right->type && left->n == right->n && strncasecmp(left->z, right->z, left->n) == 0) ? 0 : 1;
}


int32_t tSqlExprCompare(tSQLExpr *left, tSQLExpr *right) {
D
dapan1121 已提交
299 300 301 302
  if ((left == NULL && right) || (left && right == NULL)) {
    return 1;
  }
  
D
dapan1121 已提交
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
  if (left->nSQLOptr != right->nSQLOptr) {
    return 1;
  }

  if ((left->pLeft && right->pLeft == NULL) 
    || (left->pLeft == NULL && right->pLeft)
    || (left->pRight && right->pRight == NULL) 
    || (left->pRight == NULL && right->pRight)
    || (left->pParam && right->pParam == NULL) 
    || (left->pParam == NULL && right->pParam)) {
    return 1;
  }

  if (tVariantCompare(&left->val, &right->val)) {
    return 1;
  }

  if (tStrTokenCompare(&left->colInfo, &right->colInfo)) {
    return 1;
  }

  if (left->pParam && left->pParam->nExpr != right->pParam->nExpr) {
    return 1;
  }
  
  for (int32_t i = 0; i < right->pParam->nExpr; i++) {
    tSQLExpr* pSubLeft = left->pParam->a[i].pNode;
    tSQLExpr* pSubRight = right->pParam->a[i].pNode;
  
    if (tSqlExprCompare(pSubLeft, pSubRight)) {
      return 1;
    }
  }

  if (left->pLeft && tSqlExprCompare(left->pLeft, right->pLeft)) {
    return 1;
  }

  if (left->pRight && tSqlExprCompare(left->pRight, right->pRight)) {
    return 1;
  }

  return 0;
}


D
dapan1121 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369

tSQLExpr *tSqlExprClone(tSQLExpr *pSrc) {
  tSQLExpr *pExpr = calloc(1, sizeof(tSQLExpr));

  memcpy(pExpr, pSrc, sizeof(*pSrc));
  
  if (pSrc->pLeft) {
    pExpr->pLeft = tSqlExprClone(pSrc->pLeft);
  }

  if (pSrc->pRight) {
    pExpr->pRight = tSqlExprClone(pSrc->pRight);
  }

  //we don't clone pParam now because clone is only used for between/and
  assert(pSrc->pParam == NULL);

  return pExpr;
}


H
Haojun Liao 已提交
370
void tSqlExprNodeDestroy(tSQLExpr *pExpr) {
S
slguan 已提交
371 372 373
  if (pExpr == NULL) {
    return;
  }
H
hzcheng 已提交
374 375 376 377 378

  if (pExpr->nSQLOptr == TK_STRING) {
    tVariantDestroy(&pExpr->val);
  }

H
Haojun Liao 已提交
379
  tSqlExprListDestroy(pExpr->pParam);
H
hzcheng 已提交
380 381 382 383

  free(pExpr);
}

H
Haojun Liao 已提交
384
void tSqlExprDestroy(tSQLExpr *pExpr) {
S
slguan 已提交
385 386 387 388
  if (pExpr == NULL) {
    return;
  }

H
Haojun Liao 已提交
389
  tSqlExprDestroy(pExpr->pLeft);
D
dapan1121 已提交
390
  pExpr->pLeft = NULL;
H
Haojun Liao 已提交
391
  tSqlExprDestroy(pExpr->pRight);
D
dapan1121 已提交
392
  pExpr->pRight = NULL;
H
Haojun Liao 已提交
393
  tSqlExprNodeDestroy(pExpr);
S
slguan 已提交
394 395
}

H
Haojun Liao 已提交
396
SArray *tVariantListAppendToken(SArray *pList, SStrToken *pToken, uint8_t order) {
H
hzcheng 已提交
397
  if (pList == NULL) {
H
Haojun Liao 已提交
398
    pList = taosArrayInit(4, sizeof(tVariantListItem));
H
hzcheng 已提交
399 400
  }

H
Haojun Liao 已提交
401
  if (pToken) {
H
Haojun Liao 已提交
402
    tVariantListItem item;
H
Haojun Liao 已提交
403 404
    tVariantCreate(&item.pVar, pToken);
    item.sortOrder = order;
H
hzcheng 已提交
405

H
Haojun Liao 已提交
406
    taosArrayPush(pList, &item);
H
hzcheng 已提交
407 408 409 410 411
  }

  return pList;
}

H
Haojun Liao 已提交
412
SArray *tVariantListAppend(SArray *pList, tVariant *pVar, uint8_t sortOrder) {
413
  if (pList == NULL) {
H
Haojun Liao 已提交
414
    pList = taosArrayInit(4, sizeof(tVariantListItem));
415 416
  }

H
Haojun Liao 已提交
417
  if (pVar == NULL) {
418 419 420
    return pList;
  }

H
Haojun Liao 已提交
421 422 423 424 425 426
  /*
   * Here we do not employ the assign function, since we need the pz attribute of structure
   * , which is the point to char string, to free it!
   *
   * Otherwise, the original pointer may be lost, which causes memory leak.
   */
H
Haojun Liao 已提交
427
  tVariantListItem item;
H
Haojun Liao 已提交
428 429
  item.pVar = *pVar;
  item.sortOrder = sortOrder;
430

H
Haojun Liao 已提交
431
  taosArrayPush(pList, &item);
432 433 434
  return pList;
}

H
Haojun Liao 已提交
435 436 437
SArray *tVariantListInsert(SArray *pList, tVariant *pVar, uint8_t sortOrder, int32_t index) {
  if (pList == NULL || pVar == NULL || index >= taosArrayGetSize(pList)) {
    return tVariantListAppend(NULL, pVar, sortOrder);
H
hzcheng 已提交
438 439
  }

H
Haojun Liao 已提交
440 441
  tVariantListItem item;

H
Haojun Liao 已提交
442 443
  item.pVar = *pVar;
  item.sortOrder = sortOrder;
H
hzcheng 已提交
444

H
Haojun Liao 已提交
445 446
  taosArrayInsert(pList, index, &item);
  return pList;
H
hzcheng 已提交
447 448
}

H
Haojun Liao 已提交
449 450 451 452
void setDbName(SStrToken *pCpxName, SStrToken *pDb) {
  pCpxName->type = pDb->type;
  pCpxName->z = pDb->z;
  pCpxName->n = pDb->n;
H
hzcheng 已提交
453 454
}

H
Haojun Liao 已提交
455
void tSqlSetColumnInfo(TAOS_FIELD *pField, SStrToken *pName, TAOS_FIELD *pType) {
H
hzcheng 已提交
456
  int32_t maxLen = sizeof(pField->name) / sizeof(pField->name[0]);
H
hjxilinx 已提交
457 458
  
  // truncate the column name
S
TD-1057  
Shengliang Guan 已提交
459
  if ((int32_t)pName->n >= maxLen) {
H
hzcheng 已提交
460 461 462 463 464 465 466
    pName->n = maxLen - 1;
  }

  strncpy(pField->name, pName->z, pName->n);
  pField->name[pName->n] = 0;

  pField->type = pType->type;
467
  if(!isValidDataType(pField->type)){
D
fix bug  
dapan1121 已提交
468 469 470 471
    pField->bytes = 0;
  } else {
    pField->bytes = pType->bytes;
  }
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
}

static int32_t tryParseNameTwoParts(SStrToken *type) {
  int32_t t = -1;

  char* str = strndup(type->z, type->n);
  if (str == NULL) {
    return t;
  }

  char* p = strtok(str, " ");
  if (p == NULL) {
    tfree(str);
    return t;
  } else {
    char* unsign = strtok(NULL, " ");
    if (unsign == NULL) {
      tfree(str);
      return t;
    }

    if (strncasecmp(unsign, "UNSIGNED", 8) == 0) {
      for(int32_t j = TSDB_DATA_TYPE_TINYINT; j <= TSDB_DATA_TYPE_BIGINT; ++j) {
        if (strcasecmp(p, tDataTypes[j].name) == 0) {
          t = j;
          break;
        }
      }
D
fix bug  
dapan1121 已提交
500

501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
      tfree(str);

      if (t == -1) {
        return -1;
      }

      switch(t) {
        case TSDB_DATA_TYPE_TINYINT:  return TSDB_DATA_TYPE_UTINYINT;
        case TSDB_DATA_TYPE_SMALLINT: return TSDB_DATA_TYPE_USMALLINT;
        case TSDB_DATA_TYPE_INT:      return TSDB_DATA_TYPE_UINT;
        case TSDB_DATA_TYPE_BIGINT:   return TSDB_DATA_TYPE_UBIGINT;
        default:
          return -1;
      }

    } else {
      tfree(str);
      return -1;
    }
  }
H
hzcheng 已提交
521 522
}

H
Haojun Liao 已提交
523
void tSqlSetColumnType(TAOS_FIELD *pField, SStrToken *type) {
524
  // set the field type invalid
H
hzcheng 已提交
525
  pField->type = -1;
526
  pField->name[0] = 0;
H
hzcheng 已提交
527

528
  int32_t i = 0;
H
Haojun Liao 已提交
529 530 531
  while (i < tListLen(tDataTypes)) {
    if ((type->n == tDataTypes[i].nameLen) &&
        (strncasecmp(type->z, tDataTypes[i].name, tDataTypes[i].nameLen) == 0)) {
H
hzcheng 已提交
532 533
      break;
    }
534 535 536 537

    i += 1;
  }

538
  // no qualified data type found, try unsigned data type
H
Haojun Liao 已提交
539
  if (i == tListLen(tDataTypes)) {
540 541 542 543
    i = tryParseNameTwoParts(type);
    if (i == -1) {
      return;
    }
544 545 546
  }

  pField->type = i;
H
Haojun Liao 已提交
547
  pField->bytes = tDataTypes[i].bytes;
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581

  if (i == TSDB_DATA_TYPE_NCHAR) {
    /*
     * for nchar, the TOKENTYPE is the number of character, so the length is the
     * number of bytes in UCS-4 format, which is 4 times larger than the number of characters
     */
    if (type->type == 0) {
      pField->bytes = 0;
    } else {
      int32_t bytes = -(int32_t)(type->type);
      if (bytes > (TSDB_MAX_NCHAR_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE) {
        // we have to postpone reporting the error because it cannot be done here
        // as pField->bytes is int16_t, use 'TSDB_MAX_NCHAR_LEN + 1' to avoid overflow
        bytes = TSDB_MAX_NCHAR_LEN + 1;
      } else {
        bytes = bytes * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE;
      }
      pField->bytes = (int16_t)bytes;
    }
  } else if (i == TSDB_DATA_TYPE_BINARY) {
    /* for binary, the TOKENTYPE is the length of binary */
    if (type->type == 0) {
      pField->bytes = 0;
    } else {
      int32_t bytes = -(int32_t)(type->type);
      if (bytes > TSDB_MAX_BINARY_LEN - VARSTR_HEADER_SIZE) {
        // refer comment for NCHAR above
        bytes = TSDB_MAX_BINARY_LEN + 1;
      } else {
        bytes += VARSTR_HEADER_SIZE;
      }

      pField->bytes = (int16_t)bytes;
    }
H
hzcheng 已提交
582 583 584 585 586 587
  }
}

/*
 * extract the select info out of sql string
 */
H
Haojun Liao 已提交
588
SQuerySQL *tSetQuerySqlElems(SStrToken *pSelectToken, tSQLExprList *pSelection, SArray *pFrom, tSQLExpr *pWhere,
H
Haojun Liao 已提交
589
                             SArray *pGroupby, SArray *pSortOrder, SIntervalVal *pInterval,
D
dapan1121 已提交
590
                             SStrToken *pSliding, SArray *pFill, SLimitVal *pLimit, SLimitVal *pGLimit, tSQLExpr *pHaving) {
H
hjxilinx 已提交
591
  assert(pSelection != NULL);
H
hzcheng 已提交
592 593 594

  SQuerySQL *pQuery = calloc(1, sizeof(SQuerySQL));
  pQuery->selectToken = *pSelectToken;
S
TD-1057  
Shengliang Guan 已提交
595
  pQuery->selectToken.n = (uint32_t)strlen(pQuery->selectToken.z);  // all later sql string are belonged to the stream sql
H
hzcheng 已提交
596 597

  pQuery->pSelection = pSelection;
S
slguan 已提交
598
  pQuery->from = pFrom;
H
hzcheng 已提交
599 600 601
  pQuery->pGroupby = pGroupby;
  pQuery->pSortOrder = pSortOrder;
  pQuery->pWhere = pWhere;
D
dapan1121 已提交
602
  pQuery->pHaving = pHaving;
H
hzcheng 已提交
603

H
hjxilinx 已提交
604 605 606
  if (pLimit != NULL) {
    pQuery->limit = *pLimit;
  }
H
hjxilinx 已提交
607

H
hjxilinx 已提交
608 609 610
  if (pGLimit != NULL) {
    pQuery->slimit = *pGLimit;
  }
H
hzcheng 已提交
611

H
hjxilinx 已提交
612
  if (pInterval != NULL) {
613 614
    pQuery->interval = pInterval->interval;
    pQuery->offset = pInterval->offset;
H
hjxilinx 已提交
615
  }
H
hjxilinx 已提交
616

H
hjxilinx 已提交
617 618 619
  if (pSliding != NULL) {
    pQuery->sliding = *pSliding;
  }
H
hjxilinx 已提交
620

H
hzcheng 已提交
621 622 623 624
  pQuery->fillType = pFill;
  return pQuery;
}

625
static void freeVariant(void *pItem) {
H
Haojun Liao 已提交
626 627 628 629
  tVariantListItem* p = (tVariantListItem*) pItem;
  tVariantDestroy(&p->pVar);
}

630
void freeCreateTableInfo(void* p) {
631 632
  SCreatedTableInfo* pInfo = (SCreatedTableInfo*) p;  
  taosArrayDestroy(pInfo->pTagNames);
633 634 635 636 637
  taosArrayDestroyEx(pInfo->pTagVals, freeVariant);
  tfree(pInfo->fullname);
  tfree(pInfo->tagdata.data);
}

638 639 640 641
void doDestroyQuerySql(SQuerySQL *pQuerySql) {
  if (pQuerySql == NULL) {
    return;
  }
H
Haojun Liao 已提交
642 643

  tSqlExprListDestroy(pQuerySql->pSelection);
644 645
  
  pQuerySql->pSelection = NULL;
H
Haojun Liao 已提交
646 647

  tSqlExprDestroy(pQuerySql->pWhere);
648
  pQuerySql->pWhere = NULL;
D
dapan1121 已提交
649 650 651

  tSqlExprDestroy(pQuerySql->pHaving);
  pQuerySql->pHaving = NULL;
652
  
H
Haojun Liao 已提交
653
  taosArrayDestroyEx(pQuerySql->pSortOrder, freeVariant);
654
  pQuerySql->pSortOrder = NULL;
H
Haojun Liao 已提交
655 656

  taosArrayDestroyEx(pQuerySql->pGroupby, freeVariant);
657
  pQuerySql->pGroupby = NULL;
H
Haojun Liao 已提交
658 659

  taosArrayDestroyEx(pQuerySql->from, freeVariant);
660
  pQuerySql->from = NULL;
H
Haojun Liao 已提交
661 662 663 664

  taosArrayDestroyEx(pQuerySql->fillType, freeVariant);
  pQuerySql->fillType = NULL;

665 666 667 668 669 670 671
  free(pQuerySql);
}

void destroyAllSelectClause(SSubclauseInfo *pClause) {
  if (pClause == NULL || pClause->numOfClause == 0) {
    return;
  }
H
hzcheng 已提交
672

673 674 675 676
  for(int32_t i = 0; i < pClause->numOfClause; ++i) {
    SQuerySQL *pQuerySql = pClause->pClause[i];
    doDestroyQuerySql(pQuerySql);
  }
677
  
S
TD-1848  
Shengliang Guan 已提交
678
  tfree(pClause->pClause);
H
hzcheng 已提交
679 680
}

H
Haojun Liao 已提交
681
SCreateTableSQL *tSetCreateSqlElems(SArray *pCols, SArray *pTags, SQuerySQL *pSelect, int32_t type) {
H
hzcheng 已提交
682 683 684
  SCreateTableSQL *pCreate = calloc(1, sizeof(SCreateTableSQL));

  switch (type) {
685
    case TSQL_CREATE_TABLE: {
H
hzcheng 已提交
686
      pCreate->colInfo.pColumns = pCols;
687
      assert(pTags == NULL);
H
hzcheng 已提交
688 689
      break;
    }
690
    case TSQL_CREATE_STABLE: {
H
hzcheng 已提交
691 692
      pCreate->colInfo.pColumns = pCols;
      pCreate->colInfo.pTagColumns = pTags;
693
      assert(pTags != NULL && pCols != NULL);
H
hzcheng 已提交
694 695 696 697 698 699
      break;
    }
    case TSQL_CREATE_STREAM: {
      pCreate->pSelect = pSelect;
      break;
    }
700 701 702 703 704

    case TSQL_CREATE_TABLE_FROM_STABLE: {
      assert(0);
    }

H
hzcheng 已提交
705 706 707 708
    default:
      assert(false);
  }

709
  pCreate->type = type;
H
hzcheng 已提交
710 711 712
  return pCreate;
}

713
SCreatedTableInfo createNewChildTableInfo(SStrToken *pTableName, SArray *pTagNames, SArray *pTagVals, SStrToken *pToken, SStrToken* igExists) {
H
Haojun Liao 已提交
714 715 716
  SCreatedTableInfo info;
  memset(&info, 0, sizeof(SCreatedTableInfo));

717
  info.name       = *pToken;
718
  info.pTagNames  = pTagNames;
719 720 721 722 723 724 725
  info.pTagVals   = pTagVals;
  info.stableName = *pTableName;
  info.igExist    = (igExists->n > 0)? 1:0;

  return info;
}

H
Haojun Liao 已提交
726
SAlterTableInfo *tAlterTableSqlElems(SStrToken *pTableName, SArray *pCols, SArray *pVals, int32_t type, int16_t tableType) {
H
Haojun Liao 已提交
727
  SAlterTableInfo *pAlterTable = calloc(1, sizeof(SAlterTableInfo));
728
  
729
  pAlterTable->name = *pTableName;
730
  pAlterTable->type = type;
D
dapan1121 已提交
731
  pAlterTable->tableType = tableType;
H
hzcheng 已提交
732

733
  if (type == TSDB_ALTER_TABLE_ADD_COLUMN || type == TSDB_ALTER_TABLE_ADD_TAG_COLUMN) {
H
hzcheng 已提交
734 735 736
    pAlterTable->pAddColumns = pCols;
    assert(pVals == NULL);
  } else {
737 738 739 740
    /*
     * ALTER_TABLE_TAGS_CHG, ALTER_TABLE_TAGS_SET, ALTER_TABLE_TAGS_DROP,
     * ALTER_TABLE_DROP_COLUMN
     */
H
hzcheng 已提交
741 742 743 744 745 746 747
    pAlterTable->varList = pVals;
    assert(pCols == NULL);
  }

  return pAlterTable;
}

H
Haojun Liao 已提交
748
void* destroyCreateTableSql(SCreateTableSQL* pCreate) {
749 750 751 752 753 754 755 756 757 758 759
  doDestroyQuerySql(pCreate->pSelect);

  taosArrayDestroy(pCreate->colInfo.pColumns);
  taosArrayDestroy(pCreate->colInfo.pTagColumns);

  taosArrayDestroyEx(pCreate->childTableInfo, freeCreateTableInfo);
  tfree(pCreate);

  return NULL;
}

H
Haojun Liao 已提交
760
void SqlInfoDestroy(SSqlInfo *pInfo) {
H
hzcheng 已提交
761 762
  if (pInfo == NULL) return;

763 764 765
  if (pInfo->type == TSDB_SQL_SELECT) {
    destroyAllSelectClause(&pInfo->subclauseInfo);
  } else if (pInfo->type == TSDB_SQL_CREATE_TABLE) {
H
Haojun Liao 已提交
766
    pInfo->pCreateTableInfo = destroyCreateTableSql(pInfo->pCreateTableInfo);
767
  } else if (pInfo->type == TSDB_SQL_ALTER_TABLE) {
H
Haojun Liao 已提交
768 769
    taosArrayDestroyEx(pInfo->pAlterInfo->varList, freeVariant);
    taosArrayDestroy(pInfo->pAlterInfo->pAddColumns);
770
    tfree(pInfo->pAlterInfo->tagData.data);
S
TD-1848  
Shengliang Guan 已提交
771
    tfree(pInfo->pAlterInfo);
H
hzcheng 已提交
772
  } else {
H
Haojun Liao 已提交
773 774
    if (pInfo->pMiscInfo != NULL) {
      taosArrayDestroy(pInfo->pMiscInfo->a);
H
hzcheng 已提交
775 776
    }

H
Haojun Liao 已提交
777 778
    if (pInfo->pMiscInfo != NULL && pInfo->type == TSDB_SQL_CREATE_DB) {
      taosArrayDestroyEx(pInfo->pMiscInfo->dbOpt.keep, freeVariant);
H
hzcheng 已提交
779 780
    }

H
Haojun Liao 已提交
781
    tfree(pInfo->pMiscInfo);
H
hzcheng 已提交
782 783 784
  }
}

785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
SSubclauseInfo* setSubclause(SSubclauseInfo* pSubclause, void *pSqlExprInfo) {
  if (pSubclause == NULL) {
    pSubclause = calloc(1, sizeof(SSubclauseInfo));
  }
  
  int32_t newSize = pSubclause->numOfClause + 1;
  char* tmp = realloc(pSubclause->pClause, newSize * POINTER_BYTES);
  if (tmp == NULL) {
    return pSubclause;
  }
  
  pSubclause->pClause = (SQuerySQL**) tmp;
  
  pSubclause->pClause[newSize - 1] = pSqlExprInfo;
  pSubclause->numOfClause++;
  
  return pSubclause;
}
H
hzcheng 已提交
803

H
Haojun Liao 已提交
804
SSqlInfo*setSqlInfo(SSqlInfo *pInfo, void *pSqlExprInfo, SStrToken *pTableName, int32_t type) {
805 806 807 808 809 810 811 812 813
  pInfo->type = type;
  
  if (type == TSDB_SQL_SELECT) {
    pInfo->subclauseInfo = *(SSubclauseInfo*) pSqlExprInfo;
    free(pSqlExprInfo);
  } else {
    pInfo->pCreateTableInfo = pSqlExprInfo;
  }
  
814 815
  if (pTableName != NULL) {
    pInfo->pCreateTableInfo->name = *pTableName;
H
hzcheng 已提交
816
  }
817 818 819 820 821 822 823 824 825 826 827 828 829 830
  
  return pInfo;
}

SSubclauseInfo* appendSelectClause(SSubclauseInfo *pQueryInfo, void *pSubclause) {
  char* tmp = realloc(pQueryInfo->pClause, (pQueryInfo->numOfClause + 1) * POINTER_BYTES);
  if (tmp == NULL) {  // out of memory
    return pQueryInfo;
  }
  
  pQueryInfo->pClause = (SQuerySQL**) tmp;
  pQueryInfo->pClause[pQueryInfo->numOfClause++] = pSubclause;
  
  return pQueryInfo;
H
hzcheng 已提交
831 832
}

833 834
void setCreatedTableName(SSqlInfo *pInfo, SStrToken *pTableNameToken, SStrToken *pIfNotExists) {
  pInfo->pCreateTableInfo->name = *pTableNameToken;
H
hzcheng 已提交
835 836 837 838
  pInfo->pCreateTableInfo->existCheck = (pIfNotExists->n != 0);
}

void setDCLSQLElems(SSqlInfo *pInfo, int32_t type, int32_t nParam, ...) {
839
  pInfo->type = type;
H
Haojun Liao 已提交
840 841 842
  if (nParam == 0) {
    return;
  }
H
hzcheng 已提交
843

H
Haojun Liao 已提交
844 845 846 847
  if (pInfo->pMiscInfo == NULL) {
    pInfo->pMiscInfo = (SMiscInfo *)calloc(1, sizeof(SMiscInfo));
    pInfo->pMiscInfo->a = taosArrayInit(4, sizeof(SStrToken));
  }
H
hzcheng 已提交
848 849 850 851

  va_list va;
  va_start(va, nParam);

H
Haojun Liao 已提交
852
  while ((nParam--) > 0) {
H
Haojun Liao 已提交
853
    SStrToken *pToken = va_arg(va, SStrToken *);
H
Haojun Liao 已提交
854
    taosArrayPush(pInfo->pMiscInfo->a, pToken);
H
hzcheng 已提交
855 856 857 858
  }
  va_end(va);
}

D
dapan1121 已提交
859
void setDropDbTableInfo(SSqlInfo *pInfo, int32_t type, SStrToken* pToken, SStrToken* existsCheck, int16_t tableType) {
860
  pInfo->type = type;
H
Haojun Liao 已提交
861 862 863 864 865 866 867 868

  if (pInfo->pMiscInfo == NULL) {
    pInfo->pMiscInfo = (SMiscInfo *)calloc(1, sizeof(SMiscInfo));
    pInfo->pMiscInfo->a = taosArrayInit(4, sizeof(SStrToken));
  }

  taosArrayPush(pInfo->pMiscInfo->a, pToken);

H
Haojun Liao 已提交
869
  pInfo->pMiscInfo->existsCheck = (existsCheck->n == 1);
H
Haojun Liao 已提交
870
  pInfo->pMiscInfo->tableType = tableType;
871 872
}

H
Haojun Liao 已提交
873
void setShowOptions(SSqlInfo *pInfo, int32_t type, SStrToken* prefix, SStrToken* pPatterns) {
H
Haojun Liao 已提交
874 875
  if (pInfo->pMiscInfo == NULL) {
    pInfo->pMiscInfo = calloc(1, sizeof(SMiscInfo));
876 877 878 879
  }
  
  pInfo->type = TSDB_SQL_SHOW;
  
H
Haojun Liao 已提交
880
  SShowInfo* pShowInfo = &pInfo->pMiscInfo->showOpt;
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
  pShowInfo->showType = type;
  
  if (prefix != NULL && prefix->type != 0) {
    pShowInfo->prefix = *prefix;
  } else {
    pShowInfo->prefix.type = 0;
  }
  
  if (pPatterns != NULL && pPatterns->type != 0) {
    pShowInfo->pattern = *pPatterns;
  } else {
    pShowInfo->pattern.type = 0;
  }
}

H
Haojun Liao 已提交
896
void setCreateDbInfo(SSqlInfo *pInfo, int32_t type, SStrToken *pToken, SCreateDbInfo *pDB, SStrToken *pIgExists) {
897
  pInfo->type = type;
H
Haojun Liao 已提交
898 899
  if (pInfo->pMiscInfo == NULL) {
    pInfo->pMiscInfo = calloc(1, sizeof(SMiscInfo));
H
hzcheng 已提交
900 901
  }

H
Haojun Liao 已提交
902 903 904
  pInfo->pMiscInfo->dbOpt = *pDB;
  pInfo->pMiscInfo->dbOpt.dbname = *pToken;
  pInfo->pMiscInfo->dbOpt.ignoreExists = pIgExists->n; // sql.y has: ifnotexists(X) ::= IF NOT EXISTS.   {X.n = 1;}
H
hzcheng 已提交
905 906
}

H
Haojun Liao 已提交
907
void setCreateAcctSql(SSqlInfo *pInfo, int32_t type, SStrToken *pName, SStrToken *pPwd, SCreateAcctInfo *pAcctInfo) {
908
  pInfo->type = type;
H
Haojun Liao 已提交
909 910
  if (pInfo->pMiscInfo == NULL) {
    pInfo->pMiscInfo = calloc(1, sizeof(SMiscInfo));
H
hzcheng 已提交
911 912
  }

H
Haojun Liao 已提交
913
  pInfo->pMiscInfo->acctOpt = *pAcctInfo;
914 915
  
  assert(pName != NULL);
H
Haojun Liao 已提交
916
  pInfo->pMiscInfo->user.user = *pName;
917 918
  
  if (pPwd != NULL) {
H
Haojun Liao 已提交
919
    pInfo->pMiscInfo->user.passwd = *pPwd;
920 921
  }
}
H
hzcheng 已提交
922

H
Haojun Liao 已提交
923
void setCreateUserSql(SSqlInfo *pInfo, SStrToken *pName, SStrToken *pPasswd) {
924
  pInfo->type = TSDB_SQL_CREATE_USER;
H
Haojun Liao 已提交
925 926
  if (pInfo->pMiscInfo == NULL) {
    pInfo->pMiscInfo = calloc(1, sizeof(SMiscInfo));
927 928 929 930
  }
  
  assert(pName != NULL && pPasswd != NULL);
  
H
Haojun Liao 已提交
931 932
  pInfo->pMiscInfo->user.user = *pName;
  pInfo->pMiscInfo->user.passwd = *pPasswd;
933
}
H
hzcheng 已提交
934

H
Haojun Liao 已提交
935
void setAlterUserSql(SSqlInfo *pInfo, int16_t type, SStrToken *pName, SStrToken* pPwd, SStrToken *pPrivilege) {
936
  pInfo->type = TSDB_SQL_ALTER_USER;
H
Haojun Liao 已提交
937 938
  if (pInfo->pMiscInfo == NULL) {
    pInfo->pMiscInfo = calloc(1, sizeof(SMiscInfo));
939 940 941 942
  }
  
  assert(pName != NULL);
  
H
Haojun Liao 已提交
943
  SUserInfo* pUser = &pInfo->pMiscInfo->user;
944 945 946 947 948
  pUser->type = type;
  pUser->user = *pName;
  
  if (pPwd != NULL) {
    pUser->passwd = *pPwd;
L
lihui 已提交
949 950
  } else {
    pUser->passwd.type = TSDB_DATA_TYPE_NULL;
951 952 953 954
  }
  
  if (pPrivilege != NULL) {
    pUser->privilege = *pPrivilege;
L
lihui 已提交
955 956
  } else {
    pUser->privilege.type = TSDB_DATA_TYPE_NULL;
957 958 959
  }
}

H
Haojun Liao 已提交
960
void setKillSql(SSqlInfo *pInfo, int32_t type, SStrToken *id) {
961
  pInfo->type = type;
H
Haojun Liao 已提交
962 963
  if (pInfo->pMiscInfo == NULL) {
    pInfo->pMiscInfo = calloc(1, sizeof(SMiscInfo));
H
hzcheng 已提交
964
  }
965
  
H
Haojun Liao 已提交
966 967
  assert(id != NULL);
  pInfo->pMiscInfo->id = *id;
H
hzcheng 已提交
968
}
H
hjxilinx 已提交
969

H
Haojun Liao 已提交
970
void setDefaultCreateDbOption(SCreateDbInfo *pDBInfo) {
H
hjxilinx 已提交
971 972
  pDBInfo->compressionLevel = -1;

H
hjxilinx 已提交
973
  pDBInfo->walLevel = -1;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
974
  pDBInfo->fsyncPeriod = -1;
H
hjxilinx 已提交
975
  pDBInfo->commitTime = -1;
H
hjxilinx 已提交
976
  pDBInfo->maxTablesPerVnode = -1;
H
hjxilinx 已提交
977 978

  pDBInfo->cacheBlockSize = -1;
H
hjxilinx 已提交
979 980 981
  pDBInfo->numOfBlocks = -1;
  pDBInfo->maxRowsPerBlock = -1;
  pDBInfo->minRowsPerBlock = -1;
H
hjxilinx 已提交
982 983 984
  pDBInfo->daysPerFile = -1;

  pDBInfo->replica = -1;
985
  pDBInfo->quorum = -1;
H
hjxilinx 已提交
986 987
  pDBInfo->keep = NULL;

H
Hongze Cheng 已提交
988
  pDBInfo->update = -1;
D
dapan1121 已提交
989
  pDBInfo->cachelast = 0;
H
Haojun Liao 已提交
990
  memset(&pDBInfo->precision, 0, sizeof(SStrToken));
991
}