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

The following examples show how to use org.apache.phoenix.schema.PColumn#getDataType() . 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: WhereOptimizer.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Override
public KeySlots visitLeave(IsNullExpression node, List<KeySlots> childParts) {
    if (childParts.isEmpty()) {
        return null;
    }
    KeySlots childSlots = childParts.get(0);
    KeySlot childSlot = childSlots.iterator().next();
    PColumn column = childSlot.getKeyPart().getColumn();
    PDataType type = column.getDataType();
    boolean isFixedWidth = type.isFixedWidth();
    if (isFixedWidth) { // if column can't be null
        return node.isNegate() ? null : 
            newKeyParts(childSlot, node, type.getKeyRange(new byte[SchemaUtil.getFixedByteSize(column)], true,
                                                          KeyRange.UNBOUND, true));
    } else {
        KeyRange keyRange = node.isNegate() ? KeyRange.IS_NOT_NULL_RANGE : KeyRange.IS_NULL_RANGE;
        return newKeyParts(childSlot, node, keyRange);
    }
}
 
Example 2
Source File: SchemaUtil.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * Imperfect estimate of row size given a PTable
 * TODO: keep row count in stats table and use total size / row count instead
 * @param table
 * @return estimate of size in bytes of a row
 */
public static long estimateRowSize(PTable table) {
	int keyLength = estimateKeyLength(table);
	long rowSize = 0;
	for (PColumn column : table.getColumns()) {
		if (!SchemaUtil.isPKColumn(column)) {
            PDataType type = column.getDataType();
            Integer maxLength = column.getMaxLength();
            int valueLength = !type.isFixedWidth() ? VAR_KV_LENGTH_ESTIMATE : maxLength == null ? type.getByteSize() : maxLength;
			rowSize += KeyValue.getKeyValueDataStructureSize(keyLength, column.getFamilyName().getBytes().length, column.getName().getBytes().length, valueLength);
		}
	}
	// Empty key value
	rowSize += KeyValue.getKeyValueDataStructureSize(keyLength, getEmptyColumnFamily(table).length, QueryConstants.EMPTY_COLUMN_BYTES.length, 0);
	return rowSize;
}
 
Example 3
Source File: SchemaUtil.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * Imperfect estimate of row size given a PTable
 * TODO: keep row count in stats table and use total size / row count instead
 * @param table
 * @return estimate of size in bytes of a row
 */
public static long estimateRowSize(PTable table) {
	int keyLength = estimateKeyLength(table);
	long rowSize = 0;
	for (PColumn column : table.getColumns()) {
		if (!SchemaUtil.isPKColumn(column)) {
            PDataType type = column.getDataType();
            Integer maxLength = column.getMaxLength();
            int valueLength = !type.isFixedWidth() ? VAR_KV_LENGTH_ESTIMATE : maxLength == null ? type.getByteSize() : maxLength;
			rowSize += KeyValue.getKeyValueDataStructureSize(keyLength, column.getFamilyName().getBytes().length, column.getName().getBytes().length, valueLength);
		}
	}
	byte[] emptyKeyValueKV = EncodedColumnsUtil.getEmptyKeyValueInfo(table).getFirst();
	// Empty key value
	rowSize += KeyValue.getKeyValueDataStructureSize(keyLength, getEmptyColumnFamily(table).length, emptyKeyValueKV.length, 0);
	return rowSize;
}
 
Example 4
Source File: IndexColumnNames.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private String getDataTypeString(PColumn col) {
    PDataType<?> dataType = col.getDataType();
    switch (dataType.getSqlType()) {
    case Types.DECIMAL:
        String typeStr = dataType.toString();
        if (col.getMaxLength() != null) {
            typeStr += "(" + col.getMaxLength().toString();
            if (col.getScale() != null) {
                typeStr += "," + col.getScale().toString();
            }
            typeStr += ")";
        }
        return typeStr;
    default:
        if (col.getMaxLength() != null) {
            return String.format("%s(%s)", dataType.toString(), col.getMaxLength());
        }
        return dataType.toString();
    }
}
 
