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

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

H
Haojun Liao 已提交
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
      goto abort_parse;
    }

41
    t0.n = tGetToken((char *)&pStr[i], &t0.type);
H
hzcheng 已提交
42 43 44 45 46 47 48 49 50
    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
        goto abort_parse;
      }
D
fix bug  
dapan1121 已提交
61 62 63 64 65 66 67 68 69
      
      case TK_HEX:
      case TK_OCT:
      case TK_BIN:{
        snprintf(sqlInfo.msg, tListLen(sqlInfo.msg), "unsupported token: \"%s\"", t0.z);
        sqlInfo.valid = false;
        goto abort_parse;
      }
        
H
hzcheng 已提交
70
      default:
71 72
        Parse(pParser, t0.type, t0, &sqlInfo);
        if (sqlInfo.valid == false) {
H
hzcheng 已提交
73 74 75 76 77 78 79
          goto abort_parse;
        }
    }
  }

abort_parse:
  ParseFree(pParser, free);
80
  return sqlInfo;
H
hzcheng 已提交
81 82
}

H
Haojun Liao 已提交
83
SArray *tSqlExprListAppend(SArray *pList, tSqlExpr *pNode, SStrToken *pDistinct, SStrToken *pToken) {
H
hzcheng 已提交
84
  if (pList == NULL) {
H
Haojun Liao 已提交
85
    pList = taosArrayInit(4, sizeof(tSqlExprItem));
H
hzcheng 已提交
86 87 88
  }

  if (pNode || pToken) {
H
Haojun Liao 已提交
89 90 91 92 93
    struct tSqlExprItem item = {0};

    item.pNode = pNode;
    item.distinct = (pDistinct != NULL);

H
hzcheng 已提交
94
    if (pToken) {  // set the as clause
H
Haojun Liao 已提交
95 96 97
      item.aliasName = malloc(pToken->n + 1);
      strncpy(item.aliasName, pToken->z, pToken->n);
      item.aliasName[pToken->n] = 0;
H
hzcheng 已提交
98

H
Haojun Liao 已提交
99
      strdequote(item.aliasName);
H
hzcheng 已提交
100
    }
H
Haojun Liao 已提交
101 102

    taosArrayPush(pList, &item);
H
hzcheng 已提交
103
  }
H
Haojun Liao 已提交
104

H
hzcheng 已提交
105 106 107
  return pList;
}

H
Haojun Liao 已提交
108 109
static void freeExprElem(void* item) {
  tSqlExprItem* exprItem = item;
H
hzcheng 已提交
110

H
Haojun Liao 已提交
111 112 113 114 115 116 117
  tfree(exprItem->aliasName);
  tSqlExprDestroy(exprItem->pNode);
}

void tSqlExprListDestroy(SArray *pList) {
  if (pList == NULL) {
    return;
H
hzcheng 已提交
118 119
  }

H
Haojun Liao 已提交
120
  taosArrayDestroyEx(pList, freeExprElem);
H
hzcheng 已提交
121 122
}

H
Haojun Liao 已提交
123 124
tSqlExpr *tSqlExprCreateIdValue(SStrToken *pToken, int32_t optrType) {
  tSqlExpr *pSqlExpr = calloc(1, sizeof(tSqlExpr));
H
Haojun Liao 已提交
125 126

  if (pToken != NULL) {
H
Haojun Liao 已提交
127
    pSqlExpr->token = *pToken;
H
Haojun Liao 已提交
128
  }
H
hzcheng 已提交
129

130 131 132 133 134 135
  if (optrType == TK_NULL) {
    pToken->type = TSDB_DATA_TYPE_NULL;
    tVariantCreate(&pSqlExpr->value, pToken);
    pSqlExpr->tokenId = optrType;
    pSqlExpr->type    = SQL_NODE_VALUE;
  } else if (optrType == TK_INTEGER || optrType == TK_STRING || optrType == TK_FLOAT || optrType == TK_BOOL) {
H
Haojun Liao 已提交
136
    toTSDBType(pToken->type);
H
hzcheng 已提交
137

H
Haojun Liao 已提交
138 139 140
    tVariantCreate(&pSqlExpr->value, pToken);
    pSqlExpr->tokenId = optrType;
    pSqlExpr->type    = SQL_NODE_VALUE;
H
hzcheng 已提交
141
  } else if (optrType == TK_NOW) {
H
Haojun Liao 已提交
142
    // use microsecond by default
H
Haojun Liao 已提交
143 144 145 146
    pSqlExpr->value.i64 = taosGetTimestamp(TSDB_TIME_PRECISION_MICRO);
    pSqlExpr->value.nType = TSDB_DATA_TYPE_BIGINT;
    pSqlExpr->tokenId = TK_TIMESTAMP;  // TK_TIMESTAMP used to denote the time value is in microsecond
    pSqlExpr->type    = SQL_NODE_VALUE;
D
fix bug  
dapan1121 已提交
147
    pSqlExpr->flags  |= 1 << EXPR_FLAG_US_TIMESTAMP;
H
hzcheng 已提交
148
  } else if (optrType == TK_VARIABLE) {
H
Haojun Liao 已提交
149
    int32_t ret = parseAbsoluteDuration(pToken->z, pToken->n, &pSqlExpr->value.i64);
H
Haojun Liao 已提交
150
    if (ret != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
151
      terrno = TSDB_CODE_TSC_SQL_SYNTAX_ERROR;
H
Haojun Liao 已提交
152
    }
H
hzcheng 已提交
153

D
fix bug  
dapan1121 已提交
154
    pSqlExpr->flags  |= 1 << EXPR_FLAG_US_TIMESTAMP;
D
dapan1121 已提交
155
    pSqlExpr->flags  |= 1 << EXPR_FLAG_TIMESTAMP_VAR;
H
Haojun Liao 已提交
156 157 158 159 160
    pSqlExpr->value.nType = TSDB_DATA_TYPE_BIGINT;
    pSqlExpr->tokenId = TK_TIMESTAMP;
    pSqlExpr->type    = SQL_NODE_VALUE;
  } else {
    // Here it must be the column name (tk_id) if it is not a number or string.
S
slguan 已提交
161
    assert(optrType == TK_ID || optrType == TK_ALL);
H
Haojun Liao 已提交
162
    if (pToken != NULL) {
H
Haojun Liao 已提交
163
      pSqlExpr->colInfo = *pToken;
H
hzcheng 已提交
164 165
    }

H
Haojun Liao 已提交
166 167
    pSqlExpr->tokenId = optrType;
    pSqlExpr->type    = SQL_NODE_TABLE_COLUMN;
H
hzcheng 已提交
168
  }
H
Haojun Liao 已提交
169

H
Haojun Liao 已提交
170
  return pSqlExpr;
H
hzcheng 已提交
171 172 173 174 175 176
}

