Skip to content

Commit

Permalink
Merge pull request #58490 from ClickHouse/cherrypick/23.8/7f3a082c0e9…
Browse files Browse the repository at this point in the history
…68d52cbbb68bc8f0dfecfe7c79992

Cherry pick #56456 to 23.8: Fix transfer query to MySQL compatible query
  • Loading branch information
robot-ch-test-poll2 committed Jan 4, 2024
2 parents 644ed5b + 7f3a082 commit 7821d67
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
Expand Up @@ -279,9 +279,13 @@ TEST(TransformQueryForExternalDatabase, MultipleAndSubqueries)
{
const State & state = State::instance();

check(state, 1, {"column"},
"SELECT column FROM test.table WHERE 1 = 1 AND toString(column) = '42' AND column = 42 AND left(toString(column), 10) = RIGHT(toString(column), 10) AND column IN (1, 42) AND SUBSTRING(toString(column) FROM 1 FOR 2) = 'Hello' AND column != 4",
R"(SELECT "column" FROM "test"."table" WHERE 1 AND ("column" = 42) AND ("column" IN (1, 42)) AND ("column" != 4))");
check(
state,
1,
{"column"},
"SELECT column FROM test.table WHERE 1 = 1 AND toString(column) = '42' AND column = 42 AND left(toString(column), 10) = "
"RIGHT(toString(column), 10) AND column IN (1, 42) AND SUBSTRING(toString(column) FROM 1 FOR 2) = 'Hello' AND column != 4",
R"(SELECT "column" FROM "test"."table" WHERE (1 = 1) AND ("column" = 42) AND ("column" IN (1, 42)) AND ("column" != 4))");
check(state, 1, {"column"},
"SELECT column FROM test.table WHERE toString(column) = '42' AND left(toString(column), 10) = RIGHT(toString(column), 10) AND column = 42",
R"(SELECT "column" FROM "test"."table" WHERE "column" = 42)");
Expand Down
31 changes: 31 additions & 0 deletions src/Storages/transformQueryForExternalDatabase.cpp
Expand Up @@ -75,6 +75,33 @@ class ReplacingConstantExpressionsMatcherNumOrStr
}
};

struct ReplaceLiteralToExprVisitorData
{
using TypeToVisit = ASTFunction;

void visit(ASTFunction & func, ASTPtr &) const
{
if (func.name == "and" || func.name == "or")
{
for (auto & argument : func.arguments->children)
{
auto * literal_expr = typeid_cast<ASTLiteral *>(argument.get());
UInt64 value;
if (literal_expr && literal_expr->value.tryGet<UInt64>(value) && (value == 0 || value == 1))
{
/// 1 -> 1=1, 0 -> 1=0.
if (value)
argument = makeASTFunction("equals", std::make_shared<ASTLiteral>(1), std::make_shared<ASTLiteral>(1));
else
argument = makeASTFunction("equals", std::make_shared<ASTLiteral>(1), std::make_shared<ASTLiteral>(0));
}
}
}
}
};

using ReplaceLiteralToExprVisitor = InDepthNodeVisitor<OneTypeMatcher<ReplaceLiteralToExprVisitorData>, true>;

class DropAliasesMatcher
{
public:
Expand Down Expand Up @@ -288,6 +315,10 @@ String transformQueryForExternalDatabaseImpl(
{
replaceConstantExpressions(original_where, context, available_columns);

/// Replace like WHERE 1 AND 1 to WHERE 1 = 1 AND 1 = 1
ReplaceLiteralToExprVisitor::Data replace_literal_to_expr_data;
ReplaceLiteralToExprVisitor(replace_literal_to_expr_data).visit(original_where);

if (isCompatible(original_where))
{
select->setExpression(ASTSelectQuery::Expression::WHERE, std::move(original_where));
Expand Down

0 comments on commit 7821d67

Please sign in to comment.