}
return (Long) PDataType.LONG.toObject(ptr, expression.getDataType());
}
public MutationPlan compile(final CreateSequenceStatement sequence) throws SQLException {
ParseNode startsWithNode = sequence.getStartWith();
ParseNode incrementByNode = sequence.getIncrementBy();
ParseNode maxValueNode = sequence.getMaxValue();
ParseNode minValueNode = sequence.getMinValue();
ParseNode cacheNode = sequence.getCacheSize();
// validate parse nodes
if (startsWithNode!=null) {
validateNodeIsStateless(sequence, startsWithNode,
SQLExceptionCode.START_WITH_MUST_BE_CONSTANT);
}
validateNodeIsStateless(sequence, incrementByNode,
SQLExceptionCode.INCREMENT_BY_MUST_BE_CONSTANT);
validateNodeIsStateless(sequence, maxValueNode,
SQLExceptionCode.MAXVALUE_MUST_BE_CONSTANT);
validateNodeIsStateless(sequence, minValueNode,
SQLExceptionCode.MINVALUE_MUST_BE_CONSTANT);
if (cacheNode != null) {
validateNodeIsStateless(sequence, cacheNode,
SQLExceptionCode.CACHE_MUST_BE_NON_NEGATIVE_CONSTANT);
}
final PhoenixConnection connection = statement.getConnection();
final StatementContext context = new StatementContext(statement);
// add param meta data if required
if (startsWithNode instanceof BindParseNode) {
context.getBindManager().addParamMetaData((BindParseNode) startsWithNode, LONG_DATUM);
}
if (incrementByNode instanceof BindParseNode) {
context.getBindManager().addParamMetaData((BindParseNode) incrementByNode, LONG_DATUM);
}
if (maxValueNode instanceof BindParseNode) {
context.getBindManager().addParamMetaData((BindParseNode) maxValueNode, LONG_DATUM);
}
if (minValueNode instanceof BindParseNode) {
context.getBindManager().addParamMetaData((BindParseNode) minValueNode, LONG_DATUM);
}
if (cacheNode instanceof BindParseNode) {
context.getBindManager().addParamMetaData((BindParseNode) cacheNode, INTEGER_DATUM);
}
ExpressionCompiler expressionCompiler = new ExpressionCompiler(context);
final long incrementBy =
evalExpression(sequence, context, incrementByNode.accept(expressionCompiler),
SQLExceptionCode.INCREMENT_BY_MUST_BE_CONSTANT);
if (incrementBy == 0) {
throw SequenceUtil.getException(sequence.getSequenceName().getSchemaName(), sequence
.getSequenceName().getTableName(),
SQLExceptionCode.INCREMENT_BY_MUST_NOT_BE_ZERO);
}
final long maxValue =
evalExpression(sequence, context, maxValueNode.accept(expressionCompiler),
SQLExceptionCode.MAXVALUE_MUST_BE_CONSTANT);
final long minValue =
evalExpression(sequence, context, minValueNode.accept(expressionCompiler),
SQLExceptionCode.MINVALUE_MUST_BE_CONSTANT);
if (minValue>maxValue) {
TableName sequenceName = sequence.getSequenceName();
throw SequenceUtil.getException(sequenceName.getSchemaName(),
sequenceName.getTableName(),
SQLExceptionCode.MINVALUE_MUST_BE_LESS_THAN_OR_EQUAL_TO_MAXVALUE);
}
long startsWithValue;
if (startsWithNode == null) {
startsWithValue = incrementBy > 0 ? minValue : maxValue;
} else {
startsWithValue =
evalExpression(sequence, context, startsWithNode.accept(expressionCompiler),
SQLExceptionCode.START_WITH_MUST_BE_CONSTANT);
if (startsWithValue < minValue || startsWithValue > maxValue) {
TableName sequenceName = sequence.getSequenceName();
throw SequenceUtil.getException(sequenceName.getSchemaName(),
sequenceName.getTableName(),
SQLExceptionCode.STARTS_WITH_MUST_BE_BETWEEN_MIN_MAX_VALUE);
}
}
final long startsWith = startsWithValue;
long cacheSizeValue;
if (cacheNode == null) {
cacheSizeValue =
connection
.getQueryServices()
.getProps()
.getLong(QueryServices.SEQUENCE_CACHE_SIZE_ATTRIB,
QueryServicesOptions.DEFAULT_SEQUENCE_CACHE_SIZE);
}
else {
cacheSizeValue =
evalExpression(sequence, context, cacheNode.accept(expressionCompiler),
SQLExceptionCode.CACHE_MUST_BE_NON_NEGATIVE_CONSTANT);
if (cacheSizeValue < 0) {
TableName sequenceName = sequence.getSequenceName();
throw SequenceUtil.getException(sequenceName.getSchemaName(),
sequenceName.getTableName(),