Java Code Examples for org.apache.phoenix.schema.PColumn#getViewConstant()

The following examples show how to use org.apache.phoenix.schema.PColumn#getViewConstant() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FromCompiler.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public static ColumnResolver getResolverForCompiledDerivedTable(PhoenixConnection connection, TableRef tableRef, RowProjector projector)
        throws SQLException {
    List<PColumn> projectedColumns = new ArrayList<PColumn>();
    List<Expression> sourceExpressions = new ArrayList<Expression>();
    PTable table = tableRef.getTable();
    for (PColumn column : table.getColumns()) {
        Expression sourceExpression = projector.getColumnProjector(column.getPosition()).getExpression();
        PColumnImpl projectedColumn = new PColumnImpl(column.getName(), column.getFamilyName(),
                sourceExpression.getDataType(), sourceExpression.getMaxLength(), sourceExpression.getScale(), sourceExpression.isNullable(),
                column.getPosition(), sourceExpression.getSortOrder(), column.getArraySize(), column.getViewConstant(), column.isViewReferenced(), column.getExpressionStr());
        projectedColumns.add(projectedColumn);
        sourceExpressions.add(sourceExpression);
    }
    PTable t = PTableImpl.makePTable(table, projectedColumns);
    return new SingleTableColumnResolver(connection, new TableRef(tableRef.getTableAlias(), t, tableRef.getLowerBoundTimeStamp(), tableRef.hasDynamicCols()));
}
 
Example 2
Source File: UpsertCompiler.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private static SelectStatement prependTenantAndViewConstants(PTable table, SelectStatement select, String tenantId, Set<PColumn> addViewColumns) {
    if ((!table.isMultiTenant() || tenantId == null) && table.getViewIndexId() == null && addViewColumns.isEmpty()) {
        return select;
    }
    List<AliasedNode> selectNodes = newArrayListWithCapacity(select.getSelect().size() + 1 + addViewColumns.size());
    if (table.isMultiTenant() && tenantId != null) {
        selectNodes.add(new AliasedNode(null, new LiteralParseNode(tenantId)));
    }
    if (table.getViewIndexId() != null) {
        selectNodes.add(new AliasedNode(null, new LiteralParseNode(table.getViewIndexId())));
    }
    selectNodes.addAll(select.getSelect());
    for (PColumn column : addViewColumns) {
        byte[] byteValue = column.getViewConstant();
        Object value = column.getDataType().toObject(byteValue, 0, byteValue.length-1);
        selectNodes.add(new AliasedNode(null, new LiteralParseNode(value)));
    }
    
    return SelectStatement.create(select, selectNodes);
}
 
Example 3
Source File: JoinCompiler.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public ProjectedPTableWrapper createProjectedTable(RowProjector rowProjector) throws SQLException {
    assert(isSubselect());
    List<PColumn> projectedColumns = new ArrayList<PColumn>();
    List<Expression> sourceExpressions = new ArrayList<Expression>();
    ListMultimap<String, String> columnNameMap = ArrayListMultimap.<String, String>create();
    PTable table = tableRef.getTable();
    for (PColumn column : table.getColumns()) {
        String colName = getProjectedColumnName(null, tableRef.getTableAlias(), column.getName().getString());
        Expression sourceExpression = rowProjector.getColumnProjector(column.getPosition()).getExpression();
        PColumnImpl projectedColumn = new PColumnImpl(PNameFactory.newName(colName), PNameFactory.newName(TupleProjector.VALUE_COLUMN_FAMILY),
                sourceExpression.getDataType(), sourceExpression.getMaxLength(), sourceExpression.getScale(), sourceExpression.isNullable(),
                column.getPosition(), sourceExpression.getSortOrder(), column.getArraySize(), column.getViewConstant(), column.isViewReferenced(), column.getExpressionStr());
        projectedColumns.add(projectedColumn);
        sourceExpressions.add(sourceExpression);
    }
    PTable t = PTableImpl.makePTable(table.getTenantId(), PNameFactory.newName(PROJECTED_TABLE_SCHEMA), table.getName(), PTableType.JOIN,
                table.getIndexState(), table.getTimeStamp(), table.getSequenceNumber(), table.getPKName(),
                null, projectedColumns, table.getParentSchemaName(),
                table.getParentTableName(), table.getIndexes(), table.isImmutableRows(), Collections.<PName>emptyList(), null, null,
                table.isWALDisabled(), table.isMultiTenant(), table.getStoreNulls(), table.getViewType(),
                table.getViewIndexId(), table.getIndexType());
    return new ProjectedPTableWrapper(t, columnNameMap, sourceExpressions);
}
 
