phoenix-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ayingshu <...@git.apache.org>
Subject [GitHub] phoenix pull request: Phoenix-1580 union all impl
Date Fri, 06 Mar 2015 08:44:39 GMT
Github user ayingshu commented on a diff in the pull request:

    https://github.com/apache/phoenix/pull/39#discussion_r25931757
  
    --- 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);
    --- End diff --
    
    I was wrapping each subselect with TupleProjectionPlan suggested by you. So the method
buildTupleProjectPlan(plan) contains both creating the PTable and building the TupleProjectionPlan.
It was called by each subselect plan. That is why it is wrapped into a method.
    
    Look like you do not want to wrap the individual subselects and want to use its plan RowProjector
for each sub plan. Are you back to using TupleProjectionPlan for final UnionPlan only? I tested
this option before and got the same wrong result.
    
    From: maryannxue <notifications@github.com<mailto:notifications@github.com>>
    Reply-To: apache/phoenix <reply@reply.github.com<mailto:reply@reply.github.com>>
    Date: Thursday, March 5, 2015 at 7:19 PM
    To: apache/phoenix <phoenix@noreply.github.com<mailto:phoenix@noreply.github.com>>
    Cc: Alicia Shu <ashu@hortonworks.com<mailto:ashu@hortonworks.com>>
    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_r25923422>:
    
    >              }
    > -            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);
    
    
    Actually the buildTupleProjectPlan() is just a couple lines, maybe you don't even have
to write a function for this. The code would like:
    
    TupleProjector projector = new TupleProjector(plan.getRowProjector());
    plan = new TupleProjectionPlan(plan, projector, null);
    
    -
    Reply to this email directly or view it on GitHub<https://github.com/apache/phoenix/pull/39/files#r25923422>.



---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message