Search in sources :

Example 81 with SqlType

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);
    }
}
Also used : ExpressionTypeManager(io.confluent.ksql.execution.util.ExpressionTypeManager) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) Term(io.confluent.ksql.execution.interpreter.terms.Term) KsqlException(io.confluent.ksql.util.KsqlException) KsqlException(io.confluent.ksql.util.KsqlException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 82 with SqlType

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));
}
Also used : CastTerm(io.confluent.ksql.execution.interpreter.terms.CastTerm) DecimalUtil(io.confluent.ksql.util.DecimalUtil) Time(java.sql.Time) Date(java.util.Date) SqlMap(io.confluent.ksql.schema.ksql.types.SqlMap) SqlBaseType(io.confluent.ksql.schema.ksql.types.SqlBaseType) ByteBuffer(java.nio.ByteBuffer) SqlDoubles(io.confluent.ksql.schema.ksql.SqlDoubles) SqlTimeTypes(io.confluent.ksql.schema.ksql.SqlTimeTypes) BigDecimal(java.math.BigDecimal) Term(io.confluent.ksql.execution.interpreter.terms.Term) Map(java.util.Map) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) CastFunction(io.confluent.ksql.execution.interpreter.terms.CastTerm.CastFunction) SqlBooleans(io.confluent.ksql.schema.ksql.SqlBooleans) Timestamp(java.sql.Timestamp) KsqlConfig(io.confluent.ksql.util.KsqlConfig) SqlArray(io.confluent.ksql.schema.ksql.types.SqlArray) Objects(java.util.Objects) CastEvaluator(io.confluent.ksql.execution.codegen.helpers.CastEvaluator) List(java.util.List) ComparableCastFunction(io.confluent.ksql.execution.interpreter.terms.CastTerm.ComparableCastFunction) SqlDecimal(io.confluent.ksql.schema.ksql.types.SqlDecimal) KsqlException(io.confluent.ksql.util.KsqlException) SqlTypes(io.confluent.ksql.schema.ksql.types.SqlTypes) CastFunction(io.confluent.ksql.execution.interpreter.terms.CastTerm.CastFunction) ComparableCastFunction(io.confluent.ksql.execution.interpreter.terms.CastTerm.ComparableCastFunction) KsqlException(io.confluent.ksql.util.KsqlException) SqlArray(io.confluent.ksql.schema.ksql.types.SqlArray)

Example 83 with SqlType

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);
}
Also used : LogicalBinaryExpression(io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) ArithmeticUnaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression) SimpleCaseExpression(io.confluent.ksql.execution.expression.tree.SimpleCaseExpression) InListExpression(io.confluent.ksql.execution.expression.tree.InListExpression) SearchedCaseExpression(io.confluent.ksql.execution.expression.tree.SearchedCaseExpression) ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) NotExpression(io.confluent.ksql.execution.expression.tree.NotExpression) SubscriptExpression(io.confluent.ksql.execution.expression.tree.SubscriptExpression) ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) CreateMapTerm(io.confluent.ksql.execution.interpreter.terms.CreateMapTerm) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) Term(io.confluent.ksql.execution.interpreter.terms.Term) LambdaFunction3Term(io.confluent.ksql.execution.interpreter.terms.LambdaFunctionTerms.LambdaFunction3Term) ColumnReferenceTerm(io.confluent.ksql.execution.interpreter.terms.ColumnReferenceTerm) LambdaFunction1Term(io.confluent.ksql.execution.interpreter.terms.LambdaFunctionTerms.LambdaFunction1Term) LikeTerm(io.confluent.ksql.execution.interpreter.terms.LikeTerm) FunctionCallTerm(io.confluent.ksql.execution.interpreter.terms.FunctionCallTerm) SearchedCaseTerm(io.confluent.ksql.execution.interpreter.terms.SearchedCaseTerm) StructTerm(io.confluent.ksql.execution.interpreter.terms.StructTerm) LambdaVariableTerm(io.confluent.ksql.execution.interpreter.terms.LambdaVariableTerm) CreateMapTerm(io.confluent.ksql.execution.interpreter.terms.CreateMapTerm) LambdaFunction2Term(io.confluent.ksql.execution.interpreter.terms.LambdaFunctionTerms.LambdaFunction2Term) SubscriptTerm(io.confluent.ksql.execution.interpreter.terms.SubscriptTerm) NotTerm(io.confluent.ksql.execution.interpreter.terms.NotTerm) IsNullTerm(io.confluent.ksql.execution.interpreter.terms.IsNullTerm) CreateArrayTerm(io.confluent.ksql.execution.interpreter.terms.CreateArrayTerm) DereferenceTerm(io.confluent.ksql.execution.interpreter.terms.DereferenceTerm) IsNotNullTerm(io.confluent.ksql.execution.interpreter.terms.IsNotNullTerm) InPredicateTerm(io.confluent.ksql.execution.interpreter.terms.InPredicateTerm) ImmutableMap(com.google.common.collect.ImmutableMap) Pair(io.confluent.ksql.util.Pair)

