Search in sources :

Example 1 with ComparableCastFunction

use of io.confluent.ksql.execution.interpreter.terms.CastTerm.ComparableCastFunction in project ksql by confluentinc.

the class ComparisonInterpreter method doCompareTo.

// CHECKSTYLE_RULES.OFF: CyclomaticComplexity
private static Optional<ComparisonFunction> doCompareTo(final Term left, final Term right) {
    // CHECKSTYLE_RULES.ON: CyclomaticComplexity
    final SqlBaseType leftType = left.getSqlType().baseType();
    final SqlBaseType rightType = right.getSqlType().baseType();
    if (either(leftType, rightType, SqlBaseType.DECIMAL)) {
        final ComparableCastFunction<BigDecimal> castLeft = castToBigDecimalFunction(left.getSqlType());
        final ComparableCastFunction<BigDecimal> castRight = castToBigDecimalFunction(right.getSqlType());
        return Optional.of((o1, o2) -> castLeft.cast(o1).compareTo(castRight.cast(o2)));
    } else if (either(leftType, rightType, SqlBaseType.TIMESTAMP)) {
        final ComparableCastFunction<Date> castLeft = castToTimestampFunction(left.getSqlType());
        final ComparableCastFunction<Date> castRight = castToTimestampFunction(right.getSqlType());
        return Optional.of((o1, o2) -> castLeft.cast(o1).compareTo(castRight.cast(o2)));
    } else if (either(leftType, rightType, SqlBaseType.DATE)) {
        final ComparableCastFunction<Date> castLeft = castToDateFunction(left.getSqlType());
        final ComparableCastFunction<Date> castRight = castToDateFunction(right.getSqlType());
        return Optional.of((o1, o2) -> castLeft.cast(o1).compareTo(castRight.cast(o2)));
    } else if (either(leftType, rightType, SqlBaseType.TIME)) {
        final ComparableCastFunction<Date> castLeft = castToTimeFunction(left.getSqlType());
        final ComparableCastFunction<Date> castRight = castToTimeFunction(right.getSqlType());
        return Optional.of((o1, o2) -> castLeft.cast(o1).compareTo(castRight.cast(o2)));
    } else if (leftType == SqlBaseType.STRING) {
        return Optional.of((o1, o2) -> o1.toString().compareTo(o2.toString()));
    } else if (leftType == SqlBaseType.BYTES && rightType == SqlBaseType.BYTES) {
        final ComparableCastFunction<ByteBuffer> castLeft = castToBytesFunction(left.getSqlType());
        final ComparableCastFunction<ByteBuffer> castRight = castToBytesFunction(right.getSqlType());
        return Optional.of((o1, o2) -> castLeft.cast(o1).compareTo(castRight.cast(o2)));
    } else if (either(leftType, rightType, SqlBaseType.DOUBLE)) {
        final ComparableCastFunction<Double> castLeft = castToDoubleFunction(left.getSqlType());
        final ComparableCastFunction<Double> castRight = castToDoubleFunction(right.getSqlType());
        return Optional.of((o1, o2) -> castLeft.cast(o1).compareTo(castRight.cast(o2)));
    } else if (either(leftType, rightType, SqlBaseType.BIGINT)) {
        final ComparableCastFunction<Long> castLeft = castToLongFunction(left.getSqlType());
        final ComparableCastFunction<Long> castRight = castToLongFunction(right.getSqlType());
        return Optional.of((o1, o2) -> castLeft.cast(o1).compareTo(castRight.cast(o2)));
    } else if (either(leftType, rightType, SqlBaseType.INTEGER)) {
        final ComparableCastFunction<Integer> castLeft = castToIntegerFunction(left.getSqlType());
        final ComparableCastFunction<Integer> castRight = castToIntegerFunction(right.getSqlType());
        return Optional.of((o1, o2) -> castLeft.cast(o1).compareTo(castRight.cast(o2)));
    }
    return Optional.empty();
}
Also used : CastInterpreter.castToLongFunction(io.confluent.ksql.execution.interpreter.CastInterpreter.castToLongFunction) CastInterpreter.castToBytesFunction(io.confluent.ksql.execution.interpreter.CastInterpreter.castToBytesFunction) EqualsTerm(io.confluent.ksql.execution.interpreter.terms.ComparisonTerm.EqualsTerm) ComparisonCheckFunction(io.confluent.ksql.execution.interpreter.terms.ComparisonTerm.ComparisonCheckFunction) CastInterpreter.castToDoubleFunction(io.confluent.ksql.execution.interpreter.CastInterpreter.castToDoubleFunction) Date(java.util.Date) CastInterpreter.castToIntegerFunction(io.confluent.ksql.execution.interpreter.CastInterpreter.castToIntegerFunction) CastInterpreter.castToBigDecimalFunction(io.confluent.ksql.execution.interpreter.CastInterpreter.castToBigDecimalFunction) SqlBaseType(io.confluent.ksql.schema.ksql.types.SqlBaseType) ByteBuffer(java.nio.ByteBuffer) CastInterpreter.castToDateFunction(io.confluent.ksql.execution.interpreter.CastInterpreter.castToDateFunction) ComparisonFunction(io.confluent.ksql.execution.interpreter.terms.ComparisonTerm.ComparisonFunction) BigDecimal(java.math.BigDecimal) CastInterpreter.castToTimeFunction(io.confluent.ksql.execution.interpreter.CastInterpreter.castToTimeFunction) ComparableCastFunction(io.confluent.ksql.execution.interpreter.terms.CastTerm.ComparableCastFunction) Term(io.confluent.ksql.execution.interpreter.terms.Term) CompareToTerm(io.confluent.ksql.execution.interpreter.terms.ComparisonTerm.CompareToTerm) ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) ComparisonNullCheckFunction(io.confluent.ksql.execution.interpreter.terms.ComparisonTerm.ComparisonNullCheckFunction) CastInterpreter.castToTimestampFunction(io.confluent.ksql.execution.interpreter.CastInterpreter.castToTimestampFunction) KsqlException(io.confluent.ksql.util.KsqlException) Optional(java.util.Optional) EqualsFunction(io.confluent.ksql.execution.interpreter.terms.ComparisonTerm.EqualsFunction) EqualsCheckFunction(io.confluent.ksql.execution.interpreter.terms.ComparisonTerm.EqualsCheckFunction) ComparableCastFunction(io.confluent.ksql.execution.interpreter.terms.CastTerm.ComparableCastFunction) SqlBaseType(io.confluent.ksql.schema.ksql.types.SqlBaseType) BigDecimal(java.math.BigDecimal) Date(java.util.Date)

