Search in sources :

Example 51 with PDataType

use of org.apache.phoenix.schema.types.PDataType 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);
}
Also used : ArrayConstructorExpression(org.apache.phoenix.expression.ArrayConstructorExpression) ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) PDataType(org.apache.phoenix.schema.types.PDataType) DecimalAddExpression(org.apache.phoenix.expression.DecimalAddExpression) TimestampSubtractExpression(org.apache.phoenix.expression.TimestampSubtractExpression) ArrayConstructorExpression(org.apache.phoenix.expression.ArrayConstructorExpression) Expression(org.apache.phoenix.expression.Expression) LikeExpression(org.apache.phoenix.expression.LikeExpression) ByteBasedLikeExpression(org.apache.phoenix.expression.ByteBasedLikeExpression) DoubleSubtractExpression(org.apache.phoenix.expression.DoubleSubtractExpression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) InListExpression(org.apache.phoenix.expression.InListExpression) DateSubtractExpression(org.apache.phoenix.expression.DateSubtractExpression) ArrayElemRefExpression(org.apache.phoenix.expression.function.ArrayElemRefExpression) CaseExpression(org.apache.phoenix.expression.CaseExpression) DoubleDivideExpression(org.apache.phoenix.expression.DoubleDivideExpression) NotExpression(org.apache.phoenix.expression.NotExpression) DoubleAddExpression(org.apache.phoenix.expression.DoubleAddExpression) DecimalDivideExpression(org.apache.phoenix.expression.DecimalDivideExpression) RowKeyColumnExpression(org.apache.phoenix.expression.RowKeyColumnExpression) RowValueConstructorExpression(org.apache.phoenix.expression.RowValueConstructorExpression) RoundTimestampExpression(org.apache.phoenix.expression.function.RoundTimestampExpression) StringConcatExpression(org.apache.phoenix.expression.StringConcatExpression) ComparisonExpression(org.apache.phoenix.expression.ComparisonExpression) TimestampAddExpression(org.apache.phoenix.expression.TimestampAddExpression) CoerceExpression(org.apache.phoenix.expression.CoerceExpression) StringBasedLikeExpression(org.apache.phoenix.expression.StringBasedLikeExpression) ArrayAnyComparisonExpression(org.apache.phoenix.expression.function.ArrayAnyComparisonExpression) DecimalSubtractExpression(org.apache.phoenix.expression.DecimalSubtractExpression) ModulusExpression(org.apache.phoenix.expression.ModulusExpression) DoubleMultiplyExpression(org.apache.phoenix.expression.DoubleMultiplyExpression) DecimalMultiplyExpression(org.apache.phoenix.expression.DecimalMultiplyExpression) DateAddExpression(org.apache.phoenix.expression.DateAddExpression) RoundDecimalExpression(org.apache.phoenix.expression.function.RoundDecimalExpression) LongAddExpression(org.apache.phoenix.expression.LongAddExpression) LongSubtractExpression(org.apache.phoenix.expression.LongSubtractExpression) IsNullExpression(org.apache.phoenix.expression.IsNullExpression) AndExpression(org.apache.phoenix.expression.AndExpression) LongMultiplyExpression(org.apache.phoenix.expression.LongMultiplyExpression) ArrayAllComparisonExpression(org.apache.phoenix.expression.function.ArrayAllComparisonExpression) OrExpression(org.apache.phoenix.expression.OrExpression) LongDivideExpression(org.apache.phoenix.expression.LongDivideExpression) DelegateDatum(org.apache.phoenix.schema.DelegateDatum) BindParseNode(org.apache.phoenix.parse.BindParseNode) ModulusParseNode(org.apache.phoenix.parse.ModulusParseNode) LikeParseNode(org.apache.phoenix.parse.LikeParseNode) UDFParseNode(org.apache.phoenix.parse.UDFParseNode) ComparisonParseNode(org.apache.phoenix.parse.ComparisonParseNode) SequenceValueParseNode(org.apache.phoenix.parse.SequenceValueParseNode) InListParseNode(org.apache.phoenix.parse.InListParseNode) AndParseNode(org.apache.phoenix.parse.AndParseNode) ExistsParseNode(org.apache.phoenix.parse.ExistsParseNode) SubtractParseNode(org.apache.phoenix.parse.SubtractParseNode) NotParseNode(org.apache.phoenix.parse.NotParseNode) DivideParseNode(org.apache.phoenix.parse.DivideParseNode) StringConcatParseNode(org.apache.phoenix.parse.StringConcatParseNode) OrParseNode(org.apache.phoenix.parse.OrParseNode) ParseNode(org.apache.phoenix.parse.ParseNode) LiteralParseNode(org.apache.phoenix.parse.LiteralParseNode) FunctionParseNode(org.apache.phoenix.parse.FunctionParseNode) RowValueConstructorParseNode(org.apache.phoenix.parse.RowValueConstructorParseNode) MultiplyParseNode(org.apache.phoenix.parse.MultiplyParseNode) ColumnParseNode(org.apache.phoenix.parse.ColumnParseNode) CaseParseNode(org.apache.phoenix.parse.CaseParseNode) CastParseNode(org.apache.phoenix.parse.CastParseNode) AddParseNode(org.apache.phoenix.parse.AddParseNode) SubqueryParseNode(org.apache.phoenix.parse.SubqueryParseNode) ArithmeticParseNode(org.apache.phoenix.parse.ArithmeticParseNode) IsNullParseNode(org.apache.phoenix.parse.IsNullParseNode) BindParseNode(org.apache.phoenix.parse.BindParseNode)

