tscSQLParser.c 191.2 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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/>.
 */

#define _XOPEN_SOURCE
S
slguan 已提交
17
#define _DEFAULT_SOURCE
H
hzcheng 已提交
18

S
slguan 已提交
19
#include "os.h"
H
hzcheng 已提交
20
#include "taos.h"
S
slguan 已提交
21
#include "taosmsg.h"
H
hzcheng 已提交
22
#include "tstoken.h"
H
hjxilinx 已提交
23
#include "tstrbuild.h"
H
hjxilinx 已提交
24
#include "ttime.h"
H
hzcheng 已提交
25

H
hjxilinx 已提交
26
#include "tscSQLParser.h"
H
hzcheng 已提交
27 28 29
#include "tscUtil.h"
#include "tschemautil.h"
#include "tsclient.h"
H
hjxilinx 已提交
30

S
slguan 已提交
31 32 33 34 35 36 37 38
#define DEFAULT_PRIMARY_TIMESTAMP_COL_NAME "_c0"

// -1 is tbname column index, so here use the -2 as the initial value
#define COLUMN_INDEX_INITIAL_VAL (-2)
#define COLUMN_INDEX_INITIALIZER \
  { COLUMN_INDEX_INITIAL_VAL, COLUMN_INDEX_INITIAL_VAL }
#define COLUMN_INDEX_VALIDE(index) (((index).tableIndex >= 0) && ((index).columnIndex >= TSDB_TBNAME_COLUMN_INDEX))
#define TBNAME_LIST_SEP ","
H
hzcheng 已提交
39 40

typedef struct SColumnList {
S
slguan 已提交
41 42
  int32_t      num;
  SColumnIndex ids[TSDB_MAX_COLUMNS];
H
hzcheng 已提交
43 44
} SColumnList;

45
static SSqlExpr* doAddProjectCol(SQueryInfo* pQueryInfo, int32_t outputIndex, int32_t colIdx, int32_t tableIndex);
H
hzcheng 已提交
46 47

static int32_t setShowInfo(SSqlObj* pSql, SSqlInfo* pInfo);
H
hjxilinx 已提交
48
static char*   getAccountId(SSqlObj* pSql);
H
hzcheng 已提交
49

H
hjxilinx 已提交
50
static bool has(tFieldList* pFieldList, int32_t startIdx, const char* name);
H
hzcheng 已提交
51 52 53 54 55
static void getCurrentDBName(SSqlObj* pSql, SSQLToken* pDBToken);
static bool hasSpecifyDB(SSQLToken* pTableName);
static bool validateTableColumnInfo(tFieldList* pFieldList, SSqlCmd* pCmd);
static bool validateTagParams(tFieldList* pTagsList, tFieldList* pFieldList, SSqlCmd* pCmd);

56
static int32_t setObjFullName(char* fullName, const char* account, SSQLToken* pDB, SSQLToken* tableName, int32_t* len);
H
hzcheng 已提交
57

H
hjxilinx 已提交
58
static void getColumnName(tSQLExprItem* pItem, char* resultFieldName, int32_t nameLength);
H
hzcheng 已提交
59 60
static void getRevisedName(char* resultFieldName, int32_t functionId, int32_t maxLen, char* columnName);

61
static int32_t addExprAndResultField(SQueryInfo* pQueryInfo, int32_t colIdx, tSQLExprItem* pItem);
62 63
static int32_t insertResultField(SQueryInfo* pQueryInfo, int32_t outputIndex, SColumnList* pIdList, int16_t bytes,
                                 int8_t type, char* fieldName);
S
slguan 已提交
64
static int32_t changeFunctionID(int32_t optr, int16_t* functionId);
65
static int32_t parseSelectClause(SSqlCmd* pCmd, int32_t clauseIndex, tSQLExprList* pSelection, bool isMetric);
H
hzcheng 已提交
66

67
static bool validateIpAddress(const char* ip, size_t size);
68
static bool hasUnsupportFunctionsForSTableQuery(SQueryInfo* pQueryInfo);
69 70
static bool functionCompatibleCheck(SQueryInfo* pQueryInfo);
static void setColumnOffsetValueInResultset(SQueryInfo* pQueryInfo);
H
hzcheng 已提交
71

72
static int32_t parseGroupbyClause(SQueryInfo* pQueryInfo, tVariantList* pList, SSqlCmd* pCmd);
H
hzcheng 已提交
73

74 75
static int32_t parseIntervalClause(SQueryInfo* pQueryInfo, SQuerySQL* pQuerySql);
static int32_t setSlidingClause(SQueryInfo* pQueryInfo, SQuerySQL* pQuerySql);
H
hzcheng 已提交
76

77
static int32_t addProjectionExprAndResultField(SQueryInfo* pQueryInfo, tSQLExprItem* pItem);
H
hzcheng 已提交
78

79
static int32_t parseWhereClause(SQueryInfo* pQueryInfo, tSQLExpr** pExpr, SSqlObj* pSql);
80
static int32_t parseFillClause(SQueryInfo* pQueryInfo, SQuerySQL* pQuerySQL);
H
hjxilinx 已提交
81
static int32_t parseOrderbyClause(SQueryInfo* pQueryInfo, SQuerySQL* pQuerySql, SSchema* pSchema);
H
hzcheng 已提交
82

83
static int32_t tsRewriteFieldNameIfNecessary(SQueryInfo* pQueryInfo);
H
hzcheng 已提交
84
static int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo);
85
static int32_t validateSqlFunctionInStreamSql(SQueryInfo* pQueryInfo);
S
slguan 已提交
86
static int32_t buildArithmeticExprString(tSQLExpr* pExpr, char** exprString);
87
static int32_t validateFunctionsInIntervalOrGroupbyQuery(SQueryInfo* pQueryInfo);
88
static int32_t validateArithmeticSQLExpr(tSQLExpr* pExpr, SQueryInfo* pQueryInfo, SColumnList* pList);
H
hzcheng 已提交
89
static int32_t validateDNodeConfig(tDCLSQL* pOptions);
S
slguan 已提交
90
static int32_t validateLocalConfig(tDCLSQL* pOptions);
H
hzcheng 已提交
91 92
static int32_t validateColumnName(char* name);
static int32_t setKillInfo(SSqlObj* pSql, struct SSqlInfo* pInfo);
S
slguan 已提交
93

H
hjxilinx 已提交
94
static bool validateOneTags(SSqlCmd* pCmd, TAOS_FIELD* pTagField);
95 96
static bool hasTimestampForPointInterpQuery(SQueryInfo* pQueryInfo);
static void updateTagColumnIndex(SQueryInfo* pQueryInfo, int32_t tableIndex);
S
slguan 已提交
97

98
static int32_t parseLimitClause(SQueryInfo* pQueryInfo, int32_t index, SQuerySQL* pQuerySql, SSqlObj* pSql);
H
hjxilinx 已提交
99
static int32_t parseCreateDBOptions(SSqlCmd* pCmd, SCreateDBInfo* pCreateDbSql);
100
static int32_t getColumnIndexByName(SSQLToken* pToken, SQueryInfo* pQueryInfo, SColumnIndex* pIndex);
101
static int32_t getTableIndexByName(SSQLToken* pToken, SQueryInfo* pQueryInfo, SColumnIndex* pIndex);
S
slguan 已提交
102 103
static int32_t optrToString(tSQLExpr* pExpr, char** exprString);

104 105
static int32_t getMeterIndex(SSQLToken* pTableToken, SQueryInfo* pQueryInfo, SColumnIndex* pIndex);
static int32_t doFunctionsCompatibleCheck(SSqlCmd* pCmd, SQueryInfo* pQueryInfo);
106
static int32_t doLocalQueryProcess(SQueryInfo* pQueryInfo, SQuerySQL* pQuerySql);
H
hjxilinx 已提交
107
static int32_t tscCheckCreateDbParams(SSqlCmd* pCmd, SCreateDbMsg* pCreate);
H
hjxilinx 已提交
108

S
slguan 已提交
109
static SColumnList getColumnList(int32_t num, int16_t tableIndex, int32_t columnIndex);
H
hjxilinx 已提交
110

111
static int32_t doCheckForCreateTable(SSqlObj* pSql, int32_t subClauseIndex, SSqlInfo* pInfo);
112 113 114 115
static int32_t doCheckForCreateFromStable(SSqlObj* pSql, SSqlInfo* pInfo);
static int32_t doCheckForStream(SSqlObj* pSql, SSqlInfo* pInfo);
static int32_t doCheckForQuery(SSqlObj* pSql, SQuerySQL* pQuerySql, int32_t index);

H
hjxilinx 已提交
116 117 118 119
/*
 * Used during parsing query sql. Since the query sql usually small in length, error position
 * is not needed in the final error message.
 */
120 121
static int32_t invalidSqlErrMsg(char* dstBuffer, const char* errMsg) {
  return tscInvalidSQLErrMsg(dstBuffer, errMsg, NULL);
H
hjxilinx 已提交
122 123
}

124
static int32_t tscQueryOnlyMetricTags(SQueryInfo* pQueryInfo, bool* queryOnMetricTags) {
125
  assert(QUERY_IS_STABLE_QUERY(pQueryInfo->type));
H
hzcheng 已提交
126 127

  *queryOnMetricTags = true;
128 129
  for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) {
    SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i);
H
hzcheng 已提交
130

S
slguan 已提交
131 132
    if (pExpr->functionId != TSDB_FUNC_TAGPRJ &&
        !(pExpr->functionId == TSDB_FUNC_COUNT && pExpr->colInfo.colIdx == TSDB_TBNAME_COLUMN_INDEX)) {
H
hzcheng 已提交
133 134 135 136 137 138 139 140
      *queryOnMetricTags = false;
      break;
    }
  }

  return TSDB_CODE_SUCCESS;
}

141
static int setColumnFilterInfoForTimestamp(SQueryInfo* pQueryInfo, tVariant* pVar) {
S
slguan 已提交
142 143 144 145 146
  int64_t     time = 0;
  const char* msg = "invalid timestamp";

  strdequote(pVar->pz);
  char*           seg = strnchr(pVar->pz, '-', pVar->nLen, false);
147
  SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0);
S
slguan 已提交
148 149 150

  if (seg != NULL) {
    if (taosParseTime(pVar->pz, &time, pVar->nLen, pMeterMetaInfo->pMeterMeta->precision) != TSDB_CODE_SUCCESS) {
151
      return invalidSqlErrMsg(pQueryInfo->msg, msg);
S
slguan 已提交
152 153 154
    }
  } else {
    if (tVariantDump(pVar, (char*)&time, TSDB_DATA_TYPE_BIGINT)) {
155
      return invalidSqlErrMsg(pQueryInfo->msg, msg);
S
slguan 已提交
156 157 158 159 160 161 162 163 164
    }
  }

  tVariantDestroy(pVar);
  tVariantCreateFromBinary(pVar, (char*)&time, 0, TSDB_DATA_TYPE_BIGINT);

  return TSDB_CODE_SUCCESS;
}

165 166 167 168 169 170
static int32_t handlePassword(SSqlCmd* pCmd, SSQLToken* pPwd) {
  const char* msg1 = "password can not be empty";
  const char* msg2 = "name or password too long";
  const char* msg3 = "password needs single quote marks enclosed";

  if (pPwd->type != TK_STRING) {
171
    return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3);
172 173 174 175 176 177 178
  }

  strdequote(pPwd->z);
  strtrim(pPwd->z);  // trim space before and after passwords
  pPwd->n = strlen(pPwd->z);

  if (pPwd->n <= 0) {
179
    return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1);
180 181 182
  }

  if (pPwd->n > TSDB_PASSWORD_LEN) {
183
    return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2);
184 185 186 187 188
  }

  return TSDB_CODE_SUCCESS;
}

H
hzcheng 已提交
189 190 191 192 193 194
// todo handle memory leak in error handle function
int32_t tscToSQLCmd(SSqlObj* pSql, struct SSqlInfo* pInfo) {
  if (pInfo == NULL || pSql == NULL || pSql->signature != pSql) {
    return TSDB_CODE_APP_ERROR;
  }

195
  SSqlCmd*    pCmd = &(pSql->cmd);
196
  SQueryInfo* pQueryInfo = NULL;
H
hzcheng 已提交
197

198
  if (!pInfo->valid) {
199
    return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), pInfo->pzErrMsg);
H
hzcheng 已提交
200 201
  }

202
  int32_t code = tscGetQueryInfoDetailSafely(pCmd, pCmd->clauseIndex, &pQueryInfo);
203
  assert(pQueryInfo->numOfTables == 0);
H
hjxilinx 已提交
204
  
205
  SMeterMetaInfo* pMeterMetaInfo = tscAddEmptyMeterMetaInfo(pQueryInfo);
206

207 208 209 210 211 212 213 214
  pCmd->command = pInfo->type;

  switch (pInfo->type) {
    case TSDB_SQL_DROP_TABLE:
    case TSDB_SQL_DROP_USER:
    case TSDB_SQL_DROP_ACCT:
    case TSDB_SQL_DROP_DNODE:
    case TSDB_SQL_DROP_DB: {
215 216
      const char* msg1 = "invalid ip address";
      const char* msg2 = "invalid name";
217
      const char* msg3 = "param name too long";
H
hzcheng 已提交
218 219

      SSQLToken* pzName = &pInfo->pDCLInfo->a[0];
220
      if ((pInfo->type != TSDB_SQL_DROP_DNODE) && (tscValidateName(pzName) != TSDB_CODE_SUCCESS)) {
221
        return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2);
H
hzcheng 已提交
222 223
      }

224 225
      if (pInfo->type == TSDB_SQL_DROP_DB) {
        assert(pInfo->pDCLInfo->nTokens == 1);
H
hzcheng 已提交
226

227
        code = setObjFullName(pMeterMetaInfo->name, getAccountId(pSql), pzName, NULL, NULL);
H
hzcheng 已提交
228
        if (code != TSDB_CODE_SUCCESS) {
229
          return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2);
H
hzcheng 已提交
230 231
        }

232 233
      } else if (pInfo->type == TSDB_SQL_DROP_TABLE) {
        assert(pInfo->pDCLInfo->nTokens == 1);
H
hzcheng 已提交
234

235
        if (setMeterID(pMeterMetaInfo, pzName, pSql) != TSDB_CODE_SUCCESS) {
236
          return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3);
H
hzcheng 已提交
237
        }
238 239
      } else if (pInfo->type == TSDB_SQL_DROP_DNODE) {
        if (!validateIpAddress(pzName->z, pzName->n)) {
240
          return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1);
H
hzcheng 已提交
241 242
        }

243 244 245
        strncpy(pMeterMetaInfo->name, pzName->z, pzName->n);
      } else {  // drop user
        if (pzName->n > TSDB_USER_LEN) {
246
          return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3);
H
hzcheng 已提交
247 248
        }

S
slguan 已提交
249
        strncpy(pMeterMetaInfo->name, pzName->z, pzName->n);
H
hzcheng 已提交
250 251
      }

252 253
      break;
    }
H
hzcheng 已提交
254

255 256 257
    case TSDB_SQL_USE_DB: {
      const char* msg = "invalid db name";
      SSQLToken*  pToken = &pInfo->pDCLInfo->a[0];
S
slguan 已提交
258 259

      if (tscValidateName(pToken) != TSDB_CODE_SUCCESS) {
260
        return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg);
H
hzcheng 已提交
261 262
      }

S
slguan 已提交
263
      int32_t ret = setObjFullName(pMeterMetaInfo->name, getAccountId(pSql), pToken, NULL, NULL);
H
hzcheng 已提交
264
      if (ret != TSDB_CODE_SUCCESS) {
265
        return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg);
H
hzcheng 已提交
266 267 268 269 270
      }

      break;
    }

271 272
    case TSDB_SQL_RESET_CACHE: {
      return TSDB_CODE_SUCCESS;
H
hzcheng 已提交
273 274
    }

275 276 277
    case TSDB_SQL_SHOW: {
      if (setShowInfo(pSql, pInfo) != TSDB_CODE_SUCCESS) {
        return TSDB_CODE_INVALID_SQL;
H
hzcheng 已提交
278 279
      }

280 281 282 283 284 285 286 287
      break;
    }

    case TSDB_SQL_ALTER_DB:
    case TSDB_SQL_CREATE_DB: {
      const char* msg1 = "invalid db name";
      const char* msg2 = "name too long";

288
      SCreateDBInfo* pCreateDB = &(pInfo->pDCLInfo->dbOpt);
H
hzcheng 已提交
289
      if (tscValidateName(&pCreateDB->dbname) != TSDB_CODE_SUCCESS) {
290
        return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1);
H
hzcheng 已提交
291 292
      }

S
slguan 已提交
293
      int32_t ret = setObjFullName(pMeterMetaInfo->name, getAccountId(pSql), &(pCreateDB->dbname), NULL, NULL);
H
hzcheng 已提交
294
      if (ret != TSDB_CODE_SUCCESS) {
295
        return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2);
H
hzcheng 已提交
296 297
      }

H
hjxilinx 已提交
298
      if (parseCreateDBOptions(pCmd, pCreateDB) != TSDB_CODE_SUCCESS) {
299
        return TSDB_CODE_INVALID_SQL;
H
hzcheng 已提交
300 301 302 303 304
      }

      break;
    }

305
    case TSDB_SQL_CREATE_DNODE: {  // todo parse hostname
S
slguan 已提交
306 307
      const char* msg = "invalid ip address";

308
      if (pInfo->pDCLInfo->nTokens > 1) {
309
        return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg);
S
slguan 已提交
310 311
      }

312 313
      SSQLToken* pIpAddr = &pInfo->pDCLInfo->a[0];
      if (!validateIpAddress(pIpAddr->z, pIpAddr->n)) {
314
        return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg);
S
slguan 已提交
315 316 317 318 319
      }

      break;
    }

320 321 322 323 324
    case TSDB_SQL_CREATE_ACCT:
    case TSDB_SQL_ALTER_ACCT: {
      const char* msg1 = "invalid state option, available options[no, r, w, all]";
      const char* msg2 = "invalid user/account name";
      const char* msg3 = "name too long";
H
hzcheng 已提交
325

326 327
      SSQLToken* pName = &pInfo->pDCLInfo->user.user;
      SSQLToken* pPwd = &pInfo->pDCLInfo->user.passwd;
H
hzcheng 已提交
328

329 330
      if (handlePassword(pCmd, pPwd) != TSDB_CODE_SUCCESS) {
        return TSDB_CODE_INVALID_SQL;
H
hzcheng 已提交
331 332
      }

333
      if (pName->n > TSDB_USER_LEN) {
334
        return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3);
H
hzcheng 已提交
335 336
      }

337
      if (tscValidateName(pName) != TSDB_CODE_SUCCESS) {
338
        return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2);
H
hzcheng 已提交
339 340 341
      }

      SCreateAcctSQL* pAcctOpt = &pInfo->pDCLInfo->acctOpt;
342
      if (pAcctOpt->stat.n > 0) {
H
hzcheng 已提交
343 344 345 346 347
        if (pAcctOpt->stat.z[0] == 'r' && pAcctOpt->stat.n == 1) {
        } else if (pAcctOpt->stat.z[0] == 'w' && pAcctOpt->stat.n == 1) {
        } else if (strncmp(pAcctOpt->stat.z, "all", 3) == 0 && pAcctOpt->stat.n == 3) {
        } else if (strncmp(pAcctOpt->stat.z, "no", 2) == 0 && pAcctOpt->stat.n == 2) {
        } else {
348
          return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1);
H
hzcheng 已提交
349 350
        }
      }
351

H
hzcheng 已提交
352 353 354
      break;
    }

355
    case TSDB_SQL_DESCRIBE_TABLE: {
S
slguan 已提交
356
      SSQLToken*  pToken = &pInfo->pDCLInfo->a[0];
357
      const char* msg2 = "table name is too long";
H
hjxilinx 已提交
358
      const char* msg1 = "invalid table name";
H
hzcheng 已提交
359

S
slguan 已提交
360
      if (tscValidateName(pToken) != TSDB_CODE_SUCCESS) {
361
        return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1);
362
      }
S
slguan 已提交
363

H
hzcheng 已提交
364
      if (pToken->n > TSDB_METER_NAME_LEN) {
365
        return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2);
H
hzcheng 已提交
366 367
      }

368
      if (setMeterID(pMeterMetaInfo, pToken, pSql) != TSDB_CODE_SUCCESS) {
369
        return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2);
H
hzcheng 已提交
370 371
      }

372
      return tscGetMeterMeta(pSql, pMeterMetaInfo);
H
hzcheng 已提交
373 374
    }

375
    case TSDB_SQL_CFG_DNODE: {
376 377
      const char* msg1 = "invalid ip address";
      const char* msg2 = "invalid configure options or values";
H
hzcheng 已提交
378

379 380 381
      /* validate the ip address */
      tDCLSQL* pDCL = pInfo->pDCLInfo;
      if (!validateIpAddress(pDCL->a[0].z, pDCL->a[0].n)) {
382
        return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1);
H
hzcheng 已提交
383 384
      }

385 386
      /* validate the parameter names and options */
      if (validateDNodeConfig(pDCL) != TSDB_CODE_SUCCESS) {
387
        return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2);
H
hzcheng 已提交
388 389
      }

390 391
      char* pMsg = pCmd->payload + tsRpcHeadSize;
      pMsg += sizeof(SMgmtHead);
H
hzcheng 已提交
392

393 394
      SCfgMsg* pCfg = (SCfgMsg*)pMsg;
      strncpy(pCfg->ip, pDCL->a[0].z, pDCL->a[0].n);
H
hzcheng 已提交
395

396
      strncpy(pCfg->config, pDCL->a[1].z, pDCL->a[1].n);
H
hzcheng 已提交
397

398 399 400 401
      if (pDCL->nTokens == 3) {
        pCfg->config[pDCL->a[1].n] = ' ';  // add sep
        strncpy(&pCfg->config[pDCL->a[1].n + 1], pDCL->a[2].z, pDCL->a[2].n);
      }
H
hzcheng 已提交
402

403 404
      break;
    }
H
hzcheng 已提交
405

406 407 408 409 410 411
    case TSDB_SQL_CREATE_USER:
    case TSDB_SQL_ALTER_USER: {
      const char* msg5 = "invalid user rights";
      const char* msg7 = "not support options";
      const char* msg2 = "invalid user/account name";
      const char* msg3 = "name too long";
H
hzcheng 已提交
412

413
      pCmd->command = pInfo->type;
L
lihui 已提交
414
      //tDCLSQL* pDCL = pInfo->pDCLInfo;
H
hzcheng 已提交
415

416 417 418
      SUserInfo* pUser = &pInfo->pDCLInfo->user;
      SSQLToken* pName = &pUser->user;
      SSQLToken* pPwd = &pUser->passwd;
H
hzcheng 已提交
419

420
      if (pName->n > TSDB_USER_LEN) {
421
        return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3);
422
      }
H
hzcheng 已提交
423

424
      if (tscValidateName(pName) != TSDB_CODE_SUCCESS) {
425
        return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2);
426
      }
H
hzcheng 已提交
427

428 429 430 431 432 433 434 435
      if (pCmd->command == TSDB_SQL_CREATE_USER) {
        if (handlePassword(pCmd, pPwd) != TSDB_CODE_SUCCESS) {
          return TSDB_CODE_INVALID_SQL;
        }
      } else {
        if (pUser->type == TSDB_ALTER_USER_PASSWD) {
          if (handlePassword(pCmd, pPwd) != TSDB_CODE_SUCCESS) {
            return TSDB_CODE_INVALID_SQL;
H
hzcheng 已提交
436
          }
437
        } else if (pUser->type == TSDB_ALTER_USER_PRIVILEGES) {
L
lihui 已提交
438 439 440
          assert(pPwd->type == TSDB_DATA_TYPE_NULL);

          SSQLToken* pPrivilege = &pUser->privilege;
H
hzcheng 已提交
441

L
lihui 已提交
442
          if (strncasecmp(pPrivilege->z, "super", 5) == 0 && pPrivilege->n == 5) {
H
hzcheng 已提交
443
            pCmd->count = 1;
L
lihui 已提交
444
          } else if (strncasecmp(pPrivilege->z, "read", 4) == 0 && pPrivilege->n == 4) {
H
hzcheng 已提交
445
            pCmd->count = 2;
L
lihui 已提交
446
          } else if (strncasecmp(pPrivilege->z, "write", 5) == 0 && pPrivilege->n == 5) {
H
hzcheng 已提交
447 448
            pCmd->count = 3;
          } else {
449
            return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg5);
H
hzcheng 已提交
450 451
          }
        } else {
452
          return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg7);
H
hzcheng 已提交
453 454
        }
      }
455

H
hzcheng 已提交
456 457
      break;
    }
458 459

    case TSDB_SQL_CFG_LOCAL: {
S
slguan 已提交
460 461 462 463 464
      tDCLSQL*    pDCL = pInfo->pDCLInfo;
      const char* msg = "invalid configure options or values";

      // validate the parameter names and options
      if (validateLocalConfig(pDCL) != TSDB_CODE_SUCCESS) {
465
        return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg);
S
slguan 已提交
466 467 468 469 470 471 472
      }

      strncpy(pCmd->payload, pDCL->a[0].z, pDCL->a[0].n);
      if (pDCL->nTokens == 2) {
        pCmd->payload[pDCL->a[0].n] = ' ';  // add sep
        strncpy(&pCmd->payload[pDCL->a[0].n + 1], pDCL->a[1].z, pDCL->a[1].n);
      }
H
hzcheng 已提交
473 474 475 476

      break;
    }

477 478
    case TSDB_SQL_CREATE_TABLE: {
      SCreateTableSQL* pCreateTable = pInfo->pCreateTableInfo;
H
hzcheng 已提交
479

480
      if (pCreateTable->type == TSQL_CREATE_TABLE || pCreateTable->type == TSQL_CREATE_STABLE) {
481
        if ((code = doCheckForCreateTable(pSql, 0, pInfo)) != TSDB_CODE_SUCCESS) {
482
          return code;
H
hzcheng 已提交
483 484
        }

485 486 487
      } else if (pCreateTable->type == TSQL_CREATE_TABLE_FROM_STABLE) {
        if ((code = doCheckForCreateFromStable(pSql, pInfo)) != TSDB_CODE_SUCCESS) {
          return code;
H
hzcheng 已提交
488 489
        }

490 491 492
      } else if (pCreateTable->type == TSQL_CREATE_STREAM) {
        if ((code = doCheckForStream(pSql, pInfo)) != TSDB_CODE_SUCCESS) {
          return code;
S
slguan 已提交
493
        }
H
hzcheng 已提交
494 495 496 497 498
      }

      break;
    }

499
    case TSDB_SQL_SELECT: {
500
      assert(pCmd->numOfClause == 1);
H
hjxilinx 已提交
501
      const char* msg1 = "columns in select clause not identical";
502
      
503
      for (int32_t i = pCmd->numOfClause; i < pInfo->subclauseInfo.numOfClause; ++i) {
504 505 506 507
        SQueryInfo* pqi = NULL;
        if ((code = tscGetQueryInfoDetailSafely(pCmd, i, &pqi)) != TSDB_CODE_SUCCESS) {
          return code;
        }
508 509 510 511
      }

      assert(pCmd->numOfClause == pInfo->subclauseInfo.numOfClause);
      for (int32_t i = 0; i < pInfo->subclauseInfo.numOfClause; ++i) {
512
        SQuerySQL* pQuerySql = pInfo->subclauseInfo.pClause[i];
513

514 515
        if ((code = doCheckForQuery(pSql, pQuerySql, i)) != TSDB_CODE_SUCCESS) {
          return code;
H
hzcheng 已提交
516
        }
517
  
H
hjxilinx 已提交
518
        tscPrintSelectClause(pSql, i);
H
hzcheng 已提交
519
      }
520
      
H
hjxilinx 已提交
521
      // set the command/global limit parameters from the first subclause to the sqlcmd object
522 523 524 525
      SQueryInfo* pQueryInfo1 = tscGetQueryInfoDetail(pCmd, 0);
      pCmd->command = pQueryInfo1->command;
  
      // if there is only one element, the limit of clause is the limit of global result.
526 527
      for(int32_t i = 1; i < pCmd->numOfClause; ++i) {
        SQueryInfo* pQueryInfo2 = tscGetQueryInfoDetail(pCmd, i);
528
        
529 530 531
        int32_t ret = tscFieldInfoCompare(&pQueryInfo1->fieldsInfo, &pQueryInfo2->fieldsInfo);
        if (ret != 0) {
          return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1);
532
        }
533
      }
534 535

      return TSDB_CODE_SUCCESS;  // do not build query message here
536
    }
H
hzcheng 已提交
537

538 539 540
    case TSDB_SQL_ALTER_TABLE: {
      if ((code = setAlterTableInfo(pSql, pInfo)) != TSDB_CODE_SUCCESS) {
        return code;
H
hzcheng 已提交
541 542 543 544 545
      }

      break;
    }

546 547 548 549
    case TSDB_SQL_KILL_QUERY:
    case TSDB_SQL_KILL_STREAM:
    case TSDB_SQL_KILL_CONNECTION: {
      if ((code = setKillInfo(pSql, pInfo)) != TSDB_CODE_SUCCESS) {
H
hzcheng 已提交
550 551 552 553 554 555 556
        return code;
      }

      break;
    }

    default:
557
      return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), "not support sql expression");
H
hzcheng 已提交
558 559
  }

560
  return tscBuildMsg[pCmd->command](pSql, pInfo);
H
hzcheng 已提交
561 562
}

S
slguan 已提交
563 564 565 566
/*
 * if the top/bottom exists, only tags columns, tbname column, and primary timestamp column
 * are available.
 */
567 568 569
static bool isTopBottomQuery(SQueryInfo* pQueryInfo) {
  for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) {
    int32_t functionId = tscSqlExprGet(pQueryInfo, i)->functionId;
S
slguan 已提交
570 571 572 573

    if (functionId == TSDB_FUNC_TOP || functionId == TSDB_FUNC_BOTTOM) {
      return true;
    }
H
hzcheng 已提交
574 575
  }

S
slguan 已提交
576
  return false;
H
hzcheng 已提交
577 578
}

579
int32_t parseIntervalClause(SQueryInfo* pQueryInfo, SQuerySQL* pQuerySql) {
S
slguan 已提交
580 581
  const char* msg1 = "invalid query expression";
  const char* msg2 = "interval cannot be less than 10 ms";
582

583
  SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0);
S
slguan 已提交
584

H
hjxilinx 已提交
585
  if (pQuerySql->interval.type == 0 || pQuerySql->interval.n == 0) {
H
hzcheng 已提交
586 587 588 589 590
    return TSDB_CODE_SUCCESS;
  }

  // interval is not null
  SSQLToken* t = &pQuerySql->interval;
591
  if (getTimestampInUsFromStr(t->z, t->n, &pQueryInfo->nAggTimeInterval) != TSDB_CODE_SUCCESS) {
H
hzcheng 已提交
592 593 594
    return TSDB_CODE_INVALID_SQL;
  }

H
hjxilinx 已提交
595
  // if the unit of time window value is millisecond, change the value from microsecond
S
slguan 已提交
596
  if (pMeterMetaInfo->pMeterMeta->precision == TSDB_TIME_PRECISION_MILLI) {
597
    pQueryInfo->nAggTimeInterval = pQueryInfo->nAggTimeInterval / 1000;
H
hzcheng 已提交
598 599 600
  }

  /* parser has filter the illegal type, no need to check here */
601
  pQueryInfo->intervalTimeUnit = pQuerySql->interval.z[pQuerySql->interval.n - 1];
H
hzcheng 已提交
602 603

  // interval cannot be less than 10 milliseconds
604 605
  if (pQueryInfo->nAggTimeInterval < tsMinIntervalTime) {
    return invalidSqlErrMsg(pQueryInfo->msg, msg2);
H
hzcheng 已提交
606 607 608
  }

  // for top/bottom + interval query, we do not add additional timestamp column in the front
609
  if (isTopBottomQuery(pQueryInfo)) {
H
hzcheng 已提交
610 611 612
    return TSDB_CODE_SUCCESS;
  }

H
hjxilinx 已提交
613 614 615
  /*
   * check invalid SQL:
   * select count(tbname)/count(tag1)/count(tag2) from super_table_name interval(1d);
H
hjxilinx 已提交
616
   */
617 618
  for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) {
    SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i);
S
slguan 已提交
619
    if (pExpr->functionId == TSDB_FUNC_COUNT && TSDB_COL_IS_TAG(pExpr->colInfo.flag)) {
620
      return invalidSqlErrMsg(pQueryInfo->msg, msg1);
S
slguan 已提交
621 622
    }
  }
H
hjxilinx 已提交
623 624 625 626
  
  /*
   * check invalid SQL:
   * select tbname, tags_fields from super_table_name interval(1s)
H
hjxilinx 已提交
627
   */
H
hjxilinx 已提交
628 629 630
  if (tscQueryMetricTags(pQueryInfo) && pQueryInfo->nAggTimeInterval > 0) {
    return invalidSqlErrMsg(pQueryInfo->msg, msg1);
  }
S
slguan 已提交
631 632

  // need to add timestamp column in result set, if interval is existed
633
  uint64_t uid = tscSqlExprGet(pQueryInfo, 0)->uid;
S
slguan 已提交
634 635

  int32_t tableIndex = COLUMN_INDEX_INITIAL_VAL;
636 637
  for (int32_t i = 0; i < pQueryInfo->numOfTables; ++i) {
    pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, i);
S
slguan 已提交
638 639 640 641 642 643 644 645 646 647 648
    if (pMeterMetaInfo->pMeterMeta->uid == uid) {
      tableIndex = i;
      break;
    }
  }

  if (tableIndex == COLUMN_INDEX_INITIAL_VAL) {
    return TSDB_CODE_INVALID_SQL;
  }

  SColumnIndex index = {tableIndex, PRIMARYKEY_TIMESTAMP_COL_INDEX};
649
  tscSqlExprInsert(pQueryInfo, 0, TSDB_FUNC_TS, &index, TSDB_DATA_TYPE_TIMESTAMP, TSDB_KEYSIZE, TSDB_KEYSIZE);
H
hzcheng 已提交
650

S
slguan 已提交
651
  SColumnList ids = getColumnList(1, 0, PRIMARYKEY_TIMESTAMP_COL_INDEX);
652

653
  return insertResultField(pQueryInfo, 0, &ids, TSDB_KEYSIZE, TSDB_DATA_TYPE_TIMESTAMP, aAggs[TSDB_FUNC_TS].aName);
H
hzcheng 已提交
654 655
}

656
int32_t setSlidingClause(SQueryInfo* pQueryInfo, SQuerySQL* pQuerySql) {
657 658
  const char* msg0 = "sliding value too small";
  const char* msg1 = "sliding value no larger than the interval value";
H
hzcheng 已提交
659

660
  SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0);
S
slguan 已提交
661
  SSQLToken*      pSliding = &pQuerySql->sliding;
H
hzcheng 已提交
662 663

  if (pSliding->n != 0) {
664
    getTimestampInUsFromStr(pSliding->z, pSliding->n, &pQueryInfo->nSlidingTime);
S
slguan 已提交
665
    if (pMeterMetaInfo->pMeterMeta->precision == TSDB_TIME_PRECISION_MILLI) {
666
      pQueryInfo->nSlidingTime /= 1000;
H
hzcheng 已提交
667 668
    }

669 670
    if (pQueryInfo->nSlidingTime < tsMinSlidingTime) {
      return invalidSqlErrMsg(pQueryInfo->msg, msg0);
H
hzcheng 已提交
671 672
    }

673 674
    if (pQueryInfo->nSlidingTime > pQueryInfo->nAggTimeInterval) {
      return invalidSqlErrMsg(pQueryInfo->msg, msg1);
H
hzcheng 已提交
675 676 677 678 679 680
    }
  }

  return TSDB_CODE_SUCCESS;
}

681
int32_t setMeterID(SMeterMetaInfo* pMeterMetaInfo, SSQLToken* pzTableName, SSqlObj* pSql) {
682
  const char* msg = "name too long";
H
hzcheng 已提交
683

684 685
  SSqlCmd* pCmd = &pSql->cmd;
  int32_t  code = TSDB_CODE_SUCCESS;
S
slguan 已提交
686

687 688 689 690 691 692 693
  // backup the old name in pMeterMetaInfo
  size_t size = strlen(pMeterMetaInfo->name);
  char* oldName = NULL;
  if (size > 0) {
    oldName = strdup(pMeterMetaInfo->name);
  }
  
H
hzcheng 已提交
694
  if (hasSpecifyDB(pzTableName)) {
695
    // db has been specified in sql string so we ignore current db path
S
slguan 已提交
696 697
    code = setObjFullName(pMeterMetaInfo->name, getAccountId(pSql), NULL, pzTableName, NULL);
  } else {  // get current DB name first, then set it into path
H
hzcheng 已提交
698 699 700
    SSQLToken t = {0};
    getCurrentDBName(pSql, &t);

S
slguan 已提交
701
    code = setObjFullName(pMeterMetaInfo->name, NULL, &t, pzTableName, NULL);
H
hzcheng 已提交
702 703
  }

S
slguan 已提交
704
  if (code != TSDB_CODE_SUCCESS) {
705
    invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg);
H
hzcheng 已提交
706 707
  }

708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
  if (code != TSDB_CODE_SUCCESS) {
    free(oldName);
    return code;
  }
  
  /*
   * the old name exists and is not equalled to the new name. Release the metermeta/metricmeta
   * that are corresponding to the old name for the new table name.
   */
  if (size > 0) {
    if (strncasecmp(oldName, pMeterMetaInfo->name, tListLen(pMeterMetaInfo->name)) != 0) {
      tscClearMeterMetaInfo(pMeterMetaInfo, false);
    }
  } else {
    assert(pMeterMetaInfo->pMeterMeta == NULL && pMeterMetaInfo->pMetricMeta == NULL);
  }
  
  tfree(oldName);
  return TSDB_CODE_SUCCESS;
H
hzcheng 已提交
727 728 729 730 731
}

