Search in sources :

Example 46 with ColumnName

use of io.confluent.ksql.name.ColumnName in project ksql by confluentinc.

the class CreateSourceFactory method buildTimestampColumn.

private static Optional<TimestampColumn> buildTimestampColumn(final KsqlConfig ksqlConfig, final CreateSourceProperties properties, final LogicalSchema schema) {
    final Optional<ColumnName> timestampName = properties.getTimestampColumnName();
    final Optional<TimestampColumn> timestampColumn = timestampName.map(n -> new TimestampColumn(n, properties.getTimestampFormat()));
    // create the final extraction policy to validate that the ref/format are OK
    TimestampExtractionPolicyFactory.validateTimestampColumn(ksqlConfig, schema, timestampColumn);
    return timestampColumn;
}
Also used : ColumnName(io.confluent.ksql.name.ColumnName) TimestampColumn(io.confluent.ksql.execution.timestamp.TimestampColumn)

Example 47 with ColumnName

use of io.confluent.ksql.name.ColumnName in project ksql by confluentinc.

the class SchemaKGroupedTable method aggregate.

@Override
public SchemaKTable<GenericKey> aggregate(final List<ColumnName> nonAggregateColumns, final List<FunctionCall> aggregations, final Optional<WindowExpression> windowExpression, final FormatInfo valueFormat, final Stacker contextStacker) {
    if (windowExpression.isPresent()) {
        throw new KsqlException("Windowing not supported for table aggregations.");
    }
    final List<String> unsupportedFunctionNames = aggregations.stream().map(call -> UdafUtil.resolveAggregateFunction(functionRegistry, call, schema, ksqlConfig)).filter(function -> !(function instanceof TableAggregationFunction)).map(KsqlAggregateFunction::name).map(FunctionName::text).distinct().collect(Collectors.toList());
    if (!unsupportedFunctionNames.isEmpty()) {
        final String postfix = unsupportedFunctionNames.size() == 1 ? "" : "s";
        throw new KsqlException("The aggregation function" + postfix + " " + GrammaticalJoiner.and().join(unsupportedFunctionNames) + " cannot be applied to a table source, only to a stream source.");
    }
    final TableAggregate step = ExecutionStepFactory.tableAggregate(contextStacker, sourceTableStep, InternalFormats.of(keyFormat, valueFormat), nonAggregateColumns, aggregations);
    return new SchemaKTable<>(step, resolveSchema(step), keyFormat, ksqlConfig, functionRegistry);
}
Also used : ColumnName(io.confluent.ksql.name.ColumnName) GrammaticalJoiner(io.confluent.ksql.util.GrammaticalJoiner) KeyFormat(io.confluent.ksql.serde.KeyFormat) KGroupedTableHolder(io.confluent.ksql.execution.plan.KGroupedTableHolder) KsqlAggregateFunction(io.confluent.ksql.function.KsqlAggregateFunction) WindowExpression(io.confluent.ksql.parser.tree.WindowExpression) ExecutionStepFactory(io.confluent.ksql.execution.streams.ExecutionStepFactory) ExecutionStep(io.confluent.ksql.execution.plan.ExecutionStep) FunctionName(io.confluent.ksql.name.FunctionName) FunctionRegistry(io.confluent.ksql.function.FunctionRegistry) TableAggregate(io.confluent.ksql.execution.plan.TableAggregate) KsqlConfig(io.confluent.ksql.util.KsqlConfig) InternalFormats(io.confluent.ksql.serde.InternalFormats) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) Collectors(java.util.stream.Collectors) UdafUtil(io.confluent.ksql.execution.function.UdafUtil) Objects(java.util.Objects) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) List(java.util.List) Stacker(io.confluent.ksql.execution.context.QueryContext.Stacker) TableAggregationFunction(io.confluent.ksql.execution.function.TableAggregationFunction) KsqlException(io.confluent.ksql.util.KsqlException) Optional(java.util.Optional) GenericKey(io.confluent.ksql.GenericKey) FormatInfo(io.confluent.ksql.serde.FormatInfo) FunctionName(io.confluent.ksql.name.FunctionName) TableAggregate(io.confluent.ksql.execution.plan.TableAggregate) TableAggregationFunction(io.confluent.ksql.execution.function.TableAggregationFunction) KsqlException(io.confluent.ksql.util.KsqlException)

Example 48 with ColumnName

use of io.confluent.ksql.name.ColumnName in project ksql by confluentinc.

