parserImpl.c 33.1 KB
Newer Older
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 17 18
#include "parserImpl.h"

#include "astCreateContext.h"
19
#include "functionMgt.h"
20
#include "parserInt.h"
21 22
#include "tglobal.h"
#include "ttime.h"
23
#include "ttoken.h"
24 25 26

typedef void* (*FMalloc)(size_t);
typedef void (*FFree)(void*);
27 28

extern void* NewParseAlloc(FMalloc);
29
extern void NewParse(void*, int, SToken, void*);
30
extern void NewParseFree(void*, FFree);
31
extern void NewParseTrace(FILE*, char*);
32

33
static uint32_t toNewTokenId(uint32_t tokenId) {
34
  switch (tokenId) {
35 36
    case TK_OR:
      return NEW_TK_OR;
37 38
    case TK_AND:
      return NEW_TK_AND;
39 40 41 42 43 44 45 46 47 48 49 50
    case TK_UNION:
      return NEW_TK_UNION;
    case TK_ALL:
      return NEW_TK_ALL;
    case TK_MINUS:
      return NEW_TK_NK_MINUS;
    case TK_PLUS:
      return NEW_TK_NK_PLUS;
    case TK_STAR:
      return NEW_TK_NK_STAR;
    case TK_SLASH:
      return NEW_TK_NK_SLASH;
51 52
    case TK_REM:
      return NEW_TK_NK_REM;
53 54 55 56
    case TK_SHOW:
      return NEW_TK_SHOW;
    case TK_DATABASES:
      return NEW_TK_DATABASES;
57 58 59 60 61 62 63 64 65 66 67 68 69 70
    case TK_INTEGER:
      return NEW_TK_NK_INTEGER;
    case TK_FLOAT:
      return NEW_TK_NK_FLOAT;
    case TK_STRING:
      return NEW_TK_NK_STRING;
    case TK_BOOL:
      return NEW_TK_NK_BOOL;
    case TK_TIMESTAMP:
      return NEW_TK_TIMESTAMP;
    case TK_VARIABLE:
      return NEW_TK_NK_VARIABLE;
    case TK_COMMA:
      return NEW_TK_NK_COMMA;
71 72 73 74 75 76 77 78
    case TK_ID:
      return NEW_TK_NK_ID;
    case TK_LP:
      return NEW_TK_NK_LP;
    case TK_RP:
      return NEW_TK_NK_RP;
    case TK_DOT:
      return NEW_TK_NK_DOT;
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    case TK_BETWEEN:
      return NEW_TK_BETWEEN;
    case TK_NOT:
      return NEW_TK_NOT;
    case TK_IS:
      return NEW_TK_IS;
    case TK_NULL:
      return NEW_TK_NULL;
    case TK_LT:
      return NEW_TK_NK_LT;
    case TK_GT:
      return NEW_TK_NK_GT;
    case TK_LE:
      return NEW_TK_NK_LE;
    case TK_GE:
      return NEW_TK_NK_GE;
    case TK_NE:
      return NEW_TK_NK_NE;
97 98
    case TK_EQ:
      return NEW_TK_NK_EQ;
99 100 101 102 103 104 105 106
    case TK_LIKE:
      return NEW_TK_LIKE;
    case TK_MATCH:
      return NEW_TK_MATCH;
    case TK_NMATCH:
      return NEW_TK_NMATCH;
    case TK_IN:
      return NEW_TK_IN;
107 108 109 110
    case TK_SELECT:
      return NEW_TK_SELECT;
    case TK_DISTINCT:
      return NEW_TK_DISTINCT;
111 112
    case TK_WHERE:
      return NEW_TK_WHERE;
113 114 115 116
    case TK_AS:
      return NEW_TK_AS;
    case TK_FROM:
      return NEW_TK_FROM;
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    case TK_JOIN:
      return NEW_TK_JOIN;
    // case TK_PARTITION:
    //   return NEW_TK_PARTITION;
    case TK_SESSION:
      return NEW_TK_SESSION;
    case TK_STATE_WINDOW:
      return NEW_TK_STATE_WINDOW;
    case TK_INTERVAL:
      return NEW_TK_INTERVAL;
    case TK_SLIDING:
      return NEW_TK_SLIDING;
    case TK_FILL:
      return NEW_TK_FILL;
    // case TK_VALUE:
    //   return NEW_TK_VALUE;
    case TK_NONE:
      return NEW_TK_NONE;
    case TK_PREV:
      return NEW_TK_PREV;
    case TK_LINEAR:
      return NEW_TK_LINEAR;
    // case TK_NEXT:
    //   return NEW_TK_NEXT;
    case TK_GROUP:
      return NEW_TK_GROUP;
    case TK_HAVING:
      return NEW_TK_HAVING;
145 146 147 148 149 150 151 152
    case TK_ORDER:
      return NEW_TK_ORDER;
    case TK_BY:
      return NEW_TK_BY;
    case TK_ASC:
      return NEW_TK_ASC;
    case TK_DESC:
      return NEW_TK_DESC;
153 154 155 156 157 158 159 160
    case TK_SLIMIT:
      return NEW_TK_SLIMIT;
    case TK_SOFFSET:
      return NEW_TK_SOFFSET;
    case TK_LIMIT:
      return NEW_TK_LIMIT;
    case TK_OFFSET:
      return NEW_TK_OFFSET;
161
    case TK_SPACE:
162 163
    case NEW_TK_ON:
    case NEW_TK_INNER:
164 165 166
      break;
    default:
      printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!tokenId = %d\n", tokenId);
167 168 169 170
  }
  return tokenId;
}

171
static uint32_t getToken(const char* z, uint32_t* tokenId) {
172 173 174 175 176
  uint32_t n = tGetToken(z, tokenId);
  *tokenId = toNewTokenId(*tokenId);
  return n;
}

X
Xiaoyu Wang 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189
static EStmtType getStmtType(const SNode* pRootNode) {
  if (NULL == pRootNode) {
    return STMT_TYPE_CMD;
  }
  switch (nodeType(pRootNode)) {
    case QUERY_NODE_SELECT_STMT:
      return STMT_TYPE_QUERY;
    default:
      break;
  }
  return STMT_TYPE_CMD;
}

