Search in sources :

Example 6 with SelectExpression

use of io.confluent.ksql.execution.plan.SelectExpression in project ksql by confluentinc.

the class QueryProjectNode method buildOutputSchema.

/**
 * Builds the output schema of the project node.
 * The output schema comprises of exactly the columns that appear in the SELECT clause of the
 * query.
 * @param metaStore the metastore
 * @return the project node's output schema
 */
private LogicalSchema buildOutputSchema(final MetaStore metaStore) {
    final LogicalSchema outputSchema;
    final LogicalSchema parentSchema = getSource().getSchema();
    final boolean isWindowed = analysis.getFrom().getDataSource().getKsqlTopic().getKeyFormat().isWindowed();
    if (isSelectStar()) {
        outputSchema = buildPullQuerySelectStarSchema(parentSchema.withoutPseudoAndKeyColsInValue(ksqlConfig), isWindowed);
    } else {
        final List<SelectExpression> projects = projection.selectItems().stream().map(SingleColumn.class::cast).map(si -> SelectExpression.of(si.getAlias().orElseThrow(IllegalStateException::new), si.getExpression())).collect(Collectors.toList());
        outputSchema = selectOutputSchema(metaStore, projects, isWindowed);
    }
    if (isScalablePush) {
        // Transient queries return key columns in the value, so the projection includes them, and
        // the schema needs to include them too:
        final Builder builder = LogicalSchema.builder();
        outputSchema.columns().forEach(builder::valueColumn);
        return builder.build();
    }
    return outputSchema;
}
Also used : CodeGenRunner(io.confluent.ksql.execution.codegen.CodeGenRunner) SelectItem(io.confluent.ksql.parser.tree.SelectItem) RewrittenAnalysis(io.confluent.ksql.analyzer.RewrittenAnalysis) SingleColumn(io.confluent.ksql.parser.tree.SingleColumn) ImmutableList(com.google.common.collect.ImmutableList) ExpressionTypeManager(io.confluent.ksql.execution.util.ExpressionTypeManager) MetaStore(io.confluent.ksql.metastore.MetaStore) Projection(io.confluent.ksql.planner.Projection) AllColumns(io.confluent.ksql.parser.tree.AllColumns) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) InterpretedExpressionFactory(io.confluent.ksql.execution.interpreter.InterpretedExpressionFactory) SystemColumns(io.confluent.ksql.schema.ksql.SystemColumns) ExpressionEvaluator(io.confluent.ksql.execution.transform.ExpressionEvaluator) Expression(io.confluent.ksql.execution.expression.tree.Expression) KsqlConfig(io.confluent.ksql.util.KsqlConfig) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) Collectors(java.util.stream.Collectors) SelectExpression(io.confluent.ksql.execution.plan.SelectExpression) Builder(io.confluent.ksql.schema.ksql.LogicalSchema.Builder) Objects(java.util.Objects) List(java.util.List) QueryPlannerOptions(io.confluent.ksql.planner.QueryPlannerOptions) KsqlException(io.confluent.ksql.util.KsqlException) Optional(java.util.Optional) SqlTypes(io.confluent.ksql.schema.ksql.types.SqlTypes) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) Builder(io.confluent.ksql.schema.ksql.LogicalSchema.Builder) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) SelectExpression(io.confluent.ksql.execution.plan.SelectExpression) SingleColumn(io.confluent.ksql.parser.tree.SingleColumn)

Example 7 with SelectExpression

use of io.confluent.ksql.execution.plan.SelectExpression in project ksql by confluentinc.

the class SchemaKStreamTest method shouldBuildSchemaForSelect.