static bool validateTableColumnInfo(tFieldList* pFieldList, SSqlCmd* pCmd) {
  assert(pFieldList != NULL);

732 733 734 735 736 737 738
  const char* msg = "illegal number of columns";
  const char* msg1 = "first column must be timestamp";
  const char* msg2 = "row length exceeds max length";
  const char* msg3 = "duplicated column names";
  const char* msg4 = "invalid data types";
  const char* msg5 = "invalid binary/nchar column length";
  const char* msg6 = "invalid column name";
H
hzcheng 已提交
739 740 741

  // number of fields no less than 2
  if (pFieldList->nField <= 1 || pFieldList->nField > TSDB_MAX_COLUMNS) {
742
    invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg);
H
hzcheng 已提交
743 744 745 746 747
    return false;
  }

  // first column must be timestamp
  if (pFieldList->p[0].type != TSDB_DATA_TYPE_TIMESTAMP) {
748
    invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1);
H
hzcheng 已提交
749 750 751 752 753 754 755 756 757 758
    return false;
  }

  int32_t nLen = 0;
  for (int32_t i = 0; i < pFieldList->nField; ++i) {
    nLen += pFieldList->p[i].bytes;
  }

  // max row length must be less than TSDB_MAX_BYTES_PER_ROW
  if (nLen > TSDB_MAX_BYTES_PER_ROW) {
759
    invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2);
H
hzcheng 已提交
760 761 762 763 764 765 766
    return false;
  }

  // field name must be unique
  for (int32_t i = 0; i < pFieldList->nField; ++i) {
    TAOS_FIELD* pField = &pFieldList->p[i];
    if (pField->type < TSDB_DATA_TYPE_BOOL || pField->type > TSDB_DATA_TYPE_NCHAR) {
767
      invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg4);
H
hzcheng 已提交
768 769 770 771 772
      return false;
    }

    if ((pField->type == TSDB_DATA_TYPE_BINARY && (pField->bytes <= 0 || pField->bytes > TSDB_MAX_BINARY_LEN)) ||
        (pField->type == TSDB_DATA_TYPE_NCHAR && (pField->bytes <= 0 || pField->bytes > TSDB_MAX_NCHAR_LEN))) {
773
      invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg5);
H
hzcheng 已提交
774 775 776 777
      return false;
    }

    if (validateColumnName(pField->name) != TSDB_CODE_SUCCESS) {
778
      invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg6);
H
hzcheng 已提交
779 780 781 782
      return false;
    }

    if (has(pFieldList, i + 1, pFieldList->p[i].name) == true) {
783
      invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3);
H
hzcheng 已提交
784 785 786 787 788 789 790 791 792 793
      return false;
    }
  }

  return true;
}

static bool validateTagParams(tFieldList* pTagsList, tFieldList* pFieldList, SSqlCmd* pCmd) {
  assert(pTagsList != NULL);

794 795 796 797 798 799 800
  const char* msg1 = "invalid number of tag columns";
  const char* msg2 = "tag length too long";
  const char* msg3 = "duplicated column names";
  const char* msg4 = "timestamp not allowed in tags";
  const char* msg5 = "invalid data type in tags";
  const char* msg6 = "invalid tag name";
  const char* msg7 = "invalid binary/nchar tag length";
H
hzcheng 已提交
801 802 803

  // number of fields at least 1
  if (pTagsList->nField < 1 || pTagsList->nField > TSDB_MAX_TAGS) {
804
    invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1);
H
hzcheng 已提交
805 806 807 808 809 810 811 812 813 814
    return false;
  }

  int32_t nLen = 0;
  for (int32_t i = 0; i < pTagsList->nField; ++i) {
    nLen += pTagsList->p[i].bytes;
  }

  // max tag row length must be less than TSDB_MAX_TAGS_LEN
  if (nLen > TSDB_MAX_TAGS_LEN) {
815
    invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2);
H
hzcheng 已提交
816 817 818 819 820 821
    return false;
  }

  // field name must be unique
  for (int32_t i = 0; i < pTagsList->nField; ++i) {
    if (has(pFieldList, 0, pTagsList->p[i].name) == true) {
822
      invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3);
H
hzcheng 已提交
823 824 825 826 827 828 829
      return false;
    }
  }

  /* timestamp in tag is not allowed */
  for (int32_t i = 0; i < pTagsList->nField; ++i) {
    if (pTagsList->p[i].type == TSDB_DATA_TYPE_TIMESTAMP) {
830
      invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg4);
H
hzcheng 已提交
831 832 833 834
      return false;
    }

    if (pTagsList->p[i].type < TSDB_DATA_TYPE_BOOL || pTagsList->p[i].type > TSDB_DATA_TYPE_NCHAR) {
835
      invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg5);
H
hzcheng 已提交
836 837 838 839 840
      return false;
    }

    if ((pTagsList->p[i].type == TSDB_DATA_TYPE_BINARY && pTagsList->p[i].bytes <= 0) ||
        (pTagsList->p[i].type == TSDB_DATA_TYPE_NCHAR && pTagsList->p[i].bytes <= 0)) {
841
      invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg7);
H
hzcheng 已提交
842 843 844 845
      return false;
    }

    if (validateColumnName(pTagsList->p[i].name) != TSDB_CODE_SUCCESS) {
846
      invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg6);
H
hzcheng 已提交
847 848 849 850
      return false;
    }

    if (has(pTagsList, i + 1, pTagsList->p[i].name) == true) {
851
      invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3);
H
hzcheng 已提交
852 853 854 855 856 857 858 859 860 861 862
      return false;
    }
  }

  return true;
}

/*
 * tags name /column name is truncated in sql.y
 */
bool validateOneTags(SSqlCmd* pCmd, TAOS_FIELD* pTagField) {
863 864 865 866 867 868
  const char* msg1 = "timestamp not allowed in tags";
  const char* msg2 = "duplicated column names";
  const char* msg3 = "tag length too long";
  const char* msg4 = "invalid tag name";
  const char* msg5 = "invalid binary/nchar tag length";
  const char* msg6 = "invalid data type in tags";
H
hzcheng 已提交
869

870 871 872
  assert(pCmd->numOfClause == 1);
  
  SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, pCmd->clauseIndex, 0);
S
slguan 已提交
873
  SMeterMeta*     pMeterMeta = pMeterMetaInfo->pMeterMeta;
H
hzcheng 已提交
874 875 876 877 878 879

  // no more than 6 tags
  if (pMeterMeta->numOfTags == TSDB_MAX_TAGS) {
    char msg[128] = {0};
    sprintf(msg, "tags no more than %d", TSDB_MAX_TAGS);

880
    invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg);
H
hzcheng 已提交
881 882 883 884 885
    return false;
  }

  // no timestamp allowable
  if (pTagField->type == TSDB_DATA_TYPE_TIMESTAMP) {
886
    invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1);
H
hzcheng 已提交
887 888 889 890
    return false;
  }

  if (pTagField->type < TSDB_DATA_TYPE_BOOL && pTagField->type > TSDB_DATA_TYPE_NCHAR) {
891
    invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg6);
H
hzcheng 已提交
892 893 894
    return false;
  }

S
slguan 已提交
895
  SSchema* pTagSchema = tsGetTagSchema(pMeterMetaInfo->pMeterMeta);
H
hzcheng 已提交
896 897 898 899 900 901 902 903
  int32_t  nLen = 0;

  for (int32_t i = 0; i < pMeterMeta->numOfTags; ++i) {
    nLen += pTagSchema[i].bytes;
  }

  // length less than TSDB_MAX_TASG_LEN
  if (nLen + pTagField->bytes > TSDB_MAX_TAGS_LEN) {
904
    invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3);
H
hzcheng 已提交
905 906 907 908 909
    return false;
  }

  // tags name can not be a keyword
  if (validateColumnName(pTagField->name) != TSDB_CODE_SUCCESS) {
910
    invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg4);
H
hzcheng 已提交
911 912 913 914 915
    return false;
  }

  // binary(val), val can not be equalled to or less than 0
  if ((pTagField->type == TSDB_DATA_TYPE_BINARY || pTagField->type == TSDB_DATA_TYPE_NCHAR) && pTagField->bytes <= 0) {
916
    invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg5);
H
hzcheng 已提交
917 918 919 920 921 922 923 924
    return false;
  }

  // field name must be unique
  SSchema* pSchema = tsGetSchema(pMeterMeta);

  for (int32_t i = 0; i < pMeterMeta->numOfTags + pMeterMeta->numOfColumns; ++i) {
    if (strncasecmp(pTagField->name, pSchema[i].name, TSDB_COL_NAME_LEN) == 0) {
925
      invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2);
H
hzcheng 已提交
926 927 928 929 930 931 932 933
      return false;
    }
  }

  return true;
}

bool validateOneColumn(SSqlCmd* pCmd, TAOS_FIELD* pColField) {
934 935 936 937 938 939
  const char* msg1 = "too many columns";
  const char* msg2 = "duplicated column names";
  const char* msg3 = "column length too long";
  const char* msg4 = "invalid data types";
  const char* msg5 = "invalid column name";
  const char* msg6 = "invalid column length";
H
hzcheng 已提交
940

941 942
  assert(pCmd->numOfClause == 1);
  SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, pCmd->clauseIndex, 0);
S
slguan 已提交
943
  SMeterMeta*     pMeterMeta = pMeterMetaInfo->pMeterMeta;
H
hzcheng 已提交
944 945 946 947

  // no more max columns
  if (pMeterMeta->numOfColumns >= TSDB_MAX_COLUMNS ||
      pMeterMeta->numOfTags + pMeterMeta->numOfColumns >= TSDB_MAX_COLUMNS) {
948
    invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1);
H
hzcheng 已提交
949 950 951 952
    return false;
  }

  if (pColField->type < TSDB_DATA_TYPE_BOOL || pColField->type > TSDB_DATA_TYPE_NCHAR) {
953
    invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg4);
H
hzcheng 已提交
954 955 956 957
    return false;
  }

  if (validateColumnName(pColField->name) != TSDB_CODE_SUCCESS) {
958
    invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg5);
H
hzcheng 已提交
959 960 961 962 963 964 965 966 967 968 969
    return false;
  }

  SSchema* pSchema = tsGetSchema(pMeterMeta);
  int32_t  nLen = 0;

  for (int32_t i = 0; i < pMeterMeta->numOfColumns; ++i) {
    nLen += pSchema[i].bytes;
  }

  if (pColField->bytes <= 0) {
970
    invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg6);
H
hzcheng 已提交
971 972 973 974 975
    return false;
  }

  // length less than TSDB_MAX_BYTES_PER_ROW
  if (nLen + pColField->bytes > TSDB_MAX_BYTES_PER_ROW) {
976
    invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3);
H
hzcheng 已提交
977 978 979 980 981 982
    return false;
  }

  // field name must be unique
  for (int32_t i = 0; i < pMeterMeta->numOfTags + pMeterMeta->numOfColumns; ++i) {
    if (strncasecmp(pColField->name, pSchema[i].name, TSDB_COL_NAME_LEN) == 0) {
983
      invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2);
H
hzcheng 已提交
984 985 986 987 988 989 990 991
      return false;
    }
  }

  return true;
}

/* is contained in pFieldList or not */
H
hjxilinx 已提交
992
static bool has(tFieldList* pFieldList, int32_t startIdx, const char* name) {
H
hzcheng 已提交
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
  for (int32_t j = startIdx; j < pFieldList->nField; ++j) {
    if (strncasecmp(name, pFieldList->p[j].name, TSDB_COL_NAME_LEN) == 0) return true;
  }

  return false;
}

static char* getAccountId(SSqlObj* pSql) { return pSql->pTscObj->acctId; }

static void getCurrentDBName(SSqlObj* pSql, SSQLToken* pDBToken) {
  pDBToken->z = pSql->pTscObj->db;
  pDBToken->n = strlen(pSql->pTscObj->db);
}

/* length limitation, strstr cannot be applied */
static bool hasSpecifyDB(SSQLToken* pTableName) {
  for (int32_t i = 0; i < pTableName->n; ++i) {
    if (pTableName->z[i] == TS_PATH_DELIMITER[0]) {
      return true;
    }
  }

  return false;
}

1018
int32_t setObjFullName(char* fullName, const char* account, SSQLToken* pDB, SSQLToken* tableName, int32_t* xlen) {
H
hzcheng 已提交
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
  int32_t totalLen = 0;

  if (account != NULL) {
    int32_t len = strlen(account);
    strcpy(fullName, account);
    fullName[len] = TS_PATH_DELIMITER[0];
    totalLen += (len + 1);
  }

  /* db name is not specified, the tableName dose not include db name */
  if (pDB != NULL) {
    if (pDB->n > TSDB_DB_NAME_LEN) {
      return TSDB_CODE_INVALID_SQL;
    }

    memcpy(&fullName[totalLen], pDB->z, pDB->n);
    totalLen += pDB->n;
  }

  if (tableName != NULL) {
    if (pDB != NULL) {
      fullName[totalLen] = TS_PATH_DELIMITER[0];
      totalLen += 1;

      /* here we only check the table name length limitation */
      if (tableName->n > TSDB_METER_NAME_LEN) {
        return TSDB_CODE_INVALID_SQL;
      }
    } else {  // pDB == NULL, the db prefix name is specified in tableName
      /* the length limitation includes tablename + dbname + sep */
      if (tableName->n > TSDB_METER_NAME_LEN + TSDB_DB_NAME_LEN + tListLen(TS_PATH_DELIMITER)) {
        return TSDB_CODE_INVALID_SQL;
      }
    }

    memcpy(&fullName[totalLen], tableName->z, tableName->n);
    totalLen += tableName->n;
  }

  if (xlen != NULL) {
    *xlen = totalLen;
  }
S
slguan 已提交
1061 1062 1063 1064 1065

  if (totalLen < TSDB_METER_ID_LEN) {
    fullName[totalLen] = 0;
  }

H
hzcheng 已提交
1066 1067 1068
  return (totalLen <= TSDB_METER_ID_LEN) ? TSDB_CODE_SUCCESS : TSDB_CODE_INVALID_SQL;
}

S
slguan 已提交
1069
static void extractColumnNameFromString(tSQLExprItem* pItem) {
H
hzcheng 已提交
1070
  if (pItem->pNode->nSQLOptr == TK_STRING) {
S
slguan 已提交
1071
    pItem->pNode->val.nLen = strdequote(pItem->pNode->val.pz);
H
hzcheng 已提交
1072 1073 1074 1075
    pItem->pNode->nSQLOptr = TK_ID;

    SSQLToken* pIdToken = &pItem->pNode->colInfo;
    pIdToken->type = TK_ID;
S
slguan 已提交
1076 1077
    pIdToken->z = pItem->pNode->val.pz;
    pIdToken->n = pItem->pNode->val.nLen;
H
hzcheng 已提交
1078 1079 1080
  }
}

1081
int32_t parseSelectClause(SSqlCmd* pCmd, int32_t clauseIndex, tSQLExprList* pSelection, bool isSTable) {
H
hzcheng 已提交
1082 1083
  assert(pSelection != NULL && pCmd != NULL);

1084
  const char* msg1 = "invalid column name, or illegal column type";
1085 1086
  const char* msg2 = "functions can not be mixed up";
  const char* msg3 = "not support query expression";
1087
  const char* msg4 = "columns from different table mixed up in arithmetic expression";
H
hjxilinx 已提交
1088
  const char* msg5 = "invalid function name";
1089
  
1090
  SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, clauseIndex);
1091

H
hzcheng 已提交
1092
  for (int32_t i = 0; i < pSelection->nExpr; ++i) {
1093
    int32_t       outputIndex = pQueryInfo->fieldsInfo.numOfOutputCols;
H
hzcheng 已提交
1094 1095
    tSQLExprItem* pItem = &pSelection->a[i];

S
slguan 已提交
1096 1097 1098
    // project on all fields
    if (pItem->pNode->nSQLOptr == TK_ALL || pItem->pNode->nSQLOptr == TK_ID || pItem->pNode->nSQLOptr == TK_STRING) {
      // it is actually a function, but the function name is invalid
H
hzcheng 已提交
1099
      if (pItem->pNode->nSQLOptr == TK_ID && (pItem->pNode->colInfo.z == NULL && pItem->pNode->colInfo.n == 0)) {
H
hjxilinx 已提交
1100
        return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg5);
H
hzcheng 已提交
1101 1102
      }

S
slguan 已提交
1103 1104
      // if the name of column is quoted, remove it and set the right information for later process
      extractColumnNameFromString(pItem);
1105

1106
      pQueryInfo->type |= TSDB_QUERY_TYPE_PROJECTION_QUERY;
S
slguan 已提交
1107 1108

      // select table_name1.field_name1, table_name2.field_name2  from table_name1, table_name2
1109
      if (addProjectionExprAndResultField(pQueryInfo, pItem) != TSDB_CODE_SUCCESS) {
S
slguan 已提交
1110
        return TSDB_CODE_INVALID_SQL;
H
hzcheng 已提交
1111 1112
      }
    } else if (pItem->pNode->nSQLOptr >= TK_COUNT && pItem->pNode->nSQLOptr <= TK_LAST_ROW) {
S
slguan 已提交
1113
      // sql function in selection clause, append sql function info in pSqlCmd structure sequentially
1114
      if (addExprAndResultField(pQueryInfo, outputIndex, pItem) != TSDB_CODE_SUCCESS) {
H
hzcheng 已提交
1115 1116 1117 1118
        return TSDB_CODE_INVALID_SQL;
      }

    } else if (pItem->pNode->nSQLOptr >= TK_PLUS && pItem->pNode->nSQLOptr <= TK_REM) {
S
slguan 已提交
1119
      // arithmetic function in select
1120 1121
      SColumnList columnList = {0};
      if (validateArithmeticSQLExpr(pItem->pNode, pQueryInfo, &columnList) != TSDB_CODE_SUCCESS) {
1122
        return invalidSqlErrMsg(pQueryInfo->msg, msg1);
H
hzcheng 已提交
1123
      }
1124 1125 1126 1127 1128 1129 1130
      
      int32_t tableIndex = columnList.ids[0].tableIndex;
      for(int32_t f = 1; f < columnList.num; ++f) {
        if (columnList.ids[f].tableIndex != tableIndex) {
          return invalidSqlErrMsg(pQueryInfo->msg, msg4);
        }
      }
H
hzcheng 已提交
1131 1132 1133 1134

      char  arithmeticExprStr[1024] = {0};
      char* p = arithmeticExprStr;

S
slguan 已提交
1135
      if (buildArithmeticExprString(pItem->pNode, &p) != TSDB_CODE_SUCCESS) {
H
hzcheng 已提交
1136 1137 1138 1139
        return TSDB_CODE_INVALID_SQL;
      }

      // expr string is set as the parameter of function
1140
      SColumnIndex index = {.tableIndex = tableIndex};
1141
      SSqlExpr*    pExpr = tscSqlExprInsert(pQueryInfo, outputIndex, TSDB_FUNC_ARITHM, &index, TSDB_DATA_TYPE_DOUBLE,
S
slguan 已提交
1142
                                         sizeof(double), sizeof(double));
1143
      addExprParams(pExpr, arithmeticExprStr, TSDB_DATA_TYPE_BINARY, strlen(arithmeticExprStr), index.tableIndex);
H
hzcheng 已提交
1144 1145 1146 1147 1148 1149 1150 1151

      /* todo alias name should use the original sql string */
      if (pItem->aliasName != NULL) {
        strncpy(pExpr->aliasName, pItem->aliasName, TSDB_COL_NAME_LEN);
      } else {
        strncpy(pExpr->aliasName, arithmeticExprStr, TSDB_COL_NAME_LEN);
      }

1152
      insertResultField(pQueryInfo, i, &columnList, sizeof(double), TSDB_DATA_TYPE_DOUBLE, pExpr->aliasName);
H
hzcheng 已提交
1153 1154 1155 1156 1157
    } else {
      /*
       * not support such expression
       * e.g., select 12+5 from table_name
       */
1158
      return invalidSqlErrMsg(pQueryInfo->msg, msg3);
H
hzcheng 已提交
1159 1160
    }

1161
    if (pQueryInfo->fieldsInfo.numOfOutputCols > TSDB_MAX_COLUMNS) {
H
hzcheng 已提交
1162 1163 1164 1165
      return TSDB_CODE_INVALID_SQL;
    }
  }

1166 1167
  if (!functionCompatibleCheck(pQueryInfo)) {
    return invalidSqlErrMsg(pQueryInfo->msg, msg2);
H
hzcheng 已提交
1168 1169
  }

1170 1171 1172
  if (isSTable) {
    pQueryInfo->type |= TSDB_QUERY_TYPE_STABLE_QUERY;
    SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0);
H
hzcheng 已提交
1173

1174
    if (tscQueryMetricTags(pQueryInfo)) {  // local handle the metric tag query
S
slguan 已提交
1175
      pCmd->count = pMeterMetaInfo->pMeterMeta->numOfColumns;  // the number of meter schema, tricky.
1176
      pQueryInfo->command = TSDB_SQL_RETRIEVE_TAGS;
H
hzcheng 已提交
1177 1178 1179 1180 1181 1182
    }

    /*
     * transfer sql functions that need secondary merge into another format
     * in dealing with metric queries such as: count/first/last
     */
1183
    tscTansformSQLFunctionForSTableQuery(pQueryInfo);
H
hzcheng 已提交
1184

1185
    if (hasUnsupportFunctionsForSTableQuery(pQueryInfo)) {
H
hzcheng 已提交
1186 1187 1188 1189 1190 1191 1192
      return TSDB_CODE_INVALID_SQL;
    }
  }

  return TSDB_CODE_SUCCESS;
}

1193
int32_t insertResultField(SQueryInfo* pQueryInfo, int32_t outputIndex, SColumnList* pIdList, int16_t bytes, int8_t type,
H
hzcheng 已提交
1194
                          char* fieldName) {
S
slguan 已提交
1195
  for (int32_t i = 0; i < pIdList->num; ++i) {
1196
    tscColumnBaseInfoInsert(pQueryInfo, &(pIdList->ids[i]));
H
hzcheng 已提交
1197 1198
  }

1199
  tscFieldInfoSetValue(&pQueryInfo->fieldsInfo, outputIndex, type, fieldName, bytes);
H
hzcheng 已提交
1200 1201 1202
  return TSDB_CODE_SUCCESS;
}

1203 1204 1205
SSqlExpr* doAddProjectCol(SQueryInfo* pQueryInfo, int32_t outputIndex, int32_t colIdx, int32_t tableIndex) {
  SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, tableIndex);
  SMeterMeta*     pMeterMeta = pMeterMetaInfo->pMeterMeta;
H
hzcheng 已提交
1206

S
slguan 已提交
1207 1208
  SSchema* pSchema = tsGetColumnSchema(pMeterMeta, colIdx);
  int32_t  numOfCols = pMeterMeta->numOfColumns;
H
hzcheng 已提交
1209

S
slguan 已提交
1210
  int16_t functionId = (int16_t)((colIdx >= numOfCols) ? TSDB_FUNC_TAGPRJ : TSDB_FUNC_PRJ);
H
hzcheng 已提交
1211

S
slguan 已提交
1212
  if (functionId == TSDB_FUNC_TAGPRJ) {
1213 1214
    addRequiredTagColumn(pQueryInfo, colIdx - numOfCols, tableIndex);
    pQueryInfo->type = TSDB_QUERY_TYPE_STABLE_QUERY;
S
slguan 已提交
1215
  } else {
1216
    pQueryInfo->type = TSDB_QUERY_TYPE_PROJECTION_QUERY;
H
hzcheng 已提交
1217 1218
  }

S
slguan 已提交
1219 1220
  SColumnIndex index = {tableIndex, colIdx};
  SSqlExpr*    pExpr =
1221
      tscSqlExprInsert(pQueryInfo, outputIndex, functionId, &index, pSchema->type, pSchema->bytes, pSchema->bytes);
S
slguan 已提交
1222 1223

  return pExpr;
H
hzcheng 已提交
1224 1225
}

1226 1227
void addRequiredTagColumn(SQueryInfo* pQueryInfo, int32_t tagColIndex, int32_t tableIndex) {
  SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, tableIndex);
S
slguan 已提交
1228 1229 1230

  if (pMeterMetaInfo->numOfTags == 0 || pMeterMetaInfo->tagColumnIndex[pMeterMetaInfo->numOfTags - 1] < tagColIndex) {
    pMeterMetaInfo->tagColumnIndex[pMeterMetaInfo->numOfTags++] = tagColIndex;
H
hzcheng 已提交
1231
  } else {  // find the appropriate position
S
slguan 已提交
1232 1233
    for (int32_t i = 0; i < pMeterMetaInfo->numOfTags; ++i) {
      if (tagColIndex > pMeterMetaInfo->tagColumnIndex[i]) {
H
hzcheng 已提交
1234
        continue;
S
slguan 已提交
1235
      } else if (tagColIndex == pMeterMetaInfo->tagColumnIndex[i]) {
H
hzcheng 已提交
1236 1237
        break;
      } else {
S
slguan 已提交
1238 1239
        memmove(&pMeterMetaInfo->tagColumnIndex[i + 1], &pMeterMetaInfo->tagColumnIndex[i],
                sizeof(pMeterMetaInfo->tagColumnIndex[0]) * (pMeterMetaInfo->numOfTags - i));
H
hzcheng 已提交
1240

S
slguan 已提交
1241 1242 1243
        pMeterMetaInfo->tagColumnIndex[i] = tagColIndex;

        pMeterMetaInfo->numOfTags++;
H
hzcheng 已提交
1244 1245 1246 1247 1248 1249
        break;
      }
    }
  }

  // plus one means tbname
S
slguan 已提交
1250
  assert(tagColIndex >= -1 && tagColIndex < TSDB_MAX_TAGS && pMeterMetaInfo->numOfTags <= TSDB_MAX_TAGS + 1);
H
hzcheng 已提交
1251 1252
}

1253 1254
static void addProjectQueryCol(SQueryInfo* pQueryInfo, int32_t startPos, SColumnIndex* pIndex, tSQLExprItem* pItem) {
  SSqlExpr* pExpr = doAddProjectCol(pQueryInfo, startPos, pIndex->columnIndex, pIndex->tableIndex);
H
hzcheng 已提交
1255

1256 1257
  SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, pIndex->tableIndex);
  SMeterMeta*     pMeterMeta = pMeterMetaInfo->pMeterMeta;
H
hzcheng 已提交
1258

S
slguan 已提交
1259
  SSchema* pSchema = tsGetColumnSchema(pMeterMeta, pIndex->columnIndex);
H
hzcheng 已提交
1260

S
slguan 已提交
1261
  char* colName = (pItem->aliasName == NULL) ? pSchema->name : pItem->aliasName;
H
hzcheng 已提交
1262

S
slguan 已提交
1263 1264 1265
  SColumnList ids = {0};
  ids.num = 1;
  ids.ids[0] = *pIndex;
H
hzcheng 已提交
1266

S
slguan 已提交
1267 1268 1269
  if (pIndex->columnIndex >= pMeterMeta->numOfColumns || pIndex->columnIndex == TSDB_TBNAME_COLUMN_INDEX) {
    ids.num = 0;
  }
H
hzcheng 已提交
1270

1271
  insertResultField(pQueryInfo, startPos, &ids, pExpr->resBytes, pExpr->resType, colName);
S
slguan 已提交
1272
}
H
hzcheng 已提交
1273

1274 1275 1276 1277
void tscAddSpecialColumnForSelect(SQueryInfo* pQueryInfo, int32_t outputColIndex, int16_t functionId,
                                  SColumnIndex* pIndex, SSchema* pColSchema, int16_t flag) {
  SSqlExpr* pExpr = tscSqlExprInsert(pQueryInfo, outputColIndex, functionId, pIndex, pColSchema->type,
                                     pColSchema->bytes, pColSchema->bytes);
H
hzcheng 已提交
1278

S
slguan 已提交
1279 1280 1281 1282
  SColumnList ids = getColumnList(1, pIndex->tableIndex, pIndex->columnIndex);
  if (TSDB_COL_IS_TAG(flag)) {
    ids.num = 0;
  }
H
hzcheng 已提交
1283

1284
  insertResultField(pQueryInfo, outputColIndex, &ids, pColSchema->bytes, pColSchema->type, pColSchema->name);
S
slguan 已提交
1285 1286 1287

  pExpr->colInfo.flag = flag;
  if (TSDB_COL_IS_TAG(flag)) {
1288
    addRequiredTagColumn(pQueryInfo, pIndex->columnIndex, pIndex->tableIndex);
S
slguan 已提交
1289 1290 1291
  }
}

1292 1293
static int32_t doAddProjectionExprAndResultFields(SQueryInfo* pQueryInfo, SColumnIndex* pIndex, int32_t startPos) {
  SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, pIndex->tableIndex);
S
slguan 已提交
1294 1295 1296 1297 1298

  int32_t     numOfTotalColumns = 0;
  SMeterMeta* pMeterMeta = pMeterMetaInfo->pMeterMeta;
  SSchema*    pSchema = tsGetSchema(pMeterMeta);

1299
  if (UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo)) {
S
slguan 已提交
1300 1301 1302 1303 1304 1305
    numOfTotalColumns = pMeterMeta->numOfColumns + pMeterMeta->numOfTags;
  } else {
    numOfTotalColumns = pMeterMeta->numOfColumns;
  }

  for (int32_t j = 0; j < numOfTotalColumns; ++j) {
1306
    doAddProjectCol(pQueryInfo, startPos + j, j, pIndex->tableIndex);
S
slguan 已提交
1307 1308 1309 1310 1311 1312 1313 1314

    pIndex->columnIndex = j;
    SColumnList ids = {0};
    ids.ids[0] = *pIndex;

    // tag columns do not add to source list
    ids.num = (j >= pMeterMeta->numOfColumns) ? 0 : 1;

1315
    insertResultField(pQueryInfo, startPos + j, &ids, pSchema[j].bytes, pSchema[j].type, pSchema[j].name);
S
slguan 已提交
1316 1317 1318 1319 1320
  }

  return numOfTotalColumns;
}

1321
int32_t addProjectionExprAndResultField(SQueryInfo* pQueryInfo, tSQLExprItem* pItem) {
S
slguan 已提交
1322 1323 1324
  const char* msg0 = "invalid column name";
  const char* msg1 = "tag for table query is not allowed";

1325
  int32_t startPos = pQueryInfo->fieldsInfo.numOfOutputCols;
S
slguan 已提交
1326 1327 1328

  if (pItem->pNode->nSQLOptr == TK_ALL) {  // project on all fields
    SColumnIndex index = COLUMN_INDEX_INITIALIZER;
1329
    if (getTableIndexByName(&pItem->pNode->colInfo, pQueryInfo, &index) != TSDB_CODE_SUCCESS) {
1330
      return invalidSqlErrMsg(pQueryInfo->msg, msg0);
S
slguan 已提交
1331 1332 1333 1334
    }

    // all meters columns are required
    if (index.tableIndex == COLUMN_INDEX_INITIAL_VAL) {  // all table columns are required.
1335
      for (int32_t i = 0; i < pQueryInfo->numOfTables; ++i) {
S
slguan 已提交
1336
        index.tableIndex = i;
1337
        int32_t inc = doAddProjectionExprAndResultFields(pQueryInfo, &index, startPos);
S
slguan 已提交
1338
        startPos += inc;
H
hzcheng 已提交
1339 1340
      }
    } else {
1341
      doAddProjectionExprAndResultFields(pQueryInfo, &index, startPos);
S
slguan 已提交
1342 1343 1344 1345
    }
  } else if (pItem->pNode->nSQLOptr == TK_ID) {  // simple column projection query
    SColumnIndex index = COLUMN_INDEX_INITIALIZER;

1346
    if (getColumnIndexByName(&pItem->pNode->colInfo, pQueryInfo, &index) != TSDB_CODE_SUCCESS) {
1347
      return invalidSqlErrMsg(pQueryInfo->msg, msg0);
S
slguan 已提交
1348
    }
H
hzcheng 已提交
1349

S
slguan 已提交
1350
    if (index.columnIndex == TSDB_TBNAME_COLUMN_INDEX) {
H
hjxilinx 已提交
1351
      SSchema colSchema = {.type = TSDB_DATA_TYPE_BINARY, .bytes = TSDB_METER_NAME_LEN};
S
slguan 已提交
1352
      strcpy(colSchema.name, TSQL_TBNAME_L);
1353

1354 1355
      pQueryInfo->type = TSDB_QUERY_TYPE_STABLE_QUERY;
      tscAddSpecialColumnForSelect(pQueryInfo, startPos, TSDB_FUNC_TAGPRJ, &index, &colSchema, true);
S
slguan 已提交
1356
    } else {
1357
      SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, index.tableIndex);
S
slguan 已提交
1358
      SMeterMeta*     pMeterMeta = pMeterMetaInfo->pMeterMeta;
H
hzcheng 已提交
1359

S
slguan 已提交
1360
      if (index.columnIndex >= pMeterMeta->numOfColumns && UTIL_METER_IS_NOMRAL_METER(pMeterMetaInfo)) {
1361
        return invalidSqlErrMsg(pQueryInfo->msg, msg1);
H
hzcheng 已提交
1362 1363
      }

1364
      addProjectQueryCol(pQueryInfo, startPos, &index, pItem);
H
hzcheng 已提交
1365 1366 1367 1368 1369 1370 1371 1372
    }
  } else {
    return TSDB_CODE_INVALID_SQL;
  }

  return TSDB_CODE_SUCCESS;
}

1373
static int32_t setExprInfoForFunctions(SQueryInfo* pQueryInfo, SSchema* pSchema, int32_t functionID, char* aliasName,
S
slguan 已提交
1374
                                       int32_t resColIdx, SColumnIndex* pColIndex) {
H
hzcheng 已提交
1375 1376 1377
  int16_t type = 0;
  int16_t bytes = 0;

S
slguan 已提交
1378
  char        columnName[TSDB_COL_NAME_LEN] = {0};
1379
  const char* msg1 = "not support column types";
H
hzcheng 已提交
1380 1381

  if (functionID == TSDB_FUNC_SPREAD) {
S
slguan 已提交
1382 1383 1384
    if (pSchema[pColIndex->columnIndex].type == TSDB_DATA_TYPE_BINARY ||
        pSchema[pColIndex->columnIndex].type == TSDB_DATA_TYPE_NCHAR ||
        pSchema[pColIndex->columnIndex].type == TSDB_DATA_TYPE_BOOL) {
1385
      invalidSqlErrMsg(pQueryInfo->msg, msg1);
H
hzcheng 已提交
1386 1387 1388 1389 1390 1391
      return -1;
    } else {
      type = TSDB_DATA_TYPE_DOUBLE;
      bytes = tDataTypeDesc[type].nSize;
    }
  } else {
S
slguan 已提交
1392 1393
    type = pSchema[pColIndex->columnIndex].type;
    bytes = pSchema[pColIndex->columnIndex].bytes;
H
hzcheng 已提交
1394 1395 1396 1397 1398
  }

  if (aliasName != NULL) {
    strcpy(columnName, aliasName);
  } else {
S
slguan 已提交
1399
    getRevisedName(columnName, functionID, TSDB_COL_NAME_LEN, pSchema[pColIndex->columnIndex].name);
H
hzcheng 已提交
1400 1401
  }

1402
  tscSqlExprInsert(pQueryInfo, resColIdx, functionID, pColIndex, type, bytes, bytes);
H
hzcheng 已提交
1403

S
slguan 已提交
1404 1405
  // for point interpolation/last_row query, we need the timestamp column to be loaded
  SColumnIndex index = {.tableIndex = pColIndex->tableIndex, .columnIndex = PRIMARYKEY_TIMESTAMP_COL_INDEX};
H
hzcheng 已提交
1406
  if (functionID == TSDB_FUNC_INTERP || functionID == TSDB_FUNC_LAST_ROW) {
1407
    tscColumnBaseInfoInsert(pQueryInfo, &index);
H
hzcheng 已提交
1408 1409
  }

S
slguan 已提交
1410
  SColumnList ids = getColumnList(1, pColIndex->tableIndex, pColIndex->columnIndex);
1411
  insertResultField(pQueryInfo, resColIdx, &ids, bytes, type, columnName);
H
hzcheng 已提交
1412 1413 1414 1415

  return TSDB_CODE_SUCCESS;
}