Example 52 with PDataType

use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.

the class TimestampSubtractExpression method evaluate.

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    BigDecimal finalResult = BigDecimal.ZERO;
    for (int i = 0; i < children.size(); i++) {
        if (!children.get(i).evaluate(tuple, ptr)) {
            return false;
        }
        if (ptr.getLength() == 0) {
            return true;
        }
        BigDecimal value;
        PDataType type = children.get(i).getDataType();
        SortOrder sortOrder = children.get(i).getSortOrder();
        if (type == PTimestamp.INSTANCE || type == PUnsignedTimestamp.INSTANCE) {
            value = (BigDecimal) (PDecimal.INSTANCE.toObject(ptr, type, sortOrder));
        } else if (type.isCoercibleTo(PDecimal.INSTANCE)) {
            value = (((BigDecimal) PDecimal.INSTANCE.toObject(ptr, type, sortOrder)).multiply(BD_MILLIS_IN_DAY)).setScale(6, RoundingMode.HALF_UP);
        } else if (type.isCoercibleTo(PDouble.INSTANCE)) {
            value = ((BigDecimal.valueOf(type.getCodec().decodeDouble(ptr, sortOrder))).multiply(BD_MILLIS_IN_DAY)).setScale(6, RoundingMode.HALF_UP);
        } else {
            value = BigDecimal.valueOf(type.getCodec().decodeLong(ptr, sortOrder));
        }
        if (i == 0) {
            finalResult = value;
        } else {
            finalResult = finalResult.subtract(value);
        }
    }
    Timestamp ts = DateUtil.getTimestamp(finalResult);
    byte[] resultPtr = new byte[getDataType().getByteSize()];
    PTimestamp.INSTANCE.toBytes(ts, resultPtr, 0);
    ptr.set(resultPtr);
    return true;
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) SortOrder(org.apache.phoenix.schema.SortOrder) Timestamp(java.sql.Timestamp) PUnsignedTimestamp(org.apache.phoenix.schema.types.PUnsignedTimestamp) PTimestamp(org.apache.phoenix.schema.types.PTimestamp) BigDecimal(java.math.BigDecimal)

Example 53 with PDataType

use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.

the class InListExpression method toString.