/*
 * pList is the parameters for function with id(optType)
 * function name is denoted by pFunctionToken
 */
H
Haojun Liao 已提交
177
tSqlExpr *tSqlExprCreateFunction(SArray *pParam, SStrToken *pFuncToken, SStrToken *endToken, int32_t optType) {
H
Haojun Liao 已提交
178 179 180 181
  if (pFuncToken == NULL) {
    return NULL;
  }

H
Haojun Liao 已提交
182
  tSqlExpr *pExpr = calloc(1, sizeof(tSqlExpr));
H
Haojun Liao 已提交
183 184
  pExpr->tokenId = optType;
  pExpr->type    = SQL_NODE_SQLFUNCTION;
H
Haojun Liao 已提交
185
  pExpr->pParam  = pParam;
H
hzcheng 已提交
186

S
TD-1057  
Shengliang Guan 已提交
187
  int32_t len = (int32_t)((endToken->z + endToken->n) - pFuncToken->z);
H
Haojun Liao 已提交
188 189 190 191 192
  pExpr->operand = (*pFuncToken);

  pExpr->token.n = len;
  pExpr->token.z = pFuncToken->z;
  pExpr->token.type = pFuncToken->type;
H
Haojun Liao 已提交
193

H
hzcheng 已提交
194 195 196 197 198
  return pExpr;
}

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

H
Haojun Liao 已提交
204
  pExpr->type = SQL_NODE_EXPR;
H
Haojun Liao 已提交
205
  if (pLeft != NULL && pRight != NULL && (optrType != TK_IN)) {
H
Haojun Liao 已提交
206
    char* endPos   = pRight->token.z + pRight->token.n;
207
    pExpr->token.z = pLeft->token.z;
208
    pExpr->token.n = (uint32_t)(endPos - pExpr->token.z);
209 210
    pExpr->token.type = pLeft->token.type;
  }
H
Haojun Liao 已提交
211

H
Haojun Liao 已提交
212 213
  if ((pLeft != NULL && pRight != NULL) &&
      (optrType == TK_PLUS || optrType == TK_MINUS || optrType == TK_STAR || optrType == TK_DIVIDE || optrType == TK_REM)) {
H
hzcheng 已提交
214 215 216 217
    /*
     * 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.
     */
H
Haojun Liao 已提交
218 219 220 221 222
    if ((pLeft->tokenId == TK_INTEGER && pRight->tokenId == TK_INTEGER) ||
        (pLeft->tokenId == TK_TIMESTAMP && pRight->tokenId == TK_TIMESTAMP)) {
      pExpr->value.nType = TSDB_DATA_TYPE_BIGINT;
      pExpr->tokenId = pLeft->tokenId;
      pExpr->type    = SQL_NODE_VALUE;
D
fix bug  
dapan1121 已提交
223
      pExpr->flags   = pLeft->flags | pRight->flags;
H
hzcheng 已提交
224

D
dapan1121 已提交
225 226 227 228 229 230 231
      if ((pLeft->flags & (1 << EXPR_FLAG_TIMESTAMP_VAR)) && (pRight->flags & (1 << EXPR_FLAG_TIMESTAMP_VAR))) {
        pExpr->flags |= 1 << EXPR_FLAG_TS_ERROR;
      } else {
        pExpr->flags &= ~(1 << EXPR_FLAG_TIMESTAMP_VAR);
        pExpr->flags &= ~(1 << EXPR_FLAG_TS_ERROR);
      }

H
hzcheng 已提交
232 233 234

      switch (optrType) {
        case TK_PLUS: {
H
Haojun Liao 已提交
235
          pExpr->value.i64 = pLeft->value.i64 + pRight->value.i64;
H
hzcheng 已提交
236 237 238
          break;
        }
        case TK_MINUS: {
H
Haojun Liao 已提交
239
          pExpr->value.i64 = pLeft->value.i64 - pRight->value.i64;
H
hzcheng 已提交
240 241 242
          break;
        }
        case TK_STAR: {
H
Haojun Liao 已提交
243
          pExpr->value.i64 = pLeft->value.i64 * pRight->value.i64;
H
hzcheng 已提交
244 245 246
          break;
        }
        case TK_DIVIDE: {
H
Haojun Liao 已提交
247 248 249
          pExpr->tokenId = TK_FLOAT;
          pExpr->value.nType = TSDB_DATA_TYPE_DOUBLE;
          pExpr->value.dKey = (double)pLeft->value.i64 / pRight->value.i64;
H
hzcheng 已提交
250 251 252
          break;
        }
        case TK_REM: {
H
Haojun Liao 已提交
253
          pExpr->value.i64 = pLeft->value.i64 % pRight->value.i64;
H
hzcheng 已提交
254 255 256 257
          break;
        }
      }

H
Haojun Liao 已提交
258 259
      tSqlExprDestroy(pLeft);
      tSqlExprDestroy(pRight);
H
Haojun Liao 已提交
260 261 262 263 264 265
    } else if ((pLeft->tokenId == TK_FLOAT && pRight->tokenId == TK_INTEGER) ||
               (pLeft->tokenId == TK_INTEGER && pRight->tokenId == TK_FLOAT) ||
               (pLeft->tokenId == TK_FLOAT && pRight->tokenId == TK_FLOAT)) {
      pExpr->value.nType = TSDB_DATA_TYPE_DOUBLE;
      pExpr->tokenId  = TK_FLOAT;
      pExpr->type     = SQL_NODE_VALUE;
H
hzcheng 已提交
266

H
Haojun Liao 已提交
267 268
      double left  = (pLeft->value.nType == TSDB_DATA_TYPE_DOUBLE) ? pLeft->value.dKey : pLeft->value.i64;
      double right = (pRight->value.nType == TSDB_DATA_TYPE_DOUBLE) ? pRight->value.dKey : pRight->value.i64;
H
hzcheng 已提交
269 270 271

      switch (optrType) {
        case TK_PLUS: {
H
Haojun Liao 已提交
272
          pExpr->value.dKey = left + right;
H
hzcheng 已提交
273 274 275
          break;
        }
        case TK_MINUS: {
H
Haojun Liao 已提交
276
          pExpr->value.dKey = left - right;
H
hzcheng 已提交
277 278 279
          break;
        }
        case TK_STAR: {
H
Haojun Liao 已提交
280
          pExpr->value.dKey = left * right;
H
hzcheng 已提交
281 282 283
          break;
        }
        case TK_DIVIDE: {
H
Haojun Liao 已提交
284
          pExpr->value.dKey = left / right;
H
hzcheng 已提交
285 286 287
          break;
        }
        case TK_REM: {
H
Haojun Liao 已提交
288
          pExpr->value.dKey = left - ((int64_t)(left / right)) * right;
H
hzcheng 已提交
289 290 291 292
          break;
        }
      }

H
Haojun Liao 已提交
293 294
      tSqlExprDestroy(pLeft);
      tSqlExprDestroy(pRight);
H
hzcheng 已提交
295 296

    } else {
H
Haojun Liao 已提交
297
      pExpr->tokenId = optrType;
H
hzcheng 已提交
298 299 300 301
      pExpr->pLeft = pLeft;
      pExpr->pRight = pRight;
    }
  } else if (optrType == TK_IN) {
H
Haojun Liao 已提交
302
    pExpr->tokenId = optrType;
H
hzcheng 已提交
303 304
    pExpr->pLeft = pLeft;

H
Haojun Liao 已提交
305
    tSqlExpr *pRSub = calloc(1, sizeof(tSqlExpr));
H
Haojun Liao 已提交
306
    pRSub->tokenId = TK_SET;  // TODO refactor .....
H
Haojun Liao 已提交
307
    pRSub->pParam = (SArray *)pRight;
H
hzcheng 已提交
308 309 310

    pExpr->pRight = pRSub;
  } else {
H
Haojun Liao 已提交
311
    pExpr->tokenId = optrType;
H
hzcheng 已提交
312
    pExpr->pLeft = pLeft;
H
Haojun Liao 已提交
313

Y
yihaoDeng 已提交
314
    if (pLeft != NULL && pRight == NULL) {
H
Haojun Liao 已提交
315
      pRight = calloc(1, sizeof(tSqlExpr));
H
Haojun Liao 已提交
316 317
    }

H
hzcheng 已提交
318 319 320 321 322 323
    pExpr->pRight = pRight;
  }

  return pExpr;
}

