Search in sources :

Example 6 with QueryDataTypeFamily

use of com.hazelcast.sql.impl.type.QueryDataTypeFamily in project hazelcast by hazelcast.

the class IntervalConverter method convertToSelf.

@Override
public Object convertToSelf(Converter converter, Object val) {
    Object val0 = converter.asObject(val);
    if (val0 == null) {
        return null;
    }
    QueryDataTypeFamily family = getTypeFamily();
    if (family == INTERVAL_YEAR_MONTH) {
        if (val0 instanceof SqlYearMonthInterval) {
            return val0;
        }
    } else {
        assert family == INTERVAL_DAY_SECOND;
        if (val0 instanceof SqlDaySecondInterval) {
            return val0;
        }
    }
    throw converter.cannotConvertError(family);
}
Also used : QueryDataTypeFamily(com.hazelcast.sql.impl.type.QueryDataTypeFamily) SqlYearMonthInterval(com.hazelcast.sql.impl.type.SqlYearMonthInterval) SqlDaySecondInterval(com.hazelcast.sql.impl.type.SqlDaySecondInterval)

Example 7 with QueryDataTypeFamily

use of com.hazelcast.sql.impl.type.QueryDataTypeFamily in project hazelcast by hazelcast.

the class UnaryMinusFunction method eval.

@SuppressWarnings("unchecked")
@Override
public T eval(Row row, ExpressionEvalContext context) {
    Object value = operand.eval(row, context);
    if (value == null) {
        return null;
    }
    QueryDataTypeFamily family = resultType.getTypeFamily();
    return (T) evalNumeric((Number) value, family);
}
Also used : QueryDataTypeFamily(com.hazelcast.sql.impl.type.QueryDataTypeFamily) DECIMAL_MATH_CONTEXT(com.hazelcast.sql.impl.expression.math.ExpressionMath.DECIMAL_MATH_CONTEXT)

Example 8 with QueryDataTypeFamily

use of com.hazelcast.sql.impl.type.QueryDataTypeFamily in project hazelcast by hazelcast.

the class DivideFunction method eval.

@SuppressWarnings("unchecked")
@Override
public T eval(Row row, ExpressionEvalContext context) {
    Object left = operand1.eval(row, context);
    if (left == null) {
        return null;
    }
    Object right = operand2.eval(row, context);
    if (right == null) {
        return null;
    }
    QueryDataTypeFamily family = resultType.getTypeFamily();
    if (family.isTemporal()) {
        throw new UnsupportedOperationException("temporal types are unsupported currently");
    }
    return (T) evalNumeric((Number) left, (Number) right, family);
}
Also used : QueryDataTypeFamily(com.hazelcast.sql.impl.type.QueryDataTypeFamily) DECIMAL_MATH_CONTEXT(com.hazelcast.sql.impl.expression.math.ExpressionMath.DECIMAL_MATH_CONTEXT)

Example 9 with QueryDataTypeFamily

use of com.hazelcast.sql.impl.type.QueryDataTypeFamily in project hazelcast by hazelcast.

the class PlusFunction method eval.

@SuppressWarnings("unchecked")
@Override
public T eval(Row row, ExpressionEvalContext context) {
    Object left = operand1.eval(row, context);
    if (left == null) {
        return null;
    }
    Object right = operand2.eval(row, context);
    if (right == null) {
        return null;
    }
    QueryDataTypeFamily family = resultType.getTypeFamily();
    if (family.isTemporal()) {
        return (T) evalTemporal(left, right, family);
    }
    return (T) evalNumeric((Number) left, (Number) right, family);
}
Also used : QueryDataTypeFamily(com.hazelcast.sql.impl.type.QueryDataTypeFamily)

Example 10 with QueryDataTypeFamily

use of com.hazelcast.sql.impl.type.QueryDataTypeFamily in project hazelcast by hazelcast.

the class IndexResolver method removeCastIfPossible.

/**
 * Removes CAST operator from the column expression when possible.
 * <p>
 * The following expression might be found: {@code CAST(a AS BIGINT) > ?}. This may happen either due to type coercion
 * during sql->rel conversion, or due to explicit user request. In the general case, a function applied to the column makes
 * usage of the index on that column impossible.
 * <p>
 * In case of the {@code CAST} operator we may try to remove the CAST, thus relying on internal index converters to
 * downcast the other side of the comparison expression. See {@link TypeConverters}.
 *
 * @param node original node, possibly CAST
 * @return original node if there is nothing to unwrap, or the operand of the CAST
 */
@SuppressWarnings("checkstyle:NestedIfDepth")
private static RexNode removeCastIfPossible(RexNode node) {
    if (node.getKind() == SqlKind.CAST) {
        RexCall node0 = (RexCall) node;
        RexNode from = node0.getOperands().get(0);
        if (from instanceof RexInputRef) {
            RelDataType fromType = from.getType();
            RelDataType toType = node0.getType();
            if (fromType.equals(toType)) {
                // Redundant conversion => unwrap
                return from;
            }
            QueryDataTypeFamily fromFamily = HazelcastTypeUtils.toHazelcastType(fromType).getTypeFamily();
            QueryDataTypeFamily toFamily = HazelcastTypeUtils.toHazelcastType(toType).getTypeFamily();
            if (QueryDataTypeUtils.isNumeric(fromFamily) && QueryDataTypeUtils.isNumeric(toFamily)) {
                // Converting between numeric types
                if (toFamily.getPrecedence() > fromFamily.getPrecedence()) {
                    // overflow error is possible, so we cannot remove the CAST.
                    return from;
                }
            }
        }
    }
    return node;
}
Also used : RexCall(org.apache.calcite.rex.RexCall) QueryDataTypeFamily(com.hazelcast.sql.impl.type.QueryDataTypeFamily) RexInputRef(org.apache.calcite.rex.RexInputRef) RelDataType(org.apache.calcite.rel.type.RelDataType) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

QueryDataTypeFamily (com.hazelcast.sql.impl.type.QueryDataTypeFamily)11 DECIMAL_MATH_CONTEXT (com.hazelcast.sql.impl.expression.math.ExpressionMath.DECIMAL_MATH_CONTEXT)5 SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)2 HazelcastSqlValidator (com.hazelcast.jet.sql.impl.validate.HazelcastSqlValidator)1 HazelcastTypeUtils.toHazelcastTypeFromSqlTypeName (com.hazelcast.jet.sql.impl.validate.types.HazelcastTypeUtils.toHazelcastTypeFromSqlTypeName)1 SqlDaySecondInterval (com.hazelcast.sql.impl.type.SqlDaySecondInterval)1 SqlYearMonthInterval (com.hazelcast.sql.impl.type.SqlYearMonthInterval)1 Converter (com.hazelcast.sql.impl.type.converter.Converter)1 HashSet (java.util.HashSet)1 RelDataType (org.apache.calcite.rel.type.RelDataType)1 RexCall (org.apache.calcite.rex.RexCall)1 RexInputRef (org.apache.calcite.rex.RexInputRef)1 RexNode (org.apache.calcite.rex.RexNode)1 SqlNode (org.apache.calcite.sql.SqlNode)1