Java Code Examples for org.apache.phoenix.util.SchemaUtil#isPKColumn()

The following examples show how to use org.apache.phoenix.util.SchemaUtil#isPKColumn() . 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: PhoenixResultSet.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * Populate the static columns and the starting position for dynamic columns which we use when
 * merging column projectors of static and dynamic columns
 * @return Pair whose first part is the list of static column PColumns and the second part is
 * the starting position for dynamic columns
 */
private Pair<List<PColumn>, Integer> getStaticColsAndStartingPosForDynCols(){
    List<PColumn> staticCols = new ArrayList<>();
    for (ColumnProjector cp : this.rowProjector.getColumnProjectors()) {
        Expression exp = cp.getExpression();
        if (exp instanceof ProjectedColumnExpression) {
            staticCols.addAll(((ProjectedColumnExpression) exp).getColumns());
            break;
        }
    }
    int startingPosForDynCols = 0;
    for (PColumn col : staticCols) {
        if (!SchemaUtil.isPKColumn(col)) {
            startingPosForDynCols++;
        }
    }
    return new Pair<>(staticCols, startingPosForDynCols);
}
 
Example 2
Source File: ColumnRef.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public ColumnRef(TableRef tableRef, int columnPosition) {
    if (tableRef == null) {
        throw new NullPointerException();
    }
    if (columnPosition < 0 || columnPosition >= tableRef.getTable().getColumns().size()) {
        throw new IllegalArgumentException("Column position of " + columnPosition + " must be between 0 and " + tableRef.getTable().getColumns().size() + " for table " + tableRef.getTable().getName().getString());
    }
    this.tableRef = tableRef;
    this.columnPosition = columnPosition;
    PColumn column = getColumn();
    int i = -1;
    if (SchemaUtil.isPKColumn(column)) {
        for (PColumn pkColumn : tableRef.getTable().getPKColumns()) {
            i++;
            if (pkColumn == column) {
                break;
            }
        }
    }
    pkSlotPosition = i;
}
 
Example 3
Source File: WhereCompiler.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Override
protected ColumnRef resolveColumn(ColumnParseNode node) throws SQLException {
    ColumnRef ref = super.resolveColumn(node);
    PTable table = ref.getTable();
    // Track if we need to compare KeyValue during filter evaluation
    // using column family. If the column qualifier is enough, we
    // just use that.
    try {
        if (!SchemaUtil.isPKColumn(ref.getColumn())) {
            table.getColumnForColumnName(ref.getColumn().getName().getString());
        }
    } catch (AmbiguousColumnException e) {
        disambiguateWithFamily = true;
    }
    return ref;
 }
 
Example 4
Source File: SortMergeJoinPlan.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private static KeyValueSchema buildSchema(PTable table) {
    KeyValueSchemaBuilder builder = new KeyValueSchemaBuilder(0);
    if (table != null) {
        for (PColumn column : table.getColumns()) {
            if (!SchemaUtil.isPKColumn(column)) {
                builder.addField(column);
            }
        }
    }
    return builder.build();
}
 
Example 5
Source File: ColumnParseNode.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public void toSQL(ColumnResolver resolver, StringBuilder buf) {
    // If resolver is not null, then resolve to get fully qualified name
    String tableName = null;
    if (resolver == null) {
        if (this.tableName != null) {
            tableName = this.tableName.getTableName();
        }
    } else {
        try {
            ColumnRef ref = resolver.resolveColumn(this.getSchemaName(), this.getTableName(), this.getName());
            PColumn column = ref.getColumn();
            if (!SchemaUtil.isPKColumn(column)) {
                PTable table = ref.getTable();
                String defaultFamilyName = table.getDefaultFamilyName() == null ? QueryConstants.DEFAULT_COLUMN_FAMILY : table.getDefaultFamilyName().getString();
                // Translate to the data table column name
                String dataFamilyName = column.getFamilyName().getString() ;
                tableName = defaultFamilyName.equals(dataFamilyName) ? null : dataFamilyName;
            }
            
        } catch (SQLException e) {
            throw new RuntimeException(e); // Already resolved, so not possible
        }
    }
    if (tableName != null) {
        if (isTableNameCaseSensitive()) {
            buf.append('"');
            buf.append(tableName);
            buf.append('"');
        } else {
            buf.append(tableName);
        }
        buf.append('.');
    }
    toSQL(buf);
}
 