Example 2 with ComparableCastFunction

use of io.confluent.ksql.execution.interpreter.terms.CastTerm.ComparableCastFunction in project ksql by confluentinc.

the class ArithmeticInterpreter method getNonDecimalArithmeticFunction.

private static ArithmeticBinaryFunction getNonDecimalArithmeticFunction(final Operator operator, final SqlType leftType, final SqlType rightType) {
    final SqlBaseType leftBaseType = leftType.baseType();
    final SqlBaseType rightBaseType = rightType.baseType();
    if (leftBaseType == SqlBaseType.STRING && rightBaseType == SqlBaseType.STRING) {
        return (o1, o2) -> (String) o1 + (String) o2;
    } else if (leftBaseType == SqlBaseType.DOUBLE || rightBaseType == SqlBaseType.DOUBLE) {
        final TypedArithmeticBinaryFunction<Double> fn = getDoubleFunction(operator);
        final ComparableCastFunction<Double> castLeft = castToDoubleFunction(leftType);
        final ComparableCastFunction<Double> castRight = castToDoubleFunction(rightType);
        return (o1, o2) -> fn.doFunction(castLeft.cast(o1), castRight.cast(o2));
    } else if (leftBaseType == SqlBaseType.BIGINT || rightBaseType == SqlBaseType.BIGINT) {
        final TypedArithmeticBinaryFunction<Long> fn = getLongFunction(operator);
        final ComparableCastFunction<Long> castLeft = castToLongFunction(leftType);
        final ComparableCastFunction<Long> castRight = castToLongFunction(rightType);
        return (o1, o2) -> fn.doFunction(castLeft.cast(o1), castRight.cast(o2));
    } else if (leftBaseType == SqlBaseType.INTEGER || rightBaseType == SqlBaseType.INTEGER) {
        final TypedArithmeticBinaryFunction<Integer> fn = getIntegerFunction(operator);
        final ComparableCastFunction<Integer> castLeft = castToIntegerFunction(leftType);
        final ComparableCastFunction<Integer> castRight = castToIntegerFunction(rightType);
        return (o1, o2) -> fn.doFunction(castLeft.cast(o1), castRight.cast(o2));
    } else {
        throw new KsqlException("Can't do arithmetic for types " + leftType + " and " + rightType);
    }
}
Also used : CastInterpreter.castToLongFunction(io.confluent.ksql.execution.interpreter.CastInterpreter.castToLongFunction) CastTerm(io.confluent.ksql.execution.interpreter.terms.CastTerm) DecimalUtil(io.confluent.ksql.util.DecimalUtil) ArithmeticBinaryTerm(io.confluent.ksql.execution.interpreter.terms.ArithmeticBinaryTerm) CastInterpreter.castToDoubleFunction(io.confluent.ksql.execution.interpreter.CastInterpreter.castToDoubleFunction) MathContext(java.math.MathContext) CastInterpreter.castToIntegerFunction(io.confluent.ksql.execution.interpreter.CastInterpreter.castToIntegerFunction) KsqlConfig(io.confluent.ksql.util.KsqlConfig) SqlBaseType(io.confluent.ksql.schema.ksql.types.SqlBaseType) ArithmeticUnaryFunction(io.confluent.ksql.execution.interpreter.terms.ArithmeticUnaryTerm.ArithmeticUnaryFunction) BigDecimal(java.math.BigDecimal) Sign(io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression.Sign) ArithmeticBinaryFunction(io.confluent.ksql.execution.interpreter.terms.ArithmeticBinaryTerm.ArithmeticBinaryFunction) ArithmeticUnaryTerm(io.confluent.ksql.execution.interpreter.terms.ArithmeticUnaryTerm) ComparableCastFunction(io.confluent.ksql.execution.interpreter.terms.CastTerm.ComparableCastFunction) Term(io.confluent.ksql.execution.interpreter.terms.Term) SqlDecimal(io.confluent.ksql.schema.ksql.types.SqlDecimal) Operator(io.confluent.ksql.schema.Operator) KsqlException(io.confluent.ksql.util.KsqlException) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) SqlTypes(io.confluent.ksql.schema.ksql.types.SqlTypes) RoundingMode(java.math.RoundingMode) ComparableCastFunction(io.confluent.ksql.execution.interpreter.terms.CastTerm.ComparableCastFunction) SqlBaseType(io.confluent.ksql.schema.ksql.types.SqlBaseType) KsqlException(io.confluent.ksql.util.KsqlException)

