parserTests.cpp 19.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * 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/>.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
 */

#include <gtest/gtest.h>
#include <iostream>
#pragma GCC diagnostic ignored "-Wwrite-strings"

#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wsign-compare"
#include "os.h"

#include "astGenerator.h"
#include "parserInt.h"
#include "taos.h"
#include "tdef.h"
#include "tvariant.h"

namespace {
void setSchema(SSchema* p, int32_t type, int32_t bytes, const char* name, int32_t colId) {
  p->colId = colId;
  p->bytes = bytes;
  p->type = type;
  strcpy(p->name, name);
}

void setTableMetaInfo(SQueryStmtInfo* pQueryInfo, SMetaReq *req) {
  pQueryInfo->numOfTables = 1;

  pQueryInfo->pTableMetaInfo = (STableMetaInfo**)calloc(1, POINTER_BYTES);
  STableMetaInfo* pTableMetaInfo = (STableMetaInfo*)calloc(1, sizeof(STableMetaInfo));
  pQueryInfo->pTableMetaInfo[0] = pTableMetaInfo;

  SName* name = (SName*)taosArrayGet(req->pTableName, 0);

  memcpy(&pTableMetaInfo->name, taosArrayGet(req->pTableName, 0), sizeof(SName));
  pTableMetaInfo->pTableMeta = (STableMeta*)calloc(1, sizeof(STableMeta) + 4 * sizeof(SSchema));
  strcpy(pTableMetaInfo->aliasName, name->tname);
  STableMeta* pTableMeta = pTableMetaInfo->pTableMeta;
  pTableMeta->tableType = TSDB_NORMAL_TABLE;
  pTableMeta->tableInfo.numOfColumns = 4;
  pTableMeta->tableInfo.rowSize = 28;
  pTableMeta->uid = 110;

  pTableMetaInfo->tagColList = (SArray*) taosArrayInit(4, POINTER_BYTES);

  SSchema* pSchema = pTableMetaInfo->pTableMeta->schema;
  setSchema(&pSchema[0], TSDB_DATA_TYPE_TIMESTAMP, 8, "ts", 0);
  setSchema(&pSchema[1], TSDB_DATA_TYPE_INT, 4, "a", 1);
  setSchema(&pSchema[2], TSDB_DATA_TYPE_DOUBLE, 8, "b", 2);
  setSchema(&pSchema[3], TSDB_DATA_TYPE_DOUBLE, 8, "col", 3);

}
}

H
Haojun Liao 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
TEST(testCase, validateAST_test) {
  SSqlInfo info1 = doGenerateAST("select a a1111, a+b + 22, tbname from `t.1abc` where ts<now+2h and `col` < 20 + 99");
  ASSERT_EQ(info1.valid, true);

  char    msg[128] = {0};
  SMsgBuf buf;
  buf.len = 128;
  buf.buf = msg;

  SSqlNode* pNode = (SSqlNode*) taosArrayGetP(((SArray*)info1.list), 0);
  int32_t code = evaluateSqlNode(pNode, TSDB_TIME_PRECISION_NANO, &buf);
  ASSERT_EQ(code, 0);

  SMetaReq req = {0};
  int32_t  ret = qParserExtractRequestedMetaInfo(&info1, &req, msg, 128);
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(taosArrayGetSize(req.pTableName), 1);

  SQueryStmtInfo* pQueryInfo = createQueryInfo();
  setTableMetaInfo(pQueryInfo, &req);

  SSqlNode* pSqlNode = (SSqlNode*)taosArrayGetP(info1.list, 0);
  ret = validateSqlNode(pSqlNode, pQueryInfo, &buf);

  SArray* pExprList = pQueryInfo->exprList;
  ASSERT_EQ(taosArrayGetSize(pExprList), 3);

  SExprInfo* p1 = (SExprInfo*) taosArrayGetP(pExprList, 0);
  ASSERT_EQ(p1->base.uid, 110);
  ASSERT_EQ(p1->base.numOfParams, 0);
  ASSERT_EQ(p1->base.resSchema.type, TSDB_DATA_TYPE_INT);
  ASSERT_STRCASEEQ(p1->base.resSchema.name, "a1111");
  ASSERT_STRCASEEQ(p1->base.colInfo.name, "t.1abc.a");
  ASSERT_EQ(p1->base.colInfo.colId, 1);
  ASSERT_EQ(p1->base.colInfo.flag, TSDB_COL_NORMAL);
  ASSERT_STRCASEEQ(p1->base.token, "a");

  ASSERT_EQ(taosArrayGetSize(pExprList), 3);

  SExprInfo* p2 = (SExprInfo*) taosArrayGetP(pExprList, 1);
  ASSERT_EQ(p2->base.uid, 0);
  ASSERT_EQ(p2->base.numOfParams, 1);  // it is the serialized binary string of expression.
  ASSERT_EQ(p2->base.resSchema.type, TSDB_DATA_TYPE_DOUBLE);
  ASSERT_STRCASEEQ(p2->base.resSchema.name, "a+b + 22");

//  ASSERT_STRCASEEQ(p2->base.colInfo.name, "t.1abc.a");
114 115
//  ASSERT_EQ(p1->base.colInfo.colId, 1);
//  ASSERT_EQ(p1->base.colInfo.flag, TSDB_COL_NORMAL);
H
Haojun Liao 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
  ASSERT_STRCASEEQ(p2->base.token, "a+b + 22");

  ASSERT_EQ(taosArrayGetSize(pQueryInfo->colList), 3);
  ASSERT_EQ(pQueryInfo->fieldsInfo.numOfOutput, 3);

  destroyQueryInfo(pQueryInfo);
  qParserClearupMetaRequestInfo(&req);
  destroySqlInfo(&info1);
}

