Search in sources :

Example 51 with SqlType

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

the class JoinParamsFactory method createSchema.

public static LogicalSchema createSchema(final ColumnName keyColName, final LogicalSchema leftSchema, final LogicalSchema rightSchema) {
    final SqlType keyType = throwOnKeyMismatch(leftSchema, rightSchema);
    final Builder builder = LogicalSchema.builder().keyColumn(keyColName, keyType).valueColumns(leftSchema.value()).valueColumns(rightSchema.value());
    if (neitherContain(keyColName, leftSchema, rightSchema)) {
        // Append key to value so its accessible during processing:
        builder.valueColumn(keyColName, keyType);
    }
    return builder.build();
}
Also used : Builder(io.confluent.ksql.schema.ksql.LogicalSchema.Builder) SqlType(io.confluent.ksql.schema.ksql.types.SqlType)

Example 52 with SqlType

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

the class AggregateParamsFactory method buildSchema.

private static LogicalSchema buildSchema(final LogicalSchema schema, final List<ColumnName> nonAggregateColumns, final List<KsqlAggregateFunction<?, ?, ?>> aggregateFunctions, final boolean useAggregate, final boolean addWindowBounds) {
    final LogicalSchema.Builder schemaBuilder = LogicalSchema.builder();
    schemaBuilder.keyColumns(schema.key());
    for (final ColumnName columnName : nonAggregateColumns) {
        final Column col = schema.findValueColumn(columnName).orElseThrow(IllegalArgumentException::new);
        schemaBuilder.valueColumn(col);
    }
    for (int i = 0; i < aggregateFunctions.size(); i++) {
        final KsqlAggregateFunction<?, ?, ?> aggregateFunction = aggregateFunctions.get(i);
        final ColumnName colName = ColumnNames.aggregateColumn(i);
        final SqlType fieldType = useAggregate ? aggregateFunction.getAggregateType() : aggregateFunction.returnType();
        schemaBuilder.valueColumn(colName, fieldType);
    }
    if (addWindowBounds) {
        // Add window bounds columns, as populated by WindowBoundsPopulator
        schemaBuilder.valueColumn(SystemColumns.WINDOWSTART_NAME, SystemColumns.WINDOWBOUND_TYPE);
        schemaBuilder.valueColumn(SystemColumns.WINDOWEND_NAME, SystemColumns.WINDOWBOUND_TYPE);
    }
    return schemaBuilder.build();
}
Also used : ColumnName(io.confluent.ksql.name.ColumnName) Column(io.confluent.ksql.schema.ksql.Column) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) SqlType(io.confluent.ksql.schema.ksql.types.SqlType)

Example 53 with SqlType

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

the class GenericsUtil method reserveGenerics.

/**
 * 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}. Adds the mapping to an
 * existing mapping passed into the function
 *
 * @param schema    the schema that may contain generics
 * @param instance  a schema with the same structure as {@code schema} but with no generics
 * @param reservedGenerics  mapping of generic type to resolved type
 *
 * @return if the mapping succeeded and if it failed, an exception with why it failed
 */
public static Pair<Boolean, Optional<KsqlException>> reserveGenerics(final ParamType schema, final SqlArgument instance, final Map<GenericType, SqlType> reservedGenerics) {
    final List<Entry<GenericType, SqlType>> genericMapping = new ArrayList<>();
    final boolean success = resolveGenerics(genericMapping, schema, instance);
    if (!success) {
        return new Pair<>(false, Optional.of(new KsqlException(String.format("Cannot infer generics for %s from %s because " + "they do not have the same schema structure.", schema, instance))));
    }
    for (final Entry<GenericType, SqlType> entry : genericMapping) {
        final SqlType old = reservedGenerics.putIfAbsent(entry.getKey(), entry.getValue());
        if (old != null && !old.equals(entry.getValue())) {
            return new Pair<>(false, Optional.of(new KsqlException(String.format("Found invalid instance of generic schema when mapping %s to %s. " + "Cannot map %s to both %s and %s", schema, instance, entry.getKey(), old, entry.getValue()))));
        }
    }
    return new Pair<>(true, null);
}
Also used : Entry(java.util.Map.Entry) GenericType(io.confluent.ksql.function.types.GenericType) ArrayList(java.util.ArrayList) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) KsqlException(io.confluent.ksql.util.KsqlException) Pair(io.confluent.ksql.util.Pair)

