2019-05-16 19:57:20 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include <Storages/transformQueryForExternalDatabase.h>
|
|
|
|
#include <Parsers/ParserSelectQuery.h>
|
|
|
|
#include <Parsers/parseQuery.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Databases/DatabaseMemory.h>
|
|
|
|
#include <Storages/StorageMemory.h>
|
2019-05-16 21:09:06 +00:00
|
|
|
#include <Functions/registerFunctions.h>
|
2019-05-16 19:57:20 +00:00
|
|
|
|
|
|
|
|
2019-05-16 21:09:06 +00:00
|
|
|
using namespace DB;
|
2019-05-16 19:57:20 +00:00
|
|
|
|
2019-05-16 21:09:06 +00:00
|
|
|
|
2019-05-18 22:19:24 +00:00
|
|
|
/// NOTE How to do better?
|
|
|
|
struct State
|
|
|
|
{
|
|
|
|
Context context{Context::createGlobal()};
|
|
|
|
NamesAndTypesList columns{{"column", std::make_shared<DataTypeUInt8>()}};
|
|
|
|
|
|
|
|
State()
|
|
|
|
{
|
|
|
|
registerFunctions();
|
|
|
|
DatabasePtr database = std::make_shared<DatabaseMemory>("test");
|
2019-07-09 18:49:13 +00:00
|
|
|
database->attachTable("table", StorageMemory::create("test", "table", ColumnsDescription{columns}));
|
2019-07-08 02:14:32 +00:00
|
|
|
context.makeGlobalContext();
|
2019-05-18 22:19:24 +00:00
|
|
|
context.addDatabase("test", database);
|
|
|
|
context.setCurrentDatabase("test");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
State & state()
|
|
|
|
{
|
|
|
|
static State res;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-16 21:09:06 +00:00
|
|
|
void check(const std::string & query, const std::string & expected, const Context & context, const NamesAndTypesList & columns)
|
|
|
|
{
|
2019-05-16 19:57:20 +00:00
|
|
|
ParserSelectQuery parser;
|
|
|
|
ASTPtr ast = parseQuery(parser, query, 1000);
|
2019-05-16 21:09:06 +00:00
|
|
|
std::string transformed_query = transformQueryForExternalDatabase(*ast, columns, IdentifierQuotingStyle::DoubleQuotes, "test", "table", context);
|
|
|
|
|
|
|
|
EXPECT_EQ(transformed_query, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-18 22:19:24 +00:00
|
|
|
TEST(TransformQueryForExternalDatabase, InWithSingleElement)
|
2019-05-16 21:09:06 +00:00
|
|
|
{
|
2019-05-18 22:19:24 +00:00
|
|
|
check("SELECT column FROM test.table WHERE 1 IN (1)",
|
2019-06-29 16:18:59 +00:00
|
|
|
"SELECT \"column\" FROM \"test\".\"table\" WHERE 1 IN (1)",
|
2019-05-18 22:19:24 +00:00
|
|
|
state().context, state().columns);
|
|
|
|
check("SELECT column FROM test.table WHERE column IN (1, 2)",
|
2019-06-29 16:18:59 +00:00
|
|
|
"SELECT \"column\" FROM \"test\".\"table\" WHERE \"column\" IN (1, 2)",
|
2019-05-18 22:19:24 +00:00
|
|
|
state().context, state().columns);
|
|
|
|
check("SELECT column FROM test.table WHERE column NOT IN ('hello', 'world')",
|
2019-06-29 16:18:59 +00:00
|
|
|
"SELECT \"column\" FROM \"test\".\"table\" WHERE \"column\" NOT IN ('hello', 'world')",
|
2019-05-18 22:19:24 +00:00
|
|
|
state().context, state().columns);
|
2019-05-16 22:55:29 +00:00
|
|
|
}
|
|
|
|
|
2019-05-18 22:19:24 +00:00
|
|
|
TEST(TransformQueryForExternalDatabase, Like)
|
2019-05-16 22:55:29 +00:00
|
|
|
{
|
2019-05-18 22:19:24 +00:00
|
|
|
check("SELECT column FROM test.table WHERE column LIKE '%hello%'",
|
2019-06-29 16:18:59 +00:00
|
|
|
"SELECT \"column\" FROM \"test\".\"table\" WHERE \"column\" LIKE '%hello%'",
|
2019-05-18 22:19:24 +00:00
|
|
|
state().context, state().columns);
|
|
|
|
check("SELECT column FROM test.table WHERE column NOT LIKE 'w%rld'",
|
2019-06-29 16:18:59 +00:00
|
|
|
"SELECT \"column\" FROM \"test\".\"table\" WHERE \"column\" NOT LIKE 'w%rld'",
|
2019-05-18 22:19:24 +00:00
|
|
|
state().context, state().columns);
|
2019-05-16 19:57:20 +00:00
|
|
|
}
|
2019-08-07 18:52:53 +00:00
|
|
|
|
|
|
|
TEST(TransformQueryForExternalDatabase, Substring)
|
|
|
|
{
|
|
|
|
check("SELECT column FROM test.table WHERE left(column, 10) = RIGHT(column, 10) AND SUBSTRING(column FROM 1 FOR 2) = 'Hello'",
|
|
|
|
"SELECT \"column\" FROM \"test\".\"table\"",
|
|
|
|
state().context, state().columns);
|
|
|
|
}
|