TEST(testCase, function_Test) {
  SSqlInfo info1 = doGenerateAST("select count(a) from `t.1abc`");
  ASSERT_EQ(info1.valid, true);

  char    msg[128] = {0};
  SMsgBuf buf;
  buf.len = 128;
  buf.buf = msg;

  SSqlNode* pNode = (SSqlNode*) taosArrayGetP(((SArray*)info1.list), 0);
  int32_t code = evaluateSqlNode(pNode, TSDB_TIME_PRECISION_NANO, &buf);
  ASSERT_EQ(code, 0);

  SMetaReq req = {0};
  int32_t  ret = qParserExtractRequestedMetaInfo(&info1, &req, msg, 128);
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(taosArrayGetSize(req.pTableName), 1);

  SQueryStmtInfo* pQueryInfo = createQueryInfo();
  setTableMetaInfo(pQueryInfo, &req);

  SSqlNode* pSqlNode = (SSqlNode*)taosArrayGetP(info1.list, 0);
  ret = validateSqlNode(pSqlNode, pQueryInfo, &buf);

  SArray* pExprList = pQueryInfo->exprList;
  ASSERT_EQ(taosArrayGetSize(pExprList), 1);

  SExprInfo* p1 = (SExprInfo*) taosArrayGetP(pExprList, 0);
  ASSERT_EQ(p1->base.uid, 110);
  ASSERT_EQ(p1->base.numOfParams, 0);
  ASSERT_EQ(p1->base.resSchema.type, TSDB_DATA_TYPE_BIGINT);
  ASSERT_STRCASEEQ(p1->base.resSchema.name, "count(a)");
  ASSERT_STRCASEEQ(p1->base.colInfo.name, "t.1abc.a");
  ASSERT_EQ(p1->base.colInfo.colId, 1);
  ASSERT_EQ(p1->base.colInfo.flag, TSDB_COL_NORMAL);
  ASSERT_STRCASEEQ(p1->base.token, "count(a)");
  ASSERT_EQ(p1->base.interBytes, 8);

  ASSERT_EQ(taosArrayGetSize(pQueryInfo->colList), 2);
  ASSERT_EQ(pQueryInfo->fieldsInfo.numOfOutput, 1);

  destroyQueryInfo(pQueryInfo);
  qParserClearupMetaRequestInfo(&req);
  destroySqlInfo(&info1);
}

