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

    https://github.com/apache/phoenix/pull/417#discussion_r243724103
  
    --- Diff: 
phoenix-core/src/main/java/org/apache/phoenix/compile/OrderByCompiler.java ---
    @@ -88,7 +88,8 @@ public static OrderBy compile(StatementContext context,
                                       Integer offset,
                                       RowProjector rowProjector,
                                       TupleProjector tupleProjector,
    -                                  boolean isInRowKeyOrder) throws 
SQLException {
    +                                  boolean isInRowKeyOrder,
    +                                  Expression whereExpression) throws 
SQLException {
    --- End diff --
    
    We need the whereExpression because in 
OrderPreservingTracker.IsConstantVisitor, we need whereExpression to help us to 
check the columnExpression is constant or not. Just take the same sql as 
example:  
    
    select a.ak3 from
    (select rand() ak1,length(pk2) ak2,length(pk3) ak3,length(v1) 
av1,length(v2) av2 from test order by pk2,pk3 limit 10) a
    where a.ak1 = 0.0 and a.av2 = length(substr('abc',1,1))
    group by a.ak3,CASE WHEN coalesce(a.ak1,1) > coalesce(a.av2,2) THEN 
coalesce(a.ak1,1) ELSE coalesce(a.av2,2) END,a.av1
    order by a.ak3,a.av1
    
    We need to check if "CASE WHEN coalesce(a.ak1,1) > coalesce(a.av2,2) THEN 
coalesce(a.ak1,1) ELSE coalesce(a.av2,2) END,a.av1" is constant in 
OrderPreservingTracker.hasEqualityConstraints method, so we move forward to 
check if the a.ak1 and a.av2 are constant in  
OrderPreservingTracker.IsConstantVisitor, and we must visit the where clause 
"where a.ak1 = 0.0 and a.av2 = length(substr('abc',1,1))" to check the a.ak1 
and a.av2.
    
    BTW. There is no whereExpression in current code of OrderByCompiler because 
for PHOENIX-3451 which was completed by James and me before, 
RowKeyColumnExpression is only supported to check in 
OrderPreservingTracker.hasEqualityConstraints, and two FIXME tags were left in 
this method at at time:
    
     private boolean hasEqualityConstraints(int startPos, int endPos) {
            ScanRanges ranges = context.getScanRanges();
            // If a GROUP BY is being done, then the rows are ordered according 
to the GROUP BY key,
            // not by the original row key order of the table (see 
PHOENIX-3451).
            // We check each GROUP BY expression to see if it only references 
columns that are
            // matched by equality constraints, in which case the expression 
itself would be constant.
            // FIXME: this only recognizes row key columns that are held 
constant, not all columns.
            // FIXME: we should optimize out any GROUP BY or ORDER BY 
expression which is deemed to
            // be held constant based on the WHERE clause.
            if (!groupBy.isEmpty()) {
    
    In this patch, I added whereExpression to try to fix this problem to 
support non-pk columns also.


---

Reply via email to