Search in sources :

Example 56 with PColumn

use of org.apache.phoenix.schema.PColumn in project phoenix by apache.

the class IndexStatementRewriter method visit.

@Override
public ParseNode visit(ColumnParseNode node) throws SQLException {
    ColumnRef dataColRef = getResolver().resolveColumn(node.getSchemaName(), node.getTableName(), node.getName());
    PColumn dataCol = dataColRef.getColumn();
    TableRef dataTableRef = dataColRef.getTableRef();
    // view constants if based on an UPDATABLE view
    if (dataCol.getViewConstant() != null) {
        byte[] viewConstant = dataCol.getViewConstant();
        // Ignore last byte, as it's only there so we can have a way to differentiate null
        // from the absence of a value.
        ptr.set(viewConstant, 0, viewConstant.length - 1);
        Object literal = dataCol.getDataType().toObject(ptr);
        return new LiteralParseNode(literal, dataCol.getDataType());
    }
    TableName tName = getReplacedTableName(dataTableRef);
    if (multiTableRewriteMap != null && tName == null)
        return node;
    String indexColName = IndexUtil.getIndexColumnName(dataCol);
    ParseNode indexColNode = new ColumnParseNode(tName, '"' + indexColName + '"', node.getAlias());
    PDataType indexColType = IndexUtil.getIndexColumnDataType(dataCol);
    PDataType dataColType = dataColRef.getColumn().getDataType();
    // TODO: test case for this
    if (!isTopLevel() && indexColType != dataColType) {
        indexColNode = FACTORY.cast(indexColNode, dataColType, null, null);
    }
    return indexColNode;
}
Also used : PColumn(org.apache.phoenix.schema.PColumn) TableName(org.apache.phoenix.parse.TableName) PDataType(org.apache.phoenix.schema.types.PDataType) ColumnParseNode(org.apache.phoenix.parse.ColumnParseNode) WildcardParseNode(org.apache.phoenix.parse.WildcardParseNode) LiteralParseNode(org.apache.phoenix.parse.LiteralParseNode) ColumnParseNode(org.apache.phoenix.parse.ColumnParseNode) TableWildcardParseNode(org.apache.phoenix.parse.TableWildcardParseNode) ParseNode(org.apache.phoenix.parse.ParseNode) FamilyWildcardParseNode(org.apache.phoenix.parse.FamilyWildcardParseNode) ColumnRef(org.apache.phoenix.schema.ColumnRef) TableRef(org.apache.phoenix.schema.TableRef) LiteralParseNode(org.apache.phoenix.parse.LiteralParseNode)

Example 57 with PColumn

use of org.apache.phoenix.schema.PColumn in project phoenix by apache.

the class JoinCompiler method joinProjectedTables.

public static PTable joinProjectedTables(PTable left, PTable right, JoinType type) throws SQLException {
    Preconditions.checkArgument(left.getType() == PTableType.PROJECTED);
    Preconditions.checkArgument(right.getType() == PTableType.PROJECTED);
    List<PColumn> merged = Lists.<PColumn>newArrayList();
    if (type == JoinType.Full) {
        for (PColumn c : left.getColumns()) {
            merged.add(new ProjectedColumn(c.getName(), c.getFamilyName(), c.getPosition(), true, ((ProjectedColumn) c).getSourceColumnRef(), SchemaUtil.isPKColumn(c) ? null : c.getName().getBytes()));
        }
    } else {
        merged.addAll(left.getColumns());
    }
    int position = merged.size();
    for (PColumn c : right.getColumns()) {
        if (!SchemaUtil.isPKColumn(c)) {
            PColumn column = new ProjectedColumn(c.getName(), c.getFamilyName(), position++, type == JoinType.Inner ? c.isNullable() : true, ((ProjectedColumn) c).getSourceColumnRef(), c.getName().getBytes());
            merged.add(column);
        }
    }
    if (left.getBucketNum() != null) {
        merged.remove(0);
    }
    return PTableImpl.makePTable(left.getTenantId(), left.getSchemaName(), PNameFactory.newName(SchemaUtil.getTableName(left.getName().getString(), right.getName().getString())), left.getType(), left.getIndexState(), left.getTimeStamp(), left.getSequenceNumber(), left.getPKName(), left.getBucketNum(), merged, left.getParentSchemaName(), left.getParentTableName(), left.getIndexes(), left.isImmutableRows(), Collections.<PName>emptyList(), null, null, PTable.DEFAULT_DISABLE_WAL, left.isMultiTenant(), left.getStoreNulls(), left.getViewType(), left.getViewIndexId(), left.getIndexType(), left.rowKeyOrderOptimizable(), left.isTransactional(), left.getUpdateCacheFrequency(), left.getIndexDisableTimestamp(), left.isNamespaceMapped(), left.getAutoPartitionSeqName(), left.isAppendOnlySchema(), ONE_CELL_PER_COLUMN, NON_ENCODED_QUALIFIERS, PTable.EncodedCQCounter.NULL_COUNTER, left.useStatsForParallelization());
}
Also used : PColumn(org.apache.phoenix.schema.PColumn) ProjectedColumn(org.apache.phoenix.schema.ProjectedColumn) Hint(org.apache.phoenix.parse.HintNode.Hint) PTinyint(org.apache.phoenix.schema.types.PTinyint) PSmallint(org.apache.phoenix.schema.types.PSmallint)

