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));
}
}
}
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);
}
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;
}
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;
}
Aggregations