use of io.confluent.ksql.execution.util.ExpressionTypeManager in project ksql by confluentinc.
the class TermCompiler method visitInPredicate.
@Override
public Term visitInPredicate(final InPredicate inPredicate, final Context context) {
final InPredicate preprocessed = InListEvaluator.preprocess(inPredicate, expressionTypeManager, context.getLambdaSqlTypeMapping());
final Term value = process(preprocessed.getValue(), context);
final List<Term> valueList = preprocessed.getValueList().getValues().stream().map(v -> process(v, context)).collect(ImmutableList.toImmutableList());
return new InPredicateTerm(value, valueList);
}
use of io.confluent.ksql.execution.util.ExpressionTypeManager in project ksql by confluentinc.
the class UdafUtil method resolveAggregateFunction.
public static KsqlAggregateFunction<?, ?, ?> resolveAggregateFunction(final FunctionRegistry functionRegistry, final FunctionCall functionCall, final LogicalSchema schema, final KsqlConfig config) {
try {
final ExpressionTypeManager expressionTypeManager = new ExpressionTypeManager(schema, functionRegistry);
final SqlType argumentType = expressionTypeManager.getExpressionSqlType(functionCall.getArguments().get(0));
// UDAFs only support one non-constant argument, and that argument must be a column reference
final Expression arg = functionCall.getArguments().get(0);
final Optional<Column> possibleValueColumn = arg instanceof UnqualifiedColumnReferenceExp ? schema.findValueColumn(((UnqualifiedColumnReferenceExp) arg).getColumnName()) : // assume that it is a column reference with no alias
schema.findValueColumn(ColumnName.of(arg.toString()));
final Column valueColumn = possibleValueColumn.orElseThrow(() -> new KsqlException("Could not find column for expression: " + arg));
final AggregateFunctionInitArguments aggregateFunctionInitArguments = createAggregateFunctionInitArgs(valueColumn.index(), functionCall, config);
return functionRegistry.getAggregateFunction(functionCall.getName(), argumentType, aggregateFunctionInitArguments);
} catch (final Exception e) {
throw new KsqlException("Failed to create aggregate function: " + functionCall, e);
}
}
use of io.confluent.ksql.execution.util.ExpressionTypeManager in project ksql by confluentinc.
the class InterpretedExpressionFactory method create.
@VisibleForTesting
public static InterpretedExpression create(final Expression expression, final LogicalSchema schema, final FunctionRegistry functionRegistry, final KsqlConfig ksqlConfig, final Context context) {
try {
final ExpressionTypeManager expressionTypeManager = new ExpressionTypeManager(schema, functionRegistry);
final SqlType returnType = expressionTypeManager.getExpressionSqlType(expression, context.getLambdaSqlTypeMapping());
if (returnType == null) {
// practice.
throw new KsqlException("NULL expression not supported");
}
final Term term = new TermCompiler(functionRegistry, schema, ksqlConfig, expressionTypeManager).process(expression, context);
return new InterpretedExpression(expression, returnType, term);
} catch (KsqlException e) {
throw new KsqlException("Invalid expression: " + e.getMessage() + ". expression: " + expression + ", schema:" + schema, e);
} catch (final Exception e) {
throw new RuntimeException("Unexpected error generating code for expression: " + expression, e);
}
}
use of io.confluent.ksql.execution.util.ExpressionTypeManager in project ksql by confluentinc.
the class LogicalPlanner method buildAggregateSchema.
private LogicalSchema buildAggregateSchema(final PlanNode sourcePlanNode, final GroupBy groupBy, final List<SelectExpression> projectionExpressions) {
final LogicalSchema sourceSchema = sourcePlanNode.getSchema();
final LogicalSchema projectionSchema = SelectionUtil.buildProjectionSchema(sourceSchema.withPseudoAndKeyColsInValue(analysis.getWindowExpression().isPresent(), ksqlConfig), projectionExpressions, metaStore);
final List<Expression> groupByExps = groupBy.getGroupingExpressions();
final Function<Expression, Optional<ColumnName>> selectResolver = expression -> {
final List<ColumnName> foundInProjection = projectionExpressions.stream().filter(e -> e.getExpression().equals(expression)).map(SelectExpression::getAlias).collect(Collectors.toList());
switch(foundInProjection.size()) {
case 0:
return Optional.empty();
case 1:
return Optional.of(foundInProjection.get(0));
default:
final String keys = GrammaticalJoiner.and().join(foundInProjection);
throw new KsqlException("The projection contains a key column more than once: " + keys + "." + System.lineSeparator() + "Each key column must only be in the projection once. " + "If you intended to copy the key into the value, then consider using the " + AsValue.NAME + " function to indicate which key reference should be copied.");
}
};
final List<Column> valueColumns;
if (analysis.getInto().isPresent()) {
// Persistent query:
final Set<ColumnName> keyColumnNames = groupBy.getGroupingExpressions().stream().map(selectResolver).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toSet());
valueColumns = projectionSchema.value().stream().filter(col -> !keyColumnNames.contains(col.name())).collect(Collectors.toList());
if (valueColumns.isEmpty()) {
throw new KsqlException("The projection contains no value columns.");
}
} else {
// Transient query:
// Transient queries only return value columns, so must have key columns in the value:
valueColumns = projectionSchema.columns();
}
final Builder builder = LogicalSchema.builder();
final ExpressionTypeManager typeManager = new ExpressionTypeManager(sourceSchema, metaStore);
for (final Expression expression : groupByExps) {
final SqlType keyType = typeManager.getExpressionSqlType(expression);
final ColumnName keyName = selectResolver.apply(expression).orElseGet(() -> expression instanceof ColumnReferenceExp ? ((ColumnReferenceExp) expression).getColumnName() : ColumnNames.uniqueAliasFor(expression, sourceSchema));
builder.keyColumn(keyName, keyType);
}
return builder.valueColumns(valueColumns).build();
}
use of io.confluent.ksql.execution.util.ExpressionTypeManager in project ksql by confluentinc.
the class QueryProjectNode method selectOutputSchema.
private LogicalSchema selectOutputSchema(final MetaStore metaStore, final List<SelectExpression> selectExpressions, final boolean isWindowed) {
final Builder schemaBuilder = LogicalSchema.builder();
final LogicalSchema parentSchema = getSource().getSchema();
// Copy meta & key columns into the value schema as SelectValueMapper expects it:
final LogicalSchema schema = parentSchema.withPseudoAndKeyColsInValue(isWindowed, ksqlConfig);
final ExpressionTypeManager expressionTypeManager = new ExpressionTypeManager(schema, metaStore);
for (final SelectExpression select : selectExpressions) {
final SqlType type = expressionTypeManager.getExpressionSqlType(select.getExpression());
if (parentSchema.isKeyColumn(select.getAlias()) || select.getAlias().equals(SystemColumns.WINDOWSTART_NAME) || select.getAlias().equals(SystemColumns.WINDOWEND_NAME)) {
schemaBuilder.keyColumn(select.getAlias(), type);
} else {
schemaBuilder.valueColumn(select.getAlias(), type);
}
}
return schemaBuilder.build();
}
Aggregations