Aggregations

CastInterpreter.castToDoubleFunction (io.confluent.ksql.execution.interpreter.CastInterpreter.castToDoubleFunction)2 CastInterpreter.castToIntegerFunction (io.confluent.ksql.execution.interpreter.CastInterpreter.castToIntegerFunction)2 CastInterpreter.castToLongFunction (io.confluent.ksql.execution.interpreter.CastInterpreter.castToLongFunction)2 ComparableCastFunction (io.confluent.ksql.execution.interpreter.terms.CastTerm.ComparableCastFunction)2 Term (io.confluent.ksql.execution.interpreter.terms.Term)2 SqlBaseType (io.confluent.ksql.schema.ksql.types.SqlBaseType)2 KsqlException (io.confluent.ksql.util.KsqlException)2 BigDecimal (java.math.BigDecimal)2 Sign (io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression.Sign)1 ComparisonExpression (io.confluent.ksql.execution.expression.tree.ComparisonExpression)1 CastInterpreter.castToBigDecimalFunction (io.confluent.ksql.execution.interpreter.CastInterpreter.castToBigDecimalFunction)1 CastInterpreter.castToBytesFunction (io.confluent.ksql.execution.interpreter.CastInterpreter.castToBytesFunction)1 CastInterpreter.castToDateFunction (io.confluent.ksql.execution.interpreter.CastInterpreter.castToDateFunction)1 CastInterpreter.castToTimeFunction (io.confluent.ksql.execution.interpreter.CastInterpreter.castToTimeFunction)1 CastInterpreter.castToTimestampFunction (io.confluent.ksql.execution.interpreter.CastInterpreter.castToTimestampFunction)1 ArithmeticBinaryTerm (io.confluent.ksql.execution.interpreter.terms.ArithmeticBinaryTerm)1 ArithmeticBinaryFunction (io.confluent.ksql.execution.interpreter.terms.ArithmeticBinaryTerm.ArithmeticBinaryFunction)1 ArithmeticUnaryTerm (io.confluent.ksql.execution.interpreter.terms.ArithmeticUnaryTerm)1 ArithmeticUnaryFunction (io.confluent.ksql.execution.interpreter.terms.ArithmeticUnaryTerm.ArithmeticUnaryFunction)1 CastTerm (io.confluent.ksql.execution.interpreter.terms.CastTerm)1