Option to force cross_to_inner_join_rewrite

This commit is contained in:
vdimir 2022-05-10 15:12:17 +00:00
parent 425d429044
commit 00cd21cacf
No known key found for this signature in database
GPG Key ID: 6EE4CE2BEDC51862
4 changed files with 21 additions and 4 deletions

View File

@ -726,7 +726,7 @@ static constexpr UInt64 operator""_Gb(unsigned long long value)
M(Bool, output_format_pretty_row_numbers, false, "Add row numbers before each row for pretty output format", 0) \
M(Bool, insert_distributed_one_random_shard, false, "If setting is enabled, inserting into distributed table will choose a random shard to write when there is no sharding key", 0) \
\
M(Bool, cross_to_inner_join_rewrite, true, "Use inner join instead of comma/cross join if possible", 0) \
M(UInt64, cross_to_inner_join_rewrite, 1, "Use inner join instead of comma/cross join if possible. Possible values: 0 - no rewrite, 1 - apply if possible, 2 - force rewrite all cross joins", 0) \
\
M(Bool, output_format_arrow_low_cardinality_as_dictionary, false, "Enable output LowCardinality type as Dictionary Arrow type", 0) \
\

View File

@ -18,11 +18,14 @@
#include <Parsers/ParserTablesInSelectQuery.h>
#include <Parsers/parseQuery.h>
#include <Common/logger_useful.h>
namespace DB
{
namespace ErrorCodes
{
extern const int INCORRECT_QUERY;
extern const int LOGICAL_ERROR;
extern const int NOT_IMPLEMENTED;
}
@ -232,11 +235,25 @@ void CrossToInnerJoinMatcher::visit(ASTSelectQuery & select, ASTPtr &, Data & da
auto asts_to_join_on = moveExpressionToJoinOn(select.where(), joined_tables, data.tables_with_columns, data.aliases);
for (size_t i = 1; i < joined_tables.size(); ++i)
{
auto & joined = joined_tables[i];
if (joined.tableJoin()->kind != ASTTableJoin::Kind::Cross)
continue;
const auto & expr_it = asts_to_join_on.find(i);
if (expr_it != asts_to_join_on.end())
{
if (joined_tables[i].rewriteCrossToInner(makeOnExpression(expr_it->second)))
String query_before = queryToString(*joined.tableJoin());
ASTPtr on_expr = makeOnExpression(expr_it->second);
if (joined.rewriteCrossToInner(on_expr))
{
data.done = true;
LOG_DEBUG(&Poco::Logger::get("CrossToInnerJoin"), "Rewritten '{}' to '{}'", query_before, queryToString(*joined.tableJoin()));
}
else if (data.cross_to_inner_join_rewrite > 1)
{
throw Exception(ErrorCodes::INCORRECT_QUERY, "Failed to rewrite '{} WHERE {}' to INNER JOIN",
query_before, queryToString(select.where()));
}
}
}
}

View File

@ -19,7 +19,7 @@ public:
const Aliases & aliases;
const String current_database;
bool done = false;
bool cross_to_inner_join_rewrite = true;
UInt8 cross_to_inner_join_rewrite = 1;
};
static bool needChildVisit(ASTPtr &, const ASTPtr &);

View File

@ -218,7 +218,7 @@ static void rewriteMultipleJoins(ASTPtr & query, const TablesWithColumns & table
QueryAliasesNoSubqueriesVisitor(aliases).visit(select.select());
CrossToInnerJoinVisitor::Data cross_to_inner{tables, aliases, database};
cross_to_inner.cross_to_inner_join_rewrite = settings.cross_to_inner_join_rewrite;
cross_to_inner.cross_to_inner_join_rewrite = static_cast<UInt8>(std::min<UInt64>(settings.cross_to_inner_join_rewrite, 2));
CrossToInnerJoinVisitor(cross_to_inner).visit(query);
JoinToSubqueryTransformVisitor::Data join_to_subs_data{tables, aliases};