Example 58 with PColumn

use of org.apache.phoenix.schema.PColumn in project phoenix by apache.

the class IndexHalfStoreFileReaderGenerator method getViewConstants.

private byte[][] getViewConstants(PTable dataTable) {
    int dataPosOffset = (dataTable.getBucketNum() != null ? 1 : 0) + (dataTable.isMultiTenant() ? 1 : 0);
    byte[][] viewConstants = null;
    int nViewConstants = 0;
    if (dataTable.getType() == PTableType.VIEW) {
        ImmutableBytesWritable ptr = new ImmutableBytesWritable();
        List<PColumn> dataPkColumns = dataTable.getPKColumns();
        for (int i = dataPosOffset; i < dataPkColumns.size(); i++) {
            PColumn dataPKColumn = dataPkColumns.get(i);
            if (dataPKColumn.getViewConstant() != null) {
                nViewConstants++;
            }
        }
        if (nViewConstants > 0) {
            viewConstants = new byte[nViewConstants][];
            int j = 0;
            for (int i = dataPosOffset; i < dataPkColumns.size(); i++) {
                PColumn dataPkColumn = dataPkColumns.get(i);
                if (dataPkColumn.getViewConstant() != null) {
                    if (IndexUtil.getViewConstantValue(dataPkColumn, ptr)) {
                        viewConstants[j++] = ByteUtil.copyKeyBytesIfNecessary(ptr);
                    } else {
                        throw new IllegalStateException();
                    }
                }
            }
        }
    }
    return viewConstants;
}
Also used : PColumn(org.apache.phoenix.schema.PColumn) ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable)

Example 59 with PColumn

use of org.apache.phoenix.schema.PColumn in project phoenix by apache.

the class CorrelatePlanTest method createProjectedTableFromLiterals.

