Search in sources :

Example 1 with ProjectedColumn

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

the class TupleProjectionCompiler method createProjectedTable.

public static PTable createProjectedTable(SelectStatement select, StatementContext context) throws SQLException {
    Preconditions.checkArgument(!select.isJoin());
    // Non-group-by or group-by aggregations will create its own projected result.
    if (select.getInnerSelectStatement() != null || select.getFrom() == null || select.isAggregate() || select.isDistinct() || (context.getResolver().getTables().get(0).getTable().getType() != PTableType.TABLE && context.getResolver().getTables().get(0).getTable().getType() != PTableType.INDEX && context.getResolver().getTables().get(0).getTable().getType() != PTableType.VIEW))
        return null;
    List<PColumn> projectedColumns = new ArrayList<PColumn>();
    boolean isWildcard = false;
    Set<String> families = new HashSet<String>();
    ColumnRefVisitor visitor = new ColumnRefVisitor(context);
    TableRef tableRef = context.getCurrentTable();
    PTable table = tableRef.getTable();
    for (AliasedNode aliasedNode : select.getSelect()) {
        ParseNode node = aliasedNode.getNode();
        if (node instanceof WildcardParseNode) {
            if (((WildcardParseNode) node).isRewrite()) {
                TableRef parentTableRef = FromCompiler.getResolver(NODE_FACTORY.namedTable(null, TableName.create(table.getSchemaName().getString(), table.getParentTableName().getString())), context.getConnection()).resolveTable(table.getSchemaName().getString(), table.getParentTableName().getString());
                for (PColumn column : parentTableRef.getTable().getColumns()) {
                    NODE_FACTORY.column(null, '"' + IndexUtil.getIndexColumnName(column) + '"', null).accept(visitor);
                }
            }
            isWildcard = true;
        } else if (node instanceof FamilyWildcardParseNode) {
            FamilyWildcardParseNode familyWildcardNode = (FamilyWildcardParseNode) node;
            String familyName = familyWildcardNode.getName();
            if (familyWildcardNode.isRewrite()) {
                TableRef parentTableRef = FromCompiler.getResolver(NODE_FACTORY.namedTable(null, TableName.create(table.getSchemaName().getString(), table.getParentTableName().getString())), context.getConnection()).resolveTable(table.getSchemaName().getString(), table.getParentTableName().getString());
                for (PColumn column : parentTableRef.getTable().getColumnFamily(familyName).getColumns()) {
                    NODE_FACTORY.column(null, '"' + IndexUtil.getIndexColumnName(column) + '"', null).accept(visitor);
                }
            } else {
                for (PColumn column : table.getColumnFamily(familyName).getColumns()) {
                    NODE_FACTORY.column(TableName.create(null, familyName), '"' + column.getName().getString() + '"', null).accept(visitor);
                }
            }
            families.add(familyName);
        } else {
            node.accept(visitor);
        }
    }
    if (!isWildcard) {
        for (OrderByNode orderBy : select.getOrderBy()) {
            orderBy.getNode().accept(visitor);
        }
    }
    boolean hasSaltingColumn = table.getBucketNum() != null;
    int position = hasSaltingColumn ? 1 : 0;
    // Always project PK columns first in case there are some PK columns added by alter table.
    for (int i = position; i < table.getPKColumns().size(); i++) {
        PColumn sourceColumn = table.getPKColumns().get(i);
        ColumnRef sourceColumnRef = new ColumnRef(tableRef, sourceColumn.getPosition());
        PColumn column = new ProjectedColumn(sourceColumn.getName(), sourceColumn.getFamilyName(), position++, sourceColumn.isNullable(), sourceColumnRef, null);
        projectedColumns.add(column);
    }
    List<ColumnRef> nonPkColumnRefList = new ArrayList<ColumnRef>(visitor.nonPkColumnRefSet);
    for (PColumn sourceColumn : table.getColumns()) {
        if (SchemaUtil.isPKColumn(sourceColumn))
            continue;
        ColumnRef sourceColumnRef = new ColumnRef(tableRef, sourceColumn.getPosition());
        if (!isWildcard && !visitor.nonPkColumnRefSet.contains(sourceColumnRef) && !families.contains(sourceColumn.getFamilyName().getString()))
            continue;
        PColumn column = new ProjectedColumn(sourceColumn.getName(), sourceColumn.getFamilyName(), visitor.nonPkColumnRefSet.contains(sourceColumnRef) ? position + nonPkColumnRefList.indexOf(sourceColumnRef) : position++, sourceColumn.isNullable(), sourceColumnRef, sourceColumn.getColumnQualifierBytes());
        projectedColumns.add(column);
        // Wildcard or FamilyWildcard will be handled by ProjectionCompiler.
        if (!isWildcard && !families.contains(sourceColumn.getFamilyName())) {
            EncodedColumnsUtil.setColumns(column, table, context.getScan());
        }
    }
    // add LocalIndexDataColumnRef
    position = projectedColumns.size();
    for (LocalIndexDataColumnRef sourceColumnRef : visitor.localIndexColumnRefSet) {
        PColumn column = new ProjectedColumn(sourceColumnRef.getColumn().getName(), sourceColumnRef.getColumn().getFamilyName(), position++, sourceColumnRef.getColumn().isNullable(), sourceColumnRef, sourceColumnRef.getColumn().getColumnQualifierBytes());
        projectedColumns.add(column);
    }
    return PTableImpl.makePTable(table.getTenantId(), table.getSchemaName(), table.getTableName(), PTableType.PROJECTED, table.getIndexState(), table.getTimeStamp(), table.getSequenceNumber(), table.getPKName(), table.getBucketNum(), projectedColumns, table.getParentSchemaName(), table.getParentTableName(), table.getIndexes(), table.isImmutableRows(), Collections.<PName>emptyList(), table.getDefaultFamilyName(), table.getViewStatement(), table.isWALDisabled(), table.isMultiTenant(), table.getStoreNulls(), table.getViewType(), table.getViewIndexId(), table.getIndexType(), table.rowKeyOrderOptimizable(), table.isTransactional(), table.getUpdateCacheFrequency(), table.getIndexDisableTimestamp(), table.isNamespaceMapped(), table.getAutoPartitionSeqName(), table.isAppendOnlySchema(), table.getImmutableStorageScheme(), table.getEncodingScheme(), table.getEncodedCQCounter(), table.useStatsForParallelization());
}
Also used : ArrayList(java.util.ArrayList) OrderByNode(org.apache.phoenix.parse.OrderByNode) FamilyWildcardParseNode(org.apache.phoenix.parse.FamilyWildcardParseNode) WildcardParseNode(org.apache.phoenix.parse.WildcardParseNode) AliasedNode(org.apache.phoenix.parse.AliasedNode) LocalIndexDataColumnRef(org.apache.phoenix.schema.LocalIndexDataColumnRef) PTable(org.apache.phoenix.schema.PTable) ProjectedColumn(org.apache.phoenix.schema.ProjectedColumn) PColumn(org.apache.phoenix.schema.PColumn) FamilyWildcardParseNode(org.apache.phoenix.parse.FamilyWildcardParseNode) FamilyWildcardParseNode(org.apache.phoenix.parse.FamilyWildcardParseNode) WildcardParseNode(org.apache.phoenix.parse.WildcardParseNode) ColumnParseNode(org.apache.phoenix.parse.ColumnParseNode) ParseNode(org.apache.phoenix.parse.ParseNode) ColumnRef(org.apache.phoenix.schema.ColumnRef) LocalIndexDataColumnRef(org.apache.phoenix.schema.LocalIndexDataColumnRef) TableRef(org.apache.phoenix.schema.TableRef) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 2 with ProjectedColumn

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