Example 6
Source File: CorrelatePlan.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private static KeyValueSchema buildSchema(PTable table) {
    KeyValueSchemaBuilder builder = new KeyValueSchemaBuilder(0);
    if (table != null) {
        for (PColumn column : table.getColumns()) {
            if (!SchemaUtil.isPKColumn(column)) {
                builder.addField(column);
            }
        }
    }
    return builder.build();
}
 
Example 7
Source File: HashJoinInfo.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private static KeyValueSchema buildSchema(PTable table) {
	KeyValueSchemaBuilder builder = new KeyValueSchemaBuilder(0);
	if (table != null) {
	    for (PColumn column : table.getColumns()) {
	        if (!SchemaUtil.isPKColumn(column)) {
	            builder.addField(column);
	        }
	    }
	}
    return builder.build();
}
 
Example 8
Source File: JoinCompiler.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public void projectColumns(Scan scan) {
    assert(!isSubselect());
    if (isWildCardSelect()) {
        scan.getFamilyMap().clear();
        return;
    }
    for (ColumnRef columnRef : columnRefs.keySet()) {
        if (columnRef.getTableRef().equals(tableRef)
                && !SchemaUtil.isPKColumn(columnRef.getColumn())
                && !(columnRef instanceof LocalIndexColumnRef)) {
        	EncodedColumnsUtil.setColumns(columnRef.getColumn(), tableRef.getTable(), scan);
        }
    }
}
 
Example 9
Source File: ExpressionCompiler.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public Expression visit(ColumnParseNode node) throws SQLException {
    ColumnRef ref = resolveColumn(node);
    TableRef tableRef = ref.getTableRef();
    ImmutableBytesWritable ptr = context.getTempPtr();
    PColumn column = ref.getColumn();
    // If we have an UPDATABLE view, then we compile those view constants (i.e. columns in equality constraints
    // in the view) to constants. This allows the optimize to optimize out reference to them in various scenarios.
    // If the column is matched in a WHERE clause against a constant not equal to it's constant, then the entire
    // query would become degenerate.
    if (!resolveViewConstants && IndexUtil.getViewConstantValue(column, ptr)) {
        return LiteralExpression.newConstant(column.getDataType().toObject(ptr), column.getDataType());
    }
    if (tableRef.equals(context.getCurrentTable()) && !SchemaUtil.isPKColumn(column)) { // project only kv columns
        context.getScan().addColumn(column.getFamilyName().getBytes(), column.getName().getBytes());
    }
    Expression expression = ref.newColumnExpression(node.isTableNameCaseSensitive(), node.isCaseSensitive());
    Expression wrappedExpression = wrapGroupByExpression(expression);
    // If we're in an aggregate expression
    // and we're not in the context of an aggregate function
    // and we didn't just wrap our column reference
    // then we're mixing aggregate and non aggregate expressions in the same expression.
    // This catches cases like this: SELECT sum(a_integer) + a_integer FROM atable GROUP BY a_string
    if (isAggregate && aggregateFunction == null && wrappedExpression == expression) {
        throwNonAggExpressionInAggException(expression.toString());
    }
    return wrappedExpression;
}
 
Example 10
Source File: TupleProjector.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public TupleProjector(ProjectedPTableWrapper projected) {
	List<PColumn> columns = projected.getTable().getColumns();
	expressions = new Expression[columns.size() - projected.getTable().getPKColumns().size()];
	// we do not count minNullableIndex for we might do later merge.
	KeyValueSchemaBuilder builder = new KeyValueSchemaBuilder(0);
	int i = 0;
    for (PColumn column : projected.getTable().getColumns()) {
    	if (!SchemaUtil.isPKColumn(column)) {
    		builder.addField(column);
    		expressions[i++] = projected.getSourceExpression(column);
    	}
    }
    schema = builder.build();
    valueSet = ValueBitSet.newInstance(schema);
}
 