@Test
public void shouldBuildSchemaForSelect() {
    // Given:
    final PlanNode logicalPlan = givenInitialKStreamOf("SELECT col0 AS K, col2, col3 FROM test1 WHERE col0 > 100 EMIT CHANGES;");
    final ProjectNode projectNode = (ProjectNode) logicalPlan.getSources().get(0);
    final List<SelectExpression> selectExpressions = projectNode.getSelectExpressions();
    // When:
    final SchemaKStream<?> projectedSchemaKStream = initialSchemaKStream.select(ImmutableList.of(ColumnName.of("K")), selectExpressions, childContextStacker, buildContext, internalFormats);
    // Then:
    assertThat(projectedSchemaKStream.getSchema(), is(schemaResolver.resolve(projectedSchemaKStream.getSourceStep(), initialSchemaKStream.schema)));
}
Also used : PlanNode(io.confluent.ksql.planner.plan.PlanNode) ProjectNode(io.confluent.ksql.planner.plan.ProjectNode) SelectExpression(io.confluent.ksql.execution.plan.SelectExpression) Test(org.junit.Test)

Example 8 with SelectExpression

use of io.confluent.ksql.execution.plan.SelectExpression 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();
}
Also used : ExpressionTypeManager(io.confluent.ksql.execution.util.ExpressionTypeManager) Builder(io.confluent.ksql.schema.ksql.LogicalSchema.Builder) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) SelectExpression(io.confluent.ksql.execution.plan.SelectExpression)

Example 9 with SelectExpression