190
int32_t doParse(SParseContext* pParseCxt, SQuery* pQuery) {
191 192
  SAstCreateContext cxt;
  createAstCreateContext(pParseCxt, &cxt);
193
  void *pParser = NewParseAlloc(malloc);
194 195 196 197
  int32_t i = 0;
  while (1) {
    SToken t0 = {0};
    if (cxt.pQueryCxt->pSql[i] == 0) {
198
      NewParse(pParser, 0, t0, &cxt);
199 200
      goto abort_parse;
    }
201
    t0.n = getToken((char *)&cxt.pQueryCxt->pSql[i], &t0.type);
202 203 204 205 206 207 208 209 210
    t0.z = (char *)(cxt.pQueryCxt->pSql + i);
    i += t0.n;

    switch (t0.type) {
      case TK_SPACE:
      case TK_COMMENT: {
        break;
      }
      case TK_SEMI: {
211
        NewParse(pParser, 0, t0, &cxt);
212 213 214 215 216
        goto abort_parse;
      }
      case TK_QUESTION:
      case TK_ILLEGAL: {
        snprintf(cxt.pQueryCxt->pMsg, cxt.pQueryCxt->msgLen, "unrecognized token: \"%s\"", t0.z);
217
        cxt.valid = false;
218 219 220 221 222 223
        goto abort_parse;
      }
      case TK_HEX:
      case TK_OCT:
      case TK_BIN: {
        snprintf(cxt.pQueryCxt->pMsg, cxt.pQueryCxt->msgLen, "unsupported token: \"%s\"", t0.z);
224
        cxt.valid = false;
225 226 227
        goto abort_parse;
      }
      default:
228
        NewParse(pParser, t0.type, t0, &cxt);
229
        // NewParseTrace(stdout, "");
230 231 232 233 234 235 236
        if (!cxt.valid) {
          goto abort_parse;
        }
    }
  }

abort_parse:
237
  NewParseFree(pParser, free);
238
  destroyAstCreateContext(&cxt);
X
Xiaoyu Wang 已提交
239
  pQuery->stmtType = getStmtType(cxt.pRootNode);
240 241
  pQuery->pRoot = cxt.pRootNode;
  return cxt.valid ? TSDB_CODE_SUCCESS : TSDB_CODE_FAILED;
242
}
243

244
static bool afterGroupBy(ESqlClause clause) {
245 246 247 248
  return clause > SQL_CLAUSE_GROUP_BY;
}

static bool beforeHaving(ESqlClause clause) {
249 250 251
  return clause < SQL_CLAUSE_HAVING;
}

252 253 254 255 256 257
typedef struct STranslateContext {
  SParseContext* pParseCxt;
  int32_t errCode;
  SMsgBuf msgBuf;
  SArray* pNsLevel; // element is SArray*, the element of this subarray is STableNode*
  int32_t currLevel;
258
  ESqlClause currClause;
259
  SSelectStmt* pCurrStmt;
260 261 262 263 264 265
} STranslateContext;

static int32_t translateSubquery(STranslateContext* pCxt, SNode* pNode);

static char* getSyntaxErrFormat(int32_t errCode) {
  switch (errCode) {
266
    case TSDB_CODE_PAR_INVALID_COLUMN:
267
      return "Invalid column name : %s";
268
    case TSDB_CODE_PAR_TABLE_NOT_EXIST:
269
      return "Table does not exist : %s";
270
    case TSDB_CODE_PAR_AMBIGUOUS_COLUMN:
271
      return "Column ambiguously defined : %s";
272
    case TSDB_CODE_PAR_WRONG_VALUE_TYPE:
273
      return "Invalid value type : %s";
274 275
    case TSDB_CODE_PAR_INVALID_FUNTION:
      return "Invalid function name : %s";
276 277 278 279 280 281
    case TSDB_CODE_PAR_FUNTION_PARA_NUM:
      return "Invalid number of arguments : %s";
    case TSDB_CODE_PAR_FUNTION_PARA_TYPE:
      return "Inconsistent datatypes : %s";
    case TSDB_CODE_PAR_ILLEGAL_USE_AGG_FUNCTION:
      return "There mustn't be aggregation";
282 283
    case TSDB_CODE_PAR_WRONG_NUMBER_OF_SELECT:
      return "ORDER BY item must be the number of a SELECT-list expression";
284 285
    case TSDB_CODE_PAR_GROUPBY_LACK_EXPRESSION:
      return "Not a GROUP BY expression";
286 287 288 289
    case TSDB_CODE_PAR_NOT_SELECTED_EXPRESSION:
      return "Not SELECTed expression";
    case TSDB_CODE_PAR_NOT_SINGLE_GROUP:
      return "Not a single-group group function";
X
Xiaoyu Wang 已提交
290
    case TSDB_CODE_OUT_OF_MEMORY:
X
Xiaoyu Wang 已提交
291
      return "Out of memory";
292 293 294 295 296
    default:
      return "Unknown error";
  }
}

297 298 299 300 301
static int32_t generateSyntaxErrMsg(STranslateContext* pCxt, int32_t errCode, ...) {
  va_list vArgList;
  va_start(vArgList, errCode);
  vsnprintf(pCxt->msgBuf.buf, pCxt->msgBuf.len, getSyntaxErrFormat(errCode), vArgList);
  va_end(vArgList);
302 303 304 305 306
  pCxt->errCode = errCode;
  return errCode;
}

static int32_t addNamespace(STranslateContext* pCxt, void* pTable) {
307 308 309 310
  size_t currTotalLevel = taosArrayGetSize(pCxt->pNsLevel);
  if (currTotalLevel > pCxt->currLevel) {
    SArray* pTables = taosArrayGetP(pCxt->pNsLevel, pCxt->currLevel);
    taosArrayPush(pTables, &pTable);
311
  } else {
312 313 314 315 316 317 318 319
    do {
      SArray* pTables = taosArrayInit(TARRAY_MIN_SIZE, POINTER_BYTES);
      if (pCxt->currLevel == currTotalLevel) {
        taosArrayPush(pTables, &pTable);
      }
      taosArrayPush(pCxt->pNsLevel, &pTables);
      ++currTotalLevel;
    } while (currTotalLevel <= pCxt->currLevel);
320 321 322 323
  }
  return TSDB_CODE_SUCCESS;
}

324 325 326 327 328
static SName* toName(int32_t acctId, const SRealTableNode* pRealTable, SName* pName) {
  pName->type = TSDB_TABLE_NAME_T;
  pName->acctId = acctId;
  strcpy(pName->dbname, pRealTable->table.dbName);
  strcpy(pName->tname, pRealTable->table.tableName);
329 330 331 332 333 334 335 336
  return pName;
}

