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();
}
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();
}
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);
}
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);
}
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);
}
Aggregations