1416
int32_t addExprAndResultField(SQueryInfo* pQueryInfo, int32_t colIdx, tSQLExprItem* pItem) {
S
slguan 已提交
1417 1418 1419
  SMeterMetaInfo* pMeterMetaInfo = NULL;
  int32_t         optr = pItem->pNode->nSQLOptr;

1420
  const char* msg1 = "not support column types";
S
slguan 已提交
1421
  const char* msg2 = "invalid parameters";
1422
  const char* msg3 = "illegal column name";
S
slguan 已提交
1423
  const char* msg4 = "invalid table name";
1424
  const char* msg5 = "parameter is out of range [0, 100]";
S
slguan 已提交
1425
  const char* msg6 = "function applied to tags not allowed";
H
hzcheng 已提交
1426 1427 1428 1429 1430

  switch (optr) {
    case TK_COUNT: {
      if (pItem->pNode->pParam != NULL && pItem->pNode->pParam->nExpr != 1) {
        /* more than one parameter for count() function */
1431
        return invalidSqlErrMsg(pQueryInfo->msg, msg2);
H
hzcheng 已提交
1432 1433 1434 1435
      }

      int16_t functionID = 0;
      if (changeFunctionID(optr, &functionID) != TSDB_CODE_SUCCESS) {
S
slguan 已提交
1436
        return TSDB_CODE_INVALID_SQL;
H
hzcheng 已提交
1437 1438
      }

S
slguan 已提交
1439
      SColumnIndex index = COLUMN_INDEX_INITIALIZER;
H
hzcheng 已提交
1440 1441 1442 1443

      if (pItem->pNode->pParam != NULL) {
        SSQLToken* pToken = &pItem->pNode->pParam->a[0].pNode->colInfo;
        if (pToken->z == NULL || pToken->n == 0) {
1444
          return invalidSqlErrMsg(pQueryInfo->msg, msg3);
H
hzcheng 已提交
1445 1446
        }

S
slguan 已提交
1447 1448 1449 1450 1451 1452
        tSQLExprItem* pParamElem = &pItem->pNode->pParam->a[0];
        if (pParamElem->pNode->nSQLOptr == TK_ALL) {
          // select table.*
          // check if the table name is valid or not
          SSQLToken tmpToken = pParamElem->pNode->colInfo;

1453 1454
          if (getTableIndexByName(&tmpToken, pQueryInfo, &index) != TSDB_CODE_SUCCESS) {
            return invalidSqlErrMsg(pQueryInfo->msg, msg4);
S
slguan 已提交
1455 1456 1457 1458
          }

          index = (SColumnIndex){0, PRIMARYKEY_TIMESTAMP_COL_INDEX};
          int32_t size = tDataTypeDesc[TSDB_DATA_TYPE_BIGINT].nSize;
1459
          tscSqlExprInsert(pQueryInfo, colIdx, functionID, &index, TSDB_DATA_TYPE_BIGINT, size, size);
H
hzcheng 已提交
1460
        } else {
S
slguan 已提交
1461
          // count the number of meters created according to the metric
1462
          if (getColumnIndexByName(pToken, pQueryInfo, &index) != TSDB_CODE_SUCCESS) {
1463
            return invalidSqlErrMsg(pQueryInfo->msg, msg3);
S
slguan 已提交
1464 1465
          }

1466
          pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, index.tableIndex);
S
slguan 已提交
1467 1468 1469 1470

          // count tag is equalled to count(tbname)
          if (index.columnIndex >= pMeterMetaInfo->pMeterMeta->numOfColumns) {
            index.columnIndex = TSDB_TBNAME_COLUMN_INDEX;
H
hzcheng 已提交
1471 1472
          }

S
slguan 已提交
1473
          int32_t size = tDataTypeDesc[TSDB_DATA_TYPE_BIGINT].nSize;
1474
          tscSqlExprInsert(pQueryInfo, colIdx, functionID, &index, TSDB_DATA_TYPE_BIGINT, size, size);
H
hzcheng 已提交
1475
        }
S
slguan 已提交
1476 1477 1478 1479
      } else {  // count(*) is equalled to count(primary_timestamp_key)
        index = (SColumnIndex){0, PRIMARYKEY_TIMESTAMP_COL_INDEX};

        int32_t size = tDataTypeDesc[TSDB_DATA_TYPE_BIGINT].nSize;
1480
        tscSqlExprInsert(pQueryInfo, colIdx, functionID, &index, TSDB_DATA_TYPE_BIGINT, size, size);
H
hzcheng 已提交
1481 1482 1483 1484 1485 1486
      }

      char columnName[TSDB_COL_NAME_LEN] = {0};
      getColumnName(pItem, columnName, TSDB_COL_NAME_LEN);

      // count always use the primary timestamp key column, which is 0.
S
slguan 已提交
1487 1488
      SColumnList ids = getColumnList(1, index.tableIndex, index.columnIndex);

1489
      insertResultField(pQueryInfo, colIdx, &ids, sizeof(int64_t), TSDB_DATA_TYPE_BIGINT, columnName);
S
slguan 已提交
1490
      return TSDB_CODE_SUCCESS;
H
hzcheng 已提交
1491 1492 1493
    }
    case TK_SUM:
    case TK_AVG:
S
slguan 已提交
1494
    case TK_TWA:
H
hzcheng 已提交
1495 1496 1497 1498 1499 1500 1501 1502 1503
    case TK_MIN:
    case TK_MAX:
    case TK_DIFF:
    case TK_STDDEV:
    case TK_LEASTSQUARES: {
      // 1. valid the number of parameters
      if (pItem->pNode->pParam == NULL || (optr != TK_LEASTSQUARES && pItem->pNode->pParam->nExpr != 1) ||
          (optr == TK_LEASTSQUARES && pItem->pNode->pParam->nExpr != 3)) {
        /* no parameters or more than one parameter for function */
1504
        return invalidSqlErrMsg(pQueryInfo->msg, msg2);
H
hzcheng 已提交
1505 1506 1507 1508
      }

      tSQLExprItem* pParamElem = &(pItem->pNode->pParam->a[0]);
      if (pParamElem->pNode->nSQLOptr != TK_ALL && pParamElem->pNode->nSQLOptr != TK_ID) {
1509
        return invalidSqlErrMsg(pQueryInfo->msg, msg2);
H
hzcheng 已提交
1510 1511
      }

S
slguan 已提交
1512
      SColumnIndex index = COLUMN_INDEX_INITIALIZER;
1513
      if ((getColumnIndexByName(&pParamElem->pNode->colInfo, pQueryInfo, &index) != TSDB_CODE_SUCCESS) ||
1514
          index.columnIndex == TSDB_TBNAME_COLUMN_INDEX) {
1515
        return invalidSqlErrMsg(pQueryInfo->msg, msg3);
H
hzcheng 已提交
1516 1517 1518
      }

      // 2. check if sql function can be applied on this column data type
1519
      pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, index.tableIndex);
S
slguan 已提交
1520 1521 1522
      SSchema* pSchema = tsGetColumnSchema(pMeterMetaInfo->pMeterMeta, index.columnIndex);
      int16_t  colType = pSchema->type;

H
hjxilinx 已提交
1523
      if (colType <= TSDB_DATA_TYPE_BOOL || colType >= TSDB_DATA_TYPE_BINARY) {
1524
        return invalidSqlErrMsg(pQueryInfo->msg, msg1);
H
hzcheng 已提交
1525 1526 1527 1528 1529 1530 1531
      }

      char columnName[TSDB_COL_NAME_LEN] = {0};
      getColumnName(pItem, columnName, TSDB_COL_NAME_LEN);

      int16_t resultType = 0;
      int16_t resultSize = 0;
S
slguan 已提交
1532
      int16_t intermediateResSize = 0;
H
hzcheng 已提交
1533 1534 1535

      int16_t functionID = 0;
      if (changeFunctionID(optr, &functionID) != TSDB_CODE_SUCCESS) {
S
slguan 已提交
1536
        return TSDB_CODE_INVALID_SQL;
H
hzcheng 已提交
1537 1538
      }

S
slguan 已提交
1539 1540 1541 1542
      if (getResultDataInfo(pSchema->type, pSchema->bytes, functionID, 0, &resultType, &resultSize,
                            &intermediateResSize, 0, false) != TSDB_CODE_SUCCESS) {
        return TSDB_CODE_INVALID_SQL;
      }
H
hzcheng 已提交
1543

S
slguan 已提交
1544
      // set the first column ts for diff query
H
hzcheng 已提交
1545 1546
      if (optr == TK_DIFF) {
        colIdx += 1;
S
slguan 已提交
1547
        SColumnIndex indexTS = {.tableIndex = index.tableIndex, .columnIndex = 0};
1548 1549
        tscSqlExprInsert(pQueryInfo, 0, TSDB_FUNC_TS_DUMMY, &indexTS, TSDB_DATA_TYPE_TIMESTAMP, TSDB_KEYSIZE,
                         TSDB_KEYSIZE);
H
hzcheng 已提交
1550

S
slguan 已提交
1551
        SColumnList ids = getColumnList(1, 0, 0);
1552
        insertResultField(pQueryInfo, 0, &ids, TSDB_KEYSIZE, TSDB_DATA_TYPE_TIMESTAMP, aAggs[TSDB_FUNC_TS_DUMMY].aName);
H
hzcheng 已提交
1553 1554
      }

S
slguan 已提交
1555 1556
      // functions can not be applied to tags
      if (index.columnIndex >= pMeterMetaInfo->pMeterMeta->numOfColumns) {
1557
        return invalidSqlErrMsg(pQueryInfo->msg, msg6);
S
slguan 已提交
1558 1559
      }

1560
      SSqlExpr* pExpr = tscSqlExprInsert(pQueryInfo, colIdx, functionID, &index, resultType, resultSize, resultSize);
H
hzcheng 已提交
1561 1562 1563 1564 1565 1566 1567 1568

      if (optr == TK_LEASTSQUARES) {
        /* set the leastsquares parameters */
        char val[8] = {0};
        if (tVariantDump(&pParamElem[1].pNode->val, val, TSDB_DATA_TYPE_DOUBLE) < 0) {
          return TSDB_CODE_INVALID_SQL;
        }

S
slguan 已提交
1569
        addExprParams(pExpr, val, TSDB_DATA_TYPE_DOUBLE, DOUBLE_BYTES, 0);
H
hzcheng 已提交
1570 1571 1572 1573 1574 1575

        memset(val, 0, tListLen(val));
        if (tVariantDump(&pParamElem[2].pNode->val, val, TSDB_DATA_TYPE_DOUBLE) < 0) {
          return TSDB_CODE_INVALID_SQL;
        }

S
slguan 已提交
1576
        addExprParams(pExpr, val, TSDB_DATA_TYPE_DOUBLE, sizeof(double), 0);
H
hzcheng 已提交
1577 1578
      }

S
slguan 已提交
1579 1580 1581 1582
      SColumnList ids = {0};
      ids.num = 1;
      ids.ids[0] = index;

1583
      insertResultField(pQueryInfo, colIdx, &ids, pExpr->resBytes, pExpr->resType, columnName);
H
hzcheng 已提交
1584

S
slguan 已提交
1585
      return TSDB_CODE_SUCCESS;
H
hzcheng 已提交
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
    }
    case TK_FIRST:
    case TK_LAST:
    case TK_SPREAD:
    case TK_LAST_ROW:
    case TK_INTERP: {
      bool requireAllFields = (pItem->pNode->pParam == NULL);

      int16_t functionID = 0;
      changeFunctionID(optr, &functionID);

      if (!requireAllFields) {
        if (pItem->pNode->pParam->nExpr < 1) {
1599
          return invalidSqlErrMsg(pQueryInfo->msg, msg3);
H
hzcheng 已提交
1600 1601 1602 1603 1604 1605 1606
        }

        /* in first/last function, multiple columns can be add to resultset */

        for (int32_t i = 0; i < pItem->pNode->pParam->nExpr; ++i) {
          tSQLExprItem* pParamElem = &(pItem->pNode->pParam->a[i]);
          if (pParamElem->pNode->nSQLOptr != TK_ALL && pParamElem->pNode->nSQLOptr != TK_ID) {
1607
            return invalidSqlErrMsg(pQueryInfo->msg, msg3);
H
hzcheng 已提交
1608 1609
          }

S
slguan 已提交
1610 1611 1612 1613 1614 1615
          SColumnIndex index = COLUMN_INDEX_INITIALIZER;

          if (pParamElem->pNode->nSQLOptr == TK_ALL) {
            // select table.*
            SSQLToken tmpToken = pParamElem->pNode->colInfo;

1616 1617
            if (getTableIndexByName(&tmpToken, pQueryInfo, &index) != TSDB_CODE_SUCCESS) {
              return invalidSqlErrMsg(pQueryInfo->msg, msg4);
S
slguan 已提交
1618 1619
            }

1620
            pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, index.tableIndex);
S
slguan 已提交
1621 1622 1623 1624
            SSchema* pSchema = tsGetSchema(pMeterMetaInfo->pMeterMeta);

            for (int32_t j = 0; j < pMeterMetaInfo->pMeterMeta->numOfColumns; ++j) {
              index.columnIndex = j;
1625
              if (setExprInfoForFunctions(pQueryInfo, pSchema, functionID, pItem->aliasName, colIdx++, &index) != 0) {
S
slguan 已提交
1626 1627 1628
                return TSDB_CODE_INVALID_SQL;
              }
            }
H
hzcheng 已提交
1629

S
slguan 已提交
1630
          } else {
1631
            if (getColumnIndexByName(&pParamElem->pNode->colInfo, pQueryInfo, &index) != TSDB_CODE_SUCCESS) {
1632
              return invalidSqlErrMsg(pQueryInfo->msg, msg3);
S
slguan 已提交
1633 1634
            }

1635
            pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, index.tableIndex);
S
slguan 已提交
1636 1637 1638 1639
            SSchema* pSchema = tsGetSchema(pMeterMetaInfo->pMeterMeta);

            // functions can not be applied to tags
            if (index.columnIndex >= pMeterMetaInfo->pMeterMeta->numOfColumns) {
1640
              return invalidSqlErrMsg(pQueryInfo->msg, msg6);
S
slguan 已提交
1641 1642
            }

1643
            if (setExprInfoForFunctions(pQueryInfo, pSchema, functionID, pItem->aliasName, colIdx + i, &index) != 0) {
S
slguan 已提交
1644 1645
              return TSDB_CODE_INVALID_SQL;
            }
H
hzcheng 已提交
1646 1647 1648
          }
        }

S
slguan 已提交
1649 1650 1651 1652
        return TSDB_CODE_SUCCESS;
      } else {  // select * from xxx
        int32_t numOfFields = 0;

1653 1654
        for (int32_t j = 0; j < pQueryInfo->numOfTables; ++j) {
          pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, j);
S
slguan 已提交
1655 1656 1657 1658
          SSchema* pSchema = tsGetSchema(pMeterMetaInfo->pMeterMeta);

          for (int32_t i = 0; i < pMeterMetaInfo->pMeterMeta->numOfColumns; ++i) {
            SColumnIndex index = {.tableIndex = j, .columnIndex = i};
1659 1660
            if (setExprInfoForFunctions(pQueryInfo, pSchema, functionID, pItem->aliasName, colIdx + i + j, &index) !=
                0) {
S
slguan 已提交
1661 1662
              return TSDB_CODE_INVALID_SQL;
            }
H
hzcheng 已提交
1663
          }
S
slguan 已提交
1664 1665

          numOfFields += pMeterMetaInfo->pMeterMeta->numOfColumns;
H
hzcheng 已提交
1666 1667
        }

S
slguan 已提交
1668
        return TSDB_CODE_SUCCESS;
H
hzcheng 已提交
1669 1670 1671 1672 1673 1674 1675 1676 1677
      }
    }
    case TK_TOP:
    case TK_BOTTOM:
    case TK_PERCENTILE:
    case TK_APERCENTILE: {
      // 1. valid the number of parameters
      if (pItem->pNode->pParam == NULL || pItem->pNode->pParam->nExpr != 2) {
        /* no parameters or more than one parameter for function */
1678
        return invalidSqlErrMsg(pQueryInfo->msg, msg2);
H
hzcheng 已提交
1679 1680 1681 1682
      }

      tSQLExprItem* pParamElem = &(pItem->pNode->pParam->a[0]);
      if (pParamElem->pNode->nSQLOptr != TK_ID) {
1683
        return invalidSqlErrMsg(pQueryInfo->msg, msg2);
H
hzcheng 已提交
1684 1685 1686 1687 1688
      }

      char columnName[TSDB_COL_NAME_LEN] = {0};
      getColumnName(pItem, columnName, TSDB_COL_NAME_LEN);

S
slguan 已提交
1689
      SColumnIndex index = COLUMN_INDEX_INITIALIZER;
1690
      if (getColumnIndexByName(&pParamElem->pNode->colInfo, pQueryInfo, &index) != TSDB_CODE_SUCCESS) {
1691
        return invalidSqlErrMsg(pQueryInfo->msg, msg3);
S
slguan 已提交
1692 1693
      }

1694
      pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, index.tableIndex);
S
slguan 已提交
1695 1696 1697 1698
      SSchema* pSchema = tsGetSchema(pMeterMetaInfo->pMeterMeta);

      // functions can not be applied to tags
      if (index.columnIndex >= pMeterMetaInfo->pMeterMeta->numOfColumns) {
1699
        return invalidSqlErrMsg(pQueryInfo->msg, msg6);
H
hzcheng 已提交
1700 1701 1702
      }

      // 2. valid the column type
S
slguan 已提交
1703
      int16_t colType = pSchema[index.columnIndex].type;
H
hzcheng 已提交
1704
      if (colType == TSDB_DATA_TYPE_BOOL || colType >= TSDB_DATA_TYPE_BINARY) {
1705
        return invalidSqlErrMsg(pQueryInfo->msg, msg1);
H
hzcheng 已提交
1706 1707 1708 1709
      }

      // 3. valid the parameters
      if (pParamElem[1].pNode->nSQLOptr == TK_ID) {
1710
        return invalidSqlErrMsg(pQueryInfo->msg, msg2);
H
hzcheng 已提交
1711 1712 1713 1714
      }

      tVariant* pVariant = &pParamElem[1].pNode->val;

S
slguan 已提交
1715 1716
      int8_t  resultType = pSchema[index.columnIndex].type;
      int16_t resultSize = pSchema[index.columnIndex].bytes;
H
hzcheng 已提交
1717

H
hjxilinx 已提交
1718 1719
      char    val[8] = {0};
      int32_t numOfAddedColumn = 1;
H
hzcheng 已提交
1720 1721 1722
      if (optr == TK_PERCENTILE || optr == TK_APERCENTILE) {
        tVariantDump(pVariant, val, TSDB_DATA_TYPE_DOUBLE);

L
lihui 已提交
1723
        double dp = GET_DOUBLE_VAL(val);
S
slguan 已提交
1724
        if (dp < 0 || dp > TOP_BOTTOM_QUERY_LIMIT) {
1725
          return invalidSqlErrMsg(pQueryInfo->msg, msg5);
H
hzcheng 已提交
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737
        }

        resultSize = sizeof(double);
        resultType = TSDB_DATA_TYPE_DOUBLE;

        /*
         * sql function transformation
         * for dp = 0, it is actually min,
         * for dp = 100, it is max,
         */
        int16_t functionId = 0;
        if (changeFunctionID(optr, &functionId) != TSDB_CODE_SUCCESS) {
S
slguan 已提交
1738
          return TSDB_CODE_INVALID_SQL;
H
hzcheng 已提交
1739 1740
        }

1741
        SSqlExpr* pExpr = tscSqlExprInsert(pQueryInfo, colIdx, functionId, &index, resultType, resultSize, resultSize);
S
slguan 已提交
1742
        addExprParams(pExpr, val, TSDB_DATA_TYPE_DOUBLE, sizeof(double), 0);
H
hzcheng 已提交
1743 1744 1745 1746 1747
      } else {
        tVariantDump(pVariant, val, TSDB_DATA_TYPE_BIGINT);

        int64_t nTop = *((int32_t*)val);
        if (nTop <= 0 || nTop > 100) {  // todo use macro
1748
          return invalidSqlErrMsg(pQueryInfo->msg, msg5);
H
hzcheng 已提交
1749 1750 1751 1752
        }

        int16_t functionId = 0;
        if (changeFunctionID(optr, &functionId) != TSDB_CODE_SUCCESS) {
S
slguan 已提交
1753
          return TSDB_CODE_INVALID_SQL;
H
hzcheng 已提交
1754
        }
S
slguan 已提交
1755

H
hzcheng 已提交
1756
        // set the first column ts for top/bottom query
S
slguan 已提交
1757
        SColumnIndex index1 = {0, PRIMARYKEY_TIMESTAMP_COL_INDEX};
1758
        tscSqlExprInsert(pQueryInfo, 0, TSDB_FUNC_TS, &index1, TSDB_DATA_TYPE_TIMESTAMP, TSDB_KEYSIZE, TSDB_KEYSIZE);
S
slguan 已提交
1759 1760 1761

        const int32_t TS_COLUMN_INDEX = 0;
        SColumnList   ids = getColumnList(1, 0, TS_COLUMN_INDEX);
1762
        insertResultField(pQueryInfo, TS_COLUMN_INDEX, &ids, TSDB_KEYSIZE, TSDB_DATA_TYPE_TIMESTAMP,
S
slguan 已提交
1763
                          aAggs[TSDB_FUNC_TS].aName);
H
hzcheng 已提交
1764 1765 1766 1767

        colIdx += 1;  // the first column is ts
        numOfAddedColumn += 1;

1768
        SSqlExpr* pExpr = tscSqlExprInsert(pQueryInfo, colIdx, functionId, &index, resultType, resultSize, resultSize);
S
slguan 已提交
1769
        addExprParams(pExpr, val, TSDB_DATA_TYPE_BIGINT, sizeof(int64_t), 0);
H
hzcheng 已提交
1770 1771
      }

S
slguan 已提交
1772
      SColumnList ids = getColumnList(1, 0, index.columnIndex);
1773
      insertResultField(pQueryInfo, colIdx, &ids, resultSize, resultType, columnName);
S
slguan 已提交
1774 1775

      return TSDB_CODE_SUCCESS;
H
hzcheng 已提交
1776 1777
    }
    default:
S
slguan 已提交
1778
      return TSDB_CODE_INVALID_SQL;
H
hzcheng 已提交
1779 1780 1781
  }
}

S
slguan 已提交
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
// todo refactor
static SColumnList getColumnList(int32_t num, int16_t tableIndex, int32_t columnIndex) {
  assert(num == 1 && columnIndex >= -1 && tableIndex >= 0);

  SColumnList columnList = {0};
  columnList.num = num;

  int32_t index = num - 1;
  columnList.ids[index].tableIndex = tableIndex;
  columnList.ids[index].columnIndex = columnIndex;

  return columnList;
}

H
hzcheng 已提交
1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808
void getColumnName(tSQLExprItem* pItem, char* resultFieldName, int32_t nameLength) {
  if (pItem->aliasName != NULL) {
    strncpy(resultFieldName, pItem->aliasName, nameLength);
  } else {
    int32_t len = (pItem->pNode->operand.n < nameLength) ? pItem->pNode->operand.n : nameLength;
    strncpy(resultFieldName, pItem->pNode->operand.z, len);
  }
}

void getRevisedName(char* resultFieldName, int32_t functionId, int32_t maxLen, char* columnName) {
  snprintf(resultFieldName, maxLen, "%s(%s)", aAggs[functionId].aName, columnName);
}

S
slguan 已提交
1809 1810 1811 1812 1813 1814 1815 1816 1817
static bool isTablenameToken(SSQLToken* token) {
  SSQLToken tmpToken = *token;
  SSQLToken tableToken = {0};

  extractTableNameFromToken(&tmpToken, &tableToken);

  return (strncasecmp(TSQL_TBNAME_L, tmpToken.z, tmpToken.n) == 0 && tmpToken.n == strlen(TSQL_TBNAME_L));
}

1818 1819
static int16_t doGetColumnIndex(SQueryInfo* pQueryInfo, int32_t index, SSQLToken* pToken) {
  SMeterMeta* pMeterMeta = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, index)->pMeterMeta;
S
slguan 已提交
1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838

  int32_t  numOfCols = pMeterMeta->numOfColumns + pMeterMeta->numOfTags;
  SSchema* pSchema = tsGetSchema(pMeterMeta);

  int16_t columnIndex = COLUMN_INDEX_INITIAL_VAL;

  for (int16_t i = 0; i < numOfCols; ++i) {
    if (pToken->n != strlen(pSchema[i].name)) {
      continue;
    }

    if (strncasecmp(pSchema[i].name, pToken->z, pToken->n) == 0) {
      columnIndex = i;
    }
  }

  return columnIndex;
}

1839
int32_t doGetColumnIndexByName(SSQLToken* pToken, SQueryInfo* pQueryInfo, SColumnIndex* pIndex) {
S
slguan 已提交
1840 1841 1842 1843 1844 1845 1846 1847 1848 1849
  const char* msg0 = "ambiguous column name";
  const char* msg1 = "invalid column name";

  if (isTablenameToken(pToken)) {
    pIndex->columnIndex = TSDB_TBNAME_COLUMN_INDEX;
  } else if (strncasecmp(pToken->z, DEFAULT_PRIMARY_TIMESTAMP_COL_NAME, pToken->n) == 0) {
    pIndex->columnIndex = PRIMARYKEY_TIMESTAMP_COL_INDEX;
  } else {
    // not specify the table name, try to locate the table index by column name
    if (pIndex->tableIndex == COLUMN_INDEX_INITIAL_VAL) {
1850 1851
      for (int16_t i = 0; i < pQueryInfo->numOfTables; ++i) {
        int16_t colIndex = doGetColumnIndex(pQueryInfo, i, pToken);
S
slguan 已提交
1852 1853 1854

        if (colIndex != COLUMN_INDEX_INITIAL_VAL) {
          if (pIndex->columnIndex != COLUMN_INDEX_INITIAL_VAL) {
1855
            return invalidSqlErrMsg(pQueryInfo->msg, msg0);
S
slguan 已提交
1856 1857 1858 1859 1860 1861 1862
          } else {
            pIndex->tableIndex = i;
            pIndex->columnIndex = colIndex;
          }
        }
      }
    } else {  // table index is valid, get the column index
1863
      int16_t colIndex = doGetColumnIndex(pQueryInfo, pIndex->tableIndex, pToken);
S
slguan 已提交
1864 1865 1866 1867 1868 1869
      if (colIndex != COLUMN_INDEX_INITIAL_VAL) {
        pIndex->columnIndex = colIndex;
      }
    }

    if (pIndex->columnIndex == COLUMN_INDEX_INITIAL_VAL) {
1870
      return invalidSqlErrMsg(pQueryInfo->msg, msg1);
S
slguan 已提交
1871 1872 1873 1874 1875 1876 1877 1878 1879 1880
    }
  }

  if (COLUMN_INDEX_VALIDE(*pIndex)) {
    return TSDB_CODE_SUCCESS;
  } else {
    return TSDB_CODE_INVALID_SQL;
  }
}

1881
int32_t getMeterIndex(SSQLToken* pTableToken, SQueryInfo* pQueryInfo, SColumnIndex* pIndex) {
S
slguan 已提交
1882
  if (pTableToken->n == 0) {  // only one table and no table name prefix in column name
1883
    if (pQueryInfo->numOfTables == 1) {
S
slguan 已提交
1884 1885 1886 1887 1888 1889 1890 1891 1892
      pIndex->tableIndex = 0;
    }

    return TSDB_CODE_SUCCESS;
  }

  pIndex->tableIndex = COLUMN_INDEX_INITIAL_VAL;
  char tableName[TSDB_METER_ID_LEN + 1] = {0};

1893 1894
  for (int32_t i = 0; i < pQueryInfo->numOfTables; ++i) {
    SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, i);
H
hjxilinx 已提交
1895
    extractTableName(pMeterMetaInfo->name, tableName);
S
slguan 已提交
1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909

    if (strncasecmp(tableName, pTableToken->z, pTableToken->n) == 0 && strlen(tableName) == pTableToken->n) {
      pIndex->tableIndex = i;
      break;
    }
  }

  if (pIndex->tableIndex < 0) {
    return TSDB_CODE_INVALID_SQL;
  }

  return TSDB_CODE_SUCCESS;
}

1910
int32_t getTableIndexByName(SSQLToken* pToken, SQueryInfo* pQueryInfo, SColumnIndex* pIndex) {
S
slguan 已提交
1911 1912 1913
  SSQLToken tableToken = {0};
  extractTableNameFromToken(pToken, &tableToken);

1914
  if (getMeterIndex(&tableToken, pQueryInfo, pIndex) != TSDB_CODE_SUCCESS) {
S
slguan 已提交
1915
    return TSDB_CODE_INVALID_SQL;
H
hzcheng 已提交
1916 1917
  }

S
slguan 已提交
1918 1919
  return TSDB_CODE_SUCCESS;
}
H
hzcheng 已提交
1920

1921
int32_t getColumnIndexByName(SSQLToken* pToken, SQueryInfo* pQueryInfo, SColumnIndex* pIndex) {
1922
  if (pQueryInfo->pMeterInfo == NULL || pQueryInfo->numOfTables == 0) {
S
slguan 已提交
1923
    return TSDB_CODE_INVALID_SQL;
H
hzcheng 已提交
1924 1925
  }

S
slguan 已提交
1926 1927
  SSQLToken tmpToken = *pToken;

1928
  if (getTableIndexByName(&tmpToken, pQueryInfo, pIndex) != TSDB_CODE_SUCCESS) {
S
slguan 已提交
1929
    return TSDB_CODE_INVALID_SQL;
H
hzcheng 已提交
1930 1931
  }

1932
  return doGetColumnIndexByName(&tmpToken, pQueryInfo, pIndex);
H
hzcheng 已提交
1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981
}

int32_t changeFunctionID(int32_t optr, int16_t* functionId) {
  switch (optr) {
    case TK_COUNT:
      *functionId = TSDB_FUNC_COUNT;
      break;
    case TK_SUM:
      *functionId = TSDB_FUNC_SUM;
      break;
    case TK_AVG:
      *functionId = TSDB_FUNC_AVG;
      break;
    case TK_MIN:
      *functionId = TSDB_FUNC_MIN;
      break;
    case TK_MAX:
      *functionId = TSDB_FUNC_MAX;
      break;
    case TK_STDDEV:
      *functionId = TSDB_FUNC_STDDEV;
      break;
    case TK_PERCENTILE:
      *functionId = TSDB_FUNC_PERCT;
      break;
    case TK_APERCENTILE:
      *functionId = TSDB_FUNC_APERCT;
      break;
    case TK_FIRST:
      *functionId = TSDB_FUNC_FIRST;
      break;
    case TK_LAST:
      *functionId = TSDB_FUNC_LAST;
      break;
    case TK_LEASTSQUARES:
      *functionId = TSDB_FUNC_LEASTSQR;
      break;
    case TK_TOP:
      *functionId = TSDB_FUNC_TOP;
      break;
    case TK_BOTTOM:
      *functionId = TSDB_FUNC_BOTTOM;
      break;
    case TK_DIFF:
      *functionId = TSDB_FUNC_DIFF;
      break;
    case TK_SPREAD:
      *functionId = TSDB_FUNC_SPREAD;
      break;
S
slguan 已提交
1982 1983
    case TK_TWA:
      *functionId = TSDB_FUNC_TWA;
H
hzcheng 已提交
1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998
      break;
    case TK_INTERP:
      *functionId = TSDB_FUNC_INTERP;
      break;
    case TK_LAST_ROW:
      *functionId = TSDB_FUNC_LAST_ROW;
      break;
    default:
      return -1;
  }

  return TSDB_CODE_SUCCESS;
}

int32_t setShowInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) {
S
slguan 已提交
1999
  SSqlCmd*        pCmd = &pSql->cmd;
2000 2001 2002
  SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, pCmd->clauseIndex, 0);
  assert(pCmd->numOfClause == 1);
  
H
hzcheng 已提交
2003 2004
  pCmd->command = TSDB_SQL_SHOW;

2005
  const char* msg1 = "invalid name";
2006
  const char* msg2 = "pattern filter string too long";
2007 2008 2009 2010
  const char* msg3 = "database name too long";
  const char* msg4 = "invalid ip address";
  const char* msg5 = "database name is empty";
  const char* msg6 = "pattern string is empty";
H
hzcheng 已提交
2011 2012 2013 2014 2015

  /*
   * database prefix in pInfo->pDCLInfo->a[0]
   * wildcard in like clause in pInfo->pDCLInfo->a[1]
   */
2016 2017 2018
  SShowInfo* pShowInfo = &pInfo->pDCLInfo->showOpt;
  int16_t    showType = pShowInfo->showType;
  if (showType == TSDB_MGMT_TABLE_TABLE || showType == TSDB_MGMT_TABLE_METRIC || showType == TSDB_MGMT_TABLE_VGROUP) {
H
hzcheng 已提交
2019
    // db prefix in tagCond, show table conds in payload
2020 2021 2022
    SSQLToken* pDbPrefixToken = &pShowInfo->prefix;
    if (pDbPrefixToken->type != 0) {
      assert(pDbPrefixToken->n >= 0);
H
hzcheng 已提交
2023 2024

      if (pDbPrefixToken->n > TSDB_DB_NAME_LEN) {  // db name is too long
2025
        return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3);
H
hzcheng 已提交
2026 2027
      }

2028
      if (pDbPrefixToken->n <= 0) {
2029
        return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg5);
H
hzcheng 已提交
2030 2031
      }

2032
      if (tscValidateName(pDbPrefixToken) != TSDB_CODE_SUCCESS) {
2033
        return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1);
H
hzcheng 已提交
2034 2035
      }

2036
      int32_t ret = setObjFullName(pMeterMetaInfo->name, getAccountId(pSql), pDbPrefixToken, NULL, NULL);
H
hzcheng 已提交
2037
      if (ret != TSDB_CODE_SUCCESS) {
2038
        return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1);
H
hzcheng 已提交
2039
      }
2040
    }
H
hzcheng 已提交
2041

2042 2043 2044 2045
    // show table/stable like 'xxxx', set the like pattern for show tables
    SSQLToken* pPattern = &pShowInfo->pattern;
    if (pPattern->type != 0) {
      pPattern->n = strdequote(pPattern->z);
S
slguan 已提交
2046

2047
      if (pPattern->n <= 0) {
2048
        return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg6);
2049
      }
H
hzcheng 已提交
2050

2051
      if (pCmd->payloadLen > TSDB_METER_NAME_LEN) {
2052
        return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2);
H
hzcheng 已提交
2053 2054
      }
    }
2055 2056
  } else if (showType == TSDB_MGMT_TABLE_VNODES) {
    if (pShowInfo->prefix.type == 0) {
2057
      return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), "No specified ip of dnode");
L
lihui 已提交
2058 2059
    }

L
lihui 已提交
2060
    // show vnodes may be ip addr of dnode in payload
2061 2062
    SSQLToken* pDnodeIp = &pShowInfo->prefix;
    if (pDnodeIp->n > TSDB_IPv4ADDR_LEN) {  // ip addr is too long
2063
      return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3);
2064
    }
L
lihui 已提交
2065

2066
    if (!validateIpAddress(pDnodeIp->z, pDnodeIp->n)) {
2067
      return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg4);
L
lihui 已提交
2068
    }
H
hzcheng 已提交
2069 2070 2071 2072 2073 2074
  }

  return TSDB_CODE_SUCCESS;
}

int32_t setKillInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) {
2075 2076
  const char* msg1 = "invalid ip address";
  const char* msg2 = "invalid port";
H
hzcheng 已提交
2077

2078 2079
  SSqlCmd* pCmd = &pSql->cmd;
  pCmd->command = pInfo->type;
H
hzcheng 已提交
2080

2081 2082
  SSQLToken* ip = &(pInfo->pDCLInfo->ip);
  if (ip->n > TSDB_KILL_MSG_LEN) {
H
hzcheng 已提交
2083 2084 2085
    return TSDB_CODE_INVALID_SQL;
  }

2086
  strncpy(pCmd->payload, ip->z, ip->n);
H
hzcheng 已提交
2087 2088 2089

  const char delim = ':';

2090 2091 2092 2093
  char* ipStr = strtok(ip->z, &delim);
  char* portStr = strtok(NULL, &delim);

  if (!validateIpAddress(ipStr, strlen(ipStr))) {
H
hzcheng 已提交
2094 2095
    memset(pCmd->payload, 0, tListLen(pCmd->payload));

2096
    return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1);
H
hzcheng 已提交
2097 2098
  }

L
lihui 已提交
2099
  uint16_t port = (uint16_t)strtol(portStr, NULL, 10);
H
hzcheng 已提交
2100 2101
  if (port <= 0 || port > 65535) {
    memset(pCmd->payload, 0, tListLen(pCmd->payload));
2102
    return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2);
H
hzcheng 已提交
2103 2104 2105 2106 2107
  }

  return TSDB_CODE_SUCCESS;
}

2108 2109 2110 2111 2112 2113 2114 2115 2116
bool validateIpAddress(const char* ip, size_t size) {
  char tmp[128] = {0};  // buffer to build null-terminated string
  assert(size < 128);

  strncpy(tmp, ip, size);

  in_addr_t ipAddr = inet_addr(tmp);

  return ipAddr != INADDR_NONE;
H
hzcheng 已提交
2117 2118
}

2119
int32_t tscTansformSQLFunctionForSTableQuery(SQueryInfo* pQueryInfo) {
2120
  SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0);
S
slguan 已提交
2121

2122
  if (pMeterMetaInfo->pMeterMeta == NULL || !UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo)) {
S
slguan 已提交
2123
    return TSDB_CODE_INVALID_SQL;
H
hzcheng 已提交
2124 2125
  }

S
slguan 已提交
2126
  assert(pMeterMetaInfo->pMeterMeta->numOfTags >= 0);
H
hzcheng 已提交
2127 2128 2129

  int16_t bytes = 0;
  int16_t type = 0;
S
slguan 已提交
2130
  int16_t intermediateBytes = 0;
H
hzcheng 已提交
2131

2132 2133 2134
  for (int32_t k = 0; k < pQueryInfo->fieldsInfo.numOfOutputCols; ++k) {
    SSqlExpr*   pExpr = tscSqlExprGet(pQueryInfo, k);
    TAOS_FIELD* pField = tscFieldInfoGetField(pQueryInfo, k);
H
hzcheng 已提交
2135

S
slguan 已提交
2136 2137 2138 2139 2140 2141 2142 2143
    int16_t functionId = aAggs[pExpr->functionId].stableFuncId;

    if ((functionId >= TSDB_FUNC_SUM && functionId <= TSDB_FUNC_TWA) ||
        (functionId >= TSDB_FUNC_FIRST_DST && functionId <= TSDB_FUNC_LAST_DST)) {
      if (getResultDataInfo(pField->type, pField->bytes, functionId, pExpr->param[0].i64Key, &type, &bytes,
                            &intermediateBytes, 0, true) != TSDB_CODE_SUCCESS) {
        return TSDB_CODE_INVALID_SQL;
      }
H
hzcheng 已提交
2144

2145
      tscSqlExprUpdate(pQueryInfo, k, functionId, pExpr->colInfo.colIdx, TSDB_DATA_TYPE_BINARY, bytes);
S
slguan 已提交
2146 2147
      // todo refactor
      pExpr->interResBytes = intermediateBytes;
H
hzcheng 已提交
2148 2149 2150
    }
  }

2151
  tscFieldInfoUpdateOffsetForInterResult(pQueryInfo);
S
slguan 已提交
2152
  return TSDB_CODE_SUCCESS;
H
hzcheng 已提交
2153 2154 2155
}

/* transfer the field-info back to original input format */
2156 2157 2158
void tscRestoreSQLFunctionForMetricQuery(SQueryInfo* pQueryInfo) {
  SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0);
  if (!UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo)) {
H
hzcheng 已提交
2159 2160 2161
    return;
  }

2162 2163 2164
  for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) {
    SSqlExpr*   pExpr = tscSqlExprGet(pQueryInfo, i);
    TAOS_FIELD* pField = tscFieldInfoGetField(pQueryInfo, i);
H
hzcheng 已提交
2165

S
slguan 已提交
2166 2167
    if ((pExpr->functionId >= TSDB_FUNC_FIRST_DST && pExpr->functionId <= TSDB_FUNC_LAST_DST) ||
        (pExpr->functionId >= TSDB_FUNC_SUM && pExpr->functionId <= TSDB_FUNC_MAX)) {
H
hzcheng 已提交
2168 2169 2170 2171 2172 2173
      pExpr->resBytes = pField->bytes;
      pExpr->resType = pField->type;
    }
  }
}