static bool belongTable(const char* currentDb, const SColumnNode* pCol, const STableNode* pTable) {
  int cmp = 0;
  if ('\0' != pCol->dbName[0]) {
    cmp = strcmp(pCol->dbName, pTable->dbName);
  } else {
337
    cmp = (QUERY_NODE_REAL_TABLE == nodeType(pTable) ? strcmp(currentDb, pTable->dbName) : 0);
338 339 340 341 342 343 344 345 346 347 348 349 350 351
  }
  if (0 == cmp) {
    cmp = strcmp(pCol->tableAlias, pTable->tableAlias);
  }
  return (0 == cmp);
}

static SNodeList* getProjectList(SNode* pNode) {
  if (QUERY_NODE_SELECT_STMT == nodeType(pNode)) {
    return ((SSelectStmt*)pNode)->pProjectionList;
  }
  return NULL;
}

X
Xiaoyu Wang 已提交
352 353 354 355
static void setColumnInfoBySchema(const SRealTableNode* pTable, const SSchema* pColSchema, SColumnNode* pCol) {
  strcpy(pCol->dbName, pTable->table.dbName);
  strcpy(pCol->tableAlias, pTable->table.tableAlias);
  strcpy(pCol->tableName, pTable->table.tableName);
356 357 358 359
  strcpy(pCol->colName, pColSchema->name);
  if ('\0' == pCol->node.aliasName[0]) {
    strcpy(pCol->node.aliasName, pColSchema->name);
  }
X
Xiaoyu Wang 已提交
360
  pCol->tableId = pTable->pMeta->uid;
361
  pCol->colId = pColSchema->colId;
362 363
  // pCol->colType = pColSchema->type;
  pCol->node.resType.type = pColSchema->type;
364 365 366 367 368
  pCol->node.resType.bytes = pColSchema->bytes;
}

static void setColumnInfoByExpr(const STableNode* pTable, SExprNode* pExpr, SColumnNode* pCol) {
  pCol->pProjectRef = (SNode*)pExpr;
X
Xiaoyu Wang 已提交
369
  nodesListAppend(pExpr->pAssociationList, (SNode*)pCol);
370 371 372
  if (NULL != pTable) {
    strcpy(pCol->tableAlias, pTable->tableAlias);
  }
373 374 375 376
  strcpy(pCol->colName, pExpr->aliasName);
  pCol->node.resType = pExpr->resType;
}

X
Xiaoyu Wang 已提交
377
static int32_t createColumnNodeByTable(STranslateContext* pCxt, const STableNode* pTable, SNodeList* pList) {
378 379 380 381 382
  if (QUERY_NODE_REAL_TABLE == nodeType(pTable)) {
    const STableMeta* pMeta = ((SRealTableNode*)pTable)->pMeta;
    int32_t nums = pMeta->tableInfo.numOfTags + pMeta->tableInfo.numOfColumns;
    for (int32_t i = 0; i < nums; ++i) {
      SColumnNode* pCol = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN);
X
Xiaoyu Wang 已提交
383
      if (NULL == pCol) {
X
Xiaoyu Wang 已提交
384
        return generateSyntaxErrMsg(pCxt, TSDB_CODE_OUT_OF_MEMORY);
X
Xiaoyu Wang 已提交
385
      }
X
Xiaoyu Wang 已提交
386
      setColumnInfoBySchema((SRealTableNode*)pTable, pMeta->schema + i, pCol);
387 388 389 390 391 392 393
      nodesListAppend(pList, (SNode*)pCol);
    }
  } else {
    SNodeList* pProjectList = getProjectList(((STempTableNode*)pTable)->pSubquery);
    SNode* pNode;
    FOREACH(pNode, pProjectList) {
      SColumnNode* pCol = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN);
X
Xiaoyu Wang 已提交
394
      if (NULL == pCol) {
X
Xiaoyu Wang 已提交
395
        return generateSyntaxErrMsg(pCxt, TSDB_CODE_OUT_OF_MEMORY);
X
Xiaoyu Wang 已提交
396
      }
397
      setColumnInfoByExpr(pTable, (SExprNode*)pNode, pCol);
398 399 400
      nodesListAppend(pList, (SNode*)pCol);
    }
  }
X
Xiaoyu Wang 已提交
401
  return TSDB_CODE_SUCCESS;
402 403 404 405 406 407 408 409 410
}

static bool findAndSetColumn(SColumnNode* pCol, const STableNode* pTable) {
  bool found = false;
  if (QUERY_NODE_REAL_TABLE == nodeType(pTable)) {
    const STableMeta* pMeta = ((SRealTableNode*)pTable)->pMeta;
    int32_t nums = pMeta->tableInfo.numOfTags + pMeta->tableInfo.numOfColumns;
    for (int32_t i = 0; i < nums; ++i) {
      if (0 == strcmp(pCol->colName, pMeta->schema[i].name)) {
X
Xiaoyu Wang 已提交
411
        setColumnInfoBySchema((SRealTableNode*)pTable, pMeta->schema + i, pCol);
412 413 414 415 416 417 418 419 420 421
        found = true;
        break;
      }
    }
  } else {
    SNodeList* pProjectList = getProjectList(((STempTableNode*)pTable)->pSubquery);
    SNode* pNode;
    FOREACH(pNode, pProjectList) {
      SExprNode* pExpr = (SExprNode*)pNode;
      if (0 == strcmp(pCol->colName, pExpr->aliasName)) {
422
        setColumnInfoByExpr(pTable, pExpr, pCol);
423 424 425 426 427 428 429 430
        found = true;
        break;
      }
    }
  }
  return found;
}

431
static EDealRes translateColumnWithPrefix(STranslateContext* pCxt, SColumnNode* pCol) {
432 433
  SArray* pTables = taosArrayGetP(pCxt->pNsLevel, pCxt->currLevel);
  size_t nums = taosArrayGetSize(pTables);
434
  bool foundTable = false;
435 436 437
  for (size_t i = 0; i < nums; ++i) {
    STableNode* pTable = taosArrayGetP(pTables, i);
    if (belongTable(pCxt->pParseCxt->db, pCol, pTable)) {
438
      foundTable = true;
439 440
      if (findAndSetColumn(pCol, pTable)) {
        break;
441
      }
442
      generateSyntaxErrMsg(pCxt, TSDB_CODE_PAR_INVALID_COLUMN, pCol->colName);
443
      return DEAL_RES_ERROR;
444
    }
445
  }
446 447 448 449
  if (!foundTable) {
    generateSyntaxErrMsg(pCxt, TSDB_CODE_PAR_TABLE_NOT_EXIST, pCol->tableAlias);
    return DEAL_RES_ERROR;
  }
450
  return DEAL_RES_CONTINUE;
451
}
452