D
dapan1121 已提交
324 325 326 327 328
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;
}


D
dapan1121 已提交
329
int32_t tSqlExprCompare(tSqlExpr *left, tSqlExpr *right) {
D
dapan1121 已提交
330 331 332
  if ((left == NULL && right) || (left && right == NULL)) {
    return 1;
  }
H
Haojun Liao 已提交
333

D
dapan1121 已提交
334
  if (left->type != right->type) {
D
dapan1121 已提交
335 336 337
    return 1;
  }

D
fix bug  
dapan1121 已提交
338 339 340 341 342 343 344 345
  if (left->tokenId != right->tokenId) {
    return 1;
  }

  if (left->functionId != right->functionId) {
    return 1;
  }

H
Haojun Liao 已提交
346
  if ((left->pLeft && right->pLeft == NULL)
D
dapan1121 已提交
347
    || (left->pLeft == NULL && right->pLeft)
H
Haojun Liao 已提交
348
    || (left->pRight && right->pRight == NULL)
D
dapan1121 已提交
349
    || (left->pRight == NULL && right->pRight)
H
Haojun Liao 已提交
350
    || (left->pParam && right->pParam == NULL)
D
dapan1121 已提交
351 352 353 354
    || (left->pParam == NULL && right->pParam)) {
    return 1;
  }

D
dapan1121 已提交
355
  if (tVariantCompare(&left->value, &right->value)) {
D
dapan1121 已提交
356 357 358 359 360 361 362
    return 1;
  }

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

D
fix bug  
dapan1121 已提交
363
  if (right->pParam && left->pParam) {
D
fix bug  
dapan1121 已提交
364 365 366 367 368
    size_t size = taosArrayGetSize(right->pParam);
    if (left->pParam && taosArrayGetSize(left->pParam) != size) {
      return 1;
    }

H
Haojun Liao 已提交
369
    for (int32_t i = 0; i < size; i++) {
D
dapan1121 已提交
370 371
      tSqlExprItem* pLeftElem = taosArrayGet(left->pParam, i);
      tSqlExpr* pSubLeft = pLeftElem->pNode;
372
      tSqlExprItem* pRightElem = taosArrayGet(right->pParam, i);
D
dapan1121 已提交
373
      tSqlExpr* pSubRight = pRightElem->pNode;
H
Haojun Liao 已提交
374

D
fix bug  
dapan1121 已提交
375 376 377
      if (tSqlExprCompare(pSubLeft, pSubRight)) {
        return 1;
      }
D
dapan1121 已提交
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
    }
  }

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

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

  return 0;
}


D
dapan1121 已提交
393