Example 4
Source File: FromCompiler.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public static ColumnResolver getResolverForCompiledDerivedTable(PhoenixConnection connection, TableRef tableRef, RowProjector projector)
        throws SQLException {
    List<PColumn> projectedColumns = new ArrayList<PColumn>();
    PTable table = tableRef.getTable();
    for (PColumn column : table.getColumns()) {
        Expression sourceExpression = projector.getColumnProjector(column.getPosition()).getExpression();
        PColumnImpl projectedColumn = new PColumnImpl(column.getName(), column.getFamilyName(),
                sourceExpression.getDataType(), sourceExpression.getMaxLength(), sourceExpression.getScale(), sourceExpression.isNullable(),
                column.getPosition(), sourceExpression.getSortOrder(), column.getArraySize(), column.getViewConstant(), column.isViewReferenced(), column.getExpressionStr(), column.isRowTimestamp(), column.isDynamic(), column.getColumnQualifierBytes(),
            column.getTimestamp());
        projectedColumns.add(projectedColumn);
    }
    PTable t = PTableImpl.builderWithColumns(table, projectedColumns)
            .build();
    return new SingleTableColumnResolver(connection, new TableRef(tableRef.getTableAlias(), t, tableRef.getLowerBoundTimeStamp(), tableRef.hasDynamicCols()));
}
 
Example 5
Source File: UpsertCompiler.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private static SelectStatement prependTenantAndViewConstants(PTable table, SelectStatement select, String tenantId, Set<PColumn> addViewColumns, boolean useServerTimestamp) {
    if ((!table.isMultiTenant() || tenantId == null) && table.getViewIndexId() == null && addViewColumns.isEmpty() && !useServerTimestamp) {
        return select;
    }
    List<AliasedNode> selectNodes = newArrayListWithCapacity(select.getSelect().size() + 1 + addViewColumns.size());
    if (table.getViewIndexId() != null) {
        selectNodes.add(new AliasedNode(null, new LiteralParseNode(table.getViewIndexId())));
    }
    if (table.isMultiTenant() && tenantId != null) {
        selectNodes.add(new AliasedNode(null, new LiteralParseNode(tenantId)));
    }
    selectNodes.addAll(select.getSelect());
    for (PColumn column : addViewColumns) {
        byte[] byteValue = column.getViewConstant();
        Object value = column.getDataType().toObject(byteValue, 0, byteValue.length-1);
        selectNodes.add(new AliasedNode(null, new LiteralParseNode(value)));
    }
    if (useServerTimestamp) {
        PColumn rowTimestampCol = table.getPKColumns().get(table.getRowTimestampColPos());
        selectNodes.add(new AliasedNode(null, getNodeForRowTimestampColumn(rowTimestampCol)));
    }
    return SelectStatement.create(select, selectNodes);
}
 
Example 6
Source File: IndexUtil.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public static byte[][] getViewConstants(PTable dataTable) {
    if (dataTable.getType() != PTableType.VIEW && dataTable.getType() != PTableType.PROJECTED) return null;
    int dataPosOffset = (dataTable.getBucketNum() != null ? 1 : 0) + (dataTable.isMultiTenant() ? 1 : 0);
    ImmutableBytesWritable ptr = new ImmutableBytesWritable();
    List<byte[]> viewConstants = new ArrayList<byte[]>();
    List<PColumn> dataPkColumns = dataTable.getPKColumns();
    for (int i = dataPosOffset; i < dataPkColumns.size(); i++) {
        PColumn dataPKColumn = dataPkColumns.get(i);
        if (dataPKColumn.getViewConstant() != null) {
            if (IndexUtil.getViewConstantValue(dataPKColumn, ptr)) {
                viewConstants.add(ByteUtil.copyKeyBytesIfNecessary(ptr));
            } else {
                throw new IllegalStateException();
            }
        }
    }
    return viewConstants.isEmpty() ? null : viewConstants
            .toArray(new byte[viewConstants.size()][]);
}
 
Example 7
Source File: IndexHalfStoreFileReaderGenerator.java    From phoenix with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 8
Source File: IndexStatementRewriter.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@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();
    // Rewrite view constants as literals, as they won't be in the schema for
    // an index on the view. Our view may be READ_ONLY yet still have inherited
    // 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();

    // Coerce index column reference back to same type as data column so that
    // expression behave exactly the same. No need to invert, as this will be done
    // automatically as needed. If node is used at the top level, do not convert, as
    // otherwise the wrapper gets in the way in the group by clause. For example,
    // an INTEGER column in a GROUP BY gets doubly wrapped like this:
    //     CAST CAST int_col AS INTEGER AS DECIMAL
    // This is unnecessary and problematic in the case of a null value.
    // TODO: test case for this
    if (!isTopLevel() && indexColType != dataColType) {
        indexColNode = FACTORY.cast(indexColNode, dataColType, null, null);
    }
    return indexColNode;
}
 