453
static EDealRes translateColumnWithoutPrefix(STranslateContext* pCxt, SColumnNode* pCol) {
454 455 456 457 458 459 460
  SArray* pTables = taosArrayGetP(pCxt->pNsLevel, pCxt->currLevel);
  size_t nums = taosArrayGetSize(pTables);
  bool found = false;
  for (size_t i = 0; i < nums; ++i) {
    STableNode* pTable = taosArrayGetP(pTables, i);
    if (findAndSetColumn(pCol, pTable)) {
      if (found) {
461
        generateSyntaxErrMsg(pCxt, TSDB_CODE_PAR_AMBIGUOUS_COLUMN, pCol->colName);
462
        return DEAL_RES_ERROR;
463 464
      }
      found = true;
465
    }
466 467
  }
  if (!found) {
468
    generateSyntaxErrMsg(pCxt, TSDB_CODE_PAR_INVALID_COLUMN, pCol->colName);
469
    return DEAL_RES_ERROR;
470
  }
471
  return DEAL_RES_CONTINUE;
472 473
}

474
static bool translateColumnUseAlias(STranslateContext* pCxt, SColumnNode* pCol) {
475
  SNodeList* pProjectionList = pCxt->pCurrStmt->pProjectionList;
476 477 478 479 480 481 482 483 484 485 486
  SNode* pNode;
  FOREACH(pNode, pProjectionList) {
    SExprNode* pExpr = (SExprNode*)pNode;
    if (0 == strcmp(pCol->colName, pExpr->aliasName)) {
        setColumnInfoByExpr(NULL, pExpr, pCol);
        return true;
    }
  }
  return false;
}

487
static EDealRes translateColumn(STranslateContext* pCxt, SColumnNode* pCol) {
488 489
  // count(*)/first(*)/last(*)
  if (0 == strcmp(pCol->colName, "*")) {
490
    return DEAL_RES_CONTINUE;
491
  }
492 493 494
  if ('\0' != pCol->tableAlias[0]) {
    return translateColumnWithPrefix(pCxt, pCol);
  }
495 496 497 498
  bool found = false;
  if (SQL_CLAUSE_ORDER_BY == pCxt->currClause) {
    found = translateColumnUseAlias(pCxt, pCol);
  }
499
  return found ? DEAL_RES_CONTINUE : translateColumnWithoutPrefix(pCxt, pCol);
500 501
}

502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
static int32_t trimStringCopy(const char* src, int32_t len, char* dst) {
  // delete escape character: \\, \', \"
  char delim = src[0];
  int32_t cnt = 0;
  int32_t j = 0;
  for (uint32_t k = 1; k < len - 1; ++k) {
    if (src[k] == '\\' || (src[k] == delim && src[k + 1] == delim)) {
      dst[j] = src[k + 1];
      cnt++;
      j++;
      k++;
      continue;
    }
    dst[j] = src[k];
    j++;
  }
  dst[j] = '\0';
  return j;
}

522
static EDealRes translateValue(STranslateContext* pCxt, SValueNode* pVal) {
523 524 525 526
  if (pVal->isDuration) {
    char unit = 0;
    if (parseAbsoluteDuration(pVal->literal, strlen(pVal->literal), &pVal->datum.i, &unit, pVal->node.resType.precision) != TSDB_CODE_SUCCESS) {
      generateSyntaxErrMsg(pCxt, TSDB_CODE_PAR_WRONG_VALUE_TYPE, pVal->literal);
527
      return DEAL_RES_ERROR;
528 529 530 531 532 533 534 535
    }
  } else {
    switch (pVal->node.resType.type) {
      case TSDB_DATA_TYPE_NULL:
        break;
      case TSDB_DATA_TYPE_BOOL:
        pVal->datum.b = (0 == strcasecmp(pVal->literal, "true"));
        break;
536 537 538
      case TSDB_DATA_TYPE_TINYINT:
      case TSDB_DATA_TYPE_SMALLINT:
      case TSDB_DATA_TYPE_INT:
539 540
      case TSDB_DATA_TYPE_BIGINT: {
        char* endPtr = NULL;
541
        pVal->datum.i = strtoll(pVal->literal, &endPtr, 10);
542 543
        break;
      }
544 545 546
      case TSDB_DATA_TYPE_UTINYINT:
      case TSDB_DATA_TYPE_USMALLINT:
      case TSDB_DATA_TYPE_UINT:
X
Xiaoyu Wang 已提交
547
      case TSDB_DATA_TYPE_UBIGINT: {
548 549 550 551 552
        char* endPtr = NULL;
        pVal->datum.u = strtoull(pVal->literal, &endPtr, 10);
        break;
      }
      case TSDB_DATA_TYPE_FLOAT:
553 554 555 556 557
      case TSDB_DATA_TYPE_DOUBLE: {
        char* endPtr = NULL;
        pVal->datum.d = strtold(pVal->literal, &endPtr);
        break;
      }
558 559 560 561
      case TSDB_DATA_TYPE_BINARY:
      case TSDB_DATA_TYPE_NCHAR:
      case TSDB_DATA_TYPE_VARCHAR:
      case TSDB_DATA_TYPE_VARBINARY: {
562 563
        int32_t n = strlen(pVal->literal);
        pVal->datum.p = calloc(1, n);
X
Xiaoyu Wang 已提交
564
        if (NULL == pVal->datum.p) {
X
Xiaoyu Wang 已提交
565
          generateSyntaxErrMsg(pCxt, TSDB_CODE_OUT_OF_MEMORY);
X
Xiaoyu Wang 已提交
566 567
          return DEAL_RES_ERROR;
        }
568 569 570 571 572 573
        trimStringCopy(pVal->literal, n, pVal->datum.p);
        break;
      }
      case TSDB_DATA_TYPE_TIMESTAMP: {
        int32_t n = strlen(pVal->literal);
        char* tmp = calloc(1, n);
X
Xiaoyu Wang 已提交
574
        if (NULL == tmp) {
X
Xiaoyu Wang 已提交
575
          generateSyntaxErrMsg(pCxt, TSDB_CODE_OUT_OF_MEMORY);
X
Xiaoyu Wang 已提交
576 577
          return DEAL_RES_ERROR;
        }
578
        int32_t len = trimStringCopy(pVal->literal, n, tmp);
579
        if (taosParseTime(tmp, &pVal->datum.i, len, pVal->node.resType.precision, tsDaylight) != TSDB_CODE_SUCCESS) {
580 581
          tfree(tmp);
          generateSyntaxErrMsg(pCxt, TSDB_CODE_PAR_WRONG_VALUE_TYPE, pVal->literal);
582
          return DEAL_RES_ERROR;
583 584 585 586
        }
        tfree(tmp);
        break;
      }
587 588 589 590
      case TSDB_DATA_TYPE_JSON:
      case TSDB_DATA_TYPE_DECIMAL:
      case TSDB_DATA_TYPE_BLOB:
        // todo
591 592 593 594
      default:
        break;
    }
  }
595
  return DEAL_RES_CONTINUE;
596 597
}

