Search in sources :

Example 31 with Expression

use of io.confluent.ksql.parser.tree.Expression in project ksql by confluentinc.

the class SchemaKStream method createSelectValueMapperAndSchema.

Pair<Schema, SelectValueMapper> createSelectValueMapperAndSchema(final List<Pair<String, Expression>> expressionPairList) {
    try {
        final CodeGenRunner codeGenRunner = new CodeGenRunner(schema, functionRegistry);
        final SchemaBuilder schemaBuilder = SchemaBuilder.struct();
        final List<ExpressionMetadata> expressionEvaluators = new ArrayList<>();
        for (Pair<String, Expression> expressionPair : expressionPairList) {
            final ExpressionMetadata expressionEvaluator = codeGenRunner.buildCodeGenFromParseTree(expressionPair.getRight());
            schemaBuilder.field(expressionPair.getLeft(), expressionEvaluator.getExpressionType());
            expressionEvaluators.add(expressionEvaluator);
        }
        return new Pair<>(schemaBuilder.build(), new SelectValueMapper(genericRowValueTypeEnforcer, expressionPairList, expressionEvaluators));
    } catch (Exception e) {
        throw new KsqlException("Code generation failed for SelectValueMapper", e);
    }
}
Also used : ExpressionMetadata(io.confluent.ksql.util.ExpressionMetadata) CodeGenRunner(io.confluent.ksql.codegen.CodeGenRunner) DereferenceExpression(io.confluent.ksql.parser.tree.DereferenceExpression) Expression(io.confluent.ksql.parser.tree.Expression) SchemaBuilder(org.apache.kafka.connect.data.SchemaBuilder) ArrayList(java.util.ArrayList) KsqlException(io.confluent.ksql.util.KsqlException) KsqlException(io.confluent.ksql.util.KsqlException) Pair(io.confluent.ksql.util.Pair)

Example 32 with Expression

use of io.confluent.ksql.parser.tree.Expression in project ksql by confluentinc.

the class SchemaKStream method groupBy.

public SchemaKGroupedStream groupBy(final Serde<String> keySerde, final Serde<GenericRow> valSerde, final List<Expression> groupByExpressions) {
    boolean rekey = rekeyRequired(groupByExpressions);
    if (!rekey) {
        KGroupedStream kgroupedStream = kstream.groupByKey(Serialized.with(keySerde, valSerde));
        return new SchemaKGroupedStream(schema, kgroupedStream, keyField, Collections.singletonList(this), functionRegistry, schemaRegistryClient);
    }
    // Collect the column indexes, and build the new key as <column1>+<column2>+...
    StringBuilder aggregateKeyName = new StringBuilder();
    List<Integer> newKeyIndexes = new ArrayList<>();
    boolean addSeparator = false;
    for (Expression groupByExpr : groupByExpressions) {
        if (addSeparator) {
            aggregateKeyName.append("|+|");
        } else {
            addSeparator = true;
        }
        aggregateKeyName.append(groupByExpr.toString());
        newKeyIndexes.add(SchemaUtil.getIndexInSchema(groupByExpr.toString(), getSchema()));
    }
    KGroupedStream kgroupedStream = kstream.filter((key, value) -> value != null).groupBy((key, value) -> {
        StringBuilder newKey = new StringBuilder();
        boolean addSeparator1 = false;
        for (int index : newKeyIndexes) {
            if (addSeparator1) {
                newKey.append("|+|");
            } else {
                addSeparator1 = true;
            }
            newKey.append(String.valueOf(value.getColumns().get(index)));
        }
        return newKey.toString();
    }, Serialized.with(keySerde, valSerde));
    // TODO: if the key is a prefix of the grouping columns then we can
    // use the repartition reflection hack to tell streams not to
    // repartition.
    Field newKeyField = new Field(aggregateKeyName.toString(), -1, Schema.STRING_SCHEMA);
    return new SchemaKGroupedStream(schema, kgroupedStream, newKeyField, Collections.singletonList(this), functionRegistry, schemaRegistryClient);
}
Also used : Arrays(java.util.Arrays) KGroupedStream(org.apache.kafka.streams.kstream.KGroupedStream) Produced(org.apache.kafka.streams.kstream.Produced) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) Serialized(org.apache.kafka.streams.kstream.Serialized) KStream(org.apache.kafka.streams.kstream.KStream) Joined(org.apache.kafka.streams.kstream.Joined) Schema(org.apache.kafka.connect.data.Schema) ArrayList(java.util.ArrayList) Pair(io.confluent.ksql.util.Pair) Serde(org.apache.kafka.common.serialization.Serde) DereferenceExpression(io.confluent.ksql.parser.tree.DereferenceExpression) ExpressionMetadata(io.confluent.ksql.util.ExpressionMetadata) Serdes(org.apache.kafka.common.serialization.Serdes) CodeGenRunner(io.confluent.ksql.codegen.CodeGenRunner) SchemaUtil(io.confluent.ksql.util.SchemaUtil) OutputNode(io.confluent.ksql.planner.plan.OutputNode) Field(org.apache.kafka.connect.data.Field) FunctionRegistry(io.confluent.ksql.function.FunctionRegistry) Set(java.util.Set) KsqlConfig(io.confluent.ksql.util.KsqlConfig) Expression(io.confluent.ksql.parser.tree.Expression) List(java.util.List) ValueJoiner(org.apache.kafka.streams.kstream.ValueJoiner) GenericRow(io.confluent.ksql.GenericRow) Optional(java.util.Optional) KsqlException(io.confluent.ksql.util.KsqlException) SchemaBuilder(org.apache.kafka.connect.data.SchemaBuilder) KsqlTopicSerDe(io.confluent.ksql.serde.KsqlTopicSerDe) Collections(java.util.Collections) GenericRowValueTypeEnforcer(io.confluent.ksql.util.GenericRowValueTypeEnforcer) Field(org.apache.kafka.connect.data.Field) KGroupedStream(org.apache.kafka.streams.kstream.KGroupedStream) DereferenceExpression(io.confluent.ksql.parser.tree.DereferenceExpression) Expression(io.confluent.ksql.parser.tree.Expression) ArrayList(java.util.ArrayList)

