use of org.apache.phoenix.expression.Expression in project phoenix by apache.
the class MergeSortTopNResultIterator method compare.
@Override
protected int compare(Tuple t1, Tuple t2) {
for (int i = 0; i < orderByColumns.size(); i++) {
OrderByExpression order = orderByColumns.get(i);
Expression orderExpr = order.getExpression();
boolean isNull1 = !orderExpr.evaluate(t1, ptr1) || ptr1.getLength() == 0;
boolean isNull2 = !orderExpr.evaluate(t2, ptr2) || ptr2.getLength() == 0;
if (isNull1 && isNull2) {
continue;
} else if (isNull1) {
return order.isNullsLast() ? 1 : -1;
} else if (isNull2) {
return order.isNullsLast() ? -1 : 1;
}
int cmp = ptr1.compareTo(ptr2);
if (cmp == 0) {
continue;
}
return order.isAscending() ? cmp : -cmp;
}
return 0;
}
use of org.apache.phoenix.expression.Expression in project phoenix by apache.
the class ColumnDef method validateDefault.
public boolean validateDefault(StatementContext context, PrimaryKeyConstraint pkConstraint) throws SQLException {
String defaultStr = this.getExpression();
if (defaultStr == null) {
return true;
}
ExpressionCompiler compiler = new ExpressionCompiler(context);
ParseNode defaultParseNode = new SQLParser(this.getExpression()).parseExpression();
Expression defaultExpression = defaultParseNode.accept(compiler);
if (!defaultParseNode.isStateless() || defaultExpression.getDeterminism() != Determinism.ALWAYS) {
throw new SQLExceptionInfo.Builder(SQLExceptionCode.CANNOT_CREATE_DEFAULT).setColumnName(this.getColumnDefName().getColumnName()).build().buildException();
}
if (this.isRowTimestamp() || (pkConstraint != null && pkConstraint.isColumnRowTimestamp(this.getColumnDefName()))) {
throw new SQLExceptionInfo.Builder(SQLExceptionCode.CANNOT_CREATE_DEFAULT_ROWTIMESTAMP).setColumnName(this.getColumnDefName().getColumnName()).build().buildException();
}
ImmutableBytesWritable ptr = new ImmutableBytesWritable();
// Evaluate the expression to confirm it's validity
LiteralExpression defaultValue = ExpressionUtil.getConstantExpression(defaultExpression, ptr);
// A DEFAULT that evaluates to null should be ignored as it adds nothing
if (defaultValue.getValue() == null) {
return false;
}
PDataType sourceType = defaultExpression.getDataType();
PDataType targetType = this.getDataType();
// Ensure that coercion works (will throw if not)
context.getTempPtr().set(ptr.get(), ptr.getOffset(), ptr.getLength());
try {
targetType.coerceBytes(context.getTempPtr(), defaultValue.getValue(), sourceType, defaultValue.getMaxLength(), defaultValue.getScale(), defaultValue.getSortOrder(), this.getMaxLength(), this.getScale(), this.getSortOrder());
} catch (ConstraintViolationException e) {
if (e.getCause() instanceof SQLException) {
SQLException sqlE = (SQLException) e.getCause();
throw new DelegateSQLException(sqlE, ". DEFAULT " + SQLExceptionInfo.COLUMN_NAME + "=" + this.getColumnDefName().getColumnName());
}
throw e;
}
if (!targetType.isSizeCompatible(ptr, defaultValue.getValue(), sourceType, sortOrder, defaultValue.getMaxLength(), defaultValue.getScale(), this.getMaxLength(), this.getScale())) {
throw new SQLExceptionInfo.Builder(SQLExceptionCode.DATA_EXCEEDS_MAX_CAPACITY).setColumnName(this.getColumnDefName().getColumnName()).setMessage("DEFAULT " + this.getExpression()).build().buildException();
}
return true;
}
use of org.apache.phoenix.expression.Expression in project phoenix by apache.
the class FloorParseNode method getFloorExpression.
public static Expression getFloorExpression(List<Expression> children) throws SQLException {
final Expression firstChild = children.get(0);
final PDataType firstChildDataType = firstChild.getDataType();
//Which is exactly what FloorDateExpression does too.
if (firstChildDataType.isCoercibleTo(PTimestamp.INSTANCE)) {
return FloorDateExpression.create(children);
} else if (firstChildDataType.isCoercibleTo(PDecimal.INSTANCE)) {
return FloorDecimalExpression.create(children);
} else {
throw TypeMismatchException.newException(firstChildDataType, "1");
}
}
use of org.apache.phoenix.expression.Expression in project phoenix by apache.
the class FunctionParseNode method validate.
public List<Expression> validate(List<Expression> children, StatementContext context) throws SQLException {
BuiltInFunctionInfo info = this.getInfo();
BuiltInFunctionArgInfo[] args = info.getArgs();
if (args.length < children.size() || info.getRequiredArgCount() > children.size()) {
throw new FunctionNotFoundException(this.name);
}
if (args.length > children.size()) {
List<Expression> moreChildren = new ArrayList<Expression>(children);
for (int i = children.size(); i < info.getArgs().length; i++) {
moreChildren.add(LiteralExpression.newConstant(null, args[i].allowedTypes.length == 0 ? null : PDataTypeFactory.getInstance().instanceFromClass(args[i].allowedTypes[0]), Determinism.ALWAYS));
}
children = moreChildren;
}
List<ParseNode> nodeChildren = this.getChildren();
for (int i = 0; i < children.size(); i++) {
BindParseNode bindNode = null;
Class<? extends PDataType>[] allowedTypes = args[i].getAllowedTypes();
// values.
if (i < nodeChildren.size() && nodeChildren.get(i) instanceof BindParseNode) {
bindNode = (BindParseNode) nodeChildren.get(i);
}
// If the child type is null, then the expression is unbound.
// Skip any validation, since we either 1) have a default value
// which has already been validated, 2) attempting to get the
// parameter metadata, or 3) have an unbound parameter which
// will be detected futher downstream.
Expression child = children.get(i);
if (child.getDataType() == null || /* null used explicitly in query */
i >= nodeChildren.size()) /* argument left off */
{
// Replace the unbound expression with the default value expression if specified
if (args[i].getDefaultValue() != null) {
Expression defaultValue = args[i].getDefaultValue();
children.set(i, defaultValue);
// Set the parameter metadata if this is a bind parameter
if (bindNode != null) {
context.getBindManager().addParamMetaData(bindNode, defaultValue);
}
} else if (bindNode != null) {
// based on the function argument annonation set the parameter meta data.
if (child.getDataType() == null) {
if (allowedTypes.length > 0) {
context.getBindManager().addParamMetaData(bindNode, LiteralExpression.newConstant(null, PDataTypeFactory.getInstance().instanceFromClass(allowedTypes[0]), Determinism.ALWAYS));
}
} else {
// Use expression as is, since we already have the data type set
context.getBindManager().addParamMetaData(bindNode, child);
}
}
} else {
validateFunctionArguement(info, i, child);
}
}
return children;
}
use of org.apache.phoenix.expression.Expression in project phoenix by apache.
the class ExpressionUtil method isPkPositionChanging.
public static boolean isPkPositionChanging(TableRef tableRef, List<Expression> projectedExpressions) throws SQLException {
for (int i = 0; i < tableRef.getTable().getPKColumns().size(); i++) {
PColumn column = tableRef.getTable().getPKColumns().get(i);
Expression source = projectedExpressions.get(i);
if (source == null || !source.equals(new ColumnRef(tableRef, column.getPosition()).newColumnExpression())) {
return true;
}
}
return false;
}
Aggregations