598
static EDealRes translateOperator(STranslateContext* pCxt, SOperatorNode* pOp) {
599 600 601 602 603
  SDataType ldt = ((SExprNode*)(pOp->pLeft))->resType;
  SDataType rdt = ((SExprNode*)(pOp->pRight))->resType;
  if (nodesIsArithmeticOp(pOp)) {
    if (TSDB_DATA_TYPE_JSON == ldt.type || TSDB_DATA_TYPE_BLOB == ldt.type ||
        TSDB_DATA_TYPE_JSON == rdt.type || TSDB_DATA_TYPE_BLOB == rdt.type) {
604
      generateSyntaxErrMsg(pCxt, TSDB_CODE_PAR_WRONG_VALUE_TYPE, ((SExprNode*)(pOp->pRight))->aliasName);
605
      return DEAL_RES_ERROR;
606 607 608 609 610 611
    }
    pOp->node.resType.type = TSDB_DATA_TYPE_DOUBLE;
    pOp->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_DOUBLE].bytes;
  } else if (nodesIsComparisonOp(pOp)) {
    if (TSDB_DATA_TYPE_JSON == ldt.type || TSDB_DATA_TYPE_BLOB == ldt.type ||
        TSDB_DATA_TYPE_JSON == rdt.type || TSDB_DATA_TYPE_BLOB == rdt.type) {
612
      generateSyntaxErrMsg(pCxt, TSDB_CODE_PAR_WRONG_VALUE_TYPE, ((SExprNode*)(pOp->pRight))->aliasName);
613
      return DEAL_RES_ERROR;
614 615 616 617 618 619
    }
    pOp->node.resType.type = TSDB_DATA_TYPE_BOOL;
    pOp->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BOOL].bytes;
  } else {
    // todo json operator
  }
620
  return DEAL_RES_CONTINUE;
621 622
}

623
static EDealRes translateFunction(STranslateContext* pCxt, SFunctionNode* pFunc) {
X
Xiaoyu Wang 已提交
624
  if (TSDB_CODE_SUCCESS != fmGetFuncInfo(pFunc->functionName, &pFunc->funcId, &pFunc->funcType)) {
625
    generateSyntaxErrMsg(pCxt, TSDB_CODE_PAR_INVALID_FUNTION, pFunc->functionName);
626
    return DEAL_RES_ERROR;
627 628
  }
  int32_t code = fmGetFuncResultType(pFunc);
629 630
  if (TSDB_CODE_SUCCESS != code) {
    generateSyntaxErrMsg(pCxt, code, pFunc->functionName);
631
    return DEAL_RES_ERROR;
632
  }
633
  if (fmIsAggFunc(pFunc->funcId) && beforeHaving(pCxt->currClause)) {
634
    generateSyntaxErrMsg(pCxt, TSDB_CODE_PAR_ILLEGAL_USE_AGG_FUNCTION);
635
    return DEAL_RES_ERROR;
636
  }
637 638 639 640 641
  return DEAL_RES_CONTINUE;
}

static EDealRes translateExprSubquery(STranslateContext* pCxt, SNode* pNode) {
  return (TSDB_CODE_SUCCESS == translateSubquery(pCxt, pNode) ? DEAL_RES_CONTINUE : DEAL_RES_ERROR);
642 643
}

644 645 646 647 648 649
static EDealRes translateLogicCond(STranslateContext* pCxt, SLogicConditionNode* pCond) {
  pCond->node.resType.type = TSDB_DATA_TYPE_BOOL;
  pCond->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BOOL].bytes;
  return DEAL_RES_CONTINUE;
}

650
static EDealRes doTranslateExpr(SNode* pNode, void* pContext) {
651 652 653 654 655 656 657 658
  STranslateContext* pCxt = (STranslateContext*)pContext;
  switch (nodeType(pNode)) {
    case QUERY_NODE_COLUMN:
      return translateColumn(pCxt, (SColumnNode*)pNode);
    case QUERY_NODE_VALUE:
      return translateValue(pCxt, (SValueNode*)pNode);
    case QUERY_NODE_OPERATOR:
      return translateOperator(pCxt, (SOperatorNode*)pNode);
659
    case QUERY_NODE_FUNCTION:
660
      return translateFunction(pCxt, (SFunctionNode*)pNode);
661 662
    case QUERY_NODE_LOGIC_CONDITION:
      return translateLogicCond(pCxt, (SLogicConditionNode*)pNode);
663
    case QUERY_NODE_TEMP_TABLE:
664
      return translateExprSubquery(pCxt, ((STempTableNode*)pNode)->pSubquery);
665 666 667
    default:
      break;
  }
668
  return DEAL_RES_CONTINUE;
669 670 671 672 673 674 675 676 677 678 679 680
}

static int32_t translateExpr(STranslateContext* pCxt, SNode* pNode) {
  nodesWalkNodePostOrder(pNode, doTranslateExpr, pCxt);
  return pCxt->errCode;
}

static int32_t translateExprList(STranslateContext* pCxt, SNodeList* pList) {
  nodesWalkListPostOrder(pList, doTranslateExpr, pCxt);
  return pCxt->errCode;
}

681 682 683 684
static bool isAliasColumn(SColumnNode* pCol) {
  return ('\0' == pCol->tableAlias[0]);
}

