newParserTest.cpp 13.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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/>.
 */

#include <gtest/gtest.h>

18
#include "parserImpl.h"
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

using namespace std;
using namespace testing;

class NewParserTest : public Test {
protected:
  void setDatabase(const string& acctId, const string& db) {
    acctId_ = acctId;
    db_ = db;
  }

  void bind(const char* sql) {
    reset();
    cxt_.acctId = atoi(acctId_.c_str());
    cxt_.db = (char*) db_.c_str();
    strcpy(sqlBuf_, sql);
    cxt_.sqlLen = strlen(sql);
    sqlBuf_[cxt_.sqlLen] = '\0';
    cxt_.pSql = sqlBuf_;

  }

41
  bool run(int32_t parseCode = TSDB_CODE_SUCCESS, int32_t translateCode = TSDB_CODE_SUCCESS) {
42
    int32_t code = doParse(&cxt_, &query_);
43
    // cout << "doParse return " << code << endl;
44
    if (code != TSDB_CODE_SUCCESS) {
45
      cout << "sql:[" << cxt_.pSql << "] code:" << tstrerror(code) << ", msg:" << errMagBuf_ << endl;
46 47 48 49 50 51 52 53 54 55
      return (TSDB_CODE_SUCCESS != parseCode);
    }
    if (TSDB_CODE_SUCCESS != parseCode) {
      return false;
    }
    code = doTranslate(&cxt_, &query_);
    // cout << "doTranslate return " << code << endl;
    if (code != TSDB_CODE_SUCCESS) {
      cout << "sql:[" << cxt_.pSql << "] code:" << tstrerror(code) << ", msg:" << errMagBuf_ << endl;
      return (TSDB_CODE_SUCCESS != translateCode);
56 57
    }
    if (NULL != query_.pRoot && QUERY_NODE_SELECT_STMT == nodeType(query_.pRoot)) {
58
      cout << "input sql : [" << cxt_.pSql << "]" << endl;
59 60 61 62 63 64
      // string sql;
      // selectToSql(query_.pRoot, sql);
      // cout << "output sql : [" << sql << "]" << endl;
      string str;
      selectToStr(query_.pRoot, str);
      cout << "translate str : \n" << str << endl;
65
    }
66
    return (TSDB_CODE_SUCCESS == translateCode);
67 68 69 70 71 72
  }

private:
  static const int max_err_len = 1024;
  static const int max_sql_len = 1024 * 1024;

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 114 115 116 117 118 119 120
  string dataTypeToStr(const SDataType& dt) {
    switch (dt.type) {
      case TSDB_DATA_TYPE_NULL:
        return "NULL";
      case TSDB_DATA_TYPE_BOOL:
        return "BOOL";
      case TSDB_DATA_TYPE_TINYINT:
        return "TINYINT";
      case TSDB_DATA_TYPE_SMALLINT:
        return "SMALLINT";
      case TSDB_DATA_TYPE_INT:
        return "INT";
      case TSDB_DATA_TYPE_BIGINT:
        return "BIGINT";
      case TSDB_DATA_TYPE_FLOAT:
        return "FLOAT";
      case TSDB_DATA_TYPE_DOUBLE:
        return "DOUBLE";
      case TSDB_DATA_TYPE_BINARY:
        return "BINART(" + to_string(dt.bytes) + ")";
      case TSDB_DATA_TYPE_TIMESTAMP:
        return "TIMESTAMP";
      case TSDB_DATA_TYPE_NCHAR:
        return "NCHAR(" + to_string(dt.bytes) + ")";
      case TSDB_DATA_TYPE_UTINYINT:
        return "UTINYINT";
      case TSDB_DATA_TYPE_USMALLINT:
        return "USMALLINT";
      case TSDB_DATA_TYPE_UINT:
        return "UINT";
      case TSDB_DATA_TYPE_UBIGINT:
        return "UBIGINT";
      case TSDB_DATA_TYPE_VARCHAR:
        return "VARCHAR(" + to_string(dt.bytes) + ")";
      case TSDB_DATA_TYPE_VARBINARY:
        return "VARBINARY(" + to_string(dt.bytes) + ")";
      case TSDB_DATA_TYPE_JSON:
        return "JSON";
      case TSDB_DATA_TYPE_DECIMAL:
        return "DECIMAL(" + to_string(dt.precision) + ", "  + to_string(dt.scale) + ")";
      case TSDB_DATA_TYPE_BLOB:
        return "BLOB";
      default:
        break;
    }
    return "Unknown Data Type " + to_string(dt.type);
  }

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
  void valueNodeToStr(const SValueNode* pVal, string& str, bool isProject) {
    switch (pVal->node.resType.type) {
      case TSDB_DATA_TYPE_NULL:
        str.append("null");
        break;
      case TSDB_DATA_TYPE_BOOL:
        str.append(pVal->datum.b ? "true" : "false");
        break;
      case TSDB_DATA_TYPE_TINYINT:
      case TSDB_DATA_TYPE_SMALLINT:
      case TSDB_DATA_TYPE_INT:
      case TSDB_DATA_TYPE_BIGINT:
        str.append(to_string(pVal->datum.i));
        break;
      case TSDB_DATA_TYPE_FLOAT:
      case TSDB_DATA_TYPE_DOUBLE:
        str.append(to_string(pVal->datum.d));
        break;
      case TSDB_DATA_TYPE_BINARY:
      case TSDB_DATA_TYPE_NCHAR:
      case TSDB_DATA_TYPE_VARCHAR:
      case TSDB_DATA_TYPE_VARBINARY:
        str.append(pVal->datum.p);
        break;
      case TSDB_DATA_TYPE_TIMESTAMP:
        str.append(to_string(pVal->datum.u));
        break;
      case TSDB_DATA_TYPE_UTINYINT:
      case TSDB_DATA_TYPE_USMALLINT:
      case TSDB_DATA_TYPE_UINT:
      case TSDB_DATA_TYPE_UBIGINT:
        str.append(to_string(pVal->datum.u));
        break;
      case TSDB_DATA_TYPE_JSON:
      case TSDB_DATA_TYPE_DECIMAL:
      case TSDB_DATA_TYPE_BLOB:
        str.append("JSON or DECIMAL or BLOB");
        break;
      default:
        break;
    }
    str.append(" [" + dataTypeToStr(pVal->node.resType) + "]");
    if (isProject) {
      str.append(" AS " + string(pVal->node.aliasName));
    }
  }

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
  void nodeToStr(const SNode* node, string& str, bool isProject) {
    if (nullptr == node) {
      return;
    }

    switch (nodeType(node)) {
      case QUERY_NODE_COLUMN: {
        SColumnNode* pCol = (SColumnNode*)node;
        if ('\0' != pCol->dbName[0]) {
          str.append(pCol->dbName);
          str.append(".");
        }
        if ('\0' != pCol->tableAlias[0]) {
          str.append(pCol->tableAlias);
          str.append(".");
        }
        str.append(pCol->colName);
        str.append(" [" + dataTypeToStr(pCol->node.resType) + "]");
        if (isProject) {
          str.append(" AS " + string(pCol->node.aliasName));
        }
        break;
      }
      case QUERY_NODE_VALUE: {
192
        valueNodeToStr((SValueNode*)node, str, isProject);
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
        break;
      }
      case QUERY_NODE_OPERATOR: {
        SOperatorNode* pOp = (SOperatorNode*)node;
        nodeToStr(pOp->pLeft, str, false);
        str.append(opTypeToStr(pOp->opType));
        nodeToStr(pOp->pRight, str, false);
        str.append(" [" + dataTypeToStr(pOp->node.resType) + "]");
        if (isProject) {
          str.append(" AS " + string(pOp->node.aliasName));
        }
        break;
      }
      default:
        break;
    }
  }

