ClickHouse/dbms/src/Storages/tests/gtest_transform_query_for_external_database.cpp

104 lines
4.2 KiB
C++
Raw Normal View History

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>
2019-10-09 20:16:17 +00:00
#include <DataTypes/DataTypeDateTime.h>
2019-05-16 19:57:20 +00:00
#include <Interpreters/Context.h>
#include <Databases/DatabaseMemory.h>
#include <Storages/StorageMemory.h>
2019-05-16 21:09:06 +00:00
#include <Functions/registerFunctions.h>
2020-02-07 09:51:48 +00:00
#include <Common/tests/gtest_global_context.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
{
2020-02-07 09:51:48 +00:00
Context context = getContext();
2019-10-09 20:16:17 +00:00
NamesAndTypesList columns{
{"column", std::make_shared<DataTypeUInt8>()},
{"apply_id", std::make_shared<DataTypeUInt64>()},
{"apply_type", std::make_shared<DataTypeUInt8>()},
{"apply_status", std::make_shared<DataTypeUInt8>()},
{"create_time", std::make_shared<DataTypeDateTime>()},
};
2019-05-18 22:19:24 +00:00
State()
{
registerFunctions();
DatabasePtr database = std::make_shared<DatabaseMemory>("test");
2019-12-04 16:06:55 +00:00
database->attachTable("table", StorageMemory::create(StorageID("test", "table"), ColumnsDescription{columns}, ConstraintsDescription{}));
context.makeGlobalContext();
DatabaseCatalog::instance().attachDatabase("test", database);
2019-05-18 22:19:24 +00:00
context.setCurrentDatabase("test");
}
};
2019-12-15 06:34:43 +00:00
static State & state()
2019-05-18 22:19:24 +00:00
{
static State res;
return res;
}
2019-12-15 06:34:43 +00:00
static void check(const std::string & query, const std::string & expected, const Context & context, const NamesAndTypesList & columns)
2019-05-16 21:09:06 +00:00
{
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)",
2020-03-09 02:15:19 +00:00
R"(SELECT "column" FROM "test"."table" WHERE 1)",
2019-05-18 22:19:24 +00:00
state().context, state().columns);
check("SELECT column FROM test.table WHERE column IN (1, 2)",
2020-03-09 02:15:19 +00:00
R"(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')",
2020-03-09 02:15:19 +00:00
R"(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%'",
2020-03-09 02:15:19 +00:00
R"(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'",
2020-03-09 02:15:19 +00:00
R"(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'",
2020-03-09 02:15:19 +00:00
R"(SELECT "column" FROM "test"."table")",
2019-08-07 18:52:53 +00:00
state().context, state().columns);
}
TEST(TransformQueryForExternalDatabase, MultipleAndSubqueries)
{
check("SELECT column FROM test.table WHERE 1 = 1 AND toString(column) = '42' AND column = 42 AND left(column, 10) = RIGHT(column, 10) AND column IN (1, 42) AND SUBSTRING(column FROM 1 FOR 2) = 'Hello' AND column != 4",
2020-03-09 02:15:19 +00:00
R"(SELECT "column" FROM "test"."table" WHERE 1 AND ("column" = 42) AND ("column" IN (1, 42)) AND ("column" != 4))",
state().context, state().columns);
check("SELECT column FROM test.table WHERE toString(column) = '42' AND left(column, 10) = RIGHT(column, 10) AND column = 42",
2020-03-09 02:15:19 +00:00
R"(SELECT "column" FROM "test"."table" WHERE ("column" = 42))",
state().context, state().columns);
2019-10-09 20:16:17 +00:00
}
2019-10-09 20:16:17 +00:00
TEST(TransformQueryForExternalDatabase, Issue7245)
{
check("select apply_id from test.table where apply_type = 2 and create_time > addDays(toDateTime('2019-01-01 01:02:03'),-7) and apply_status in (3,4)",
2020-03-09 02:15:19 +00:00
R"(SELECT "apply_id", "apply_type", "apply_status", "create_time" FROM "test"."table" WHERE ("apply_type" = 2) AND ("create_time" > '2018-12-25 01:02:03') AND ("apply_status" IN (3, 4)))",
2019-10-09 20:16:17 +00:00
state().context, state().columns);
2019-08-27 13:13:40 +00:00
}