Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix transfer query to MySQL compatible query #56456

Merged
merged 5 commits into from Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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