Search in sources :

Example 1 with ExpressionTypeManager

use of io.confluent.ksql.execution.util.ExpressionTypeManager in project ksql by confluentinc.

the class FilterTypeValidator method getExpressionReturnType.

private SqlType getExpressionReturnType(final Expression exp) {
    final ExpressionTypeManager expressionTypeManager = new ExpressionTypeManager(schema, functionRegistry);
    // Rewrite the expression with magic timestamps, so type checking can pass
    final Expression magicTimestampRewrite = new StatementRewriteForMagicPseudoTimestamp().rewrite(exp);
    try {
        return expressionTypeManager.getExpressionSqlType(magicTimestampRewrite);
    } catch (KsqlException e) {
        throw new KsqlStatementException("Error in " + filterType.name() + " expression: " + e.getMessage(), exp.toString());
    }
}
Also used : ExpressionTypeManager(io.confluent.ksql.execution.util.ExpressionTypeManager) Expression(io.confluent.ksql.execution.expression.tree.Expression) StatementRewriteForMagicPseudoTimestamp(io.confluent.ksql.engine.rewrite.StatementRewriteForMagicPseudoTimestamp) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) KsqlException(io.confluent.ksql.util.KsqlException)

Example 2 with ExpressionTypeManager

use of io.confluent.ksql.execution.util.ExpressionTypeManager in project ksql by confluentinc.

the class StepSchemaResolver method handleStreamSelectKeyV1.

private LogicalSchema handleStreamSelectKeyV1(final LogicalSchema sourceSchema, final StreamSelectKeyV1 step) {
    final ExpressionTypeManager expressionTypeManager = new ExpressionTypeManager(sourceSchema, functionRegistry);
    final SqlType keyType = expressionTypeManager.getExpressionSqlType(step.getKeyExpression());
    return LogicalSchema.builder().keyColumn(SystemColumns.ROWKEY_NAME, keyType).valueColumns(sourceSchema.value()).build();
}
Also used : ExpressionTypeManager(io.confluent.ksql.execution.util.ExpressionTypeManager) SqlType(io.confluent.ksql.schema.ksql.types.SqlType)

Example 3 with ExpressionTypeManager

use of io.confluent.ksql.execution.util.ExpressionTypeManager in project ksql by confluentinc.

the class TermCompiler method visitCreateArrayExpression.

