use of org.apache.phoenix.compile.ColumnProjector in project phoenix by apache.
the class PhoenixResultSet method getString.
@Override
public String getString(int columnIndex) throws SQLException {
checkCursorState();
// Get the value using the expected type instead of trying to coerce to VARCHAR.
// We can't coerce using our formatter because we don't have enough context in PDataType.
ColumnProjector projector = rowProjector.getColumnProjector(columnIndex - 1);
PDataType type = projector.getExpression().getDataType();
Object value = projector.getValue(currentRow, type, ptr);
if (wasNull = (value == null)) {
return null;
}
// Run Object through formatter to get String.
// This provides a simple way of getting a reasonable string representation
// for types like DATE and TIME
Format formatter = statement.getFormatter(type);
return formatter == null ? value.toString() : formatter.format(value);
}
use of org.apache.phoenix.compile.ColumnProjector in project phoenix by apache.
the class PhoenixResultSet method getArray.
@Override
public Array getArray(int columnIndex) throws SQLException {
checkCursorState();
// Get the value using the expected type instead of trying to coerce to VARCHAR.
// We can't coerce using our formatter because we don't have enough context in PDataType.
ColumnProjector projector = rowProjector.getColumnProjector(columnIndex - 1);
Array value = (Array) projector.getValue(currentRow, projector.getExpression().getDataType(), ptr);
wasNull = (value == null);
return value;
}
use of org.apache.phoenix.compile.ColumnProjector in project phoenix by apache.
the class QuerySchemaParserFunction method apply.
@Override
public Pair<String, String> apply(final String selectStatement) {
Preconditions.checkNotNull(selectStatement);
Preconditions.checkArgument(!selectStatement.isEmpty(), "Select Query is empty!!");
Connection connection = null;
try {
connection = ConnectionUtil.getInputConnection(this.configuration);
final Statement statement = connection.createStatement();
final PhoenixStatement pstmt = statement.unwrap(PhoenixStatement.class);
final QueryPlan queryPlan = pstmt.compileQuery(selectStatement);
isValidStatement(queryPlan);
final String tableName = queryPlan.getTableRef().getTable().getName().getString();
final List<? extends ColumnProjector> projectedColumns = queryPlan.getProjector().getColumnProjectors();
final List<String> columns = Lists.transform(projectedColumns, new Function<ColumnProjector, String>() {
@Override
public String apply(ColumnProjector column) {
return column.getName();
}
});
final String columnsAsStr = Joiner.on(",").join(columns);
return new Pair<String, String>(tableName, columnsAsStr);
} catch (SQLException e) {
LOG.error(String.format(" Error [%s] parsing SELECT query [%s] ", e.getMessage(), selectStatement));
throw new RuntimeException(e);
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException sqle) {
LOG.error(" Error closing connection ");
throw new RuntimeException(sqle);
}
}
}
}
use of org.apache.phoenix.compile.ColumnProjector in project phoenix by apache.
the class QueryOptimizer method getApplicablePlans.
private List<QueryPlan> getApplicablePlans(QueryPlan dataPlan, PhoenixStatement statement, List<? extends PDatum> targetColumns, ParallelIteratorFactory parallelIteratorFactory, boolean stopAtBestPlan) throws SQLException {
SelectStatement select = (SelectStatement) dataPlan.getStatement();
// Exit early if we have a point lookup as we can't get better than that
if (!useIndexes || (dataPlan.getContext().getScanRanges().isPointLookup() && stopAtBestPlan)) {
return Collections.singletonList(dataPlan);
}
// For single query tuple projection, indexes are inherited from the original table to the projected
// table; otherwise not. So we pass projected table here, which is enough to tell if this is from a
// single query or a part of join query.
List<PTable> indexes = Lists.newArrayList(dataPlan.getContext().getResolver().getTables().get(0).getTable().getIndexes());
if (indexes.isEmpty() || dataPlan.isDegenerate() || dataPlan.getTableRef().hasDynamicCols() || select.getHint().hasHint(Hint.NO_INDEX)) {
return Collections.singletonList(dataPlan);
}
// 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, FromCompiler.getResolver(dataPlan.getTableRef()));
List<QueryPlan> plans = Lists.newArrayListWithExpectedSize(1 + indexes.size());
plans.add(dataPlan);
QueryPlan hintedPlan = getHintedQueryPlan(statement, translatedIndexSelect, indexes, targetColumns, parallelIteratorFactory, plans);
if (hintedPlan != null) {
if (stopAtBestPlan) {
return Collections.singletonList(hintedPlan);
}
plans.add(0, hintedPlan);
}
for (PTable index : indexes) {
QueryPlan plan = addPlan(statement, translatedIndexSelect, index, targetColumns, parallelIteratorFactory, dataPlan, false);
if (plan != null) {
// Query can't possibly return anything so just return this plan.
if (plan.isDegenerate()) {
return Collections.singletonList(plan);
}
plans.add(plan);
}
}
return hintedPlan == null ? orderPlansBestToWorst(select, plans, stopAtBestPlan) : plans;
}
use of org.apache.phoenix.compile.ColumnProjector in project phoenix by apache.
the class PhoenixResultSet method getObject.
@Override
public Object getObject(int columnIndex) throws SQLException {
checkCursorState();
ColumnProjector projector = rowProjector.getColumnProjector(columnIndex - 1);
Object value = projector.getValue(currentRow, projector.getExpression().getDataType(), ptr);
wasNull = (value == null);
return value;
}
Aggregations