Skip to content

Commit

Permalink
Merge pull request #57888 from ucasfl/fix-transfer-external-query
Browse files Browse the repository at this point in the history
Fix transform query for external database
  • Loading branch information
vdimir committed Dec 18, 2023
2 parents f10dae4 + 82fc51f commit f024e39
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/Storages/tests/gtest_transform_query_for_external_database.cpp
Expand Up @@ -320,6 +320,18 @@ TEST(TransformQueryForExternalDatabase, ForeignColumnInWhere)
R"(SELECT "column", "apply_id" FROM "test"."table" WHERE ("column" > 2) AND ("apply_id" = 1))");
}

TEST(TransformQueryForExternalDatabase, TupleSurroundPredicates)
{
const State & state = State::instance();

check(
state,
1,
{"column", "field", "a"},
"SELECT column, field, a FROM table WHERE ((column > 10) AND (length(field) > 0)) AND a > 0",
R"(SELECT "column", "field", "a" FROM "test"."table" WHERE ("a" > 0) AND ("column" > 10))");
}

TEST(TransformQueryForExternalDatabase, NoStrict)
{
const State & state = State::instance();
Expand Down
20 changes: 16 additions & 4 deletions src/Storages/transformQueryForExternalDatabase.cpp
Expand Up @@ -329,14 +329,26 @@ String transformQueryForExternalDatabaseImpl(
}
else if (auto * function = original_where->as<ASTFunction>())
{
if (function->name == "and")
if (function->name == "and" || function->name == "tuple")
{
auto new_function_and = makeASTFunction("and");
for (auto & elem : function->arguments->children)
std::queue<const ASTFunction *> predicates;
predicates.push(function);

while (!predicates.empty())
{
if (isCompatible(elem))
new_function_and->arguments->children.push_back(elem);
const auto * func = predicates.front();
predicates.pop();

for (auto & elem : func->arguments->children)
{
if (isCompatible(elem))
new_function_and->arguments->children.push_back(elem);
else if (const auto * child = elem->as<ASTFunction>(); child && (child->name == "and" || child->name == "tuple"))
predicates.push(child);
}
}

if (new_function_and->arguments->children.size() == 1)
select->setExpression(ASTSelectQuery::Expression::WHERE, std::move(new_function_and->arguments->children[0]));
else if (new_function_and->arguments->children.size() > 1)
Expand Down

0 comments on commit f024e39

Please sign in to comment.