685 686 687 688
static bool isDistinctOrderBy(STranslateContext* pCxt) {
  return (SQL_CLAUSE_ORDER_BY == pCxt->currClause && pCxt->pCurrStmt->isDistinct);
}

689
static SNodeList* getGroupByList(STranslateContext* pCxt) {
690
  if (isDistinctOrderBy(pCxt)) {
691 692 693 694 695 696 697 698 699 700 701 702 703
    return pCxt->pCurrStmt->pProjectionList;
  }
  return pCxt->pCurrStmt->pGroupByList;
}

static SNode* getGroupByNode(SNode* pNode) {
  if (QUERY_NODE_GROUPING_SET == nodeType(pNode)) {
    return nodesListGetNode(((SGroupingSetNode*)pNode)->pParameterList, 0);
  }
  return pNode;
}

static int32_t getGroupByErrorCode(STranslateContext* pCxt) {
704
  if (isDistinctOrderBy(pCxt)) {
705 706 707 708 709 710
    return TSDB_CODE_PAR_NOT_SELECTED_EXPRESSION;
  }
  return TSDB_CODE_PAR_GROUPBY_LACK_EXPRESSION;
}

static EDealRes doCheckExprForGroupBy(SNode* pNode, void* pContext) {
711 712 713 714
  STranslateContext* pCxt = (STranslateContext*)pContext;
  if (!nodesIsExprNode(pNode) || (QUERY_NODE_COLUMN == nodeType(pNode) && isAliasColumn((SColumnNode*)pNode))) {
    return DEAL_RES_CONTINUE;
  }
715
  if (QUERY_NODE_FUNCTION == nodeType(pNode) && fmIsAggFunc(((SFunctionNode*)pNode)->funcId) && !isDistinctOrderBy(pCxt)) {
716 717 718
    return DEAL_RES_IGNORE_CHILD;
  }
  SNode* pGroupNode;
719 720
  FOREACH(pGroupNode, getGroupByList(pCxt)) {
    if (nodesEqualNode(getGroupByNode(pGroupNode), pNode)) {
721 722 723
      return DEAL_RES_IGNORE_CHILD;
    }
  }
724 725
  if (QUERY_NODE_COLUMN == nodeType(pNode) ||
      (QUERY_NODE_FUNCTION == nodeType(pNode) && fmIsAggFunc(((SFunctionNode*)pNode)->funcId) && isDistinctOrderBy(pCxt))) {
726
    generateSyntaxErrMsg(pCxt, getGroupByErrorCode(pCxt));
727 728 729 730 731 732
    return DEAL_RES_ERROR;
  }
  return DEAL_RES_CONTINUE;
}

static int32_t checkExprForGroupBy(STranslateContext* pCxt, SNode* pNode) {
733
  nodesWalkNode(pNode, doCheckExprForGroupBy, pCxt);
734 735 736 737
  return pCxt->errCode;
}

static int32_t checkExprListForGroupBy(STranslateContext* pCxt, SNodeList* pList) {
738 739 740 741
  if (NULL == getGroupByList(pCxt)) {
    return TSDB_CODE_SUCCESS;
  }
  nodesWalkList(pList, doCheckExprForGroupBy, pCxt);
742 743 744
  return pCxt->errCode;
}

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
typedef struct CheckAggColCoexistCxt {
  STranslateContext* pTranslateCxt;
  bool existAggFunc;
  bool existCol;
} CheckAggColCoexistCxt;

static EDealRes doCheckAggColCoexist(SNode* pNode, void* pContext) {
  CheckAggColCoexistCxt* pCxt = (CheckAggColCoexistCxt*)pContext;
  if (QUERY_NODE_FUNCTION == nodeType(pNode) && fmIsAggFunc(((SFunctionNode*)pNode)->funcId)) {
    pCxt->existAggFunc = true;
    return DEAL_RES_IGNORE_CHILD;
  }
  if (QUERY_NODE_COLUMN == nodeType(pNode)) {
    pCxt->existCol = true;
  }
  return DEAL_RES_CONTINUE;
}

static int32_t checkAggColCoexist(STranslateContext* pCxt, SSelectStmt* pSelect) {
  if (NULL != pSelect->pGroupByList) {
    return TSDB_CODE_SUCCESS;
  }
  CheckAggColCoexistCxt cxt = { .pTranslateCxt = pCxt, .existAggFunc = false, .existCol = false };
  nodesWalkList(pSelect->pProjectionList, doCheckAggColCoexist, &cxt);
  if (!pSelect->isDistinct) {
    nodesWalkList(pSelect->pOrderByList, doCheckAggColCoexist, &cxt);
  }
  if (cxt.existAggFunc && cxt.existCol) {
    return generateSyntaxErrMsg(pCxt, TSDB_CODE_PAR_NOT_SINGLE_GROUP);
  }
  return TSDB_CODE_SUCCESS;
}

778 779 780 781 782 783
static int32_t translateTable(STranslateContext* pCxt, SNode* pTable) {
  int32_t code = TSDB_CODE_SUCCESS;
  switch (nodeType(pTable)) {
    case QUERY_NODE_REAL_TABLE: {
      SRealTableNode* pRealTable = (SRealTableNode*)pTable;
      SName name;
784 785
      code = catalogGetTableMeta(pCxt->pParseCxt->pCatalog, pCxt->pParseCxt->pTransporter, &(pCxt->pParseCxt->mgmtEpSet),
          toName(pCxt->pParseCxt->acctId, pRealTable, &name), &(pRealTable->pMeta));
786
      if (TSDB_CODE_SUCCESS != code) {
787
        return generateSyntaxErrMsg(pCxt, TSDB_CODE_PAR_TABLE_NOT_EXIST, pRealTable->table.tableName);
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
      }
      code = addNamespace(pCxt, pRealTable);
      break;
    }
    case QUERY_NODE_TEMP_TABLE: {
      STempTableNode* pTempTable = (STempTableNode*)pTable;
      code = translateSubquery(pCxt, pTempTable->pSubquery);
      if (TSDB_CODE_SUCCESS == code) {
        code = addNamespace(pCxt, pTempTable);
      }
      break;
    }
    case QUERY_NODE_JOIN_TABLE: {
      SJoinTableNode* pJoinTable = (SJoinTableNode*)pTable;
      code = translateTable(pCxt, pJoinTable->pLeft);
      if (TSDB_CODE_SUCCESS == code) {
        code = translateTable(pCxt, pJoinTable->pRight);
      }
      if (TSDB_CODE_SUCCESS == code) {
        code = translateExpr(pCxt, pJoinTable->pOnCond);
      }
      break;
    }
    default:
      break;
  }
  return code;
}