the class TupleProjectionCompiler method createProjectedTable.

public static PTable createProjectedTable(TableRef tableRef, List<ColumnRef> sourceColumnRefs, boolean retainPKColumns) throws SQLException {
    PTable table = tableRef.getTable();
    List<PColumn> projectedColumns = new ArrayList<PColumn>();
    int position = table.getBucketNum() != null ? 1 : 0;
    for (int i = retainPKColumns ? position : 0; i < sourceColumnRefs.size(); i++) {
        ColumnRef sourceColumnRef = sourceColumnRefs.get(i);
        PColumn sourceColumn = sourceColumnRef.getColumn();
        String colName = sourceColumn.getName().getString();
        String aliasedName = tableRef.getTableAlias() == null ? SchemaUtil.getColumnName(table.getName().getString(), colName) : SchemaUtil.getColumnName(tableRef.getTableAlias(), colName);
        PName familyName = SchemaUtil.isPKColumn(sourceColumn) ? (retainPKColumns ? null : PNameFactory.newName(VALUE_COLUMN_FAMILY)) : sourceColumn.getFamilyName();
        // If we're not retaining the PK columns, then we should switch columns to be nullable
        PColumn column = new ProjectedColumn(PNameFactory.newName(aliasedName), familyName, position++, sourceColumn.isNullable(), sourceColumnRef, sourceColumn.getColumnQualifierBytes());
        projectedColumns.add(column);
    }
    EncodedCQCounter cqCounter = EncodedCQCounter.NULL_COUNTER;
    if (EncodedColumnsUtil.usesEncodedColumnNames(table)) {
        cqCounter = EncodedCQCounter.copy(table.getEncodedCQCounter());
    }
    return PTableImpl.makePTable(table.getTenantId(), PROJECTED_TABLE_SCHEMA, table.getName(), PTableType.PROJECTED, null, table.getTimeStamp(), table.getSequenceNumber(), table.getPKName(), table.getBucketNum(), projectedColumns, null, null, Collections.<PTable>emptyList(), table.isImmutableRows(), Collections.<PName>emptyList(), null, null, table.isWALDisabled(), table.isMultiTenant(), table.getStoreNulls(), table.getViewType(), table.getViewIndexId(), null, table.rowKeyOrderOptimizable(), table.isTransactional(), table.getUpdateCacheFrequency(), table.getIndexDisableTimestamp(), table.isNamespaceMapped(), table.getAutoPartitionSeqName(), table.isAppendOnlySchema(), table.getImmutableStorageScheme(), table.getEncodingScheme(), cqCounter, table.useStatsForParallelization());
}
Also used : PColumn(org.apache.phoenix.schema.PColumn) EncodedCQCounter(org.apache.phoenix.schema.PTable.EncodedCQCounter) PName(org.apache.phoenix.schema.PName) ArrayList(java.util.ArrayList) ColumnRef(org.apache.phoenix.schema.ColumnRef) LocalIndexDataColumnRef(org.apache.phoenix.schema.LocalIndexDataColumnRef) PTable(org.apache.phoenix.schema.PTable) ProjectedColumn(org.apache.phoenix.schema.ProjectedColumn)

