use of org.apache.phoenix.parse.BindParseNode in project phoenix by apache.
the class ExpressionCompiler method visitLeave.
@Override
public Expression visitLeave(NotParseNode node, List<Expression> children) throws SQLException {
ParseNode childNode = node.getChildren().get(0);
Expression child = children.get(0);
if (!PBoolean.INSTANCE.isCoercibleTo(child.getDataType())) {
throw TypeMismatchException.newException(PBoolean.INSTANCE, child.getDataType(), node.toString());
}
if (childNode instanceof BindParseNode) {
// TODO: valid/possibe?
context.getBindManager().addParamMetaData((BindParseNode) childNode, child);
}
return wrapGroupByExpression(NotExpression.create(child, context.getTempPtr()));
}
use of org.apache.phoenix.parse.BindParseNode in project phoenix by apache.
the class ExpressionCompiler method visitLeave.
@Override
public Expression visitLeave(StringConcatParseNode node, List<Expression> children) throws SQLException {
final StringConcatExpression expression = new StringConcatExpression(children);
for (int i = 0; i < children.size(); i++) {
ParseNode childNode = node.getChildren().get(i);
if (childNode instanceof BindParseNode) {
context.getBindManager().addParamMetaData((BindParseNode) childNode, expression);
}
PDataType type = children.get(i).getDataType();
if (type == PVarbinary.INSTANCE) {
throw new SQLExceptionInfo.Builder(SQLExceptionCode.TYPE_NOT_SUPPORTED_FOR_OPERATOR).setMessage("Concatenation does not support " + type + " in expression" + node).build().buildException();
}
}
ImmutableBytesWritable ptr = context.getTempPtr();
if (ExpressionUtil.isConstant(expression)) {
return ExpressionUtil.getConstantExpression(expression, ptr);
}
return wrapGroupByExpression(expression);
}
use of org.apache.phoenix.parse.BindParseNode in project phoenix by apache.
the class ExpressionCompiler method visitLeave.
private Expression visitLeave(ArithmeticParseNode node, List<Expression> children, ArithmeticExpressionBinder binder, ArithmeticExpressionFactory factory) throws SQLException {
boolean isNull = false;
for (Expression child : children) {
boolean isChildLiteral = (child instanceof LiteralExpression);
isNull |= isChildLiteral && ((LiteralExpression) child).getValue() == null;
}
Expression expression = factory.create(node, children);
for (int i = 0; i < node.getChildren().size(); i++) {
ParseNode childNode = node.getChildren().get(i);
if (childNode instanceof BindParseNode) {
context.getBindManager().addParamMetaData((BindParseNode) childNode, binder == null ? expression : binder.getBindMetaData(i, children, expression));
}
}
ImmutableBytesWritable ptr = context.getTempPtr();
// If all children are literals, just evaluate now
if (ExpressionUtil.isConstant(expression)) {
return ExpressionUtil.getConstantExpression(expression, ptr);
} else if (isNull) {
return LiteralExpression.newConstant(null, expression.getDataType(), expression.getDeterminism());
}
// Otherwise create and return the expression
return wrapGroupByExpression(expression);
}
use of org.apache.phoenix.parse.BindParseNode in project phoenix by apache.
the class ExpressionCompiler method visitLeave.
@Override
public Expression visitLeave(ArrayConstructorNode node, List<Expression> children) throws SQLException {
boolean isChildTypeUnknown = false;
Expression arrayElemChild = null;
PDataType arrayElemDataType = children.get(0).getDataType();
for (int i = 0; i < children.size(); i++) {
Expression child = children.get(i);
PDataType childType = child.getDataType();
if (childType == null) {
isChildTypeUnknown = true;
} else if (arrayElemDataType == null) {
arrayElemDataType = childType;
isChildTypeUnknown = true;
arrayElemChild = child;
} else if (arrayElemDataType == childType || childType.isCoercibleTo(arrayElemDataType)) {
continue;
} else if (arrayElemDataType.isCoercibleTo(childType)) {
arrayElemChild = child;
arrayElemDataType = childType;
} else {
throw new SQLExceptionInfo.Builder(SQLExceptionCode.TYPE_MISMATCH).setMessage("Case expressions must have common type: " + arrayElemDataType + " cannot be coerced to " + childType).build().buildException();
}
}
// make the return type be the most general number type of DECIMAL.
if (isChildTypeUnknown && arrayElemDataType != null && arrayElemDataType.isCoercibleTo(PDecimal.INSTANCE)) {
arrayElemDataType = PDecimal.INSTANCE;
}
final PDataType theArrayElemDataType = arrayElemDataType;
for (int i = 0; i < node.getChildren().size(); i++) {
ParseNode childNode = node.getChildren().get(i);
if (childNode instanceof BindParseNode) {
context.getBindManager().addParamMetaData((BindParseNode) childNode, arrayElemDataType == arrayElemChild.getDataType() ? arrayElemChild : new DelegateDatum(arrayElemChild) {
@Override
public PDataType getDataType() {
return theArrayElemDataType;
}
});
}
}
ImmutableBytesWritable ptr = context.getTempPtr();
// the value object array type should match the java known type
Object[] elements = (Object[]) java.lang.reflect.Array.newInstance(theArrayElemDataType.getJavaClass(), children.size());
boolean rowKeyOrderOptimizable = context.getCurrentTable().getTable().rowKeyOrderOptimizable();
ArrayConstructorExpression arrayExpression = new ArrayConstructorExpression(children, arrayElemDataType, rowKeyOrderOptimizable);
if (ExpressionUtil.isConstant(arrayExpression)) {
for (int i = 0; i < children.size(); i++) {
Expression child = children.get(i);
child.evaluate(null, ptr);
Object value = null;
if (child.getDataType() == null) {
value = arrayElemDataType.toObject(ptr, theArrayElemDataType, child.getSortOrder());
} else {
value = arrayElemDataType.toObject(ptr, child.getDataType(), child.getSortOrder());
}
elements[i] = LiteralExpression.newConstant(value, theArrayElemDataType, child.getDeterminism()).getValue();
}
Object value = PArrayDataType.instantiatePhoenixArray(arrayElemDataType, elements);
return LiteralExpression.newConstant(value, PDataType.fromTypeId(arrayElemDataType.getSqlType() + PDataType.ARRAY_TYPE_BASE), null, null, arrayExpression.getSortOrder(), Determinism.ALWAYS, rowKeyOrderOptimizable);
}
return wrapGroupByExpression(arrayExpression);
}
use of org.apache.phoenix.parse.BindParseNode in project phoenix by apache.
the class CreateSequenceCompiler method compile.
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(), SQLExceptionCode.CACHE_MUST_BE_NON_NEGATIVE_CONSTANT);
}
}
final long cacheSize = Math.max(1L, cacheSizeValue);
final MetaDataClient client = new MetaDataClient(connection);
return new BaseMutationPlan(context, operation) {
@Override
public MutationState execute() throws SQLException {
return client.createSequence(sequence, startsWith, incrementBy, cacheSize, minValue, maxValue);
}
@Override
public ExplainPlan getExplainPlan() throws SQLException {
return new ExplainPlan(Collections.singletonList("CREATE SEQUENCE"));
}
};
}
Aggregations