Example 5
Source File: WhereOptimizer.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Override
public KeySlots visitLeave(IsNullExpression node, List<KeySlots> childParts) {
    if (childParts.isEmpty()) {
        return null;
    }
    KeySlots childSlots = childParts.get(0);
    KeySlot childSlot = childSlots.getSlots().get(0);
    PColumn column = childSlot.getKeyPart().getColumn();
    PDataType type = column.getDataType();
    boolean isFixedWidth = type.isFixedWidth();
    // Nothing changes for IS NULL and IS NOT NULL when DESC since
    // we represent NULL the same way for ASC and DESC
    if (isFixedWidth) { // if column can't be null
        return node.isNegate() ? null : 
            newKeyParts(childSlot, node, type.getKeyRange(new byte[SchemaUtil.getFixedByteSize(column)], true,
                                                          KeyRange.UNBOUND, true));
    } else {
        KeyRange keyRange = node.isNegate() ? KeyRange.IS_NOT_NULL_RANGE : KeyRange.IS_NULL_RANGE;
        return newKeyParts(childSlot, node, keyRange);
    }
}
 
Example 6
Source File: WhereOptimizer.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public KeySlots visitLeave(LikeExpression node, List<KeySlots> childParts) {
    // TODO: optimize ILIKE by creating two ranges for the literal prefix: one with lower case, one with upper case
    if (childParts.isEmpty()) {
        return null;
    }
    // for SUBSTR(<column>,1,3) LIKE 'foo%'
    KeySlots childSlots = childParts.get(0);
    KeySlot childSlot = childSlots.iterator().next();
    final String startsWith = node.getLiteralPrefix();
    byte[] key = PChar.INSTANCE.toBytes(startsWith, node.getChildren().get(0).getSortOrder());
    // If the expression is an equality expression against a fixed length column
    // and the key length doesn't match the column length, the expression can
    // never be true.
    // An zero length byte literal is null which can never be compared against as true
    Expression firstChild = node.getChildren().get(0);
    Integer childNodeFixedLength = firstChild.getDataType().isFixedWidth() ? firstChild.getMaxLength() : null;
    if (childNodeFixedLength != null && key.length > childNodeFixedLength) {
        return EMPTY_KEY_SLOTS;
    }
    // TODO: is there a case where we'd need to go through the childPart to calculate the key range?
    PColumn column = childSlot.getKeyPart().getColumn();
    PDataType type = column.getDataType();
    byte[] lowerRange = key;
    byte[] upperRange = ByteUtil.nextKey(key);
    Integer columnFixedLength = column.getMaxLength();
    if (type.isFixedWidth() && columnFixedLength != null) {
        lowerRange = StringUtil.padChar(lowerRange, columnFixedLength);
        upperRange = StringUtil.padChar(upperRange, columnFixedLength);
    }
    KeyRange keyRange = type.getKeyRange(lowerRange, true, upperRange, false);
    // Only extract LIKE expression if pattern ends with a wildcard and everything else was extracted
    return newKeyParts(childSlot, node.endsWithOnlyWildcard() ? node : null, keyRange);
}
 
Example 7
Source File: IndexTestUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private static void coerceDataValueToIndexValue(PColumn dataColumn, PColumn indexColumn, ImmutableBytesWritable ptr) {
    PDataType dataType = dataColumn.getDataType();
    // TODO: push to RowKeySchema? 
    SortOrder dataModifier = dataColumn.getSortOrder();
    PDataType indexType = indexColumn.getDataType();
    SortOrder indexModifier = indexColumn.getSortOrder();
    // We know ordinal position will match pk position, because you cannot
    // alter an index table.
    indexType.coerceBytes(ptr, dataType, dataModifier, indexModifier);
}
 
Example 8
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param pCol
 * @return sql type name that could be used in DDL statements, dynamic column types etc. 
 */