TEST(testCase, function_Test2) {
  SSqlInfo info1 = doGenerateAST("select count(a) abc from `t.1abc`");
  ASSERT_EQ(info1.valid, true);

  char    msg[128] = {0};
  SMsgBuf buf;
  buf.len = 128;
  buf.buf = msg;

  SSqlNode* pNode = (SSqlNode*) taosArrayGetP(((SArray*)info1.list), 0);
  int32_t code = evaluateSqlNode(pNode, TSDB_TIME_PRECISION_NANO, &buf);
  ASSERT_EQ(code, 0);

  SMetaReq req = {0};
  int32_t  ret = qParserExtractRequestedMetaInfo(&info1, &req, msg, 128);
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(taosArrayGetSize(req.pTableName), 1);

  SQueryStmtInfo* pQueryInfo = createQueryInfo();
  setTableMetaInfo(pQueryInfo, &req);

  SSqlNode* pSqlNode = (SSqlNode*)taosArrayGetP(info1.list, 0);
  ret = validateSqlNode(pSqlNode, pQueryInfo, &buf);

  SArray* pExprList = pQueryInfo->exprList;
  ASSERT_EQ(taosArrayGetSize(pExprList), 1);

  SExprInfo* p1 = (SExprInfo*) taosArrayGetP(pExprList, 0);
  ASSERT_EQ(p1->base.uid, 110);
  ASSERT_EQ(p1->base.numOfParams, 0);
  ASSERT_EQ(p1->base.resSchema.type, TSDB_DATA_TYPE_BIGINT);
  ASSERT_STRCASEEQ(p1->base.resSchema.name, "abc");
  ASSERT_STRCASEEQ(p1->base.colInfo.name, "t.1abc.a");
  ASSERT_EQ(p1->base.colInfo.colId, 1);
  ASSERT_EQ(p1->base.colInfo.flag, TSDB_COL_NORMAL);
  ASSERT_STRCASEEQ(p1->base.token, "count(a)");
  ASSERT_EQ(p1->base.interBytes, 8);

  ASSERT_EQ(taosArrayGetSize(pQueryInfo->colList), 2);
  ASSERT_EQ(pQueryInfo->fieldsInfo.numOfOutput, 1);

  destroyQueryInfo(pQueryInfo);
  qParserClearupMetaRequestInfo(&req);
  destroySqlInfo(&info1);
}

TEST(testCase, function_Test3) {
  SSqlInfo info1 = doGenerateAST("select first(*) from `t.1abc`");
  ASSERT_EQ(info1.valid, true);

  char    msg[128] = {0};
  SMsgBuf buf;
  buf.len = 128;
  buf.buf = msg;

  SSqlNode* pNode = (SSqlNode*) taosArrayGetP(((SArray*)info1.list), 0);
  int32_t code = evaluateSqlNode(pNode, TSDB_TIME_PRECISION_NANO, &buf);
  ASSERT_EQ(code, 0);

  SMetaReq req = {0};
  int32_t  ret = qParserExtractRequestedMetaInfo(&info1, &req, msg, 128);
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(taosArrayGetSize(req.pTableName), 1);

  SQueryStmtInfo* pQueryInfo = createQueryInfo();
  setTableMetaInfo(pQueryInfo, &req);

  SSqlNode* pSqlNode = (SSqlNode*)taosArrayGetP(info1.list, 0);
  ret = validateSqlNode(pSqlNode, pQueryInfo, &buf);

  SArray* pExprList = pQueryInfo->exprList;
  ASSERT_EQ(taosArrayGetSize(pExprList), 4);

  SExprInfo* p1 = (SExprInfo*) taosArrayGetP(pExprList, 0);
  ASSERT_EQ(p1->base.uid, 110);
  ASSERT_EQ(p1->base.numOfParams, 0);
  ASSERT_EQ(p1->base.resSchema.type, TSDB_DATA_TYPE_TIMESTAMP);
  ASSERT_STRCASEEQ(p1->base.resSchema.name, "first(ts)");
  ASSERT_STRCASEEQ(p1->base.colInfo.name, "t.1abc.ts");
  ASSERT_EQ(p1->base.colInfo.colId, 0);
  ASSERT_EQ(p1->base.colInfo.flag, TSDB_COL_NORMAL);
  ASSERT_STRCASEEQ(p1->base.token, "first(ts)");
  ASSERT_EQ(p1->base.interBytes, 24);

  ASSERT_EQ(pQueryInfo->fieldsInfo.numOfOutput, 4);

  destroyQueryInfo(pQueryInfo);
  qParserClearupMetaRequestInfo(&req);
  destroySqlInfo(&info1);
}