Example 11
Source File: ProjectedColumnExpression.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static KeyValueSchema buildSchema(Collection<PColumn> columns) {
    KeyValueSchemaBuilder builder = new KeyValueSchemaBuilder(0);
    for (PColumn column : columns) {
        if (!SchemaUtil.isPKColumn(column)) {
            builder.addField(column);
        }
    }
    return builder.build();
}
 
Example 12
Source File: ColumnParseNode.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public void toSQL(ColumnResolver resolver, StringBuilder buf) {
    // If resolver is not null, then resolve to get fully qualified name
    String tableName = null;
    if (resolver == null) {
        if (this.tableName != null) {
            tableName = this.tableName.getTableName();
        }
    } else {
        try {
            ColumnRef ref = resolver.resolveColumn(this.getSchemaName(), this.getTableName(), this.getName());
            PColumn column = ref.getColumn();
            if (!SchemaUtil.isPKColumn(column)) {
                PTable table = ref.getTable();
                String defaultFamilyName = table.getDefaultFamilyName() == null ? QueryConstants.DEFAULT_COLUMN_FAMILY : table.getDefaultFamilyName().getString();
                // Translate to the data table column name
                String dataFamilyName = column.getFamilyName().getString() ;
                tableName = defaultFamilyName.equals(dataFamilyName) ? null : dataFamilyName;
            }
            
        } catch (SQLException e) {
            throw new RuntimeException(e); // Already resolved, so not possible
        }
    }
    if (tableName != null) {
        if (isTableNameCaseSensitive()) {
            buf.append('"');
            buf.append(tableName);
            buf.append('"');
        } else {
            buf.append(tableName);
        }
        buf.append('.');
    }
    toSQL(buf);
}
 
Example 13
Source File: HashJoinInfo.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private static KeyValueSchema buildSchema(PTable table) {
	KeyValueSchemaBuilder builder = new KeyValueSchemaBuilder(0);
	if (table != null) {
	    for (PColumn column : table.getColumns()) {
	        if (!SchemaUtil.isPKColumn(column)) {
	            builder.addField(column);
	        }
	    }
	}
    return builder.build();
}
 
Example 14
Source File: JoinCompiler.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public void projectColumns(Scan scan) {
    assert(!isSubselect());
    if (isWildCardSelect()) {
        scan.getFamilyMap().clear();
        return;
    }
    for (ColumnRef columnRef : columnRefs.keySet()) {
        if (columnRef.getTableRef().equals(tableRef)
                && !SchemaUtil.isPKColumn(columnRef.getColumn())
                && !(columnRef instanceof LocalIndexColumnRef)) {
            scan.addColumn(columnRef.getColumn().getFamilyName().getBytes(), columnRef.getColumn().getName().getBytes());
        }
    }
}
 
Example 15
Source File: SortMergeJoinPlan.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private static KeyValueSchema buildSchema(PTable table) {
    KeyValueSchemaBuilder builder = new KeyValueSchemaBuilder(0);
    if (table != null) {
        for (PColumn column : table.getColumns()) {
            if (!SchemaUtil.isPKColumn(column)) {
                builder.addField(column);
            }
        }
    }
    return builder.build();
}
 
Example 16
Source File: WhereCompiler.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public Expression visit(ColumnParseNode node) throws SQLException {
    ColumnRef ref = resolveColumn(node);
    TableRef tableRef = ref.getTableRef();
    if (tableRef.equals(context.getCurrentTable()) && !SchemaUtil.isPKColumn(ref.getColumn())) {
        // track the where condition columns. Later we need to ensure the Scan in HRS scans these column CFs
        context.addWhereCoditionColumn(ref.getColumn().getFamilyName().getBytes(), ref.getColumn().getName()
                .getBytes());
    }
    return ref.newColumnExpression(node.isTableNameCaseSensitive(), node.isCaseSensitive());
}
 