public static String getSqlTypeName(PColumn pCol) {
    PDataType dataType = pCol.getDataType();
    Integer maxLength = pCol.getMaxLength();
    Integer scale = pCol.getScale();
    return getSqlTypeName(dataType, maxLength, scale);
}
 
Example 9
Source File: IndexUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static PColumn getIndexPKColumn(int position, PColumn dataColumn) {
	assert (SchemaUtil.isPKColumn(dataColumn));
	PName indexColumnName = PNameFactory.newName(getIndexColumnName(null, dataColumn.getName().getString()));
	PColumn column = new PColumnImpl(indexColumnName, null, dataColumn.getDataType(), dataColumn.getMaxLength(),
			dataColumn.getScale(), dataColumn.isNullable(), position, dataColumn.getSortOrder(),
			dataColumn.getArraySize(), null, false, dataColumn.getExpressionStr(), dataColumn.isRowTimestamp(), false,
			// TODO set the columnQualifierBytes correctly
			/*columnQualifierBytes*/null, HConstants.LATEST_TIMESTAMP); 
	return column;
}
 
Example 10
Source File: SchemaUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * Pads the data in ptr by the required amount for fixed width data types
 */
public static void padData(String tableName, PColumn column, ImmutableBytesWritable ptr) {
    PDataType type = column.getDataType();
    byte[] byteValue = ptr.get();
    boolean isNull = type.isNull(byteValue);
    Integer maxLength = column.getMaxLength();
    if (!isNull && type.isFixedWidth() && maxLength != null) {
        if (ptr.getLength() < maxLength) {
            type.pad(ptr, maxLength, column.getSortOrder());
        } else if (ptr.getLength() > maxLength) {
            throw new DataExceedsCapacityException(tableName + "." + column.getName().getString() + " may not exceed " + maxLength + " bytes (" + type.toObject(byteValue) + ")");
        }
    }
}
 
Example 11
Source File: SchemaUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * Estimate the max key length in bytes of the PK for a given table
 * @param table the table
 * @return the max PK length
 */
public static int estimateKeyLength(PTable table) {
    int maxKeyLength = 0;
    // Calculate the max length of a key (each part must currently be of a fixed width)
    int i = 0;
    List<PColumn> columns = table.getPKColumns();
    while (i < columns.size()) {
        PColumn keyColumn = columns.get(i++);
        PDataType type = keyColumn.getDataType();
        Integer maxLength = keyColumn.getMaxLength();
        maxKeyLength += !type.isFixedWidth() ? VAR_LENGTH_ESTIMATE : maxLength == null ? type.getByteSize() : maxLength;
    }
    return maxKeyLength;
}
 
Example 12
Source File: BaseResultIterators.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private int computeColumnsInCommon() {
    PTable dataTable;
    if ((dataTable=dataPlan.getTableRef().getTable()).getBucketNum() != null) { // unable to compute prefix range for salted data table
        return 0;
    }

    PTable table = getTable();
    int nColumnsOffset = dataTable.isMultiTenant() ? 1 :0;
    int nColumnsInCommon = nColumnsOffset;
    List<PColumn> dataPKColumns = dataTable.getPKColumns();
    List<PColumn> indexPKColumns = table.getPKColumns();
    int nIndexPKColumns = indexPKColumns.size();
    int nDataPKColumns = dataPKColumns.size();
    // Skip INDEX_ID and tenant ID columns
    for (int i = 1 + nColumnsInCommon; i < nIndexPKColumns; i++) {
        PColumn indexColumn = indexPKColumns.get(i);
        String indexColumnName = indexColumn.getName().getString();
        String cf = IndexUtil.getDataColumnFamilyName(indexColumnName);
        if (cf.length() != 0) {
            break;
        }
        if (i > nDataPKColumns) {
            break;
        }
        PColumn dataColumn = dataPKColumns.get(i-1);
        String dataColumnName = dataColumn.getName().getString();
        // Ensure both name and type are the same. Because of the restrictions we have
        // on PK column types (namely that you can only have a fixed width nullable
        // column as your last column), the type check is more of a sanity check
        // since it wouldn't make sense to have an index with every column in common.
        if (indexColumn.getDataType() == dataColumn.getDataType() 
                && dataColumnName.equals(IndexUtil.getDataColumnName(indexColumnName))) {
            nColumnsInCommon++;
            continue;
        }
        break;
    }
    return nColumnsInCommon;
}
 
