[ 
https://issues.apache.org/jira/browse/CALCITE-5927?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17753802#comment-17753802
 ] 

Julian Hyde commented on CALCITE-5927:
--------------------------------------

[~qiang.wang] I’m sure you’re right, that this is a bug. To help those of us 
understand it who are not reading through the code, can you provide a simple 
test case? e.g. a query involving EMP and DEPT and some derived columns where 
this rule makes the wrong decision. Those test cases are useful for people 
figuring out whether they are running into this bug. After you have constructed 
that test case you may even be able to change the summary to describe the bug 
based on its symptoms. 

> LoptOptimizeJoinRule has wrong condition when finding out if Self-Join keys 
> are unique
> --------------------------------------------------------------------------------------
>
>                 Key: CALCITE-5927
>                 URL: https://issues.apache.org/jira/browse/CALCITE-5927
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 1.33.0, 1.34.0, 1.35.0
>            Reporter: qiang.wang
>            Priority: Minor
>
> There's wrong condition in function _areSelfJoinKeysUnique._ 
>  
> {code:java}
> // Make sure each key on the left maps to the same simple column as the
> // corresponding key on the right
> for (IntPair pair : joinInfo.pairs()) {
>   final RelColumnOrigin leftOrigin =
>       mq.getColumnOrigin(leftRel, pair.source);
>   if (leftOrigin == null || !leftOrigin.isDerived()) {
>     return false;
>   }
>   final RelColumnOrigin rightOrigin =
>       mq.getColumnOrigin(rightRel, pair.target);
>   if (rightOrigin == null || !rightOrigin.isDerived()) {
>     return false;
>   }
>   if (leftOrigin.getOriginColumnOrdinal()
>       != rightOrigin.getOriginColumnOrdinal()) {
>     return false;
>   }
> } {code}
> The wrong conditions are '{_}if (leftOrigin == null || 
> !leftOrigin.isDerived()){_}' and '{_}if (rightOrigin == null || 
> !rightOrigin.isDerived()){_}'.  
> This function wants to find out if the self-join keys are unique. so for each 
> self-join key, find
> _leftOrigin_ and _rightOrigin_ first, then will return false if any of them 
> is null. But why it returns false when any of them is not _Derived?_  I think 
> exactly the opposite is right.
>  I think this is a bug comes from CALCITE-4251
> Before that PR, this function will return false only when _leftOrigin_ or 
> _rightOrigin_ is null, and the function _RelMetadataQuery#getColumnOrigin_ 
> will return null if column is derived, so the logic is : 'this function will 
> return null when  _leftOrigin_ or _rightOrigin_ is null or is derived', but 
> now is : 'this function will return null when  _leftOrigin_ or _rightOrigin_ 
> is null or is not derived'.
>  



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to