Search in sources :

Example 91 with SqlType

use of io.confluent.ksql.schema.ksql.types.SqlType in project ksql by confluentinc.

the class GenericsUtil method resolveGenerics.

/**
 * Identifies a mapping from generic type to concrete type based on a {@code schema} and
 * an {@code instance}, where the {@code instance} schema is expected to have no generic
 * types and have the same nested structure as {@code schema}. Any Generic type mapping
 * identified is added to the list passed in.
 *
 * @param mapping   a list of GenericType to SqlType mappings
 * @param schema    the schema that may contain generics
 * @param instance  a schema with the same structure as {@code schema} but with no generics
 *
 * @return whether we were able to resolve generics in the instance and schema
 */
// CHECKSTYLE_RULES.OFF: NPathComplexity
// CHECKSTYLE_RULES.OFF: CyclomaticComplexity
private static boolean resolveGenerics(final List<Entry<GenericType, SqlType>> mapping, final ParamType schema, final SqlArgument instance) {
    if (!isGeneric(schema) && !matches(schema, instance)) {
        // cannot identify from type mismatch
        return false;
    } else if (!hasGenerics(schema)) {
        // nothing left to identify
        return true;
    }
    KsqlPreconditions.checkArgument(isGeneric(schema) || (matches(schema, instance)), "Cannot resolve generics if the schema and instance have differing types: " + schema + " vs. " + instance);
    if (schema instanceof LambdaType) {
        final LambdaType lambdaType = (LambdaType) schema;
        final SqlLambda sqlLambda = instance.getSqlLambdaOrThrow();
        if (lambdaType.inputTypes().size() == sqlLambda.getNumInputs()) {
            if (sqlLambda instanceof SqlLambdaResolved) {
                final SqlLambdaResolved sqlLambdaResolved = (SqlLambdaResolved) sqlLambda;
                int i = 0;
                for (final ParamType paramType : lambdaType.inputTypes()) {
                    if (!resolveGenerics(mapping, paramType, SqlArgument.of(sqlLambdaResolved.getInputType().get(i)))) {
                        return false;
                    }
                    i++;
                }
                return resolveGenerics(mapping, lambdaType.returnType(), SqlArgument.of(sqlLambdaResolved.getReturnType()));
            } else {
                return true;
            }
        } else {
            return false;
        }
    }
    final SqlType sqlType = instance.getSqlTypeOrThrow();
    if (isGeneric(schema)) {
        mapping.add(new HashMap.SimpleEntry<>((GenericType) schema, sqlType));
    }
    if (schema instanceof ArrayType) {
        final SqlArray sqlArray = (SqlArray) sqlType;
        return resolveGenerics(mapping, ((ArrayType) schema).element(), SqlArgument.of(sqlArray.getItemType()));
    }
    if (schema instanceof MapType) {
        final SqlMap sqlMap = (SqlMap) sqlType;
        final MapType mapType = (MapType) schema;
        return resolveGenerics(mapping, mapType.key(), SqlArgument.of(sqlMap.getKeyType())) && resolveGenerics(mapping, mapType.value(), SqlArgument.of(sqlMap.getValueType()));
    }
    if (schema instanceof StructType) {
        throw new KsqlException("Generic STRUCT is not yet supported");
    }
    return true;
}
Also used : LambdaType(io.confluent.ksql.function.types.LambdaType) GenericType(io.confluent.ksql.function.types.GenericType) SqlMap(io.confluent.ksql.schema.ksql.types.SqlMap) StructType(io.confluent.ksql.function.types.StructType) SqlLambda(io.confluent.ksql.schema.ksql.types.SqlLambda) HashMap(java.util.HashMap) KsqlException(io.confluent.ksql.util.KsqlException) ParamType(io.confluent.ksql.function.types.ParamType) MapType(io.confluent.ksql.function.types.MapType) ArrayType(io.confluent.ksql.function.types.ArrayType) SqlLambdaResolved(io.confluent.ksql.schema.ksql.types.SqlLambdaResolved) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) SqlArray(io.confluent.ksql.schema.ksql.types.SqlArray)

Example 92 with SqlType

use of io.confluent.ksql.schema.ksql.types.SqlType in project ksql by confluentinc.

the class ParamTypes method areCompatible.