Example 13
Source File: UpsertCompiler.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private static LiteralParseNode getNodeForRowTimestampColumn(PColumn col) {
    PDataType type = col.getDataType();
    long dummyValue = 0L;
    if (type.isCoercibleTo(PTimestamp.INSTANCE)) {
        return new LiteralParseNode(new Timestamp(dummyValue), PTimestamp.INSTANCE);
    } else if (type == PLong.INSTANCE || type == PUnsignedLong.INSTANCE) {
        return new LiteralParseNode(dummyValue, PLong.INSTANCE);
    }
    throw new IllegalArgumentException();
}
 
Example 14
Source File: IndexStatementRewriter.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public ParseNode visit(ColumnParseNode node) throws SQLException {
    ColumnRef dataColRef = getResolver().resolveColumn(node.getSchemaName(), node.getTableName(), node.getName());
    PColumn dataCol = dataColRef.getColumn();
    TableRef dataTableRef = dataColRef.getTableRef();
    // Rewrite view constants as literals, as they won't be in the schema for
    // an index on the view. Our view may be READ_ONLY yet still have inherited
    // view constants if based on an UPDATABLE view
    if (dataCol.getViewConstant() != null) {
        byte[] viewConstant = dataCol.getViewConstant();
        // Ignore last byte, as it's only there so we can have a way to differentiate null
        // from the absence of a value.
        ptr.set(viewConstant, 0, viewConstant.length-1);
        Object literal = dataCol.getDataType().toObject(ptr);
        return new LiteralParseNode(literal, dataCol.getDataType());
    }
    TableName tName = getReplacedTableName(dataTableRef);
    if (multiTableRewriteMap != null && tName == null)
        return node;

    String indexColName = IndexUtil.getIndexColumnName(dataCol);
    ParseNode indexColNode = new ColumnParseNode(tName, '"' + indexColName + '"', node.getAlias());
    PDataType indexColType = IndexUtil.getIndexColumnDataType(dataCol);
    PDataType dataColType = dataColRef.getColumn().getDataType();

    // Coerce index column reference back to same type as data column so that
    // expression behave exactly the same. No need to invert, as this will be done
    // automatically as needed. If node is used at the top level, do not convert, as
    // otherwise the wrapper gets in the way in the group by clause. For example,
    // an INTEGER column in a GROUP BY gets doubly wrapped like this:
    //     CAST CAST int_col AS INTEGER AS DECIMAL
    // This is unnecessary and problematic in the case of a null value.
    // TODO: test case for this
    if (!isTopLevel() && indexColType != dataColType) {
        indexColNode = FACTORY.cast(indexColNode, dataColType, null, null);
    }
    return indexColNode;
}
 
Example 15
Source File: IndexTestUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private static void coerceDataValueToIndexValue(PColumn dataColumn, PColumn indexColumn, ImmutableBytesWritable ptr) {
    PDataType dataType = dataColumn.getDataType();
    // TODO: push to RowKeySchema? 
    SortOrder dataModifier = dataColumn.getSortOrder();
    PDataType indexType = indexColumn.getDataType();
    SortOrder indexModifier = indexColumn.getSortOrder();
    // We know ordinal position will match pk position, because you cannot
    // alter an index table.
    indexType.coerceBytes(ptr, dataType, dataModifier, indexModifier);
}
 
Example 16
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param pCol
 * @return sql type name that could be used in DDL statements, dynamic column types etc. 
 */
