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

The following examples show how to use org.apache.phoenix.schema.PColumn#getPosition() . 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: 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 3
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 4
Source File: ProjectionCompiler.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private static void projectTableColumnFamily(StatementContext context, String cfName, TableRef tableRef, boolean resolveColumn, List<Expression> projectedExpressions, List<ExpressionProjector> projectedColumns) throws SQLException {
    PTable table = tableRef.getTable();
    PColumnFamily pfamily = table.getColumnFamily(cfName);
    for (PColumn column : pfamily.getColumns()) {
        ColumnRef ref = new ColumnRef(tableRef, column.getPosition());
        if (resolveColumn) {
            ref = context.getResolver().resolveColumn(table.getTableName().getString(), cfName, column.getName().getString());
        }
        Expression expression = ref.newColumnExpression();
        projectedExpressions.add(expression);
        String colName = column.getName().toString();
        boolean isCaseSensitive = !SchemaUtil.normalizeIdentifier(colName).equals(colName);
        projectedColumns.add(new ExpressionProjector(colName, tableRef.getTableAlias() == null ? 
                table.getName().getString() : tableRef.getTableAlias(), expression, isCaseSensitive));
    }
}
 
Example 5
Source File: FromCompiler.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public ColumnRef resolveColumn(String schemaName, String tableName,
		String colName) throws SQLException {
	TableRef tableRef = tableRefs.get(0);
	boolean resolveCF = false;
	if (schemaName != null || tableName != null) {
	    String resolvedTableName = tableRef.getTable().getTableName().getString();
	    String resolvedSchemaName = tableRef.getTable().getSchemaName().getString();
	    if (schemaName != null && tableName != null) {
                  if ( ! ( schemaName.equals(resolvedSchemaName)  &&
                           tableName.equals(resolvedTableName) )) {
                      if (!(resolveCF = schemaName.equals(alias))) {
                          throw new ColumnNotFoundException(schemaName, tableName, null, colName);
                      }
                  }
	    } else { // schemaName == null && tableName != null
                  if (tableName != null && !tableName.equals(alias) && (!tableName.equals(resolvedTableName) || !resolvedSchemaName.equals(""))) {
                      resolveCF = true;
                 }
	    }

	}
      	PColumn column = resolveCF
      	        ? tableRef.getTable().getColumnFamily(tableName).getColumn(colName)
      			: tableRef.getTable().getColumn(colName);
          return new ColumnRef(tableRef, column.getPosition());
}
 
Example 6
Source File: ProjectionCompiler.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private static void projectTableColumnFamily(StatementContext context, String cfName, TableRef tableRef, List<Expression> projectedExpressions, List<ExpressionProjector> projectedColumns) throws SQLException {
    PTable table = tableRef.getTable();
    PColumnFamily pfamily = table.getColumnFamily(cfName);
    for (PColumn column : pfamily.getColumns()) {
        ColumnRef ref = new ColumnRef(tableRef, column.getPosition());
        Expression expression = ref.newColumnExpression();
        projectedExpressions.add(expression);
        String colName = column.getName().toString();
        boolean isCaseSensitive = !SchemaUtil.normalizeIdentifier(colName).equals(colName);
        projectedColumns.add(new ExpressionProjector(colName, tableRef.getTableAlias() == null ? 
                table.getName().getString() : tableRef.getTableAlias(), expression, isCaseSensitive));
    }
}
 
