Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/dsql/BoolNodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1339,8 +1339,69 @@ BoolExprNode* InListBoolNode::copy(thread_db* tdbb, NodeCopier& copier) const
return node;
}

BoolExprNode* InListBoolNode::decompose(CompilerScratch* csb)
{
// Search for list items depending on record streams.
// If found, decompose expression:
// <arg> IN (<item1>, <item2>, <item3>, <item4> ...)
// into:
// <arg> IN (<item1>, <item2>, ...) OR <arg> = <item3> OR <arg> = <item4> ...
// where the ORed booleans are known to be stream-based (i.e. contain fields inside)
// and thus could use an index, if possible.
//
// See #8109 in the tracker, example:
//
// SELECT e.*
// FROM Employees e
// WHERE :SomeID IN (e.LeaderID, e.DispEmpID)

auto& pool = csb->csb_pool;
BoolExprNode* boolNode = nullptr;

for (auto iter = list->items.begin(); iter != list->items.end();)
{
ValueExprNode* const item = *iter;

SortedStreamList streams;
item->collectStreams(streams);

if (streams.isEmpty())
{
iter++;
continue;
}

list->items.remove(iter);

const auto cmpNode = FB_NEW_POOL(pool) ComparativeBoolNode(pool, blr_eql, arg, item);

if (boolNode)
boolNode = FB_NEW_POOL(pool) BinaryBoolNode(pool, blr_or, boolNode, cmpNode);
else
boolNode = cmpNode;
}

if (boolNode && list->items.hasData())
{
BoolExprNode* priorNode = this;

if (list->items.getCount() == 1)
{
// Convert A IN (B) into A = B
priorNode = FB_NEW_POOL(pool) ComparativeBoolNode(pool, blr_eql, arg, list->items.front());
}

boolNode = FB_NEW_POOL(pool) BinaryBoolNode(pool, blr_or, boolNode, priorNode);
}

return boolNode;
}

BoolExprNode* InListBoolNode::pass1(thread_db* tdbb, CompilerScratch* csb)
{
if (const auto node = decompose(csb))
return node->pass1(tdbb, csb);

doPass1(tdbb, csb, arg.getAddress());

nodFlags |= FLAG_INVARIANT;
Expand Down
3 changes: 3 additions & 0 deletions src/dsql/BoolNodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ class InListBoolNode : public TypedNode<BoolExprNode, ExprNode::TYPE_IN_LIST_BOO
void pass2Boolean(thread_db* tdbb, CompilerScratch* csb, std::function<void ()> process) override;
bool execute(thread_db* tdbb, Request* request) const override;

private:
BoolExprNode* decompose(CompilerScratch* csb);

public:
NestConst<ValueExprNode> arg;
NestConst<ValueListNode> list;
Expand Down
Loading