  void nodeListToStr(const SNodeList* nodelist, const string& prefix, string& str, bool isProject = false) {
    SNode* node = nullptr;
    FOREACH(node, nodelist) {
      str.append(prefix);
      nodeToStr(node, str, isProject);
      str.append("\n");
    }
  }

  void tableToStr(const SNode* node, const string& prefix, string& str) {
    const STableNode* table = (const STableNode*)node;
    switch (nodeType(node)) {
      case QUERY_NODE_REAL_TABLE: {
        SRealTableNode* realTable = (SRealTableNode*)table;
        str.append(prefix);
        if ('\0' != realTable->table.dbName[0]) {
          str.append(realTable->table.dbName);
          str.append(".");
        }
        str.append(realTable->table.tableName);
        str.append(string(" ") + realTable->table.tableAlias);
        break;
      }
      case QUERY_NODE_TEMP_TABLE: {
        STempTableNode* tempTable = (STempTableNode*)table;
        str.append(prefix + "(\n");
        selectToStr(tempTable->pSubquery, str, prefix + "\t");
        str.append("\n");
        str.append(prefix + ") ");
        str.append(tempTable->table.tableAlias);
        break;
      }
      case QUERY_NODE_JOIN_TABLE: {
        SJoinTableNode* joinTable = (SJoinTableNode*)table;
        tableToStr(joinTable->pLeft, prefix, str);
        str.append("\n" + prefix + "JOIN\n");
        tableToStr(joinTable->pRight, prefix, str);
        if (nullptr != joinTable->pOnCond) {
          str.append("\n" + prefix + "\tON ");
          nodeToStr(joinTable->pOnCond, str, false);
        }
        break;
      }
      default:
        break;
    }
  }