H
Haojun Liao 已提交
394 395
tSqlExpr *tSqlExprClone(tSqlExpr *pSrc) {
  tSqlExpr *pExpr = calloc(1, sizeof(tSqlExpr));
D
dapan1121 已提交
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411

  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 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
void tSqlExprCompact(tSqlExpr** pExpr) {
  if (*pExpr == NULL || tSqlExprIsParentOfLeaf(*pExpr)) {
    return;
  }

  if ((*pExpr)->pLeft) {
    tSqlExprCompact(&(*pExpr)->pLeft);
  }

  if ((*pExpr)->pRight) {
    tSqlExprCompact(&(*pExpr)->pRight);
  }

  if ((*pExpr)->pLeft == NULL && (*pExpr)->pRight == NULL && ((*pExpr)->tokenId == TK_OR || (*pExpr)->tokenId == TK_AND)) {
    tSqlExprDestroy(*pExpr);
    *pExpr = NULL;
  } else if ((*pExpr)->pLeft == NULL && (*pExpr)->pRight != NULL) {
    tSqlExpr* tmpPtr = (*pExpr)->pRight;
    (*pExpr)->pRight = NULL;

    tSqlExprDestroy(*pExpr);
    (*pExpr) = tmpPtr;
  } else if ((*pExpr)->pRight == NULL && (*pExpr)->pLeft != NULL) {
    tSqlExpr* tmpPtr = (*pExpr)->pLeft;
    (*pExpr)->pLeft = NULL;

    tSqlExprDestroy(*pExpr);
    (*pExpr) = tmpPtr;
  }
}

bool tSqlExprIsLeaf(tSqlExpr* pExpr) {
  return (pExpr->pRight == NULL && pExpr->pLeft == NULL) &&
445 446 447 448 449
         (pExpr->tokenId == 0 ||
         (pExpr->tokenId == TK_ID) ||
         (pExpr->tokenId >= TK_BOOL && pExpr->tokenId <= TK_NCHAR) ||
         (pExpr->tokenId == TK_NULL) ||
         (pExpr->tokenId == TK_SET));
H
Haojun Liao 已提交
450 451 452 453 454 455
}

bool tSqlExprIsParentOfLeaf(tSqlExpr* pExpr) {
  return (pExpr->pLeft != NULL && pExpr->pRight != NULL) &&
         (tSqlExprIsLeaf(pExpr->pLeft) && tSqlExprIsLeaf(pExpr->pRight));
}
D
dapan1121 已提交
456

H
Haojun Liao 已提交
457
static void doDestroySqlExprNode(tSqlExpr *pExpr) {
S
slguan 已提交
458 459 460
  if (pExpr == NULL) {
    return;
  }
H
hzcheng 已提交
461

H
Haojun Liao 已提交
462 463
  if (pExpr->tokenId == TK_STRING) {
    tVariantDestroy(&pExpr->value);
H
hzcheng 已提交
464 465
  }

H
Haojun Liao 已提交
466
  tSqlExprListDestroy(pExpr->pParam);
H
hzcheng 已提交
467 468 469
  free(pExpr);
}

H
Haojun Liao 已提交
470
void tSqlExprDestroy(tSqlExpr *pExpr) {
S
slguan 已提交
471 472 473 474
  if (pExpr == NULL) {
    return;
  }

H
Haojun Liao 已提交
475
  tSqlExprDestroy(pExpr->pLeft);
D
dapan1121 已提交
476
  pExpr->pLeft = NULL;
H
Haojun Liao 已提交
477
  tSqlExprDestroy(pExpr->pRight);
D
dapan1121 已提交
478
  pExpr->pRight = NULL;
H
Haojun Liao 已提交
479 480

  doDestroySqlExprNode(pExpr);
S
slguan 已提交
481 482
}

H
Haojun Liao 已提交
483
SArray *tVariantListAppendToken(SArray *pList, SStrToken *pToken, uint8_t order) {
H
hzcheng 已提交
484
  if (pList == NULL) {
H
Haojun Liao 已提交
485
    pList = taosArrayInit(4, sizeof(tVariantListItem));
H
hzcheng 已提交
486 487
  }

H
Haojun Liao 已提交
488
  if (pToken) {
H
Haojun Liao 已提交
489
    tVariantListItem item;
H
Haojun Liao 已提交
490 491
    tVariantCreate(&item.pVar, pToken);
    item.sortOrder = order;
H
hzcheng 已提交
492

H
Haojun Liao 已提交
493
    taosArrayPush(pList, &item);
H
hzcheng 已提交
494 495 496 497 498
  }

  return pList;
}

H
Haojun Liao 已提交
499
SArray *tVariantListAppend(SArray *pList, tVariant *pVar, uint8_t sortOrder) {
500
  if (pList == NULL) {
H
Haojun Liao 已提交
501
    pList = taosArrayInit(4, sizeof(tVariantListItem));
502 503
  }

H
Haojun Liao 已提交
504
  if (pVar == NULL) {
505 506 507
    return pList;
  }

H
Haojun Liao 已提交
508 509 510 511 512 513
  /*
   * 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 已提交
514
  tVariantListItem item;
H
Haojun Liao 已提交
515 516
  item.pVar = *pVar;
  item.sortOrder = sortOrder;
517

H
Haojun Liao 已提交
518
  taosArrayPush(pList, &item);
519 520 521
  return pList;
}

H
Haojun Liao 已提交
522 523 524
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 已提交
525 526
  }

H
Haojun Liao 已提交
527 528
  tVariantListItem item;

H
Haojun Liao 已提交
529 530
  item.pVar = *pVar;
  item.sortOrder = sortOrder;
H
hzcheng 已提交
531

H
Haojun Liao 已提交
532 533
  taosArrayInsert(pList, index, &item);
  return pList;
H
hzcheng 已提交
534 535
}

536 537 538 539
SRelationInfo *setTableNameList(SRelationInfo* pRelationInfo, SStrToken *pName, SStrToken* pAlias) {
  if (pRelationInfo == NULL) {
    pRelationInfo = calloc(1, sizeof(SRelationInfo));
    pRelationInfo->list = taosArrayInit(4, sizeof(STableNamePair));
H
Haojun Liao 已提交
540 541
  }

542
  pRelationInfo->type = SQL_NODE_FROM_TABLELIST;
H
Haojun Liao 已提交
543 544 545 546 547 548 549
  STableNamePair p = {.name = *pName};
  if (pAlias != NULL) {
    p.aliasName = *pAlias;
  } else {
    TPARSER_SET_NONE_TOKEN(p.aliasName);
  }

550 551
  taosArrayPush(pRelationInfo->list, &p);
  return pRelationInfo;
H
Haojun Liao 已提交
552 553
}

554 555 556 557
SRelationInfo* setSubquery(SRelationInfo* pRelationInfo, SArray* pList) {
  if (pRelationInfo == NULL) {
    pRelationInfo = calloc(1, sizeof(SRelationInfo));
    pRelationInfo->list = taosArrayInit(4, POINTER_BYTES);
H
Haojun Liao 已提交
558 559
  }

560 561
  pRelationInfo->type = SQL_NODE_FROM_SUBQUERY;
  taosArrayPush(pRelationInfo->list, &pList);
H
Haojun Liao 已提交
562

563
  return pRelationInfo;
H
Haojun Liao 已提交
564 565
}

H
Haojun Liao 已提交
566
void* destroyRelationInfo(SRelationInfo* pRelationInfo) {
567
  if (pRelationInfo == NULL) {
H
Haojun Liao 已提交
568 569 570
    return NULL;
  }

571 572
  if (pRelationInfo->type == SQL_NODE_FROM_TABLELIST) {
    taosArrayDestroy(pRelationInfo->list);
H
Haojun Liao 已提交
573
  } else {
574 575 576 577 578 579
    size_t size = taosArrayGetSize(pRelationInfo->list);
    for(int32_t i = 0; i < size; ++i) {
      SArray* pa = taosArrayGetP(pRelationInfo->list, 0);
      destroyAllSqlNode(pa);
    }
    taosArrayDestroy(pRelationInfo->list);
H
Haojun Liao 已提交
580 581
  }

582
  tfree(pRelationInfo);
H
Haojun Liao 已提交
583 584 585 586
  return NULL;
}


H
Haojun Liao 已提交
587
void tSetDbName(SStrToken *pCpxName, SStrToken *pDb) {
H
Haojun Liao 已提交
588 589 590
  pCpxName->type = pDb->type;
  pCpxName->z = pDb->z;
  pCpxName->n = pDb->n;
H
hzcheng 已提交
591 592
}

H
Haojun Liao 已提交
593
void tSetColumnInfo(TAOS_FIELD *pField, SStrToken *pName, TAOS_FIELD *pType) {
H
hzcheng 已提交
594 595
  int32_t maxLen = sizeof(pField->name) / sizeof(pField->name[0]);

596 597 598 599 600 601 602
  // column name is too long, set the it to be invalid.
  if ((int32_t) pName->n >= maxLen) {
    pName->n = -1;
  } else {
    strncpy(pField->name, pName->z, pName->n);
    pField->name[pName->n] = 0;
  }
H
hzcheng 已提交
603 604

  pField->type = pType->type;
605
  if(!isValidDataType(pField->type)){
D
fix bug  
dapan1121 已提交
606 607 608 609
    pField->bytes = 0;
  } else {
    pField->bytes = pType->bytes;
  }
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
}

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 已提交
638

639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
      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 已提交
659 660
}

H
Haojun Liao 已提交
661
void tSetColumnType(TAOS_FIELD *pField, SStrToken *type) {
662
  // set the field type invalid
H
hzcheng 已提交
663
  pField->type = -1;
664
  pField->name[0] = 0;
H
hzcheng 已提交
665

666
  int32_t i = 0;
H
Haojun Liao 已提交
667 668 669
  while (i < tListLen(tDataTypes)) {
    if ((type->n == tDataTypes[i].nameLen) &&
        (strncasecmp(type->z, tDataTypes[i].name, tDataTypes[i].nameLen) == 0)) {
H
hzcheng 已提交
670 671
      break;
    }
672 673 674 675

    i += 1;
  }

676
  // no qualified data type found, try unsigned data type
H
Haojun Liao 已提交
677
  if (i == tListLen(tDataTypes)) {
678 679 680 681
    i = tryParseNameTwoParts(type);
    if (i == -1) {
      return;
    }
682 683 684
  }

  pField->type = i;
H
Haojun Liao 已提交
685
  pField->bytes = tDataTypes[i].bytes;
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719

  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 已提交
720 721 722 723 724 725
  }
}

/*
 * extract the select info out of sql string
 */
726
SSqlNode *tSetQuerySqlNode(SStrToken *pSelectToken, SArray *pSelNodeList, SRelationInfo *pFrom, tSqlExpr *pWhere,
H
Haojun Liao 已提交
727 728
                                SArray *pGroupby, SArray *pSortOrder, SIntervalVal *pInterval,
                                SSessionWindowVal *pSession, SStrToken *pSliding, SArray *pFill, SLimitVal *pLimit,
D
dapan1121 已提交
729
                                SLimitVal *psLimit, tSqlExpr *pHaving) {
H
Haojun Liao 已提交
730
  assert(pSelNodeList != NULL);
H
hzcheng 已提交
731

732
  SSqlNode *pSqlNode = calloc(1, sizeof(SSqlNode));
H
hzcheng 已提交
733

H
Haojun Liao 已提交
734
  // all later sql string are belonged to the stream sql
H
Haojun Liao 已提交
735 736 737
  pSqlNode->sqlstr   = *pSelectToken;
  pSqlNode->sqlstr.n = (uint32_t)strlen(pSqlNode->sqlstr.z);

H
Haojun Liao 已提交
738
  pSqlNode->pSelNodeList = pSelNodeList;
H
Haojun Liao 已提交
739 740 741 742 743
  pSqlNode->from        = pFrom;
  pSqlNode->pGroupby    = pGroupby;
  pSqlNode->pSortOrder  = pSortOrder;
  pSqlNode->pWhere      = pWhere;
  pSqlNode->fillType    = pFill;
744
  pSqlNode->pHaving = pHaving;
H
Haojun Liao 已提交
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780

  if (pLimit != NULL) {
    pSqlNode->limit = *pLimit;
  } else {
    pSqlNode->limit.limit = -1;
    pSqlNode->limit.offset = 0;
  }

  if (psLimit != NULL) {
    pSqlNode->slimit = *psLimit;
  } else {
    pSqlNode->slimit.limit = -1;
    pSqlNode->slimit.offset = 0;
  }

  if (pInterval != NULL) {
    pSqlNode->interval = *pInterval;
  } else {
    TPARSER_SET_NONE_TOKEN(pSqlNode->interval.interval);
    TPARSER_SET_NONE_TOKEN(pSqlNode->interval.offset);
  }

  if (pSliding != NULL) {
    pSqlNode->sliding = *pSliding;
  } else {
    TPARSER_SET_NONE_TOKEN(pSqlNode->sliding);
  }

  if (pSession != NULL) {
    pSqlNode->sessionVal = *pSession;
  } else {
    TPARSER_SET_NONE_TOKEN(pSqlNode->sessionVal.gap);
    TPARSER_SET_NONE_TOKEN(pSqlNode->sessionVal.col);
  }

  return pSqlNode;
H
hzcheng 已提交
781 782
}

783
static void freeVariant(void *pItem) {
H
Haojun Liao 已提交
784 785 786 787
  tVariantListItem* p = (tVariantListItem*) pItem;
  tVariantDestroy(&p->pVar);
}

788
void freeCreateTableInfo(void* p) {
789 790
  SCreatedTableInfo* pInfo = (SCreatedTableInfo*) p;  
  taosArrayDestroy(pInfo->pTagNames);
791 792 793 794 795
  taosArrayDestroyEx(pInfo->pTagVals, freeVariant);
  tfree(pInfo->fullname);
  tfree(pInfo->tagdata.data);
}

796 797
void destroySqlNode(SSqlNode *pSqlNode) {
  if (pSqlNode == NULL) {
798 799
    return;
  }
H
Haojun Liao 已提交
800

801 802
  tSqlExprListDestroy(pSqlNode->pSelNodeList);
  pSqlNode->pSelNodeList = NULL;
H
Haojun Liao 已提交
803

804 805
  tSqlExprDestroy(pSqlNode->pWhere);
  pSqlNode->pWhere = NULL;
806
  
807 808
  taosArrayDestroyEx(pSqlNode->pSortOrder, freeVariant);
  pSqlNode->pSortOrder = NULL;
H
Haojun Liao 已提交
809

810 811
  taosArrayDestroyEx(pSqlNode->pGroupby, freeVariant);
  pSqlNode->pGroupby = NULL;
H
Haojun Liao 已提交
812

H
Haojun Liao 已提交
813
  pSqlNode->from = destroyRelationInfo(pSqlNode->from);
H
Haojun Liao 已提交
814

815 816
  taosArrayDestroyEx(pSqlNode->fillType, freeVariant);
  pSqlNode->fillType = NULL;
H
Haojun Liao 已提交
817

H
Haojun Liao 已提交
818
  tSqlExprDestroy(pSqlNode->pHaving);
819
  free(pSqlNode);
820 821
}

822 823
void destroyAllSqlNode(SArray *pList) {
  if (pList == NULL) {
824 825
    return;
  }
H
hzcheng 已提交
826

827 828
  size_t size = taosArrayGetSize(pList);
  for(int32_t i = 0; i < size; ++i) {
829
    SSqlNode *pNode = taosArrayGetP(pList, i);
830
    destroySqlNode(pNode);
831
  }
832 833

  taosArrayDestroy(pList);
H
hzcheng 已提交
834 835
}

836
SCreateTableSql *tSetCreateTableInfo(SArray *pCols, SArray *pTags, SSqlNode *pSelect, int32_t type) {
H
Haojun Liao 已提交
837
  SCreateTableSql *pCreate = calloc(1, sizeof(SCreateTableSql));
H
hzcheng 已提交
838 839

  switch (type) {
840
    case TSQL_CREATE_TABLE: {
H
hzcheng 已提交
841
      pCreate->colInfo.pColumns = pCols;
842
      assert(pTags == NULL);
H
hzcheng 已提交
843 844
      break;
    }
845
    case TSQL_CREATE_STABLE: {
H
hzcheng 已提交
846 847
      pCreate->colInfo.pColumns = pCols;
      pCreate->colInfo.pTagColumns = pTags;
848
      assert(pTags != NULL && pCols != NULL);
H
hzcheng 已提交
849 850 851 852 853 854
      break;
    }
    case TSQL_CREATE_STREAM: {
      pCreate->pSelect = pSelect;
      break;
    }
855 856 857 858 859

    case TSQL_CREATE_TABLE_FROM_STABLE: {
      assert(0);
    }

H
hzcheng 已提交
860 861 862 863
    default:
      assert(false);
  }

864
  pCreate->type = type;
H
hzcheng 已提交
865 866 867
  return pCreate;
}

868
SCreatedTableInfo createNewChildTableInfo(SStrToken *pTableName, SArray *pTagNames, SArray *pTagVals, SStrToken *pToken, SStrToken* igExists) {
H
Haojun Liao 已提交
869 870 871
  SCreatedTableInfo info;
  memset(&info, 0, sizeof(SCreatedTableInfo));

872
  info.name       = *pToken;
873
  info.pTagNames  = pTagNames;
874 875 876 877 878 879 880
  info.pTagVals   = pTagVals;
  info.stableName = *pTableName;
  info.igExist    = (igExists->n > 0)? 1:0;

  return info;
}

H
Haojun Liao 已提交
881
SAlterTableInfo *tSetAlterTableInfo(SStrToken *pTableName, SArray *pCols, SArray *pVals, int32_t type, int16_t tableType) {
H
Haojun Liao 已提交
882
  SAlterTableInfo *pAlterTable = calloc(1, sizeof(SAlterTableInfo));
883
  
884
  pAlterTable->name = *pTableName;
885
  pAlterTable->type = type;
D
dapan1121 已提交
886
  pAlterTable->tableType = tableType;
H
hzcheng 已提交
887

888
  if (type == TSDB_ALTER_TABLE_ADD_COLUMN || type == TSDB_ALTER_TABLE_ADD_TAG_COLUMN) {
H
hzcheng 已提交
889 890 891
    pAlterTable->pAddColumns = pCols;
    assert(pVals == NULL);
  } else {
892 893 894 895
    /*
     * ALTER_TABLE_TAGS_CHG, ALTER_TABLE_TAGS_SET, ALTER_TABLE_TAGS_DROP,
     * ALTER_TABLE_DROP_COLUMN
     */
H
hzcheng 已提交
896 897 898 899 900 901 902
    pAlterTable->varList = pVals;
    assert(pCols == NULL);
  }

  return pAlterTable;
}

H
Haojun Liao 已提交
903
void* destroyCreateTableSql(SCreateTableSql* pCreate) {
904
  destroySqlNode(pCreate->pSelect);
905 906 907 908 909 910 911 912 913 914

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

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

  return NULL;
}

H
Haojun Liao 已提交
915
void SqlInfoDestroy(SSqlInfo *pInfo) {
H
hzcheng 已提交
916 917
  if (pInfo == NULL) return;

918
  if (pInfo->type == TSDB_SQL_SELECT) {
919
    destroyAllSqlNode(pInfo->list);
920
  } else if (pInfo->type == TSDB_SQL_CREATE_TABLE) {
H
Haojun Liao 已提交
921
    pInfo->pCreateTableInfo = destroyCreateTableSql(pInfo->pCreateTableInfo);
922
  } else if (pInfo->type == TSDB_SQL_ALTER_TABLE) {
H
Haojun Liao 已提交
923 924
    taosArrayDestroyEx(pInfo->pAlterInfo->varList, freeVariant);
    taosArrayDestroy(pInfo->pAlterInfo->pAddColumns);
925
    tfree(pInfo->pAlterInfo->tagData.data);
S
TD-1848  
Shengliang Guan 已提交
926
    tfree(pInfo->pAlterInfo);
H
hzcheng 已提交
927
  } else {
H
Haojun Liao 已提交
928 929
    if (pInfo->pMiscInfo != NULL) {
      taosArrayDestroy(pInfo->pMiscInfo->a);
H
hzcheng 已提交
930 931
    }

H
Haojun Liao 已提交
932 933
    if (pInfo->pMiscInfo != NULL && pInfo->type == TSDB_SQL_CREATE_DB) {
      taosArrayDestroyEx(pInfo->pMiscInfo->dbOpt.keep, freeVariant);
H
hzcheng 已提交
934 935
    }

H
Haojun Liao 已提交
936
    tfree(pInfo->pMiscInfo);
H
hzcheng 已提交
937 938 939
  }
}

940 941 942
SArray* setSubclause(SArray* pList, void *pSqlNode) {
  if (pList == NULL) {
    pList = taosArrayInit(1, POINTER_BYTES);
943 944
  }
  
945 946
  taosArrayPush(pList, &pSqlNode);
  return pList;
947
}
H
hzcheng 已提交
948

H
Haojun Liao 已提交
949
SSqlInfo* setSqlInfo(SSqlInfo *pInfo, void *pSqlExprInfo, SStrToken *pTableName, int32_t type) {
950 951 952
  pInfo->type = type;
  
  if (type == TSDB_SQL_SELECT) {
953
    pInfo->list = (SArray*) pSqlExprInfo;
954 955 956 957
  } else {
    pInfo->pCreateTableInfo = pSqlExprInfo;
  }
  
958 959
  if (pTableName != NULL) {
    pInfo->pCreateTableInfo->name = *pTableName;
H
hzcheng 已提交
960
  }
961 962 963 964
  
  return pInfo;
}

965 966 967
SArray* appendSelectClause(SArray *pList, void *pSubclause) {
  taosArrayPush(pList, &pSubclause);
  return pList;
H
hzcheng 已提交
968 969
}

970 971
void setCreatedTableName(SSqlInfo *pInfo, SStrToken *pTableNameToken, SStrToken *pIfNotExists) {
  pInfo->pCreateTableInfo->name = *pTableNameToken;
H
hzcheng 已提交
972 973 974
  pInfo->pCreateTableInfo->existCheck = (pIfNotExists->n != 0);
}

H
Haojun Liao 已提交
975
void setDCLSqlElems(SSqlInfo *pInfo, int32_t type, int32_t nParam, ...) {
976
  pInfo->type = type;
H
Haojun Liao 已提交
977 978 979
  if (nParam == 0) {
    return;
  }
H
hzcheng 已提交
980

H
Haojun Liao 已提交
981 982 983 984
  if (pInfo->pMiscInfo == NULL) {
    pInfo->pMiscInfo = (SMiscInfo *)calloc(1, sizeof(SMiscInfo));
    pInfo->pMiscInfo->a = taosArrayInit(4, sizeof(SStrToken));
  }
H
hzcheng 已提交
985 986 987 988

  va_list va;
  va_start(va, nParam);

H
Haojun Liao 已提交
989
  while ((nParam--) > 0) {
H
Haojun Liao 已提交
990
    SStrToken *pToken = va_arg(va, SStrToken *);
H
Haojun Liao 已提交
991
    taosArrayPush(pInfo->pMiscInfo->a, pToken);
H
hzcheng 已提交
992
  }
993

H
hzcheng 已提交
994 995 996
  va_end(va);
}

D
dapan1121 已提交
997
void setDropDbTableInfo(SSqlInfo *pInfo, int32_t type, SStrToken* pToken, SStrToken* existsCheck, int16_t dbType, int16_t tableType) {
998
  pInfo->type = type;
H
Haojun Liao 已提交
999 1000 1001 1002 1003 1004 1005 1006

  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 已提交
1007
  pInfo->pMiscInfo->existsCheck = (existsCheck->n == 1);
D
dapan1121 已提交
1008
  pInfo->pMiscInfo->dbType = dbType;
H
Haojun Liao 已提交
1009
  pInfo->pMiscInfo->tableType = tableType;
1010 1011
}

H
Haojun Liao 已提交
1012
void setShowOptions(SSqlInfo *pInfo, int32_t type, SStrToken* prefix, SStrToken* pPatterns) {
H
Haojun Liao 已提交
1013 1014
  if (pInfo->pMiscInfo == NULL) {
    pInfo->pMiscInfo = calloc(1, sizeof(SMiscInfo));
1015 1016 1017 1018
  }
  
  pInfo->type = TSDB_SQL_SHOW;
  
H
Haojun Liao 已提交
1019
  SShowInfo* pShowInfo = &pInfo->pMiscInfo->showOpt;
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
  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 已提交
1035
void setCreateDbInfo(SSqlInfo *pInfo, int32_t type, SStrToken *pToken, SCreateDbInfo *pDB, SStrToken *pIgExists) {
1036
  pInfo->type = type;
H
Haojun Liao 已提交
1037 1038
  if (pInfo->pMiscInfo == NULL) {
    pInfo->pMiscInfo = calloc(1, sizeof(SMiscInfo));
H
hzcheng 已提交
1039 1040
  }

H
Haojun Liao 已提交
1041 1042 1043
  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 已提交
1044 1045
}

H
Haojun Liao 已提交
1046
void setCreateAcctSql(SSqlInfo *pInfo, int32_t type, SStrToken *pName, SStrToken *pPwd, SCreateAcctInfo *pAcctInfo) {
1047
  pInfo->type = type;
H
Haojun Liao 已提交
1048 1049
  if (pInfo->pMiscInfo == NULL) {
    pInfo->pMiscInfo = calloc(1, sizeof(SMiscInfo));
H
hzcheng 已提交
1050 1051
  }

H
Haojun Liao 已提交
1052
  pInfo->pMiscInfo->acctOpt = *pAcctInfo;
1053 1054
  
  assert(pName != NULL);
H
Haojun Liao 已提交
1055
  pInfo->pMiscInfo->user.user = *pName;
1056 1057
  
  if (pPwd != NULL) {
H
Haojun Liao 已提交
1058
    pInfo->pMiscInfo->user.passwd = *pPwd;
1059 1060
  }
}
H
hzcheng 已提交
1061

H
Haojun Liao 已提交
1062
void setCreateUserSql(SSqlInfo *pInfo, SStrToken *pName, SStrToken *pPasswd) {
1063
  pInfo->type = TSDB_SQL_CREATE_USER;
H
Haojun Liao 已提交
1064 1065
  if (pInfo->pMiscInfo == NULL) {
    pInfo->pMiscInfo = calloc(1, sizeof(SMiscInfo));
1066 1067 1068 1069
  }
  
  assert(pName != NULL && pPasswd != NULL);
  
H
Haojun Liao 已提交
1070 1071
  pInfo->pMiscInfo->user.user = *pName;
  pInfo->pMiscInfo->user.passwd = *pPasswd;
1072
}
H
hzcheng 已提交
1073

H
Haojun Liao 已提交
1074
void setAlterUserSql(SSqlInfo *pInfo, int16_t type, SStrToken *pName, SStrToken* pPwd, SStrToken *pPrivilege) {
1075
  pInfo->type = TSDB_SQL_ALTER_USER;
H
Haojun Liao 已提交
1076 1077
  if (pInfo->pMiscInfo == NULL) {
    pInfo->pMiscInfo = calloc(1, sizeof(SMiscInfo));
1078 1079 1080 1081
  }
  
  assert(pName != NULL);
  
H
Haojun Liao 已提交
1082
  SUserInfo* pUser = &pInfo->pMiscInfo->user;
1083 1084 1085 1086 1087
  pUser->type = type;
  pUser->user = *pName;
  
  if (pPwd != NULL) {
    pUser->passwd = *pPwd;
L
lihui 已提交
1088 1089
  } else {
    pUser->passwd.type = TSDB_DATA_TYPE_NULL;
1090 1091 1092 1093
  }
  
  if (pPrivilege != NULL) {
    pUser->privilege = *pPrivilege;
L
lihui 已提交
1094 1095
  } else {
    pUser->privilege.type = TSDB_DATA_TYPE_NULL;
1096 1097 1098
  }
}

H
Haojun Liao 已提交
1099
void setKillSql(SSqlInfo *pInfo, int32_t type, SStrToken *id) {
1100
  pInfo->type = type;
H
Haojun Liao 已提交
1101 1102
  if (pInfo->pMiscInfo == NULL) {
    pInfo->pMiscInfo = calloc(1, sizeof(SMiscInfo));
H
hzcheng 已提交
1103
  }
1104
  
H
Haojun Liao 已提交
1105 1106
  assert(id != NULL);
  pInfo->pMiscInfo->id = *id;
H
hzcheng 已提交
1107
}
H
hjxilinx 已提交
1108

H
Haojun Liao 已提交
1109
void setDefaultCreateDbOption(SCreateDbInfo *pDBInfo) {
H
hjxilinx 已提交
1110 1111
  pDBInfo->compressionLevel = -1;

H
hjxilinx 已提交
1112
  pDBInfo->walLevel = -1;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
1113
  pDBInfo->fsyncPeriod = -1;
H
hjxilinx 已提交
1114
  pDBInfo->commitTime = -1;
H
hjxilinx 已提交
1115
  pDBInfo->maxTablesPerVnode = -1;
H
hjxilinx 已提交
1116 1117

  pDBInfo->cacheBlockSize = -1;
H
hjxilinx 已提交
1118 1119 1120
  pDBInfo->numOfBlocks = -1;
  pDBInfo->maxRowsPerBlock = -1;
  pDBInfo->minRowsPerBlock = -1;
H
hjxilinx 已提交
1121 1122 1123
  pDBInfo->daysPerFile = -1;

  pDBInfo->replica = -1;
1124
  pDBInfo->quorum = -1;
H
hjxilinx 已提交
1125 1126
  pDBInfo->keep = NULL;

H
Hongze Cheng 已提交
1127
  pDBInfo->update = -1;
D
fix bug  
dapan1121 已提交
1128
  pDBInfo->cachelast = -1;
D
dapan1121 已提交
1129

D
fix bug  
dapan1121 已提交
1130 1131
  pDBInfo->dbType = -1;
  pDBInfo->partitions = -1;
D
dapan1121 已提交
1132
  
H
Haojun Liao 已提交
1133
  memset(&pDBInfo->precision, 0, sizeof(SStrToken));
1134
}
D
dapan1121 已提交
1135 1136

void setDefaultCreateTopicOption(SCreateDbInfo *pDBInfo) {
D
fix bu  
dapan1121 已提交
1137 1138
  setDefaultCreateDbOption(pDBInfo);

D
dapan1121 已提交
1139 1140 1141
  pDBInfo->dbType = TSDB_DB_TYPE_TOPIC;
  pDBInfo->partitions = TSDB_DEFAULT_DB_PARTITON_OPTION;
}