2017-03-07 02:01:00 +01:00
|
|
|
#include "thirdparty/microtest/microtest.h"
|
2017-04-07 15:47:51 +02:00
|
|
|
|
2017-03-07 02:01:00 +01:00
|
|
|
#include "SQLParser.h"
|
2017-04-07 15:47:51 +02:00
|
|
|
#include "util/sqlhelper.h"
|
|
|
|
|
|
|
|
#include "sql_asserts.h"
|
2017-03-07 02:01:00 +01:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <fstream>
|
|
|
|
#include <streambuf>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using namespace hsql;
|
|
|
|
|
2017-03-08 16:55:29 +01:00
|
|
|
std::string readFileContents(std::string file_path) {
|
|
|
|
std::ifstream t(file_path.c_str());
|
|
|
|
std::string text((std::istreambuf_iterator<char>(t)),
|
|
|
|
std::istreambuf_iterator<char>());
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2017-03-07 15:30:54 +01:00
|
|
|
TEST(TPCHQueryGrammarTests) {
|
2017-03-07 02:01:00 +01:00
|
|
|
std::vector<std::string> files = {
|
|
|
|
"test/queries/tpc-h-01.sql",
|
|
|
|
"test/queries/tpc-h-02.sql",
|
|
|
|
"test/queries/tpc-h-03.sql",
|
|
|
|
"test/queries/tpc-h-04.sql",
|
|
|
|
"test/queries/tpc-h-05.sql",
|
|
|
|
"test/queries/tpc-h-06.sql",
|
|
|
|
"test/queries/tpc-h-07.sql",
|
|
|
|
"test/queries/tpc-h-08.sql",
|
|
|
|
"test/queries/tpc-h-09.sql",
|
2017-03-07 15:30:54 +01:00
|
|
|
"test/queries/tpc-h-10.sql",
|
2017-07-21 02:47:45 +02:00
|
|
|
"test/queries/tpc-h-11.sql",
|
|
|
|
"test/queries/tpc-h-12.sql",
|
|
|
|
"test/queries/tpc-h-13.sql",
|
|
|
|
"test/queries/tpc-h-14.sql",
|
|
|
|
"test/queries/tpc-h-15.sql",
|
|
|
|
"test/queries/tpc-h-16.sql",
|
|
|
|
"test/queries/tpc-h-17.sql",
|
|
|
|
"test/queries/tpc-h-18.sql",
|
|
|
|
"test/queries/tpc-h-19.sql",
|
|
|
|
"test/queries/tpc-h-20.sql",
|
|
|
|
"test/queries/tpc-h-21.sql",
|
|
|
|
"test/queries/tpc-h-22.sql",
|
2017-03-07 02:01:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
int testsFailed = 0;
|
2017-07-21 02:47:45 +02:00
|
|
|
std::string concatenated = "";
|
2017-03-07 02:01:00 +01:00
|
|
|
for (const std::string& file_path : files) {
|
2017-03-08 16:55:29 +01:00
|
|
|
std::string query = readFileContents(file_path);
|
2017-03-07 13:49:56 +01:00
|
|
|
|
2017-07-21 02:47:45 +02:00
|
|
|
concatenated += query;
|
|
|
|
if (concatenated.back() != ';') concatenated += ';';
|
|
|
|
|
2017-04-07 16:28:33 +02:00
|
|
|
SQLParserResult result;
|
2017-07-21 02:47:45 +02:00
|
|
|
SQLParser::parse(query.c_str(), &result);
|
2017-04-07 16:28:33 +02:00
|
|
|
if (!result.isValid()) {
|
2017-03-07 02:01:00 +01:00
|
|
|
mt::printFailed(file_path.c_str());
|
2017-04-07 16:28:33 +02:00
|
|
|
printf("%s %s (L%d:%d)%s\n", mt::red(), result.errorMsg(), result.errorLine(), result.errorColumn(), mt::def());
|
2017-03-07 02:01:00 +01:00
|
|
|
++testsFailed;
|
|
|
|
} else {
|
|
|
|
mt::printOk(file_path.c_str());
|
|
|
|
}
|
|
|
|
}
|
2017-07-21 02:47:45 +02:00
|
|
|
|
|
|
|
SQLParserResult result;
|
|
|
|
SQLParser::parse(concatenated.c_str(), &result);
|
|
|
|
if (!result.isValid()) {
|
|
|
|
mt::printFailed("TPCHAllConcatenated");
|
|
|
|
printf("%s %s (L%d:%d)%s\n", mt::red(), result.errorMsg(), result.errorLine(), result.errorColumn(), mt::def());
|
|
|
|
++testsFailed;
|
|
|
|
} else {
|
|
|
|
mt::printOk("TPCHAllConcatenated");
|
|
|
|
}
|
|
|
|
|
2017-03-07 02:01:00 +01:00
|
|
|
ASSERT_EQ(testsFailed, 0);
|
|
|
|
}
|
2017-03-08 16:55:29 +01:00
|
|
|
|
|
|
|
TEST(TPCHQueryDetailTest) {
|
2017-07-21 02:47:45 +02:00
|
|
|
std::string query = readFileContents("test/queries/tpc-h-20.sql");
|
2017-03-08 16:55:29 +01:00
|
|
|
|
2017-04-07 16:28:33 +02:00
|
|
|
SQLParserResult result;
|
2017-07-21 02:47:45 +02:00
|
|
|
SQLParser::parse(query.c_str(), &result);
|
2017-04-07 16:28:33 +02:00
|
|
|
ASSERT(result.isValid());
|
2017-07-21 02:47:45 +02:00
|
|
|
ASSERT_EQ(result.size(), 1);
|
2017-03-08 16:55:29 +01:00
|
|
|
|
2017-07-21 02:47:45 +02:00
|
|
|
const SQLStatement* stmt20 = result.getStatement(0);
|
2017-03-08 16:55:29 +01:00
|
|
|
ASSERT_EQ(stmt20->type(), kStmtSelect);
|
|
|
|
|
|
|
|
const SelectStatement* select20 = (const SelectStatement*) stmt20;
|
|
|
|
ASSERT_EQ(select20->selectList->size(), 2);
|
|
|
|
ASSERT_STREQ(select20->selectList->at(0)->getName(), "S_NAME");
|
|
|
|
ASSERT_STREQ(select20->selectList->at(1)->getName(), "S_ADDRESS");
|
|
|
|
|
|
|
|
// Test WHERE Clause.
|
|
|
|
Expr* where = select20->whereClause;
|
|
|
|
ASSERT_NOTNULL(where);
|
|
|
|
ASSERT(where->isType(kExprOperator));
|
2017-04-06 17:25:47 +02:00
|
|
|
ASSERT_EQ(where->opType, kOpAnd);
|
2017-03-08 16:55:29 +01:00
|
|
|
|
|
|
|
Expr* andExpr2 = where->expr;
|
|
|
|
ASSERT_NOTNULL(andExpr2);
|
|
|
|
ASSERT(andExpr2->isType(kExprOperator));
|
2017-04-06 17:25:47 +02:00
|
|
|
ASSERT_EQ(andExpr2->opType, kOpAnd);
|
2017-03-08 16:55:29 +01:00
|
|
|
|
|
|
|
// Test IN expression.
|
|
|
|
Expr* inExpr = andExpr2->expr;
|
|
|
|
ASSERT_NOTNULL(inExpr);
|
|
|
|
ASSERT(inExpr->isType(kExprOperator));
|
2017-04-06 17:25:47 +02:00
|
|
|
ASSERT_EQ(inExpr->opType, kOpIn);
|
2017-03-08 16:55:29 +01:00
|
|
|
|
|
|
|
ASSERT_STREQ(inExpr->expr->getName(), "S_SUPPKEY");
|
|
|
|
ASSERT_NOTNULL(inExpr->select);
|
|
|
|
ASSERT_EQ(inExpr->select->selectList->size(), 1);
|
|
|
|
ASSERT(inExpr->select->selectList->at(0)->isType(kExprColumnRef));
|
|
|
|
ASSERT_STREQ(inExpr->select->selectList->at(0)->getName(), "PS_SUPPKEY");
|
|
|
|
|
|
|
|
// Test ORDER BY clause.
|
|
|
|
ASSERT_NOTNULL(select20->order);
|
|
|
|
ASSERT_EQ(select20->order->size(), 1);
|
|
|
|
ASSERT(select20->order->at(0)->expr->isType(kExprColumnRef));
|
|
|
|
ASSERT_STREQ(select20->order->at(0)->expr->getName(), "S_NAME");
|
2017-04-07 16:28:33 +02:00
|
|
|
}
|