2174
bool hasUnsupportFunctionsForSTableQuery(SQueryInfo* pQueryInfo) {
S
slguan 已提交
2175
  const char* msg1 = "TWA not allowed to apply to super table directly";
H
hjxilinx 已提交
2176
  const char* msg2 = "TWA only support group by tbname for super table query";
2177 2178
  const char* msg3 = "function not support for super table query";
  
S
slguan 已提交
2179
  // filter sql function not supported by metric query yet.
2180 2181
  for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) {
    int32_t functionId = tscSqlExprGet(pQueryInfo, i)->functionId;
S
slguan 已提交
2182
    if ((aAggs[functionId].nStatus & TSDB_FUNCSTATE_METRIC) == 0) {
2183
      invalidSqlErrMsg(pQueryInfo->msg, msg3);
S
slguan 已提交
2184
      return true;
H
hzcheng 已提交
2185 2186 2187
    }
  }

2188 2189 2190
  if (tscIsTWAQuery(pQueryInfo)) {
    if (pQueryInfo->groupbyExpr.numOfGroupCols == 0) {
      invalidSqlErrMsg(pQueryInfo->msg, msg1);
S
slguan 已提交
2191 2192
      return true;
    }
H
hzcheng 已提交
2193

2194 2195 2196
    if (pQueryInfo->groupbyExpr.numOfGroupCols != 1 ||
        pQueryInfo->groupbyExpr.columnInfo[0].colIdx != TSDB_TBNAME_COLUMN_INDEX) {
      invalidSqlErrMsg(pQueryInfo->msg, msg2);
H
hzcheng 已提交
2197 2198 2199
      return true;
    }
  }
S
slguan 已提交
2200

H
hzcheng 已提交
2201 2202 2203
  return false;
}

2204
static bool functionCompatibleCheck(SQueryInfo* pQueryInfo) {
H
hzcheng 已提交
2205
  int32_t startIdx = 0;
2206
  int32_t functionID = tscSqlExprGet(pQueryInfo, startIdx)->functionId;
S
slguan 已提交
2207 2208

  // ts function can be simultaneously used with any other functions.
H
hzcheng 已提交
2209
  if (functionID == TSDB_FUNC_TS || functionID == TSDB_FUNC_TS_DUMMY) {
S
slguan 已提交
2210
    startIdx++;
H
hzcheng 已提交
2211 2212
  }

2213
  int32_t factor = funcCompatDefList[tscSqlExprGet(pQueryInfo, startIdx)->functionId];
H
hzcheng 已提交
2214 2215 2216

  // diff function cannot be executed with other function
  // arithmetic function can be executed with other arithmetic functions
2217 2218
  for (int32_t i = startIdx + 1; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) {
    SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i);
2219 2220 2221 2222 2223 2224 2225

    int16_t functionId = pExpr->functionId;
    if (functionId == TSDB_FUNC_TAGPRJ || functionId == TSDB_FUNC_TAG || functionId == TSDB_FUNC_TS) {
      continue;
    }

    if (functionId == TSDB_FUNC_PRJ && pExpr->colInfo.colId == PRIMARYKEY_TIMESTAMP_COL_INDEX) {
S
slguan 已提交
2226 2227 2228 2229
      continue;
    }

    if (funcCompatDefList[functionId] != factor) {
H
hzcheng 已提交
2230 2231 2232 2233 2234 2235 2236
      return false;
    }
  }

  return true;
}

2237 2238
void updateTagColumnIndex(SQueryInfo* pQueryInfo, int32_t tableIndex) {
  SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, tableIndex);
S
slguan 已提交
2239

H
hjxilinx 已提交
2240 2241 2242 2243
  /*
   * update tags column index for group by tags
   * group by columns belong to this table
   */
2244 2245 2246
  if (pQueryInfo->groupbyExpr.numOfGroupCols > 0 && pQueryInfo->groupbyExpr.tableIndex == tableIndex) {
    for (int32_t i = 0; i < pQueryInfo->groupbyExpr.numOfGroupCols; ++i) {
      int32_t index = pQueryInfo->groupbyExpr.columnInfo[i].colIdx;
H
hjxilinx 已提交
2247

H
hjxilinx 已提交
2248 2249 2250
      for (int32_t j = 0; j < pMeterMetaInfo->numOfTags; ++j) {
        int32_t tagColIndex = pMeterMetaInfo->tagColumnIndex[j];
        if (tagColIndex == index) {
2251
          pQueryInfo->groupbyExpr.columnInfo[i].colIdx = j;
H
hjxilinx 已提交
2252 2253
          break;
        }
H
hzcheng 已提交
2254 2255 2256 2257 2258
      }
    }
  }

  // update tags column index for expression
2259 2260
  for (int32_t i = 0; i < pQueryInfo->exprsInfo.numOfExprs; ++i) {
    SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i);
H
hjxilinx 已提交
2261

S
slguan 已提交
2262
    if (!TSDB_COL_IS_TAG(pExpr->colInfo.flag)) {  // not tags, continue
H
hzcheng 已提交
2263 2264
      continue;
    }
H
hjxilinx 已提交
2265

H
hjxilinx 已提交
2266 2267 2268 2269
    // not belongs to this table
    if (pExpr->uid != pMeterMetaInfo->pMeterMeta->uid) {
      continue;
    }
H
hzcheng 已提交
2270

S
slguan 已提交
2271 2272
    for (int32_t j = 0; j < pMeterMetaInfo->numOfTags; ++j) {
      if (pExpr->colInfo.colIdx == pMeterMetaInfo->tagColumnIndex[j]) {
H
hzcheng 已提交
2273 2274 2275 2276 2277
        pExpr->colInfo.colIdx = j;
        break;
      }
    }
  }
H
hjxilinx 已提交
2278

H
hjxilinx 已提交
2279
  // update join condition tag column index
2280
  SJoinInfo* pJoinInfo = &pQueryInfo->tagCond.joinInfo;
H
hjxilinx 已提交
2281 2282 2283
  if (!pJoinInfo->hasJoin) {  // not join query
    return;
  }
H
hjxilinx 已提交
2284

H
hjxilinx 已提交
2285
  assert(pJoinInfo->left.uid != pJoinInfo->right.uid);
H
hjxilinx 已提交
2286

H
hjxilinx 已提交
2287 2288
  // the join condition expression node belongs to this table(super table)
  if (pMeterMetaInfo->pMeterMeta->uid == pJoinInfo->left.uid) {
H
hjxilinx 已提交
2289
    for (int32_t i = 0; i < pMeterMetaInfo->numOfTags; ++i) {
H
hjxilinx 已提交
2290 2291 2292 2293 2294
      if (pJoinInfo->left.tagCol == pMeterMetaInfo->tagColumnIndex[i]) {
        pJoinInfo->left.tagCol = i;
      }
    }
  }
H
hjxilinx 已提交
2295

H
hjxilinx 已提交
2296
  if (pMeterMetaInfo->pMeterMeta->uid == pJoinInfo->right.uid) {
H
hjxilinx 已提交
2297
    for (int32_t i = 0; i < pMeterMetaInfo->numOfTags; ++i) {
H
hjxilinx 已提交
2298 2299 2300 2301 2302
      if (pJoinInfo->right.tagCol == pMeterMetaInfo->tagColumnIndex[i]) {
        pJoinInfo->right.tagCol = i;
      }
    }
  }
H
hzcheng 已提交
2303 2304
}

2305
int32_t parseGroupbyClause(SQueryInfo* pQueryInfo, tVariantList* pList, SSqlCmd* pCmd) {
2306 2307
  const char* msg1 = "too many columns in group by clause";
  const char* msg2 = "invalid column name in group by clause";
H
hjxilinx 已提交
2308
  const char* msg3 = "group by columns must belong to one table";
S
slguan 已提交
2309 2310 2311
  const char* msg7 = "not support group by expression";
  const char* msg8 = "not allowed column type for group by";
  const char* msg9 = "tags not allowed for table query";
H
hzcheng 已提交
2312

S
slguan 已提交
2313 2314
  // todo : handle two meter situation
  SMeterMetaInfo* pMeterMetaInfo = NULL;
H
hzcheng 已提交
2315 2316 2317 2318 2319

  if (pList == NULL) {
    return TSDB_CODE_SUCCESS;
  }

2320
  pQueryInfo->groupbyExpr.numOfGroupCols = pList->nExpr;
H
hzcheng 已提交
2321
  if (pList->nExpr > TSDB_MAX_TAGS) {
2322
    return invalidSqlErrMsg(pQueryInfo->msg, msg1);
H
hzcheng 已提交
2323 2324
  }

S
slguan 已提交
2325 2326
  SMeterMeta* pMeterMeta = NULL;
  SSchema*    pSchema = NULL;
H
hjxilinx 已提交
2327
  SSchema     s = tsGetTbnameColumnSchema();
H
hzcheng 已提交
2328

S
slguan 已提交
2329
  int32_t tableIndex = COLUMN_INDEX_INITIAL_VAL;
H
hzcheng 已提交
2330 2331 2332 2333 2334

  for (int32_t i = 0; i < pList->nExpr; ++i) {
    tVariant* pVar = &pList->a[i].pVar;
    SSQLToken token = {pVar->nLen, pVar->nType, pVar->pz};

S
slguan 已提交
2335
    SColumnIndex index = COLUMN_INDEX_INITIALIZER;
H
hzcheng 已提交
2336

2337
    if (getColumnIndexByName(&token, pQueryInfo, &index) != TSDB_CODE_SUCCESS) {
2338
      return invalidSqlErrMsg(pQueryInfo->msg, msg2);
S
slguan 已提交
2339
    }
H
hzcheng 已提交
2340

S
slguan 已提交
2341
    if (tableIndex != index.tableIndex && tableIndex >= 0) {
2342
      return invalidSqlErrMsg(pQueryInfo->msg, msg3);
H
hzcheng 已提交
2343 2344
    }

S
slguan 已提交
2345
    tableIndex = index.tableIndex;
H
hzcheng 已提交
2346

2347
    pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, index.tableIndex);
S
slguan 已提交
2348
    pMeterMeta = pMeterMetaInfo->pMeterMeta;
H
hzcheng 已提交
2349

S
slguan 已提交
2350 2351 2352 2353 2354
    if (index.columnIndex == TSDB_TBNAME_COLUMN_INDEX) {
      pSchema = &s;
    } else {
      pSchema = tsGetColumnSchema(pMeterMeta, index.columnIndex);
    }
H
hzcheng 已提交
2355

S
slguan 已提交
2356 2357 2358
    bool groupTag = false;
    if (index.columnIndex == TSDB_TBNAME_COLUMN_INDEX || index.columnIndex >= pMeterMeta->numOfColumns) {
      groupTag = true;
H
hzcheng 已提交
2359 2360
    }

S
slguan 已提交
2361
    if (groupTag) {
2362 2363
      if (!UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo)) {
        return invalidSqlErrMsg(pQueryInfo->msg, msg9);
S
slguan 已提交
2364 2365 2366 2367 2368 2369 2370
      }

      int32_t relIndex = index.columnIndex;
      if (index.columnIndex != TSDB_TBNAME_COLUMN_INDEX) {
        relIndex -= pMeterMeta->numOfColumns;
      }

2371
      pQueryInfo->groupbyExpr.columnInfo[i] =
S
slguan 已提交
2372
          (SColIndexEx){.colIdx = relIndex, .flag = TSDB_COL_TAG, .colId = pSchema->colId};  // relIndex;
2373
      addRequiredTagColumn(pQueryInfo, pQueryInfo->groupbyExpr.columnInfo[i].colIdx, index.tableIndex);
S
slguan 已提交
2374 2375
    } else {
      // check if the column type is valid, here only support the bool/tinyint/smallint/bigint group by
2376
      if (pSchema->type > TSDB_DATA_TYPE_BINARY) {
2377
        return invalidSqlErrMsg(pQueryInfo->msg, msg8);
S
slguan 已提交
2378 2379
      }

2380 2381
      tscColumnBaseInfoInsert(pQueryInfo, &index);
      pQueryInfo->groupbyExpr.columnInfo[i] =
S
slguan 已提交
2382
          (SColIndexEx){.colIdx = index.columnIndex, .flag = TSDB_COL_NORMAL, .colId = pSchema->colId};  // relIndex;
2383
      pQueryInfo->groupbyExpr.orderType = TSQL_SO_ASC;
S
slguan 已提交
2384 2385

      if (i == 0 && pList->nExpr > 1) {
2386
        return invalidSqlErrMsg(pQueryInfo->msg, msg7);
S
slguan 已提交
2387
      }
H
hzcheng 已提交
2388 2389 2390
    }
  }

2391
  pQueryInfo->groupbyExpr.tableIndex = tableIndex;
S
slguan 已提交
2392

H
hzcheng 已提交
2393 2394 2395
  return TSDB_CODE_SUCCESS;
}

2396 2397
void setColumnOffsetValueInResultset(SQueryInfo* pQueryInfo) {
  if (QUERY_IS_STABLE_QUERY(pQueryInfo->type)) {
2398
    tscFieldInfoUpdateOffsetForInterResult(pQueryInfo);
H
hzcheng 已提交
2399
  } else {
2400
    tscFieldInfoCalOffset(pQueryInfo);
H
hzcheng 已提交
2401 2402 2403
  }
}

S
slguan 已提交
2404 2405 2406 2407
static SColumnFilterInfo* addColumnFilterInfo(SColumnBase* pColumn) {
  if (pColumn == NULL) {
    return NULL;
  }
2408

S
slguan 已提交
2409
  int32_t size = pColumn->numOfFilters + 1;
L
lihui 已提交
2410
  char*   tmp = (char*)realloc((void*)(pColumn->filterInfo), sizeof(SColumnFilterInfo) * (size));
S
slguan 已提交
2411 2412
  if (tmp != NULL) {
    pColumn->filterInfo = (SColumnFilterInfo*)tmp;
2413 2414
  }

S
slguan 已提交
2415
  pColumn->numOfFilters++;
2416

S
slguan 已提交
2417 2418 2419 2420
  SColumnFilterInfo* pColFilterInfo = &pColumn->filterInfo[pColumn->numOfFilters - 1];
  memset(pColFilterInfo, 0, sizeof(SColumnFilterInfo));

  return pColFilterInfo;
2421 2422
}

2423 2424
static int32_t doExtractColumnFilterInfo(SQueryInfo* pQueryInfo, SColumnFilterInfo* pColumnFilter,
                                         SColumnIndex* columnIndex, tSQLExpr* pExpr) {
S
slguan 已提交
2425
  const char* msg = "not supported filter condition";
H
hzcheng 已提交
2426

S
slguan 已提交
2427
  tSQLExpr*       pRight = pExpr->pRight;
2428
  SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, columnIndex->tableIndex);
H
hzcheng 已提交
2429

S
slguan 已提交
2430
  SSchema* pSchema = tsGetColumnSchema(pMeterMetaInfo->pMeterMeta, columnIndex->columnIndex);
H
hzcheng 已提交
2431

S
slguan 已提交
2432
  int16_t colType = pSchema->type;
2433
  if (colType >= TSDB_DATA_TYPE_TINYINT && colType <= TSDB_DATA_TYPE_BIGINT) {
H
hzcheng 已提交
2434 2435 2436
    colType = TSDB_DATA_TYPE_BIGINT;
  } else if (colType == TSDB_DATA_TYPE_FLOAT || colType == TSDB_DATA_TYPE_DOUBLE) {
    colType = TSDB_DATA_TYPE_DOUBLE;
2437
  } else if ((colType == TSDB_DATA_TYPE_TIMESTAMP) && (TSDB_DATA_TYPE_BINARY == pRight->val.nType)) {
2438
    int retVal = setColumnFilterInfoForTimestamp(pQueryInfo, &pRight->val);
2439 2440 2441
    if (TSDB_CODE_SUCCESS != retVal) {
      return retVal;
    }
H
hzcheng 已提交
2442 2443 2444
  }

  if (pExpr->nSQLOptr == TK_LE || pExpr->nSQLOptr == TK_LT) {
S
slguan 已提交
2445 2446
    tVariantDump(&pRight->val, (char*)&pColumnFilter->upperBndd, colType);
  } else {  // TK_GT,TK_GE,TK_EQ,TK_NE are based on the pColumn->lowerBndd
H
hzcheng 已提交
2447
    if (colType == TSDB_DATA_TYPE_BINARY) {
S
slguan 已提交
2448 2449
      pColumnFilter->pz = (int64_t)calloc(1, pRight->val.nLen + 1);
      pColumnFilter->len = pRight->val.nLen;
H
hzcheng 已提交
2450

S
slguan 已提交
2451 2452 2453 2454 2455 2456 2457 2458 2459
      tVariantDump(&pRight->val, (char*)pColumnFilter->pz, colType);
    } else if (colType == TSDB_DATA_TYPE_NCHAR) {
      // pRight->val.nLen + 1 is larger than the actual nchar string length
      pColumnFilter->pz = (int64_t)calloc(1, (pRight->val.nLen + 1) * TSDB_NCHAR_SIZE);

      tVariantDump(&pRight->val, (char*)pColumnFilter->pz, colType);

      size_t len = wcslen((wchar_t*)pColumnFilter->pz);
      pColumnFilter->len = len * TSDB_NCHAR_SIZE;
H
hzcheng 已提交
2460
    } else {
S
slguan 已提交
2461
      tVariantDump(&pRight->val, (char*)&pColumnFilter->lowerBndd, colType);
H
hzcheng 已提交
2462 2463 2464 2465 2466
    }
  }

  switch (pExpr->nSQLOptr) {
    case TK_LE:
S
slguan 已提交
2467
      pColumnFilter->upperRelOptr = TSDB_RELATION_LESS_EQUAL;
H
hzcheng 已提交
2468 2469
      break;
    case TK_LT:
S
slguan 已提交
2470
      pColumnFilter->upperRelOptr = TSDB_RELATION_LESS;
H
hzcheng 已提交
2471 2472
      break;
    case TK_GT:
S
slguan 已提交
2473
      pColumnFilter->lowerRelOptr = TSDB_RELATION_LARGE;
H
hzcheng 已提交
2474 2475
      break;
    case TK_GE:
S
slguan 已提交
2476
      pColumnFilter->lowerRelOptr = TSDB_RELATION_LARGE_EQUAL;
H
hzcheng 已提交
2477 2478
      break;
    case TK_EQ:
S
slguan 已提交
2479
      pColumnFilter->lowerRelOptr = TSDB_RELATION_EQUAL;
H
hzcheng 已提交
2480 2481
      break;
    case TK_NE:
S
slguan 已提交
2482
      pColumnFilter->lowerRelOptr = TSDB_RELATION_NOT_EQUAL;
H
hzcheng 已提交
2483 2484
      break;
    case TK_LIKE:
S
slguan 已提交
2485
      pColumnFilter->lowerRelOptr = TSDB_RELATION_LIKE;
H
hzcheng 已提交
2486
      break;
S
slguan 已提交
2487
    default:
2488
      return invalidSqlErrMsg(pQueryInfo->msg, msg);
H
hzcheng 已提交
2489
  }
S
slguan 已提交
2490

2491
  return TSDB_CODE_SUCCESS;
H
hzcheng 已提交
2492 2493
}

S
slguan 已提交
2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507
typedef struct SCondExpr {
  tSQLExpr* pTagCond;
  tSQLExpr* pTimewindow;

  tSQLExpr* pColumnCond;

  tSQLExpr* pTableCond;
  int16_t   relType;  // relation between table name in expression and other tag
                      // filter condition expression, TK_AND or TK_OR
  int16_t tableCondIndex;

  tSQLExpr* pJoinExpr;  // join condition
  bool      tsJoin;
} SCondExpr;
H
hzcheng 已提交
2508

S
slguan 已提交
2509 2510 2511
static int32_t getTimeRange(int64_t* stime, int64_t* etime, tSQLExpr* pRight, int32_t optr, int16_t timePrecision);

static int32_t tSQLExprNodeToString(tSQLExpr* pExpr, char** str) {
H
hzcheng 已提交
2512
  if (pExpr->nSQLOptr == TK_ID) {  // column name
S
slguan 已提交
2513 2514
    strncpy(*str, pExpr->colInfo.z, pExpr->colInfo.n);
    *str += pExpr->colInfo.n;
H
hzcheng 已提交
2515 2516

  } else if (pExpr->nSQLOptr >= TK_BOOL && pExpr->nSQLOptr <= TK_STRING) {  // value
S
slguan 已提交
2517 2518
    *str += tVariantToString(&pExpr->val, *str);

H
hzcheng 已提交
2519
  } else {
S
slguan 已提交
2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538
    assert(false);
  }

  return TSDB_CODE_SUCCESS;
}

static bool isExprLeafNode(tSQLExpr* pExpr) {
  return (pExpr->pRight == NULL && pExpr->pLeft == NULL) &&
         (pExpr->nSQLOptr == TK_ID || (pExpr->nSQLOptr >= TK_BOOL && pExpr->nSQLOptr <= TK_NCHAR) ||
          pExpr->nSQLOptr == TK_SET);
}

static bool isExprDirectParentOfLeaftNode(tSQLExpr* pExpr) {
  return (pExpr->pLeft != NULL && pExpr->pRight != NULL) &&
         (isExprLeafNode(pExpr->pLeft) && isExprLeafNode(pExpr->pRight));
}

static int32_t tSQLExprLeafToString(tSQLExpr* pExpr, bool addParentheses, char** output) {
  if (!isExprDirectParentOfLeaftNode(pExpr)) {
H
hzcheng 已提交
2539 2540 2541
    return TSDB_CODE_INVALID_SQL;
  }

S
slguan 已提交
2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561
  tSQLExpr* pLeft = pExpr->pLeft;
  tSQLExpr* pRight = pExpr->pRight;

  if (addParentheses) {
    *(*output) = '(';
    *output += 1;
  }

  tSQLExprNodeToString(pLeft, output);
  if (optrToString(pExpr, output) != TSDB_CODE_SUCCESS) {
    return TSDB_CODE_INVALID_SQL;
  }

  tSQLExprNodeToString(pRight, output);

  if (addParentheses) {
    *(*output) = ')';
    *output += 1;
  }

H
hzcheng 已提交
2562 2563 2564 2565
  return TSDB_CODE_SUCCESS;
}

static int32_t optrToString(tSQLExpr* pExpr, char** exprString) {
S
slguan 已提交
2566 2567 2568 2569
  const char* le = "<=";
  const char* ge = ">=";
  const char* ne = "<>";
  const char* likeOptr = "LIKE";
H
hzcheng 已提交
2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625

  switch (pExpr->nSQLOptr) {
    case TK_LE: {
      *(int16_t*)(*exprString) = *(int16_t*)le;
      *exprString += 1;
      break;
    }
    case TK_GE: {
      *(int16_t*)(*exprString) = *(int16_t*)ge;
      *exprString += 1;
      break;
    }
    case TK_NE: {
      *(int16_t*)(*exprString) = *(int16_t*)ne;
      *exprString += 1;
      break;
    }

    case TK_LT:
      *(*exprString) = '<';
      break;
    case TK_GT:
      *(*exprString) = '>';
      break;
    case TK_EQ:
      *(*exprString) = '=';
      break;
    case TK_PLUS:
      *(*exprString) = '+';
      break;
    case TK_MINUS:
      *(*exprString) = '-';
      break;
    case TK_STAR:
      *(*exprString) = '*';
      break;
    case TK_DIVIDE:
      *(*exprString) = '/';
      break;
    case TK_REM:
      *(*exprString) = '%';
      break;
    case TK_LIKE: {
      int32_t len = sprintf(*exprString, " %s ", likeOptr);
      *exprString += (len - 1);
      break;
    }
    default:
      return TSDB_CODE_INVALID_SQL;
  }

  *exprString += 1;

  return TSDB_CODE_SUCCESS;
}

H
hjxilinx 已提交
2626
static int32_t tablenameListToString(tSQLExpr* pExpr, /*char* str*/ SStringBuilder* sb) {
H
hzcheng 已提交
2627 2628 2629 2630 2631
  tSQLExprList* pList = pExpr->pParam;
  if (pList->nExpr <= 0) {
    return TSDB_CODE_INVALID_SQL;
  }

S
slguan 已提交
2632
  if (pList->nExpr > 0) {
H
hjxilinx 已提交
2633
    taosStringBuilderAppendStringLen(sb, QUERY_COND_REL_PREFIX_IN, QUERY_COND_REL_PREFIX_IN_LEN);
S
slguan 已提交
2634 2635
  }

H
hzcheng 已提交
2636 2637
  for (int32_t i = 0; i < pList->nExpr; ++i) {
    tSQLExpr* pSub = pList->a[i].pNode;
H
hjxilinx 已提交
2638
    taosStringBuilderAppendStringLen(sb, pSub->val.pz, pSub->val.nLen);
S
slguan 已提交
2639 2640

    if (i < pList->nExpr - 1) {
H
hjxilinx 已提交
2641
      taosStringBuilderAppendString(sb, TBNAME_LIST_SEP);
S
slguan 已提交
2642
    }
H
hzcheng 已提交
2643 2644 2645 2646 2647 2648 2649 2650 2651

    if (pSub->val.nLen <= 0 || pSub->val.nLen > TSDB_METER_NAME_LEN) {
      return TSDB_CODE_INVALID_SQL;
    }
  }

  return TSDB_CODE_SUCCESS;
}

H
hjxilinx 已提交
2652
static int32_t tablenameCondToString(tSQLExpr* pExpr, /*char* str*/ SStringBuilder* sb) {
H
hjxilinx 已提交
2653 2654
  taosStringBuilderAppendStringLen(sb, QUERY_COND_REL_PREFIX_LIKE, QUERY_COND_REL_PREFIX_LIKE_LEN);
  taosStringBuilderAppendString(sb, pExpr->val.pz);
S
slguan 已提交
2655 2656

  return TSDB_CODE_SUCCESS;
H
hzcheng 已提交
2657 2658
}

S
slguan 已提交
2659 2660 2661 2662 2663 2664
enum {
  TSQL_EXPR_TS = 0,
  TSQL_EXPR_TAG = 1,
  TSQL_EXPR_COLUMN = 2,
  TSQL_EXPR_TBNAME = 3,
};
H
hzcheng 已提交
2665

2666 2667
static int32_t extractColumnFilterInfo(SQueryInfo* pQueryInfo, SColumnIndex* pIndex, tSQLExpr* pExpr, int32_t sqlOptr) {
  SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, pIndex->tableIndex);
S
slguan 已提交
2668 2669 2670

  SMeterMeta* pMeterMeta = pMeterMetaInfo->pMeterMeta;
  SSchema*    pSchema = tsGetColumnSchema(pMeterMeta, pIndex->columnIndex);
H
hzcheng 已提交
2671

S
slguan 已提交
2672 2673 2674
  const char* msg1 = "non binary column not support like operator";
  const char* msg2 = "binary column not support this operator";

2675
  SColumnBase*       pColumn = tscColumnBaseInfoInsert(pQueryInfo, pIndex);
S
slguan 已提交
2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700
  SColumnFilterInfo* pColFilter = NULL;

  /*
   * in case of TK_AND filter condition, we first find the corresponding column and build the query condition together
   * the already existed condition.
   */
  if (sqlOptr == TK_AND) {
    // this is a new filter condition on this column
    if (pColumn->numOfFilters == 0) {
      pColFilter = addColumnFilterInfo(pColumn);
    } else {  // update the existed column filter information, find the filter info here
      pColFilter = &pColumn->filterInfo[0];
    }
  } else if (sqlOptr == TK_OR) {
    // TODO fixme: failed to invalid the filter expression: "col1 = 1 OR col2 = 2"
    pColFilter = addColumnFilterInfo(pColumn);
  } else {  // error;
    return TSDB_CODE_INVALID_SQL;
  }

  pColFilter->filterOnBinary =
      ((pSchema->type == TSDB_DATA_TYPE_BINARY || pSchema->type == TSDB_DATA_TYPE_NCHAR) ? 1 : 0);

  if (pColFilter->filterOnBinary) {
    if (pExpr->nSQLOptr != TK_EQ && pExpr->nSQLOptr != TK_NE && pExpr->nSQLOptr != TK_LIKE) {
2701
      return invalidSqlErrMsg(pQueryInfo->msg, msg2);
H
hzcheng 已提交
2702
    }
S
slguan 已提交
2703 2704
  } else {
    if (pExpr->nSQLOptr == TK_LIKE) {
2705
      return invalidSqlErrMsg(pQueryInfo->msg, msg1);
H
hzcheng 已提交
2706
    }
S
slguan 已提交
2707 2708 2709
  }

  pColumn->colIndex = *pIndex;
2710
  return doExtractColumnFilterInfo(pQueryInfo, pColFilter, pIndex, pExpr);
S
slguan 已提交
2711 2712
}

2713
static void relToString(tSQLExpr* pExpr, char** str) {
S
slguan 已提交
2714 2715 2716 2717 2718
  assert(pExpr->nSQLOptr == TK_AND || pExpr->nSQLOptr == TK_OR);

  const char* or = "OR";
  const char*and = "AND";

2719
  //    if (pQueryInfo->tagCond.relType == TSQL_STABLE_QTYPE_COND) {
S
slguan 已提交
2720 2721 2722 2723 2724 2725 2726 2727 2728
  if (pExpr->nSQLOptr == TK_AND) {
    strcpy(*str, and);
    *str += strlen(and);
  } else {
    strcpy(*str, or);
    *str += strlen(or);
  }
}

2729
static int32_t getTagCondString(tSQLExpr* pExpr, char** str) {
S
slguan 已提交
2730 2731 2732 2733 2734 2735 2736
  if (pExpr == NULL) {
    return TSDB_CODE_SUCCESS;
  }

  if (!isExprDirectParentOfLeaftNode(pExpr)) {
    *(*str) = '(';
    *str += 1;
H
hzcheng 已提交
2737

2738
    int32_t ret = getTagCondString(pExpr->pLeft, str);
H
hzcheng 已提交
2739
    if (ret != TSDB_CODE_SUCCESS) {
S
slguan 已提交
2740
      return ret;
H
hzcheng 已提交
2741
    }
S
slguan 已提交
2742

2743
    relToString(pExpr, str);
S
slguan 已提交
2744

2745
    ret = getTagCondString(pExpr->pRight, str);
S
slguan 已提交
2746 2747 2748 2749

    *(*str) = ')';
    *str += 1;

H
hzcheng 已提交
2750 2751 2752
    return ret;
  }

S
slguan 已提交
2753 2754 2755
  return tSQLExprLeafToString(pExpr, true, str);
}

2756
static int32_t getTablenameCond(SQueryInfo* pQueryInfo, tSQLExpr* pTableCond, SStringBuilder* sb) {
S
slguan 已提交
2757 2758 2759 2760
  const char* msg0 = "invalid table name list";

  if (pTableCond == NULL) {
    return TSDB_CODE_SUCCESS;
H
hzcheng 已提交
2761 2762
  }

S
slguan 已提交
2763 2764
  tSQLExpr* pLeft = pTableCond->pLeft;
  tSQLExpr* pRight = pTableCond->pRight;
H
hzcheng 已提交
2765

S
slguan 已提交
2766
  if (!isTablenameToken(&pLeft->colInfo)) {
H
hzcheng 已提交
2767 2768 2769
    return TSDB_CODE_INVALID_SQL;
  }

S
slguan 已提交
2770
  int32_t ret = TSDB_CODE_SUCCESS;
H
hzcheng 已提交
2771

S
slguan 已提交
2772
  if (pTableCond->nSQLOptr == TK_IN) {
H
hjxilinx 已提交
2773
    ret = tablenameListToString(pRight, sb);
S
slguan 已提交
2774
  } else if (pTableCond->nSQLOptr == TK_LIKE) {
H
hjxilinx 已提交
2775
    ret = tablenameCondToString(pRight, sb);
S
slguan 已提交
2776
  }
H
hzcheng 已提交
2777

S
slguan 已提交
2778
  if (ret != TSDB_CODE_SUCCESS) {
2779
    invalidSqlErrMsg(pQueryInfo->msg, msg0);
S
slguan 已提交
2780
  }
H
hzcheng 已提交
2781

S
slguan 已提交
2782 2783 2784
  return ret;
}

2785
static int32_t getColumnQueryCondInfo(SQueryInfo* pQueryInfo, tSQLExpr* pExpr, int32_t relOptr) {
S
slguan 已提交
2786 2787 2788 2789 2790
  if (pExpr == NULL) {
    return TSDB_CODE_SUCCESS;
  }

  if (!isExprDirectParentOfLeaftNode(pExpr)) {  // internal node
2791
    int32_t ret = getColumnQueryCondInfo(pQueryInfo, pExpr->pLeft, pExpr->nSQLOptr);
S
slguan 已提交
2792 2793 2794 2795
    if (ret != TSDB_CODE_SUCCESS) {
      return ret;
    }

2796
    return getColumnQueryCondInfo(pQueryInfo, pExpr->pRight, pExpr->nSQLOptr);
S
slguan 已提交
2797 2798
  } else {  // handle leaf node
    SColumnIndex index = COLUMN_INDEX_INITIALIZER;
2799
    if (getColumnIndexByName(&pExpr->pLeft->colInfo, pQueryInfo, &index) != TSDB_CODE_SUCCESS) {
H
hzcheng 已提交
2800 2801
      return TSDB_CODE_INVALID_SQL;
    }
S
slguan 已提交
2802

2803
    return extractColumnFilterInfo(pQueryInfo, &index, pExpr, relOptr);
H
hzcheng 已提交
2804
  }
S
slguan 已提交
2805
}
H
hzcheng 已提交
2806

2807
static int32_t getJoinCondInfo(SQueryInfo* pQueryInfo, tSQLExpr* pExpr) {
S
slguan 已提交
2808
  const char* msg = "invalid join query condition";
H
hzcheng 已提交
2809

S
slguan 已提交
2810 2811 2812
  if (pExpr == NULL) {
    return TSDB_CODE_SUCCESS;
  }
H
hzcheng 已提交
2813

S
slguan 已提交
2814
  if (!isExprDirectParentOfLeaftNode(pExpr)) {
2815
    return invalidSqlErrMsg(pQueryInfo->msg, msg);
S
slguan 已提交
2816 2817
  }

2818
  STagCond*  pTagCond = &pQueryInfo->tagCond;
S
slguan 已提交
2819 2820 2821 2822
  SJoinNode* pLeft = &pTagCond->joinInfo.left;
  SJoinNode* pRight = &pTagCond->joinInfo.right;

  SColumnIndex index = COLUMN_INDEX_INITIALIZER;
2823
  if (getColumnIndexByName(&pExpr->pLeft->colInfo, pQueryInfo, &index) != TSDB_CODE_SUCCESS) {
S
slguan 已提交
2824 2825 2826
    return TSDB_CODE_INVALID_SQL;
  }

2827
  SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, index.tableIndex);
S
slguan 已提交
2828 2829 2830 2831 2832 2833 2834
  int16_t         tagColIndex = index.columnIndex - pMeterMetaInfo->pMeterMeta->numOfColumns;

  pLeft->uid = pMeterMetaInfo->pMeterMeta->uid;
  pLeft->tagCol = tagColIndex;
  strcpy(pLeft->meterId, pMeterMetaInfo->name);

  index = (SColumnIndex)COLUMN_INDEX_INITIALIZER;
2835
  if (getColumnIndexByName(&pExpr->pRight->colInfo, pQueryInfo, &index) != TSDB_CODE_SUCCESS) {
S
slguan 已提交
2836 2837 2838
    return TSDB_CODE_INVALID_SQL;
  }

2839
  pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, index.tableIndex);
S
slguan 已提交
2840 2841 2842 2843 2844 2845 2846
  tagColIndex = index.columnIndex - pMeterMetaInfo->pMeterMeta->numOfColumns;

  pRight->uid = pMeterMetaInfo->pMeterMeta->uid;
  pRight->tagCol = tagColIndex;
  strcpy(pRight->meterId, pMeterMetaInfo->name);

  pTagCond->joinInfo.hasJoin = true;
H
hzcheng 已提交
2847 2848 2849 2850
  return TSDB_CODE_SUCCESS;
}

// todo error handle / such as and /or mixed with +/-/*/
S
slguan 已提交
2851
int32_t buildArithmeticExprString(tSQLExpr* pExpr, char** exprString) {
H
hzcheng 已提交
2852 2853 2854 2855 2856 2857 2858
  tSQLExpr* pLeft = pExpr->pLeft;
  tSQLExpr* pRight = pExpr->pRight;

  *(*exprString) = '(';
  *exprString += 1;

  if (pLeft->nSQLOptr >= TK_PLUS && pLeft->nSQLOptr <= TK_REM) {
S
slguan 已提交
2859
    buildArithmeticExprString(pLeft, exprString);
H
hzcheng 已提交
2860
  } else {
S
slguan 已提交
2861
    int32_t ret = tSQLExprNodeToString(pLeft, exprString);
H
hzcheng 已提交
2862 2863 2864 2865 2866 2867 2868 2869
    if (ret != TSDB_CODE_SUCCESS) {
      return TSDB_CODE_INVALID_SQL;
    }
  }

  optrToString(pExpr, exprString);

  if (pRight->nSQLOptr >= TK_PLUS && pRight->nSQLOptr <= TK_REM) {
S
slguan 已提交
2870
    buildArithmeticExprString(pRight, exprString);
H
hzcheng 已提交
2871
  } else {
S
slguan 已提交
2872
    int32_t ret = tSQLExprNodeToString(pRight, exprString);
H
hzcheng 已提交
2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883
    if (ret != TSDB_CODE_SUCCESS) {
      return TSDB_CODE_INVALID_SQL;
    }
  }

  *(*exprString) = ')';
  *exprString += 1;

  return TSDB_CODE_SUCCESS;
}