Example 17
Source File: ProjectionCompiler.java    From phoenix with Apache License 2.0 5 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();
        if (resolveColumn) {
            if (tableRef.getTableAlias() != null) {
                ref = resolver.resolveColumn(null, tableRef.getTableAlias(), colName);
            } else {
                String schemaName = table.getSchemaName().getString();
                ref = resolver.resolveColumn(schemaName.length() == 0 ? null : schemaName, table.getTableName().getString(), colName);
            }
        }
        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 18
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 19
Source File: UpsertCompiler.java    From phoenix with Apache License 2.0 4 votes vote down vote up
private static void setValues(byte[][] values, int[] pkSlotIndex, int[] columnIndexes,
        PTable table, MultiRowMutationState mutation,
        PhoenixStatement statement, boolean useServerTimestamp, IndexMaintainer maintainer,
        byte[][] viewConstants, byte[] onDupKeyBytes, int numSplColumns) throws SQLException {
    long columnValueSize = 0;
    Map<PColumn,byte[]> columnValues = Maps.newHashMapWithExpectedSize(columnIndexes.length);
    byte[][] pkValues = new byte[table.getPKColumns().size()][];
    // If the table uses salting, the first byte is the salting byte, set to an empty array
    // here and we will fill in the byte later in PRowImpl.
    if (table.getBucketNum() != null) {
        pkValues[0] = new byte[] {0};
    }
    for(int i = 0; i < numSplColumns; i++) {
        pkValues[i + (table.getBucketNum() != null ? 1 : 0)] = values[i];
    }
    Long rowTimestamp = null; // case when the table doesn't have a row timestamp column
    RowTimestampColInfo rowTsColInfo = new RowTimestampColInfo(useServerTimestamp, rowTimestamp);
    for (int i = 0, j = numSplColumns; j < values.length; j++, i++) {
        byte[] value = values[j];
        PColumn column = table.getColumns().get(columnIndexes[i]);
        if (SchemaUtil.isPKColumn(column)) {
            pkValues[pkSlotIndex[i]] = value;
            if (SchemaUtil.getPKPosition(table, column) == table.getRowTimestampColPos()) {
                if (!useServerTimestamp) {
                    PColumn rowTimestampCol = table.getPKColumns().get(table.getRowTimestampColPos());
                    rowTimestamp = PLong.INSTANCE.getCodec().decodeLong(value, 0, rowTimestampCol.getSortOrder());
                    if (rowTimestamp < 0) {
                        throw new IllegalDataException("Value of a column designated as ROW_TIMESTAMP cannot be less than zero");
                    }
                    rowTsColInfo = new RowTimestampColInfo(useServerTimestamp, rowTimestamp);
                } 
            }
        } else {
            columnValues.put(column, value);
            columnValueSize += (column.getEstimatedSize() + value.length);
        }
    }
    ImmutableBytesPtr ptr = new ImmutableBytesPtr();
    table.newKey(ptr, pkValues);
    if (table.getIndexType() == IndexType.LOCAL && maintainer != null) {
        byte[] rowKey = maintainer.buildDataRowKey(ptr, viewConstants);
        HRegionLocation region =
                statement.getConnection().getQueryServices()
                        .getTableRegionLocation(table.getParentName().getBytes(), rowKey);
        byte[] regionPrefix =
                region.getRegion().getStartKey().length == 0 ? new byte[region
                        .getRegion().getEndKey().length] : region.getRegion()
                        .getStartKey();
        if (regionPrefix.length != 0) {
            ptr.set(ScanRanges.prefixKey(ptr.get(), 0, ptr.getLength(), regionPrefix,
                regionPrefix.length));
        }
    } 
    mutation.put(ptr, new RowMutationState(columnValues, columnValueSize, statement.getConnection().getStatementExecutionCounter(), rowTsColInfo, onDupKeyBytes));
}
 
Example 20
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));
    }
}