TEST(testCase, function_Test4) {
  SSqlInfo info1 = doGenerateAST("select _block_dist() as a1 from `t.1abc`");
  ASSERT_EQ(info1.valid, true);

  char    msg[128] = {0};
  SMsgBuf buf;
  buf.len = 128;
  buf.buf = msg;

  SSqlNode* pNode = (SSqlNode*) taosArrayGetP(((SArray*)info1.list), 0);
  int32_t code = evaluateSqlNode(pNode, TSDB_TIME_PRECISION_NANO, &buf);
  ASSERT_EQ(code, 0);

  SMetaReq req = {0};
  int32_t  ret = qParserExtractRequestedMetaInfo(&info1, &req, msg, 128);
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(taosArrayGetSize(req.pTableName), 1);

  SQueryStmtInfo* pQueryInfo = createQueryInfo();
  setTableMetaInfo(pQueryInfo, &req);

  SSqlNode* pSqlNode = (SSqlNode*)taosArrayGetP(info1.list, 0);
  ret = validateSqlNode(pSqlNode, pQueryInfo, &buf);

  SArray* pExprList = pQueryInfo->exprList;
  ASSERT_EQ(taosArrayGetSize(pExprList), 1);

  SExprInfo* p1 = (SExprInfo*) taosArrayGetP(pExprList, 0);
  ASSERT_EQ(p1->base.uid, 110);
  ASSERT_EQ(p1->base.numOfParams, 1);
  ASSERT_EQ(p1->base.resSchema.type, TSDB_DATA_TYPE_BINARY);
  ASSERT_STRCASEEQ(p1->base.resSchema.name, "a1");
295 296
//  ASSERT_STRCASEEQ(p1->base.colInfo.name, "t.1abc.ts");
//  ASSERT_EQ(p1->base.colInfo.colId, 0);
H
Haojun Liao 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
  ASSERT_EQ(p1->base.colInfo.flag, TSDB_COL_NORMAL);
  ASSERT_STRCASEEQ(p1->base.token, "_block_dist()");
  ASSERT_EQ(p1->base.interBytes, 0);

  ASSERT_EQ(taosArrayGetSize(pQueryInfo->colList), 1);
  ASSERT_EQ(pQueryInfo->fieldsInfo.numOfOutput, 1);

  destroyQueryInfo(pQueryInfo);
  qParserClearupMetaRequestInfo(&req);
  destroySqlInfo(&info1);
}

TEST(testCase, function_Test5) {
  SSqlInfo info1 = doGenerateAST("select sum(a) + avg(b)  as a1 from `t.1abc`");
  ASSERT_EQ(info1.valid, true);

  char    msg[128] = {0};
  SMsgBuf buf;
  buf.len = 128;
  buf.buf = msg;

  SSqlNode* pNode = (SSqlNode*) taosArrayGetP(((SArray*)info1.list), 0);
  int32_t code = evaluateSqlNode(pNode, TSDB_TIME_PRECISION_NANO, &buf);
  ASSERT_EQ(code, 0);

  SMetaReq req = {0};
  int32_t  ret = qParserExtractRequestedMetaInfo(&info1, &req, msg, 128);
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(taosArrayGetSize(req.pTableName), 1);

  SQueryStmtInfo* pQueryInfo = createQueryInfo();
  setTableMetaInfo(pQueryInfo, &req);

  SSqlNode* pSqlNode = (SSqlNode*)taosArrayGetP(info1.list, 0);
  ret = validateSqlNode(pSqlNode, pQueryInfo, &buf);
  ASSERT_EQ(ret, 0);

  SArray* pExprList = pQueryInfo->exprList;
  ASSERT_EQ(taosArrayGetSize(pExprList), 3);

  SExprInfo* p1 = (SExprInfo*) taosArrayGetP(pExprList, 0);
  ASSERT_EQ(p1->base.uid, 0);
  ASSERT_EQ(p1->base.numOfParams, 1);
  ASSERT_EQ(p1->base.resSchema.type, TSDB_DATA_TYPE_DOUBLE);
  ASSERT_STRCASEEQ(p1->base.resSchema.name, "a1");
//  ASSERT_STRCASEEQ(p1->base.colInfo.name, "t.1abc.ts");
//  ASSERT_EQ(p1->base.colInfo.colId, 0);
  ASSERT_EQ(p1->base.colInfo.flag, TSDB_COL_NORMAL);
  ASSERT_STRCASEEQ(p1->base.token, "sum(a) + avg(b)");
  ASSERT_EQ(p1->base.interBytes, 0);

  ASSERT_EQ(taosArrayGetSize(pQueryInfo->colList), 3);
  ASSERT_EQ(pQueryInfo->fieldsInfo.numOfOutput, 1);

  destroyQueryInfo(pQueryInfo);
  qParserClearupMetaRequestInfo(&req);
  destroySqlInfo(&info1);
}