@Override
public String toString() {
    int maxToStringLen = 200;
    Expression firstChild = children.get(0);
    PDataType type = firstChild.getDataType();
    StringBuilder buf = new StringBuilder(firstChild + " IN (");
    for (ImmutableBytesPtr value : values) {
        if (firstChild.getSortOrder() != null) {
            type.coerceBytes(value, type, firstChild.getSortOrder(), SortOrder.getDefault());
        }
        buf.append(type.toStringLiteral(value, null));
        buf.append(',');
        if (buf.length() >= maxToStringLen) {
            buf.append("... ");
            break;
        }
    }
    buf.setCharAt(buf.length() - 1, ')');
    return buf.toString();
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)

Example 54 with PDataType

use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.

the class ArrayModifierFunction method evaluate.

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    Expression arrayExpr = null;
    PDataType baseDataType = null;
    Expression otherExpr = null;
    PDataType otherExpressionType = null;
    if (getLHSExpr().getDataType().isArrayType()) {
        arrayExpr = getLHSExpr();
        baseDataType = getLHSBaseType();
        otherExpr = getRHSExpr();
        otherExpressionType = getRHSBaseType();
    } else {
        arrayExpr = getRHSExpr();
        baseDataType = getRHSBaseType();
        otherExpr = getLHSExpr();
        otherExpressionType = getLHSBaseType();
    }
    if (!arrayExpr.evaluate(tuple, ptr)) {
        return false;
    } else if (ptr.getLength() == 0) {
        return true;
    }
    int arrayLength = PArrayDataType.getArrayLength(ptr, baseDataType, arrayExpr.getMaxLength());
    int length = ptr.getLength();
    int offset = ptr.getOffset();
    byte[] arrayBytes = ptr.get();
    otherExpr.evaluate(tuple, ptr);
    checkSizeCompatibility(ptr, otherExpr.getSortOrder(), arrayExpr, baseDataType, otherExpr, otherExpressionType);
    coerceBytes(ptr, arrayExpr, baseDataType, otherExpr, otherExpressionType);
    return modifierFunction(ptr, length, offset, arrayBytes, baseDataType, arrayLength, getMaxLength(), arrayExpr);
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) Expression(org.apache.phoenix.expression.Expression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression)

Example 55 with PDataType

use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.

the class LiteralExpression method newConstant.

// TODO: cache?
public static LiteralExpression newConstant(Object value, Determinism determinism) {
    if (value instanceof Boolean) {
        return getBooleanLiteralExpression((Boolean) value, determinism);
    } else if (value == null) {
        return getNullLiteralExpression(determinism);
    }
    PDataType type = PDataType.fromLiteral(value);
    byte[] b = type.toBytes(value);
    if (type.isNull(b)) {
        return getTypedNullLiteralExpression(type, determinism);
    }
    if (type == PVarchar.INSTANCE) {
        String s = (String) value;
        if (s.length() == b.length) {
            // single byte characters only
            type = PChar.INSTANCE;
        }
    }
    return new LiteralExpression(value, type, b, determinism);
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) PBoolean(org.apache.phoenix.schema.types.PBoolean)

Aggregations

PDataType (org.apache.phoenix.schema.types.PDataType)152 Expression (org.apache.phoenix.expression.Expression)54 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)31 SortOrder (org.apache.phoenix.schema.SortOrder)29 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)21 CoerceExpression (org.apache.phoenix.expression.CoerceExpression)18 Test (org.junit.Test)15 PDatum (org.apache.phoenix.schema.PDatum)12 BigDecimal (java.math.BigDecimal)11 SQLException (java.sql.SQLException)11 ArrayList (java.util.ArrayList)11 List (java.util.List)10 KeyValueColumnExpression (org.apache.phoenix.expression.KeyValueColumnExpression)10 RowKeyColumnExpression (org.apache.phoenix.expression.RowKeyColumnExpression)10 SingleCellConstructorExpression (org.apache.phoenix.expression.SingleCellConstructorExpression)10 IOException (java.io.IOException)9 PreparedStatement (java.sql.PreparedStatement)7 Pair (org.apache.hadoop.hbase.util.Pair)7 Date (java.sql.Date)6 AndExpression (org.apache.phoenix.expression.AndExpression)6