public static String getSqlTypeName(PColumn pCol) {
    PDataType dataType = pCol.getDataType();
    Integer maxLength = pCol.getMaxLength();
    Integer scale = pCol.getScale();
    return dataType.isArrayType() ? getArraySqlTypeName(maxLength, scale, dataType) : appendMaxLengthAndScale(maxLength, scale, dataType.getSqlTypeName());
}
 
Example 17
Source File: SchemaUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * Estimate the max key length in bytes of the PK for a given table
 * @param table the table
 * @return the max PK length
 */
public static int estimateKeyLength(PTable table) {
    int maxKeyLength = 0;
    // Calculate the max length of a key (each part must currently be of a fixed width)
    int i = 0;
    List<PColumn> columns = table.getPKColumns();
    while (i < columns.size()) {
        PColumn keyColumn = columns.get(i++);
        PDataType type = keyColumn.getDataType();
        Integer maxLength = keyColumn.getMaxLength();
        maxKeyLength += !type.isFixedWidth() ? VAR_LENGTH_ESTIMATE : maxLength == null ? type.getByteSize() : maxLength;
    }
    return maxKeyLength;
}
 
Example 18
Source File: JoinCompiler.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private void addProjectedColumn(List<PColumn> projectedColumns, List<Expression> sourceExpressions,
        ListMultimap<String, String> columnNameMap, PColumn sourceColumn, PName familyName, boolean hasSaltingColumn,
        boolean isLocalIndexColumnRef, StatementContext context)
throws SQLException {
    if (sourceColumn == SALTING_COLUMN)
        return;

    int position = projectedColumns.size() + (hasSaltingColumn ? 1 : 0);
    PTable table = tableRef.getTable();
    String schemaName = table.getSchemaName().getString();
    String tableName = table.getTableName().getString();
    String colName = isLocalIndexColumnRef ? IndexUtil.getIndexColumnName(sourceColumn) : sourceColumn.getName().getString();
    String fullName = getProjectedColumnName(schemaName, tableName, colName);
    String aliasedName = tableRef.getTableAlias() == null ? fullName : getProjectedColumnName(null, tableRef.getTableAlias(), colName);

    columnNameMap.put(colName, aliasedName);
    if (!fullName.equals(aliasedName)) {
        columnNameMap.put(fullName, aliasedName);
    }

    PName name = PNameFactory.newName(aliasedName);
    PColumnImpl column = new PColumnImpl(name, familyName, sourceColumn.getDataType(),
            sourceColumn.getMaxLength(), sourceColumn.getScale(), sourceColumn.isNullable(),
            position, sourceColumn.getSortOrder(), sourceColumn.getArraySize(), sourceColumn.getViewConstant(), sourceColumn.isViewReferenced(), sourceColumn.getExpressionStr());
    Expression sourceExpression = isLocalIndexColumnRef ?
              NODE_FACTORY.column(TableName.create(schemaName, tableName), "\"" + colName + "\"", null).accept(new ExpressionCompiler(context))
            : new ColumnRef(tableRef, sourceColumn.getPosition()).newColumnExpression();
    projectedColumns.add(column);
    sourceExpressions.add(sourceExpression);
}
 