private TableRef createProjectedTableFromLiterals(Object[] row) {
    List<PColumn> columns = Lists.<PColumn>newArrayList();
    for (int i = 0; i < row.length; i++) {
        String name = ParseNodeFactory.createTempAlias();
        Expression expr = LiteralExpression.newConstant(row[i]);
        PName colName = PNameFactory.newName(name);
        columns.add(new PColumnImpl(PNameFactory.newName(name), PNameFactory.newName(VALUE_COLUMN_FAMILY), expr.getDataType(), expr.getMaxLength(), expr.getScale(), expr.isNullable(), i, expr.getSortOrder(), null, null, false, name, false, false, colName.getBytes()));
    }
    try {
        PTable pTable = PTableImpl.makePTable(null, PName.EMPTY_NAME, PName.EMPTY_NAME, PTableType.SUBQUERY, null, MetaDataProtocol.MIN_TABLE_TIMESTAMP, PTable.INITIAL_SEQ_NUM, null, null, columns, null, null, Collections.<PTable>emptyList(), false, Collections.<PName>emptyList(), null, null, false, false, false, null, null, null, true, false, 0, 0L, Boolean.FALSE, null, false, ImmutableStorageScheme.ONE_CELL_PER_COLUMN, QualifierEncodingScheme.NON_ENCODED_QUALIFIERS, EncodedCQCounter.NULL_COUNTER, true);
        TableRef sourceTable = new TableRef(pTable);
        List<ColumnRef> sourceColumnRefs = Lists.<ColumnRef>newArrayList();
        for (PColumn column : sourceTable.getTable().getColumns()) {
            sourceColumnRefs.add(new ColumnRef(sourceTable, column.getPosition()));
        }
        return new TableRef(TupleProjectionCompiler.createProjectedTable(sourceTable, sourceColumnRefs, false));
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : PColumnImpl(org.apache.phoenix.schema.PColumnImpl) SQLException(java.sql.SQLException) PTable(org.apache.phoenix.schema.PTable) PColumn(org.apache.phoenix.schema.PColumn) Expression(org.apache.phoenix.expression.Expression) ProjectedColumnExpression(org.apache.phoenix.expression.ProjectedColumnExpression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) ComparisonExpression(org.apache.phoenix.expression.ComparisonExpression) CorrelateVariableFieldAccessExpression(org.apache.phoenix.expression.CorrelateVariableFieldAccessExpression) PName(org.apache.phoenix.schema.PName) ColumnRef(org.apache.phoenix.schema.ColumnRef) TableRef(org.apache.phoenix.schema.TableRef)

Example 60 with PColumn

use of org.apache.phoenix.schema.PColumn in project phoenix by apache.

the class LiteralResultIteratorPlanTest method testLiteralResultIteratorPlan.

private void testLiteralResultIteratorPlan(Object[][] expectedResult, Integer offset, Integer limit) throws SQLException {
    QueryPlan plan = newLiteralResultIterationPlan(offset, limit);
    ResultIterator iter = plan.iterator();
    ImmutableBytesWritable ptr = new ImmutableBytesWritable();
    for (Object[] row : expectedResult) {
        Tuple next = iter.next();
        assertNotNull(next);
        for (int i = 0; i < row.length; i++) {
            PColumn column = table.getColumns().get(i);
            boolean eval = new ProjectedColumnExpression(column, table, column.getName().getString()).evaluate(next, ptr);
            Object o = eval ? column.getDataType().toObject(ptr) : null;
            assertEquals(row[i], o);
        }
    }
    assertNull(iter.next());
}
Also used : PColumn(org.apache.phoenix.schema.PColumn) ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) ResultIterator(org.apache.phoenix.iterate.ResultIterator) ProjectedColumnExpression(org.apache.phoenix.expression.ProjectedColumnExpression) QueryPlan(org.apache.phoenix.compile.QueryPlan) Tuple(org.apache.phoenix.schema.tuple.Tuple) SingleKeyValueTuple(org.apache.phoenix.schema.tuple.SingleKeyValueTuple)

Aggregations

PColumn (org.apache.phoenix.schema.PColumn)101 PTable (org.apache.phoenix.schema.PTable)59 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)26 Expression (org.apache.phoenix.expression.Expression)21 TableRef (org.apache.phoenix.schema.TableRef)20 ArrayList (java.util.ArrayList)19 PName (org.apache.phoenix.schema.PName)18 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)17 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)17 ImmutableBytesPtr (org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)17 ColumnRef (org.apache.phoenix.schema.ColumnRef)17 Hint (org.apache.phoenix.parse.HintNode.Hint)14 PTableKey (org.apache.phoenix.schema.PTableKey)14 ColumnNotFoundException (org.apache.phoenix.schema.ColumnNotFoundException)13 PColumnFamily (org.apache.phoenix.schema.PColumnFamily)13 PSmallint (org.apache.phoenix.schema.types.PSmallint)13 SQLException (java.sql.SQLException)12 ProjectedColumnExpression (org.apache.phoenix.expression.ProjectedColumnExpression)12 PColumnImpl (org.apache.phoenix.schema.PColumnImpl)12 Map (java.util.Map)11