Example 84 with SqlType

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);
}
Also used : DereferenceTerm(io.confluent.ksql.execution.interpreter.terms.DereferenceTerm) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) Term(io.confluent.ksql.execution.interpreter.terms.Term) LambdaFunction3Term(io.confluent.ksql.execution.interpreter.terms.LambdaFunctionTerms.LambdaFunction3Term) ColumnReferenceTerm(io.confluent.ksql.execution.interpreter.terms.ColumnReferenceTerm) LambdaFunction1Term(io.confluent.ksql.execution.interpreter.terms.LambdaFunctionTerms.LambdaFunction1Term) LikeTerm(io.confluent.ksql.execution.interpreter.terms.LikeTerm) FunctionCallTerm(io.confluent.ksql.execution.interpreter.terms.FunctionCallTerm) SearchedCaseTerm(io.confluent.ksql.execution.interpreter.terms.SearchedCaseTerm) StructTerm(io.confluent.ksql.execution.interpreter.terms.StructTerm) LambdaVariableTerm(io.confluent.ksql.execution.interpreter.terms.LambdaVariableTerm) CreateMapTerm(io.confluent.ksql.execution.interpreter.terms.CreateMapTerm) LambdaFunction2Term(io.confluent.ksql.execution.interpreter.terms.LambdaFunctionTerms.LambdaFunction2Term) SubscriptTerm(io.confluent.ksql.execution.interpreter.terms.SubscriptTerm) NotTerm(io.confluent.ksql.execution.interpreter.terms.NotTerm) IsNullTerm(io.confluent.ksql.execution.interpreter.terms.IsNullTerm) CreateArrayTerm(io.confluent.ksql.execution.interpreter.terms.CreateArrayTerm) DereferenceTerm(io.confluent.ksql.execution.interpreter.terms.DereferenceTerm) IsNotNullTerm(io.confluent.ksql.execution.interpreter.terms.IsNotNullTerm) InPredicateTerm(io.confluent.ksql.execution.interpreter.terms.InPredicateTerm) KsqlException(io.confluent.ksql.util.KsqlException)

Example 85 with SqlType

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);
}
Also used : SqlType(io.confluent.ksql.schema.ksql.types.SqlType) Term(io.confluent.ksql.execution.interpreter.terms.Term) LambdaFunction3Term(io.confluent.ksql.execution.interpreter.terms.LambdaFunctionTerms.LambdaFunction3Term) ColumnReferenceTerm(io.confluent.ksql.execution.interpreter.terms.ColumnReferenceTerm) LambdaFunction1Term(io.confluent.ksql.execution.interpreter.terms.LambdaFunctionTerms.LambdaFunction1Term) LikeTerm(io.confluent.ksql.execution.interpreter.terms.LikeTerm) FunctionCallTerm(io.confluent.ksql.execution.interpreter.terms.FunctionCallTerm) SearchedCaseTerm(io.confluent.ksql.execution.interpreter.terms.SearchedCaseTerm) StructTerm(io.confluent.ksql.execution.interpreter.terms.StructTerm) LambdaVariableTerm(io.confluent.ksql.execution.interpreter.terms.LambdaVariableTerm) CreateMapTerm(io.confluent.ksql.execution.interpreter.terms.CreateMapTerm) LambdaFunction2Term(io.confluent.ksql.execution.interpreter.terms.LambdaFunctionTerms.LambdaFunction2Term) SubscriptTerm(io.confluent.ksql.execution.interpreter.terms.SubscriptTerm) NotTerm(io.confluent.ksql.execution.interpreter.terms.NotTerm) IsNullTerm(io.confluent.ksql.execution.interpreter.terms.IsNullTerm) CreateArrayTerm(io.confluent.ksql.execution.interpreter.terms.CreateArrayTerm) DereferenceTerm(io.confluent.ksql.execution.interpreter.terms.DereferenceTerm) IsNotNullTerm(io.confluent.ksql.execution.interpreter.terms.IsNotNullTerm) InPredicateTerm(io.confluent.ksql.execution.interpreter.terms.InPredicateTerm)

Aggregations

SqlType (io.confluent.ksql.schema.ksql.types.SqlType)140 Test (org.junit.Test)80 Expression (io.confluent.ksql.execution.expression.tree.Expression)47 CreateStructExpression (io.confluent.ksql.execution.expression.tree.CreateStructExpression)38 KsqlException (io.confluent.ksql.util.KsqlException)33 InListExpression (io.confluent.ksql.execution.expression.tree.InListExpression)30 ArithmeticBinaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression)29 ComparisonExpression (io.confluent.ksql.execution.expression.tree.ComparisonExpression)29 CreateArrayExpression (io.confluent.ksql.execution.expression.tree.CreateArrayExpression)29 CreateMapExpression (io.confluent.ksql.execution.expression.tree.CreateMapExpression)29 DereferenceExpression (io.confluent.ksql.execution.expression.tree.DereferenceExpression)29 NotExpression (io.confluent.ksql.execution.expression.tree.NotExpression)29 SearchedCaseExpression (io.confluent.ksql.execution.expression.tree.SearchedCaseExpression)29 SimpleCaseExpression (io.confluent.ksql.execution.expression.tree.SimpleCaseExpression)29 SubscriptExpression (io.confluent.ksql.execution.expression.tree.SubscriptExpression)29 Optional (java.util.Optional)20 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)15 Result (io.confluent.ksql.schema.ksql.SqlValueCoercer.Result)14 Struct (org.apache.kafka.connect.data.Struct)14 IntegerLiteral (io.confluent.ksql.execution.expression.tree.IntegerLiteral)13