Java Code Examples for org.apache.phoenix.schema.PTable#getColumnFamily()

The following examples show how to use org.apache.phoenix.schema.PTable#getColumnFamily() . 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
protected PTable addDynamicColumns(List<ColumnDef> dynColumns, PTable theTable)
        throws SQLException {
    if (!dynColumns.isEmpty()) {
        List<PColumn> allcolumns = new ArrayList<PColumn>();
        List<PColumn> existingColumns = theTable.getColumns();
        // Need to skip the salting column, as it's added in the makePTable call below
        allcolumns.addAll(theTable.getBucketNum() == null ? existingColumns : existingColumns.subList(1, existingColumns.size()));
        // Position still based on with the salting columns
        int position = existingColumns.size();
        PName defaultFamilyName = PNameFactory.newName(SchemaUtil.getEmptyColumnFamily(theTable));
        for (ColumnDef dynColumn : dynColumns) {
            PName familyName = defaultFamilyName;
            PName name = PNameFactory.newName(dynColumn.getColumnDefName().getColumnName());
            String family = dynColumn.getColumnDefName().getFamilyName();
            if (family != null) {
                theTable.getColumnFamily(family); // Verifies that column family exists
                familyName = PNameFactory.newName(family);
            }
            allcolumns.add(new PColumnImpl(name, familyName, dynColumn.getDataType(), dynColumn.getMaxLength(),
                    dynColumn.getScale(), dynColumn.isNull(), position, dynColumn.getSortOrder(), dynColumn.getArraySize(), null, false, dynColumn.getExpression()));
            position++;
        }
        theTable = PTableImpl.makePTable(theTable, allcolumns);
    }
    return theTable;
}
 
Example 2
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the column info for the given column for the given table.
 *
 * @param table
 * @param columnName User-specified column name. May be family-qualified or bare.
 * @return columnInfo associated with the column in the table
 * @throws SQLException if parameters are null or if column is not found or if column is ambiguous.
 */
public static ColumnInfo getColumnInfo(PTable table, String columnName) throws SQLException {
    if (table==null) {
        throw new SQLException("Table must not be null.");
    }
    if (columnName==null) {
        throw new SQLException("columnName must not be null.");
    }
    PColumn pColumn = null;
    if (columnName.contains(QueryConstants.NAME_SEPARATOR)) {
        String[] tokens = columnName.split(QueryConstants.NAME_SEPARATOR_REGEX);
        if (tokens.length!=2) {
            throw new SQLException(String.format("Unable to process column %s, expected family-qualified name.",columnName));
        }
        String familyName = tokens[0];
        String familyColumn = tokens[1];
        PColumnFamily family = table.getColumnFamily(familyName);
        pColumn = family.getColumn(familyColumn);
    } else {
        pColumn = table.getColumn(columnName);
    }
    return getColumnInfo(pColumn);
}
 
Example 3
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private static PColumn getPColumn(PTable table, @Nullable String familyName, String columnName) throws SQLException {
    if (table==null) {
        throw new SQLException("Table must not be null.");
    }
    if (columnName==null) {
        throw new SQLException("columnName must not be null.");
    }
    // normalize and remove quotes from family and column names before looking up.
    familyName = SchemaUtil.normalizeIdentifier(familyName);
    columnName = SchemaUtil.normalizeIdentifier(columnName);
    PColumn pColumn = null;
    if (familyName != null) {
        PColumnFamily family = table.getColumnFamily(familyName);
        pColumn = family.getColumn(columnName);
    } else {
        pColumn = table.getColumn(columnName);
    }
    return pColumn;
}
 
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: PhoenixRuntime.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the column info for the given column for the given table.
 *
 * @param table
 * @param columnName User-specified column name. May be family-qualified or bare.
 * @return columnInfo associated with the column in the table
 * @throws SQLException if parameters are null or if column is not found or if column is ambiguous.
 */
