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

Enze Liu edited comment on CALCITE-4721 at 8/10/21, 12:46 AM:
--------------------------------------------------------------

Actually, we don't have to set hints.

when do sqlNode level manipulation, like remove certain function call , this 
kind of code will panic.
{code:java}
  1 @Override
  2 public SqlNode visit(SqlCall call) {
  3     if (call.getKind().equals(SqlKind.OTHER_FUNCTION) && ((SqlCall) 
call).getOperator().getName().equals("date")) {
  4         return ((SqlBasicCall) rand).getOperands()[0];
  5     }
  6
  7     List<SqlNode> operands = call.getOperandList();
  8     if (operands == null) {
  9         return call;
 10     }
 11
 12     List<SqlNode> newOperands = operands.stream().map(rand -> {
 13         if (rand == null) {
 14             return rand;
 15         }
 16         return rand.accept(this);
 17     }.collect(Collectors.toList());
 18
 19     AtomicInteger i = new AtomicInteger();
 20     newOperands.forEach(rand -> {
 21         int index = i.getAndIncrement();
 22         call.setOperand(index, rand);
 23     });
 24     return call;
 25 }
~
{code}
 

surely we can change to this. but I think maybe, this kind of behavior is wrong 
? 

 
{code:java}
@Override
public SqlNode visit(SqlCall call) {
    List<SqlNode> operands = call.getOperandList();
    if (operands == null) {
        return call;
    }    
    AtomicInteger i = new AtomicInteger();
    operands.forEach(rand -> {
        int index = i.getAndIncrement();        
        if (rand == null) {
            return;
        }
        if (rand.getKind().equals(SqlKind.OTHER_FUNCTION) && ((SqlCall) 
rand).getOperator().getName().equals("date")) {
            call.setOperand(index, ((SqlBasicCall) rand).getOperands()[0]);
            return;
        }
        rand.accept(this);
    });    
    return call;
}                   {code}
 


was (Author: enze):
Actually, we don't have to set hints.

when do sqlNode level manipulation, like remove certain function call , this 
kind of code will panic.
{code:java}
  1 @Override
  2 public SqlNode visit(SqlCall call) {
  3     if (call.getKind().equals(SqlKind.OTHER_FUNCTION) && ((SqlCall) 
call).getOperator().getName().equals("date")) {
  4         return ((SqlBasicCall) rand).getOperands()[0];
  5     }
  6
  7     List<SqlNode> operands = call.getOperandList();
  8     if (operands == null) {
  9         return call;
 10     }
 11
 12     List<SqlNode> newOperands = operands.stream().map(rand -> {
 13         if (rand == null) {
 14             return rand;
 15         }
 16         return rand.accept(this);
 17     }.collect(Collectors.toList());
 18
 19     AtomicInteger i = new AtomicInteger();
 20     newOperands.forEach(rand -> {
 21         int index = i.getAndIncrement();
 22         call.setOperand(index, rand);
 23     });
 24     return call;
 25 }
~
{code}
 

surely we can change to this. but I think maybe, this kind of behavior is wrong 
? 

 
{code:java}
@Override
public SqlNode visit(SqlCall call) {
    List<SqlNode> operands = call.getOperandList();
    if (operands == null) {
        return call;
    }    AtomicInteger i = new AtomicInteger();
    operands.forEach(rand -> {
        int index = i.getAndIncrement();        if (rand == null) {
            return;
        }
        if (rand.getKind().equals(SqlKind.OTHER_FUNCTION) && ((SqlCall) 
rand).getOperator().getName().equals("date")) {
            call.setOperand(index, ((SqlBasicCall) rand).getOperands()[0]);
            return;
        }
        rand.accept(this);
    });    return call;
}                   {code}
 

> SqlSelect's setOperand method cannot set `hints`, failed with AssertionError
> ----------------------------------------------------------------------------
>
>                 Key: CALCITE-4721
>                 URL: https://issues.apache.org/jira/browse/CALCITE-4721
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 1.27.0
>            Reporter: Enze Liu
>            Priority: Minor
>              Labels: pull-requests-available
>          Time Spent: 10m
>  Remaining Estimate: 0h
>
> in SqlSelect, 
> getOperandList() returns with 
> {code:java}
> return ImmutableNullableList.of(keywordList, selectList, from, where,
>  groupBy, having, windowDecls, orderBy, offset, fetch, hints);
> {code}
> while we trying to setOperand() by the returned list, we have AssertionError.
> seems the setOperand only set the first ten operands. 
> {code:java}
>   @Override public void setOperand(int i, @Nullable SqlNode operand) {
>     switch (i) {
>     case 0:
>       keywordList = requireNonNull((SqlNodeList) operand);
>       break;
>     case 1:
>       selectList = requireNonNull((SqlNodeList) operand);
>       break;
>     case 2:
>       from = operand;
>       break;
>     case 3:
>       where = operand;
>       break;
>     case 4:
>       groupBy = (SqlNodeList) operand;
>       break;
>     case 5:
>       having = operand;
>       break;
>     case 6:
>       windowDecls = requireNonNull((SqlNodeList) operand);
>       break;
>     case 7:
>       orderBy = (SqlNodeList) operand;
>       break;
>     case 8:
>       offset = operand;
>       break;
>     case 9:
>       fetch = operand;
>       break;
>     default:
>       throw new AssertionError(i);
>     }
>   }
> {code}
>  
> a minor modification could be:
> {code:java}
>   @Override public void setOperand(int i, @Nullable SqlNode operand) {
>     switch (i) {
>     case 0:
>       keywordList = requireNonNull((SqlNodeList) operand);
>       break;
>     case 1:
>       selectList = requireNonNull((SqlNodeList) operand);
>       break;
>     case 2:
>       from = operand;
>       break;
>     case 3:
>       where = operand;
>       break;
>     case 4:
>       groupBy = (SqlNodeList) operand;
>       break;
>     case 5:
>       having = operand;
>       break;
>     case 6:
>       windowDecls = requireNonNull((SqlNodeList) operand);
>       break;
>     case 7:
>       orderBy = (SqlNodeList) operand;
>       break;
>     case 8:
>       offset = operand;
>       break;
>     case 9:
>       fetch = operand;
>       break;
>     case 10: 
>       hints = operand; 
>       break;
>     default:
>       throw new AssertionError(i);
>     }
>   }
> {code}
>   



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to