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

The following examples show how to use org.apache.phoenix.schema.PColumn#getFamilyName() . 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: PhoenixRuntime.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * @param columns - Initialized empty list to be filled with the pairs of column family name and column name for columns that are used 
 * as row key for the query plan. Column family names are optional and hence the first part of the pair is nullable.
 * Column names and family names are enclosed in double quotes to allow for case sensitivity and for presence of 
 * special characters. Salting column and view index id column are not included. If the connection is tenant specific 
 * and the table used by the query plan is multi-tenant, then the tenant id column is not included as well.
 * @param datatypes - Initialized empty list to be filled with the corresponding data type for the columns in @param columns. 
 * @param plan - query plan to get info for
 * @param conn - phoenix connection used to generate the query plan. Caller should take care of closing the connection appropriately.
 * @param forDataTable - if true, then column names and data types correspond to the data table even if the query plan uses
 * the secondary index table. If false, and if the query plan uses the secondary index table, then the column names and data 
 * types correspond to the index table.
 * @throws SQLException
 */
public static void getPkColsDataTypesForSql(List<Pair<String, String>> columns, List<String> dataTypes, QueryPlan plan, Connection conn, boolean forDataTable) throws SQLException {
    checkNotNull(columns);
    checkNotNull(dataTypes);
    checkNotNull(plan);
    checkNotNull(conn);
    List<PColumn> pkColumns = getPkColumns(plan.getTableRef().getTable(), conn, forDataTable);
    String columnName;
    String familyName;
    for (PColumn pCol : pkColumns) {
        String sqlTypeName = getSqlTypeName(pCol);
        dataTypes.add(sqlTypeName);
        columnName = addQuotes(pCol.getName().getString());
        familyName = pCol.getFamilyName() != null ? addQuotes(pCol.getFamilyName().getString()) : null;
        columns.add(new Pair<String, String>(familyName, columnName));
    }
}
 
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: PhoenixRuntime.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * @param columns - Initialized empty list to be filled with the pairs of column family name and column name for columns that are used 
 * as row key for the query plan. Column family names are optional and hence the first part of the pair is nullable.
 * Column names and family names are enclosed in double quotes to allow for case sensitivity and for presence of 
 * special characters. Salting column and view index id column are not included. If the connection is tenant specific 
 * and the table used by the query plan is multi-tenant, then the tenant id column is not included as well.
 * @param dataTypes - Initialized empty list to be filled with the corresponding data type for the columns in @param columns.
 * @param plan - query plan to get info for
 * @param conn - phoenix connection used to generate the query plan. Caller should take care of closing the connection appropriately.
 * @param forDataTable - if true, then column names and data types correspond to the data table even if the query plan uses
 * the secondary index table. If false, and if the query plan uses the secondary index table, then the column names and data 
 * types correspond to the index table.
 * @throws SQLException
 */
@Deprecated
public static void getPkColsDataTypesForSql(List<Pair<String, String>> columns, List<String> dataTypes, QueryPlan plan, Connection conn, boolean forDataTable) throws SQLException {
    checkNotNull(columns);
    checkNotNull(dataTypes);
    checkNotNull(plan);
    checkNotNull(conn);
    List<PColumn> pkColumns = getPkColumns(plan.getTableRef().getTable(), conn, forDataTable);
    String columnName;
    String familyName;
    for (PColumn pCol : pkColumns) {
        String sqlTypeName = getSqlTypeName(pCol);
        dataTypes.add(sqlTypeName);
        columnName = addQuotes(pCol.getName().getString());
        familyName = pCol.getFamilyName() != null ? addQuotes(pCol.getFamilyName().getString()) : null;
        columns.add(new Pair<String, String>(familyName, columnName));
    }
}
 
Example 5
Source File: SchemaUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @return a fully qualified column name in the format: "CFNAME"."COLNAME" or "COLNAME" depending on whether or not
 * there is a column family name present. 
 */
public static String getQuotedFullColumnName(PColumn pCol) {
    checkNotNull(pCol);
    String columnName = pCol.getName().getString();
    String columnFamilyName = pCol.getFamilyName() != null ? pCol.getFamilyName().getString() : null;
    return getQuotedFullColumnName(columnFamilyName, columnName);
}
 
Example 6
Source File: IndexColumnNames.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private String getDataColFullName(PColumn dCol) {
    String dColFullName = "";
    if (dCol.getFamilyName() != null) {
        dColFullName += dCol.getFamilyName().getString() + QueryConstants.NAME_SEPARATOR;
    }
    dColFullName += dCol.getName().getString();
    return dColFullName;
}
 
Example 7
Source File: SchemaUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @return a fully qualified column name in the format: "CFNAME"."COLNAME" or "COLNAME" depending on whether or not
 * there is a column family name present. 
 */
public static String getQuotedFullColumnName(PColumn pCol) {
    checkNotNull(pCol);
    String columnName = pCol.getName().getString();
    String columnFamilyName = pCol.getFamilyName() != null ? pCol.getFamilyName().getString() : null;
    return getQuotedFullColumnName(columnFamilyName, columnName);
}
 