// CHECKSTYLE_RULES.OFF: CyclomaticComplexity
// CHECKSTYLE_RULES.OFF: NPathComplexity
public static boolean areCompatible(final SqlArgument argument, final ParamType declared, final boolean allowCast) {
    // CHECKSTYLE_RULES.ON: CyclomaticComplexity
    // CHECKSTYLE_RULES.ON: NPathComplexity
    final Optional<SqlLambda> sqlLambdaOptional = argument.getSqlLambda();
    if (sqlLambdaOptional.isPresent() && declared instanceof LambdaType) {
        final LambdaType declaredLambda = (LambdaType) declared;
        final SqlLambda sqlLambda = sqlLambdaOptional.get();
        if (sqlLambda instanceof SqlLambdaResolved) {
            final SqlLambdaResolved sqlLambdaResolved = (SqlLambdaResolved) sqlLambda;
            if (sqlLambdaResolved.getInputType().size() != declaredLambda.inputTypes().size()) {
                return false;
            }
            int i = 0;
            for (final ParamType paramType : declaredLambda.inputTypes()) {
                if (!areCompatible(SqlArgument.of(sqlLambdaResolved.getInputType().get(i)), paramType, allowCast)) {
                    return false;
                }
                i++;
            }
            return areCompatible(SqlArgument.of(sqlLambdaResolved.getReturnType()), declaredLambda.returnType(), allowCast);
        } else {
            return sqlLambda.getNumInputs() == declaredLambda.inputTypes().size();
        }
    }
    if (argument.getSqlIntervalUnit().isPresent() && declared instanceof IntervalUnitType) {
        return true;
    } else if (argument.getSqlIntervalUnit().isPresent() || declared instanceof IntervalUnitType) {
        return false;
    }
    final SqlType argumentSqlType = argument.getSqlTypeOrThrow();
    if (argumentSqlType.baseType() == SqlBaseType.ARRAY && declared instanceof ArrayType) {
        return areCompatible(SqlArgument.of(((SqlArray) argumentSqlType).getItemType()), ((ArrayType) declared).element(), allowCast);
    }
    if (argumentSqlType.baseType() == SqlBaseType.MAP && declared instanceof MapType) {
        final SqlMap sqlType = (SqlMap) argumentSqlType;
        final MapType mapType = (MapType) declared;
        return areCompatible(SqlArgument.of(sqlType.getKeyType()), mapType.key(), allowCast) && areCompatible(SqlArgument.of(sqlType.getValueType()), mapType.value(), allowCast);
    }
    if (argumentSqlType.baseType() == SqlBaseType.STRUCT && declared instanceof StructType) {
        return isStructCompatible(argumentSqlType, declared);
    }
    return isPrimitiveMatch(argumentSqlType, declared, allowCast);
}
Also used : SqlMap(io.confluent.ksql.schema.ksql.types.SqlMap) SqlLambda(io.confluent.ksql.schema.ksql.types.SqlLambda) SqlLambdaResolved(io.confluent.ksql.schema.ksql.types.SqlLambdaResolved) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) SqlArray(io.confluent.ksql.schema.ksql.types.SqlArray)

Example 93 with SqlType

use of io.confluent.ksql.schema.ksql.types.SqlType in project ksql by confluentinc.

the class SqlTypeWalker method visitStruct.

private static <S, F> S visitStruct(final SqlTypeWalker.Visitor<S, F> visitor, final SqlType type) {
    final SqlStruct struct = (SqlStruct) type;
    final List<F> fields = struct.fields().stream().map(field -> visitField(visitor, field)).collect(Collectors.toList());
    return visitor.visitStruct(struct, fields);
}
Also used : SqlStruct(io.confluent.ksql.schema.ksql.types.SqlStruct) Field(io.confluent.ksql.schema.ksql.types.SqlStruct.Field) ImmutableMap(com.google.common.collect.ImmutableMap) SqlPrimitiveType(io.confluent.ksql.schema.ksql.types.SqlPrimitiveType) BiFunction(java.util.function.BiFunction) SqlArray(io.confluent.ksql.schema.ksql.types.SqlArray) SqlMap(io.confluent.ksql.schema.ksql.types.SqlMap) Collectors(java.util.stream.Collectors) SqlBaseType(io.confluent.ksql.schema.ksql.types.SqlBaseType) List(java.util.List) SqlDecimal(io.confluent.ksql.schema.ksql.types.SqlDecimal) Map(java.util.Map) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) SqlStruct(io.confluent.ksql.schema.ksql.types.SqlStruct)

Example 94 with SqlType

use of io.confluent.ksql.schema.ksql.types.SqlType in project ksql by confluentinc.

the class OperatorTest method getType.

private static SqlType getType(final SqlBaseType baseType) {
    final SqlType type = TYPES.get(baseType);
    assertThat("invalid test: need type for base type:" + baseType, type, is(notNullValue()));
    return type;
}
Also used : SqlType(io.confluent.ksql.schema.ksql.types.SqlType)

Example 95 with SqlType

use of io.confluent.ksql.schema.ksql.types.SqlType in project ksql by confluentinc.

the class FunctionArgumentsUtil method firstPassOverFunctionArguments.

private static List<SqlArgument> firstPassOverFunctionArguments(final List<Expression> arguments, final ExpressionTypeManager expressionTypeManager, final Map<String, SqlType> lambdaMapping) {
    final List<SqlArgument> functionArgumentTypes = new ArrayList<>();
    for (final Expression expression : arguments) {
        if (expression instanceof LambdaFunctionCall) {
            functionArgumentTypes.add(SqlArgument.of(SqlLambda.of(((LambdaFunctionCall) expression).getArguments().size())));
        } else {
            final SqlType resolvedArgType = expressionTypeManager.getExpressionSqlType(expression, new HashMap<>(lambdaMapping));
            functionArgumentTypes.add(SqlArgument.of(resolvedArgType));
        }
    }
    return functionArgumentTypes;
}
Also used : SqlArgument(io.confluent.ksql.schema.ksql.SqlArgument) Expression(io.confluent.ksql.execution.expression.tree.Expression) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) ArrayList(java.util.ArrayList) SqlType(io.confluent.ksql.schema.ksql.types.SqlType)

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