Search in sources :

Example 1 with ComparisonFunction

use of io.confluent.ksql.execution.interpreter.terms.ComparisonTerm.ComparisonFunction 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 ComparisonFunction

use of io.confluent.ksql.execution.interpreter.terms.ComparisonTerm.ComparisonFunction in project ksql by confluentinc.

the class ComparisonInterpreter method doComparison.

/**
 * Does a comparison between the two terms
 * @param type The type of the comparison
 * @param left Left term
 * @param right Right term
 * @return The term representing the result of the comparison
 */
public static Term doComparison(final ComparisonExpression.Type type, final Term left, final Term right) {
    final ComparisonNullCheckFunction nullCheckFunction = getNullCheckFunction(type);
    final Optional<ComparisonFunction> compareTo = doCompareTo(left, right);
    if (compareTo.isPresent()) {
        return new CompareToTerm(left, right, nullCheckFunction, compareTo.get(), getComparisonCheckFunction(type, left, right));
    }
    final Optional<EqualsFunction> equals = doEquals(left, right);
    if (equals.isPresent()) {
        return new EqualsTerm(left, right, nullCheckFunction, equals.get(), getEqualsCheckFunction(type, left, right));
    }
    throw new KsqlException("Unsupported comparison between " + left.getSqlType() + " and " + right.getSqlType());
}
Also used : ComparisonNullCheckFunction(io.confluent.ksql.execution.interpreter.terms.ComparisonTerm.ComparisonNullCheckFunction) ComparisonFunction(io.confluent.ksql.execution.interpreter.terms.ComparisonTerm.ComparisonFunction) EqualsTerm(io.confluent.ksql.execution.interpreter.terms.ComparisonTerm.EqualsTerm) CompareToTerm(io.confluent.ksql.execution.interpreter.terms.ComparisonTerm.CompareToTerm) KsqlException(io.confluent.ksql.util.KsqlException) EqualsFunction(io.confluent.ksql.execution.interpreter.terms.ComparisonTerm.EqualsFunction)

Aggregations

CompareToTerm (io.confluent.ksql.execution.interpreter.terms.ComparisonTerm.CompareToTerm)2 ComparisonFunction (io.confluent.ksql.execution.interpreter.terms.ComparisonTerm.ComparisonFunction)2 ComparisonNullCheckFunction (io.confluent.ksql.execution.interpreter.terms.ComparisonTerm.ComparisonNullCheckFunction)2 EqualsFunction (io.confluent.ksql.execution.interpreter.terms.ComparisonTerm.EqualsFunction)2 EqualsTerm (io.confluent.ksql.execution.interpreter.terms.ComparisonTerm.EqualsTerm)2 KsqlException (io.confluent.ksql.util.KsqlException)2 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.castToDoubleFunction (io.confluent.ksql.execution.interpreter.CastInterpreter.castToDoubleFunction)1 CastInterpreter.castToIntegerFunction (io.confluent.ksql.execution.interpreter.CastInterpreter.castToIntegerFunction)1 CastInterpreter.castToLongFunction (io.confluent.ksql.execution.interpreter.CastInterpreter.castToLongFunction)1 CastInterpreter.castToTimeFunction (io.confluent.ksql.execution.interpreter.CastInterpreter.castToTimeFunction)1 CastInterpreter.castToTimestampFunction (io.confluent.ksql.execution.interpreter.CastInterpreter.castToTimestampFunction)1 ComparableCastFunction (io.confluent.ksql.execution.interpreter.terms.CastTerm.ComparableCastFunction)1 ComparisonCheckFunction (io.confluent.ksql.execution.interpreter.terms.ComparisonTerm.ComparisonCheckFunction)1 EqualsCheckFunction (io.confluent.ksql.execution.interpreter.terms.ComparisonTerm.EqualsCheckFunction)1 Term (io.confluent.ksql.execution.interpreter.terms.Term)1 SqlBaseType (io.confluent.ksql.schema.ksql.types.SqlBaseType)1