Example 7
Source File: ProjectionCompiler.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private static void projectIndexColumnFamily(StatementContext context, String cfName, TableRef tableRef, List<Expression> projectedExpressions, List<ExpressionProjector> projectedColumns) throws SQLException {
    PTable index = tableRef.getTable();
    PhoenixConnection conn = context.getConnection();
    String tableName = index.getParentName().getString();
    PTable table = conn.getMetaDataCache().getTable(new PTableKey(conn.getTenantId(), tableName));
    PColumnFamily pfamily = table.getColumnFamily(cfName);
    for (PColumn column : pfamily.getColumns()) {
        String indexColName = IndexUtil.getIndexColumnName(column);
        PColumn indexColumn = null;
        ColumnRef ref = null;
        try {
            indexColumn = index.getColumn(indexColName);
            ref = new ColumnRef(tableRef, indexColumn.getPosition());
        } catch (ColumnNotFoundException e) {
            if (index.getIndexType() == IndexType.LOCAL) {
                try {
                    ref = new LocalIndexDataColumnRef(context, indexColName);
                    indexColumn = ref.getColumn();
                } catch (ColumnFamilyNotFoundException c) {
                    throw e;
                }
            } else {
                throw e;
            }
        }
        Expression expression = ref.newColumnExpression();
        projectedExpressions.add(expression);
        String colName = column.getName().toString();
        boolean isCaseSensitive = !SchemaUtil.normalizeIdentifier(colName).equals(colName);
        projectedColumns.add(new ExpressionProjector(colName, 
                tableRef.getTableAlias() == null ? table.getName().getString() : tableRef.getTableAlias(), expression, isCaseSensitive));
    }
}
 
Example 8
Source File: PostDDLCompiler.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public ColumnRef resolveColumn(String schemaName, String tableName, String colName) throws SQLException {
    PColumn column = tableName != null
            ? tableRef.getTable().getColumnFamily(tableName).getPColumnForColumnName(colName)
            : tableRef.getTable().getColumnForColumnName(colName);
    return new ColumnRef(tableRef, column.getPosition());
}
 
Example 9
Source File: FromCompiler.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public ColumnRef resolveColumn(String schemaName, String tableName,
		String colName) throws SQLException {
	TableRef tableRef = tableRefs.get(0);
	boolean resolveCF = false;
	if (schemaName != null || tableName != null) {
	    String resolvedTableName = tableRef.getTable().getTableName().getString();
	    String resolvedSchemaName = tableRef.getTable().getSchemaName().getString();
	    if (schemaName != null && tableName != null) {
                  if ( ! ( schemaName.equals(resolvedSchemaName)  &&
                           tableName.equals(resolvedTableName) )) {
                      if (!(resolveCF = schemaName.equals(alias))) {
                          throw new ColumnNotFoundException(schemaName, tableName, null, colName);
                      }
                  }
	    } else { // schemaName == null && tableName != null
                  if (tableName != null && !tableName.equals(alias) && (!tableName.equals(resolvedTableName) || !resolvedSchemaName.equals(""))) {
                      resolveCF = true;
                 }
	    }

	}
      	PColumn column = resolveCF
      	        ? tableRef.getTable().getColumnFamily(tableName).getPColumnForColumnName(colName)
      			: tableRef.getTable().getColumnForColumnName(colName);
          return new ColumnRef(tableRef, column.getPosition());
}
 
Example 10
Source File: ProjectedColumnExpression.java    From phoenix with Apache License 2.0 4 votes vote down vote up
public ProjectedColumnExpression(PColumn column, PTable table, String displayName) {
	this(column, table.getColumns(), column.getPosition() - table.getPKColumns().size(), displayName);
}
 