Example 54 with SqlType

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

the class GenericsUtil method applyResolved.

/**
 * Replaces all generics in a schema with concrete schemas defined in {@code resolved}
 *
 * @param schema    the schema which may contain generics
 * @param resolved  the mapping from generics to resolved types
 * @return a schema with the same structure as {@code schema} but with no generics
 *
 * @throws KsqlException if there is a generic in {@code schema} that is not present
 *                       in {@code mapping}
 */
public static SqlType applyResolved(final ParamType schema, final Map<GenericType, SqlType> resolved) {
    if (schema instanceof ArrayType) {
        return SqlTypes.array(applyResolved(((ArrayType) schema).element(), resolved));
    }
    if (schema instanceof MapType) {
        final MapType mapType = (MapType) schema;
        final SqlType keyType = applyResolved(mapType.key(), resolved);
        final SqlType valueType = applyResolved(mapType.value(), resolved);
        return SqlTypes.map(keyType, valueType);
    }
    if (schema instanceof StructType) {
        final Builder struct = SqlTypes.struct();
        ((StructType) schema).getSchema().forEach((fieldName, type) -> struct.field(fieldName, applyResolved(type, resolved)));
        return struct.build();
    }
    if (schema instanceof GenericType) {
        final SqlType instance = resolved.get(schema);
        if (instance == null) {
            throw new KsqlException("Could not find mapping for generic type: " + schema);
        }
        return instance;
    }
    return SchemaConverters.functionToSqlConverter().toSqlType(schema);
}
Also used : ArrayType(io.confluent.ksql.function.types.ArrayType) GenericType(io.confluent.ksql.function.types.GenericType) StructType(io.confluent.ksql.function.types.StructType) Builder(io.confluent.ksql.schema.ksql.types.SqlStruct.Builder) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) KsqlException(io.confluent.ksql.util.KsqlException) MapType(io.confluent.ksql.function.types.MapType)

Example 55 with SqlType

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

the class KsqlScalarFunction method createLegacyBuiltIn.

/**
 * Create built in / legacy function.
 */
public static KsqlScalarFunction createLegacyBuiltIn(final SqlType returnType, final List<ParamType> arguments, final FunctionName functionName, final Class<? extends Kudf> kudfClass) {
    final ParamType javaReturnType = SchemaConverters.sqlToFunctionConverter().toFunctionType(returnType);
    final List<ParameterInfo> paramInfos = arguments.stream().map(type -> new ParameterInfo("", type, "", false)).collect(Collectors.toList());
    return create((i1, i2) -> returnType, javaReturnType, paramInfos, functionName, kudfClass, // findbugs was complaining about a dead store so I inlined this
    ksqlConfig -> {
        try {
            return kudfClass.newInstance();
        } catch (final Exception e) {
            throw new KsqlException("Failed to create instance of kudfClass " + kudfClass + " for function " + functionName, e);
        }
    }, "", INTERNAL_PATH, false);
}
Also used : Kudf(io.confluent.ksql.function.udf.Kudf) FunctionName(io.confluent.ksql.name.FunctionName) ParamType(io.confluent.ksql.function.types.ParamType) KsqlConfig(io.confluent.ksql.util.KsqlConfig) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Immutable(com.google.errorprone.annotations.Immutable) KsqlException(io.confluent.ksql.util.KsqlException) SchemaConverters(io.confluent.ksql.schema.ksql.SchemaConverters) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) EffectivelyImmutable(io.confluent.ksql.testing.EffectivelyImmutable) KsqlException(io.confluent.ksql.util.KsqlException) ParamType(io.confluent.ksql.function.types.ParamType) KsqlException(io.confluent.ksql.util.KsqlException)

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