Example 8
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
* Get the column family, column name pairs that make up the row key of the table that will be queried.
* @param conn - connection used to generate the query plan. Caller should take care of closing the connection appropriately.
* @param plan - query plan to get info for.
* @return the pairs of column family name and column name columns in the data table that make up the row key for
* the table used in the query plan. Column family names are optional and hence the first part of the pair is nullable.
* Column names and family names are enclosed in double quotes to allow for case sensitivity and for presence of 
* special characters. Salting column and view index id column are not included. If the connection is tenant specific 
* and the table used by the query plan is multi-tenant, then the tenant id column is not included as well.
* @throws SQLException
*/
public static List<Pair<String, String>> getPkColsForSql(Connection conn, QueryPlan plan) throws SQLException {
    checkNotNull(plan);
    checkNotNull(conn);
    List<PColumn> pkColumns = getPkColumns(plan.getTableRef().getTable(), conn);
    List<Pair<String, String>> columns = Lists.newArrayListWithExpectedSize(pkColumns.size());
    String columnName;
    String familyName;
    for (PColumn pCol : pkColumns ) {
        columnName = addQuotes(pCol.getName().getString());
        familyName = pCol.getFamilyName() != null ? addQuotes(pCol.getFamilyName().getString()) : null;
        columns.add(new Pair<String, String>(familyName, columnName));
    }
    return columns;
}
 
Example 9
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param columns - Initialized empty list to be filled with the pairs of column family name and column name for columns that are used 
 * as row key for the query plan. Column family names are optional and hence the first part of the pair is nullable.
 * Column names and family names are enclosed in double quotes to allow for case sensitivity and for presence of 
 * special characters. Salting column and view index id column are not included. If the connection is tenant specific 
 * and the table used by the query plan is multi-tenant, then the tenant id column is not included as well.
 * @param plan - query plan to get info for.
 * @param conn - connection used to generate the query plan. Caller should take care of closing the connection appropriately.
 * @param forDataTable - if true, then family names and column names correspond to the data table even if the query plan uses
 * the secondary index table. If false, and if the query plan uses the secondary index table, then the family names and column 
 * names correspond to the index table.
 * @throws SQLException
 */
@Deprecated
public static void getPkColsForSql(List<Pair<String, String>> columns, QueryPlan plan, Connection conn, boolean forDataTable) throws SQLException {
    checkNotNull(columns);
    checkNotNull(plan);
    checkNotNull(conn);
    List<PColumn> pkColumns = getPkColumns(plan.getTableRef().getTable(), conn, forDataTable);
    String columnName;
    String familyName;
    for (PColumn pCol : pkColumns ) {
        columnName = addQuotes(pCol.getName().getString());
        familyName = pCol.getFamilyName() != null ? addQuotes(pCol.getFamilyName().getString()) : null;
        columns.add(new Pair<String, String>(familyName, columnName));
    }
}
 
Example 10
Source File: SchemaUtil.java    From phoenix with Apache License 2.0 4 votes vote down vote up
public static boolean isPKColumn(PColumn column) {
    return column.getFamilyName() == null;
}
 
Example 11
Source File: TupleProjectionCompiler.java    From phoenix with Apache License 2.0 4 votes vote down vote up
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 new PTableImpl.Builder()
            .setType(PTableType.PROJECTED)
            .setTimeStamp(table.getTimeStamp())
            .setIndexDisableTimestamp(table.getIndexDisableTimestamp())
            .setSequenceNumber(table.getSequenceNumber())
            .setImmutableRows(table.isImmutableRows())
            .setDisableWAL(table.isWALDisabled())
            .setMultiTenant(table.isMultiTenant())
            .setStoreNulls(table.getStoreNulls())
            .setViewType(table.getViewType())
            .setViewIndexIdType(table.getviewIndexIdType())
            .setViewIndexId(table.getViewIndexId())
            .setTransactionProvider(table.getTransactionProvider())
            .setUpdateCacheFrequency(table.getUpdateCacheFrequency())
            .setNamespaceMapped(table.isNamespaceMapped())
            .setAutoPartitionSeqName(table.getAutoPartitionSeqName())
            .setAppendOnlySchema(table.isAppendOnlySchema())
            .setImmutableStorageScheme(table.getImmutableStorageScheme())
            .setQualifierEncodingScheme(table.getEncodingScheme())
            .setBaseColumnCount(BASE_TABLE_BASE_COLUMN_COUNT)
            .setEncodedCQCounter(cqCounter)
            .setUseStatsForParallelization(table.useStatsForParallelization())
            .setExcludedColumns(ImmutableList.of())
            .setTenantId(table.getTenantId())
            .setSchemaName(PROJECTED_TABLE_SCHEMA)
            .setTableName(table.getTableName())
            .setPkName(table.getPKName())
            .setRowKeyOrderOptimizable(table.rowKeyOrderOptimizable())
            .setBucketNum(table.getBucketNum())
            .setIndexes(Collections.emptyList())
            .setPhysicalNames(ImmutableList.of())
            .setColumns(projectedColumns)
            .build();
}
 
