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

Do not consider non-deterministic expressions as invariants in pre-filters #7853

Merged
merged 3 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions src/dsql/BoolNodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,11 @@ class RseBoolNode final : public TypedNode<BoolExprNode, ExprNode::TYPE_RSE_BOOL
return true;
}

virtual bool deterministic() const override
{
return false;
}

virtual bool possiblyUnknown() const
{
return true;
Expand Down
24 changes: 24 additions & 0 deletions src/dsql/ExprNodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,20 @@ bool ExprNode::sameAs(const ExprNode* other, bool ignoreStreams) const
return true;
}

bool ExprNode::deterministic() const
{
NodeRefsHolder holder;
getChildren(holder, false);

for (auto i : holder.refs)
{
if (*i && !(*i)->deterministic())
return false;
}

return true;
}

bool ExprNode::possiblyUnknown() const
{
NodeRefsHolder holder;
Expand Down Expand Up @@ -12333,6 +12347,11 @@ void SysFuncCallNode::make(DsqlCompilerScratch* dsqlScratch, dsc* desc)
function->makeFunc(&dataTypeUtil, function, desc, argsArray.getCount(), argsArray.begin());
}

bool SysFuncCallNode::deterministic() const
{
return ExprNode::deterministic() && function->deterministic();
}

void SysFuncCallNode::getDesc(thread_db* tdbb, CompilerScratch* csb, dsc* desc)
{
Array<const dsc*> argsArray;
Expand Down Expand Up @@ -12980,6 +12999,11 @@ void UdfCallNode::make(DsqlCompilerScratch* /*dsqlScratch*/, dsc* desc)
desc->setTextType(dsqlFunction->udf_character_set_id);
}

bool UdfCallNode::deterministic() const
{
return ExprNode::deterministic() && function->fun_deterministic;
}

void UdfCallNode::getDesc(thread_db* /*tdbb*/, CompilerScratch* /*csb*/, dsc* desc)
{
// Null value for the function indicates that the function was not
Expand Down
34 changes: 34 additions & 0 deletions src/dsql/ExprNodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,11 @@ class FieldNode final : public TypedNode<ValueExprNode, ExprNode::TYPE_FIELD>
dsqlDesc = desc;
}

virtual bool deterministic() const override
{
return false;
}

