Search in sources :

Example 6 with SqlArray

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

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

the class SqlTypeWalker method visitArray.

private static <S, F> S visitArray(final SqlTypeWalker.Visitor<S, F> visitor, final SqlType type) {
    final SqlArray array = (SqlArray) type;
    final S element = visit(array.getItemType(), visitor);
    return visitor.visitArray(array, element);
}
Also used : SqlArray(io.confluent.ksql.schema.ksql.types.SqlArray)

Example 8 with SqlArray

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

the class SqlTypeWalkerTest method shouldVisitArray.

@Test
public void shouldVisitArray() {
    // Given:
    final SqlArray type = SqlTypes.array(SqlTypes.BIGINT);
    when(visitor.visitBigInt(any())).thenReturn("Expected-element");
    when(visitor.visitArray(any(), any())).thenReturn("Expected");
    // When:
    final String result = SqlTypeWalker.visit(type, visitor);
    // Then:
    verify(visitor).visitBigInt(same(SqlTypes.BIGINT));
    verify(visitor).visitArray(same(type), eq("Expected-element"));
    assertThat(result, is("Expected"));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) SqlArray(io.confluent.ksql.schema.ksql.types.SqlArray) Test(org.junit.Test)

Aggregations

SqlArray (io.confluent.ksql.schema.ksql.types.SqlArray)8 SqlMap (io.confluent.ksql.schema.ksql.types.SqlMap)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 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 IsNullTerm (io.confluent.ksql.execution.interpreter.terms.IsNullTerm)1