public AbstractExpression replaceWithTVE ( Map < AbstractExpression , Integer > aggTableIndexMap , Map < Integer , ParsedColInfo > indexToColumnMap ) { Integer ii = aggTableIndexMap . get ( this ) ; if ( ii != null ) { ParsedColInfo col = indexToColumnMap . get ( ii ) ; TupleValueExpression tve = new TupleValueExpression ( col . m_tableName , col . m_tableAlias , col . m_columnName , col . m_alias , this , ii ) ; if ( this instanceof TupleValueExpression ) { tve . setOrigStmtId ( ( ( TupleValueExpression ) this ) . getOrigStmtId ( ) ) ; } // To prevent pushdown of LIMIT when ORDER BY references an agg. ENG-3487. if ( hasAnySubexpressionOfClass ( AggregateExpression . class ) ) { tve . setHasAggregate ( true ) ; } return tve ; } AbstractExpression lnode = null ; AbstractExpression rnode = null ; if ( m_left != null ) { lnode = m_left . replaceWithTVE ( aggTableIndexMap , indexToColumnMap ) ; } if ( m_right != null ) { rnode = m_right . replaceWithTVE ( aggTableIndexMap , indexToColumnMap ) ; } ArrayList < AbstractExpression > newArgs = null ; boolean changed = false ; if ( m_args != null ) { newArgs = new ArrayList <> ( ) ; for ( AbstractExpression expr : m_args ) { AbstractExpression ex = expr . replaceWithTVE ( aggTableIndexMap , indexToColumnMap ) ; newArgs . add ( ex ) ; if ( ex != expr ) { changed = true ; } } } if ( m_left != lnode || m_right != rnode || changed ) { AbstractExpression resExpr = clone ( ) ; resExpr . setLeft ( lnode ) ; resExpr . setRight ( rnode ) ; resExpr . setArgs ( newArgs ) ; return resExpr ; } return this ; }
This function recursively replaces any subexpression matching an entry in aggTableIndexMap with an equivalent TVE . Its column index and alias are also built up here .