External queries: fix the case of expr IN table #9756

This commit is contained in:
Alexey Milovidov 2021-01-09 06:28:54 +03:00
parent 7d25c1f7f7
commit 8fb64472f2
2 changed files with 24 additions and 0 deletions

View File

@ -80,6 +80,24 @@ TEST(TransformQueryForExternalDatabase, InWithSingleElement)
state.context, state.columns);
}
TEST(TransformQueryForExternalDatabase, InWithTable)
{
const State & state = State::instance();
check("SELECT column FROM test.table WHERE 1 IN external_table",
R"(SELECT "column" FROM "test"."table")",
state.context, state.columns);
check("SELECT column FROM test.table WHERE 1 IN (x)",
R"(SELECT "column" FROM "test"."table")",
state.context, state.columns);
check("SELECT column, field, value FROM test.table WHERE column IN (field, value)",
R"(SELECT "column", "field", "value" FROM "test"."table" WHERE "column" IN ("field", "value"))",
state.context, state.columns);
check("SELECT column FROM test.table WHERE column NOT IN hello AND column = 123",
R"(SELECT "column" FROM "test"."table" WHERE ("column" = 123))",
state.context, state.columns);
}
TEST(TransformQueryForExternalDatabase, Like)
{
const State & state = State::instance();

View File

@ -138,6 +138,12 @@ bool isCompatible(const IAST & node)
if (name == "tuple" && function->arguments->children.size() <= 1)
return false;
/// If the right hand side of IN is an identifier (example: x IN table), then it's not compatible.
if ((name == "in" || name == "notIn")
&& (function->arguments->children.size() != 2
|| function->arguments->children[1]->as<ASTIdentifier>()))
return false;
for (const auto & expr : function->arguments->children)
if (!isCompatible(*expr))
return false;