@Override
public Term visitCreateArrayExpression(final CreateArrayExpression exp, final Context context) {
    final List<Expression> expressions = CoercionUtil.coerceUserList(exp.getValues(), expressionTypeManager, context.getLambdaSqlTypeMapping()).expressions();
    final List<Term> arrayTerms = expressions.stream().map(value -> process(value, context)).collect(ImmutableList.toImmutableList());
    final SqlType sqlType = expressionTypeManager.getExpressionSqlType(exp, context.getLambdaSqlTypeMapping());
    return new CreateArrayTerm(arrayTerms, sqlType);
}
Also used : TimestampLiteral(io.confluent.ksql.execution.expression.tree.TimestampLiteral) LogicalBinaryExpression(io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression) GenericsUtil(io.confluent.ksql.function.GenericsUtil) Pair(io.confluent.ksql.util.Pair) Term(io.confluent.ksql.execution.interpreter.terms.Term) LambdaFunction3Term(io.confluent.ksql.execution.interpreter.terms.LambdaFunctionTerms.LambdaFunction3Term) LiteralTerms(io.confluent.ksql.execution.interpreter.terms.LiteralTerms) Map(java.util.Map) ColumnReferenceTerm(io.confluent.ksql.execution.interpreter.terms.ColumnReferenceTerm) ExpressionTypeManager(io.confluent.ksql.execution.util.ExpressionTypeManager) NullLiteral(io.confluent.ksql.execution.expression.tree.NullLiteral) SchemaConverters.sqlToFunctionConverter(io.confluent.ksql.schema.ksql.SchemaConverters.sqlToFunctionConverter) DecimalLiteral(io.confluent.ksql.execution.expression.tree.DecimalLiteral) BytesLiteral(io.confluent.ksql.execution.expression.tree.BytesLiteral) LambdaVariable(io.confluent.ksql.execution.expression.tree.LambdaVariable) Expression(io.confluent.ksql.execution.expression.tree.Expression) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) LambdaFunction1Term(io.confluent.ksql.execution.interpreter.terms.LambdaFunctionTerms.LambdaFunction1Term) KsqlException(io.confluent.ksql.util.KsqlException) ArithmeticUnaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression) Iterables(com.google.common.collect.Iterables) KsqlScalarFunction(io.confluent.ksql.function.KsqlScalarFunction) ExpressionVisitor(io.confluent.ksql.execution.expression.tree.ExpressionVisitor) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp) Field(io.confluent.ksql.execution.expression.tree.CreateStructExpression.Field) SqlMap(io.confluent.ksql.schema.ksql.types.SqlMap) SqlBaseType(io.confluent.ksql.schema.ksql.types.SqlBaseType) ArrayList(java.util.ArrayList) InListEvaluator(io.confluent.ksql.execution.codegen.helpers.InListEvaluator) BetweenPredicate(io.confluent.ksql.execution.expression.tree.BetweenPredicate) Cast(io.confluent.ksql.execution.expression.tree.Cast) LikeTerm(io.confluent.ksql.execution.interpreter.terms.LikeTerm) CoercionUtil(io.confluent.ksql.execution.util.CoercionUtil) FunctionCallTerm(io.confluent.ksql.execution.interpreter.terms.FunctionCallTerm) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) Context(io.confluent.ksql.execution.interpreter.TermCompiler.Context) LogicalBinaryTerms(io.confluent.ksql.execution.interpreter.terms.LogicalBinaryTerms) FunctionTypeInfo(io.confluent.ksql.execution.util.FunctionArgumentsUtil.FunctionTypeInfo) Type(io.confluent.ksql.execution.expression.tree.Type) Kudf(io.confluent.ksql.function.udf.Kudf) ParamTypes(io.confluent.ksql.function.types.ParamTypes) InPredicate(io.confluent.ksql.execution.expression.tree.InPredicate) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) SimpleCaseExpression(io.confluent.ksql.execution.expression.tree.SimpleCaseExpression) InListExpression(io.confluent.ksql.execution.expression.tree.InListExpression) ArrayType(io.confluent.ksql.function.types.ArrayType) SearchedCaseExpression(io.confluent.ksql.execution.expression.tree.SearchedCaseExpression) DecimalUtil(io.confluent.ksql.util.DecimalUtil) ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) UdfFactory(io.confluent.ksql.function.UdfFactory) ArgumentInfo(io.confluent.ksql.execution.util.FunctionArgumentsUtil.ArgumentInfo) SearchedCaseTerm(io.confluent.ksql.execution.interpreter.terms.SearchedCaseTerm) StructTerm(io.confluent.ksql.execution.interpreter.terms.StructTerm) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) LambdaVariableTerm(io.confluent.ksql.execution.interpreter.terms.LambdaVariableTerm) DateLiteral(io.confluent.ksql.execution.expression.tree.DateLiteral) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) DoubleLiteral(io.confluent.ksql.execution.expression.tree.DoubleLiteral) Immutable(com.google.errorprone.annotations.Immutable) IntervalUnit(io.confluent.ksql.execution.expression.tree.IntervalUnit) 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) ImmutableMap(com.google.common.collect.ImmutableMap) NotTerm(io.confluent.ksql.execution.interpreter.terms.NotTerm) FunctionRegistry(io.confluent.ksql.function.FunctionRegistry) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) WhenClause(io.confluent.ksql.execution.expression.tree.WhenClause) IsNullTerm(io.confluent.ksql.execution.interpreter.terms.IsNullTerm) IsNotNullPredicate(io.confluent.ksql.execution.expression.tree.IsNotNullPredicate) FunctionArgumentsUtil(io.confluent.ksql.execution.util.FunctionArgumentsUtil) KsqlConfig(io.confluent.ksql.util.KsqlConfig) Streams(com.google.common.collect.Streams) SqlArray(io.confluent.ksql.schema.ksql.types.SqlArray) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) String.format(java.lang.String.format) CreateArrayTerm(io.confluent.ksql.execution.interpreter.terms.CreateArrayTerm) List(java.util.List) BooleanLiteral(io.confluent.ksql.execution.expression.tree.BooleanLiteral) Optional(java.util.Optional) Column(io.confluent.ksql.schema.ksql.Column) DereferenceTerm(io.confluent.ksql.execution.interpreter.terms.DereferenceTerm) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) ArrayAccess(io.confluent.ksql.execution.codegen.helpers.ArrayAccess) IsNullPredicate(io.confluent.ksql.execution.expression.tree.IsNullPredicate) ParamType(io.confluent.ksql.function.types.ParamType) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) HashMap(java.util.HashMap) LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) ImmutableList(com.google.common.collect.ImmutableList) QualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.QualifiedColumnReferenceExp) SchemaConverters(io.confluent.ksql.schema.ksql.SchemaConverters) TimeLiteral(io.confluent.ksql.execution.expression.tree.TimeLiteral) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) IsNotNullTerm(io.confluent.ksql.execution.interpreter.terms.IsNotNullTerm) NotExpression(io.confluent.ksql.execution.expression.tree.NotExpression) InPredicateTerm(io.confluent.ksql.execution.interpreter.terms.InPredicateTerm) LikePredicate(io.confluent.ksql.execution.expression.tree.LikePredicate) SubscriptExpression(io.confluent.ksql.execution.expression.tree.SubscriptExpression) ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) 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) CreateArrayTerm(io.confluent.ksql.execution.interpreter.terms.CreateArrayTerm) 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)

Example 4 with ExpressionTypeManager