2884
static int32_t validateSQLExpr(tSQLExpr* pExpr, SQueryInfo* pQueryInfo, SColumnList* pList) {
H
hzcheng 已提交
2885
  if (pExpr->nSQLOptr == TK_ID) {
2886 2887 2888
      SColumnIndex index = COLUMN_INDEX_INITIALIZER;
      if (getColumnIndexByName(&pExpr->colInfo, pQueryInfo, &index) != TSDB_CODE_SUCCESS) {
        return TSDB_CODE_INVALID_SQL;
H
hzcheng 已提交
2889
      }
L
lihui 已提交
2890

H
hjxilinx 已提交
2891 2892
      // if column is timestamp, bool, binary, nchar, not support arithmetic, so return invalid sql
      SMeterMeta* pMeterMeta = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, index.tableIndex)->pMeterMeta;
L
lihui 已提交
2893 2894 2895 2896 2897
      SSchema* pSchema = tsGetSchema(pMeterMeta) + index.columnIndex;
      if ((pSchema->type == TSDB_DATA_TYPE_TIMESTAMP) || (pSchema->type == TSDB_DATA_TYPE_BOOL)
        || (pSchema->type == TSDB_DATA_TYPE_BINARY) || (pSchema->type == TSDB_DATA_TYPE_NCHAR)){
        return TSDB_CODE_INVALID_SQL;
      }
2898
      
2899
      pList->ids[pList->num++] = index;
H
hzcheng 已提交
2900 2901
  } else if (pExpr->nSQLOptr == TK_FLOAT && (isnan(pExpr->val.dKey) || isinf(pExpr->val.dKey))) {
    return TSDB_CODE_INVALID_SQL;
S
slguan 已提交
2902 2903
  } else if (pExpr->nSQLOptr >= TK_MIN && pExpr->nSQLOptr <= TK_LAST_ROW) {
    return TSDB_CODE_INVALID_SQL;
H
hzcheng 已提交
2904 2905 2906 2907 2908
  }

  return TSDB_CODE_SUCCESS;
}

2909
static int32_t validateArithmeticSQLExpr(tSQLExpr* pExpr, SQueryInfo* pQueryInfo, SColumnList* pList) {
H
hzcheng 已提交
2910 2911 2912 2913 2914 2915
  if (pExpr == NULL) {
    return TSDB_CODE_SUCCESS;
  }

  tSQLExpr* pLeft = pExpr->pLeft;
  if (pLeft->nSQLOptr >= TK_PLUS && pLeft->nSQLOptr <= TK_REM) {
2916
    int32_t ret = validateArithmeticSQLExpr(pLeft, pQueryInfo, pList);
H
hzcheng 已提交
2917 2918 2919 2920
    if (ret != TSDB_CODE_SUCCESS) {
      return ret;
    }
  } else {
2921
    int32_t ret = validateSQLExpr(pLeft, pQueryInfo, pList);
H
hzcheng 已提交
2922 2923 2924 2925 2926 2927 2928
    if (ret != TSDB_CODE_SUCCESS) {
      return ret;
    }
  }

  tSQLExpr* pRight = pExpr->pRight;
  if (pRight->nSQLOptr >= TK_PLUS && pRight->nSQLOptr <= TK_REM) {
2929
    int32_t ret = validateArithmeticSQLExpr(pRight, pQueryInfo, pList);
H
hzcheng 已提交
2930 2931 2932 2933
    if (ret != TSDB_CODE_SUCCESS) {
      return ret;
    }
  } else {
2934
    int32_t ret = validateSQLExpr(pRight, pQueryInfo, pList);
H
hzcheng 已提交
2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949
    if (ret != TSDB_CODE_SUCCESS) {
      return ret;
    }
  }

  return TSDB_CODE_SUCCESS;
}

static bool isValidExpr(tSQLExpr* pLeft, tSQLExpr* pRight, int32_t optr) {
  if (pLeft == NULL || (pRight == NULL && optr != TK_IN)) {
    return false;
  }

  /*
   * filter illegal expression in where clause:
S
slguan 已提交
2950 2951 2952
   * 1. count(*) > 12
   * 2. sum(columnA) > sum(columnB)
   * 3. 4 < 5,  'ABC'>'abc'
H
hzcheng 已提交
2953 2954 2955
   *
   * However, columnA < 4+12 is valid
   */
S
slguan 已提交
2956 2957
  if ((pLeft->nSQLOptr >= TK_COUNT && pLeft->nSQLOptr <= TK_LAST_ROW) ||
      (pRight->nSQLOptr >= TK_COUNT && pRight->nSQLOptr <= TK_LAST_ROW) ||
H
hzcheng 已提交
2958 2959 2960 2961 2962 2963 2964 2965
      (pLeft->nSQLOptr >= TK_BOOL && pLeft->nSQLOptr <= TK_BINARY && pRight->nSQLOptr >= TK_BOOL &&
       pRight->nSQLOptr <= TK_BINARY)) {
    return false;
  }

  return true;
}

S
slguan 已提交
2966 2967 2968
static void exchangeExpr(tSQLExpr* pExpr) {
  tSQLExpr* pLeft = pExpr->pLeft;
  tSQLExpr* pRight = pExpr->pRight;
H
hzcheng 已提交
2969

S
slguan 已提交
2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994
  if (pRight->nSQLOptr == TK_ID && (pLeft->nSQLOptr == TK_INTEGER || pLeft->nSQLOptr == TK_FLOAT ||
                                    pLeft->nSQLOptr == TK_STRING || pLeft->nSQLOptr == TK_BOOL)) {
    /*
     * exchange value of the left handside and the value of the right-handside
     * to make sure that the value of filter expression always locates in
     * right-handside and
     * the column-id is at the left handside.
     */
    uint32_t optr = 0;
    switch (pExpr->nSQLOptr) {
      case TK_LE:
        optr = TK_GE;
        break;
      case TK_LT:
        optr = TK_GT;
        break;
      case TK_GT:
        optr = TK_LT;
        break;
      case TK_GE:
        optr = TK_LE;
        break;
      default:
        optr = pExpr->nSQLOptr;
    }
H
hzcheng 已提交
2995

S
slguan 已提交
2996 2997 2998 2999 3000
    pExpr->nSQLOptr = optr;
    SWAP(pExpr->pLeft, pExpr->pRight, void*);
  }
}

3001
static bool validateJoinExprNode(SQueryInfo* pQueryInfo, tSQLExpr* pExpr, SColumnIndex* pLeftIndex) {
S
slguan 已提交
3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015
  const char* msg1 = "illegal column name";
  const char* msg2 = "= is expected in join expression";
  const char* msg3 = "join column must have same type";
  const char* msg4 = "self join is not allowed";
  const char* msg5 = "join table must be the same type(table to table, super table to super table)";
  const char* msg6 = "tags in join condition not support binary/nchar types";

  tSQLExpr* pRight = pExpr->pRight;

  if (pRight->nSQLOptr != TK_ID) {
    return true;
  }

  if (pExpr->nSQLOptr != TK_EQ) {
3016
    invalidSqlErrMsg(pQueryInfo->msg, msg2);
S
slguan 已提交
3017 3018 3019 3020 3021
    return false;
  }

  SColumnIndex rightIndex = COLUMN_INDEX_INITIALIZER;

3022
  if (getColumnIndexByName(&pRight->colInfo, pQueryInfo, &rightIndex) != TSDB_CODE_SUCCESS) {
3023
    invalidSqlErrMsg(pQueryInfo->msg, msg1);
S
slguan 已提交
3024
    return false;
H
hzcheng 已提交
3025 3026
  }

S
slguan 已提交
3027
  // todo extract function
3028
  SMeterMetaInfo* pLeftMeterMeta = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, pLeftIndex->tableIndex);
S
slguan 已提交
3029 3030 3031
  SSchema*        pLeftSchema = tsGetSchema(pLeftMeterMeta->pMeterMeta);
  int16_t         leftType = pLeftSchema[pLeftIndex->columnIndex].type;

3032
  SMeterMetaInfo* pRightMeterMeta = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, rightIndex.tableIndex);
S
slguan 已提交
3033 3034 3035 3036
  SSchema*        pRightSchema = tsGetSchema(pRightMeterMeta->pMeterMeta);
  int16_t         rightType = pRightSchema[rightIndex.columnIndex].type;

  if (leftType != rightType) {
3037
    invalidSqlErrMsg(pQueryInfo->msg, msg3);
S
slguan 已提交
3038 3039
    return false;
  } else if (pLeftIndex->tableIndex == rightIndex.tableIndex) {
3040
    invalidSqlErrMsg(pQueryInfo->msg, msg4);
S
slguan 已提交
3041 3042
    return false;
  } else if (leftType == TSDB_DATA_TYPE_BINARY || leftType == TSDB_DATA_TYPE_NCHAR) {
3043
    invalidSqlErrMsg(pQueryInfo->msg, msg6);
S
slguan 已提交
3044
    return false;
H
hzcheng 已提交
3045 3046
  }

S
slguan 已提交
3047
  // table to table/ super table to super table are allowed
3048 3049
  if (UTIL_METER_IS_SUPERTABLE(pLeftMeterMeta) != UTIL_METER_IS_SUPERTABLE(pRightMeterMeta)) {
    invalidSqlErrMsg(pQueryInfo->msg, msg5);
S
slguan 已提交
3050 3051
    return false;
  }
H
hzcheng 已提交
3052

S
slguan 已提交
3053 3054
  return true;
}
H
hzcheng 已提交
3055

S
slguan 已提交
3056 3057 3058 3059 3060 3061
static bool validTableNameOptr(tSQLExpr* pExpr) {
  const char nameFilterOptr[] = {TK_IN, TK_LIKE};

  for (int32_t i = 0; i < tListLen(nameFilterOptr); ++i) {
    if (pExpr->nSQLOptr == nameFilterOptr[i]) {
      return true;
H
hzcheng 已提交
3062 3063 3064
    }
  }

S
slguan 已提交
3065
  return false;
H
hzcheng 已提交
3066 3067
}

3068
static int32_t setExprToCond(tSQLExpr** parent, tSQLExpr* pExpr, const char* msg, int32_t parentOptr, char* msgBuf) {
S
slguan 已提交
3069 3070
  if (*parent != NULL) {
    if (parentOptr == TK_OR && msg != NULL) {
3071
      return invalidSqlErrMsg(msgBuf, msg);
S
slguan 已提交
3072
    }
H
hzcheng 已提交
3073

S
slguan 已提交
3074 3075 3076 3077
    *parent = tSQLExprCreate((*parent), pExpr, parentOptr);
  } else {
    *parent = pExpr;
  }
H
hzcheng 已提交
3078

S
slguan 已提交
3079 3080
  return TSDB_CODE_SUCCESS;
}
H
hzcheng 已提交
3081

3082
static int32_t handleExprInQueryCond(SQueryInfo* pQueryInfo, tSQLExpr** pExpr, SCondExpr* pCondExpr, int32_t* type,
S
slguan 已提交
3083 3084 3085 3086 3087
                                     int32_t parentOptr) {
  const char* msg1 = "meter query cannot use tags filter";
  const char* msg2 = "illegal column name";
  const char* msg3 = "only one query time range allowed";
  const char* msg4 = "only one join condition allowed";
H
hjxilinx 已提交
3088 3089 3090
  const char* msg5 = "not support ordinary column join";
  const char* msg6 = "only one query condition on tbname allowed";
  const char* msg7 = "only in/like allowed in filter table name";
H
hzcheng 已提交
3091

S
slguan 已提交
3092 3093
  tSQLExpr* pLeft = (*pExpr)->pLeft;
  tSQLExpr* pRight = (*pExpr)->pRight;
H
hzcheng 已提交
3094

S
slguan 已提交
3095 3096 3097
  int32_t ret = TSDB_CODE_SUCCESS;

  SColumnIndex index = COLUMN_INDEX_INITIALIZER;
3098
  if (getColumnIndexByName(&pLeft->colInfo, pQueryInfo, &index) != TSDB_CODE_SUCCESS) {
3099
    return invalidSqlErrMsg(pQueryInfo->msg, msg2);
H
hzcheng 已提交
3100 3101
  }

S
slguan 已提交
3102 3103
  assert(isExprDirectParentOfLeaftNode(*pExpr));

3104
  SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, index.tableIndex);
S
slguan 已提交
3105 3106 3107
  SMeterMeta*     pMeterMeta = pMeterMetaInfo->pMeterMeta;

  if (index.columnIndex == PRIMARYKEY_TIMESTAMP_COL_INDEX) {  // query on time range
3108
    if (!validateJoinExprNode(pQueryInfo, *pExpr, &index)) {
H
hzcheng 已提交
3109 3110
      return TSDB_CODE_INVALID_SQL;
    }
S
slguan 已提交
3111 3112 3113

    // set join query condition
    if (pRight->nSQLOptr == TK_ID) {  // no need to keep the timestamp join condition
3114
      pQueryInfo->type |= TSDB_QUERY_TYPE_JOIN_QUERY;
S
slguan 已提交
3115 3116 3117 3118 3119 3120 3121 3122
      pCondExpr->tsJoin = true;

      /*
       * to release expression, e.g., m1.ts = m2.ts,
       * since this expression is used to set the join query type
       */
      tSQLExprDestroy(*pExpr);
    } else {
3123
      ret = setExprToCond(&pCondExpr->pTimewindow, *pExpr, msg3, parentOptr, pQueryInfo->msg);
S
slguan 已提交
3124 3125 3126 3127 3128 3129 3130 3131
    }

    *pExpr = NULL;  // remove this expression
    *type = TSQL_EXPR_TS;
  } else if (index.columnIndex >= pMeterMeta->numOfColumns ||
             index.columnIndex == TSDB_TBNAME_COLUMN_INDEX) {  // query on tags
    // check for tag query condition
    if (UTIL_METER_IS_NOMRAL_METER(pMeterMetaInfo)) {
3132
      return invalidSqlErrMsg(pQueryInfo->msg, msg1);
S
slguan 已提交
3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144
    }

    // check for like expression
    if ((*pExpr)->nSQLOptr == TK_LIKE) {
      if (pRight->val.nLen > TSDB_PATTERN_STRING_MAX_LEN) {
        return TSDB_CODE_INVALID_SQL;
      }

      SSchema* pSchema = tsGetSchema(pMeterMetaInfo->pMeterMeta);

      if ((!isTablenameToken(&pLeft->colInfo)) && pSchema[index.columnIndex].type != TSDB_DATA_TYPE_BINARY &&
          pSchema[index.columnIndex].type != TSDB_DATA_TYPE_NCHAR) {
3145
        return invalidSqlErrMsg(pQueryInfo->msg, msg2);
S
slguan 已提交
3146 3147 3148 3149 3150 3151
      }
    }

    // in case of in operator, keep it in a seperate attribute
    if (index.columnIndex == TSDB_TBNAME_COLUMN_INDEX) {
      if (!validTableNameOptr(*pExpr)) {
3152
        return invalidSqlErrMsg(pQueryInfo->msg, msg7);
S
slguan 已提交
3153 3154 3155 3156 3157 3158 3159
      }

      if (pCondExpr->pTableCond == NULL) {
        pCondExpr->pTableCond = *pExpr;
        pCondExpr->relType = parentOptr;
        pCondExpr->tableCondIndex = index.tableIndex;
      } else {
3160
        return invalidSqlErrMsg(pQueryInfo->msg, msg6);
S
slguan 已提交
3161 3162 3163 3164 3165 3166
      }

      *type = TSQL_EXPR_TBNAME;
      *pExpr = NULL;
    } else {
      if (pRight->nSQLOptr == TK_ID) {  // join on tag columns for stable query
3167
        if (!validateJoinExprNode(pQueryInfo, *pExpr, &index)) {
S
slguan 已提交
3168 3169 3170 3171
          return TSDB_CODE_INVALID_SQL;
        }

        if (pCondExpr->pJoinExpr != NULL) {
3172
          return invalidSqlErrMsg(pQueryInfo->msg, msg4);
S
slguan 已提交
3173 3174
        }

3175 3176
        pQueryInfo->type |= TSDB_QUERY_TYPE_JOIN_QUERY;
        ret = setExprToCond(&pCondExpr->pJoinExpr, *pExpr, NULL, parentOptr, pQueryInfo->msg);
S
slguan 已提交
3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190
        *pExpr = NULL;
      } else {
        // do nothing
        //                ret = setExprToCond(pCmd, &pCondExpr->pTagCond,
        //                *pExpr, NULL, parentOptr);
      }

      *type = TSQL_EXPR_TAG;
    }

  } else {  // query on other columns
    *type = TSQL_EXPR_COLUMN;

    if (pRight->nSQLOptr == TK_ID) {  // other column cannot be served as the join column
3191
      return invalidSqlErrMsg(pQueryInfo->msg, msg5);
H
hzcheng 已提交
3192 3193
    }

3194
    ret = setExprToCond(&pCondExpr->pColumnCond, *pExpr, NULL, parentOptr, pQueryInfo->msg);
S
slguan 已提交
3195 3196
    *pExpr = NULL;  // remove it from expr tree
  }
H
hzcheng 已提交
3197

S
slguan 已提交
3198
  return ret;
H
hzcheng 已提交
3199 3200
}

3201 3202
int32_t getQueryCondExpr(SQueryInfo* pQueryInfo, tSQLExpr** pExpr, SCondExpr* pCondExpr, int32_t* type,
                         int32_t parentOptr) {
H
hzcheng 已提交
3203 3204 3205 3206
  if (pExpr == NULL) {
    return TSDB_CODE_SUCCESS;
  }

S
slguan 已提交
3207
  const char* msg1 = "query condition between different columns must use 'AND'";
H
hzcheng 已提交
3208

S
slguan 已提交
3209 3210 3211 3212
  tSQLExpr* pLeft = (*pExpr)->pLeft;
  tSQLExpr* pRight = (*pExpr)->pRight;

  if (!isValidExpr(pLeft, pRight, (*pExpr)->nSQLOptr)) {
H
hzcheng 已提交
3213 3214 3215
    return TSDB_CODE_INVALID_SQL;
  }

S
slguan 已提交
3216 3217
  int32_t leftType = -1;
  int32_t rightType = -1;
H
hzcheng 已提交
3218

S
slguan 已提交
3219
  if (!isExprDirectParentOfLeaftNode(*pExpr)) {
3220
    int32_t ret = getQueryCondExpr(pQueryInfo, &(*pExpr)->pLeft, pCondExpr, &leftType, (*pExpr)->nSQLOptr);
H
hzcheng 已提交
3221 3222 3223 3224
    if (ret != TSDB_CODE_SUCCESS) {
      return ret;
    }

3225
    ret = getQueryCondExpr(pQueryInfo, &(*pExpr)->pRight, pCondExpr, &rightType, (*pExpr)->nSQLOptr);
S
slguan 已提交
3226 3227 3228
    if (ret != TSDB_CODE_SUCCESS) {
      return ret;
    }
H
hzcheng 已提交
3229

S
slguan 已提交
3230 3231 3232 3233 3234 3235
    /*
     *  if left child and right child do not belong to the same group, the sub
     *  expression is not valid for parent node, it must be TK_AND operator.
     */
    if (leftType != rightType) {
      if ((*pExpr)->nSQLOptr == TK_OR && (leftType + rightType != TSQL_EXPR_TBNAME + TSQL_EXPR_TAG)) {
3236
        return invalidSqlErrMsg(pQueryInfo->msg, msg1);
S
slguan 已提交
3237
      }
H
hzcheng 已提交
3238 3239
    }

S
slguan 已提交
3240 3241 3242
    *type = rightType;
    return TSDB_CODE_SUCCESS;
  }
H
hzcheng 已提交
3243

S
slguan 已提交
3244
  exchangeExpr(*pExpr);
H
hzcheng 已提交
3245

3246
  return handleExprInQueryCond(pQueryInfo, pExpr, pCondExpr, type, parentOptr);
S
slguan 已提交
3247
}
H
hzcheng 已提交
3248

S
slguan 已提交
3249 3250 3251 3252
static void doCompactQueryExpr(tSQLExpr** pExpr) {
  if (*pExpr == NULL || isExprDirectParentOfLeaftNode(*pExpr)) {
    return;
  }
H
hzcheng 已提交
3253

S
slguan 已提交
3254 3255 3256
  if ((*pExpr)->pLeft) {
    doCompactQueryExpr(&(*pExpr)->pLeft);
  }
H
hzcheng 已提交
3257

S
slguan 已提交
3258 3259 3260
  if ((*pExpr)->pRight) {
    doCompactQueryExpr(&(*pExpr)->pRight);
  }
H
hzcheng 已提交
3261

S
slguan 已提交
3262 3263 3264 3265
  if ((*pExpr)->pLeft == NULL && (*pExpr)->pRight == NULL &&
      ((*pExpr)->nSQLOptr == TK_OR || (*pExpr)->nSQLOptr == TK_AND)) {
    tSQLExprNodeDestroy(*pExpr);
    *pExpr = NULL;
H
hzcheng 已提交
3266

S
slguan 已提交
3267 3268 3269
  } else if ((*pExpr)->pLeft == NULL && (*pExpr)->pRight != NULL) {
    tSQLExpr* tmpPtr = (*pExpr)->pRight;
    tSQLExprNodeDestroy(*pExpr);
H
hzcheng 已提交
3270

S
slguan 已提交
3271 3272 3273 3274
    (*pExpr) = tmpPtr;
  } else if ((*pExpr)->pRight == NULL && (*pExpr)->pLeft != NULL) {
    tSQLExpr* tmpPtr = (*pExpr)->pLeft;
    tSQLExprNodeDestroy(*pExpr);
H
hzcheng 已提交
3275

S
slguan 已提交
3276
    (*pExpr) = tmpPtr;
H
hzcheng 已提交
3277
  }
S
slguan 已提交
3278
}
H
hzcheng 已提交
3279

3280
static void doExtractExprForSTable(tSQLExpr** pExpr, SQueryInfo* pQueryInfo, tSQLExpr** pOut, int32_t tableIndex) {
S
slguan 已提交
3281 3282 3283 3284
  if (isExprDirectParentOfLeaftNode(*pExpr)) {
    tSQLExpr* pLeft = (*pExpr)->pLeft;

    SColumnIndex index = COLUMN_INDEX_INITIALIZER;
3285
    if (getColumnIndexByName(&pLeft->colInfo, pQueryInfo, &index) != TSDB_CODE_SUCCESS) {
S
slguan 已提交
3286
      return;
H
hzcheng 已提交
3287 3288
    }

S
slguan 已提交
3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301
    if (index.tableIndex != tableIndex) {
      return;
    }

    SSQLToken t = {0};
    extractTableNameFromToken(&pLeft->colInfo, &t);

    *pOut = *pExpr;
    (*pExpr) = NULL;

  } else {
    *pOut = tSQLExprCreate(NULL, NULL, (*pExpr)->nSQLOptr);

3302 3303
    doExtractExprForSTable(&(*pExpr)->pLeft, pQueryInfo, &((*pOut)->pLeft), tableIndex);
    doExtractExprForSTable(&(*pExpr)->pRight, pQueryInfo, &((*pOut)->pRight), tableIndex);
S
slguan 已提交
3304 3305 3306
  }
}

3307
static tSQLExpr* extractExprForSTable(tSQLExpr** pExpr, SQueryInfo* pQueryInfo, int32_t tableIndex) {
S
slguan 已提交
3308
  tSQLExpr* pResExpr = NULL;
H
hzcheng 已提交
3309

S
slguan 已提交
3310
  if (*pExpr != NULL) {
3311
    doExtractExprForSTable(pExpr, pQueryInfo, &pResExpr, tableIndex);
S
slguan 已提交
3312
    doCompactQueryExpr(&pResExpr);
H
hzcheng 已提交
3313 3314
  }

S
slguan 已提交
3315
  return pResExpr;
H
hzcheng 已提交
3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330
}

int tableNameCompar(const void* lhs, const void* rhs) {
  char* left = *(char**)lhs;
  char* right = *(char**)rhs;

  int32_t ret = strcmp(left, right);

  if (ret == 0) {
    return 0;
  }

  return ret > 0 ? 1 : -1;
}

3331 3332
static int32_t setTableCondForMetricQuery(SQueryInfo* pQueryInfo, const char* account, tSQLExpr* pExpr,
                                          int16_t tableCondIndex, SStringBuilder* sb) {
3333
  const char* msg = "table name too long";
H
hzcheng 已提交
3334

S
slguan 已提交
3335 3336 3337 3338
  if (pExpr == NULL) {
    return TSDB_CODE_SUCCESS;
  }

3339
  SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, tableCondIndex);
S
slguan 已提交
3340

3341
  STagCond* pTagCond = &pQueryInfo->tagCond;
S
slguan 已提交
3342 3343 3344 3345 3346
  pTagCond->tbnameCond.uid = pMeterMetaInfo->pMeterMeta->uid;

  assert(pExpr->nSQLOptr == TK_LIKE || pExpr->nSQLOptr == TK_IN);

  if (pExpr->nSQLOptr == TK_LIKE) {
H
hjxilinx 已提交
3347
    char* str = taosStringBuilderGetResult(sb, NULL);
3348
    pQueryInfo->tagCond.tbnameCond.cond = strdup(str);
S
slguan 已提交
3349 3350 3351
    return TSDB_CODE_SUCCESS;
  }

H
hjxilinx 已提交
3352 3353
  SStringBuilder sb1 = {0};
  taosStringBuilderAppendStringLen(&sb1, QUERY_COND_REL_PREFIX_IN, QUERY_COND_REL_PREFIX_IN_LEN);
H
hzcheng 已提交
3354 3355 3356

  char db[TSDB_METER_ID_LEN] = {0};

S
slguan 已提交
3357
  // remove the duplicated input table names
H
hzcheng 已提交
3358
  int32_t num = 0;
H
hjxilinx 已提交
3359 3360 3361
  char*   tableNameString = taosStringBuilderGetResult(sb, NULL);

  char** segments = strsplit(tableNameString + QUERY_COND_REL_PREFIX_IN_LEN, TBNAME_LIST_SEP, &num);
H
hjxilinx 已提交
3362
  qsort(segments, num, POINTER_BYTES, tableNameCompar);
H
hzcheng 已提交
3363 3364 3365 3366 3367 3368 3369 3370 3371

  int32_t j = 1;
  for (int32_t i = 1; i < num; ++i) {
    if (strcmp(segments[i], segments[i - 1]) != 0) {
      segments[j++] = segments[i];
    }
  }
  num = j;

S
slguan 已提交
3372
  SSQLToken dbToken = extractDBName(pMeterMetaInfo->name, db);
H
hzcheng 已提交
3373 3374 3375

  for (int32_t i = 0; i < num; ++i) {
    if (i >= 1) {
H
hjxilinx 已提交
3376
      taosStringBuilderAppendStringLen(&sb1, TBNAME_LIST_SEP, 1);
H
hzcheng 已提交
3377
    }
H
hjxilinx 已提交
3378 3379

    char      idBuf[TSDB_METER_ID_LEN + 1] = {0};
S
slguan 已提交
3380
    int32_t   xlen = strlen(segments[i]);
H
hzcheng 已提交
3381 3382
    SSQLToken t = {.z = segments[i], .n = xlen, .type = TK_STRING};

3383
    int32_t ret = setObjFullName(idBuf, account, &dbToken, &t, &xlen);
H
hzcheng 已提交
3384
    if (ret != TSDB_CODE_SUCCESS) {
H
hjxilinx 已提交
3385
      taosStringBuilderDestroy(&sb1);
H
hzcheng 已提交
3386
      tfree(segments);
H
hjxilinx 已提交
3387

3388
      invalidSqlErrMsg(pQueryInfo->msg, msg);
H
hzcheng 已提交
3389 3390
      return ret;
    }
H
hjxilinx 已提交
3391

H
hjxilinx 已提交
3392
    taosStringBuilderAppendString(&sb1, idBuf);
H
hzcheng 已提交
3393
  }
H
hjxilinx 已提交
3394

H
hjxilinx 已提交
3395
  char* str = taosStringBuilderGetResult(&sb1, NULL);
3396
  pQueryInfo->tagCond.tbnameCond.cond = strdup(str);
H
hjxilinx 已提交
3397

H
hjxilinx 已提交
3398
  taosStringBuilderDestroy(&sb1);
H
hzcheng 已提交
3399 3400 3401 3402
  tfree(segments);
  return TSDB_CODE_SUCCESS;
}

3403 3404 3405
static bool validateFilterExpr(SQueryInfo* pQueryInfo) {
  for (int32_t i = 0; i < pQueryInfo->colList.numOfCols; ++i) {
    SColumnBase* pColBase = &pQueryInfo->colList.pColList[i];
3406

S
slguan 已提交
3407 3408 3409 3410
    for (int32_t j = 0; j < pColBase->numOfFilters; ++j) {
      SColumnFilterInfo* pColFilter = &pColBase->filterInfo[j];
      int32_t            lowerOptr = pColFilter->lowerRelOptr;
      int32_t            upperOptr = pColFilter->upperRelOptr;
3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426

      if ((lowerOptr == TSDB_RELATION_LARGE_EQUAL || lowerOptr == TSDB_RELATION_LARGE) &&
          (upperOptr == TSDB_RELATION_LESS_EQUAL || upperOptr == TSDB_RELATION_LESS)) {
        continue;
      }

      // there must be at least two range, not support yet.
      if (lowerOptr * upperOptr != TSDB_RELATION_INVALID) {
        return false;
      }
    }
  }

  return true;
}

3427
static int32_t getTimeRangeFromExpr(SQueryInfo* pQueryInfo, tSQLExpr* pExpr) {
S
slguan 已提交
3428 3429
  const char* msg0 = "invalid timestamp";
  const char* msg1 = "only one time stamp window allowed";
H
hzcheng 已提交
3430 3431 3432 3433 3434

  if (pExpr == NULL) {
    return TSDB_CODE_SUCCESS;
  }

S
slguan 已提交
3435 3436
  if (!isExprDirectParentOfLeaftNode(pExpr)) {
    if (pExpr->nSQLOptr == TK_OR) {
3437
      return invalidSqlErrMsg(pQueryInfo->msg, msg1);
S
slguan 已提交
3438
    }
H
hzcheng 已提交
3439

3440
    getTimeRangeFromExpr(pQueryInfo, pExpr->pLeft);
S
slguan 已提交
3441

3442
    return getTimeRangeFromExpr(pQueryInfo, pExpr->pRight);
S
slguan 已提交
3443 3444
  } else {
    SColumnIndex index = COLUMN_INDEX_INITIALIZER;
3445
    if (getColumnIndexByName(&pExpr->pLeft->colInfo, pQueryInfo, &index) != TSDB_CODE_SUCCESS) {
S
slguan 已提交
3446 3447 3448
      return TSDB_CODE_INVALID_SQL;
    }

3449
    SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, index.tableIndex);
S
slguan 已提交
3450 3451 3452 3453 3454 3455 3456 3457
    SMeterMeta*     pMeterMeta = pMeterMetaInfo->pMeterMeta;

    tSQLExpr* pRight = pExpr->pRight;

    TSKEY stime = 0;
    TSKEY etime = INT64_MAX;

    if (getTimeRange(&stime, &etime, pRight, pExpr->nSQLOptr, pMeterMeta->precision) != TSDB_CODE_SUCCESS) {
3458
      return invalidSqlErrMsg(pQueryInfo->msg, msg0);
S
slguan 已提交
3459 3460 3461
    }

    // update the timestamp query range
3462 3463
    if (pQueryInfo->stime < stime) {
      pQueryInfo->stime = stime;
S
slguan 已提交
3464 3465
    }

3466 3467
    if (pQueryInfo->etime > etime) {
      pQueryInfo->etime = etime;
S
slguan 已提交
3468 3469 3470 3471 3472 3473
    }
  }

  return TSDB_CODE_SUCCESS;
}

3474
static int32_t validateJoinExpr(SQueryInfo* pQueryInfo, SCondExpr* pCondExpr) {
S
slguan 已提交
3475 3476 3477 3478
  const char* msg1 = "super table join requires tags column";
  const char* msg2 = "timestamp join condition missing";
  const char* msg3 = "condition missing for join query";

3479 3480
  if (!QUERY_IS_JOIN_QUERY(pQueryInfo->type)) {
    if (pQueryInfo->numOfTables == 1) {
S
slguan 已提交
3481 3482
      return TSDB_CODE_SUCCESS;
    } else {
3483
      return invalidSqlErrMsg(pQueryInfo->msg, msg3);
S
slguan 已提交
3484 3485 3486
    }
  }

3487 3488
  SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0);
  if (UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo)) {  // for stable join, tag columns
3489
                                                   // must be present for join
S
slguan 已提交
3490
    if (pCondExpr->pJoinExpr == NULL) {
3491
      return invalidSqlErrMsg(pQueryInfo->msg, msg1);
S
slguan 已提交
3492 3493 3494 3495
    }
  }

  if (!pCondExpr->tsJoin) {
3496
    return invalidSqlErrMsg(pQueryInfo->msg, msg2);
H
hzcheng 已提交
3497 3498
  }

S
slguan 已提交
3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523
  return TSDB_CODE_SUCCESS;
}

static void cleanQueryExpr(SCondExpr* pCondExpr) {
  if (pCondExpr->pTableCond) {
    tSQLExprDestroy(pCondExpr->pTableCond);
  }

  if (pCondExpr->pTagCond) {
    tSQLExprDestroy(pCondExpr->pTagCond);
  }

  if (pCondExpr->pColumnCond) {
    tSQLExprDestroy(pCondExpr->pColumnCond);
  }

  if (pCondExpr->pTimewindow) {
    tSQLExprDestroy(pCondExpr->pTimewindow);
  }

  if (pCondExpr->pJoinExpr) {
    tSQLExprDestroy(pCondExpr->pJoinExpr);
  }
}

3524 3525 3526
static void doAddJoinTagsColumnsIntoTagList(SQueryInfo* pQueryInfo, SCondExpr* pCondExpr) {
  SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0);
  if (QUERY_IS_JOIN_QUERY(pQueryInfo->type) && UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo)) {
S
slguan 已提交
3527
    SColumnIndex index = {0};
H
hjxilinx 已提交
3528

3529
    getColumnIndexByName(&pCondExpr->pJoinExpr->pLeft->colInfo, pQueryInfo, &index);
3530
    pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, index.tableIndex);
H
hjxilinx 已提交
3531

S
slguan 已提交
3532
    int32_t columnInfo = index.columnIndex - pMeterMetaInfo->pMeterMeta->numOfColumns;
3533
    addRequiredTagColumn(pQueryInfo, columnInfo, index.tableIndex);
H
hjxilinx 已提交
3534

3535
    getColumnIndexByName(&pCondExpr->pJoinExpr->pRight->colInfo, pQueryInfo, &index);
3536
    pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, index.tableIndex);
H
hjxilinx 已提交
3537

S
slguan 已提交
3538
    columnInfo = index.columnIndex - pMeterMetaInfo->pMeterMeta->numOfColumns;
3539
    addRequiredTagColumn(pQueryInfo, columnInfo, index.tableIndex);
S
slguan 已提交
3540
  }
H
hjxilinx 已提交
3541
}
S
slguan 已提交
3542

3543
static int32_t getTagQueryCondExpr(SQueryInfo* pQueryInfo, SCondExpr* pCondExpr, tSQLExpr** pExpr) {
H
hjxilinx 已提交
3544
  int32_t ret = TSDB_CODE_SUCCESS;
H
hjxilinx 已提交
3545

H
hjxilinx 已提交
3546
  if (pCondExpr->pTagCond != NULL) {
3547 3548
    for (int32_t i = 0; i < pQueryInfo->numOfTables; ++i) {
      tSQLExpr* p1 = extractExprForSTable(pExpr, pQueryInfo, i);
H
hjxilinx 已提交
3549

3550
      SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, i);
H
hjxilinx 已提交
3551

H
hjxilinx 已提交
3552 3553
      char  c[TSDB_MAX_TAGS_LEN] = {0};
      char* str = c;
H
hjxilinx 已提交
3554

3555
      if ((ret = getTagCondString(p1, &str)) != TSDB_CODE_SUCCESS) {
H
hjxilinx 已提交
3556 3557
        return ret;
      }
H
hjxilinx 已提交
3558

3559
      tsSetMetricQueryCond(&pQueryInfo->tagCond, pMeterMetaInfo->pMeterMeta->uid, c);
H
hjxilinx 已提交
3560

H
hjxilinx 已提交
3561 3562 3563
      doCompactQueryExpr(pExpr);
      tSQLExprDestroy(p1);
    }
H
hjxilinx 已提交
3564

H
hjxilinx 已提交
3565 3566
    pCondExpr->pTagCond = NULL;
  }
H
hjxilinx 已提交
3567