TEST(testCase, function_Test6) {
  SSqlInfo info1 = doGenerateAST("select sum(a+b) as a1, first(b*a), count(b+b), count(1), count(42.1) from `t.1abc` interval(10s, 1s)");
  ASSERT_EQ(info1.valid, true);

  char    msg[128] = {0};
  SMsgBuf buf;
  buf.len = 128;
  buf.buf = msg;

  SSqlNode* pNode = (SSqlNode*) taosArrayGetP(((SArray*)info1.list), 0);
  int32_t code = evaluateSqlNode(pNode, TSDB_TIME_PRECISION_NANO, &buf);
  ASSERT_EQ(code, 0);

  SMetaReq req = {0};
  int32_t  ret = qParserExtractRequestedMetaInfo(&info1, &req, msg, 128);
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(taosArrayGetSize(req.pTableName), 1);

  SQueryStmtInfo* pQueryInfo = createQueryInfo();
  setTableMetaInfo(pQueryInfo, &req);

  SSqlNode* pSqlNode = (SSqlNode*)taosArrayGetP(info1.list, 0);
  ret = validateSqlNode(pSqlNode, pQueryInfo, &buf);
  ASSERT_EQ(ret, 0);

  SArray* pExprList = pQueryInfo->exprList;
  ASSERT_EQ(taosArrayGetSize(pExprList), 5);

  SExprInfo* p1 = (SExprInfo*) taosArrayGetP(pExprList, 0);
  ASSERT_EQ(p1->base.uid, 110);
  ASSERT_EQ(p1->base.numOfParams, 0);
  ASSERT_EQ(p1->base.resSchema.type, TSDB_DATA_TYPE_DOUBLE);
  ASSERT_STRCASEEQ(p1->base.resSchema.name, "a1");
  ASSERT_EQ(p1->base.colInfo.flag, TSDB_COL_NORMAL);
  ASSERT_STRCASEEQ(p1->base.token, "sum(a+b)");
  ASSERT_EQ(p1->base.interBytes, 16);
  ASSERT_EQ(p1->pExpr->nodeType, TEXPR_UNARYEXPR_NODE);
  ASSERT_EQ(p1->pExpr->_node.functionId, FUNCTION_SUM);
  ASSERT_TRUE(p1->pExpr->_node.pRight == NULL);

  tExprNode* pParam = p1->pExpr->_node.pLeft;

  ASSERT_EQ(pParam->nodeType, TEXPR_BINARYEXPR_NODE);
  ASSERT_EQ(pParam->_node.optr, TSDB_BINARY_OP_ADD);
  ASSERT_EQ(pParam->_node.pLeft->nodeType, TEXPR_COL_NODE);
  ASSERT_EQ(pParam->_node.pRight->nodeType, TEXPR_COL_NODE);

  ASSERT_EQ(taosArrayGetSize(pQueryInfo->colList), 3);
  ASSERT_EQ(pQueryInfo->fieldsInfo.numOfOutput, 5);

  SExprInfo* p2 = (SExprInfo*) taosArrayGetP(pExprList, 1);
  ASSERT_EQ(p2->base.uid, 110);
  ASSERT_EQ(p2->base.numOfParams, 0);
  ASSERT_EQ(p2->base.resSchema.type, TSDB_DATA_TYPE_DOUBLE);
  ASSERT_STRCASEEQ(p2->base.resSchema.name, "first(b*a)");
  ASSERT_EQ(p2->base.colInfo.flag, TSDB_COL_NORMAL);
  ASSERT_STRCASEEQ(p2->base.token, "first(b*a)");
  ASSERT_EQ(p2->base.interBytes, 24);
  ASSERT_EQ(p2->pExpr->nodeType, TEXPR_UNARYEXPR_NODE);
  ASSERT_EQ(p2->pExpr->_node.functionId, FUNCTION_FIRST);
  ASSERT_TRUE(p2->pExpr->_node.pRight == NULL);

  destroyQueryInfo(pQueryInfo);
  qParserClearupMetaRequestInfo(&req);
  destroySqlInfo(&info1);
}