Example 33 with Expression

use of io.confluent.ksql.parser.tree.Expression in project ksql by confluentinc.

the class CommandFactoriesTest method shouldFailCreateTableIfTimestampColumnNameIsIncorrect.

@Test
public void shouldFailCreateTableIfTimestampColumnNameIsIncorrect() {
    HashMap<String, Expression> tableProperties = new HashMap<>();
    tableProperties.putAll(properties);
    tableProperties.put(DdlConfig.TIMESTAMP_NAME_PROPERTY, new StringLiteral("COL3"));
    try {
        final DdlCommand result = commandFactories.create(sqlExpression, new CreateTable(QualifiedName.of("foo"), Arrays.asList(new TableElement("COL1", "BIGINT"), new TableElement("COL2", "VARCHAR")), true, tableProperties), Collections.emptyMap());
    } catch (KsqlException e) {
        assertThat(e.getMessage(), equalTo("No column with the provided timestamp column name in the WITH clause, COL3, exists in the defined schema."));
    }
}
Also used : StringLiteral(io.confluent.ksql.parser.tree.StringLiteral) Expression(io.confluent.ksql.parser.tree.Expression) HashMap(java.util.HashMap) CreateTable(io.confluent.ksql.parser.tree.CreateTable) EasyMock.anyString(org.easymock.EasyMock.anyString) KsqlException(io.confluent.ksql.util.KsqlException) TableElement(io.confluent.ksql.parser.tree.TableElement) Test(org.junit.Test)

Example 34 with Expression

use of io.confluent.ksql.parser.tree.Expression in project ksql by confluentinc.

the class DefaultTraversalVisitor method visitSampledRelation.