S
slguan 已提交
3568 3569
  return ret;
}
3570
int32_t parseWhereClause(SQueryInfo* pQueryInfo, tSQLExpr** pExpr, SSqlObj* pSql) {
H
hjxilinx 已提交
3571 3572 3573
  if (pExpr == NULL) {
    return TSDB_CODE_SUCCESS;
  }
H
hjxilinx 已提交
3574

S
slguan 已提交
3575
  const char* msg = "invalid filter expression";
H
hjxilinx 已提交
3576
  const char* msg1 = "invalid expression";
H
hjxilinx 已提交
3577

H
hjxilinx 已提交
3578
  int32_t ret = TSDB_CODE_SUCCESS;
H
hjxilinx 已提交
3579

3580 3581
  pQueryInfo->stime = 0;
  pQueryInfo->etime = INT64_MAX;
S
slguan 已提交
3582

H
hjxilinx 已提交
3583
  // tags query condition may be larger than 512bytes, therefore, we need to prepare enough large space
H
hjxilinx 已提交
3584
  SStringBuilder sb = {0};
H
hjxilinx 已提交
3585
  SCondExpr      condExpr = {0};
S
slguan 已提交
3586

H
hjxilinx 已提交
3587
  if ((*pExpr)->pLeft == NULL || (*pExpr)->pRight == NULL) {
3588
    return invalidSqlErrMsg(pQueryInfo->msg, msg1);
S
slguan 已提交
3589 3590
  }

H
hjxilinx 已提交
3591
  int32_t type = 0;
3592
  if ((ret = getQueryCondExpr(pQueryInfo, pExpr, &condExpr, &type, (*pExpr)->nSQLOptr)) != TSDB_CODE_SUCCESS) {
H
hjxilinx 已提交
3593 3594
    return ret;
  }
H
hjxilinx 已提交
3595

S
slguan 已提交
3596
  doCompactQueryExpr(pExpr);
H
hjxilinx 已提交
3597

S
slguan 已提交
3598
  // after expression compact, the expression tree is only include tag query condition
H
hjxilinx 已提交
3599
  condExpr.pTagCond = (*pExpr);
H
hjxilinx 已提交
3600

S
slguan 已提交
3601
  // 1. check if it is a join query
3602
  if ((ret = validateJoinExpr(pQueryInfo, &condExpr)) != TSDB_CODE_SUCCESS) {
S
slguan 已提交
3603 3604
    return ret;
  }
H
hjxilinx 已提交
3605

S
slguan 已提交
3606
  // 2. get the query time range
3607
  if ((ret = getTimeRangeFromExpr(pQueryInfo, condExpr.pTimewindow)) != TSDB_CODE_SUCCESS) {
S
slguan 已提交
3608 3609
    return ret;
  }
H
hjxilinx 已提交
3610

S
slguan 已提交
3611
  // 3. get the tag query condition
3612
  if ((ret = getTagQueryCondExpr(pQueryInfo, &condExpr, pExpr)) != TSDB_CODE_SUCCESS) {
H
hjxilinx 已提交
3613
    return ret;
S
slguan 已提交
3614
  }
H
hjxilinx 已提交
3615

S
slguan 已提交
3616
  // 4. get the table name query condition
3617
  if ((ret = getTablenameCond(pQueryInfo, condExpr.pTableCond, &sb)) != TSDB_CODE_SUCCESS) {
S
slguan 已提交
3618 3619
    return ret;
  }
H
hjxilinx 已提交
3620

S
slguan 已提交
3621
  // 5. other column query condition
3622
  if ((ret = getColumnQueryCondInfo(pQueryInfo, condExpr.pColumnCond, TK_AND)) != TSDB_CODE_SUCCESS) {
S
slguan 已提交
3623 3624
    return ret;
  }
H
hjxilinx 已提交
3625

S
slguan 已提交
3626
  // 6. join condition
3627
  if ((ret = getJoinCondInfo(pQueryInfo, condExpr.pJoinExpr)) != TSDB_CODE_SUCCESS) {
S
slguan 已提交
3628
    return ret;
H
hzcheng 已提交
3629
  }
H
hjxilinx 已提交
3630

S
slguan 已提交
3631
  // 7. query condition for table name
3632
  pQueryInfo->tagCond.relType = (condExpr.relType == TK_AND) ? TSDB_RELATION_AND : TSDB_RELATION_OR;
H
hjxilinx 已提交
3633

3634
  ret = setTableCondForMetricQuery(pQueryInfo, getAccountId(pSql), condExpr.pTableCond, condExpr.tableCondIndex, &sb);
H
hjxilinx 已提交
3635
  taosStringBuilderDestroy(&sb);
H
hjxilinx 已提交
3636

3637 3638
  if (!validateFilterExpr(pQueryInfo)) {
    return invalidSqlErrMsg(pQueryInfo->msg, msg);
3639
  }
H
hjxilinx 已提交
3640

3641
  doAddJoinTagsColumnsIntoTagList(pQueryInfo, &condExpr);
H
hjxilinx 已提交
3642

H
hjxilinx 已提交
3643
  cleanQueryExpr(&condExpr);
H
hzcheng 已提交
3644 3645 3646 3647
  return ret;
}

int32_t getTimeRange(int64_t* stime, int64_t* etime, tSQLExpr* pRight, int32_t optr, int16_t timePrecision) {
S
slguan 已提交
3648 3649 3650 3651 3652
  // this is join condition, do nothing
  if (pRight->nSQLOptr == TK_ID) {
    return TSDB_CODE_SUCCESS;
  }

S
slguan 已提交
3653 3654
  /*
   * filter primary ts filter expression like:
S
slguan 已提交
3655
   * where ts in ('2015-12-12 4:8:12')
S
slguan 已提交
3656 3657 3658 3659
   */
  if (pRight->nSQLOptr == TK_SET || optr == TK_IN) {
    return TSDB_CODE_INVALID_SQL;
  }
H
hzcheng 已提交
3660 3661 3662 3663

  int64_t val = 0;
  bool    parsed = false;
  if (pRight->val.nType == TSDB_DATA_TYPE_BINARY) {
S
slguan 已提交
3664 3665
    pRight->val.nLen = strdequote(pRight->val.pz);

S
slguan 已提交
3666
    char* seg = strnchr(pRight->val.pz, '-', pRight->val.nLen, false);
H
hzcheng 已提交
3667 3668 3669 3670 3671 3672
    if (seg != NULL) {
      if (taosParseTime(pRight->val.pz, &val, pRight->val.nLen, TSDB_TIME_PRECISION_MICRO) == TSDB_CODE_SUCCESS) {
        parsed = true;
      } else {
        return TSDB_CODE_INVALID_SQL;
      }
S
slguan 已提交
3673 3674 3675 3676 3677 3678 3679
    } else {
      SSQLToken token = {.z = pRight->val.pz, .n = pRight->val.nLen, .type = TK_ID};
      int32_t   len = tSQLGetToken(pRight->val.pz, &token.type);

      if ((token.type != TK_INTEGER && token.type != TK_FLOAT) || len != pRight->val.nLen) {
        return TSDB_CODE_INVALID_SQL;
      }
H
hzcheng 已提交
3680 3681 3682 3683
    }
  } else if (pRight->nSQLOptr == TK_INTEGER && timePrecision == TSDB_TIME_PRECISION_MILLI) {
    /*
     * if the pRight->nSQLOptr == TK_INTEGER/TK_FLOAT, the value is adaptive, we
S
slguan 已提交
3684
     * need the time precision in metermeta to transfer the value in MICROSECOND
H
hzcheng 已提交
3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735
     *
     * Additional check to avoid data overflow
     */
    if (pRight->val.i64Key <= INT64_MAX / 1000) {
      pRight->val.i64Key *= 1000;
    }
  } else if (pRight->nSQLOptr == TK_FLOAT && timePrecision == TSDB_TIME_PRECISION_MILLI) {
    pRight->val.dKey *= 1000;
  }

  if (!parsed) {
    /*
     * failed to parse timestamp in regular formation, try next
     * it may be a epoch time in string format
     */
    tVariantDump(&pRight->val, (char*)&val, TSDB_DATA_TYPE_BIGINT);

    /*
     * transfer it into MICROSECOND format if it is a string, since for
     * TK_INTEGER/TK_FLOAT the value has been transferred
     *
     * additional check to avoid data overflow
     */
    if (pRight->nSQLOptr == TK_STRING && timePrecision == TSDB_TIME_PRECISION_MILLI) {
      if (val <= INT64_MAX / 1000) {
        val *= 1000;
      }
    }
  }

  int32_t delta = 1;
  /* for millisecond, delta is 1ms=1000us */
  if (timePrecision == TSDB_TIME_PRECISION_MILLI) {
    delta *= 1000;
  }

  if (optr == TK_LE) {
    *etime = val;
  } else if (optr == TK_LT) {
    *etime = val - delta;
  } else if (optr == TK_GT) {
    *stime = val + delta;
  } else if (optr == TK_GE) {
    *stime = val;
  } else if (optr == TK_EQ) {
    *stime = val;
    *etime = *stime;
  }
  return TSDB_CODE_SUCCESS;
}

3736
int32_t tsRewriteFieldNameIfNecessary(SQueryInfo* pQueryInfo) {
S
slguan 已提交
3737 3738
  const char rep[] = {'(', ')', '*', ',', '.', '/', '\\', '+', '-', '%', ' '};

3739 3740
  for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) {
    char* fieldName = tscFieldInfoGetField(pQueryInfo, i)->name;
H
hzcheng 已提交
3741
    for (int32_t j = 0; j < TSDB_COL_NAME_LEN && fieldName[j] != 0; ++j) {
S
slguan 已提交
3742 3743 3744 3745 3746
      for (int32_t k = 0; k < tListLen(rep); ++k) {
        if (fieldName[j] == rep[k]) {
          fieldName[j] = '_';
          break;
        }
H
hzcheng 已提交
3747 3748
      }
    }
S
slguan 已提交
3749

H
hzcheng 已提交
3750 3751 3752 3753
    fieldName[TSDB_COL_NAME_LEN - 1] = 0;
  }

  // the column name may be identical, here check again
3754 3755 3756 3757
  for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) {
    char* fieldName = tscFieldInfoGetField(pQueryInfo, i)->name;
    for (int32_t j = i + 1; j < pQueryInfo->fieldsInfo.numOfOutputCols; ++j) {
      if (strncasecmp(fieldName, tscFieldInfoGetField(pQueryInfo, j)->name, TSDB_COL_NAME_LEN) == 0) {
3758
        const char* msg = "duplicated column name in new table";
3759
        return invalidSqlErrMsg(pQueryInfo->msg, msg);
H
hzcheng 已提交
3760 3761 3762 3763 3764 3765 3766
      }
    }
  }

  return TSDB_CODE_SUCCESS;
}

3767
int32_t parseFillClause(SQueryInfo* pQueryInfo, SQuerySQL* pQuerySQL) {
H
hzcheng 已提交
3768 3769 3770 3771
  tVariantList*     pFillToken = pQuerySQL->fillType;
  tVariantListItem* pItem = &pFillToken->a[0];

  const int32_t START_INTERPO_COL_IDX = 1;
3772 3773 3774 3775

  const char* msg = "illegal value or data overflow";
  const char* msg1 = "value is expected";
  const char* msg2 = "invalid fill option";
H
hzcheng 已提交
3776 3777

  if (pItem->pVar.nType != TSDB_DATA_TYPE_BINARY) {
3778
    return invalidSqlErrMsg(pQueryInfo->msg, msg2);
H
hzcheng 已提交
3779 3780
  }

3781 3782 3783 3784 3785 3786 3787
  if (pQueryInfo->defaultVal == NULL) {
    pQueryInfo->defaultVal = calloc(pQueryInfo->fieldsInfo.numOfOutputCols, sizeof(int64_t));
    if (pQueryInfo->defaultVal == NULL) {
      return TSDB_CODE_CLI_OUT_OF_MEMORY;
    }
  }

H
hzcheng 已提交
3788
  if (strncasecmp(pItem->pVar.pz, "none", 4) == 0 && pItem->pVar.nLen == 4) {
3789
    pQueryInfo->interpoType = TSDB_INTERPO_NONE;
H
hzcheng 已提交
3790
  } else if (strncasecmp(pItem->pVar.pz, "null", 4) == 0 && pItem->pVar.nLen == 4) {
3791 3792 3793 3794
    pQueryInfo->interpoType = TSDB_INTERPO_NULL;
    for (int32_t i = START_INTERPO_COL_IDX; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) {
      TAOS_FIELD* pFields = tscFieldInfoGetField(pQueryInfo, i);
      setNull((char*)&pQueryInfo->defaultVal[i], pFields->type, pFields->bytes);
H
hzcheng 已提交
3795 3796
    }
  } else if (strncasecmp(pItem->pVar.pz, "prev", 4) == 0 && pItem->pVar.nLen == 4) {
3797
    pQueryInfo->interpoType = TSDB_INTERPO_PREV;
H
hzcheng 已提交
3798
  } else if (strncasecmp(pItem->pVar.pz, "linear", 6) == 0 && pItem->pVar.nLen == 6) {
3799
    pQueryInfo->interpoType = TSDB_INTERPO_LINEAR;
H
hzcheng 已提交
3800
  } else if (strncasecmp(pItem->pVar.pz, "value", 5) == 0 && pItem->pVar.nLen == 5) {
3801
    pQueryInfo->interpoType = TSDB_INTERPO_SET_VALUE;
H
hzcheng 已提交
3802 3803

    if (pFillToken->nExpr == 1) {
3804
      return invalidSqlErrMsg(pQueryInfo->msg, msg1);
H
hzcheng 已提交
3805 3806 3807 3808 3809 3810
    }

    int32_t startPos = 1;
    int32_t numOfFillVal = pFillToken->nExpr - 1;

    /* for point interpolation query, we do not have the timestamp column */
3811
    if (tscIsPointInterpQuery(pQueryInfo)) {
H
hzcheng 已提交
3812 3813
      startPos = 0;

3814 3815
      if (numOfFillVal > pQueryInfo->fieldsInfo.numOfOutputCols) {
        numOfFillVal = pQueryInfo->fieldsInfo.numOfOutputCols;
H
hzcheng 已提交
3816 3817
      }
    } else {
3818 3819 3820
      numOfFillVal = (pFillToken->nExpr > pQueryInfo->fieldsInfo.numOfOutputCols)
                         ? pQueryInfo->fieldsInfo.numOfOutputCols
                         : pFillToken->nExpr;
H
hzcheng 已提交
3821 3822 3823 3824 3825
    }

    int32_t j = 1;

    for (int32_t i = startPos; i < numOfFillVal; ++i, ++j) {
3826
      TAOS_FIELD* pFields = tscFieldInfoGetField(pQueryInfo, i);
3827 3828 3829 3830 3831 3832
  
      if (pFields->type == TSDB_DATA_TYPE_BINARY || pFields->type == TSDB_DATA_TYPE_NCHAR) {
        setNull((char*)(&pQueryInfo->defaultVal[i]), pFields->type, pFields->bytes);
        continue;
      }
      
3833
      int32_t ret = tVariantDump(&pFillToken->a[j].pVar, (char*)&pQueryInfo->defaultVal[i], pFields->type);
H
hzcheng 已提交
3834
      if (ret != TSDB_CODE_SUCCESS) {
3835
        return invalidSqlErrMsg(pQueryInfo->msg, msg);
H
hzcheng 已提交
3836 3837 3838
      }
    }

3839 3840
    if ((pFillToken->nExpr < pQueryInfo->fieldsInfo.numOfOutputCols) ||
        ((pFillToken->nExpr - 1 < pQueryInfo->fieldsInfo.numOfOutputCols) && (tscIsPointInterpQuery(pQueryInfo)))) {
H
hzcheng 已提交
3841 3842
      tVariantListItem* lastItem = &pFillToken->a[pFillToken->nExpr - 1];

3843 3844 3845
      for (int32_t i = numOfFillVal; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) {
        TAOS_FIELD* pFields = tscFieldInfoGetField(pQueryInfo, i);
        tVariantDump(&lastItem->pVar, (char*)&pQueryInfo->defaultVal[i], pFields->type);
H
hzcheng 已提交
3846 3847

        if (pFields->type == TSDB_DATA_TYPE_BINARY || pFields->type == TSDB_DATA_TYPE_NCHAR) {
3848
          setNull((char*)(&pQueryInfo->defaultVal[i]), pFields->type, pFields->bytes);
H
hzcheng 已提交
3849 3850 3851 3852
        }
      }
    }
  } else {
3853
    return invalidSqlErrMsg(pQueryInfo->msg, msg2);
H
hzcheng 已提交
3854 3855 3856 3857 3858
  }

  return TSDB_CODE_SUCCESS;
}

3859
static void setDefaultOrderInfo(SQueryInfo* pQueryInfo) {
H
hzcheng 已提交
3860
  /* set default timestamp order information for all queries */
3861 3862
  pQueryInfo->order.order = TSQL_SO_ASC;
  SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0);
H
hzcheng 已提交
3863

3864 3865 3866
  if (isTopBottomQuery(pQueryInfo)) {
    pQueryInfo->order.order = TSQL_SO_ASC;
    pQueryInfo->order.orderColId = PRIMARYKEY_TIMESTAMP_COL_INDEX;
H
hzcheng 已提交
3867
  } else {
3868
    pQueryInfo->order.orderColId = -1;
H
hzcheng 已提交
3869 3870 3871
  }

  /* for metric query, set default ascending order for group output */
3872 3873
  if (UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo)) {
    pQueryInfo->groupbyExpr.orderType = TSQL_SO_ASC;
H
hzcheng 已提交
3874 3875 3876
  }
}

H
hjxilinx 已提交
3877
int32_t parseOrderbyClause(SQueryInfo* pQueryInfo, SQuerySQL* pQuerySql, SSchema* pSchema) {
S
slguan 已提交
3878 3879 3880 3881
  const char* msg0 = "only support order by primary timestamp";
  const char* msg1 = "invalid column name";
  const char* msg2 = "only support order by primary timestamp and queried column";
  const char* msg3 = "only support order by primary timestamp and first tag in groupby clause";
H
hzcheng 已提交
3882

3883 3884
  setDefaultOrderInfo(pQueryInfo);
  SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0);
H
hzcheng 已提交
3885 3886 3887 3888 3889 3890

  if (pQuerySql->pSortOrder == NULL) {
    return TSDB_CODE_SUCCESS;
  }

  tVariantList* pSortorder = pQuerySql->pSortOrder;
S
slguan 已提交
3891 3892 3893 3894 3895 3896 3897 3898

  /*
   * for table query, there is only one or none order option is allowed, which is the
   * ts or values(top/bottom) order is supported.
   *
   * for super table query, the order option must be less than 3.
   */
  if (UTIL_METER_IS_NOMRAL_METER(pMeterMetaInfo)) {
H
hzcheng 已提交
3899
    if (pSortorder->nExpr > 1) {
3900
      return invalidSqlErrMsg(pQueryInfo->msg, msg0);
H
hzcheng 已提交
3901 3902 3903
    }
  } else {
    if (pSortorder->nExpr > 2) {
3904
      return invalidSqlErrMsg(pQueryInfo->msg, msg3);
H
hzcheng 已提交
3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915
    }
  }

  // handle the first part of order by
  tVariant* pVar = &pSortorder->a[0].pVar;

  // e.g., order by 1 asc, return directly with out further check.
  if (pVar->nType >= TSDB_DATA_TYPE_TINYINT && pVar->nType <= TSDB_DATA_TYPE_BIGINT) {
    return TSDB_CODE_SUCCESS;
  }

S
slguan 已提交
3916 3917 3918
  SSQLToken    columnName = {pVar->nLen, pVar->nType, pVar->pz};
  SColumnIndex index = {0};

3919
  if (UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo)) {  // metric query
3920
    if (getColumnIndexByName(&columnName, pQueryInfo, &index) != TSDB_CODE_SUCCESS) {
3921
      return invalidSqlErrMsg(pQueryInfo->msg, msg1);
S
slguan 已提交
3922 3923 3924 3925
    }

    bool orderByTags = false;
    bool orderByTS = false;
H
hzcheng 已提交
3926

S
slguan 已提交
3927 3928
    if (index.columnIndex >= pMeterMetaInfo->pMeterMeta->numOfColumns) {
      int32_t relTagIndex = index.columnIndex - pMeterMetaInfo->pMeterMeta->numOfColumns;
3929
      if (relTagIndex == pQueryInfo->groupbyExpr.columnInfo[0].colIdx) {
H
hzcheng 已提交
3930 3931
        orderByTags = true;
      }
S
slguan 已提交
3932 3933
    } else if (index.columnIndex == TSDB_TBNAME_COLUMN_INDEX) {
      orderByTags = true;
H
hzcheng 已提交
3934 3935
    }

S
slguan 已提交
3936
    if (PRIMARYKEY_TIMESTAMP_COL_INDEX == index.columnIndex) {
H
hzcheng 已提交
3937 3938 3939
      orderByTS = true;
    }

3940 3941
    if (!(orderByTags || orderByTS) && !isTopBottomQuery(pQueryInfo)) {
      return invalidSqlErrMsg(pQueryInfo->msg, msg3);
H
hzcheng 已提交
3942 3943 3944 3945 3946 3947
    } else {
      assert(!(orderByTags && orderByTS));
    }

    if (pSortorder->nExpr == 1) {
      if (orderByTags) {
3948 3949 3950
        pQueryInfo->groupbyExpr.orderIndex = index.columnIndex - pMeterMetaInfo->pMeterMeta->numOfColumns;
        pQueryInfo->groupbyExpr.orderType = pQuerySql->pSortOrder->a[0].sortOrder;
      } else if (isTopBottomQuery(pQueryInfo)) {
S
slguan 已提交
3951
        /* order of top/bottom query in interval is not valid  */
3952
        SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, 0);
S
slguan 已提交
3953 3954
        assert(pExpr->functionId == TSDB_FUNC_TS);

3955
        pExpr = tscSqlExprGet(pQueryInfo, 1);
S
slguan 已提交
3956
        if (pExpr->colInfo.colIdx != index.columnIndex && index.columnIndex != PRIMARYKEY_TIMESTAMP_COL_INDEX) {
3957
          return invalidSqlErrMsg(pQueryInfo->msg, msg2);
S
slguan 已提交
3958
        }
3959

3960 3961
        pQueryInfo->order.order = pQuerySql->pSortOrder->a[0].sortOrder;
        pQueryInfo->order.orderColId = pSchema[index.columnIndex].colId;
S
slguan 已提交
3962
        return TSDB_CODE_SUCCESS;
H
hzcheng 已提交
3963
      } else {
3964 3965
        pQueryInfo->order.order = pSortorder->a[0].sortOrder;
        pQueryInfo->order.orderColId = PRIMARYKEY_TIMESTAMP_COL_INDEX;
H
hzcheng 已提交
3966 3967 3968 3969
      }
    }

    if (pSortorder->nExpr == 2) {
S
slguan 已提交
3970
      if (orderByTags) {
3971 3972
        pQueryInfo->groupbyExpr.orderIndex = index.columnIndex - pMeterMetaInfo->pMeterMeta->numOfColumns;
        pQueryInfo->groupbyExpr.orderType = pQuerySql->pSortOrder->a[0].sortOrder;
S
slguan 已提交
3973
      } else {
3974 3975
        pQueryInfo->order.order = pSortorder->a[0].sortOrder;
        pQueryInfo->order.orderColId = PRIMARYKEY_TIMESTAMP_COL_INDEX;
S
slguan 已提交
3976 3977
      }

H
hzcheng 已提交
3978 3979
      tVariant* pVar2 = &pSortorder->a[1].pVar;
      SSQLToken cname = {pVar2->nLen, pVar2->nType, pVar2->pz};
3980
      if (getColumnIndexByName(&cname, pQueryInfo, &index) != TSDB_CODE_SUCCESS) {
3981
        return invalidSqlErrMsg(pQueryInfo->msg, msg1);
S
slguan 已提交
3982 3983 3984
      }

      if (index.columnIndex != PRIMARYKEY_TIMESTAMP_COL_INDEX) {
3985
        return invalidSqlErrMsg(pQueryInfo->msg, msg2);
H
hzcheng 已提交
3986
      } else {
3987 3988
        pQueryInfo->order.order = pSortorder->a[1].sortOrder;
        pQueryInfo->order.orderColId = PRIMARYKEY_TIMESTAMP_COL_INDEX;
H
hzcheng 已提交
3989 3990 3991 3992
      }
    }

  } else {  // meter query
3993
    if (getColumnIndexByName(&columnName, pQueryInfo, &index) != TSDB_CODE_SUCCESS) {
3994
      return invalidSqlErrMsg(pQueryInfo->msg, msg1);
H
hzcheng 已提交
3995 3996
    }

3997 3998
    if (index.columnIndex != PRIMARYKEY_TIMESTAMP_COL_INDEX && !isTopBottomQuery(pQueryInfo)) {
      return invalidSqlErrMsg(pQueryInfo->msg, msg2);
H
hzcheng 已提交
3999 4000
    }

4001
    if (isTopBottomQuery(pQueryInfo)) {
H
hzcheng 已提交
4002
      /* order of top/bottom query in interval is not valid  */
4003
      SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, 0);
S
slguan 已提交
4004
      assert(pExpr->functionId == TSDB_FUNC_TS);
H
hzcheng 已提交
4005

4006
      pExpr = tscSqlExprGet(pQueryInfo, 1);
S
slguan 已提交
4007
      if (pExpr->colInfo.colIdx != index.columnIndex && index.columnIndex != PRIMARYKEY_TIMESTAMP_COL_INDEX) {
4008
        return invalidSqlErrMsg(pQueryInfo->msg, msg2);
H
hzcheng 已提交
4009
      }
4010

4011 4012
      pQueryInfo->order.order = pQuerySql->pSortOrder->a[0].sortOrder;
      pQueryInfo->order.orderColId = pSchema[index.columnIndex].colId;
H
hzcheng 已提交
4013 4014
      return TSDB_CODE_SUCCESS;
    }
4015

4016
    pQueryInfo->order.order = pQuerySql->pSortOrder->a[0].sortOrder;
H
hzcheng 已提交
4017 4018 4019 4020 4021 4022
  }

  return TSDB_CODE_SUCCESS;
}

int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) {
S
slguan 已提交
4023
  const int32_t DEFAULT_TABLE_INDEX = 0;
4024

4025 4026 4027 4028 4029 4030
  const char* msg1 = "invalid table name";
  const char* msg2 = "table name too long";
  const char* msg3 = "manipulation of tag available for super table";
  const char* msg4 = "set tag value only available for table";
  const char* msg5 = "only support add one tag";
  const char* msg6 = "column can only be modified by super table";
S
slguan 已提交
4031 4032

  SSqlCmd*        pCmd = &pSql->cmd;
H
hzcheng 已提交
4033
  SAlterTableSQL* pAlterSQL = pInfo->pAlterInfo;
4034 4035
  SQueryInfo*     pQueryInfo = tscGetQueryInfoDetail(pCmd, 0);

4036
  SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, DEFAULT_TABLE_INDEX);
H
hzcheng 已提交
4037

4038
  if (tscValidateName(&(pAlterSQL->name)) != TSDB_CODE_SUCCESS) {
4039
    return invalidSqlErrMsg(pQueryInfo->msg, msg1);
4040
  }
P
plum-lihui 已提交
4041

4042
  if (setMeterID(pMeterMetaInfo, &(pAlterSQL->name), pSql) != TSDB_CODE_SUCCESS) {
4043
    return invalidSqlErrMsg(pQueryInfo->msg, msg2);
H
hzcheng 已提交
4044 4045
  }

4046
  int32_t ret = tscGetMeterMeta(pSql, pMeterMetaInfo);
H
hzcheng 已提交
4047 4048 4049 4050
  if (ret != TSDB_CODE_SUCCESS) {
    return ret;
  }

S
slguan 已提交
4051
  SMeterMeta* pMeterMeta = pMeterMetaInfo->pMeterMeta;
H
hzcheng 已提交
4052

4053 4054
  if (pAlterSQL->type == TSDB_ALTER_TABLE_ADD_TAG_COLUMN || pAlterSQL->type == TSDB_ALTER_TABLE_DROP_TAG_COLUMN ||
      pAlterSQL->type == TSDB_ALTER_TABLE_CHANGE_TAG_COLUMN) {
S
slguan 已提交
4055
    if (UTIL_METER_IS_NOMRAL_METER(pMeterMetaInfo)) {
4056
      return invalidSqlErrMsg(pQueryInfo->msg, msg3);
H
hzcheng 已提交
4057
    }
4058 4059
  } else if ((pAlterSQL->type == TSDB_ALTER_TABLE_UPDATE_TAG_VAL) && (UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo))) {
    return invalidSqlErrMsg(pQueryInfo->msg, msg4);
4060
  } else if ((pAlterSQL->type == TSDB_ALTER_TABLE_ADD_COLUMN || pAlterSQL->type == TSDB_ALTER_TABLE_DROP_COLUMN) &&
S
slguan 已提交
4061
             UTIL_METER_IS_CREATE_FROM_METRIC(pMeterMetaInfo)) {
4062
    return invalidSqlErrMsg(pQueryInfo->msg, msg6);
H
hzcheng 已提交
4063 4064
  }

4065
  if (pAlterSQL->type == TSDB_ALTER_TABLE_ADD_TAG_COLUMN) {
H
hzcheng 已提交
4066 4067
    tFieldList* pFieldList = pAlterSQL->pAddColumns;
    if (pFieldList->nField > 1) {
4068
      return invalidSqlErrMsg(pQueryInfo->msg, msg5);
H
hzcheng 已提交
4069 4070 4071 4072 4073 4074
    }

    if (!validateOneTags(pCmd, &pFieldList->p[0])) {
      return TSDB_CODE_INVALID_SQL;
    }

4075
    tscFieldInfoSetValFromField(&pQueryInfo->fieldsInfo, 0, &pFieldList->p[0]);
4076
  } else if (pAlterSQL->type == TSDB_ALTER_TABLE_DROP_TAG_COLUMN) {
4077 4078 4079 4080 4081
    const char* msg1 = "no tags can be dropped";
    const char* msg2 = "only support one tag";
    const char* msg3 = "tag name too long";
    const char* msg4 = "illegal tag name";
    const char* msg5 = "primary tag cannot be dropped";
H
hzcheng 已提交
4082 4083

    if (pMeterMeta->numOfTags == 1) {
4084
      return invalidSqlErrMsg(pQueryInfo->msg, msg1);
H
hzcheng 已提交
4085 4086 4087 4088
    }

    // numOfTags == 1
    if (pAlterSQL->varList->nExpr > 1) {
4089
      return invalidSqlErrMsg(pQueryInfo->msg, msg2);
H
hzcheng 已提交
4090 4091 4092 4093
    }

    tVariantListItem* pItem = &pAlterSQL->varList->a[0];
    if (pItem->pVar.nLen > TSDB_COL_NAME_LEN) {
4094
      return invalidSqlErrMsg(pQueryInfo->msg, msg3);
H
hzcheng 已提交
4095
    }
4096

4097
    SColumnIndex index = COLUMN_INDEX_INITIALIZER;
4098 4099
    SSQLToken    name = {.z = pItem->pVar.pz, .n = pItem->pVar.nLen, .type = TK_STRING};

4100
    if (getColumnIndexByName(&name, pQueryInfo, &index) != TSDB_CODE_SUCCESS) {
4101
      return TSDB_CODE_INVALID_SQL;
H
hzcheng 已提交
4102 4103
    }

4104
    if (index.columnIndex < pMeterMeta->numOfColumns) {
4105
      return invalidSqlErrMsg(pQueryInfo->msg, msg4);
4106
    } else if (index.columnIndex == 0) {
4107
      return invalidSqlErrMsg(pQueryInfo->msg, msg5);
H
hzcheng 已提交
4108 4109
    }

4110 4111
    char name1[128] = {0};
    strncpy(name1, pItem->pVar.pz, pItem->pVar.nLen);
4112 4113
    tscFieldInfoSetValue(&pQueryInfo->fieldsInfo, 0, TSDB_DATA_TYPE_INT, name1,
                         tDataTypeDesc[TSDB_DATA_TYPE_INT].nSize);
4114
  } else if (pAlterSQL->type == TSDB_ALTER_TABLE_CHANGE_TAG_COLUMN) {
4115 4116
    const char* msg1 = "tag name too long";
    const char* msg2 = "invalid tag name";
4117

H
hzcheng 已提交
4118 4119 4120 4121 4122
    tVariantList* pVarList = pAlterSQL->varList;
    if (pVarList->nExpr > 2) {
      return TSDB_CODE_INVALID_SQL;
    }

4123 4124 4125 4126
    tVariantListItem* pSrcItem = &pAlterSQL->varList->a[0];
    tVariantListItem* pDstItem = &pAlterSQL->varList->a[1];

    if (pSrcItem->pVar.nLen >= TSDB_COL_NAME_LEN || pDstItem->pVar.nLen >= TSDB_COL_NAME_LEN) {
4127
      return invalidSqlErrMsg(pQueryInfo->msg, msg1);
H
hzcheng 已提交
4128 4129
    }

4130
    if (pSrcItem->pVar.nType != TSDB_DATA_TYPE_BINARY || pDstItem->pVar.nType != TSDB_DATA_TYPE_BINARY) {
4131
      return invalidSqlErrMsg(pQueryInfo->msg, msg2);
4132
    }
H
hzcheng 已提交
4133

S
slguan 已提交
4134 4135
    SColumnIndex srcIndex = COLUMN_INDEX_INITIALIZER;
    SColumnIndex destIndex = COLUMN_INDEX_INITIALIZER;
H
hzcheng 已提交
4136

S
slguan 已提交
4137
    SSQLToken srcToken = {.z = pSrcItem->pVar.pz, .n = pSrcItem->pVar.nLen, .type = TK_STRING};
4138
    if (getColumnIndexByName(&srcToken, pQueryInfo, &srcIndex) != TSDB_CODE_SUCCESS) {
S
slguan 已提交
4139
      return TSDB_CODE_INVALID_SQL;
H
hzcheng 已提交
4140 4141
    }

S
slguan 已提交
4142
    SSQLToken destToken = {.z = pDstItem->pVar.pz, .n = pDstItem->pVar.nLen, .type = TK_STRING};
4143
    if (getColumnIndexByName(&destToken, pQueryInfo, &destIndex) == TSDB_CODE_SUCCESS) {
H
hzcheng 已提交
4144 4145 4146 4147 4148
      return TSDB_CODE_INVALID_SQL;
    }

    char name[128] = {0};
    strncpy(name, pVarList->a[0].pVar.pz, pVarList->a[0].pVar.nLen);
4149
    tscFieldInfoSetValue(&pQueryInfo->fieldsInfo, 0, TSDB_DATA_TYPE_INT, name, tDataTypeDesc[TSDB_DATA_TYPE_INT].nSize);
H
hzcheng 已提交
4150 4151 4152

    memset(name, 0, tListLen(name));
    strncpy(name, pVarList->a[1].pVar.pz, pVarList->a[1].pVar.nLen);
4153
    tscFieldInfoSetValue(&pQueryInfo->fieldsInfo, 1, TSDB_DATA_TYPE_INT, name, tDataTypeDesc[TSDB_DATA_TYPE_INT].nSize);
4154
  } else if (pAlterSQL->type == TSDB_ALTER_TABLE_UPDATE_TAG_VAL) {
S
slguan 已提交
4155
    const char* msg1 = "invalid tag value";
4156
    const char* msg2 = "update normal column not supported";
S
slguan 已提交
4157 4158
    const char* msg3 = "tag value too long";

4159 4160
    // Note: update can only be applied to table not super table.
    // the following is handle display tags value for meters created according to super table
H
hzcheng 已提交
4161 4162 4163
    tVariantList* pVarList = pAlterSQL->varList;
    tVariant*     pTagName = &pVarList->a[0].pVar;

4164 4165
    SColumnIndex columnIndex = COLUMN_INDEX_INITIALIZER;
    SSQLToken    name = {.type = TK_STRING, .z = pTagName->pz, .n = pTagName->nLen};
4166
    if (getColumnIndexByName(&name, pQueryInfo, &columnIndex) != TSDB_CODE_SUCCESS) {
4167
      return TSDB_CODE_INVALID_SQL;
H
hzcheng 已提交
4168 4169
    }

4170
    if (columnIndex.columnIndex < pMeterMeta->numOfColumns) {
4171
      return invalidSqlErrMsg(pQueryInfo->msg, msg2);
S
slguan 已提交
4172 4173
    }

4174
    SSchema* pTagsSchema = tsGetColumnSchema(pMeterMetaInfo->pMeterMeta, columnIndex.columnIndex);
4175 4176 4177
    if (tVariantDump(&pVarList->a[1].pVar, pAlterSQL->tagData.data /*pCmd->payload*/, pTagsSchema->type) !=
        TSDB_CODE_SUCCESS) {
      return invalidSqlErrMsg(pQueryInfo->msg, msg1);
H
hzcheng 已提交
4178 4179 4180
    }

    // validate the length of binary
4181 4182
    if ((pTagsSchema->type == TSDB_DATA_TYPE_BINARY || pTagsSchema->type == TSDB_DATA_TYPE_NCHAR) &&
        pVarList->a[1].pVar.nLen > pTagsSchema->bytes) {
4183
      return invalidSqlErrMsg(pQueryInfo->msg, msg3);
H
hzcheng 已提交
4184 4185
    }

4186 4187
    char name1[128] = {0};
    strncpy(name1, pTagName->pz, pTagName->nLen);
4188 4189
    tscFieldInfoSetValue(&pQueryInfo->fieldsInfo, 0, TSDB_DATA_TYPE_INT, name1,
                         tDataTypeDesc[TSDB_DATA_TYPE_INT].nSize);
H
hzcheng 已提交
4190

4191
  } else if (pAlterSQL->type == TSDB_ALTER_TABLE_ADD_COLUMN) {
H
hzcheng 已提交
4192 4193
    tFieldList* pFieldList = pAlterSQL->pAddColumns;
    if (pFieldList->nField > 1) {
4194
      const char* msg = "only support add one column";
4195
      return invalidSqlErrMsg(pQueryInfo->msg, msg);
H
hzcheng 已提交
4196 4197 4198 4199 4200 4201
    }

    if (!validateOneColumn(pCmd, &pFieldList->p[0])) {
      return TSDB_CODE_INVALID_SQL;
    }

4202
    tscFieldInfoSetValFromField(&pQueryInfo->fieldsInfo, 0, &pFieldList->p[0]);
4203
  } else if (pAlterSQL->type == TSDB_ALTER_TABLE_DROP_COLUMN) {
4204 4205 4206
    const char* msg1 = "no columns can be dropped";
    const char* msg2 = "only support one column";
    const char* msg4 = "illegal column name";
4207
    const char* msg3 = "primary timestamp column cannot be dropped";
H
hzcheng 已提交
4208 4209

    if (pMeterMeta->numOfColumns == TSDB_MIN_COLUMNS) {  //
4210
      return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1);
H
hzcheng 已提交
4211 4212 4213
    }

    if (pAlterSQL->varList->nExpr > 1) {
4214
      return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2);
H
hzcheng 已提交
4215 4216 4217 4218
    }

    tVariantListItem* pItem = &pAlterSQL->varList->a[0];

4219 4220
    SColumnIndex columnIndex = COLUMN_INDEX_INITIALIZER;
    SSQLToken    name = {.type = TK_STRING, .z = pItem->pVar.pz, .n = pItem->pVar.nLen};
4221
    if (getColumnIndexByName(&name, pQueryInfo, &columnIndex) != TSDB_CODE_SUCCESS) {
4222
      return invalidSqlErrMsg(pQueryInfo->msg, msg4);
H
hzcheng 已提交
4223 4224
    }

4225
    if (columnIndex.columnIndex == PRIMARYKEY_TIMESTAMP_COL_INDEX) {
4226
      return invalidSqlErrMsg(pQueryInfo->msg, msg3);
4227
    }
H
hzcheng 已提交
4228

4229 4230
    char name1[128] = {0};
    strncpy(name1, pItem->pVar.pz, pItem->pVar.nLen);
4231 4232
    tscFieldInfoSetValue(&pQueryInfo->fieldsInfo, 0, TSDB_DATA_TYPE_INT, name1,
                         tDataTypeDesc[TSDB_DATA_TYPE_INT].nSize);
H
hzcheng 已提交
4233 4234 4235 4236 4237
  }

  return TSDB_CODE_SUCCESS;
}

