return optimize(select, statement, Collections.<PColumn>emptyList(), null);
}
public QueryPlan optimize(SelectStatement select, PhoenixStatement statement, List<? extends PDatum> targetColumns, ParallelIteratorFactory parallelIteratorFactory) throws SQLException {
QueryCompiler compiler = new QueryCompiler(statement, targetColumns, parallelIteratorFactory);
QueryPlan dataPlan = compiler.compile(select);
if (!useIndexes || select.getFrom().size() > 1) {
return dataPlan;
}
// Get the statement as it's been normalized now
// TODO: the recompile for the index tables could skip the normalize step
select = (SelectStatement)dataPlan.getStatement();
PTable dataTable = dataPlan.getTableRef().getTable();
List<PTable>indexes = Lists.newArrayList(dataTable.getIndexes());
if (indexes.isEmpty() || dataPlan.getTableRef().hasDynamicCols() || select.getHint().hasHint(Hint.NO_INDEX)) {
return dataPlan;
}
// The targetColumns is set for UPSERT SELECT to ensure that the proper type conversion takes place.
// For a SELECT, it is empty. In this case, we want to set the targetColumns to match the projection
// from the dataPlan to ensure that the metadata for when an index is used matches the metadata for
// when the data table is used.
if (targetColumns.isEmpty()) {
List<? extends ColumnProjector> projectors = dataPlan.getProjector().getColumnProjectors();
List<PDatum> targetDatums = Lists.newArrayListWithExpectedSize(projectors.size());
for (ColumnProjector projector : projectors) {
targetDatums.add(projector.getExpression());
}
targetColumns = targetDatums;
}
SelectStatement translatedIndexSelect = IndexStatementRewriter.translate(select, dataPlan.getContext().getResolver());
List<QueryPlan> plans = Lists.newArrayListWithExpectedSize(1 + indexes.size());
plans.add(dataPlan);
QueryPlan hintedPlan = getHintedQueryPlan(statement, translatedIndexSelect, indexes, targetColumns, parallelIteratorFactory, plans);
if (hintedPlan != null) {
return hintedPlan;
}
for (PTable index : indexes) {
addPlan(statement, translatedIndexSelect, index, targetColumns, parallelIteratorFactory, plans);