Example 12
Source File: ProjectionCompiler.java    From phoenix with Apache License 2.0 4 votes vote down vote up
private static void projectAllTableColumns(StatementContext context, TableRef tableRef, boolean resolveColumn, List<Expression> projectedExpressions, List<ExpressionProjector> projectedColumns, List<? extends PDatum> targetColumns) throws SQLException {
    ColumnResolver resolver = context.getResolver();
    PTable table = tableRef.getTable();
    int projectedOffset = projectedExpressions.size();
    int posOffset = table.getBucketNum() == null ? 0 : 1;
    int minPKOffset = getMinPKOffset(table, context.getConnection().getTenantId());
    for (int i = posOffset, j = posOffset; i < table.getColumns().size(); i++) {
        PColumn column = table.getColumns().get(i);
        // Skip tenant ID column (which may not be the first column, but is the first PK column)
        if (SchemaUtil.isPKColumn(column) && j++ < minPKOffset) {
            posOffset++;
            continue;
        }
        ColumnRef ref = new ColumnRef(tableRef,i);
        String colName = ref.getColumn().getName().getString();
        String tableAlias = tableRef.getTableAlias();
        if (resolveColumn) {
            try {
                if (tableAlias != null) {
                    ref = resolver.resolveColumn(null, tableAlias, colName);
                } else {
                    String schemaName = table.getSchemaName().getString();
                    ref = resolver.resolveColumn(schemaName.length() == 0 ? null : schemaName, table.getTableName().getString(), colName);
                }
            } catch (AmbiguousColumnException e) {
                if (column.getFamilyName() != null) {
                    ref = resolver.resolveColumn(tableAlias != null ? tableAlias : table.getTableName().getString(), column.getFamilyName().getString(), colName);
                } else {
                    throw e;
                }
            }
        }
        Expression expression = ref.newColumnExpression();
        expression = coerceIfNecessary(i-posOffset+projectedOffset, targetColumns, expression);
        ImmutableBytesWritable ptr = context.getTempPtr();
        if (IndexUtil.getViewConstantValue(column, ptr)) {
            expression = LiteralExpression.newConstant(column.getDataType().toObject(ptr), expression.getDataType());
        }
        projectedExpressions.add(expression);
        boolean isCaseSensitive = !SchemaUtil.normalizeIdentifier(colName).equals(colName);
        projectedColumns.add(new ExpressionProjector(colName, tableRef.getTableAlias() == null ? table.getName().getString() : tableRef.getTableAlias(), expression, isCaseSensitive));
    }
}
 
Example 13
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 14
Source File: SchemaUtil.java    From phoenix with Apache License 2.0 4 votes vote down vote up
public static boolean isPKColumn(PColumn column) {
    return column.getFamilyName() == null;
}
 
Example 15
Source File: SchemaUtil.java    From phoenix with Apache License 2.0 4 votes vote down vote up
public static String getColumnDisplayName(PColumn column) {
    PName columnName = column.getFamilyName();
    String cf = columnName == null ? null : columnName.getString();
    return getName(cf == null || cf.isEmpty() ? null : cf, column.getName().getString(), false);
}
 
Example 16
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 3 votes vote down vote up
/**
 *
 * @param columns - Initialized empty list to be filled with the pairs of column family name and column name for columns that are used 
 * as row key for the query plan. Column family names are optional and hence the first part of the pair is nullable.
 * Column names and family names are enclosed in double quotes to allow for case sensitivity and for presence of 
 * special characters. Salting column and view index id column are not included. If the connection is tenant specific 
 * and the table used by the query plan is multi-tenant, then the tenant id column is not included as well.
 * @param plan - query plan to get info for.
 * @param conn - connection used to generate the query plan. Caller should take care of closing the connection appropriately.
 * @param forDataTable - if true, then family names and column names correspond to the data table even if the query plan uses
 * the secondary index table. If false, and if the query plan uses the secondary index table, then the family names and column 
 * names correspond to the index table.
 * @throws SQLException
 */
public static void getPkColsForSql(List<Pair<String, String>> columns, QueryPlan plan, Connection conn, boolean forDataTable) throws SQLException {
    checkNotNull(columns);
    checkNotNull(plan);
    checkNotNull(conn);
    List<PColumn> pkColumns = getPkColumns(plan.getTableRef().getTable(), conn, forDataTable);
    String columnName;
    String familyName;
    for (PColumn pCol : pkColumns ) {
        columnName = addQuotes(pCol.getName().getString());
        familyName = pCol.getFamilyName() != null ? addQuotes(pCol.getFamilyName().getString()) : null;
        columns.add(new Pair<String, String>(familyName, columnName));
    }
}