Search in sources :

Example 36 with Expression

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;
}
Also used : Expression(org.apache.phoenix.expression.Expression) OrderByExpression(org.apache.phoenix.expression.OrderByExpression) OrderByExpression(org.apache.phoenix.expression.OrderByExpression)

Example 37 with Expression

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;
}
Also used : ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) SQLException(java.sql.SQLException) DelegateSQLException(org.apache.phoenix.schema.DelegateSQLException) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) DelegateSQLException(org.apache.phoenix.schema.DelegateSQLException) PDataType(org.apache.phoenix.schema.types.PDataType) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) Expression(org.apache.phoenix.expression.Expression) ConstraintViolationException(org.apache.phoenix.schema.ConstraintViolationException) ExpressionCompiler(org.apache.phoenix.compile.ExpressionCompiler) SQLExceptionInfo(org.apache.phoenix.exception.SQLExceptionInfo)

Example 38 with Expression

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");
    }
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) Expression(org.apache.phoenix.expression.Expression) FloorDateExpression(org.apache.phoenix.expression.function.FloorDateExpression) FloorDecimalExpression(org.apache.phoenix.expression.function.FloorDecimalExpression)

Example 39 with Expression

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;
}
Also used : ArrayList(java.util.ArrayList) FunctionNotFoundException(org.apache.phoenix.schema.FunctionNotFoundException) PDataType(org.apache.phoenix.schema.types.PDataType) Expression(org.apache.phoenix.expression.Expression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) UDFExpression(org.apache.phoenix.expression.function.UDFExpression) FunctionExpression(org.apache.phoenix.expression.function.FunctionExpression)

Example 40 with Expression

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;
}
Also used : PColumn(org.apache.phoenix.schema.PColumn) Expression(org.apache.phoenix.expression.Expression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) ColumnRef(org.apache.phoenix.schema.ColumnRef)

Aggregations

Expression (org.apache.phoenix.expression.Expression)182 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)101 PDataType (org.apache.phoenix.schema.types.PDataType)54 RowKeyColumnExpression (org.apache.phoenix.expression.RowKeyColumnExpression)39 CoerceExpression (org.apache.phoenix.expression.CoerceExpression)37 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)33 KeyValueColumnExpression (org.apache.phoenix.expression.KeyValueColumnExpression)30 PTable (org.apache.phoenix.schema.PTable)25 ParseNode (org.apache.phoenix.parse.ParseNode)23 RowValueConstructorExpression (org.apache.phoenix.expression.RowValueConstructorExpression)22 Test (org.junit.Test)22 ComparisonExpression (org.apache.phoenix.expression.ComparisonExpression)21 AndExpression (org.apache.phoenix.expression.AndExpression)20 SingleCellColumnExpression (org.apache.phoenix.expression.SingleCellColumnExpression)20 PColumn (org.apache.phoenix.schema.PColumn)20 ArrayList (java.util.ArrayList)19 InListExpression (org.apache.phoenix.expression.InListExpression)17 IsNullExpression (org.apache.phoenix.expression.IsNullExpression)16 OrExpression (org.apache.phoenix.expression.OrExpression)16 LikeExpression (org.apache.phoenix.expression.LikeExpression)15