virtual bool possiblyUnknown() const
{
return false;
Expand Down Expand Up @@ -857,6 +862,11 @@ class GenIdNode final : public TypedNode<ValueExprNode, ExprNode::TYPE_GEN_ID>
virtual void genBlr(DsqlCompilerScratch* dsqlScratch);
virtual void make(DsqlCompilerScratch* dsqlScratch, dsc* desc);

virtual bool deterministic() const override
{
return false;
}

virtual void getDesc(thread_db* tdbb, CompilerScratch* csb, dsc* desc);
virtual ValueExprNode* copy(thread_db* tdbb, NodeCopier& copier) const;
virtual bool dsqlMatch(DsqlCompilerScratch* dsqlScratch, const ExprNode* other, bool ignoreMapCast) const;
Expand Down Expand Up @@ -1613,6 +1623,11 @@ class ParameterNode final : public TypedNode<ValueExprNode, ExprNode::TYPE_PARAM

Request* getParamRequest(Request* request) const;

virtual bool deterministic() const override
{
return false;
}

virtual void getDesc(thread_db* tdbb, CompilerScratch* csb, dsc* desc);
virtual ParameterNode* copy(thread_db* tdbb, NodeCopier& copier) const;
virtual ParameterNode* pass1(thread_db* tdbb, CompilerScratch* csb);
Expand Down Expand Up @@ -1660,6 +1675,11 @@ class RecordKeyNode final : public TypedNode<ValueExprNode, ExprNode::TYPE_RECOR
virtual void genBlr(DsqlCompilerScratch* dsqlScratch);
virtual void make(DsqlCompilerScratch* dsqlScratch, dsc* desc);

virtual bool deterministic() const override
{
return false;
}

virtual bool possiblyUnknown() const
{
return false;
Expand Down Expand Up @@ -1928,6 +1948,11 @@ class SubQueryNode final : public TypedNode<ValueExprNode, ExprNode::TYPE_SUBQUE
return false;
}

virtual bool deterministic() const override
{
return false;
}

virtual bool possiblyUnknown() const
{
return true;
Expand Down Expand Up @@ -2062,6 +2087,8 @@ class SysFuncCallNode final : public TypedNode<ValueExprNode, ExprNode::TYPE_SYS
virtual void genBlr(DsqlCompilerScratch* dsqlScratch);
virtual void make(DsqlCompilerScratch* dsqlScratch, dsc* desc);

virtual bool deterministic() const override;

virtual void getDesc(thread_db* tdbb, CompilerScratch* csb, dsc* desc);
virtual ValueExprNode* copy(thread_db* tdbb, NodeCopier& copier) const;
virtual bool dsqlMatch(DsqlCompilerScratch* dsqlScratch, const ExprNode* other, bool ignoreMapCast) const;
Expand Down Expand Up @@ -2142,6 +2169,8 @@ class UdfCallNode final : public TypedNode<ValueExprNode, ExprNode::TYPE_UDF_CAL
virtual void genBlr(DsqlCompilerScratch* dsqlScratch);
virtual void make(DsqlCompilerScratch* dsqlScratch, dsc* desc);

virtual bool deterministic() const override;

virtual bool possiblyUnknown() const
{
return true;
Expand Down Expand Up @@ -2240,6 +2269,11 @@ class VariableNode final : public TypedNode<ValueExprNode, ExprNode::TYPE_VARIAB

Request* getVarRequest(Request* request) const;

virtual bool deterministic() const override
{
return false;
}

virtual void getDesc(thread_db* tdbb, CompilerScratch* csb, dsc* desc);
virtual ValueExprNode* copy(thread_db* tdbb, NodeCopier& copier) const;
virtual ValueExprNode* pass1(thread_db* tdbb, CompilerScratch* csb);
Expand Down
8 changes: 8 additions & 0 deletions src/dsql/Nodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,9 @@ class ExprNode : public DmlNode
target = node ? node->dsqlFieldRemapper(visitor) : NULL;
}

// Check if expression returns deterministic result
virtual bool deterministic() const;

// Check if expression could return NULL or expression can turn NULL into a true/false.
virtual bool possiblyUnknown() const;

Expand Down Expand Up @@ -1184,6 +1187,11 @@ class RecordSourceNode : public ExprNode
fb_assert(false);
}

virtual bool deterministic() const override
{
return false;
}

virtual bool possiblyUnknown() const
{
return true;
Expand Down
13 changes: 13 additions & 0 deletions src/jrd/SysFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6962,3 +6962,16 @@ void SysFunction::checkArgsMismatch(int count) const
status_exception::raise(Arg::Gds(isc_funmismat) << Arg::Str(name));
}
}


bool SysFunction::deterministic() const
{
const MetaName funcName(name);

return (funcName != "GEN_UUID" &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be defined in table at SysFunction::functions.

funcName != "RAND" &&
funcName != "RSA_PRIVATE" &&
funcName != "RSA_PUBLIC" &&
funcName != "RDB_GET_CONTEXT" &&
funcName != "RDB_SET_CONTEXT");
}
1 change: 1 addition & 0 deletions src/jrd/SysFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class SysFunction
static const SysFunction* lookup(const Jrd::MetaName& name);

void checkArgsMismatch(int count) const;
bool deterministic() const;

private:
const static SysFunction functions[];
Expand Down
10 changes: 6 additions & 4 deletions src/jrd/optimizer/Optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -788,19 +788,21 @@ RecordSource* Optimizer::compile(BoolExprNodeStack* parentStack)
for (StreamList::iterator i = rseStreams.begin(); i != rseStreams.end(); ++i)
csb->csb_rpt[*i].deactivate();

// Find and collect booleans that are invariant in this context
// (i.e. independent from streams in the RseNode). We can do that
// easily because these streams are inactive at this point and
// any node that references them will be not computable.
// Find and collect booleans that are both deterministic and invariant
// in this context (i.e. independent from streams in the current RseNode).
// We can check that easily because these streams are inactive at this point
// and any node that references them will be not computable.
// Note that we cannot do that for outer joins, as in this case boolean
// represents a join condition which does not filter out the rows.

BoolExprNode* invariantBoolean = nullptr;

if (isInnerJoin())
{
for (auto iter = getBaseConjuncts(); iter.hasData(); ++iter)
{
if (!(iter & CONJUNCT_USED) &&
iter->deterministic() &&
iter->computable(csb, INVALID_STREAM, false))
{
compose(getPool(), &invariantBoolean, iter);
Expand Down
Loading