  void selectToStr(const SNode* node, string& str, const string& prefix = "") {
    SSelectStmt* select = (SSelectStmt*)node;
    str.append(prefix + "SELECT ");
    if (select->isDistinct) {
      str.append("DISTINCT");
    }
    str.append("\n");
    nodeListToStr(select->pProjectionList, prefix + "\t", str, true);
    str.append("\n" + prefix + "FROM\n");
    tableToStr(select->pFromTable, prefix + "\t", str);
  }

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
  void selectToSql(const SNode* node, string& sql) {
    SSelectStmt* select = (SSelectStmt*)node;
    sql.append("SELECT ");
    if (select->isDistinct) {
      sql.append("DISTINCT ");
    }
    if (nullptr == select->pProjectionList) {
      sql.append("* ");
    } else {
      nodeListToSql(select->pProjectionList, sql);
      sql.append(" ");
    }
    sql.append("FROM ");
    tableToSql(select->pFromTable, sql);
    if (nullptr != select->pWhere) {
      sql.append(" WHERE ");
      nodeToSql(select->pWhere, sql);
    }
  }

291 292 293 294 295
  void tableToSql(const SNode* node, string& sql) {
    const STableNode* table = (const STableNode*)node;
    switch (nodeType(node)) {
      case QUERY_NODE_REAL_TABLE: {
        SRealTableNode* realTable = (SRealTableNode*)table;
296 297
        if ('\0' != realTable->table.dbName[0]) {
          sql.append(realTable->table.dbName);
298 299 300 301 302
          sql.append(".");
        }
        sql.append(realTable->table.tableName);
        break;
      }
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
      case QUERY_NODE_TEMP_TABLE: {
        STempTableNode* tempTable = (STempTableNode*)table;
        sql.append("(");
        selectToSql(tempTable->pSubquery, sql);
        sql.append(") ");
        sql.append(tempTable->table.tableAlias);
        break;
      }
      case QUERY_NODE_JOIN_TABLE: {
        SJoinTableNode* joinTable = (SJoinTableNode*)table;
        tableToSql(joinTable->pLeft, sql);
        sql.append(" JOIN ");
        tableToSql(joinTable->pRight, sql);
        if (nullptr != joinTable->pOnCond) {
          sql.append(" ON ");
          nodeToSql(joinTable->pOnCond, sql);
        }
        break;
      }
      default:
        break;
    }
  }

327
  string opTypeToStr(EOperatorType type) {
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
    switch (type) {
      case OP_TYPE_ADD:
        return " + ";
      case OP_TYPE_SUB:
        return " - ";
      case OP_TYPE_MULTI:
      case OP_TYPE_DIV:
      case OP_TYPE_MOD:
      case OP_TYPE_GREATER_THAN:
      case OP_TYPE_GREATER_EQUAL:
      case OP_TYPE_LOWER_THAN:
      case OP_TYPE_LOWER_EQUAL:
      case OP_TYPE_EQUAL:
        return " = ";
      case OP_TYPE_NOT_EQUAL:
      case OP_TYPE_IN:
      case OP_TYPE_NOT_IN:
      case OP_TYPE_LIKE:
      case OP_TYPE_NOT_LIKE:
      case OP_TYPE_MATCH:
      case OP_TYPE_NMATCH:
      case OP_TYPE_JSON_GET_VALUE:
      case OP_TYPE_JSON_CONTAINS:
      default:
        break;
    }
    return " unknown operator ";
  }