Example 19
Source File: IndexStatementRewriter.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public ParseNode visit(ColumnParseNode node) throws SQLException {
    ColumnRef dataColRef = getResolver().resolveColumn(node.getSchemaName(), node.getTableName(), node.getName());
    PColumn dataCol = dataColRef.getColumn();
    TableRef dataTableRef = dataColRef.getTableRef();
    // Rewrite view constants as literals, as they won't be in the schema for
    // an index on the view. Our view may be READ_ONLY yet still have inherited
    // view constants if based on an UPDATABLE view
    if (dataCol.getViewConstant() != null) {
        byte[] viewConstant = dataCol.getViewConstant();
        // Ignore last byte, as it's only there so we can have a way to differentiate null
        // from the absence of a value.
        ptr.set(viewConstant, 0, viewConstant.length-1);
        Object literal = dataCol.getDataType().toObject(ptr);
        return new LiteralParseNode(literal, dataCol.getDataType());
    }
    TableName tName = getReplacedTableName(dataTableRef);
    if (multiTableRewriteMap != null && tName == null)
        return node;

    String indexColName = IndexUtil.getIndexColumnName(dataCol);
    ParseNode indexColNode = new ColumnParseNode(tName, '"' + indexColName + '"', node.getAlias());
    PDataType indexColType = IndexUtil.getIndexColumnDataType(dataCol);
    PDataType dataColType = dataColRef.getColumn().getDataType();

    // Coerce index column reference back to same type as data column so that
    // expression behave exactly the same. No need to invert, as this will be done
    // automatically as needed. If node is used at the top level, do not convert, as
    // otherwise the wrapper gets in the way in the group by clause. For example,
    // an INTEGER column in a GROUP BY gets doubly wrapped like this:
    //     CAST CAST int_col AS INTEGER AS DECIMAL
    // This is unnecessary and problematic in the case of a null value.
    // TODO: test case for this
    if (!isTopLevel() && indexColType != dataColType) {
        indexColNode = FACTORY.cast(indexColNode, dataColType, null, null);
    }
    return indexColNode;
}
 
Example 20
Source File: WhereOptimizer.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@Override
public KeySlots visitLeave(LikeExpression node, List<KeySlots> childParts) {
    // TODO: optimize ILIKE by creating two ranges for the literal prefix: one with lower case, one with upper case
    if (childParts.isEmpty()) {
        return null;
    }
    // for SUBSTR(<column>,1,3) LIKE 'foo%'
    KeySlots childSlots = childParts.get(0);
    KeySlot childSlot = childSlots.getSlots().get(0);
    final String startsWith = node.getLiteralPrefix();
    SortOrder sortOrder = node.getChildren().get(0).getSortOrder();
    byte[] key = PVarchar.INSTANCE.toBytes(startsWith, sortOrder);
    // If the expression is an equality expression against a fixed length column
    // and the key length doesn't match the column length, the expression can
    // never be true.
    // An zero length byte literal is null which can never be compared against as true
    Expression firstChild = node.getChildren().get(0);
    Integer childNodeFixedLength = firstChild.getDataType().isFixedWidth() ? firstChild.getMaxLength() : null;
    if (childNodeFixedLength != null && key.length > childNodeFixedLength) {
        return EMPTY_KEY_SLOTS;
    }
    // TODO: is there a case where we'd need to go through the childPart to calculate the key range?
    PColumn column = childSlot.getKeyPart().getColumn();
    PDataType type = column.getDataType();
    byte[] lowerRange = key;
    byte[] upperRange = ByteUtil.nextKey(key);
    Integer columnFixedLength = column.getMaxLength();
    if (type.isFixedWidth()) {
        if (columnFixedLength != null) { // Sanity check - should always be non null
            // Always use minimum byte to fill as otherwise our key is bigger
            // that it should be when the sort order is descending.
            lowerRange = type.pad(lowerRange, columnFixedLength, SortOrder.ASC);
            upperRange = type.pad(upperRange, columnFixedLength, SortOrder.ASC);
        }
    } else if (column.getSortOrder() == SortOrder.DESC && table.rowKeyOrderOptimizable()) {
        // Append a zero byte if descending since a \xFF byte will be appended to the lowerRange
        // causing rows to be skipped that should be included. For example, with rows 'ab', 'a',
        // a lowerRange of 'a\xFF' would skip 'ab', while 'a\x00\xFF' would not.
        lowerRange = Arrays.copyOf(lowerRange, lowerRange.length+1);
        lowerRange[lowerRange.length-1] = QueryConstants.SEPARATOR_BYTE;
    }
    KeyRange range = type.getKeyRange(lowerRange, true, upperRange, false);
    if (column.getSortOrder() == SortOrder.DESC) {
        range = range.invert();
    }
    // Only extract LIKE expression if pattern ends with a wildcard and everything else was extracted
    return newKeyParts(childSlot, node.endsWithOnlyWildcard() ? node : null, range);
}