public static ColumnInfo getColumnInfo(PTable table, String columnName) throws SQLException {
    if (table==null) {
        throw new SQLException("Table must not be null.");
    }
    if (columnName==null) {
        throw new SQLException("columnName must not be null.");
    }
    PColumn pColumn = null;
    if (columnName.contains(QueryConstants.NAME_SEPARATOR)) {
        String[] tokens = columnName.split(QueryConstants.NAME_SEPARATOR_REGEX);
        if (tokens.length!=2) {
            throw new SQLException(String.format("Unable to process column %s, expected family-qualified name.",columnName));
        }
        String familyName = tokens[0];
        String familyColumn = tokens[1];
        PColumnFamily family = table.getColumnFamily(familyName);
        pColumn = family.getPColumnForColumnName(familyColumn);
    } else {
        pColumn = table.getColumnForColumnName(columnName);
    }
    return getColumnInfo(pColumn);
}
 
Example 6
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Deprecated
private static PColumn getPColumn(PTable table, @Nullable String familyName, String columnName) throws SQLException {
    if (table==null) {
        throw new SQLException("Table must not be null.");
    }
    if (columnName==null) {
        throw new SQLException("columnName must not be null.");
    }
    // normalize and remove quotes from family and column names before looking up.
    familyName = SchemaUtil.normalizeIdentifier(familyName);
    columnName = SchemaUtil.normalizeIdentifier(columnName);
    PColumn pColumn = null;
    if (familyName != null) {
        PColumnFamily family = table.getColumnFamily(familyName);
        pColumn = family.getPColumnForColumnName(columnName);
    } else {
        pColumn = table.getColumnForColumnName(columnName);
    }
    return pColumn;
}
 
Example 7
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private static PColumn getColumn(PTable table, @Nullable String familyName, String columnName) throws SQLException {
    if (table==null) {
        throw new SQLException("Table must not be null.");
    }
    if (columnName==null) {
        throw new SQLException("columnName must not be null.");
    }
    // normalize and remove quotes from family and column names before looking up.
    familyName = SchemaUtil.normalizeIdentifier(familyName);
    columnName = SchemaUtil.normalizeIdentifier(columnName);
    // Column names are always for the data table, so we must translate them if
    // we're dealing with an index table.
    if (table.getType() == PTableType.INDEX) {
        columnName = IndexUtil.getIndexColumnName(familyName, columnName);
    }
    PColumn pColumn = null;
    if (familyName != null) {
        PColumnFamily family = table.getColumnFamily(familyName);
        pColumn = family.getPColumnForColumnName(columnName);
    } else {
        pColumn = table.getColumnForColumnName(columnName);
    }
    return pColumn;
}
 
Example 8
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 9
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 10
Source File: FromCompiler.java    From phoenix with Apache License 2.0 5 votes vote down vote up
protected PTable addDynamicColumns(List<ColumnDef> dynColumns, PTable theTable)
        throws SQLException {
    if (!dynColumns.isEmpty()) {
        List<PColumn> existingColumns = theTable.getColumns();
        // Need to skip the salting column, as it's handled in the PTable builder call below
        List<PColumn> allcolumns = new ArrayList<>(
                theTable.getBucketNum() == null ? existingColumns :
                        existingColumns.subList(1, existingColumns.size()));
        // Position still based on with the salting columns
        int position = existingColumns.size();
        PName defaultFamilyName = PNameFactory.newName(SchemaUtil.getEmptyColumnFamily(theTable));
        for (ColumnDef dynColumn : dynColumns) {
            PName familyName = defaultFamilyName;
            PName name = PNameFactory.newName(dynColumn.getColumnDefName().getColumnName());
            String family = dynColumn.getColumnDefName().getFamilyName();
            if (family != null) {
                theTable.getColumnFamily(family); // Verifies that column family exists
                familyName = PNameFactory.newName(family);
            }
            allcolumns.add(new PColumnImpl(name, familyName, dynColumn.getDataType(), dynColumn.getMaxLength(),
                    dynColumn.getScale(), dynColumn.isNull(), position, dynColumn.getSortOrder(), dynColumn.getArraySize(), null, false, dynColumn.getExpression(), false, true, Bytes.toBytes(dynColumn.getColumnDefName().getColumnName()),
                HConstants.LATEST_TIMESTAMP));
            position++;
        }
        theTable = PTableImpl.builderWithColumns(theTable, allcolumns)
                .build();
    }
    return theTable;
}
 
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));
    }
}