use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.
the class UnionCompiler method compareExperssions.
private static void compareExperssions(int i, Expression expression, List<TargetDataExpression> targetTypes) throws SQLException {
PDataType type = expression.getDataType();
if (type != null && type.isCoercibleTo(targetTypes.get(i).getType())) {
;
} else if (targetTypes.get(i).getType() == null || targetTypes.get(i).getType().isCoercibleTo(type)) {
targetTypes.get(i).setType(type);
} else {
throw new SQLExceptionInfo.Builder(SQLExceptionCode.SELECT_COLUMN_TYPE_IN_UNIONALL_DIFFS).setMessage("Column # " + i + " is " + targetTypes.get(i).getType().getSqlTypeName() + " in 1st query where as it is " + type.getSqlTypeName() + " in 2nd query").build().buildException();
}
Integer len = expression.getMaxLength();
if (len != null && (targetTypes.get(i).getMaxLength() == null || len > targetTypes.get(i).getMaxLength())) {
targetTypes.get(i).setMaxLength(len);
}
Integer scale = expression.getScale();
if (scale != null && (targetTypes.get(i).getScale() == null || scale > targetTypes.get(i).getScale())) {
targetTypes.get(i).setScale(scale);
}
SortOrder sortOrder = expression.getSortOrder();
if (sortOrder != null && (!sortOrder.equals(targetTypes.get(i).getSortOrder())))
targetTypes.get(i).setSortOrder(SortOrder.getDefault());
}
use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.
the class MetaDataEndpointImpl method addColumnToTable.
private void addColumnToTable(List<Cell> results, PName colName, PName famName, Cell[] colKeyValues, List<PColumn> columns, boolean isSalted) {
int i = 0;
int j = 0;
while (i < results.size() && j < COLUMN_KV_COLUMNS.size()) {
Cell kv = results.get(i);
Cell searchKv = COLUMN_KV_COLUMNS.get(j);
int cmp = Bytes.compareTo(kv.getQualifierArray(), kv.getQualifierOffset(), kv.getQualifierLength(), searchKv.getQualifierArray(), searchKv.getQualifierOffset(), searchKv.getQualifierLength());
if (cmp == 0) {
colKeyValues[j++] = kv;
i++;
} else if (cmp > 0) {
colKeyValues[j++] = null;
} else {
// shouldn't happen - means unexpected KV in system table column row
i++;
}
}
if (colKeyValues[DATA_TYPE_INDEX] == null || colKeyValues[NULLABLE_INDEX] == null || colKeyValues[ORDINAL_POSITION_INDEX] == null) {
throw new IllegalStateException("Didn't find all required key values in '" + colName.getString() + "' column metadata row");
}
Cell columnSizeKv = colKeyValues[COLUMN_SIZE_INDEX];
Integer maxLength = columnSizeKv == null ? null : PInteger.INSTANCE.getCodec().decodeInt(columnSizeKv.getValueArray(), columnSizeKv.getValueOffset(), SortOrder.getDefault());
Cell decimalDigitKv = colKeyValues[DECIMAL_DIGITS_INDEX];
Integer scale = decimalDigitKv == null ? null : PInteger.INSTANCE.getCodec().decodeInt(decimalDigitKv.getValueArray(), decimalDigitKv.getValueOffset(), SortOrder.getDefault());
Cell ordinalPositionKv = colKeyValues[ORDINAL_POSITION_INDEX];
int position = PInteger.INSTANCE.getCodec().decodeInt(ordinalPositionKv.getValueArray(), ordinalPositionKv.getValueOffset(), SortOrder.getDefault()) + (isSalted ? 1 : 0);
Cell nullableKv = colKeyValues[NULLABLE_INDEX];
boolean isNullable = PInteger.INSTANCE.getCodec().decodeInt(nullableKv.getValueArray(), nullableKv.getValueOffset(), SortOrder.getDefault()) != ResultSetMetaData.columnNoNulls;
Cell dataTypeKv = colKeyValues[DATA_TYPE_INDEX];
PDataType dataType = PDataType.fromTypeId(PInteger.INSTANCE.getCodec().decodeInt(dataTypeKv.getValueArray(), dataTypeKv.getValueOffset(), SortOrder.getDefault()));
// For
if (maxLength == null && dataType == PBinary.INSTANCE)
dataType = PVarbinary.INSTANCE;
// backward
// compatibility.
Cell sortOrderKv = colKeyValues[SORT_ORDER_INDEX];
SortOrder sortOrder = sortOrderKv == null ? SortOrder.getDefault() : SortOrder.fromSystemValue(PInteger.INSTANCE.getCodec().decodeInt(sortOrderKv.getValueArray(), sortOrderKv.getValueOffset(), SortOrder.getDefault()));
Cell arraySizeKv = colKeyValues[ARRAY_SIZE_INDEX];
Integer arraySize = arraySizeKv == null ? null : PInteger.INSTANCE.getCodec().decodeInt(arraySizeKv.getValueArray(), arraySizeKv.getValueOffset(), SortOrder.getDefault());
Cell viewConstantKv = colKeyValues[VIEW_CONSTANT_INDEX];
byte[] viewConstant = viewConstantKv == null ? null : viewConstantKv.getValue();
Cell isViewReferencedKv = colKeyValues[IS_VIEW_REFERENCED_INDEX];
boolean isViewReferenced = isViewReferencedKv != null && Boolean.TRUE.equals(PBoolean.INSTANCE.toObject(isViewReferencedKv.getValueArray(), isViewReferencedKv.getValueOffset(), isViewReferencedKv.getValueLength()));
Cell columnDefKv = colKeyValues[COLUMN_DEF_INDEX];
String expressionStr = columnDefKv == null ? null : (String) PVarchar.INSTANCE.toObject(columnDefKv.getValueArray(), columnDefKv.getValueOffset(), columnDefKv.getValueLength());
Cell isRowTimestampKV = colKeyValues[IS_ROW_TIMESTAMP_INDEX];
boolean isRowTimestamp = isRowTimestampKV == null ? false : Boolean.TRUE.equals(PBoolean.INSTANCE.toObject(isRowTimestampKV.getValueArray(), isRowTimestampKV.getValueOffset(), isRowTimestampKV.getValueLength()));
boolean isPkColumn = famName == null || famName.getString() == null;
Cell columnQualifierKV = colKeyValues[COLUMN_QUALIFIER_INDEX];
// Older tables won't have column qualifier metadata present. To make things simpler, just set the
// column qualifier bytes by using the column name.
byte[] columnQualifierBytes = columnQualifierKV != null ? Arrays.copyOfRange(columnQualifierKV.getValueArray(), columnQualifierKV.getValueOffset(), columnQualifierKV.getValueOffset() + columnQualifierKV.getValueLength()) : (isPkColumn ? null : colName.getBytes());
PColumn column = new PColumnImpl(colName, famName, dataType, maxLength, scale, isNullable, position - 1, sortOrder, arraySize, viewConstant, isViewReferenced, expressionStr, isRowTimestamp, false, columnQualifierBytes);
columns.add(column);
}
use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.
the class WeekFunction method evaluate.
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
Expression expression = getChildExpression();
if (!expression.evaluate(tuple, ptr)) {
return false;
}
if (ptr.getLength() == 0) {
//means null
return true;
}
long dateTime = inputCodec.decodeLong(ptr, expression.getSortOrder());
DateTime dt = new DateTime(dateTime);
int week = dt.getWeekOfWeekyear();
PDataType returnType = getDataType();
byte[] byteValue = new byte[returnType.getByteSize()];
returnType.getCodec().encodeInt(week, byteValue, 0);
ptr.set(byteValue);
return true;
}
use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.
the class YearFunction method evaluate.
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
Expression expression = getChildExpression();
if (!expression.evaluate(tuple, ptr)) {
return false;
}
if (ptr.getLength() == 0) {
//means null
return true;
}
long dateTime = inputCodec.decodeLong(ptr, expression.getSortOrder());
DateTime dt = new DateTime(dateTime);
int year = dt.getYear();
PDataType returnType = getDataType();
byte[] byteValue = new byte[returnType.getByteSize()];
returnType.getCodec().encodeInt(year, byteValue, 0);
ptr.set(byteValue);
return true;
}
use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.
the class ToDateFunction method evaluate.
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
Expression expression = getExpression();
if (!expression.evaluate(tuple, ptr)) {
return false;
}
if (ptr.getLength() == 0) {
return true;
}
PDataType type = expression.getDataType();
String dateStr = (String) type.toObject(ptr, expression.getSortOrder());
long epochTime = dateParser.parseDateTime(dateStr);
PDataType returnType = getDataType();
byte[] byteValue = new byte[returnType.getByteSize()];
codec.encodeLong(epochTime, byteValue, 0);
ptr.set(byteValue);
return true;
}
Aggregations