}
private static final PDatum LONG_DATUM = new LongDatum();
private static final PDatum INTEGER_DATUM = new IntegerDatum();
public MutationPlan compile(final CreateSequenceStatement sequence) throws SQLException {
ParseNode startsWithNode = sequence.getStartWith();
ParseNode incrementByNode = sequence.getIncrementBy();
if (!startsWithNode.isStateless()) {
throw new SQLExceptionInfo.Builder(SQLExceptionCode.STARTS_WITH_MUST_BE_CONSTANT)
.setSchemaName(sequence.getSequenceName().getSchemaName())
.setTableName(sequence.getSequenceName().getTableName()).build().buildException();
}
if (!incrementByNode.isStateless()) {
throw new SQLExceptionInfo.Builder(SQLExceptionCode.INCREMENT_BY_MUST_BE_CONSTANT)
.setSchemaName(sequence.getSequenceName().getSchemaName())
.setTableName(sequence.getSequenceName().getTableName()).build().buildException();
}
ParseNode cacheNode = sequence.getCacheSize();
if (cacheNode != null && !cacheNode.isStateless()) {
throw new SQLExceptionInfo.Builder(SQLExceptionCode.CACHE_MUST_BE_NON_NEGATIVE_CONSTANT)
.setSchemaName(sequence.getSequenceName().getSchemaName())
.setTableName(sequence.getSequenceName().getTableName()).build().buildException();
}
final PhoenixConnection connection = statement.getConnection();
final ColumnResolver resolver = FromCompiler.EMPTY_TABLE_RESOLVER;
final StatementContext context = new StatementContext(statement, resolver, statement.getParameters(), new Scan());
if (startsWithNode instanceof BindParseNode) {
context.getBindManager().addParamMetaData((BindParseNode)startsWithNode, LONG_DATUM);
}
if (incrementByNode instanceof BindParseNode) {
context.getBindManager().addParamMetaData((BindParseNode)incrementByNode, LONG_DATUM);
}
if (cacheNode instanceof BindParseNode) {
context.getBindManager().addParamMetaData((BindParseNode)cacheNode, INTEGER_DATUM);
}
ExpressionCompiler expressionCompiler = new ExpressionCompiler(context);
Expression startsWithExpr = startsWithNode.accept(expressionCompiler);
ImmutableBytesWritable ptr = context.getTempPtr();
startsWithExpr.evaluate(null, ptr);
if (ptr.getLength() == 0 || !startsWithExpr.getDataType().isCoercibleTo(PDataType.LONG)) {
throw new SQLExceptionInfo.Builder(SQLExceptionCode.STARTS_WITH_MUST_BE_CONSTANT)
.setSchemaName(sequence.getSequenceName().getSchemaName())
.setTableName(sequence.getSequenceName().getTableName()).build().buildException();
}
final long startsWith = (Long)PDataType.LONG.toObject(ptr, startsWithExpr.getDataType());
Expression incrementByExpr = incrementByNode.accept(expressionCompiler);
incrementByExpr.evaluate(null, ptr);
if (ptr.getLength() == 0 || !incrementByExpr.getDataType().isCoercibleTo(PDataType.LONG)) {
throw new SQLExceptionInfo.Builder(SQLExceptionCode.INCREMENT_BY_MUST_BE_CONSTANT)
.setSchemaName(sequence.getSequenceName().getSchemaName())
.setTableName(sequence.getSequenceName().getTableName()).build().buildException();
}
final long incrementBy = (Long)PDataType.LONG.toObject(ptr, incrementByExpr.getDataType());
int cacheSizeValue = connection.getQueryServices().getProps().getInt(QueryServices.SEQUENCE_CACHE_SIZE_ATTRIB,QueryServicesOptions.DEFAULT_SEQUENCE_CACHE_SIZE);
if (cacheNode != null) {
Expression cacheSizeExpr = cacheNode.accept(expressionCompiler);
cacheSizeExpr.evaluate(null, ptr);
if (ptr.getLength() != 0 && (!cacheSizeExpr.getDataType().isCoercibleTo(PDataType.INTEGER) || (cacheSizeValue = (Integer)PDataType.INTEGER.toObject(ptr)) < 0)) {
throw new SQLExceptionInfo.Builder(SQLExceptionCode.CACHE_MUST_BE_NON_NEGATIVE_CONSTANT)
.setSchemaName(sequence.getSequenceName().getSchemaName())
.setTableName(sequence.getSequenceName().getTableName()).build().buildException();