Fix segfault in arrayExists AST optimization

This commit is contained in:
Nikolay Degterinsky 2023-09-07 02:10:14 +00:00
parent abc48a0b73
commit 4b764f7b65
4 changed files with 22 additions and 1 deletions

View File

@ -2,6 +2,7 @@
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTIdentifier.h>
#include <Parsers/ASTLiteral.h>
#include <Parsers/ASTTablesInSelectQuery.h>
namespace DB
{
@ -76,4 +77,22 @@ void RewriteArrayExistsFunctionMatcher::visit(const ASTFunction & func, ASTPtr &
}
}
bool RewriteArrayExistsFunctionMatcher::needChildVisit(const ASTPtr & ast, const ASTPtr & child)
{
/// SELECT ... JOIN ... ON arrayExists(x -> x = 1, arr)
/// A JOIN on expression is invalid, ignore it to avoid segmentation fault
if (auto * join = ast->as<ASTTableJoin>())
{
if (auto * func = child->as<ASTFunction>())
{
if (func->name == "arrayExists")
return false;
}
}
return true;
}
}

View File

@ -18,7 +18,7 @@ public:
static void visit(ASTPtr & ast, Data &);
static void visit(const ASTFunction &, ASTPtr & ast, Data &);
static bool needChildVisit(const ASTPtr &, const ASTPtr &) { return true; }
static bool needChildVisit(const ASTPtr & ast, const ASTPtr & child);
};
using RewriteArrayExistsFunctionVisitor = InDepthNodeVisitor<RewriteArrayExistsFunctionMatcher, false>;

View File

@ -0,0 +1,2 @@
CREATE TABLE 02834_t (id UInt64, arr Array(UInt64)) ENGINE = MergeTree ORDER BY id;
WITH subquery AS (SELECT []) SELECT t.* FROM 02834_t AS t JOIN subquery ON arrayExists(x -> x = 1, t.arr); -- { serverError INVALID_JOIN_ON_EXPRESSION }