Search in sources :

Example 16 with StatementContext

use of org.apache.phoenix.compile.StatementContext in project phoenix by apache.

the class RoundRobinResultIteratorIT method testIteratorsPickedInRoundRobinFashionForSaltedTable.

@Test
public void testIteratorsPickedInRoundRobinFashionForSaltedTable() throws Exception {
    try (Connection conn = getConnection()) {
        String testTable = "testIteratorsPickedInRoundRobinFashionForSaltedTable".toUpperCase();
        Statement stmt = conn.createStatement();
        stmt.execute("CREATE TABLE " + testTable + "(K VARCHAR PRIMARY KEY) SALT_BUCKETS = 8");
        PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class);
        MockParallelIteratorFactory parallelIteratorFactory = new MockParallelIteratorFactory();
        phxConn.setIteratorFactory(parallelIteratorFactory);
        ResultSet rs = stmt.executeQuery("SELECT * FROM " + testTable);
        StatementContext ctx = rs.unwrap(PhoenixResultSet.class).getContext();
        PTable table = ctx.getResolver().getTables().get(0).getTable();
        parallelIteratorFactory.setTable(table);
        PhoenixStatement pstmt = stmt.unwrap(PhoenixStatement.class);
        int numIterators = pstmt.getQueryPlan().getSplits().size();
        assertEquals(8, numIterators);
        int numFetches = 2 * numIterators;
        List<String> iteratorOrder = new ArrayList<>(numFetches);
        for (int i = 1; i <= numFetches; i++) {
            rs.next();
            iteratorOrder.add(rs.getString(1));
        }
        /*
             * Because TableResultIterators are created in parallel in multiple threads, their relative order is not
             * deterministic. However, once the iterators are assigned to a RoundRobinResultIterator, the order in which
             * the next iterator is picked is deterministic - i1, i2, .. i7, i8, i1, i2, .. i7, i8, i1, i2, ..
             */
        for (int i = 0; i < numIterators; i++) {
            assertEquals(iteratorOrder.get(i), iteratorOrder.get(i + numIterators));
        }
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) PhoenixStatement(org.apache.phoenix.jdbc.PhoenixStatement) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ArrayList(java.util.ArrayList) PhoenixStatement(org.apache.phoenix.jdbc.PhoenixStatement) PTable(org.apache.phoenix.schema.PTable) StatementContext(org.apache.phoenix.compile.StatementContext) PhoenixResultSet(org.apache.phoenix.jdbc.PhoenixResultSet) ResultSet(java.sql.ResultSet) PhoenixResultSet(org.apache.phoenix.jdbc.PhoenixResultSet) Test(org.junit.Test)

Example 17 with StatementContext

use of org.apache.phoenix.compile.StatementContext in project phoenix by apache.

the class PhoenixMetricsIT method changeInternalStateForTesting.

private void changeInternalStateForTesting(PhoenixResultSet rs) {
    // get and set the internal state for testing purposes.
    ReadMetricQueue testMetricsQueue = new TestReadMetricsQueue(true);
    StatementContext ctx = (StatementContext) Whitebox.getInternalState(rs, "context");
    Whitebox.setInternalState(ctx, "readMetricsQueue", testMetricsQueue);
    Whitebox.setInternalState(rs, "readMetricsQueue", testMetricsQueue);
}
Also used : StatementContext(org.apache.phoenix.compile.StatementContext)

Example 18 with StatementContext

use of org.apache.phoenix.compile.StatementContext in project phoenix by apache.

the class CursorFetchPlan method iterator.

@Override
public ResultIterator iterator(ParallelScanGrouper scanGrouper, Scan scan) throws SQLException {
    StatementContext context = delegate.getContext();
    if (resultIterator == null) {
        context.getOverallQueryMetrics().startQuery();
        resultIterator = new CursorResultIterator(LookAheadResultIterator.wrap(delegate.iterator(scanGrouper, scan)), cursorName);
    }
    return resultIterator;
}
Also used : CursorResultIterator(org.apache.phoenix.iterate.CursorResultIterator) StatementContext(org.apache.phoenix.compile.StatementContext)

Example 19 with StatementContext

use of org.apache.phoenix.compile.StatementContext 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.add(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);
        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) FilterResultIterator(org.apache.phoenix.iterate.FilterResultIterator) ResultIterator(org.apache.phoenix.iterate.ResultIterator) JobCallable(org.apache.phoenix.job.JobManager.JobCallable) HashCacheClient(org.apache.phoenix.join.HashCacheClient) PTable(org.apache.phoenix.schema.PTable) StatementContext(org.apache.phoenix.compile.StatementContext) FilterResultIterator(org.apache.phoenix.iterate.FilterResultIterator) SelectStatement(org.apache.phoenix.parse.SelectStatement) 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) SQLParser(org.apache.phoenix.parse.SQLParser) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ParseNode(org.apache.phoenix.parse.ParseNode) ExecutionException(java.util.concurrent.ExecutionException) ConnectionQueryServices(org.apache.phoenix.query.ConnectionQueryServices)

Aggregations

StatementContext (org.apache.phoenix.compile.StatementContext)19 PhoenixStatement (org.apache.phoenix.jdbc.PhoenixStatement)10 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)8 ParseNode (org.apache.phoenix.parse.ParseNode)7 SQLException (java.sql.SQLException)6 ColumnResolver (org.apache.phoenix.compile.ColumnResolver)6 Expression (org.apache.phoenix.expression.Expression)6 ArrayList (java.util.ArrayList)5 SQLParser (org.apache.phoenix.parse.SQLParser)5 PTable (org.apache.phoenix.schema.PTable)5 Scan (org.apache.hadoop.hbase.client.Scan)4 RowKeyColumnExpression (org.apache.phoenix.expression.RowKeyColumnExpression)4 ResultIterator (org.apache.phoenix.iterate.ResultIterator)4 PDataType (org.apache.phoenix.schema.types.PDataType)4 PreparedStatement (java.sql.PreparedStatement)3 List (java.util.List)3 RowProjector (org.apache.phoenix.compile.RowProjector)3 KeyValueColumnExpression (org.apache.phoenix.expression.KeyValueColumnExpression)3 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)3 PhoenixResultSet (org.apache.phoenix.jdbc.PhoenixResultSet)3