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

ASF GitHub Bot commented on TRAFODION-1610:
-------------------------------------------

Github user DaveBirdsall commented on a diff in the pull request:

    
https://github.com/apache/incubator-trafodion/pull/1253#discussion_r142711212
  
    --- Diff: core/sql/generator/GenRelUpdate.cpp ---
    @@ -292,8 +292,85 @@ static short genUpdExpr(
       return 0;
     }
     
    -static short genUpdConstraintExpr(Generator *generator)
    +// Used to generate update or insert constraint expressions for update 
operators
    +static short genUpdConstraintExpr(Generator * generator,
    +                                  ItemExpr * constrTree,
    +                                  const ValueIdSet & constraintColumns,
    +                                  ValueIdArray & targetRecExprArray,
    +                                  ex_expr ** targetExpr /* out */)
     {
    +  ExpGenerator * expGen = generator->getExpGenerator();
    +
    +  // The Attributes for the table columns refer to the old values of the 
column.
    +  // The constraints must operate on the new values, though. So we must do 
a
    +  // switcheroo on the Attributes for the update expression. The target 
value IDs
    +  // come from targetRecExprArray.
    +
    +  ValueIdList savedSourceVIDlist;
    +  NAList<Attributes*> savedSourceAttrsList(generator->wHeap());
    +
    +  for (ValueId sourceValId = constraintColumns.init();
    +       constraintColumns.next(sourceValId);
    +       constraintColumns.advance(sourceValId))
    +    {
    +      NAColumn * sourceCol = 
((IndexColumn*)sourceValId.getItemExpr())->getNAColumn();
    +      ValueId targetValId;
    +      NABoolean found = FALSE;
    +      for (CollIndex ni = 0; (!found) && (ni < 
targetRecExprArray.entries()); ni++)
    +        {
    +          const ItemExpr *assignExpr = 
targetRecExprArray[ni].getItemExpr();
    +          targetValId = 
assignExpr->child(0)->castToItemExpr()->getValueId();
    +          NAColumn *targetCol = NULL;         
    +          if (targetValId.getItemExpr()->getOperatorType() == 
ITM_BASECOLUMN)
    +            targetCol = 
((BaseColumn*)targetValId.getItemExpr())->getNAColumn();
    +          else if (targetValId.getItemExpr()->getOperatorType() == 
ITM_INDEXCOLUMN)
    +            targetCol = 
((IndexColumn*)targetValId.getItemExpr())->getNAColumn();
    +                
    +          if ((targetCol) &&
    +              (targetCol->getColName() == sourceCol->getColName()) &&
    +              (targetCol->getHbaseColFam() == sourceCol->getHbaseColFam()) 
&&
    +              (targetCol->getHbaseColQual() == 
sourceCol->getHbaseColQual()) &&
    +              
(targetCol->getNATable()->getTableName().getQualifiedNameAsAnsiString() ==
    +               
sourceCol->getNATable()->getTableName().getQualifiedNameAsAnsiString()))
    +            {
    +              found = TRUE;
    +              break;
    +            }
    +        }
    +
    +      if (found)
    +   {
    +          Attributes * sourceValAttr = (generator->addMapInfo(sourceValId, 
0))->getAttr();
    +          Attributes * targetValAttr = (generator->getMapInfo(targetValId, 
0))->getAttr();
    +
    +          // Save original location attributes so we can change them back 
after
    +          // generating the update constraint expression
    +
    +          Attributes * savedValAttr = new(generator->wHeap()) Attributes();
    +          savedValAttr->copyLocationAttrs(sourceValAttr);
    +          savedSourceAttrsList.insert(savedValAttr);
    +          savedSourceVIDlist.insert(sourceValId);
    +
    +          sourceValAttr->copyLocationAttrs(targetValAttr);
    +        }
    +
    +    }
    +
    +  // Now that we have remapped the Attributes for the columns to their 
values
    +  // in the new record, we can generate the update constraint expression.
    +
    +  expGen->generateExpr(constrTree->getValueId(), ex_expr::exp_SCAN_PRED,
    --- End diff --
    
    I stepped through the code for an example of this. In the example I looked 
at, it appears the assign list is complete. My example was an unsalted table 
with three columns, and I did a MERGE that updated one column, with a set 
clause of C = 'GLAD'. I found that the assign list for the update record had 
all three columns in it:
    
    (gdb) p targetRecExprArray.entries()
    $2 = 3
    (gdb) p targetRecExprArray[0].getItemExpr()->display()
    (TRAFODION.SCH.T2.A ASSIGN TRAFODION.SCH.T2.A)
    $3 = void
    (gdb) p targetRecExprArray[1].getItemExpr()->display()
    (TRAFODION.SCH.T2.B ASSIGN TRAFODION.SCH.T2.B)
    $4 = void
    (gdb) p targetRecExprArray[2].getItemExpr()->display()
    (TRAFODION.SCH.T2.C ASSIGN %('GLAD'))
    $5 = void
    (gdb) 
    
    In a nutshell, then, the constraint expression is generated whole, and the 
columns that aren't affected by the update simply refer to their old values.
    
    It does seem like there is an optimization opportunity here: If the 
constraint expression doesn't refer to a changed column, we could omit it. If 
it is not too much trouble, I will do this.


> Generate constraint expression for update/merge commands
> --------------------------------------------------------
>
>                 Key: TRAFODION-1610
>                 URL: https://issues.apache.org/jira/browse/TRAFODION-1610
>             Project: Apache Trafodion
>          Issue Type: Sub-task
>          Components: sql-cmp
>            Reporter: Selvaganesan Govindarajan
>            Assignee: David Wayne Birdsall
>




--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to