@@ -67,6 +67,27 @@ using namespace Jrd;
6767
6868namespace
6969{
70+ bool sameNodes (const ValueIfNode* node1, const CoalesceNode* node2, bool ignoreStreams)
71+ {
72+ // dimitr: COALESCE could be represented as ValueIfNode in older databases,
73+ // so compare them for actually being the same thing:
74+ // COALESCE(A, B) == VALUE_IF(A IS NULL, B, A)
75+
76+ if (node1 && node2)
77+ {
78+ const MissingBoolNode* const missing = node1->condition ->as <MissingBoolNode>();
79+ if (missing && missing->arg ->sameAs (node1->falseValue , false ) &&
80+ node2->args ->items .getCount () == 2 &&
81+ node2->args ->items [0 ]->sameAs (node1->falseValue , ignoreStreams) &&
82+ node2->args ->items [1 ]->sameAs (node1->trueValue , ignoreStreams))
83+ {
84+ return true ;
85+ }
86+ }
87+
88+ return false ;
89+ }
90+
7091 // Expand DBKEY for view
7192 void expandViewNodes (thread_db* tdbb, CompilerScratch* csb, StreamType stream,
7293 ValueExprNodeStack& stack, UCHAR blrOp)
@@ -2988,6 +3009,14 @@ ValueExprNode* CoalesceNode::copy(thread_db* tdbb, NodeCopier& copier) const
29883009 return node;
29893010}
29903011
3012+ bool CoalesceNode::sameAs (const ExprNode* other, bool ignoreStreams) const
3013+ {
3014+ if (ExprNode::sameAs (other, ignoreStreams))
3015+ return true ;
3016+
3017+ return sameNodes (other->as <ValueIfNode>(), this , ignoreStreams);
3018+ }
3019+
29913020ValueExprNode* CoalesceNode::pass2 (thread_db* tdbb, CompilerScratch* csb)
29923021{
29933022 ValueExprNode::pass2 (tdbb, csb);
@@ -11738,6 +11767,14 @@ ValueExprNode* ValueIfNode::copy(thread_db* tdbb, NodeCopier& copier) const
1173811767 return node;
1173911768}
1174011769
11770+ bool ValueIfNode::sameAs (const ExprNode* other, bool ignoreStreams) const
11771+ {
11772+ if (ExprNode::sameAs (other, ignoreStreams))
11773+ return true ;
11774+
11775+ return sameNodes (this , other->as <CoalesceNode>(), ignoreStreams);
11776+ }
11777+
1174111778ValueExprNode* ValueIfNode::pass2 (thread_db* tdbb, CompilerScratch* csb)
1174211779{
1174311780 ValueExprNode::pass2 (tdbb, csb);
0 commit comments