4238
int32_t validateSqlFunctionInStreamSql(SQueryInfo* pQueryInfo) {
4239 4240
  const char* msg0 = "sample interval can not be less than 10ms.";
  const char* msg1 = "functions not allowed in select clause";
H
hzcheng 已提交
4241

4242 4243
  if (pQueryInfo->nAggTimeInterval != 0 && pQueryInfo->nAggTimeInterval < 10) {
    return invalidSqlErrMsg(pQueryInfo->msg, msg0);
H
hzcheng 已提交
4244 4245
  }

4246 4247
  for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) {
    int32_t functId = tscSqlExprGet(pQueryInfo, i)->functionId;
H
hzcheng 已提交
4248
    if (!IS_STREAM_QUERY_VALID(aAggs[functId].nStatus)) {
4249
      return invalidSqlErrMsg(pQueryInfo->msg, msg1);
H
hzcheng 已提交
4250 4251 4252 4253 4254 4255
    }
  }

  return TSDB_CODE_SUCCESS;
}

4256
int32_t validateFunctionsInIntervalOrGroupbyQuery(SQueryInfo* pQueryInfo) {
S
slguan 已提交
4257
  bool        isProjectionFunction = false;
4258
  const char* msg1 = "column projection is not compatible with interval";
H
hjxilinx 已提交
4259

H
hzcheng 已提交
4260
  // multi-output set/ todo refactor
4261 4262
  for (int32_t k = 0; k < pQueryInfo->fieldsInfo.numOfOutputCols; ++k) {
    SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, k);
H
hjxilinx 已提交
4263

4264 4265 4266
    // projection query on primary timestamp, the selectivity function needs to be present.
    if (pExpr->functionId == TSDB_FUNC_PRJ && pExpr->colInfo.colId == PRIMARYKEY_TIMESTAMP_COL_INDEX) {
      bool hasSelectivity = false;
4267 4268
      for (int32_t j = 0; j < pQueryInfo->fieldsInfo.numOfOutputCols; ++j) {
        SSqlExpr* pEx = tscSqlExprGet(pQueryInfo, j);
4269 4270 4271 4272 4273
        if ((aAggs[pEx->functionId].nStatus & TSDB_FUNCSTATE_SELECTIVITY) == TSDB_FUNCSTATE_SELECTIVITY) {
          hasSelectivity = true;
          break;
        }
      }
H
hjxilinx 已提交
4274

4275 4276 4277 4278
      if (hasSelectivity) {
        continue;
      }
    }
H
hjxilinx 已提交
4279

S
slguan 已提交
4280
    if (pExpr->functionId == TSDB_FUNC_PRJ || pExpr->functionId == TSDB_FUNC_DIFF ||
H
hjxilinx 已提交
4281
        pExpr->functionId == TSDB_FUNC_ARITHM) {
H
hzcheng 已提交
4282 4283 4284
      isProjectionFunction = true;
    }
  }
S
slguan 已提交
4285 4286

  if (isProjectionFunction) {
4287
    invalidSqlErrMsg(pQueryInfo->msg, msg1);
H
hzcheng 已提交
4288 4289 4290 4291 4292 4293
  }

  return isProjectionFunction == true ? TSDB_CODE_INVALID_SQL : TSDB_CODE_SUCCESS;
}

typedef struct SDNodeDynConfOption {
H
hjxilinx 已提交
4294 4295
  char*   name;  // command name
  int32_t len;   // name string length
H
hzcheng 已提交
4296 4297 4298 4299 4300 4301 4302
} SDNodeDynConfOption;

int32_t validateDNodeConfig(tDCLSQL* pOptions) {
  if (pOptions->nTokens < 2 || pOptions->nTokens > 3) {
    return TSDB_CODE_INVALID_SQL;
  }

H
hjxilinx 已提交
4303
  const SDNodeDynConfOption DNODE_DYNAMIC_CFG_OPTIONS[14] = {
S
slguan 已提交
4304
      {"resetLog", 8},      {"resetQueryCache", 15}, {"dDebugFlag", 10},       {"rpcDebugFlag", 12},
H
hzcheng 已提交
4305 4306
      {"tmrDebugFlag", 12}, {"cDebugFlag", 10},      {"uDebugFlag", 10},       {"mDebugFlag", 10},
      {"sdbDebugFlag", 12}, {"httpDebugFlag", 13},   {"monitorDebugFlag", 16}, {"qDebugflag", 10},
S
slguan 已提交
4307
      {"debugFlag", 9},     {"monitor", 7}};
H
hzcheng 已提交
4308 4309 4310 4311 4312 4313

  SSQLToken* pOptionToken = &pOptions->a[1];

  if (pOptions->nTokens == 2) {
    // reset log and reset query cache does not need value
    for (int32_t i = 0; i < 2; ++i) {
H
hjxilinx 已提交
4314
      const SDNodeDynConfOption* pOption = &DNODE_DYNAMIC_CFG_OPTIONS[i];
H
hzcheng 已提交
4315 4316 4317 4318
      if ((strncasecmp(pOption->name, pOptionToken->z, pOptionToken->n) == 0) && (pOption->len == pOptionToken->n)) {
        return TSDB_CODE_SUCCESS;
      }
    }
S
slguan 已提交
4319 4320 4321 4322 4323 4324 4325 4326
  } else if ((strncasecmp(DNODE_DYNAMIC_CFG_OPTIONS[13].name, pOptionToken->z, pOptionToken->n) == 0) &&
             (DNODE_DYNAMIC_CFG_OPTIONS[13].len == pOptionToken->n)) {
    SSQLToken* pValToken = &pOptions->a[2];
    int32_t    val = strtol(pValToken->z, NULL, 10);
    if (val != 0 && val != 1) {
      return TSDB_CODE_INVALID_SQL;  // options value is invalid
    }
    return TSDB_CODE_SUCCESS;
H
hzcheng 已提交
4327 4328 4329 4330 4331 4332 4333 4334 4335
  } else {
    SSQLToken* pValToken = &pOptions->a[2];

    int32_t val = strtol(pValToken->z, NULL, 10);
    if (val < 131 || val > 199) {
      /* options value is out of valid range */
      return TSDB_CODE_INVALID_SQL;
    }

S
slguan 已提交
4336
    for (int32_t i = 2; i < tListLen(DNODE_DYNAMIC_CFG_OPTIONS) - 1; ++i) {
H
hjxilinx 已提交
4337
      const SDNodeDynConfOption* pOption = &DNODE_DYNAMIC_CFG_OPTIONS[i];
H
hzcheng 已提交
4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348

      if ((strncasecmp(pOption->name, pOptionToken->z, pOptionToken->n) == 0) && (pOption->len == pOptionToken->n)) {
        /* options is valid */
        return TSDB_CODE_SUCCESS;
      }
    }
  }

  return TSDB_CODE_INVALID_SQL;
}

S
slguan 已提交
4349 4350 4351 4352 4353
int32_t validateLocalConfig(tDCLSQL* pOptions) {
  if (pOptions->nTokens < 1 || pOptions->nTokens > 2) {
    return TSDB_CODE_INVALID_SQL;
  }

H
hjxilinx 已提交
4354 4355
  SDNodeDynConfOption LOCAL_DYNAMIC_CFG_OPTIONS[6] = {{"resetLog", 8},    {"rpcDebugFlag", 12}, {"tmrDebugFlag", 12},
                                                      {"cDebugFlag", 10}, {"uDebugFlag", 10},   {"debugFlag", 9}};
S
slguan 已提交
4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386

  SSQLToken* pOptionToken = &pOptions->a[0];

  if (pOptions->nTokens == 1) {
    // reset log does not need value
    for (int32_t i = 0; i < 1; ++i) {
      SDNodeDynConfOption* pOption = &LOCAL_DYNAMIC_CFG_OPTIONS[i];
      if ((strncasecmp(pOption->name, pOptionToken->z, pOptionToken->n) == 0) && (pOption->len == pOptionToken->n)) {
        return TSDB_CODE_SUCCESS;
      }
    }
  } else {
    SSQLToken* pValToken = &pOptions->a[1];

    int32_t val = strtol(pValToken->z, NULL, 10);
    if (val < 131 || val > 199) {
      // options value is out of valid range
      return TSDB_CODE_INVALID_SQL;
    }

    for (int32_t i = 1; i < tListLen(LOCAL_DYNAMIC_CFG_OPTIONS); ++i) {
      SDNodeDynConfOption* pOption = &LOCAL_DYNAMIC_CFG_OPTIONS[i];
      if ((strncasecmp(pOption->name, pOptionToken->z, pOptionToken->n) == 0) && (pOption->len == pOptionToken->n)) {
        // options is valid
        return TSDB_CODE_SUCCESS;
      }
    }
  }
  return TSDB_CODE_INVALID_SQL;
}

H
hzcheng 已提交
4387 4388 4389 4390 4391 4392
int32_t validateColumnName(char* name) {
  bool ret = isKeyWord(name, strlen(name));
  if (ret) {
    return TSDB_CODE_INVALID_SQL;
  }

S
slguan 已提交
4393
  SSQLToken token = {.z = name};
H
hzcheng 已提交
4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419
  token.n = tSQLGetToken(name, &token.type);

  if (token.type != TK_STRING && token.type != TK_ID) {
    return TSDB_CODE_INVALID_SQL;
  }

  if (token.type == TK_STRING) {
    strdequote(token.z);
    strtrim(token.z);
    token.n = (uint32_t)strlen(token.z);

    int32_t k = tSQLGetToken(token.z, &token.type);
    if (k != token.n) {
      return TSDB_CODE_INVALID_SQL;
    }

    return validateColumnName(token.z);
  } else {
    if (isNumber(&token)) {
      return TSDB_CODE_INVALID_SQL;
    }
  }

  return TSDB_CODE_SUCCESS;
}

4420 4421
bool hasTimestampForPointInterpQuery(SQueryInfo* pQueryInfo) {
  if (!tscIsPointInterpQuery(pQueryInfo)) {
H
hzcheng 已提交
4422 4423 4424
    return true;
  }

4425
  return (pQueryInfo->stime == pQueryInfo->etime) && (pQueryInfo->stime != 0);
H
hzcheng 已提交
4426 4427
}

4428
int32_t parseLimitClause(SQueryInfo* pQueryInfo, int32_t clauseIndex, SQuerySQL* pQuerySql, SSqlObj* pSql) {
4429
  SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0);
H
hzcheng 已提交
4430

S
slguan 已提交
4431 4432 4433 4434
  const char* msg0 = "soffset/offset can not be less than 0";
  const char* msg1 = "slimit/soffset only available for STable query";
  const char* msg2 = "function not supported on table";
  const char* msg3 = "slimit/soffset can not apply to projection query";
4435
  const char* msg4 = "projection on super table requires order by clause along with limitation";
H
hjxilinx 已提交
4436 4437
  const char* msg5 = "ordered projection result too large";
  
H
hzcheng 已提交
4438
  // handle the limit offset value, validate the limit
4439
  pQueryInfo->limit = pQuerySql->limit;
4440
  pQueryInfo->clauseLimit = pQueryInfo->limit.limit;
H
hjxilinx 已提交
4441

4442
  pQueryInfo->slimit = pQuerySql->slimit;
H
hzcheng 已提交
4443

4444
  if (pQueryInfo->slimit.offset < 0 || pQueryInfo->limit.offset < 0) {
4445
    return invalidSqlErrMsg(pQueryInfo->msg, msg0);
S
slguan 已提交
4446 4447
  }

4448
  if (pQueryInfo->limit.limit == 0) {
S
slguan 已提交
4449
    tscTrace("%p limit 0, no output result", pSql);
4450
    pQueryInfo->command = TSDB_SQL_RETRIEVE_EMPTY_RESULT;
H
hjxilinx 已提交
4451
    return TSDB_CODE_SUCCESS;
S
slguan 已提交
4452 4453
  }

4454
  if (UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo)) {
H
hjxilinx 已提交
4455
    bool queryOnTags = false;
4456
    if (tscQueryOnlyMetricTags(pQueryInfo, &queryOnTags) != TSDB_CODE_SUCCESS) {
H
hzcheng 已提交
4457 4458 4459 4460
      return TSDB_CODE_INVALID_SQL;
    }

    if (queryOnTags == true) {  // local handle the metric tag query
4461
      pQueryInfo->command = TSDB_SQL_RETRIEVE_TAGS;
S
slguan 已提交
4462
    } else {
L
lihui 已提交
4463
      if (tscProjectionQueryOnSTable(pQueryInfo, 0)) {
4464
        if (pQueryInfo->order.orderColId >= 0) {
H
hjxilinx 已提交
4465 4466
          if (pQueryInfo->limit.limit == -1) {
            return invalidSqlErrMsg(pQueryInfo->msg, msg4);
4467
          } else if (pQueryInfo->limit.limit > 10000) { // the result set can not be larger than 10000
H
hjxilinx 已提交
4468 4469 4470 4471 4472 4473 4474 4475
            //todo use global config parameter
            return invalidSqlErrMsg(pQueryInfo->msg, msg5);
          }
        }
        
        if (pQueryInfo->slimit.limit > 0 || pQueryInfo->slimit.offset > 0) {
          return invalidSqlErrMsg(pQueryInfo->msg, msg3);
        }
L
lihui 已提交
4476
        pQueryInfo->type |= TSDB_QUERY_TYPE_SUBQUERY; // for projection query on super table, all queries are subqueries
S
slguan 已提交
4477
      }
H
hzcheng 已提交
4478 4479
    }

4480
    if (pQueryInfo->slimit.limit == 0) {
H
hzcheng 已提交
4481
      tscTrace("%p limit 0, no output result", pSql);
4482
      pQueryInfo->command = TSDB_SQL_RETRIEVE_EMPTY_RESULT;
H
hzcheng 已提交
4483 4484 4485 4486
      return TSDB_CODE_SUCCESS;
    }

    /*
4487 4488 4489 4490
     * Get the distribution of all tables among all available virtual nodes that are qualified for the query condition
     * and created according to this super table from management node.
     * And then launching multiple async-queries against all qualified virtual nodes, during the first-stage
     * query operation.
H
hzcheng 已提交
4491
     */
4492
    int32_t code = tscGetMetricMeta(pSql, clauseIndex);
H
hzcheng 已提交
4493 4494 4495 4496
    if (code != TSDB_CODE_SUCCESS) {
      return code;
    }

S
slguan 已提交
4497 4498 4499
    // No tables included. No results generated. Query results are empty.
    SMetricMeta* pMetricMeta = pMeterMetaInfo->pMetricMeta;
    if (pMeterMetaInfo->pMeterMeta == NULL || pMetricMeta == NULL || pMetricMeta->numOfMeters == 0) {
H
hzcheng 已提交
4500
      tscTrace("%p no table in metricmeta, no output result", pSql);
4501
      pQueryInfo->command = TSDB_SQL_RETRIEVE_EMPTY_RESULT;
H
hzcheng 已提交
4502 4503 4504
    }

    // keep original limitation value in globalLimit
4505
    pQueryInfo->clauseLimit = pQueryInfo->limit.limit;
H
hzcheng 已提交
4506
  } else {
4507 4508
    if (pQueryInfo->slimit.limit != -1 || pQueryInfo->slimit.offset != 0) {
      return invalidSqlErrMsg(pQueryInfo->msg, msg1);
H
hzcheng 已提交
4509 4510
    }

S
slguan 已提交
4511
    // filter the query functions operating on "tbname" column that are not supported by normal columns.
4512 4513
    for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) {
      SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i);
S
slguan 已提交
4514
      if (pExpr->colInfo.colIdx == TSDB_TBNAME_COLUMN_INDEX) {
4515
        return invalidSqlErrMsg(pQueryInfo->msg, msg2);
H
hzcheng 已提交
4516 4517 4518 4519 4520 4521
      }
    }
  }

  return TSDB_CODE_SUCCESS;
}
4522

H
hjxilinx 已提交
4523 4524
static int32_t setKeepOption(SSqlCmd* pCmd, SCreateDbMsg* pMsg, SCreateDBInfo* pCreateDb) {
  const char* msg = "invalid number of options";
H
hjxilinx 已提交
4525

4526 4527 4528
  pMsg->daysToKeep = htonl(-1);
  pMsg->daysToKeep1 = htonl(-1);
  pMsg->daysToKeep2 = htonl(-1);
H
hjxilinx 已提交
4529

H
hjxilinx 已提交
4530 4531 4532
  tVariantList* pKeep = pCreateDb->keep;
  if (pKeep != NULL) {
    switch (pKeep->nExpr) {
S
slguan 已提交
4533
      case 1:
H
hjxilinx 已提交
4534
        pMsg->daysToKeep = htonl(pKeep->a[0].pVar.i64Key);
4535 4536
        break;
      case 2: {
H
hjxilinx 已提交
4537 4538
        pMsg->daysToKeep = htonl(pKeep->a[0].pVar.i64Key);
        pMsg->daysToKeep1 = htonl(pKeep->a[1].pVar.i64Key);
4539 4540 4541
        break;
      }
      case 3: {
H
hjxilinx 已提交
4542 4543 4544
        pMsg->daysToKeep = htonl(pKeep->a[0].pVar.i64Key);
        pMsg->daysToKeep1 = htonl(pKeep->a[1].pVar.i64Key);
        pMsg->daysToKeep2 = htonl(pKeep->a[2].pVar.i64Key);
4545 4546
        break;
      }
4547
      default: { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg); }
4548 4549
    }
  }
H
hjxilinx 已提交
4550

H
hjxilinx 已提交
4551 4552
  return TSDB_CODE_SUCCESS;
}
4553

H
hjxilinx 已提交
4554 4555
static int32_t setTimePrecisionOption(SSqlCmd* pCmd, SCreateDbMsg* pMsg, SCreateDBInfo* pCreateDbInfo) {
  const char* msg = "invalid time precision";
H
hjxilinx 已提交
4556

H
hjxilinx 已提交
4557
  pMsg->precision = TSDB_TIME_PRECISION_MILLI;  // millisecond by default
H
hjxilinx 已提交
4558

H
hjxilinx 已提交
4559
  SSQLToken* pToken = &pCreateDbInfo->precision;
4560 4561
  if (pToken->n > 0) {
    pToken->n = strdequote(pToken->z);
H
hjxilinx 已提交
4562

4563 4564 4565 4566 4567
    if (strncmp(pToken->z, TSDB_TIME_PRECISION_MILLI_STR, pToken->n) == 0 &&
        strlen(TSDB_TIME_PRECISION_MILLI_STR) == pToken->n) {
      // time precision for this db: million second
      pMsg->precision = TSDB_TIME_PRECISION_MILLI;
    } else if (strncmp(pToken->z, TSDB_TIME_PRECISION_MICRO_STR, pToken->n) == 0 &&
H
hjxilinx 已提交
4568
               strlen(TSDB_TIME_PRECISION_MICRO_STR) == pToken->n) {
4569 4570
      pMsg->precision = TSDB_TIME_PRECISION_MICRO;
    } else {
4571
      return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg);
4572 4573
    }
  }
H
hjxilinx 已提交
4574

H
hjxilinx 已提交
4575 4576
  return TSDB_CODE_SUCCESS;
}
4577

H
hjxilinx 已提交
4578 4579 4580 4581
static void setCreateDBOption(SCreateDbMsg* pMsg, SCreateDBInfo* pCreateDb) {
  pMsg->blocksPerMeter = htons(pCreateDb->numOfBlocksPerTable);
  pMsg->compression = pCreateDb->compressionLevel;

H
hjxilinx 已提交
4582
  pMsg->commitLog = (char)pCreateDb->commitLog;
H
hjxilinx 已提交
4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594
  pMsg->commitTime = htonl(pCreateDb->commitTime);
  pMsg->maxSessions = htonl(pCreateDb->tablesPerVnode);
  pMsg->cacheNumOfBlocks.fraction = pCreateDb->numOfAvgCacheBlocks;
  pMsg->cacheBlockSize = htonl(pCreateDb->cacheBlockSize);
  pMsg->rowsInFileBlock = htonl(pCreateDb->rowPerFileBlock);
  pMsg->daysPerFile = htonl(pCreateDb->daysPerFile);
  pMsg->replications = pCreateDb->replica;
}

int32_t parseCreateDBOptions(SSqlCmd* pCmd, SCreateDBInfo* pCreateDbSql) {
  SCreateDbMsg* pMsg = (SCreateDbMsg*)(pCmd->payload + tsRpcHeadSize + sizeof(SMgmtHead));
  setCreateDBOption(pMsg, pCreateDbSql);
H
hjxilinx 已提交
4595

H
hjxilinx 已提交
4596 4597 4598
  if (setKeepOption(pCmd, pMsg, pCreateDbSql) != TSDB_CODE_SUCCESS) {
    return TSDB_CODE_INVALID_SQL;
  }
H
hjxilinx 已提交
4599

H
hjxilinx 已提交
4600 4601 4602
  if (setTimePrecisionOption(pCmd, pMsg, pCreateDbSql) != TSDB_CODE_SUCCESS) {
    return TSDB_CODE_INVALID_SQL;
  }
H
hjxilinx 已提交
4603

H
hjxilinx 已提交
4604 4605 4606
  if (tscCheckCreateDbParams(pCmd, pMsg) != TSDB_CODE_SUCCESS) {
    return TSDB_CODE_INVALID_SQL;
  }
H
hjxilinx 已提交
4607

4608
  return TSDB_CODE_SUCCESS;
H
huili 已提交
4609
}
S
slguan 已提交
4610

4611
void tscAddTimestampColumn(SQueryInfo* pQueryInfo, int16_t functionId, int16_t tableIndex) {
S
slguan 已提交
4612 4613
  // the first column not timestamp column, add it
  SSqlExpr* pExpr = NULL;
4614 4615
  if (pQueryInfo->exprsInfo.numOfExprs > 0) {
    pExpr = tscSqlExprGet(pQueryInfo, 0);
S
slguan 已提交
4616 4617 4618 4619 4620
  }

  if (pExpr == NULL || pExpr->colInfo.colId != PRIMARYKEY_TIMESTAMP_COL_INDEX || pExpr->functionId != functionId) {
    SColumnIndex index = {tableIndex, PRIMARYKEY_TIMESTAMP_COL_INDEX};

4621
    pExpr = tscSqlExprInsert(pQueryInfo, 0, functionId, &index, TSDB_DATA_TYPE_TIMESTAMP, TSDB_KEYSIZE, TSDB_KEYSIZE);
S
slguan 已提交
4622 4623 4624 4625 4626
    pExpr->colInfo.flag = TSDB_COL_NORMAL;

    // NOTE: tag column does not add to source column list
    SColumnList ids = getColumnList(1, tableIndex, PRIMARYKEY_TIMESTAMP_COL_INDEX);

4627
    insertResultField(pQueryInfo, 0, &ids, TSDB_KEYSIZE, TSDB_DATA_TYPE_TIMESTAMP, "ts");
S
slguan 已提交
4628 4629 4630
  }
}

4631 4632
void addGroupInfoForSubquery(SSqlObj* pParentObj, SSqlObj* pSql, int32_t subClauseIndex, int32_t tableIndex) {
  SQueryInfo* pParentQueryInfo = tscGetQueryInfoDetail(&pParentObj->cmd, subClauseIndex);
4633

4634 4635
  if (pParentQueryInfo->groupbyExpr.numOfGroupCols > 0) {
    SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, subClauseIndex);
4636 4637
    int32_t     num = pQueryInfo->exprsInfo.numOfExprs;

4638
    SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, num - 1);
4639

S
slguan 已提交
4640
    if (pExpr->functionId != TSDB_FUNC_TAG) {
4641
      SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, tableIndex);
4642 4643 4644
      int16_t         columnInfo = tscGetJoinTagColIndexByUid(&pQueryInfo->tagCond, pMeterMetaInfo->pMeterMeta->uid);
      SColumnIndex    index = {.tableIndex = 0, .columnIndex = columnInfo};
      SSchema*        pSchema = tsGetTagSchema(pMeterMetaInfo->pMeterMeta);
S
slguan 已提交
4645 4646 4647 4648 4649

      int16_t type = pSchema[index.columnIndex].type;
      int16_t bytes = pSchema[index.columnIndex].bytes;
      char*   name = pSchema[index.columnIndex].name;

4650 4651
      pExpr = tscSqlExprInsert(pQueryInfo, pQueryInfo->fieldsInfo.numOfOutputCols, TSDB_FUNC_TAG, &index, type, bytes,
                               bytes);
S
slguan 已提交
4652 4653 4654 4655
      pExpr->colInfo.flag = TSDB_COL_TAG;

      // NOTE: tag column does not add to source column list
      SColumnList ids = {0};
4656
      insertResultField(pQueryInfo, pQueryInfo->fieldsInfo.numOfOutputCols, &ids, bytes, type, name);
S
slguan 已提交
4657 4658 4659 4660

      int32_t relIndex = index.columnIndex;

      pExpr->colInfo.colIdx = relIndex;
4661
      pQueryInfo->groupbyExpr.columnInfo[0].colIdx = relIndex;
S
slguan 已提交
4662

4663
      addRequiredTagColumn(pQueryInfo, pQueryInfo->groupbyExpr.columnInfo[0].colIdx, 0);
S
slguan 已提交
4664 4665 4666 4667
    }
  }
}

H
hjxilinx 已提交
4668 4669 4670 4671 4672 4673 4674
// limit the output to be 1 for each state value
static void doLimitOutputNormalColOfGroupby(SSqlExpr* pExpr) {
  int32_t outputRow = 1;
  tVariantCreateFromBinary(&pExpr->param[0], (char*) &outputRow, sizeof(int32_t), TSDB_DATA_TYPE_INT);
  pExpr->numOfParams = 1;
}

4675 4676
void doAddGroupColumnForSubquery(SQueryInfo* pQueryInfo, int32_t tagIndex) {
  int32_t index = pQueryInfo->groupbyExpr.columnInfo[tagIndex].colIdx;
S
slguan 已提交
4677

4678
  SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0);
S
slguan 已提交
4679 4680 4681 4682

  SSchema*     pSchema = tsGetColumnSchema(pMeterMetaInfo->pMeterMeta, index);
  SColumnIndex colIndex = {.tableIndex = 0, .columnIndex = index};

4683 4684
  SSqlExpr* pExpr = tscSqlExprInsert(pQueryInfo, pQueryInfo->fieldsInfo.numOfOutputCols, TSDB_FUNC_PRJ, &colIndex,
                                     pSchema->type, pSchema->bytes, pSchema->bytes);
S
slguan 已提交
4685 4686

  pExpr->colInfo.flag = TSDB_COL_NORMAL;
H
hjxilinx 已提交
4687 4688
  doLimitOutputNormalColOfGroupby(pExpr);
  
S
slguan 已提交
4689 4690 4691 4692 4693
  // NOTE: tag column does not add to source column list
  SColumnList list = {0};
  list.num = 1;
  list.ids[0] = colIndex;

4694 4695 4696
  insertResultField(pQueryInfo, pQueryInfo->fieldsInfo.numOfOutputCols, &list, pSchema->bytes, pSchema->type,
                    pSchema->name);
  tscFieldInfoUpdateVisible(&pQueryInfo->fieldsInfo, pQueryInfo->fieldsInfo.numOfOutputCols - 1, false);
S
slguan 已提交
4697 4698
}

4699
static void doUpdateSqlFunctionForTagPrj(SQueryInfo* pQueryInfo) {
S
slguan 已提交
4700
  int32_t tagLength = 0;
4701 4702
  for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) {
    SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i);
S
slguan 已提交
4703 4704 4705 4706 4707 4708 4709 4710 4711
    if (pExpr->functionId == TSDB_FUNC_TAGPRJ || pExpr->functionId == TSDB_FUNC_TAG) {
      pExpr->functionId = TSDB_FUNC_TAG_DUMMY;
      tagLength += pExpr->resBytes;
    } else if (pExpr->functionId == TSDB_FUNC_PRJ && pExpr->colInfo.colId == PRIMARYKEY_TIMESTAMP_COL_INDEX) {
      pExpr->functionId = TSDB_FUNC_TS_DUMMY;
      tagLength += pExpr->resBytes;
    }
  }

4712
  SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0);
S
slguan 已提交
4713 4714
  SSchema*        pSchema = tsGetSchema(pMeterMetaInfo->pMeterMeta);

4715 4716
  for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) {
    SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i);
S
slguan 已提交
4717 4718 4719 4720 4721 4722 4723 4724
    if (pExpr->functionId != TSDB_FUNC_TAG_DUMMY && pExpr->functionId != TSDB_FUNC_TS_DUMMY) {
      SSchema* pColSchema = &pSchema[pExpr->colInfo.colIdx];
      getResultDataInfo(pColSchema->type, pColSchema->bytes, pExpr->functionId, pExpr->param[0].i64Key, &pExpr->resType,
                        &pExpr->resBytes, &pExpr->interResBytes, tagLength, true);
    }
  }
}

4725 4726 4727
static void doUpdateSqlFunctionForColPrj(SQueryInfo* pQueryInfo) {
  for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) {
    SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i);
H
hjxilinx 已提交
4728 4729
    if (pExpr->functionId == TSDB_FUNC_PRJ) {
      bool qualifiedCol = false;
4730 4731
      for (int32_t j = 0; j < pQueryInfo->groupbyExpr.numOfGroupCols; ++j) {
        if (pExpr->colInfo.colId == pQueryInfo->groupbyExpr.columnInfo[j].colId) {
H
hjxilinx 已提交
4732
          qualifiedCol = true;
H
hjxilinx 已提交
4733
          doLimitOutputNormalColOfGroupby(pExpr);
H
hjxilinx 已提交
4734 4735 4736 4737
          pExpr->numOfParams = 1;
          break;
        }
      }
H
hjxilinx 已提交
4738

H
hjxilinx 已提交
4739 4740 4741 4742 4743
      assert(qualifiedCol);
    }
  }
}

S
slguan 已提交
4744 4745 4746 4747 4748 4749 4750 4751 4752 4753
static bool tagColumnInGroupby(SSqlGroupbyExpr* pGroupbyExpr, int16_t columnId) {
  for (int32_t j = 0; j < pGroupbyExpr->numOfGroupCols; ++j) {
    if (columnId == pGroupbyExpr->columnInfo[j].colId && pGroupbyExpr->columnInfo[j].flag == TSDB_COL_TAG) {
      return true;
    }
  }

  return false;
}

4754
static bool onlyTagPrjFunction(SQueryInfo* pQueryInfo) {
S
slguan 已提交
4755 4756 4757
  bool hasTagPrj = false;
  bool hasColumnPrj = false;

4758 4759
  for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) {
    SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i);
S
slguan 已提交
4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770
    if (pExpr->functionId == TSDB_FUNC_PRJ) {
      hasColumnPrj = true;
    } else if (pExpr->functionId == TSDB_FUNC_TAGPRJ) {
      hasTagPrj = true;
    }
  }

  return (hasTagPrj) && (hasColumnPrj == false);
}

// check if all the tags prj columns belongs to the group by columns
4771
static bool allTagPrjInGroupby(SQueryInfo* pQueryInfo) {
S
slguan 已提交
4772 4773
  bool allInGroupby = true;

4774 4775
  for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) {
    SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i);
S
slguan 已提交
4776 4777 4778 4779
    if (pExpr->functionId != TSDB_FUNC_TAGPRJ) {
      continue;
    }

4780
    if (!tagColumnInGroupby(&pQueryInfo->groupbyExpr, pExpr->colInfo.colId)) {
S
slguan 已提交
4781 4782 4783 4784 4785 4786 4787 4788 4789
      allInGroupby = false;
      break;
    }
  }

  // all selected tag columns belong to the group by columns set, always correct
  return allInGroupby;
}

4790 4791 4792
static void updateTagPrjFunction(SQueryInfo* pQueryInfo) {
  for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) {
    SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i);
S
slguan 已提交
4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804
    if (pExpr->functionId == TSDB_FUNC_TAGPRJ) {
      pExpr->functionId = TSDB_FUNC_TAG;
    }
  }
}

/*
 * check for selectivity function + tags column function both exist.
 * 1. tagprj functions are not compatible with aggregated function when missing "group by" clause
 * 2. if selectivity function and tagprj function both exist, there should be only
 *    one selectivity function exists.
 */
4805
static int32_t checkUpdateTagPrjFunctions(SQueryInfo* pQueryInfo) {
S
slguan 已提交
4806
  const char* msg1 = "only one selectivity function allowed in presence of tags function";
H
hjxilinx 已提交
4807
  const char* msg3 = "aggregation function should not be mixed up with projection";
H
hjxilinx 已提交
4808

S
slguan 已提交
4809 4810 4811 4812
  bool    tagColExists = false;
  int16_t numOfSelectivity = 0;
  int16_t numOfAggregation = 0;

4813 4814
  for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) {
    SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i);
S
slguan 已提交
4815 4816 4817 4818 4819 4820
    if (pExpr->functionId == TSDB_FUNC_TAGPRJ ||
        (pExpr->functionId == TSDB_FUNC_PRJ && pExpr->colInfo.colId == PRIMARYKEY_TIMESTAMP_COL_INDEX)) {
      tagColExists = true;
      break;
    }
  }
H
hjxilinx 已提交
4821

4822 4823
  for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) {
    int16_t functionId = tscSqlExprGet(pQueryInfo, i)->functionId;
4824
    if (functionId == TSDB_FUNC_TAGPRJ || functionId == TSDB_FUNC_PRJ || functionId == TSDB_FUNC_TS ||
H
hjxilinx 已提交
4825
        functionId == TSDB_FUNC_ARITHM) {
H
hjxilinx 已提交
4826
      continue;
S
slguan 已提交
4827
    }
H
hjxilinx 已提交
4828

H
hjxilinx 已提交
4829 4830 4831 4832 4833 4834
    if ((aAggs[functionId].nStatus & TSDB_FUNCSTATE_SELECTIVITY) != 0) {
      numOfSelectivity++;
    } else {
      numOfAggregation++;
    }
  }
H
hjxilinx 已提交
4835

H
hjxilinx 已提交
4836
  if (tagColExists) {  // check if the selectivity function exists
S
slguan 已提交
4837 4838
    // When the tag projection function on tag column that is not in the group by clause, aggregation function and
    // selectivity function exist in select clause is not allowed.
4839
    if (numOfAggregation > 0) {
4840
      return invalidSqlErrMsg(pQueryInfo->msg, msg1);
S
slguan 已提交
4841 4842 4843 4844 4845 4846
    }

    /*
     *  if numOfSelectivity equals to 0, it is a super table projection query
     */
    if (numOfSelectivity == 1) {
4847 4848
      doUpdateSqlFunctionForTagPrj(pQueryInfo);
      doUpdateSqlFunctionForColPrj(pQueryInfo);
S
slguan 已提交
4849 4850 4851 4852 4853
    } else if (numOfSelectivity > 1) {
      /*
       * If more than one selectivity functions exist, all the selectivity functions must be last_row.
       * Otherwise, return with error code.
       */
4854 4855
      for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) {
        int16_t functionId = tscSqlExprGet(pQueryInfo, i)->functionId;
S
slguan 已提交
4856 4857 4858 4859 4860
        if (functionId == TSDB_FUNC_TAGPRJ) {
          continue;
        }

        if (((aAggs[functionId].nStatus & TSDB_FUNCSTATE_SELECTIVITY) != 0) && (functionId != TSDB_FUNC_LAST_ROW)) {
4861
          return invalidSqlErrMsg(pQueryInfo->msg, msg1);
S
slguan 已提交
4862 4863 4864
        }
      }

4865 4866
      doUpdateSqlFunctionForTagPrj(pQueryInfo);
      doUpdateSqlFunctionForColPrj(pQueryInfo);
H
hjxilinx 已提交
4867 4868
    }
  } else {
4869 4870 4871
    if ((pQueryInfo->type & TSDB_QUERY_TYPE_PROJECTION_QUERY) == TSDB_QUERY_TYPE_PROJECTION_QUERY) {
      if (numOfAggregation > 0 && pQueryInfo->groupbyExpr.numOfGroupCols == 0) {
        return invalidSqlErrMsg(pQueryInfo->msg, msg3);
H
hjxilinx 已提交
4872
      }
H
hjxilinx 已提交
4873

H
hjxilinx 已提交
4874 4875
      if (numOfAggregation > 0 || numOfSelectivity > 0) {
        // clear the projection type flag
4876 4877
        pQueryInfo->type &= (~TSDB_QUERY_TYPE_PROJECTION_QUERY);
        doUpdateSqlFunctionForColPrj(pQueryInfo);
H
hjxilinx 已提交
4878
      }
S
slguan 已提交
4879 4880 4881 4882 4883 4884
    }
  }

  return TSDB_CODE_SUCCESS;
}

4885
static int32_t doAddGroupbyColumnsOnDemand(SQueryInfo* pQueryInfo) {
S
slguan 已提交
4886 4887
  const char* msg2 = "interval not allowed in group by normal column";

4888
  SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0);
S
slguan 已提交
4889 4890 4891 4892 4893 4894

  SSchema* pSchema = tsGetSchema(pMeterMetaInfo->pMeterMeta);
  int16_t  bytes = 0;
  int16_t  type = 0;
  char*    name = NULL;

4895 4896
  for (int32_t i = 0; i < pQueryInfo->groupbyExpr.numOfGroupCols; ++i) {
    SColIndexEx* pColIndex = &pQueryInfo->groupbyExpr.columnInfo[i];
S
slguan 已提交
4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912

    int16_t colIndex = pColIndex->colIdx;
    if (pColIndex->colIdx == TSDB_TBNAME_COLUMN_INDEX) {
      type = TSDB_DATA_TYPE_BINARY;
      bytes = TSDB_METER_NAME_LEN;
      name = TSQL_TBNAME_L;
    } else {
      colIndex = (TSDB_COL_IS_TAG(pColIndex->flag)) ? pMeterMetaInfo->pMeterMeta->numOfColumns + pColIndex->colIdx
                                                    : pColIndex->colIdx;

      type = pSchema[colIndex].type;
      bytes = pSchema[colIndex].bytes;
      name = pSchema[colIndex].name;
    }

    if (TSDB_COL_IS_TAG(pColIndex->flag)) {
4913
      SColumnIndex index = {.tableIndex = pQueryInfo->groupbyExpr.tableIndex, .columnIndex = colIndex};
S
slguan 已提交
4914

4915 4916
      SSqlExpr* pExpr = tscSqlExprInsert(pQueryInfo, pQueryInfo->fieldsInfo.numOfOutputCols, TSDB_FUNC_TAG, &index,
                                         type, bytes, bytes);
S
slguan 已提交
4917 4918 4919 4920 4921

      pExpr->colInfo.flag = TSDB_COL_TAG;

      // NOTE: tag column does not add to source column list
      SColumnList ids = {0};
4922
      insertResultField(pQueryInfo, pQueryInfo->fieldsInfo.numOfOutputCols, &ids, bytes, type, name);
S
slguan 已提交
4923 4924
    } else {
      // if this query is "group by" normal column, interval is not allowed
4925 4926
      if (pQueryInfo->nAggTimeInterval > 0) {
        return invalidSqlErrMsg(pQueryInfo->msg, msg2);
S
slguan 已提交
4927 4928 4929
      }

      bool hasGroupColumn = false;
4930 4931
      for (int32_t j = 0; j < pQueryInfo->fieldsInfo.numOfOutputCols; ++j) {
        SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, j);
S
slguan 已提交
4932 4933 4934 4935 4936 4937 4938 4939 4940 4941
        if (pExpr->colInfo.colId == pColIndex->colId) {
          break;
        }
      }

      /*
       * if the group by column does not required by user, add this column into the final result set
       * but invisible to user
       */
      if (!hasGroupColumn) {
4942
        doAddGroupColumnForSubquery(pQueryInfo, i);
S
slguan 已提交
4943 4944 4945 4946 4947 4948 4949
      }
    }
  }

  return TSDB_CODE_SUCCESS;
}

