Search in sources :

Example 6 with ColumnProjector

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

the class PhoenixResultSetMetaData method getColumnDisplaySize.

@Override
public int getColumnDisplaySize(int column) throws SQLException {
    ColumnProjector projector = rowProjector.getColumnProjector(column - 1);
    PDataType type = projector.getExpression().getDataType();
    if (type == null) {
        return QueryConstants.NULL_DISPLAY_TEXT.length();
    }
    if (type.isCoercibleTo(PDate.INSTANCE)) {
        return connection.getDatePattern().length();
    }
    if (projector.getExpression().getMaxLength() != null) {
        return projector.getExpression().getMaxLength();
    }
    return DEFAULT_DISPLAY_WIDTH;
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) ColumnProjector(org.apache.phoenix.compile.ColumnProjector)

Example 7 with ColumnProjector

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

the class SqlQueryToColumnInfoFunction method apply.

@Override
public List<ColumnInfo> apply(String sqlQuery) {
    Preconditions.checkNotNull(sqlQuery);
    Connection connection = null;
    List<ColumnInfo> columnInfos = null;
    try {
        connection = ConnectionUtil.getInputConnection(this.configuration);
        final Statement statement = connection.createStatement();
        final PhoenixStatement pstmt = statement.unwrap(PhoenixStatement.class);
        final QueryPlan queryPlan = pstmt.compileQuery(sqlQuery);
        final List<? extends ColumnProjector> projectedColumns = queryPlan.getProjector().getColumnProjectors();
        columnInfos = Lists.newArrayListWithCapacity(projectedColumns.size());
        columnInfos = Lists.transform(projectedColumns, new Function<ColumnProjector, ColumnInfo>() {

            @Override
            public ColumnInfo apply(final ColumnProjector columnProjector) {
                return new ColumnInfo(columnProjector.getName(), columnProjector.getExpression().getDataType().getSqlType());
            }
        });
    } catch (SQLException e) {
        LOG.error(String.format(" Error [%s] parsing SELECT query [%s] ", e.getMessage(), sqlQuery));
        throw new RuntimeException(e);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException sqle) {
                LOG.error("Error closing connection!!");
                throw new RuntimeException(sqle);
            }
        }
    }
    return columnInfos;
}
Also used : Function(com.google.common.base.Function) SQLException(java.sql.SQLException) PhoenixStatement(org.apache.phoenix.jdbc.PhoenixStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ColumnInfo(org.apache.phoenix.util.ColumnInfo) QueryPlan(org.apache.phoenix.compile.QueryPlan) PhoenixStatement(org.apache.phoenix.jdbc.PhoenixStatement) ColumnProjector(org.apache.phoenix.compile.ColumnProjector)

Example 8 with ColumnProjector

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

the class PhoenixResultSet method getBoolean.

@Override
public boolean getBoolean(int columnIndex) throws SQLException {
    checkCursorState();
    ColumnProjector colProjector = rowProjector.getColumnProjector(columnIndex - 1);
    PDataType type = colProjector.getExpression().getDataType();
    Object value = colProjector.getValue(currentRow, type, ptr);
    wasNull = (value == null);
    if (value == null) {
        return false;
    }
    if (type == PBoolean.INSTANCE) {
        return Boolean.TRUE.equals(value);
    } else if (type == PVarchar.INSTANCE) {
        return !STRING_FALSE.equals(value);
    } else if (type == PInteger.INSTANCE) {
        return !INTEGER_FALSE.equals(value);
    } else if (type == PDecimal.INSTANCE) {
        return !BIG_DECIMAL_FALSE.equals(value);
    } else {
        throw new SQLExceptionInfo.Builder(SQLExceptionCode.CANNOT_CALL_METHOD_ON_TYPE).setMessage("Method: getBoolean; Type:" + type).build().buildException();
    }
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) 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