use of io.confluent.ksql.execution.plan.SelectExpression 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();
}
Also used : JoinInfo(io.confluent.ksql.analyzer.Analysis.JoinInfo) DataSource(io.confluent.ksql.metastore.model.DataSource) Leaf(io.confluent.ksql.planner.JoinTree.Leaf) AggregateAnalysisResult(io.confluent.ksql.analyzer.AggregateAnalysisResult) Into(io.confluent.ksql.analyzer.Analysis.Into) ColumnName(io.confluent.ksql.name.ColumnName) SourceName(io.confluent.ksql.name.SourceName) BiFunction(java.util.function.BiFunction) AggregateAnalyzer(io.confluent.ksql.analyzer.AggregateAnalyzer) FilterNode(io.confluent.ksql.planner.plan.FilterNode) SerdeFeaturesFactory(io.confluent.ksql.serde.SerdeFeaturesFactory) JoinKey(io.confluent.ksql.planner.plan.JoinNode.JoinKey) CodeGenRunner(io.confluent.ksql.execution.codegen.CodeGenRunner) WindowInfo(io.confluent.ksql.serde.WindowInfo) RewrittenAnalysis(io.confluent.ksql.analyzer.RewrittenAnalysis) QueryLimitNode(io.confluent.ksql.planner.plan.QueryLimitNode) AggregateNode(io.confluent.ksql.planner.plan.AggregateNode) AliasedDataSource(io.confluent.ksql.analyzer.Analysis.AliasedDataSource) TimestampExtractionPolicyFactory(io.confluent.ksql.execution.streams.timestamp.TimestampExtractionPolicyFactory) ExpressionTypeManager(io.confluent.ksql.execution.util.ExpressionTypeManager) KsqlBareOutputNode(io.confluent.ksql.planner.plan.KsqlBareOutputNode) SelectionUtil(io.confluent.ksql.planner.plan.SelectionUtil) PartitionBy(io.confluent.ksql.parser.tree.PartitionBy) ColumnReferenceExp(io.confluent.ksql.execution.expression.tree.ColumnReferenceExp) PreJoinProjectNode(io.confluent.ksql.planner.plan.PreJoinProjectNode) VisitParentExpressionVisitor(io.confluent.ksql.execution.expression.tree.VisitParentExpressionVisitor) FinalProjectNode(io.confluent.ksql.planner.plan.FinalProjectNode) ColumnNames(io.confluent.ksql.schema.ksql.ColumnNames) RefinementInfo(io.confluent.ksql.serde.RefinementInfo) ImmutableAnalysis(io.confluent.ksql.analyzer.ImmutableAnalysis) ExpressionEvaluator(io.confluent.ksql.execution.transform.ExpressionEvaluator) Expression(io.confluent.ksql.execution.expression.tree.Expression) JoinType(io.confluent.ksql.planner.plan.JoinNode.JoinType) Set(java.util.Set) QueryFilterNode(io.confluent.ksql.planner.plan.QueryFilterNode) KsqlConfig(io.confluent.ksql.util.KsqlConfig) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) Collectors(java.util.stream.Collectors) TimestampColumn(io.confluent.ksql.execution.timestamp.TimestampColumn) PlanNodeId(io.confluent.ksql.planner.plan.PlanNodeId) SingleSourcePlanNode(io.confluent.ksql.planner.plan.SingleSourcePlanNode) Builder(io.confluent.ksql.schema.ksql.LogicalSchema.Builder) Objects(java.util.Objects) Join(io.confluent.ksql.planner.JoinTree.Join) List(java.util.List) KsqlException(io.confluent.ksql.util.KsqlException) Optional(java.util.Optional) QueryProjectNode(io.confluent.ksql.planner.plan.QueryProjectNode) Column(io.confluent.ksql.schema.ksql.Column) FormatInfo(io.confluent.ksql.serde.FormatInfo) ProjectNode(io.confluent.ksql.planner.plan.ProjectNode) Iterables(com.google.common.collect.Iterables) FormatFactory(io.confluent.ksql.serde.FormatFactory) GrammaticalJoiner(io.confluent.ksql.util.GrammaticalJoiner) KeyFormat(io.confluent.ksql.serde.KeyFormat) JoinNode(io.confluent.ksql.planner.plan.JoinNode) SuppressNode(io.confluent.ksql.planner.plan.SuppressNode) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp) PartitionByParamsFactory(io.confluent.ksql.execution.streams.PartitionByParamsFactory) DataSourceType(io.confluent.ksql.metastore.model.DataSource.DataSourceType) Function(java.util.function.Function) NoneFormat(io.confluent.ksql.serde.none.NoneFormat) AsValue(io.confluent.ksql.function.udf.AsValue) QualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.QualifiedColumnReferenceExp) MetaStore(io.confluent.ksql.metastore.MetaStore) GroupBy(io.confluent.ksql.parser.tree.GroupBy) KsqlStructuredDataOutputNode(io.confluent.ksql.planner.plan.KsqlStructuredDataOutputNode) UserRepartitionNode(io.confluent.ksql.planner.plan.UserRepartitionNode) WindowExpression(io.confluent.ksql.parser.tree.WindowExpression) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) SerdeFeatures(io.confluent.ksql.serde.SerdeFeatures) DataSourceNode(io.confluent.ksql.planner.plan.DataSourceNode) NewTopic(io.confluent.ksql.analyzer.Analysis.Into.NewTopic) KsqlWindowExpression(io.confluent.ksql.execution.windows.KsqlWindowExpression) OutputNode(io.confluent.ksql.planner.plan.OutputNode) FilterTypeValidator(io.confluent.ksql.analyzer.FilterTypeValidator) FlatMapNode(io.confluent.ksql.planner.plan.FlatMapNode) ValueFormat(io.confluent.ksql.serde.ValueFormat) SelectExpression(io.confluent.ksql.execution.plan.SelectExpression) NodeLocation(io.confluent.ksql.parser.NodeLocation) PreJoinRepartitionNode(io.confluent.ksql.planner.plan.PreJoinRepartitionNode) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) KsqlTopic(io.confluent.ksql.execution.ddl.commands.KsqlTopic) PlanNode(io.confluent.ksql.planner.plan.PlanNode) Context(io.confluent.ksql.engine.rewrite.ExpressionTreeRewriter.Context) ExpressionTreeRewriter(io.confluent.ksql.engine.rewrite.ExpressionTreeRewriter) OutputRefinement(io.confluent.ksql.parser.OutputRefinement) FilterType(io.confluent.ksql.analyzer.FilterTypeValidator.FilterType) Collections(java.util.Collections) ExpressionTypeManager(io.confluent.ksql.execution.util.ExpressionTypeManager) Optional(java.util.Optional) Builder(io.confluent.ksql.schema.ksql.LogicalSchema.Builder) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) SelectExpression(io.confluent.ksql.execution.plan.SelectExpression) KsqlException(io.confluent.ksql.util.KsqlException) ColumnName(io.confluent.ksql.name.ColumnName) ColumnReferenceExp(io.confluent.ksql.execution.expression.tree.ColumnReferenceExp) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp) QualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.QualifiedColumnReferenceExp) Expression(io.confluent.ksql.execution.expression.tree.Expression) WindowExpression(io.confluent.ksql.parser.tree.WindowExpression) KsqlWindowExpression(io.confluent.ksql.execution.windows.KsqlWindowExpression) SelectExpression(io.confluent.ksql.execution.plan.SelectExpression) TimestampColumn(io.confluent.ksql.execution.timestamp.TimestampColumn) Column(io.confluent.ksql.schema.ksql.Column) List(java.util.List) SqlType(io.confluent.ksql.schema.ksql.types.SqlType)