4950
int32_t doFunctionsCompatibleCheck(SSqlCmd* pCmd, SQueryInfo* pQueryInfo) {
S
slguan 已提交
4951
  const char* msg1 = "functions/columns not allowed in group by query";
H
hjxilinx 已提交
4952
  const char* msg2 = "projection query on columns not allowed";
S
slguan 已提交
4953
  const char* msg3 = "group by not allowed on projection query";
H
hjxilinx 已提交
4954
  const char* msg4 = "retrieve tags not compatible with group by or interval query";
S
slguan 已提交
4955 4956 4957

  // only retrieve tags, group by is not supportted
  if (pCmd->command == TSDB_SQL_RETRIEVE_TAGS) {
4958 4959
    if (pQueryInfo->groupbyExpr.numOfGroupCols > 0 || pQueryInfo->nAggTimeInterval > 0) {
      return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg4);
S
slguan 已提交
4960 4961 4962 4963 4964
    } else {
      return TSDB_CODE_SUCCESS;
    }
  }

4965
  if (pQueryInfo->groupbyExpr.numOfGroupCols > 0) {
S
slguan 已提交
4966
    // check if all the tags prj columns belongs to the group by columns
4967 4968 4969
    if (onlyTagPrjFunction(pQueryInfo) && allTagPrjInGroupby(pQueryInfo)) {
      updateTagPrjFunction(pQueryInfo);
      return doAddGroupbyColumnsOnDemand(pQueryInfo);
S
slguan 已提交
4970 4971 4972
    }

    // check all query functions in selection clause, multi-output functions are not allowed
4973 4974
    for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) {
      SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i);
S
slguan 已提交
4975 4976 4977 4978 4979 4980
      int32_t   functId = pExpr->functionId;

      /*
       * group by normal columns.
       * Check if the column projection is identical to the group by column or not
       */
4981
      if (functId == TSDB_FUNC_PRJ && pExpr->colInfo.colId != PRIMARYKEY_TIMESTAMP_COL_INDEX) {
S
slguan 已提交
4982
        bool qualified = false;
4983 4984
        for (int32_t j = 0; j < pQueryInfo->groupbyExpr.numOfGroupCols; ++j) {
          SColIndexEx* pColIndex = &pQueryInfo->groupbyExpr.columnInfo[j];
S
slguan 已提交
4985 4986 4987 4988 4989 4990 4991
          if (pColIndex->colId == pExpr->colInfo.colId) {
            qualified = true;
            break;
          }
        }

        if (!qualified) {
4992
          return invalidSqlErrMsg(pQueryInfo->msg, msg2);
S
slguan 已提交
4993 4994 4995 4996
        }
      }

      if (IS_MULTIOUTPUT(aAggs[functId].nStatus) && functId != TSDB_FUNC_TOP && functId != TSDB_FUNC_BOTTOM &&
H
hjxilinx 已提交
4997
          functId != TSDB_FUNC_TAGPRJ && functId != TSDB_FUNC_PRJ) {
4998
        return invalidSqlErrMsg(pQueryInfo->msg, msg1);
S
slguan 已提交
4999 5000 5001
      }

      if (functId == TSDB_FUNC_COUNT && pExpr->colInfo.colIdx == TSDB_TBNAME_COLUMN_INDEX) {
5002
        return invalidSqlErrMsg(pQueryInfo->msg, msg1);
S
slguan 已提交
5003 5004 5005
      }
    }

5006
    if (checkUpdateTagPrjFunctions(pQueryInfo) != TSDB_CODE_SUCCESS) {
S
slguan 已提交
5007 5008 5009 5010 5011 5012 5013
      return TSDB_CODE_INVALID_SQL;
    }

    /*
     * group by tag function must be not changed the function name, otherwise, the group operation may fail to
     * divide the subset of final result.
     */
5014
    if (doAddGroupbyColumnsOnDemand(pQueryInfo) != TSDB_CODE_SUCCESS) {
S
slguan 已提交
5015 5016 5017 5018
      return TSDB_CODE_INVALID_SQL;
    }

    // projection query on metric does not compatible with "group by" syntax
5019
    if (tscProjectionQueryOnSTable(pQueryInfo, 0)) {
5020
      return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3);
S
slguan 已提交
5021
    }
H
hjxilinx 已提交
5022

H
hjxilinx 已提交
5023
    return TSDB_CODE_SUCCESS;
S
slguan 已提交
5024
  } else {
5025
    return checkUpdateTagPrjFunctions(pQueryInfo);
S
slguan 已提交
5026 5027
  }
}
H
hjxilinx 已提交
5028

5029
int32_t doLocalQueryProcess(SQueryInfo* pQueryInfo, SQuerySQL* pQuerySql) {
H
hjxilinx 已提交
5030 5031 5032
  const char* msg1 = "only one expression allowed";
  const char* msg2 = "invalid expression in select clause";
  const char* msg3 = "invalid function";
H
hjxilinx 已提交
5033

H
hjxilinx 已提交
5034 5035
  tSQLExprList* pExprList = pQuerySql->pSelection;
  if (pExprList->nExpr != 1) {
5036
    return invalidSqlErrMsg(pQueryInfo->msg, msg1);
H
hjxilinx 已提交
5037
  }
H
hjxilinx 已提交
5038

H
hjxilinx 已提交
5039 5040
  tSQLExpr* pExpr = pExprList->a[0].pNode;
  if (pExpr->operand.z == NULL) {
5041
    return invalidSqlErrMsg(pQueryInfo->msg, msg2);
H
hjxilinx 已提交
5042
  }
H
hjxilinx 已提交
5043

H
hjxilinx 已提交
5044
  // TODO redefine the function
H
hjxilinx 已提交
5045 5046 5047 5048 5049 5050
  SDNodeDynConfOption functionsInfo[5] = {{"database()", 10},
                                          {"server_version()", 16},
                                          {"server_status()", 15},
                                          {"client_version()", 16},
                                          {"current_user()", 14}};

H
hjxilinx 已提交
5051
  int32_t index = -1;
H
hjxilinx 已提交
5052
  for (int32_t i = 0; i < tListLen(functionsInfo); ++i) {
H
hjxilinx 已提交
5053
    if (strncasecmp(functionsInfo[i].name, pExpr->operand.z, functionsInfo[i].len) == 0 &&
H
hjxilinx 已提交
5054
        functionsInfo[i].len == pExpr->operand.n) {
H
hjxilinx 已提交
5055 5056 5057 5058
      index = i;
      break;
    }
  }
H
hjxilinx 已提交
5059

5060
  SSqlExpr* pExpr1 = tscSqlExprInsertEmpty(pQueryInfo, 0, TSDB_FUNC_TAG_DUMMY);
H
hjxilinx 已提交
5061 5062 5063 5064 5065
  if (pExprList->a[0].aliasName != NULL) {
    strncpy(pExpr1->aliasName, pExprList->a[0].aliasName, tListLen(pExpr1->aliasName));
  } else {
    strncpy(pExpr1->aliasName, functionsInfo[index].name, tListLen(pExpr1->aliasName));
  }
H
hjxilinx 已提交
5066 5067 5068

  switch (index) {
    case 0:
5069
      pQueryInfo->command = TSDB_SQL_CURRENT_DB;
H
hjxilinx 已提交
5070 5071
      return TSDB_CODE_SUCCESS;
    case 1:
5072
      pQueryInfo->command = TSDB_SQL_SERV_VERSION;
H
hjxilinx 已提交
5073 5074
      return TSDB_CODE_SUCCESS;
    case 2:
5075
      pQueryInfo->command = TSDB_SQL_SERV_STATUS;
H
hjxilinx 已提交
5076 5077
      return TSDB_CODE_SUCCESS;
    case 3:
5078
      pQueryInfo->command = TSDB_SQL_CLI_VERSION;
H
hjxilinx 已提交
5079 5080
      return TSDB_CODE_SUCCESS;
    case 4:
5081
      pQueryInfo->command = TSDB_SQL_CURRENT_USER;
H
hjxilinx 已提交
5082
      return TSDB_CODE_SUCCESS;
5083
    default: { return invalidSqlErrMsg(pQueryInfo->msg, msg3); }
H
hjxilinx 已提交
5084 5085
  }
}
H
hjxilinx 已提交
5086 5087

// can only perform the parameters based on the macro definitation
H
hjxilinx 已提交
5088
int32_t tscCheckCreateDbParams(SSqlCmd* pCmd, SCreateDbMsg* pCreate) {
H
hjxilinx 已提交
5089
  char msg[512] = {0};
H
hjxilinx 已提交
5090

H
hjxilinx 已提交
5091 5092
  if (pCreate->commitLog != -1 && (pCreate->commitLog < 0 || pCreate->commitLog > 1)) {
    snprintf(msg, tListLen(msg), "invalid db option commitLog: %d, only 0 or 1 allowed", pCreate->commitLog);
5093
    return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg);
H
hjxilinx 已提交
5094
  }
H
hjxilinx 已提交
5095

H
hjxilinx 已提交
5096 5097
  if (pCreate->replications != -1 &&
      (pCreate->replications < TSDB_REPLICA_MIN_NUM || pCreate->replications > TSDB_REPLICA_MAX_NUM)) {
H
hjxilinx 已提交
5098 5099
    snprintf(msg, tListLen(msg), "invalid db option replications: %d valid range: [%d, %d]", pCreate->replications,
             TSDB_REPLICA_MIN_NUM, TSDB_REPLICA_MAX_NUM);
5100
    return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg);
H
hjxilinx 已提交
5101
  }
H
hjxilinx 已提交
5102

H
hjxilinx 已提交
5103 5104 5105 5106
  int32_t val = htonl(pCreate->daysPerFile);
  if (val != -1 && (val < TSDB_FILE_MIN_PARTITION_RANGE || val > TSDB_FILE_MAX_PARTITION_RANGE)) {
    snprintf(msg, tListLen(msg), "invalid db option daysPerFile: %d valid range: [%d, %d]", val,
             TSDB_FILE_MIN_PARTITION_RANGE, TSDB_FILE_MAX_PARTITION_RANGE);
5107
    return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg);
H
hjxilinx 已提交
5108
  }
H
hjxilinx 已提交
5109

H
hjxilinx 已提交
5110 5111 5112 5113
  val = htonl(pCreate->rowsInFileBlock);
  if (val != -1 && (val < TSDB_MIN_ROWS_IN_FILEBLOCK || val > TSDB_MAX_ROWS_IN_FILEBLOCK)) {
    snprintf(msg, tListLen(msg), "invalid db option rowsInFileBlock: %d valid range: [%d, %d]", val,
             TSDB_MIN_ROWS_IN_FILEBLOCK, TSDB_MAX_ROWS_IN_FILEBLOCK);
5114
    return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg);
H
hjxilinx 已提交
5115
  }
H
hjxilinx 已提交
5116

H
hjxilinx 已提交
5117 5118 5119 5120
  val = htonl(pCreate->cacheBlockSize);
  if (val != -1 && (val < TSDB_MIN_CACHE_BLOCK_SIZE || val > TSDB_MAX_CACHE_BLOCK_SIZE)) {
    snprintf(msg, tListLen(msg), "invalid db option cacheBlockSize: %d valid range: [%d, %d]", val,
             TSDB_MIN_CACHE_BLOCK_SIZE, TSDB_MAX_CACHE_BLOCK_SIZE);
5121
    return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg);
H
hjxilinx 已提交
5122
  }
H
hjxilinx 已提交
5123

H
hjxilinx 已提交
5124 5125
  val = htonl(pCreate->maxSessions);
  if (val != -1 && (val < TSDB_MIN_TABLES_PER_VNODE || val > TSDB_MAX_TABLES_PER_VNODE)) {
H
hjxilinx 已提交
5126 5127
    snprintf(msg, tListLen(msg), "invalid db option maxSessions: %d valid range: [%d, %d]", val,
             TSDB_MIN_TABLES_PER_VNODE, TSDB_MAX_TABLES_PER_VNODE);
5128
    return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg);
H
hjxilinx 已提交
5129
  }
H
hjxilinx 已提交
5130 5131 5132 5133

  if (pCreate->precision != TSDB_TIME_PRECISION_MILLI && pCreate->precision != TSDB_TIME_PRECISION_MICRO) {
    snprintf(msg, tListLen(msg), "invalid db option timePrecision: %d valid value: [%d, %d]", pCreate->precision,
             TSDB_TIME_PRECISION_MILLI, TSDB_TIME_PRECISION_MICRO);
5134
    return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg);
H
hjxilinx 已提交
5135
  }
H
hjxilinx 已提交
5136

H
hjxilinx 已提交
5137
  if (pCreate->cacheNumOfBlocks.fraction != -1 && (pCreate->cacheNumOfBlocks.fraction < TSDB_MIN_AVG_BLOCKS ||
H
hjxilinx 已提交
5138 5139 5140
                                                   pCreate->cacheNumOfBlocks.fraction > TSDB_MAX_AVG_BLOCKS)) {
    snprintf(msg, tListLen(msg), "invalid db option ablocks: %f valid value: [%d, %d]",
             pCreate->cacheNumOfBlocks.fraction, TSDB_MIN_AVG_BLOCKS, TSDB_MAX_AVG_BLOCKS);
5141
    return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg);
H
hjxilinx 已提交
5142
  }
H
hjxilinx 已提交
5143

H
hjxilinx 已提交
5144 5145 5146 5147
  val = htonl(pCreate->commitTime);
  if (val != -1 && (val < TSDB_MIN_COMMIT_TIME_INTERVAL || val > TSDB_MAX_COMMIT_TIME_INTERVAL)) {
    snprintf(msg, tListLen(msg), "invalid db option commitTime: %d valid range: [%d, %d]", val,
             TSDB_MIN_COMMIT_TIME_INTERVAL, TSDB_MAX_COMMIT_TIME_INTERVAL);
5148
    return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg);
H
hjxilinx 已提交
5149
  }
H
hjxilinx 已提交
5150

H
hjxilinx 已提交
5151 5152
  if (pCreate->compression != -1 &&
      (pCreate->compression < TSDB_MIN_COMPRESSION_LEVEL || pCreate->compression > TSDB_MAX_COMPRESSION_LEVEL)) {
H
hjxilinx 已提交
5153 5154
    snprintf(msg, tListLen(msg), "invalid db option compression: %d valid range: [%d, %d]", pCreate->compression,
             TSDB_MIN_COMPRESSION_LEVEL, TSDB_MAX_COMPRESSION_LEVEL);
5155
    return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg);
H
hjxilinx 已提交
5156
  }
H
hjxilinx 已提交
5157

H
hjxilinx 已提交
5158 5159
  return TSDB_CODE_SUCCESS;
}
H
hjxilinx 已提交
5160 5161

// for debug purpose
H
hjxilinx 已提交
5162 5163
void tscPrintSelectClause(SSqlObj* pSql, int32_t subClauseIndex) {
  SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, subClauseIndex);
5164

H
hjxilinx 已提交
5165
  if (pQueryInfo->exprsInfo.numOfExprs == 0) {
H
hjxilinx 已提交
5166 5167
    return;
  }
H
hjxilinx 已提交
5168

H
hjxilinx 已提交
5169 5170 5171
  int32_t totalBufSize = 1024;
  
  char str[1024] = {0};
H
hjxilinx 已提交
5172
  int32_t offset = 0;
H
hjxilinx 已提交
5173

H
hjxilinx 已提交
5174
  offset += sprintf(str, "num:%d [", pQueryInfo->exprsInfo.numOfExprs);
5175 5176
  for (int32_t i = 0; i < pQueryInfo->exprsInfo.numOfExprs; ++i) {
    SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i);
H
hjxilinx 已提交
5177

L
lihui 已提交
5178 5179 5180 5181 5182 5183
    char    tmpBuf[1024] = {0};
    int32_t tmpLen       = 0;
    tmpLen = sprintf(tmpBuf, "%s(uid:%" PRId64 ", %d)", aAggs[pExpr->functionId].aName, pExpr->uid, pExpr->colInfo.colId);
    if (tmpLen + offset > totalBufSize) break;

    offset += sprintf(str + offset, "%s", tmpBuf);
H
hjxilinx 已提交
5184
    
5185
    if (i < pQueryInfo->exprsInfo.numOfExprs - 1) {
H
hjxilinx 已提交
5186 5187 5188
      str[offset++] = ',';
    }
  }
H
hjxilinx 已提交
5189

H
hjxilinx 已提交
5190
  str[offset] = ']';
H
hjxilinx 已提交
5191
  tscTrace("%p select clause:%s", pSql, str);
H
hjxilinx 已提交
5192
}
5193

5194
int32_t doCheckForCreateTable(SSqlObj* pSql, int32_t subClauseIndex, SSqlInfo* pInfo) {
5195 5196 5197
  const char* msg1 = "invalid table name";
  const char* msg2 = "table name too long";

5198 5199 5200
  SSqlCmd*        pCmd = &pSql->cmd;
  SQueryInfo*     pQueryInfo = tscGetQueryInfoDetail(pCmd, subClauseIndex);
  SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0);
5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212

  SCreateTableSQL* pCreateTable = pInfo->pCreateTableInfo;

  tFieldList* pFieldList = pCreateTable->colInfo.pColumns;
  tFieldList* pTagList = pCreateTable->colInfo.pTagColumns;

  assert(pFieldList != NULL);

  // if sql specifies db, use it, otherwise use default db
  SSQLToken* pzTableName = &(pCreateTable->name);

  if (tscValidateName(pzTableName) != TSDB_CODE_SUCCESS) {
5213
    return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1);
5214 5215
  }

5216
  if (setMeterID(pMeterMetaInfo, pzTableName, pSql) != TSDB_CODE_SUCCESS) {
5217
    return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2);
5218 5219 5220 5221 5222 5223 5224 5225 5226
  }

  if (!validateTableColumnInfo(pFieldList, pCmd) ||
      (pTagList != NULL && !validateTagParams(pTagList, pFieldList, pCmd))) {
    return TSDB_CODE_INVALID_SQL;
  }

  int32_t col = 0;
  for (; col < pFieldList->nField; ++col) {
5227
    tscFieldInfoSetValFromField(&pQueryInfo->fieldsInfo, col, &pFieldList->p[col]);
5228 5229 5230 5231 5232 5233
  }

  pCmd->numOfCols = (int16_t)pFieldList->nField;

  if (pTagList != NULL) {  // create metric[optional]
    for (int32_t i = 0; i < pTagList->nField; ++i) {
5234
      tscFieldInfoSetValFromField(&pQueryInfo->fieldsInfo, col++, &pTagList->p[i]);
5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251
    }

    pCmd->count = pTagList->nField;
  }

  return TSDB_CODE_SUCCESS;
}

int32_t doCheckForCreateFromStable(SSqlObj* pSql, SSqlInfo* pInfo) {
  const char* msg1 = "invalid table name";
  const char* msg3 = "tag value too long";
  const char* msg4 = "illegal value or data overflow";
  const char* msg5 = "tags number not matched";

  SSqlCmd* pCmd = &pSql->cmd;

  SCreateTableSQL* pCreateTable = pInfo->pCreateTableInfo;
5252 5253 5254 5255 5256 5257 5258 5259 5260 5261
  SQueryInfo*      pQueryInfo = tscGetQueryInfoDetail(pCmd, 0);

  // two table: the first one is for current table, and the secondary is for the super table.
  tscAddEmptyMeterMetaInfo(pQueryInfo);
  assert(pQueryInfo->numOfTables == 2);

  const int32_t TABLE_INDEX = 0;
  const int32_t STABLE_INDEX = 1;

  SMeterMetaInfo* pStableMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, STABLE_INDEX);
5262 5263 5264 5265 5266

  // super table name, create table by using dst
  SSQLToken* pToken = &(pCreateTable->usingInfo.stableName);

  if (tscValidateName(pToken) != TSDB_CODE_SUCCESS) {
5267
    return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1);
5268 5269
  }

5270
  if (setMeterID(pStableMeterMetaInfo, pToken, pSql) != TSDB_CODE_SUCCESS) {
5271
    return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1);
5272 5273 5274
  }

  // get meter meta from mnode
5275
  strncpy(pCreateTable->usingInfo.tagdata.name, pStableMeterMetaInfo->name, TSDB_METER_ID_LEN);
5276 5277
  tVariantList* pList = pInfo->pCreateTableInfo->usingInfo.pTagVals;

5278
  int32_t code = tscGetMeterMeta(pSql, pStableMeterMetaInfo);
5279 5280 5281 5282
  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }

5283
  if (pStableMeterMetaInfo->pMeterMeta->numOfTags != pList->nExpr) {
5284
    return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg5);
5285 5286 5287
  }

  // too long tag values will return invalid sql, not be truncated automatically
5288
  SSchema* pTagSchema = tsGetTagSchema(pStableMeterMetaInfo->pMeterMeta);
5289 5290 5291 5292 5293

  char* tagVal = pCreateTable->usingInfo.tagdata.data;
  for (int32_t i = 0; i < pList->nExpr; ++i) {
    int32_t ret = tVariantDump(&(pList->a[i].pVar), tagVal, pTagSchema[i].type);
    if (ret != TSDB_CODE_SUCCESS) {
5294
      return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg4);
5295 5296 5297 5298 5299
    }

    // validate the length of binary
    if ((pTagSchema[i].type == TSDB_DATA_TYPE_BINARY || pTagSchema[i].type == TSDB_DATA_TYPE_NCHAR) &&
        pList->a[i].pVar.nLen > pTagSchema[i].bytes) {
5300
      return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3);
5301 5302 5303 5304 5305 5306 5307
    }

    tagVal += pTagSchema[i].bytes;
  }

  // table name
  if (tscValidateName(&pInfo->pCreateTableInfo->name) != TSDB_CODE_SUCCESS) {
5308
    return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1);
5309 5310
  }

5311 5312
  SMeterMetaInfo* pTableMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, TABLE_INDEX);
  int32_t         ret = setMeterID(pTableMeterMetaInfo, &pInfo->pCreateTableInfo->name, pSql);
5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326
  if (ret != TSDB_CODE_SUCCESS) {
    return ret;
  }

  return TSDB_CODE_SUCCESS;
}

int32_t doCheckForStream(SSqlObj* pSql, SSqlInfo* pInfo) {
  const char* msg1 = "invalid table name";
  const char* msg2 = "table name too long";
  const char* msg3 = "fill only available for interval query";
  const char* msg4 = "fill option not supported in stream computing";
  const char* msg5 = "sql too long";  // todo ADD support

5327
  SSqlCmd*    pCmd = &pSql->cmd;
5328
  SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0);
5329 5330
  assert(pQueryInfo->numOfTables == 1);

5331
  SCreateTableSQL* pCreateTable = pInfo->pCreateTableInfo;
5332
  SMeterMetaInfo*  pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0);
5333 5334 5335 5336 5337 5338

  // if sql specifies db, use it, otherwise use default db
  SSQLToken* pzTableName = &(pCreateTable->name);
  SQuerySQL* pQuerySql = pCreateTable->pSelect;

  if (tscValidateName(pzTableName) != TSDB_CODE_SUCCESS) {
5339
    return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1);
5340 5341 5342 5343 5344 5345 5346
  }

  tVariantList* pSrcMeterName = pInfo->pCreateTableInfo->pSelect->from;
  tVariant*     pVar = &pSrcMeterName->a[0].pVar;

  SSQLToken srcToken = {.z = pVar->pz, .n = pVar->nLen, .type = TK_STRING};
  if (tscValidateName(&srcToken) != TSDB_CODE_SUCCESS) {
5347
    return invalidSqlErrMsg(pQueryInfo->msg, msg1);
5348 5349
  }

5350
  if (setMeterID(pMeterMetaInfo, &srcToken, pSql) != TSDB_CODE_SUCCESS) {
5351
    return invalidSqlErrMsg(pQueryInfo->msg, msg2);
5352 5353
  }

5354
  int32_t code = tscGetMeterMeta(pSql, pMeterMetaInfo);
5355 5356 5357 5358
  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }

5359
  bool isSTable = UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo);
5360
  if (parseSelectClause(&pSql->cmd, 0, pQuerySql->pSelection, isSTable) != TSDB_CODE_SUCCESS) {
5361 5362 5363 5364
    return TSDB_CODE_INVALID_SQL;
  }

  if (pQuerySql->pWhere != NULL) {  // query condition in stream computing
5365
    if (parseWhereClause(pQueryInfo, &pQuerySql->pWhere, pSql) != TSDB_CODE_SUCCESS) {
5366 5367 5368 5369 5370
      return TSDB_CODE_INVALID_SQL;
    }
  }

  // set interval value
5371
  if (parseIntervalClause(pQueryInfo, pQuerySql) != TSDB_CODE_SUCCESS) {
5372 5373
    return TSDB_CODE_INVALID_SQL;
  } else {
5374 5375
    if ((pQueryInfo->nAggTimeInterval > 0) &&
        (validateFunctionsInIntervalOrGroupbyQuery(pQueryInfo) != TSDB_CODE_SUCCESS)) {
5376 5377 5378 5379
      return TSDB_CODE_INVALID_SQL;
    }
  }

5380
  if (setSlidingClause(pQueryInfo, pQuerySql) != TSDB_CODE_SUCCESS) {
5381 5382 5383 5384
    return TSDB_CODE_INVALID_SQL;
  }

  // set the created table[stream] name
5385
  if (setMeterID(pMeterMetaInfo, pzTableName, pSql) != TSDB_CODE_SUCCESS) {
5386
    return invalidSqlErrMsg(pQueryInfo->msg, msg1);
5387 5388 5389
  }

  if (pQuerySql->selectToken.n > TSDB_MAX_SAVED_SQL_LEN) {
5390
    return invalidSqlErrMsg(pQueryInfo->msg, msg5);
5391 5392
  }

5393
  if (tsRewriteFieldNameIfNecessary(pQueryInfo) != TSDB_CODE_SUCCESS) {
5394 5395 5396
    return TSDB_CODE_INVALID_SQL;
  }

5397
  pCmd->numOfCols = pQueryInfo->fieldsInfo.numOfOutputCols;
5398

5399
  if (validateSqlFunctionInStreamSql(pQueryInfo) != TSDB_CODE_SUCCESS) {
5400 5401 5402 5403 5404 5405 5406 5407
    return TSDB_CODE_INVALID_SQL;
  }

  /*
   * check if fill operation is available, the fill operation is parsed and executed during query execution,
   * not here.
   */
  if (pQuerySql->fillType != NULL) {
5408 5409
    if (pQueryInfo->nAggTimeInterval == 0) {
      return invalidSqlErrMsg(pQueryInfo->msg, msg3);
5410 5411 5412 5413 5414 5415
    }

    tVariantListItem* pItem = &pQuerySql->fillType->a[0];
    if (pItem->pVar.nType == TSDB_DATA_TYPE_BINARY) {
      if (!((strncmp(pItem->pVar.pz, "none", 4) == 0 && pItem->pVar.nLen == 4) ||
            (strncmp(pItem->pVar.pz, "null", 4) == 0 && pItem->pVar.nLen == 4))) {
5416
        return invalidSqlErrMsg(pQueryInfo->msg, msg4);
5417 5418 5419 5420 5421
      }
    }
  }

  // set the number of stream table columns
5422
  pCmd->numOfCols = pQueryInfo->fieldsInfo.numOfOutputCols;
5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440
  return TSDB_CODE_SUCCESS;
}

int32_t doCheckForQuery(SSqlObj* pSql, SQuerySQL* pQuerySql, int32_t index) {
  assert(pQuerySql != NULL && (pQuerySql->from == NULL || pQuerySql->from->nExpr > 0));

  const char* msg0 = "invalid table name";
  const char* msg1 = "table name too long";
  const char* msg2 = "point interpolation query needs timestamp";
  const char* msg3 = "sliding value too small";
  const char* msg4 = "sliding value no larger than the interval value";
  const char* msg5 = "fill only available for interval query";
  const char* msg6 = "start(end) time of query range required or time range too large";
  const char* msg7 = "illegal number of tables in from clause";
  const char* msg8 = "too many columns in selection clause";
  const char* msg9 = "TWA query requires both the start and end time";

  int32_t code = TSDB_CODE_SUCCESS;
5441

5442
  SSqlCmd* pCmd = &pSql->cmd;
5443

5444 5445
  SQueryInfo*     pQueryInfo = tscGetQueryInfoDetail(pCmd, index);
  SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0);
5446 5447 5448 5449
  if (pMeterMetaInfo == NULL) {
    pMeterMetaInfo = tscAddEmptyMeterMetaInfo(pQueryInfo);
  }
  
5450 5451
  // too many result columns not support order by in query
  if (pQuerySql->pSelection->nExpr > TSDB_MAX_COLUMNS) {
5452
    return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg8);
5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464
  }

  /*
   * handle the sql expression without from subclause
   * select current_database();
   * select server_version();
   * select client_version();
   * select server_state();
   */
  if (pQuerySql->from == NULL) {
    assert(pQuerySql->fillType == NULL && pQuerySql->pGroupby == NULL && pQuerySql->pWhere == NULL &&
           pQuerySql->pSortOrder == NULL);
5465
    return doLocalQueryProcess(pQueryInfo, pQuerySql);
5466 5467 5468
  }

  if (pQuerySql->from->nExpr > TSDB_MAX_JOIN_TABLE_NUM) {
5469
    return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg7);
5470 5471
  }

5472 5473
  pQueryInfo->command = TSDB_SQL_SELECT;
  
5474 5475 5476 5477 5478
  // set all query tables, which are maybe more than one.
  for (int32_t i = 0; i < pQuerySql->from->nExpr; ++i) {
    tVariant* pTableItem = &pQuerySql->from->a[i].pVar;

    if (pTableItem->nType != TSDB_DATA_TYPE_BINARY) {
5479
      return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg0);
5480 5481 5482 5483 5484 5485
    }

    pTableItem->nLen = strdequote(pTableItem->pz);

    SSQLToken tableName = {.z = pTableItem->pz, .n = pTableItem->nLen, .type = TK_STRING};
    if (tscValidateName(&tableName) != TSDB_CODE_SUCCESS) {
5486
      return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg0);
5487 5488
    }

5489
    if (pQueryInfo->numOfTables <= i) {  // more than one table
5490
      tscAddEmptyMeterMetaInfo(pQueryInfo);
5491 5492
    }

5493 5494
    SMeterMetaInfo* pMeterInfo1 = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, i);

5495
    SSQLToken t = {.type = TSDB_DATA_TYPE_BINARY, .n = pTableItem->nLen, .z = pTableItem->pz};
5496
    if (setMeterID(pMeterInfo1, &t, pSql) != TSDB_CODE_SUCCESS) {
5497
      return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1);
5498 5499
    }

5500
    code = tscGetMeterMeta(pSql, pMeterInfo1);
5501 5502 5503 5504 5505
    if (code != TSDB_CODE_SUCCESS) {
      return code;
    }
  }

5506 5507
  assert(pQueryInfo->numOfTables == pQuerySql->from->nExpr);

5508
  // parse the group by clause in the first place
5509
  if (parseGroupbyClause(pQueryInfo, pQuerySql->pGroupby, pCmd) != TSDB_CODE_SUCCESS) {
5510 5511 5512
    return TSDB_CODE_INVALID_SQL;
  }

5513
  bool isSTable = UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo);
5514
  if (parseSelectClause(pCmd, index, pQuerySql->pSelection, isSTable) != TSDB_CODE_SUCCESS) {
5515 5516 5517 5518
    return TSDB_CODE_INVALID_SQL;
  }

  // set interval value
5519
  if (parseIntervalClause(pQueryInfo, pQuerySql) != TSDB_CODE_SUCCESS) {
5520 5521
    return TSDB_CODE_INVALID_SQL;
  } else {
5522 5523
    if ((pQueryInfo->nAggTimeInterval > 0) &&
        (validateFunctionsInIntervalOrGroupbyQuery(pQueryInfo) != TSDB_CODE_SUCCESS)) {
5524 5525 5526 5527 5528 5529 5530
      return TSDB_CODE_INVALID_SQL;
    }
  }

  // set sliding value
  SSQLToken* pSliding = &pQuerySql->sliding;
  if (pSliding->n != 0) {
5531
    if (!tscEmbedded && pCmd->inStream == 0) {  // sliding only allowed in stream
5532
      const char* msg = "not support sliding in query";
5533
      return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg);
5534 5535
    }

5536
    getTimestampInUsFromStr(pSliding->z, pSliding->n, &pQueryInfo->nSlidingTime);
5537
    if (pMeterMetaInfo->pMeterMeta->precision == TSDB_TIME_PRECISION_MILLI) {
5538
      pQueryInfo->nSlidingTime /= 1000;
5539 5540
    }

5541 5542
    if (pQueryInfo->nSlidingTime < tsMinSlidingTime) {
      return invalidSqlErrMsg(pQueryInfo->msg, msg3);
5543 5544
    }

5545 5546
    if (pQueryInfo->nSlidingTime > pQueryInfo->nAggTimeInterval) {
      return invalidSqlErrMsg(pQueryInfo->msg, msg4);
5547 5548 5549 5550
    }
  }

  // set order by info
H
hjxilinx 已提交
5551
  if (parseOrderbyClause(pQueryInfo, pQuerySql, tsGetSchema(pMeterMetaInfo->pMeterMeta)) != TSDB_CODE_SUCCESS) {
5552 5553 5554 5555 5556
    return TSDB_CODE_INVALID_SQL;
  }

  // set where info
  if (pQuerySql->pWhere != NULL) {
5557
    if (parseWhereClause(pQueryInfo, &pQuerySql->pWhere, pSql) != TSDB_CODE_SUCCESS) {
5558 5559 5560 5561 5562 5563
      return TSDB_CODE_INVALID_SQL;
    }

    pQuerySql->pWhere = NULL;

    if (pMeterMetaInfo->pMeterMeta->precision == TSDB_TIME_PRECISION_MILLI) {
5564 5565
      pQueryInfo->stime = pQueryInfo->stime / 1000;
      pQueryInfo->etime = pQueryInfo->etime / 1000;
5566 5567
    }
  } else {  // set the time rang
5568 5569
    pQueryInfo->stime = 0;
    pQueryInfo->etime = INT64_MAX;
5570 5571 5572
  }

  // user does not specified the query time window, twa is not allowed in such case.
5573
  if ((pQueryInfo->stime == 0 || pQueryInfo->etime == INT64_MAX ||
5574
       (pQueryInfo->etime == INT64_MAX / 1000 && pMeterMetaInfo->pMeterMeta->precision == TSDB_TIME_PRECISION_MILLI)) &&
5575 5576
      tscIsTWAQuery(pQueryInfo)) {
    return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg9);
5577 5578 5579
  }

  // no result due to invalid query time range
5580
  if (pQueryInfo->stime > pQueryInfo->etime) {
5581 5582 5583 5584
    pCmd->command = TSDB_SQL_RETRIEVE_EMPTY_RESULT;
    return TSDB_CODE_SUCCESS;
  }

5585 5586
  if (!hasTimestampForPointInterpQuery(pQueryInfo)) {
    return invalidSqlErrMsg(pQueryInfo->msg, msg2);
5587 5588 5589
  }

  // in case of join query, time range is required.
5590 5591
  if (QUERY_IS_JOIN_QUERY(pQueryInfo->type)) {
    int64_t timeRange = labs(pQueryInfo->stime - pQueryInfo->etime);
5592

5593 5594
    if (timeRange == 0 && pQueryInfo->stime == 0) {
      return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg6);
5595 5596 5597
    }
  }

5598
  if ((code = parseLimitClause(pQueryInfo, index, pQuerySql, pSql)) != TSDB_CODE_SUCCESS) {
5599 5600 5601
    return code;
  }

5602
  if ((code = doFunctionsCompatibleCheck(pCmd, pQueryInfo)) != TSDB_CODE_SUCCESS) {
5603 5604 5605
    return code;
  }

5606
  setColumnOffsetValueInResultset(pQueryInfo);
5607

5608 5609
  for (int32_t i = 0; i < pQueryInfo->numOfTables; ++i) {
    updateTagColumnIndex(pQueryInfo, i);
5610
  }
5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633
  
  /*
   * fill options are set at the end position, when all columns are set properly
   * the columns may be increased due to group by operation
   */
  if (pQuerySql->fillType != NULL) {
    if (pQueryInfo->nAggTimeInterval == 0 && (!tscIsPointInterpQuery(pQueryInfo))) {
      return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg5);
    }
    
    if (pQueryInfo->nAggTimeInterval > 0) {
      int64_t timeRange = labs(pQueryInfo->stime - pQueryInfo->etime);
      // number of result is not greater than 10,000,000
      if ((timeRange == 0) || (timeRange / pQueryInfo->nAggTimeInterval) > MAX_RETRIEVE_ROWS_IN_INTERVAL_QUERY) {
        return invalidSqlErrMsg(pQueryInfo->msg, msg6);
      }
    }
    
    int32_t ret = parseFillClause(pQueryInfo, pQuerySql);
    if (ret != TSDB_CODE_SUCCESS) {
      return ret;
    }
  }
5634 5635 5636

  return TSDB_CODE_SUCCESS;  // Does not build query message here
}