static int32_t translateStar(STranslateContext* pCxt, SSelectStmt* pSelect, bool* pIsSelectStar) {
  if (NULL == pSelect->pProjectionList) { // select * ...
    SArray* pTables = taosArrayGetP(pCxt->pNsLevel, pCxt->currLevel);
    size_t nums = taosArrayGetSize(pTables);
821
    pSelect->pProjectionList = nodesMakeList();
X
Xiaoyu Wang 已提交
822
    if (NULL == pSelect->pProjectionList) {
X
Xiaoyu Wang 已提交
823
      return generateSyntaxErrMsg(pCxt, TSDB_CODE_OUT_OF_MEMORY);
X
Xiaoyu Wang 已提交
824
    }
825 826
    for (size_t i = 0; i < nums; ++i) {
      STableNode* pTable = taosArrayGetP(pTables, i);
X
Xiaoyu Wang 已提交
827 828 829 830
      int32_t code = createColumnNodeByTable(pCxt, pTable, pSelect->pProjectionList);
      if (TSDB_CODE_SUCCESS != code) {
        return code;
      }
831 832 833 834 835
    }
    *pIsSelectStar = true;
  } else {

  }
836
  return TSDB_CODE_SUCCESS;
837 838
}

839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
static int32_t getPositionValue(const SValueNode* pVal) {
  switch (pVal->node.resType.type) {
    case TSDB_DATA_TYPE_NULL:
    case TSDB_DATA_TYPE_BINARY:
    case TSDB_DATA_TYPE_TIMESTAMP:
    case TSDB_DATA_TYPE_NCHAR:
    case TSDB_DATA_TYPE_VARCHAR:
    case TSDB_DATA_TYPE_VARBINARY:
    case TSDB_DATA_TYPE_JSON:
      return -1;
    case TSDB_DATA_TYPE_BOOL:
      return (pVal->datum.b ? 1 : 0);
    case TSDB_DATA_TYPE_TINYINT:
    case TSDB_DATA_TYPE_SMALLINT:
    case TSDB_DATA_TYPE_INT:
    case TSDB_DATA_TYPE_BIGINT:
      return pVal->datum.i;
    case TSDB_DATA_TYPE_FLOAT:
    case TSDB_DATA_TYPE_DOUBLE:
      return pVal->datum.d;
    case TSDB_DATA_TYPE_UTINYINT:
    case TSDB_DATA_TYPE_USMALLINT:
    case TSDB_DATA_TYPE_UINT:
    case TSDB_DATA_TYPE_UBIGINT:
      return pVal->datum.u; 
    default:
      break;
  }
  return -1;
}

X
Xiaoyu Wang 已提交
870
static int32_t translateOrderByPosition(STranslateContext* pCxt, SNodeList* pProjectionList, SNodeList* pOrderByList, bool* pOther) {
871 872 873
  *pOther = false;
  SNode* pNode;
  FOREACH(pNode, pOrderByList) {
874 875 876 877
    SNode* pExpr = ((SOrderByExprNode*)pNode)->pExpr;
    if (QUERY_NODE_VALUE == nodeType(pExpr)) {
      SValueNode* pVal = (SValueNode*)pExpr;
      if (!translateValue(pCxt, pVal)) {
X
Xiaoyu Wang 已提交
878
        return pCxt->errCode;
879
      }
X
Xiaoyu Wang 已提交
880
      int32_t pos = getPositionValue(pVal);
881 882 883 884 885
      if (pos < 0) {
        ERASE_NODE(pOrderByList);
        nodesDestroyNode(pNode);
        continue;
      } else if (0 == pos || pos > LIST_LENGTH(pProjectionList)) {
X
Xiaoyu Wang 已提交
886
        return generateSyntaxErrMsg(pCxt, TSDB_CODE_PAR_WRONG_NUMBER_OF_SELECT);
887 888
      } else {
        SColumnNode* pCol = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN);
X
Xiaoyu Wang 已提交
889
        if (NULL == pCol) {
X
Xiaoyu Wang 已提交
890
          return generateSyntaxErrMsg(pCxt, TSDB_CODE_OUT_OF_MEMORY);
X
Xiaoyu Wang 已提交
891
        }
892 893 894
        setColumnInfoByExpr(NULL, (SExprNode*)nodesListGetNode(pProjectionList, pos - 1), pCol);
        ((SOrderByExprNode*)pNode)->pExpr = (SNode*)pCol;
        nodesDestroyNode(pExpr);
895 896 897 898 899
      }
    } else {
      *pOther = true;
    }
  }
X
Xiaoyu Wang 已提交
900
  return TSDB_CODE_SUCCESS;
901 902
}

903
static int32_t translateOrderBy(STranslateContext* pCxt, SSelectStmt* pSelect) {
904
  bool other;
X
Xiaoyu Wang 已提交
905 906 907
  int32_t code = translateOrderByPosition(pCxt, pSelect->pProjectionList, pSelect->pOrderByList, &other);
  if (TSDB_CODE_SUCCESS != code) {
    return code;
908 909 910 911 912
  }
  if (!other) {
    return TSDB_CODE_SUCCESS;
  }
  pCxt->currClause = SQL_CLAUSE_ORDER_BY;
X
Xiaoyu Wang 已提交
913
  code = translateExprList(pCxt, pSelect->pOrderByList);
914
  if (TSDB_CODE_SUCCESS == code) {
915 916 917
    code = checkExprListForGroupBy(pCxt, pSelect->pOrderByList);
  }
  return code;
918 919 920 921 922 923 924 925 926
}

static int32_t translateSelectList(STranslateContext* pCxt, SSelectStmt* pSelect) {
  bool isSelectStar = false;
  int32_t code = translateStar(pCxt, pSelect, &isSelectStar);
  if (TSDB_CODE_SUCCESS == code && !isSelectStar) {
    pCxt->currClause = SQL_CLAUSE_SELECT;
    code = translateExprList(pCxt, pSelect->pProjectionList);
  }
927
  if (TSDB_CODE_SUCCESS == code) {
928 929
    code = checkExprListForGroupBy(pCxt, pSelect->pProjectionList);
  }
930 931 932
  return code;
}

