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