Example 10 with SelectExpression

use of io.confluent.ksql.execution.plan.SelectExpression in project ksql by confluentinc.

the class LogicalPlanner method buildAggregateNode.

private AggregateNode buildAggregateNode(final PlanNode sourcePlanNode) {
    final GroupBy groupBy = analysis.getGroupBy().orElseThrow(IllegalStateException::new);
    final List<SelectExpression> projectionExpressions = SelectionUtil.buildSelectExpressions(sourcePlanNode, analysis.getSelectItems(), getTargetSchema());
    final RewrittenAggregateAnalysis aggregateAnalysis = new RewrittenAggregateAnalysis(aggregateAnalyzer.analyze(analysis, projectionExpressions), refRewriter::process);
    final LogicalSchema schema = buildAggregateSchema(sourcePlanNode, groupBy, projectionExpressions);
    if (analysis.getHavingExpression().isPresent()) {
        final FilterTypeValidator validator = new FilterTypeValidator(sourcePlanNode.getSchema(), metaStore, FilterType.HAVING);
        validator.validateFilterExpression(analysis.getHavingExpression().get());
    }
    return new AggregateNode(new PlanNodeId("Aggregate"), sourcePlanNode, schema, groupBy, metaStore, analysis, aggregateAnalysis, projectionExpressions, analysis.getInto().isPresent(), ksqlConfig);
}
Also used : PlanNodeId(io.confluent.ksql.planner.plan.PlanNodeId) GroupBy(io.confluent.ksql.parser.tree.GroupBy) AggregateNode(io.confluent.ksql.planner.plan.AggregateNode) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) SelectExpression(io.confluent.ksql.execution.plan.SelectExpression) FilterTypeValidator(io.confluent.ksql.analyzer.FilterTypeValidator)

Aggregations

SelectExpression (io.confluent.ksql.execution.plan.SelectExpression)12 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)9 Builder (io.confluent.ksql.schema.ksql.LogicalSchema.Builder)7 ExpressionTypeManager (io.confluent.ksql.execution.util.ExpressionTypeManager)6 SqlType (io.confluent.ksql.schema.ksql.types.SqlType)6 List (java.util.List)6 Optional (java.util.Optional)6 Collectors (java.util.stream.Collectors)6 ColumnReferenceExp (io.confluent.ksql.execution.expression.tree.ColumnReferenceExp)5 Expression (io.confluent.ksql.execution.expression.tree.Expression)5 UnqualifiedColumnReferenceExp (io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp)5 ColumnName (io.confluent.ksql.name.ColumnName)5 Column (io.confluent.ksql.schema.ksql.Column)5 Set (java.util.Set)5 SelectItem (io.confluent.ksql.parser.tree.SelectItem)4 AllColumns (io.confluent.ksql.parser.tree.AllColumns)3 SingleColumn (io.confluent.ksql.parser.tree.SingleColumn)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 ImmutableList (com.google.common.collect.ImmutableList)2