TEST(testCase, function_Test7) {
  SSqlInfo info1 = doGenerateAST("select count(a+b),count(1) from `t.1abc` interval(10s, 1s)");
  ASSERT_EQ(info1.valid, true);

  char    msg[128] = {0};
  SMsgBuf buf;
  buf.len = 128;
  buf.buf = msg;

  SSqlNode* pNode = (SSqlNode*) taosArrayGetP(((SArray*)info1.list), 0);
  int32_t code = evaluateSqlNode(pNode, TSDB_TIME_PRECISION_NANO, &buf);
  ASSERT_EQ(code, 0);

  SMetaReq req = {0};
  int32_t  ret = qParserExtractRequestedMetaInfo(&info1, &req, msg, 128);
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(taosArrayGetSize(req.pTableName), 1);

  SQueryStmtInfo* pQueryInfo = createQueryInfo();
  setTableMetaInfo(pQueryInfo, &req);

  SSqlNode* pSqlNode = (SSqlNode*)taosArrayGetP(info1.list, 0);
  ret = validateSqlNode(pSqlNode, pQueryInfo, &buf);
  ASSERT_EQ(ret, 0);

  SArray* pExprList = pQueryInfo->exprList;
  ASSERT_EQ(taosArrayGetSize(pExprList), 2);

  SExprInfo* p1 = (SExprInfo*) taosArrayGetP(pExprList, 0);
  ASSERT_EQ(p1->base.uid, 110);
  ASSERT_EQ(p1->base.numOfParams, 0);
  ASSERT_EQ(p1->base.resSchema.type, TSDB_DATA_TYPE_BIGINT);
  ASSERT_STRCASEEQ(p1->base.resSchema.name, "count(a+b)");
  ASSERT_EQ(p1->base.colInfo.flag, TSDB_COL_NORMAL);
  ASSERT_STRCASEEQ(p1->base.token, "count(a+b)");
  ASSERT_EQ(p1->base.interBytes, 8);
  ASSERT_EQ(p1->pExpr->nodeType, TEXPR_UNARYEXPR_NODE);
  ASSERT_EQ(p1->pExpr->_node.functionId, FUNCTION_COUNT);
  ASSERT_TRUE(p1->pExpr->_node.pRight == NULL);

  tExprNode* pParam = p1->pExpr->_node.pLeft;

  ASSERT_EQ(pParam->nodeType, TEXPR_BINARYEXPR_NODE);
  ASSERT_EQ(pParam->_node.optr, TSDB_BINARY_OP_ADD);
  ASSERT_EQ(pParam->_node.pLeft->nodeType, TEXPR_COL_NODE);
  ASSERT_EQ(pParam->_node.pRight->nodeType, TEXPR_COL_NODE);

  ASSERT_EQ(taosArrayGetSize(pQueryInfo->colList), 3);
  ASSERT_EQ(pQueryInfo->fieldsInfo.numOfOutput, 2);

  destroyQueryInfo(pQueryInfo);
  qParserClearupMetaRequestInfo(&req);
  destroySqlInfo(&info1);
}
477 478 479