933 934 935 936
static int32_t translateHaving(STranslateContext* pCxt, SSelectStmt* pSelect) {
  if (NULL == pSelect->pGroupByList && NULL != pSelect->pHaving) {
    return generateSyntaxErrMsg(pCxt, TSDB_CODE_PAR_GROUPBY_LACK_EXPRESSION);
  }
937
  pCxt->currClause = SQL_CLAUSE_HAVING;
938 939 940 941 942
  int32_t code = translateExpr(pCxt, pSelect->pHaving);
  if (TSDB_CODE_SUCCESS == code) {
    code = checkExprForGroupBy(pCxt, pSelect->pHaving);
  }
  return code;
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
}

static int32_t translateGroupBy(STranslateContext* pCxt, SNodeList* pGroupByList) {
  pCxt->currClause = SQL_CLAUSE_GROUP_BY;
  return translateExprList(pCxt, pGroupByList);
}

static int32_t translateWindow(STranslateContext* pCxt, SNode* pWindow) {
  pCxt->currClause = SQL_CLAUSE_WINDOW;
  return translateExpr(pCxt, pWindow);
}

static int32_t translatePartitionBy(STranslateContext* pCxt, SNodeList* pPartitionByList) {
  pCxt->currClause = SQL_CLAUSE_PARTITION_BY;
  return translateExprList(pCxt, pPartitionByList);
}

static int32_t translateWhere(STranslateContext* pCxt, SNode* pWhere) {
  pCxt->currClause = SQL_CLAUSE_WHERE;
  return translateExpr(pCxt, pWhere);
}

static int32_t translateFrom(STranslateContext* pCxt, SNode* pTable) {
  pCxt->currClause = SQL_CLAUSE_FROM;
  return translateTable(pCxt, pTable);
}

970
static int32_t translateSelect(STranslateContext* pCxt, SSelectStmt* pSelect) {
971 972
  pCxt->pCurrStmt = pSelect;
  int32_t code = translateFrom(pCxt, pSelect->pFromTable);
973
  if (TSDB_CODE_SUCCESS == code) {
974
    code = translateWhere(pCxt, pSelect->pWhere);
975
  }
976
  if (TSDB_CODE_SUCCESS == code) {
977
    code = translatePartitionBy(pCxt, pSelect->pPartitionByList);
978
  }
979
  if (TSDB_CODE_SUCCESS == code) {
980
    code = translateWindow(pCxt, pSelect->pWindow);
981
  }
982 983 984 985
  if (TSDB_CODE_SUCCESS == code) {
    code = translateGroupBy(pCxt, pSelect->pGroupByList);
  }
  if (TSDB_CODE_SUCCESS == code) {
986
    code = translateHaving(pCxt, pSelect);
987 988 989 990 991
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = translateSelectList(pCxt, pSelect);
  }
  if (TSDB_CODE_SUCCESS == code) {
992
    code = translateOrderBy(pCxt, pSelect);
993
  }
994 995 996
  if (TSDB_CODE_SUCCESS == code) {
    code = checkAggColCoexist(pCxt, pSelect);
  }
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
  return code;
}

static int32_t translateQuery(STranslateContext* pCxt, SNode* pNode) {
  int32_t code = TSDB_CODE_SUCCESS;
  switch (nodeType(pNode)) {
    case QUERY_NODE_SELECT_STMT:
      code = translateSelect(pCxt, (SSelectStmt*)pNode);
      break;
    default:
      break;
  }
  return code;
}

static int32_t translateSubquery(STranslateContext* pCxt, SNode* pNode) {
  ++(pCxt->currLevel);
1014
  ESqlClause currClause = pCxt->currClause;
1015
  SSelectStmt* pCurrStmt = pCxt->pCurrStmt;
1016 1017
  int32_t code = translateQuery(pCxt, pNode);
  --(pCxt->currLevel);
1018
  pCxt->currClause = currClause;
1019
  pCxt->pCurrStmt = pCurrStmt;
1020 1021 1022
  return code;
}

X
Xiaoyu Wang 已提交
1023 1024 1025 1026 1027 1028
int32_t setReslutSchema(STranslateContext* pCxt, SQuery* pQuery) {
  if (QUERY_NODE_SELECT_STMT == nodeType(pQuery->pRoot)) {
    SSelectStmt* pSelect = (SSelectStmt*)pQuery->pRoot;
    pQuery->numOfResCols = LIST_LENGTH(pSelect->pProjectionList);
    pQuery->pResSchema = calloc(pQuery->numOfResCols, sizeof(SSchema));
    if (NULL == pQuery->pResSchema) {
X
Xiaoyu Wang 已提交
1029
      return generateSyntaxErrMsg(pCxt, TSDB_CODE_OUT_OF_MEMORY);
X
Xiaoyu Wang 已提交
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
    }
    SNode* pNode;
    int32_t index = 0;
    FOREACH(pNode, pSelect->pProjectionList) {
      SExprNode* pExpr = (SExprNode*)pNode;
      pQuery->pResSchema[index].type = pExpr->resType.type;
      pQuery->pResSchema[index].bytes = pExpr->resType.bytes;
      strcpy(pQuery->pResSchema[index].name, pExpr->aliasName);
    }
  }
  return TSDB_CODE_SUCCESS;
}

1043
int32_t doTranslate(SParseContext* pParseCxt, SQuery* pQuery) {
1044 1045 1046 1047 1048 1049 1050 1051
  STranslateContext cxt = {
    .pParseCxt = pParseCxt,
    .errCode = TSDB_CODE_SUCCESS,
    .msgBuf = { .buf = pParseCxt->pMsg, .len = pParseCxt->msgLen },
    .pNsLevel = taosArrayInit(TARRAY_MIN_SIZE, POINTER_BYTES),
    .currLevel = 0,
    .currClause = 0
  };
1052
  int32_t code = fmFuncMgtInit();
X
Xiaoyu Wang 已提交
1053 1054
  if (TSDB_CODE_SUCCESS == code) {
    code = translateQuery(&cxt, pQuery->pRoot);
1055
  }
X
Xiaoyu Wang 已提交
1056 1057
  if (TSDB_CODE_SUCCESS == code && STMT_TYPE_QUERY == pQuery->stmtType) {
    code = setReslutSchema(&cxt, pQuery);
1058
  }
X
Xiaoyu Wang 已提交
1059
  return code;
1060
}