Example 3 with ProjectedColumn

use of org.apache.phoenix.schema.ProjectedColumn 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)

Aggregations

PColumn (org.apache.phoenix.schema.PColumn)3 ProjectedColumn (org.apache.phoenix.schema.ProjectedColumn)3 ArrayList (java.util.ArrayList)2 ColumnRef (org.apache.phoenix.schema.ColumnRef)2 LocalIndexDataColumnRef (org.apache.phoenix.schema.LocalIndexDataColumnRef)2 PTable (org.apache.phoenix.schema.PTable)2 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 AliasedNode (org.apache.phoenix.parse.AliasedNode)1 ColumnParseNode (org.apache.phoenix.parse.ColumnParseNode)1 FamilyWildcardParseNode (org.apache.phoenix.parse.FamilyWildcardParseNode)1 Hint (org.apache.phoenix.parse.HintNode.Hint)1 OrderByNode (org.apache.phoenix.parse.OrderByNode)1 ParseNode (org.apache.phoenix.parse.ParseNode)1 WildcardParseNode (org.apache.phoenix.parse.WildcardParseNode)1 PName (org.apache.phoenix.schema.PName)1 EncodedCQCounter (org.apache.phoenix.schema.PTable.EncodedCQCounter)1 TableRef (org.apache.phoenix.schema.TableRef)1 PSmallint (org.apache.phoenix.schema.types.PSmallint)1 PTinyint (org.apache.phoenix.schema.types.PTinyint)1