Search in sources :

Example 26 with ResultIterator

use of org.apache.phoenix.iterate.ResultIterator in project phoenix by apache.

the class HashJoinPlan method iterator.

@Override
public ResultIterator iterator(ParallelScanGrouper scanGrouper, Scan scan) throws SQLException {
    if (scan == null) {
        scan = delegate.getContext().getScan();
    }
    int count = subPlans.length;
    PhoenixConnection connection = getContext().getConnection();
    ConnectionQueryServices services = connection.getQueryServices();
    ExecutorService executor = services.getExecutor();
    List<Future<ServerCache>> futures = Lists.newArrayListWithExpectedSize(count);
    if (joinInfo != null) {
        hashClient = hashClient != null ? hashClient : new HashCacheClient(delegate.getContext().getConnection());
        firstJobEndTime = new AtomicLong(0);
        keyRangeExpressions = new CopyOnWriteArrayList<Expression>();
    }
    for (int i = 0; i < count; i++) {
        final int index = i;
        futures.add(executor.submit(new JobCallable<ServerCache>() {

            @Override
            public ServerCache call() throws Exception {
                ServerCache cache = subPlans[index].execute(HashJoinPlan.this);
                return cache;
            }

            @Override
            public Object getJobId() {
                return HashJoinPlan.this;
            }

            @Override
            public TaskExecutionMetricsHolder getTaskExecutionMetric() {
                return NO_OP_INSTANCE;
            }
        }));
    }
    SQLException firstException = null;
    for (int i = 0; i < count; i++) {
        try {
            ServerCache result = futures.get(i).get();
            if (result != null) {
                dependencies.put(new ImmutableBytesPtr(result.getId()), result);
            }
            subPlans[i].postProcess(result, this);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            if (firstException == null) {
                firstException = new SQLExceptionInfo.Builder(SQLExceptionCode.INTERRUPTED_EXCEPTION).setRootCause(e).setMessage("Sub plan [" + i + "] execution interrupted.").build().buildException();
            }
        } catch (ExecutionException e) {
            if (firstException == null) {
                firstException = new SQLException("Encountered exception in sub plan [" + i + "] execution.", e.getCause());
            }
        }
    }
    if (firstException != null) {
        SQLCloseables.closeAllQuietly(dependencies.values());
        throw firstException;
    }
    Expression postFilter = null;
    boolean hasKeyRangeExpressions = keyRangeExpressions != null && !keyRangeExpressions.isEmpty();
    if (recompileWhereClause || hasKeyRangeExpressions) {
        StatementContext context = delegate.getContext();
        PTable table = context.getCurrentTable().getTable();
        ParseNode viewWhere = table.getViewStatement() == null ? null : new SQLParser(table.getViewStatement()).parseQuery().getWhere();
        context.setResolver(FromCompiler.getResolverForQuery((SelectStatement) (delegate.getStatement()), delegate.getContext().getConnection()));
        if (recompileWhereClause) {
            postFilter = WhereCompiler.compile(delegate.getContext(), delegate.getStatement(), viewWhere, null);
        }
        if (hasKeyRangeExpressions) {
            WhereCompiler.compile(delegate.getContext(), delegate.getStatement(), viewWhere, keyRangeExpressions, true, null);
        }
    }
    if (joinInfo != null) {
        HashJoinInfo.serializeHashJoinIntoScan(scan, joinInfo);
    }
    ResultIterator iterator = joinInfo == null ? delegate.iterator(scanGrouper, scan) : ((BaseQueryPlan) delegate).iterator(dependencies, scanGrouper, scan);
    if (statement.getInnerSelectStatement() != null && postFilter != null) {
        iterator = new FilterResultIterator(iterator, postFilter);
    }
    return iterator;
}
Also used : ServerCache(org.apache.phoenix.cache.ServerCacheClient.ServerCache) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) SQLException(java.sql.SQLException) JobCallable(org.apache.phoenix.job.JobManager.JobCallable) PTable(org.apache.phoenix.schema.PTable) StatementContext(org.apache.phoenix.compile.StatementContext) ExecutionException(java.util.concurrent.ExecutionException) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) FilterResultIterator(org.apache.phoenix.iterate.FilterResultIterator) ResultIterator(org.apache.phoenix.iterate.ResultIterator) HashCacheClient(org.apache.phoenix.join.HashCacheClient) FilterResultIterator(org.apache.phoenix.iterate.FilterResultIterator) AtomicLong(java.util.concurrent.atomic.AtomicLong) Expression(org.apache.phoenix.expression.Expression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) InListExpression(org.apache.phoenix.expression.InListExpression) RowValueConstructorExpression(org.apache.phoenix.expression.RowValueConstructorExpression) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ConnectionQueryServices(org.apache.phoenix.query.ConnectionQueryServices)

Aggregations

ResultIterator (org.apache.phoenix.iterate.ResultIterator)26 SequenceResultIterator (org.apache.phoenix.iterate.SequenceResultIterator)9 List (java.util.List)8 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)8 LimitingResultIterator (org.apache.phoenix.iterate.LimitingResultIterator)8 OffsetResultIterator (org.apache.phoenix.iterate.OffsetResultIterator)8 SQLException (java.sql.SQLException)7 ConcatResultIterator (org.apache.phoenix.iterate.ConcatResultIterator)7 PColumn (org.apache.phoenix.schema.PColumn)7 TableRef (org.apache.phoenix.schema.TableRef)7 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)6 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)5 DelegateResultIterator (org.apache.phoenix.iterate.DelegateResultIterator)5 ParallelIterators (org.apache.phoenix.iterate.ParallelIterators)5 SpoolingResultIterator (org.apache.phoenix.iterate.SpoolingResultIterator)5 PTable (org.apache.phoenix.schema.PTable)5 Tuple (org.apache.phoenix.schema.tuple.Tuple)5 ArrayList (java.util.ArrayList)4 QueryPlan (org.apache.phoenix.compile.QueryPlan)4 StatementContext (org.apache.phoenix.compile.StatementContext)4