@Override
protected R visitSampledRelation(SampledRelation node, C context) {
    process(node.getRelation(), context);
    process(node.getSamplePercentage(), context);
    if (node.getColumnsToStratifyOn().isPresent()) {
        for (Expression expression : node.getColumnsToStratifyOn().get()) {
            process(expression, context);
        }
    }
    return null;
}
Also used : InListExpression(io.confluent.ksql.parser.tree.InListExpression) NullIfExpression(io.confluent.ksql.parser.tree.NullIfExpression) SimpleCaseExpression(io.confluent.ksql.parser.tree.SimpleCaseExpression) ComparisonExpression(io.confluent.ksql.parser.tree.ComparisonExpression) DereferenceExpression(io.confluent.ksql.parser.tree.DereferenceExpression) SubscriptExpression(io.confluent.ksql.parser.tree.SubscriptExpression) Expression(io.confluent.ksql.parser.tree.Expression) SearchedCaseExpression(io.confluent.ksql.parser.tree.SearchedCaseExpression) SubqueryExpression(io.confluent.ksql.parser.tree.SubqueryExpression) LogicalBinaryExpression(io.confluent.ksql.parser.tree.LogicalBinaryExpression) ArithmeticUnaryExpression(io.confluent.ksql.parser.tree.ArithmeticUnaryExpression) ArithmeticBinaryExpression(io.confluent.ksql.parser.tree.ArithmeticBinaryExpression) NotExpression(io.confluent.ksql.parser.tree.NotExpression)

Example 35 with Expression

use of io.confluent.ksql.parser.tree.Expression in project ksql by confluentinc.

the class LogicalPlanner method buildAggregateNode.

private AggregateNode buildAggregateNode(final Schema inputSchema, final PlanNode sourcePlanNode) {
    SchemaBuilder aggregateSchema = SchemaBuilder.struct();
    ExpressionTypeManager expressionTypeManager = new ExpressionTypeManager(inputSchema, functionRegistry);
    for (int i = 0; i < analysis.getSelectExpressions().size(); i++) {
        Expression expression = analysis.getSelectExpressions().get(i);
        String alias = analysis.getSelectExpressionAlias().get(i);
        Schema expressionType = expressionTypeManager.getExpressionType(expression);
        aggregateSchema = aggregateSchema.field(alias, expressionType);
    }
    return new AggregateNode(new PlanNodeId("Aggregate"), sourcePlanNode, aggregateSchema, analysis.getGroupByExpressions(), analysis.getWindowExpression(), aggregateAnalysis.getAggregateFunctionArguments(), aggregateAnalysis.getFunctionList(), aggregateAnalysis.getRequiredColumnsList(), aggregateAnalysis.getFinalSelectExpressions(), aggregateAnalysis.getHavingExpression());
}
Also used : PlanNodeId(io.confluent.ksql.planner.plan.PlanNodeId) ExpressionTypeManager(io.confluent.ksql.util.ExpressionTypeManager) AggregateNode(io.confluent.ksql.planner.plan.AggregateNode) Expression(io.confluent.ksql.parser.tree.Expression) Schema(org.apache.kafka.connect.data.Schema) SchemaBuilder(org.apache.kafka.connect.data.SchemaBuilder)

Aggregations

Expression (io.confluent.ksql.parser.tree.Expression)36 DereferenceExpression (io.confluent.ksql.parser.tree.DereferenceExpression)17 ComparisonExpression (io.confluent.ksql.parser.tree.ComparisonExpression)13 WindowExpression (io.confluent.ksql.parser.tree.WindowExpression)10 HashMap (java.util.HashMap)10 ArithmeticBinaryExpression (io.confluent.ksql.parser.tree.ArithmeticBinaryExpression)8 ArithmeticUnaryExpression (io.confluent.ksql.parser.tree.ArithmeticUnaryExpression)8 InListExpression (io.confluent.ksql.parser.tree.InListExpression)8 LogicalBinaryExpression (io.confluent.ksql.parser.tree.LogicalBinaryExpression)8 NotExpression (io.confluent.ksql.parser.tree.NotExpression)8 NullIfExpression (io.confluent.ksql.parser.tree.NullIfExpression)8 QualifiedNameReference (io.confluent.ksql.parser.tree.QualifiedNameReference)8 SearchedCaseExpression (io.confluent.ksql.parser.tree.SearchedCaseExpression)8 SimpleCaseExpression (io.confluent.ksql.parser.tree.SimpleCaseExpression)8 StringLiteral (io.confluent.ksql.parser.tree.StringLiteral)8 SubqueryExpression (io.confluent.ksql.parser.tree.SubqueryExpression)8 SubscriptExpression (io.confluent.ksql.parser.tree.SubscriptExpression)8 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)8 HoppingWindowExpression (io.confluent.ksql.parser.tree.HoppingWindowExpression)7