use of io.confluent.ksql.schema.ksql.types.SqlType 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.schema.ksql.types.SqlType in project ksql by confluentinc.
the class CastInterpreter method castToArrayFunction.
public static CastFunction castToArrayFunction(final SqlType from, final SqlType to, final KsqlConfig config) {
if (from.baseType() == SqlBaseType.ARRAY && to.baseType() == SqlBaseType.ARRAY) {
final SqlArray fromArray = (SqlArray) from;
final SqlArray toArray = (SqlArray) to;
final CastFunction itemCastFunction = castFunction(fromArray.getItemType(), toArray.getItemType(), config);
return o -> CastEvaluator.castArray((List<?>) o, itemCastFunction::cast);
}
throw new KsqlException(getErrorMessage(from, to));
}
use of io.confluent.ksql.schema.ksql.types.SqlType 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);
}
use of io.confluent.ksql.schema.ksql.types.SqlType in project ksql by confluentinc.
the class TermCompiler method visitDereferenceExpression.
@Override
public Term visitDereferenceExpression(final DereferenceExpression node, final Context context) {
final SqlType functionReturnSchema = expressionTypeManager.getExpressionSqlType(node, context.getLambdaSqlTypeMapping());
final Term struct = process(node.getBase(), context);
if (struct.getSqlType().baseType() != SqlBaseType.STRUCT) {
throw new KsqlException("Can only dereference Struct type, instead got " + struct.getSqlType());
}
return new DereferenceTerm(struct, node.getFieldName(), functionReturnSchema);
}
use of io.confluent.ksql.schema.ksql.types.SqlType in project ksql by confluentinc.
the class TermCompiler method visitArithmeticBinary.
@Override
public Term visitArithmeticBinary(final ArithmeticBinaryExpression node, final Context context) {
final Term left = process(node.getLeft(), context);
final Term right = process(node.getRight(), context);
final SqlType schema = expressionTypeManager.getExpressionSqlType(node, context.getLambdaSqlTypeMapping());
return ArithmeticInterpreter.doBinaryArithmetic(node.getOperator(), left, right, schema, ksqlConfig);
}
Aggregations