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();
}
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());
}
Aggregations