Example 11
Source File: ProjectionCompiler.java    From phoenix with Apache License 2.0 4 votes vote down vote up
private static void projectIndexColumnFamily(StatementContext context, String cfName, TableRef tableRef, boolean resolveColumn, List<Expression> projectedExpressions, List<ExpressionProjector> projectedColumns) throws SQLException {
    PTable index = tableRef.getTable();
    PhoenixConnection conn = context.getConnection();
    String tableName = index.getParentName().getString();
    PTable table = conn.getTable(new PTableKey(conn.getTenantId(), tableName));
    PColumnFamily pfamily = table.getColumnFamily(cfName);
    for (PColumn column : pfamily.getColumns()) {
        String indexColName = IndexUtil.getIndexColumnName(column);
        PColumn indexColumn = null;
        ColumnRef ref = null;
        String indexColumnFamily = null;
        try {
            indexColumn = index.getColumnForColumnName(indexColName);
            ref = new ColumnRef(tableRef, indexColumn.getPosition());
            indexColumnFamily = indexColumn.getFamilyName() == null ? null : indexColumn.getFamilyName().getString();
        } catch (ColumnNotFoundException e) {
            if (index.getIndexType() == IndexType.LOCAL) {
                try {
                    ref = new LocalIndexDataColumnRef(context, tableRef, indexColName);
                    indexColumn = ref.getColumn();
                    indexColumnFamily =
                            indexColumn.getFamilyName() == null ? null
                                    : (index.getIndexType() == IndexType.LOCAL ? IndexUtil
                                            .getLocalIndexColumnFamily(indexColumn
                                                    .getFamilyName().getString()) : indexColumn
                                            .getFamilyName().getString());
                } catch (ColumnFamilyNotFoundException c) {
                    throw e;
                }
            } else {
                throw e;
            }
        }
        if (resolveColumn) {
            ref = context.getResolver().resolveColumn(index.getTableName().getString(), indexColumnFamily, indexColName);
        }
        Expression expression = ref.newColumnExpression();
        projectedExpressions.add(expression);
        String colName = column.getName().toString();
        boolean isCaseSensitive = !SchemaUtil.normalizeIdentifier(colName).equals(colName);
        projectedColumns.add(new ExpressionProjector(colName, 
                tableRef.getTableAlias() == null ? table.getName().getString() : tableRef.getTableAlias(), expression, isCaseSensitive));
    }
}
 
Example 12
Source File: ProjectedColumnExpression.java    From phoenix with Apache License 2.0 4 votes vote down vote up
public ProjectedColumnExpression(PColumn column, PTable table, String displayName) {
	this(column, table.getColumns(), column.getPosition() - table.getPKColumns().size(), displayName);
}
 
Example 13
Source File: WhereConstantParser.java    From phoenix with Apache License 2.0 4 votes vote down vote up
public static PTable addViewInfoToPColumnsIfNeeded(PTable view) throws SQLException {
    byte[][] viewColumnConstantsToBe = new byte[view.getColumns().size()][];
    if (view.getViewStatement() == null) {
    	return view;
    }
    SelectStatement select = new SQLParser(view.getViewStatement()).parseQuery();
    ParseNode whereNode = select.getWhere();
    ColumnResolver resolver = FromCompiler.getResolver(new TableRef(view));

    try (PhoenixConnection conn = getConnectionlessConnection()) {
        StatementContext context = new StatementContext(new PhoenixStatement(conn), resolver);

        Expression expression;
        try {
            expression = WhereCompiler.compile(context, whereNode);
        } catch (ColumnNotFoundException e) {
            // if we could not find a column used in the view statement
            // (which means its was dropped) this view is not valid any more
            return null;
        }
        CreateTableCompiler.ViewWhereExpressionVisitor visitor = new CreateTableCompiler
                .ViewWhereExpressionVisitor(view, viewColumnConstantsToBe);
        expression.accept(visitor);

        BitSet isViewColumnReferencedToBe = new BitSet(view.getColumns().size());
        // Used to track column references in a view
        ExpressionCompiler expressionCompiler = new CreateTableCompiler
                .ColumnTrackingExpressionCompiler(context, isViewColumnReferencedToBe);
        whereNode.accept(expressionCompiler);

        List<PColumn> result = Lists.newArrayList();
        for (PColumn column : PTableImpl.getColumnsToClone(view)) {
            boolean isViewReferenced = isViewColumnReferencedToBe.get(column.getPosition());
            if ((visitor.isUpdatable() || view.getPKColumns()
                    .get(MetaDataUtil.getAutoPartitionColIndex(view)).equals(column))
                    && viewColumnConstantsToBe[column.getPosition()] != null) {
                result.add(new PColumnImpl(column,
                        viewColumnConstantsToBe[column.getPosition()], isViewReferenced));
            }
            // If view is not updatable, viewColumnConstants should be empty. We will still
            // inherit our parent viewConstants, but we have no additional ones.
            else if (isViewReferenced ){
                result.add(new PColumnImpl(column, column.getViewConstant(), isViewReferenced));
            } else {
                result.add(column);
            }
        }
        return PTableImpl.builderWithColumns(view, result)
                .build();
    }
}