Search in sources :

Example 1 with ColumnProjector

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);
}
Also used : Format(java.text.Format) PDataType(org.apache.phoenix.schema.types.PDataType) ColumnProjector(org.apache.phoenix.compile.ColumnProjector)

Example 2 with ColumnProjector

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;
}
Also used : Array(java.sql.Array) ColumnProjector(org.apache.phoenix.compile.ColumnProjector)

Example 3 with ColumnProjector

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);
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) PhoenixStatement(org.apache.phoenix.jdbc.PhoenixStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) QueryPlan(org.apache.phoenix.compile.QueryPlan) PhoenixStatement(org.apache.phoenix.jdbc.PhoenixStatement) ColumnProjector(org.apache.phoenix.compile.ColumnProjector) Pair(org.apache.hadoop.hbase.util.Pair)

Example 4 with ColumnProjector

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;
}
Also used : PDatum(org.apache.phoenix.schema.PDatum) SelectStatement(org.apache.phoenix.parse.SelectStatement) QueryPlan(org.apache.phoenix.compile.QueryPlan) PTable(org.apache.phoenix.schema.PTable) ColumnProjector(org.apache.phoenix.compile.ColumnProjector)

Example 5 with ColumnProjector

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;
}
Also used : ColumnProjector(org.apache.phoenix.compile.ColumnProjector)

Aggregations

ColumnProjector (org.apache.phoenix.compile.ColumnProjector)8 QueryPlan (org.apache.phoenix.compile.QueryPlan)3 PDataType (org.apache.phoenix.schema.types.PDataType)3 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 Statement (java.sql.Statement)2 PhoenixStatement (org.apache.phoenix.jdbc.PhoenixStatement)2 Function (com.google.common.base.Function)1 Array (java.sql.Array)1 Format (java.text.Format)1 Pair (org.apache.hadoop.hbase.util.Pair)1 SelectStatement (org.apache.phoenix.parse.SelectStatement)1 PDatum (org.apache.phoenix.schema.PDatum)1 PTable (org.apache.phoenix.schema.PTable)1 ColumnInfo (org.apache.phoenix.util.ColumnInfo)1