private static int estimatePartLength(int pos, Iterator<PColumn> iterator) { int length = 0; while (iterator.hasNext()) { PColumn column = iterator.next(); if (column.getDataType().isFixedWidth()) { length += SchemaUtil.getFixedByteSize(column); } else { length += 1; // SEPARATOR byte. } } return length; }
private boolean isLastPKVariableLength(PTable table) { List<PColumn> pkColumns = table.getPKColumns(); return !pkColumns.get(pkColumns.size()-1).getDataType().isFixedWidth(); }
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(); }
/** * Constructs a column info for the supplied pColumn * @param pColumn * @return columnInfo * @throws SQLException if the parameter is null. */ public static ColumnInfo getColumnInfo(PColumn pColumn) throws SQLException { if (pColumn == null) { throw new SQLException("pColumn must not be null."); } return ColumnInfo.create(pColumn.toString(), pColumn.getDataType().getSqlType(), pColumn.getMaxLength(), pColumn.getScale()); }
@Override public byte[] getViewConstant() { PDataType dataType = autoPartitionCol.getDataType(); Object val = dataType.toObject(autoPartitionNum, PLong.INSTANCE); byte[] bytes = new byte [dataType.getByteSize() + 1]; dataType.toBytes(val, bytes, 0); return bytes; } @Override
/** * * @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); }
/** * 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; }
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); }
public static PDataType getIndexColumnDataType(PColumn dataColumn) throws SQLException { PDataType type = getIndexColumnDataType(dataColumn.isNullable(),dataColumn.getDataType()); if (type == null) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.CANNOT_INDEX_COLUMN_ON_TYPE).setColumnName(dataColumn.getName().getString()) .setMessage("Type="+dataColumn.getDataType()).build().buildException(); } return type; }
@Override public Expression visit(LiteralParseNode node) throws SQLException { if (isTopLevel()) { return LiteralExpression.newConstant(node.getValue(), column.getDataType(), column.getSortOrder(), Determinism.ALWAYS); } return super.visit(node); } }
@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); } }
private void assertColumnNamesAndDefinitionsEqual(PTable table, Map<String, String> expected) { Map<String, String> actual = Maps.newHashMap(); for (PColumn column : table.getColumns()) { actual.put(column.getName().getString().trim(), column.getDataType().getSqlTypeName()); } assertEquals(expected, actual); }
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(); } }
@Override public Expression visit(BindParseNode node) throws SQLException { if (isTopLevel()) { context.getBindManager().addParamMetaData(node, column); Object value = context.getBindManager().getBindValue(node); return LiteralExpression.newConstant(value, column.getDataType(), column.getSortOrder(), Determinism.ALWAYS); } return super.visit(node); }
public PColumnImpl(PColumn column, boolean derivedColumn, int position, byte[] viewConstant) { this(column.getName(), column.getFamilyName(), column.getDataType(), column.getMaxLength(), column.getScale(), column.isNullable(), position, column.getSortOrder(), column.getArraySize(), viewConstant, column.isViewReferenced(), column.getExpressionStr(), column.isRowTimestamp(), column.isDynamic(), column.getColumnQualifierBytes(), column.getTimestamp(), derivedColumn); }
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; }
/** * 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) + ")"); } } }
public PColumnImpl(PColumn column, byte[] viewConstant, boolean isViewReferenced) { this(column.getName(), column.getFamilyName(), column.getDataType(), column.getMaxLength(), column.getScale(), column.isNullable(), column.getPosition(), column.getSortOrder(), column.getArraySize(), viewConstant, isViewReferenced, column.getExpressionStr(), column.isRowTimestamp(), column.isDynamic(), column.getColumnQualifierBytes(), column.getTimestamp(), column.isDerived()); }
@Override public KeyRange getKeyRange(CompareOp op, Expression rhs) { ImmutableBytesWritable ptr = new ImmutableBytesWritable(); rhs.evaluate(null, ptr); // If the column is fixed width, fill is up to it's byte size PDataType type = getColumn().getDataType(); if (type.isFixedWidth()) { Integer length = getColumn().getMaxLength(); if (length != null) { // Go through type to pad as the fill character depends on the type. type.pad(ptr, length, SortOrder.ASC); } } byte[] key = ByteUtil.copyKeyBytesIfNecessary(ptr); KeyRange range = ByteUtil.getKeyRange(key, rhs.getSortOrder().transform(op)/*op*/, type); // Constants will have been inverted, so we invert them back here so that // RVC comparisons work correctly (see PHOENIX-3383). if (rhs.getSortOrder() == SortOrder.DESC) { range = range.invert(); } return range; }
private void testLiteralResultIteratorPlan(Object[][] expectedResult, Integer offset, Integer limit) throws SQLException { QueryPlan plan = newLiteralResultIterationPlan(offset, limit); ResultIterator iter = plan.iterator(); ImmutableBytesWritable ptr = new ImmutableBytesWritable(); for (Object[] row : expectedResult) { Tuple next = iter.next(); assertNotNull(next); for (int i = 0; i < row.length; i++) { PColumn column = table.getColumns().get(i); boolean eval = new ProjectedColumnExpression(column, table, column.getName().getString()).evaluate(next, ptr); Object o = eval ? column.getDataType().toObject(ptr) : null; assertEquals(row[i], o); } } assertNull(iter.next()); }