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

    https://github.com/apache/phoenix/pull/39#discussion_r25931807
  
    --- Diff: 
phoenix-core/src/main/java/org/apache/phoenix/compile/QueryCompiler.java ---
    @@ -147,21 +169,146 @@ public QueryCompiler(PhoenixStatement statement, 
SelectStatement select, ColumnR
          */
         public QueryPlan compile() throws SQLException{
             SelectStatement select = this.select;
    -        List<Object> binds = statement.getParameters();
    +        QueryPlan plan;
    +        if (isUnionAll()) {
    +            plan = compileUnionAll(select);
    +        } else {
    +            plan = compileSelect(select);
    +        }
    +        return plan;
    +    }
    +
    +    public QueryPlan compileSelect(SelectStatement select) throws 
SQLException{
    +     //   SelectStatement select = this.select;
    +           List<Object> binds = statement.getParameters();
    +           StatementContext context = new StatementContext(statement, 
resolver, scan, sequenceManager);
    +           ColumnResolver resolver;
    +           if (isUnionAll()) {
    +                   resolver = FromCompiler.getResolverForQuery(select, 
statement.getConnection());
    +                   context = new StatementContext(statement, resolver, new 
Scan(), sequenceManager);
    +           } 
    +           if (select.isJoin()) {
    +                   if (isUnionAll()) {
    +                           resolver = 
FromCompiler.getResolverForQuery(select, statement.getConnection());
    +                           select = JoinCompiler.optimize(statement, 
select, resolver);
    +                   }
    +                   else { 
    +                       select = JoinCompiler.optimize(statement, select, 
this.resolver);
    +                   }
    +                   if (this.select != select) {
    +                       ColumnResolver resolver1 = 
FromCompiler.getResolverForQuery(select, statement.getConnection());
    +                       context = new StatementContext(statement, 
resolver1, scan, sequenceManager);
    +                   }
    +                   JoinTable joinTable = JoinCompiler.compile(statement, 
select, context.getResolver());
    +                   return compileJoinQuery(context, binds, joinTable, 
false, false, null);
    +           } else {
    +               return compileSingleQuery(context, select, binds, false, 
true);
    +           }
    +    }
    +
    +    private void checkForOrderByLimitInUnionAllSelect(SelectStatement 
select) throws SQLException {
    +        if (select.getOrderBy() != null && !select.getOrderBy().isEmpty()) 
{
    +            throw new 
SQLExceptionInfo.Builder(SQLExceptionCode.ORDER_BY_IN_UNIONALL_SELECT_NOT_SUPPORTED).setMessage(".").build().buildException();
    +        }
    +        if (select.getLimit() != null) {
    +            throw new 
SQLExceptionInfo.Builder(SQLExceptionCode.LIMIT_IN_UNIONALL_SELECT_NOT_SUPPORTED).setMessage(".").build().buildException();
    +        }
    +    }
    +
    +    private PTable createTempTableForUnionAllResultResolver(QueryPlan 
plan) throws SQLException {
    +        List<PColumn> projectedColumns = new ArrayList<PColumn>();
    +        Long scn = statement.getConnection().getSCN();
    +        List<PColumnFamily> families = 
Collections.<PColumnFamily>emptyList(); // new ArrayList<PColumnFamily>();
    +        PTable theTable = new 
PTableImpl(statement.getConnection().getTenantId(), "unionAllSchema", 
"unionAllTable", scn == null ? HConstants.LATEST_TIMESTAMP : scn, families);
    +        PTable table = plan.getTableRef().getTable();
    +        for (int i=0; i< plan.getProjector().getColumnCount(); i++) {
    +            ColumnProjector colProj = 
plan.getProjector().getColumnProjector(i);
    +            Expression sourceExpression = colProj.getExpression();
    +            PColumnImpl projectedColumn = new 
PColumnImpl(PNameFactory.newName(colProj.getName().getBytes()), 
table.getDefaultFamilyName(),
    +                    sourceExpression.getDataType(), 
sourceExpression.getMaxLength(), sourceExpression.getScale(), 
sourceExpression.isNullable(),
    +                    i, sourceExpression.getSortOrder(), 50, new byte[0], 
true, sourceExpression.toString());
    +            projectedColumns.add(projectedColumn);
    +        }
    +        PTable t = PTableImpl.makePTable(theTable, projectedColumns);
    +        return t;
    +    }
    +
    +    private QueryPlan buildTupleProjectPlan(QueryPlan plan) throws 
SQLException {
    +        List<ExpressionProjector> projectedColumns = new 
ArrayList<ExpressionProjector>();
    +        PTable tbl = createTempTableForUnionAllResultResolver(plan);
    +        for (int i=0; i<tbl.getColumns().size(); i++) {
    +            ProjectedColumnExpression expression = new 
ProjectedColumnExpression(tbl.getColumns().get(i), tbl.getColumns(), i, 
tbl.getColumns().get(i).getExpressionStr());
    +            projectedColumns.add(new 
ExpressionProjector(tbl.getColumns().get(i).getName().getString(), 
tbl.getName().getString(), expression, true));
    +        }
    +        RowProjector rowProjector = new RowProjector(projectedColumns, 
100, true);
    +        TupleProjector tupleProjector = new TupleProjector(rowProjector);
    +        plan = new TupleProjectionPlan(plan, tupleProjector, null);
    +        return plan;
    +    }
    +    private boolean containOrderBy(SelectStatement select) {
    +        if (select.getOrderBy() != null && !select.getOrderBy().isEmpty()) 
    +            return true;
    +        else
    +            return false;
    +    }
    +
    +    private QueryPlan compileUnionAll(SelectStatement select) throws 
SQLException { 
    +        List<SelectStatement> unionAllSelects = select.getSelects();
    +        List<QueryPlan> plans = new ArrayList<QueryPlan>();
             StatementContext context = new StatementContext(statement, 
resolver, scan, sequenceManager);
    -        if (select.isJoin()) {
    -            select = JoinCompiler.optimize(statement, select, resolver);
    -            if (this.select != select) {
    -                ColumnResolver resolver = 
FromCompiler.getResolverForQuery(select, statement.getConnection());
    -                context = new StatementContext(statement, resolver, scan, 
sequenceManager);
    +
    +        checkForOrderByLimitInUnionAllSelect(select);
    +        QueryPlan plan = compileSelect(select);
    +        plan = buildTupleProjectPlan(plan);
    +        plans.add(plan);
    +        OrderBy orderBy = OrderBy.EMPTY_ORDER_BY;
    +        boolean containOrderBy = false;
    +        int numSelects = unionAllSelects.size();
    +        for (int i=0; i < numSelects; i++ ) {
    +            if (i < numSelects-1)
    +                
checkForOrderByLimitInUnionAllSelect(unionAllSelects.get(i));
    +            else if (i == numSelects-1) {
    +                containOrderBy = containOrderBy(unionAllSelects.get(i));
                 }
    -            JoinTable joinTable = JoinCompiler.compile(statement, select, 
context.getResolver());
    -            return compileJoinQuery(context, binds, joinTable, false, 
false, null);
    -        } else {
    -            return compileSingleQuery(context, select, binds, false, true);
    +            plan = compileSelect(unionAllSelects.get(i));
    +            if (containOrderBy) {
    +                orderBy = plan.getOrderBy();
    +                unionAllSelects.get(i).removeOrderBy();
    +                plan = compileSelect(unionAllSelects.get(i));
    +            } 
    +            plan = buildTupleProjectPlan(plan);
    +            plans.add(plan);
             }
    +        checkProjectionNumAndTypes(plans);
    +        plan =  new UnionPlan(context, plan.getStatement(), 
plan.getTableRef(), plan.getProjector(), plan.getLimit(), orderBy, 
parallelIteratorFactory, plan.getGroupBy(), plans);
    --- End diff --
    
    OrderBy is used by union all plan to get Order by information. Cannot be 
null if it has order by clause.
    
    When building the RowProjector, which "select" you referred to here. There 
are many selects in union all. Not matter which one you choose, effectively you 
pick one of the selects to build the RowProjector. It is the problem I have 
mentioned. And that is why I pass down RowProjector to result sets.
    RowProjector rowProjector = ProjectorCompiler.compile(context, select, 
groupBy, Collections.emptyList());
    
    From: maryannxue <[email protected]<mailto:[email protected]>>
    Reply-To: apache/phoenix 
<[email protected]<mailto:[email protected]>>
    Date: Thursday, March 5, 2015 at 7:43 PM
    To: apache/phoenix 
<[email protected]<mailto:[email protected]>>
    Cc: Alicia Shu <[email protected]<mailto:[email protected]>>
    Subject: Re: [phoenix] Phoenix-1580 union all impl (#39)
    
    
    In 
phoenix-core/src/main/java/org/apache/phoenix/compile/QueryCompiler.java<https://github.com/apache/phoenix/pull/39#discussion_r25924080>:
    
    >          }
    > +        checkProjectionNumAndTypes(plans);
    > +        plan =  new UnionPlan(context, plan.getStatement(), 
plan.getTableRef(), plan.getProjector(), plan.getLimit(), orderBy, 
parallelIteratorFactory, plan.getGroupBy(), plans);
    
    
    Should not pass the inner plan's attributes to UnionPlan, instead you 
should create its own tableRef, rowProjector, limit, orderBy, groupBy, etc.
    
    For orderBy, groupBy and limit, I think we should just leave them as NULL. 
And the tableRef and rowProjector is what really matters here.
    
    Since we have already used TupleProjectionPlan earlier, here the tableRef 
and rowProjector is more like a flat dummy one.
    
    We should create a table like (pretty much what 
createTempTableForUnionAllResultResolver() does):
    List columns = new ArrayList();
    // we'll use one of inner plan's row projector just for type information.
    for (ColumnProjector colProjector : 
plan.getProjector().getColumnProjectors()) {
    PColumn column = new PColumnImpl(colProject.getName(), ..., 
colProjector.getExpression().getType(), ...);
    }
    PTable tempTable = PTableImpl.makePTable("dummyName", "dummySchemaName", 
PTableType.SUBQUERY, columns);
    TableRef tableRef = new TableRef(null, tempTable, 0);
    
    GroupBy groupBy = GroupBy.EMPTY_GROUP_BY;
    OrderBy orderBy = OrderBy.EMPTY_ORDER_BY;
    context.setResolver(FromCompiler.getResolver(tableRef));
    RowProjector rowProjector = ProjectorCompiler.compile(context, select, 
groupBy, Collections.emptyList());
    
    plan = new UnionPlan(context, this.statement, tableRef, rowProjector, null, 
orderBy, parallelIteratorFactory, groupBy, plans);
    
    -
    Reply to this email directly or view it on 
GitHub<https://github.com/apache/phoenix/pull/39/files#r25924080>.



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to