use of org.apache.phoenix.expression.Expression in project phoenix by apache.
the class SequenceManager method determineNumToAllocate.
/**
* If caller specified used NEXT <n> VALUES FOR <seq> expression then we have set the numToAllocate.
* If numToAllocate is > 1 we treat this as a bulk reservation of a block of sequence slots.
*
* @throws a SQLException if we can't compile the expression
*/
private long determineNumToAllocate(TableName sequenceName, ParseNode numToAllocateNode) throws SQLException {
if (numToAllocateNode != null) {
final StatementContext context = new StatementContext(statement);
ExpressionCompiler expressionCompiler = new ExpressionCompiler(context);
Expression expression = numToAllocateNode.accept(expressionCompiler);
ImmutableBytesWritable ptr = context.getTempPtr();
expression.evaluate(null, ptr);
if (ptr.getLength() == 0 || !expression.getDataType().isCoercibleTo(PLong.INSTANCE)) {
throw SequenceUtil.getException(sequenceName.getSchemaName(), sequenceName.getTableName(), SQLExceptionCode.NUM_SEQ_TO_ALLOCATE_MUST_BE_CONSTANT);
}
// Parse <n> and make sure it is greater than 0. We don't support allocating 0 or negative values!
long numToAllocate = (long) PLong.INSTANCE.toObject(ptr, expression.getDataType());
if (numToAllocate < 1) {
throw SequenceUtil.getException(sequenceName.getSchemaName(), sequenceName.getTableName(), SQLExceptionCode.NUM_SEQ_TO_ALLOCATE_MUST_BE_CONSTANT);
}
return numToAllocate;
} else {
// Standard Sequence Allocation Behavior
return SequenceUtil.DEFAULT_NUM_SLOTS_TO_ALLOCATE;
}
}
use of org.apache.phoenix.expression.Expression 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.expression.Expression 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.expression.Expression 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;
}
use of org.apache.phoenix.expression.Expression in project phoenix by apache.
the class NonAggregateRegionScannerFactory method deserializeArrayPostionalExpressionInfoFromScan.
private Expression[] deserializeArrayPostionalExpressionInfoFromScan(Scan scan, RegionScanner s, Set<KeyValueColumnExpression> arrayKVRefs) {
byte[] specificArrayIdx = scan.getAttribute(BaseScannerRegionObserver.SPECIFIC_ARRAY_INDEX);
if (specificArrayIdx == null) {
return null;
}
KeyValueSchema.KeyValueSchemaBuilder builder = new KeyValueSchema.KeyValueSchemaBuilder(0);
ByteArrayInputStream stream = new ByteArrayInputStream(specificArrayIdx);
try {
DataInputStream input = new DataInputStream(stream);
int arrayKVRefSize = WritableUtils.readVInt(input);
for (int i = 0; i < arrayKVRefSize; i++) {
PTable.ImmutableStorageScheme scheme = EncodedColumnsUtil.getImmutableStorageScheme(scan);
KeyValueColumnExpression kvExp = scheme != PTable.ImmutableStorageScheme.ONE_CELL_PER_COLUMN ? new SingleCellColumnExpression() : new KeyValueColumnExpression();
kvExp.readFields(input);
arrayKVRefs.add(kvExp);
}
int arrayKVFuncSize = WritableUtils.readVInt(input);
Expression[] arrayFuncRefs = new Expression[arrayKVFuncSize];
for (int i = 0; i < arrayKVFuncSize; i++) {
ArrayIndexFunction arrayIdxFunc = new ArrayIndexFunction();
arrayIdxFunc.readFields(input);
arrayFuncRefs[i] = arrayIdxFunc;
builder.addField(arrayIdxFunc);
}
kvSchema = builder.build();
kvSchemaBitSet = ValueBitSet.newInstance(kvSchema);
return arrayFuncRefs;
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
stream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Aggregations