[ 
https://issues.apache.org/jira/browse/DERBY-3880?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12641062#action_12641062
 ] 

Kathey Marsden commented on DERBY-3880:
---------------------------------------

Thanks Mamta for looking at this. I think the reason for the difference is that 
sourceRow has only 3 columns yet projectMapping has values  [3, -1, 5, -1, -1, 
2, -1], the third entry (5) does not refer to an actual column so when we get 
to 
        result.setColumn(index + 1, sourceRow.getColumn(projectMapping[index]));

We call ValueRow.getColumn()  which is defined as:
        public DataValueDescriptor      getColumn (int position) {
                if (position <= column.length)
                        return column[position-1];
                else
                        return (DataValueDescriptor)null;
        }

and return null because position is greater than column.length.

I found that projectMapping is originally generated in 
projectRestrictNode.generateMinion() when we call 
        int[] mapArray = resultColumns.mapSourceColumns();

ResultColumnList. mapSourceColumns for the column in question  executes this 
code:
else if (resultColumn.getExpression() instanceof ColumnReference)
                        {
                                ColumnReference cr = (ColumnReference) 
resultColumn.getExpression();
                                .....
                                        // Virtual column #s are 1-based
                                        mapArray[index] = 
cr.getSource().getVirtualColumnId();
                        }


Where the resultColumn looks like:
        ResultColumn  (id=72)   
        exposedName     "##aggregate expression"        
        virtualColumnId 3       
        expression      ColumnReference  (id=74)        
                columnName      "I2"    
                source  ResultColumn  (id=76)   
                        exposedName     "I2"    
                        virtualColumnId 5       
                        expression      VirtualColumnNode  (id=94)      
                                sourceColumn    ResultColumn  (id=100)  
                                        exposedName     "I2"    
                                        virtualColumnId 2       
                                        expression      VirtualColumnNode  
(id=102)     
                                                sourceColumn    ResultColumn  
(id=214)  
                                                        expression      
VirtualColumnNode  (id=227)     
                                                        virtualColumnId 2       
                                                                sourceColumn    
ResultColumn  (id=239)  
                                                                        
exposedName     "I2"    
                                                                        
expression      BaseColumnNode  (id=251)        
                                                                                
columnName      "I2"    
                                                                                
virtualColumnId 2       


The virtualColumnId of the ColumnReference under the ##aggregate expression is 
*5*, yet there are more deeply nested VirtualColumnNode's with a 
virtualColumnId of 2 which I think is correct.  I think I need to better 
understand mapSourceColumns()  and the meaning of this deeply nested 
ResultColumn structure to understand what's right here.

Kathey




> NPE on a query with having clause involving a join
> --------------------------------------------------
>
>                 Key: DERBY-3880
>                 URL: https://issues.apache.org/jira/browse/DERBY-3880
>             Project: Derby
>          Issue Type: Bug
>          Components: SQL
>    Affects Versions: 10.4.2.0
>         Environment: Windows 2003 Server 
>            Reporter: Venkateswaran Iyer
>            Priority: Minor
>         Attachments: querytree_fail.txt, querytree_works.txt
>
>
> A simple query involving a join and having clause causes a NPE. Any 
> subsequent executions cause severe errors. It almost looks like the 
> underlying connection was closed out.
> ====
> C:\apps\derby\db-derby-10.4.2.0-bin\db-derby-10.4.2.0-bin\bin>ij
> ij version 10.4
> ij> connect 'jdbc:derby://speed:1527/ClassicModels;user=sa;password=sa
> ';
> ij> create table t1(i int, c varchar(20));
> 0 rows inserted/updated/deleted
> ij> create table t2(i int, c2 varchar(20), i2 int);
> 0 rows inserted/updated/deleted
> ij> insert into t1 values(1, 'abc');
> 1 row inserted/updated/deleted
> ij> insert into t1 values(2, 'abc');
> 1 row inserted/updated/deleted
> ij> insert into t2 values(1, 'xyz', 10);
> 1 row inserted/updated/deleted
> ij> insert into t2 values(1, 'aaa', 20);
> 1 row inserted/updated/deleted
> ij> insert into t2 values(2, 'xxx', 30);
> 1 row inserted/updated/deleted
> ij> select t1.i, avg(t2.i2) from t1 inner join t2 on (t1.i = t2.i) group by 
> t1.i
>  having avg(t2.i2) > 0;
> ERROR XJ001: DERBY SQL error: SQLCODE: -1, SQLSTATE: XJ001, SQLERRMC: 
> java.lang.
> NullPointerException¶¶XJ001.U

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to