Search in sources :

Example 1 with SqlArray

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

the class ExpressionFormatterTest method shouldFormatArray.

@Test
public void shouldFormatArray() {
    final SqlArray array = SqlTypes.array(SqlTypes.BOOLEAN);
    assertThat(ExpressionFormatter.formatExpression(new Type(array)), equalTo("ARRAY<BOOLEAN>"));
}
Also used : Type(io.confluent.ksql.execution.expression.tree.Type) SqlArray(io.confluent.ksql.schema.ksql.types.SqlArray) Test(org.junit.Test)

Example 2 with SqlArray

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

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

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

the class CastEvaluator method castArrayToArray.

private static String castArrayToArray(final String innerCode, final SqlType from, final SqlType to, final KsqlConfig config) {
    final SqlArray fromArray = (SqlArray) from;
    final SqlArray toArray = (SqlArray) to;
    try {
        return "CastEvaluator.castArray(" + innerCode + ", " + mapperFunction(fromArray.getItemType(), toArray.getItemType(), config) + ")";
    } catch (final UnsupportedCastException e) {
        throw new UnsupportedCastException(from, to, e);
    }
}
Also used : SqlArray(io.confluent.ksql.schema.ksql.types.SqlArray)

Example 5 with SqlArray

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

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