TEST(testCase, function_Test8) {
  SSqlInfo info1 = doGenerateAST("select top(a*b / 99, 20) from `t.1abc` interval(10s, 1s)");
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
  ASSERT_EQ(info1.valid, true);

  char    msg[128] = {0};
  SMsgBuf buf;
  buf.len = 128;
  buf.buf = msg;

  SSqlNode* pNode = (SSqlNode*) taosArrayGetP(((SArray*)info1.list), 0);
  int32_t code = evaluateSqlNode(pNode, TSDB_TIME_PRECISION_NANO, &buf);
  ASSERT_EQ(code, 0);

  SMetaReq req = {0};
  int32_t  ret = qParserExtractRequestedMetaInfo(&info1, &req, msg, 128);
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(taosArrayGetSize(req.pTableName), 1);

  SQueryStmtInfo* pQueryInfo = createQueryInfo();
  setTableMetaInfo(pQueryInfo, &req);

  SSqlNode* pSqlNode = (SSqlNode*)taosArrayGetP(info1.list, 0);
  ret = validateSqlNode(pSqlNode, pQueryInfo, &buf);
  ASSERT_EQ(ret, 0);

  SArray* pExprList = pQueryInfo->exprList;
504
  ASSERT_EQ(taosArrayGetSize(pExprList), 2);
505

506
  SExprInfo* p1 = (SExprInfo*) taosArrayGetP(pExprList, 1);
507
  ASSERT_EQ(p1->base.uid, 110);
508 509 510
  ASSERT_EQ(p1->base.numOfParams, 1);
  ASSERT_EQ(p1->base.resSchema.type, TSDB_DATA_TYPE_DOUBLE);
  ASSERT_STRCASEEQ(p1->base.resSchema.name, "top(a*b / 99, 20)");
511
  ASSERT_EQ(p1->base.colInfo.flag, TSDB_COL_NORMAL);
512 513
  ASSERT_STRCASEEQ(p1->base.token, "top(a*b / 99, 20)");
  ASSERT_EQ(p1->base.interBytes, 16);
514

515 516 517
  ASSERT_EQ(p1->pExpr->nodeType, TEXPR_UNARYEXPR_NODE);
  ASSERT_EQ(p1->pExpr->_node.functionId, FUNCTION_TOP);
  ASSERT_TRUE(p1->pExpr->_node.pRight == NULL);
518

519
  tExprNode* pParam = p1->pExpr->_node.pLeft;
520

521 522 523 524
  ASSERT_EQ(pParam->nodeType, TEXPR_BINARYEXPR_NODE);
  ASSERT_EQ(pParam->_node.optr, TSDB_BINARY_OP_DIVIDE);
  ASSERT_EQ(pParam->_node.pLeft->nodeType, TEXPR_BINARYEXPR_NODE);
  ASSERT_EQ(pParam->_node.pRight->nodeType, TEXPR_VALUE_NODE);
525

526 527
  ASSERT_EQ(taosArrayGetSize(pQueryInfo->colList), 3);
  ASSERT_EQ(pQueryInfo->fieldsInfo.numOfOutput, 2);
528 529 530 531 532 533

  destroyQueryInfo(pQueryInfo);
  qParserClearupMetaRequestInfo(&req);
  destroySqlInfo(&info1);
}

534
TEST(testCase, invalid_sql_Test) {
535 536 537 538 539
  char    msg[128] = {0};
  SMsgBuf buf;
  buf.len = 128;
  buf.buf = msg;

540
  SSqlInfo info1 = doGenerateAST("select count(k) from `t.1abc` interval(10s, 1s)");
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
  ASSERT_EQ(info1.valid, true);

  SSqlNode* pNode = (SSqlNode*) taosArrayGetP(((SArray*)info1.list), 0);
  int32_t code = evaluateSqlNode(pNode, TSDB_TIME_PRECISION_NANO, &buf);
  ASSERT_EQ(code, 0);

  SMetaReq req = {0};
  int32_t  ret = qParserExtractRequestedMetaInfo(&info1, &req, msg, 128);
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(taosArrayGetSize(req.pTableName), 1);

  SQueryStmtInfo* pQueryInfo = createQueryInfo();
  setTableMetaInfo(pQueryInfo, &req);

  SSqlNode* pSqlNode = (SSqlNode*)taosArrayGetP(info1.list, 0);
  ret = validateSqlNode(pSqlNode, pQueryInfo, &buf);
557
  ASSERT_NE(ret, 0);
558 559 560 561

  destroyQueryInfo(pQueryInfo);
  qParserClearupMetaRequestInfo(&req);
  destroySqlInfo(&info1);
562 563
//===============================================================================================================
  info1 = doGenerateAST("select top(a*b, ABC) from `t.1abc` interval(10s, 1s)");
564 565
  ASSERT_EQ(info1.valid, true);

566 567
  pNode = (SSqlNode*) taosArrayGetP(((SArray*)info1.list), 0);
  code = evaluateSqlNode(pNode, TSDB_TIME_PRECISION_NANO, &buf);
568 569
  ASSERT_EQ(code, 0);

570
  ret = qParserExtractRequestedMetaInfo(&info1, &req, msg, 128);
571 572 573
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(taosArrayGetSize(req.pTableName), 1);

574
  pQueryInfo = createQueryInfo();
575 576
  setTableMetaInfo(pQueryInfo, &req);

577
  pSqlNode = (SSqlNode*)taosArrayGetP(info1.list, 0);
578
  ret = validateSqlNode(pSqlNode, pQueryInfo, &buf);
579
  ASSERT_NE(ret, 0);
580

H
Haojun Liao 已提交
581 582 583
  destroyQueryInfo(pQueryInfo);
  qParserClearupMetaRequestInfo(&req);
  destroySqlInfo(&info1);
584
}