Set default value cross_to_inner_join_rewrite = 2 for comma join

This commit is contained in:
vdimir 2022-07-18 15:18:39 +02:00
parent acb1fa53ed
commit 10e4ef135d
No known key found for this signature in database
GPG Key ID: 6EE4CE2BEDC51862
4 changed files with 47 additions and 4 deletions

View File

@ -758,7 +758,7 @@ static constexpr UInt64 operator""_GiB(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, 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, 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(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(UInt64, cross_to_inner_join_rewrite, 2, "Use inner join instead of comma/cross join if possible. Possible values: 0 - no rewrite, 1 - apply if possible for comma/cross, 2 - force rewrite all comma joins, cross - if possible", 0) \
\ \
M(Bool, output_format_arrow_low_cardinality_as_dictionary, false, "Enable output LowCardinality type as Dictionary Arrow type", 0) \ M(Bool, output_format_arrow_low_cardinality_as_dictionary, false, "Enable output LowCardinality type as Dictionary Arrow type", 0) \
M(Bool, output_format_arrow_string_as_string, false, "Use Arrow String type instead of Binary for String columns", 0) \ M(Bool, output_format_arrow_string_as_string, false, "Use Arrow String type instead of Binary for String columns", 0) \

View File

@ -39,7 +39,10 @@ struct JoinedElement
: element(table_element) : element(table_element)
{ {
if (element.table_join) if (element.table_join)
{
join = element.table_join->as<ASTTableJoin>(); join = element.table_join->as<ASTTableJoin>();
original_kind = join->kind;
}
} }
void checkTableName(const DatabaseAndTableWithAlias & table, const String & current_database) const void checkTableName(const DatabaseAndTableWithAlias & table, const String & current_database) const
@ -61,6 +64,8 @@ struct JoinedElement
join->kind = ASTTableJoin::Kind::Cross; join->kind = ASTTableJoin::Kind::Cross;
} }
ASTTableJoin::Kind getOriginalKind() const { return original_kind; }
bool rewriteCrossToInner(ASTPtr on_expression) bool rewriteCrossToInner(ASTPtr on_expression)
{ {
if (join->kind != ASTTableJoin::Kind::Cross) if (join->kind != ASTTableJoin::Kind::Cross)
@ -83,6 +88,8 @@ struct JoinedElement
private: private:
const ASTTablesInSelectQueryElement & element; const ASTTablesInSelectQueryElement & element;
ASTTableJoin * join = nullptr; ASTTableJoin * join = nullptr;
ASTTableJoin::Kind original_kind;
}; };
bool isAllowedToRewriteCrossJoin(const ASTPtr & node, const Aliases & aliases) bool isAllowedToRewriteCrossJoin(const ASTPtr & node, const Aliases & aliases)
@ -251,10 +258,17 @@ void CrossToInnerJoinMatcher::visit(ASTSelectQuery & select, ASTPtr &, Data & da
} }
} }
if (data.cross_to_inner_join_rewrite > 1 && !rewritten) if (joined.getOriginalKind() == ASTTableJoin::Kind::Comma &&
data.cross_to_inner_join_rewrite > 1 &&
!rewritten)
{ {
throw Exception(ErrorCodes::INCORRECT_QUERY, "Failed to rewrite '{} WHERE {}' to INNER JOIN", throw Exception(
query_before, queryToString(select.where())); ErrorCodes::INCORRECT_QUERY,
"Failed to rewrite comma join to INNER. "
"Please, try to simplify WHERE section "
"or set the setting `cross_to_inner_join_rewrite` to 1 to allow slow CROSS JOIN for this case"
"(cannot rewrite '{} WHERE {}' to INNER JOIN)",
query_before, queryToString(select.where()));
} }
} }
} }

View File

@ -0,0 +1,7 @@
1
1
1
1
1
1
1

View File

@ -0,0 +1,22 @@
DROP TABLE IF EXISTS t1;
DROP TABLE IF EXISTS t2;
CREATE TABLE t1 ( x Int ) Engine = Memory;
INSERT INTO t1 VALUES ( 1 ), ( 2 ), ( 3 );
CREATE TABLE t2 ( x Int ) Engine = Memory;
INSERT INTO t2 VALUES ( 2 ), ( 3 ), ( 4 );
SET cross_to_inner_join_rewrite = 1;
SELECT count() = 1 FROM t1, t2 WHERE t1.x > t2.x;
SELECT count() = 1 2ROM t1, t2 WHERE t1.x = t2.x;
SELECT count() = 1 2ROM t1 CROSS JOIN t2 WHERE t1.x = t2.x;
SELECT count() = 1 FROM t1 CROSS JOIN t2 WHERE t1.x > t2.x;
SET cross_to_inner_join_rewrite = 2;
SELECT count() = 1 FROM t1, t2 WHERE t1.x > t2.x; -- { serverError INCORRECT_QUERY }
SELECT count() = 2 FROM t1, t2 WHERE t1.x = t2.x;
SELECT count() = 2 FROM t1 CROSS JOIN t2 WHERE t1.x = t2.x;
SELECT count() = 1 FROM t1 CROSS JOIN t2 WHERE t1.x > t2.x; -- do not force rewrite explicit CROSS