Search in sources :

Example 6 with SqlMap

use of io.confluent.ksql.schema.ksql.types.SqlMap 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 7 with SqlMap

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

the class TermCompiler method visitSubscriptExpression.

@Override
public Term visitSubscriptExpression(final SubscriptExpression node, final Context context) {
    final SqlType internalSchema = expressionTypeManager.getExpressionSqlType(node.getBase(), context.getLambdaSqlTypeMapping());
    switch(internalSchema.baseType()) {
        case ARRAY:
            final SqlArray array = (SqlArray) internalSchema;
            final Term listTerm = process(node.getBase(), context);
            final Term indexTerm = process(node.getIndex(), context);
            return new SubscriptTerm(listTerm, indexTerm, (o, index) -> ArrayAccess.arrayAccess(((List<?>) o), (Integer) index), array.getItemType());
        case MAP:
            final SqlMap mapSchema = (SqlMap) internalSchema;
            final Term mapTerm = process(node.getBase(), context);
            final Term keyTerm = process(node.getIndex(), context);
            return new SubscriptTerm(mapTerm, keyTerm, (map, key) -> ((Map<?, ?>) map).get(key), mapSchema.getValueType());
        default:
            throw new UnsupportedOperationException();
    }
}
Also used : SqlMap(io.confluent.ksql.schema.ksql.types.SqlMap) 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) SqlArray(io.confluent.ksql.schema.ksql.types.SqlArray) SubscriptTerm(io.confluent.ksql.execution.interpreter.terms.SubscriptTerm)

Example 8 with SqlMap

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

the class CastEvaluator method castMapToMap.

private static String castMapToMap(final String innerCode, final SqlType from, final SqlType to, final KsqlConfig config) {
    final SqlMap fromMap = (SqlMap) from;
    final SqlMap toMap = (SqlMap) to;
    try {
        return "CastEvaluator.castMap(" + innerCode + ", " + mapperFunction(fromMap.getKeyType(), toMap.getKeyType(), config) + ", " + mapperFunction(fromMap.getValueType(), toMap.getValueType(), config) + ")";
    } catch (final UnsupportedCastException e) {
        throw new UnsupportedCastException(from, to, e);
    }
}
Also used : SqlMap(io.confluent.ksql.schema.ksql.types.SqlMap)

Aggregations

SqlMap (io.confluent.ksql.schema.ksql.types.SqlMap)8 SqlArray (io.confluent.ksql.schema.ksql.types.SqlArray)4 SqlType (io.confluent.ksql.schema.ksql.types.SqlType)4 Term (io.confluent.ksql.execution.interpreter.terms.Term)2 SqlLambda (io.confluent.ksql.schema.ksql.types.SqlLambda)2 SqlLambdaResolved (io.confluent.ksql.schema.ksql.types.SqlLambdaResolved)2 KsqlException (io.confluent.ksql.util.KsqlException)2 Test (org.junit.Test)2 CastEvaluator (io.confluent.ksql.execution.codegen.helpers.CastEvaluator)1 Type (io.confluent.ksql.execution.expression.tree.Type)1 CastTerm (io.confluent.ksql.execution.interpreter.terms.CastTerm)1 CastFunction (io.confluent.ksql.execution.interpreter.terms.CastTerm.CastFunction)1 ComparableCastFunction (io.confluent.ksql.execution.interpreter.terms.CastTerm.ComparableCastFunction)1 ColumnReferenceTerm (io.confluent.ksql.execution.interpreter.terms.ColumnReferenceTerm)1 CreateArrayTerm (io.confluent.ksql.execution.interpreter.terms.CreateArrayTerm)1 CreateMapTerm (io.confluent.ksql.execution.interpreter.terms.CreateMapTerm)1 DereferenceTerm (io.confluent.ksql.execution.interpreter.terms.DereferenceTerm)1 FunctionCallTerm (io.confluent.ksql.execution.interpreter.terms.FunctionCallTerm)1 InPredicateTerm (io.confluent.ksql.execution.interpreter.terms.InPredicateTerm)1 IsNotNullTerm (io.confluent.ksql.execution.interpreter.terms.IsNotNullTerm)1