use of io.confluent.ksql.execution.interpreter.terms.Term in project ksql by confluentinc.
the class TermCompiler method visitSearchedCaseExpression.
@Override
public Term visitSearchedCaseExpression(final SearchedCaseExpression node, final Context context) {
final SqlType resultSchema = expressionTypeManager.getExpressionSqlType(node, context.getLambdaSqlTypeMapping());
final List<Pair<Term, Term>> operandResultTerms = node.getWhenClauses().stream().map(whenClause -> Pair.of(process(whenClause.getOperand(), context), process(whenClause.getResult(), context))).collect(ImmutableList.toImmutableList());
final Optional<Term> defaultValueTerm = node.getDefaultValue().map(exp -> process(node.getDefaultValue().get(), context));
return new SearchedCaseTerm(operandResultTerms, defaultValueTerm, resultSchema);
}
use of io.confluent.ksql.execution.interpreter.terms.Term 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.Term in project ksql by confluentinc.
the class ArithmeticInterpreter method doBinaryArithmetic.
/**
* Creates a term representing binary arithmetic on the given input term.
* @param operator The operator in use
* @param left The left term
* @param right The right term
* @param resultType The type of the resulting operation
* @param ksqlConfig The ksqlconfig
* @return the resulting term
*/
public static Term doBinaryArithmetic(final Operator operator, final Term left, final Term right, final SqlType resultType, final KsqlConfig ksqlConfig) {
if (resultType.baseType() == SqlBaseType.DECIMAL) {
final SqlDecimal decimal = (SqlDecimal) resultType;
final CastTerm leftTerm = CastInterpreter.cast(left, left.getSqlType(), DecimalUtil.toSqlDecimal(left.getSqlType()), ksqlConfig);
final CastTerm rightTerm = CastInterpreter.cast(right, right.getSqlType(), DecimalUtil.toSqlDecimal(right.getSqlType()), ksqlConfig);
final TypedArithmeticBinaryFunction<BigDecimal> fn = getDecimalFunction(decimal, operator);
return new ArithmeticBinaryTerm(leftTerm, rightTerm, (o1, o2) -> fn.doFunction((BigDecimal) o1, (BigDecimal) o2), resultType);
} else {
final Term leftTerm = left.getSqlType().baseType() == SqlBaseType.DECIMAL ? CastInterpreter.cast(left, left.getSqlType(), SqlTypes.DOUBLE, ksqlConfig) : left;
final Term rightTerm = right.getSqlType().baseType() == SqlBaseType.DECIMAL ? CastInterpreter.cast(right, right.getSqlType(), SqlTypes.DOUBLE, ksqlConfig) : right;
return new ArithmeticBinaryTerm(leftTerm, rightTerm, getNonDecimalArithmeticFunction(operator, leftTerm.getSqlType(), rightTerm.getSqlType()), resultType);
}
}
use of io.confluent.ksql.execution.interpreter.terms.Term in project ksql by confluentinc.
the class InterpretedExpressionFactory method create.
@VisibleForTesting
public static InterpretedExpression create(final Expression expression, final LogicalSchema schema, final FunctionRegistry functionRegistry, final KsqlConfig ksqlConfig, final Context context) {
try {
final ExpressionTypeManager expressionTypeManager = new ExpressionTypeManager(schema, functionRegistry);
final SqlType returnType = expressionTypeManager.getExpressionSqlType(expression, context.getLambdaSqlTypeMapping());
if (returnType == null) {
// practice.
throw new KsqlException("NULL expression not supported");
}
final Term term = new TermCompiler(functionRegistry, schema, ksqlConfig, expressionTypeManager).process(expression, context);
return new InterpretedExpression(expression, returnType, term);
} catch (KsqlException e) {
throw new KsqlException("Invalid expression: " + e.getMessage() + ". expression: " + expression + ", schema:" + schema, e);
} catch (final Exception e) {
throw new RuntimeException("Unexpected error generating code for expression: " + expression, e);
}
}
use of io.confluent.ksql.execution.interpreter.terms.Term in project ksql by confluentinc.
the class TermCompiler method visitCreateMapExpression.
@Override
public Term visitCreateMapExpression(final CreateMapExpression exp, final Context context) {
final ImmutableMap<Expression, Expression> map = exp.getMap();
final List<Expression> keys = CoercionUtil.coerceUserList(map.keySet(), expressionTypeManager, context.getLambdaSqlTypeMapping()).expressions();
final List<Expression> values = CoercionUtil.coerceUserList(map.values(), expressionTypeManager, context.getLambdaSqlTypeMapping()).expressions();
final Iterable<Pair<Expression, Expression>> pairs = () -> Streams.zip(keys.stream(), values.stream(), Pair::of).iterator();
final ImmutableMap.Builder<Term, Term> mapTerms = ImmutableMap.builder();
for (Pair<Expression, Expression> p : pairs) {
mapTerms.put(process(p.getLeft(), context), process(p.getRight(), context));
}
final SqlType resultType = expressionTypeManager.getExpressionSqlType(exp, context.getLambdaSqlTypeMapping());
return new CreateMapTerm(mapTerms.build(), resultType);
}
Aggregations