use of io.confluent.ksql.execution.util.ExpressionTypeManager in project ksql by confluentinc.

the class UdtfUtil method resolveTableFunction.

public static KsqlTableFunction resolveTableFunction(final FunctionRegistry functionRegistry, final FunctionCall functionCall, final LogicalSchema schema) {
    final ExpressionTypeManager expressionTypeManager = new ExpressionTypeManager(schema, functionRegistry);
    final List<Expression> functionArgs = functionCall.getArguments();
    final List<SqlArgument> argTypes = functionArgs.isEmpty() ? ImmutableList.of(SqlArgument.of(FunctionRegistry.DEFAULT_FUNCTION_ARG_SCHEMA)) : functionArgs.stream().map(expressionTypeManager::getExpressionSqlType).map(SqlArgument::of).collect(Collectors.toList());
    return functionRegistry.getTableFunction(functionCall.getName(), argTypes);
}
Also used : ExpressionTypeManager(io.confluent.ksql.execution.util.ExpressionTypeManager) SqlArgument(io.confluent.ksql.schema.ksql.SqlArgument) Expression(io.confluent.ksql.execution.expression.tree.Expression)

Example 5 with ExpressionTypeManager

use of io.confluent.ksql.execution.util.ExpressionTypeManager in project ksql by confluentinc.

the class InListEvaluator method preprocess.

/**
 * Preprocess the list of possible expression, ensuring compatible types, performing literal
 * coercion and removing null literals (which can never match).
 *
 * @param predicate The predicate to process
 * @param typeManager the type manager for the predicate
 * @param lambdaTypeMapping mapping of lambda variables to type
 * @return {@code predicate} after processing.
 */
public static InPredicate preprocess(final InPredicate predicate, final ExpressionTypeManager typeManager, final Map<String, SqlType> lambdaTypeMapping) {
    final List<Expression> nonNull = ImmutableList.<Expression>builder().add(predicate.getValue()).addAll(predicate.getValueList().getValues().stream().filter(e -> !(e instanceof NullLiteral)).collect(Collectors.toList())).build();
    final List<Expression> coerced = CoercionUtil.coerceUserList(nonNull, typeManager, lambdaTypeMapping).expressions();
    return new InPredicate(predicate.getLocation(), coerced.get(0), new InListExpression(predicate.getValueList().getLocation(), coerced.subList(1, coerced.size())));
}
Also used : Iterator(java.util.Iterator) ImmutableMap(com.google.common.collect.ImmutableMap) Expression(io.confluent.ksql.execution.expression.tree.Expression) Field(org.apache.kafka.connect.data.Field) SqlBooleans(io.confluent.ksql.schema.ksql.SqlBooleans) BiFunction(java.util.function.BiFunction) InPredicate(io.confluent.ksql.execution.expression.tree.InPredicate) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Schema(org.apache.kafka.connect.data.Schema) BigDecimal(java.math.BigDecimal) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) InListExpression(io.confluent.ksql.execution.expression.tree.InListExpression) Map(java.util.Map) Struct(org.apache.kafka.connect.data.Struct) CoercionUtil(io.confluent.ksql.execution.util.CoercionUtil) ExpressionTypeManager(io.confluent.ksql.execution.util.ExpressionTypeManager) Entry(java.util.Map.Entry) NullLiteral(io.confluent.ksql.execution.expression.tree.NullLiteral) Optional(java.util.Optional) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) RoundingMode(java.math.RoundingMode) Expression(io.confluent.ksql.execution.expression.tree.Expression) InListExpression(io.confluent.ksql.execution.expression.tree.InListExpression) InListExpression(io.confluent.ksql.execution.expression.tree.InListExpression) InPredicate(io.confluent.ksql.execution.expression.tree.InPredicate) NullLiteral(io.confluent.ksql.execution.expression.tree.NullLiteral)

Aggregations

ExpressionTypeManager (io.confluent.ksql.execution.util.ExpressionTypeManager)13 SqlType (io.confluent.ksql.schema.ksql.types.SqlType)11 Expression (io.confluent.ksql.execution.expression.tree.Expression)8 KsqlException (io.confluent.ksql.util.KsqlException)6 Column (io.confluent.ksql.schema.ksql.Column)5 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)5 List (java.util.List)5 Optional (java.util.Optional)5 FunctionCall (io.confluent.ksql.execution.expression.tree.FunctionCall)4 UnqualifiedColumnReferenceExp (io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp)4 Builder (io.confluent.ksql.schema.ksql.LogicalSchema.Builder)4 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 Iterables (com.google.common.collect.Iterables)3 SelectExpression (io.confluent.ksql.execution.plan.SelectExpression)3 ColumnName (io.confluent.ksql.name.ColumnName)3 Streams (com.google.common.collect.Streams)2 Immutable (com.google.errorprone.annotations.Immutable)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 ColumnReferenceExp (io.confluent.ksql.execution.expression.tree.ColumnReferenceExp)2