Example 9
Source File: JoinCompiler.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private void addProjectedColumn(List<PColumn> projectedColumns, List<Expression> sourceExpressions,
        ListMultimap<String, String> columnNameMap, PColumn sourceColumn, PName familyName, boolean hasSaltingColumn,
        boolean isLocalIndexColumnRef, StatementContext context)
throws SQLException {
    if (sourceColumn == SALTING_COLUMN)
        return;

    int position = projectedColumns.size() + (hasSaltingColumn ? 1 : 0);
    PTable table = tableRef.getTable();
    String schemaName = table.getSchemaName().getString();
    String tableName = table.getTableName().getString();
    String colName = isLocalIndexColumnRef ? IndexUtil.getIndexColumnName(sourceColumn) : sourceColumn.getName().getString();
    String fullName = getProjectedColumnName(schemaName, tableName, colName);
    String aliasedName = tableRef.getTableAlias() == null ? fullName : getProjectedColumnName(null, tableRef.getTableAlias(), colName);

    columnNameMap.put(colName, aliasedName);
    if (!fullName.equals(aliasedName)) {
        columnNameMap.put(fullName, aliasedName);
    }

    PName name = PNameFactory.newName(aliasedName);
    PColumnImpl column = new PColumnImpl(name, familyName, sourceColumn.getDataType(),
            sourceColumn.getMaxLength(), sourceColumn.getScale(), sourceColumn.isNullable(),
            position, sourceColumn.getSortOrder(), sourceColumn.getArraySize(), sourceColumn.getViewConstant(), sourceColumn.isViewReferenced(), sourceColumn.getExpressionStr());
    Expression sourceExpression = isLocalIndexColumnRef ?
              NODE_FACTORY.column(TableName.create(schemaName, tableName), "\"" + colName + "\"", null).accept(new ExpressionCompiler(context))
            : new ColumnRef(tableRef, sourceColumn.getPosition()).newColumnExpression();
    projectedColumns.add(column);
    sourceExpressions.add(sourceExpression);
}
 
Example 10
Source File: BaseQueryPlan.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private void serializeViewConstantsIntoScan(Scan scan, PTable dataTable) {
    int dataPosOffset = (dataTable.getBucketNum() != null ? 1 : 0) + (dataTable.isMultiTenant() ? 1 : 0);
    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) {
            byte[][] 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();
                    }
                }
            }
            serializeViewConstantsIntoScan(viewConstants, scan);
        }
    }
}
 
Example 11
Source File: IndexUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static boolean getViewConstantValue(PColumn column, ImmutableBytesWritable ptr) {
    byte[] value = column.getViewConstant();
    if (value != null) {
        ptr.set(value, 0, value.length-1);
        return true;
    }
    return false;
}
 
Example 12
Source File: IndexHalfStoreFileReaderGenerator.java    From phoenix with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 13
Source File: IndexStatementRewriter.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@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();
    // Rewrite view constants as literals, as they won't be in the schema for
    // an index on the view. Our view may be READ_ONLY yet still have inherited
    // 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();

    // Coerce index column reference back to same type as data column so that
    // expression behave exactly the same. No need to invert, as this will be done
    // automatically as needed. If node is used at the top level, do not convert, as
    // otherwise the wrapper gets in the way in the group by clause. For example,
    // an INTEGER column in a GROUP BY gets doubly wrapped like this:
    //     CAST CAST int_col AS INTEGER AS DECIMAL
    // This is unnecessary and problematic in the case of a null value.
    // TODO: test case for this
    if (!isTopLevel() && indexColType != dataColType) {
        indexColNode = FACTORY.cast(indexColNode, dataColType, null, null);
    }
    return indexColNode;
}
 
Example 14
Source File: BaseQueryPlan.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static void serializeViewConstantsIntoScan(Scan scan, PTable dataTable) {
    int dataPosOffset = (dataTable.getBucketNum() != null ? 1 : 0) + (dataTable.isMultiTenant() ? 1 : 0);
    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) {
            byte[][] 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();
                    }
                }
            }
            serializeViewConstantsIntoScan(viewConstants, scan);
        }
    }
}
 
Example 15
Source File: IndexUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static boolean getViewConstantValue(PColumn column, ImmutableBytesWritable ptr) {
    byte[] value = column.getViewConstant();
    if (value != null) {
        ptr.set(value, 0, value.length-1);
        return true;
    }
    return false;
}