the class AggregateAnalyzerTest method shouldNotCaptureWindowStartAsRequiredColumn.

@Test
public void shouldNotCaptureWindowStartAsRequiredColumn() {
    // Given:
    givenWindowExpression();
    givenSelectExpression(new UnqualifiedColumnReferenceExp(SystemColumns.WINDOWSTART_NAME));
    // When:
    final AggregateAnalysisResult result = analyzer.analyze(analysis, selects);
    // Then:
    final List<ColumnName> requiredColumnNames = result.getRequiredColumns().stream().map(ColumnReferenceExp::getColumnName).collect(Collectors.toList());
    assertThat(requiredColumnNames, not(hasItem(SystemColumns.WINDOWSTART_NAME)));
}
Also used : ColumnName(io.confluent.ksql.name.ColumnName) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp) Test(org.junit.Test)

Example 49 with ColumnName

use of io.confluent.ksql.name.ColumnName in project ksql by confluentinc.

the class Repartitioning method repartitionNeeded.

/**
 * Determine if a repartition is needed.
 *
 * <p>A repartition is only not required if partitioning by the existing key columns.
 *
 * @param schema the schema of the data before any repartition
 * @param partitionBy the expressions to partition by
 * @return {@code true} if a repartition is needed.
 */
public static boolean repartitionNeeded(final LogicalSchema schema, final List<Expression> partitionBy) {
    if (schema.key().isEmpty()) {
        // No current key, so repartition needed:
        return true;
    }
    // do end up supporting this (we'll have to change the logic here)
    if (schema.key().size() != 1) {
        return true;
    }
    if (partitionBy.size() != schema.key().size()) {
        // Different number of expressions to keys means it must be a repartition:
        return true;
    }
    final Expression expression = partitionBy.get(0);
    if (!(expression instanceof ColumnReferenceExp)) {
        // If expression is not a column reference then the key will be changing
        return true;
    }
    final ColumnName newKeyColName = ((ColumnReferenceExp) expression).getColumnName();
    return !newKeyColName.equals(schema.key().get(0).name());
}
Also used : ColumnReferenceExp(io.confluent.ksql.execution.expression.tree.ColumnReferenceExp) ColumnName(io.confluent.ksql.name.ColumnName) Expression(io.confluent.ksql.execution.expression.tree.Expression)

Example 50 with ColumnName

use of io.confluent.ksql.name.ColumnName in project ksql by confluentinc.

the class AggregateAnalyzerTest method shouldNotCaptureWindowEndAsRequiredColumn.

@Test
public void shouldNotCaptureWindowEndAsRequiredColumn() {
    // Given:
    givenWindowExpression();
    givenSelectExpression(new UnqualifiedColumnReferenceExp(SystemColumns.WINDOWEND_NAME));
    // When:
    final AggregateAnalysisResult result = analyzer.analyze(analysis, selects);
    // Then:
    final List<ColumnName> requiredColumnNames = result.getRequiredColumns().stream().map(ColumnReferenceExp::getColumnName).collect(Collectors.toList());
    assertThat(requiredColumnNames, not(hasItem(SystemColumns.WINDOWEND_NAME)));
}
Also used : ColumnName(io.confluent.ksql.name.ColumnName) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp) Test(org.junit.Test)

Aggregations

ColumnName (io.confluent.ksql.name.ColumnName)63 Test (org.junit.Test)32 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)31 Expression (io.confluent.ksql.execution.expression.tree.Expression)23 KsqlException (io.confluent.ksql.util.KsqlException)13 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)11 Column (io.confluent.ksql.schema.ksql.Column)11 UnqualifiedColumnReferenceExp (io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp)10 SqlType (io.confluent.ksql.schema.ksql.types.SqlType)9 ColumnReferenceExp (io.confluent.ksql.execution.expression.tree.ColumnReferenceExp)8 Optional (java.util.Optional)8 Collectors (java.util.stream.Collectors)8 FunctionCall (io.confluent.ksql.execution.expression.tree.FunctionCall)7 List (java.util.List)7 TimestampColumn (io.confluent.ksql.execution.timestamp.TimestampColumn)6 Builder (io.confluent.ksql.schema.ksql.LogicalSchema.Builder)6 Set (java.util.Set)6 DereferenceExpression (io.confluent.ksql.execution.expression.tree.DereferenceExpression)5 QualifiedColumnReferenceExp (io.confluent.ksql.execution.expression.tree.QualifiedColumnReferenceExp)5 SelectExpression (io.confluent.ksql.execution.plan.SelectExpression)5