Java Code Examples for org.apache.phoenix.schema.PTable#getViewStatement()

The following examples show how to use org.apache.phoenix.schema.PTable#getViewStatement() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QueryCompiler.java    From phoenix with Apache License 2.0 4 votes vote down vote up
protected QueryPlan compileSingleFlatQuery(StatementContext context, SelectStatement select, List<Object> binds, boolean asSubquery, boolean allowPageFilter, QueryPlan innerPlan, TupleProjector innerPlanTupleProjector, boolean isInRowKeyOrder) throws SQLException{
    PhoenixConnection connection = statement.getConnection();
    ColumnResolver resolver = context.getResolver();
    TableRef tableRef = context.getCurrentTable();
    PTable table = tableRef.getTable();
    
    ParseNode viewWhere = null;
    if (table.getViewStatement() != null) {
        viewWhere = new SQLParser(table.getViewStatement()).parseQuery().getWhere();
    }
    Integer limit = LimitCompiler.compile(context, select);

    GroupBy groupBy = GroupByCompiler.compile(context, select, innerPlanTupleProjector, isInRowKeyOrder);
    // Optimize the HAVING clause by finding any group by expressions that can be moved
    // to the WHERE clause
    select = HavingCompiler.rewrite(context, select, groupBy);
    Expression having = HavingCompiler.compile(context, select, groupBy);
    // Don't pass groupBy when building where clause expression, because we do not want to wrap these
    // expressions as group by key expressions since they're pre, not post filtered.
    if (innerPlan == null) {
        context.setResolver(FromCompiler.getResolverForQuery(select, connection));
    }
    Set<SubqueryParseNode> subqueries = Sets.<SubqueryParseNode> newHashSet();
    Expression where = WhereCompiler.compile(context, select, viewWhere, subqueries);
    context.setResolver(resolver); // recover resolver
    OrderBy orderBy = OrderByCompiler.compile(context, select, groupBy, limit, isInRowKeyOrder); 
    RowProjector projector = ProjectionCompiler.compile(context, select, groupBy, asSubquery ? Collections.<PDatum>emptyList() : targetColumns);
    
    // Final step is to build the query plan
    int maxRows = statement.getMaxRows();
    if (maxRows > 0) {
        if (limit != null) {
            limit = Math.min(limit, maxRows);
        } else {
            limit = maxRows;
        }
    }
    
    QueryPlan plan = innerPlan;
    if (plan == null) {
        ParallelIteratorFactory parallelIteratorFactory = asSubquery ? null : this.parallelIteratorFactory;
        plan = select.isAggregate() || select.isDistinct() ? 
                  new AggregatePlan(context, select, tableRef, projector, limit, orderBy, parallelIteratorFactory, groupBy, having)
                : new ScanPlan(context, select, tableRef, projector, limit, orderBy, parallelIteratorFactory, allowPageFilter);
    }
    if (!subqueries.isEmpty()) {
        int count = subqueries.size();
        WhereClauseSubPlan[] subPlans = new WhereClauseSubPlan[count];
        int i = 0;
        for (SubqueryParseNode subqueryNode : subqueries) {
            SelectStatement stmt = subqueryNode.getSelectNode();
            subPlans[i++] = new WhereClauseSubPlan(compileSubquery(stmt), stmt, subqueryNode.expectSingleRow());
        }
        plan = HashJoinPlan.create(select, plan, null, subPlans);
    }
    
    if (innerPlan != null) {
        if (LiteralExpression.isTrue(where)) {
            where = null; // we do not pass "true" as filter
        }
        plan =  select.isAggregate() || select.isDistinct() ?
                  new ClientAggregatePlan(context, select, tableRef, projector, limit, where, orderBy, groupBy, having, plan)
                : new ClientScanPlan(context, select, tableRef, projector, limit, where, orderBy, plan);

    }
    
    return plan;
}
 
Example 2
Source File: WhereConstantParser.java    From phoenix with Apache License 2.0 4 votes vote down vote up
public static PTable addViewInfoToPColumnsIfNeeded(PTable view) throws SQLException {
    byte[][] viewColumnConstantsToBe = new byte[view.getColumns().size()][];
    if (view.getViewStatement() == null) {
    	return view;
    }
    SelectStatement select = new SQLParser(view.getViewStatement()).parseQuery();
    ParseNode whereNode = select.getWhere();
    ColumnResolver resolver = FromCompiler.getResolver(new TableRef(view));

    try (PhoenixConnection conn = getConnectionlessConnection()) {
        StatementContext context = new StatementContext(new PhoenixStatement(conn), resolver);

        Expression expression;
        try {
            expression = WhereCompiler.compile(context, whereNode);
        } catch (ColumnNotFoundException e) {
            // if we could not find a column used in the view statement
            // (which means its was dropped) this view is not valid any more
            return null;
        }
        CreateTableCompiler.ViewWhereExpressionVisitor visitor = new CreateTableCompiler
                .ViewWhereExpressionVisitor(view, viewColumnConstantsToBe);
        expression.accept(visitor);

        BitSet isViewColumnReferencedToBe = new BitSet(view.getColumns().size());
        // Used to track column references in a view
        ExpressionCompiler expressionCompiler = new CreateTableCompiler
                .ColumnTrackingExpressionCompiler(context, isViewColumnReferencedToBe);
        whereNode.accept(expressionCompiler);

        List<PColumn> result = Lists.newArrayList();
        for (PColumn column : PTableImpl.getColumnsToClone(view)) {
            boolean isViewReferenced = isViewColumnReferencedToBe.get(column.getPosition());
            if ((visitor.isUpdatable() || view.getPKColumns()
                    .get(MetaDataUtil.getAutoPartitionColIndex(view)).equals(column))
                    && viewColumnConstantsToBe[column.getPosition()] != null) {
                result.add(new PColumnImpl(column,
                        viewColumnConstantsToBe[column.getPosition()], isViewReferenced));
            }
            // If view is not updatable, viewColumnConstants should be empty. We will still
            // inherit our parent viewConstants, but we have no additional ones.
            else if (isViewReferenced ){
                result.add(new PColumnImpl(column, column.getViewConstant(), isViewReferenced));
            } else {
                result.add(column);
            }
        }
        return PTableImpl.builderWithColumns(view, result)
                .build();
    }
}