  void nodeToSql(const SNode* node, string& sql) {
    if (nullptr == node) {
      return;
    }

    switch (nodeType(node)) {
      case QUERY_NODE_COLUMN: {
        SColumnNode* pCol = (SColumnNode*)node;
        if ('\0' != pCol->dbName[0]) {
          sql.append(pCol->dbName);
          sql.append(".");
        }
        if ('\0' != pCol->tableAlias[0]) {
          sql.append(pCol->tableAlias);
          sql.append(".");
        }
        sql.append(pCol->colName);
        break;
      }
      case QUERY_NODE_VALUE:
        break;
      case QUERY_NODE_OPERATOR: {
        SOperatorNode* pOp = (SOperatorNode*)node;
        nodeToSql(pOp->pLeft, sql);
381
        sql.append(opTypeToStr(pOp->opType));
382 383 384
        nodeToSql(pOp->pRight, sql);
        break;
      }
385 386 387 388 389 390 391 392 393 394 395 396
      default:
        break;
    }
  }

  void nodeListToSql(const SNodeList* nodelist, string& sql, const string& seq = ",") {
    SNode* node = nullptr;
    bool firstNode = true;
    FOREACH(node, nodelist) {
      if (!firstNode) {
        sql.append(", ");
      }
397
      firstNode = false;
398
      nodeToSql(node, sql);
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
    }
  }

  void reset() {
    memset(&cxt_, 0, sizeof(cxt_));
    memset(errMagBuf_, 0, max_err_len);
    cxt_.pMsg = errMagBuf_;
    cxt_.msgLen = max_err_len;
  }

  string acctId_;
  string db_;
  char errMagBuf_[max_err_len];
  char sqlBuf_[max_sql_len];
  SParseContext cxt_;
  SQuery query_;
};

417
TEST_F(NewParserTest, selectSimple) {
418 419 420
  setDatabase("root", "test");

  bind("SELECT * FROM t1");
421
  ASSERT_TRUE(run());
422 423

  bind("SELECT * FROM test.t1");
424
  ASSERT_TRUE(run());
425

426
  bind("SELECT ts, c1 FROM t1");
427 428
  ASSERT_TRUE(run());

429 430 431 432
  bind("SELECT ts, t.c1 FROM (SELECT * FROM t1) t");
  ASSERT_TRUE(run());

  bind("SELECT * FROM t1 tt1, t1 tt2 WHERE tt1.c1 = tt2.c1");
433
  ASSERT_TRUE(run());
434 435
}

436 437 438 439 440 441 442 443 444 445
TEST_F(NewParserTest, selectConstant) {
  setDatabase("root", "test");

  bind("SELECT 123, 20.4, 'abc', \"wxy\", TIMESTAMP '2022-02-09 17:30:20', true, false, 10s FROM t1");
  ASSERT_TRUE(run());

  bind("SELECT 1234567890123456789012345678901234567890, 20.1234567890123456789012345678901234567890, 'abc', \"wxy\", TIMESTAMP '2022-02-09 17:30:20', true, false, 15s FROM t1");
  ASSERT_TRUE(run());
}

446 447 448
TEST_F(NewParserTest, selectExpression) {
  setDatabase("root", "test");

449
  bind("SELECT ts + 10s, c1 + 10, concat(c2, 'abc') FROM t1");
450 451 452 453
  ASSERT_TRUE(run());
}

TEST_F(NewParserTest, selectSyntaxError) {
454 455 456 457 458 459 460 461 462 463 464 465 466
  setDatabase("root", "test");

  bind("SELECTT * FROM t1");
  ASSERT_TRUE(run(TSDB_CODE_FAILED));

  bind("SELECT *");
  ASSERT_TRUE(run(TSDB_CODE_FAILED));

  bind("SELECT *, * FROM test.t1");
  ASSERT_TRUE(run(TSDB_CODE_FAILED));

  bind("SELECT * FROM test.t1 t WHER");
  ASSERT_TRUE(run(TSDB_CODE_FAILED));
467
}
468

469
TEST_F(NewParserTest, selectSemanticError) {
470 471 472 473 474 475 476 477 478 479 480
  setDatabase("root", "test");

  bind("SELECT * FROM t10");
  ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_FAILED));

  bind("SELECT c1, c3 FROM t1");
  ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_FAILED));

  bind("SELECT c2 FROM t1 tt1, t1 tt2 WHERE tt1.c1 = tt2.c1");
  ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_FAILED));
}