Search in sources :

Example 6 with Pair

use of io.confluent.ksql.util.Pair in project ksql by confluentinc.

the class QueryEngine method buildLogicalPlans.

List<Pair<String, PlanNode>> buildLogicalPlans(final MetaStore metaStore, final List<Pair<String, Statement>> statementList) {
    List<Pair<String, PlanNode>> logicalPlansList = new ArrayList<>();
    // TODO: the purpose of tempMetaStore here
    MetaStore tempMetaStore = metaStore.clone();
    for (Pair<String, Statement> statementQueryPair : statementList) {
        if (statementQueryPair.getRight() instanceof Query) {
            PlanNode logicalPlan = buildQueryLogicalPlan(statementQueryPair.getLeft(), (Query) statementQueryPair.getRight(), tempMetaStore);
            logicalPlansList.add(new Pair<>(statementQueryPair.getLeft(), logicalPlan));
        } else {
            logicalPlansList.add(new Pair<>(statementQueryPair.getLeft(), null));
        }
        log.info("Build logical plan for {}.", statementQueryPair.getLeft());
    }
    return logicalPlansList;
}
Also used : MetaStore(io.confluent.ksql.metastore.MetaStore) PlanNode(io.confluent.ksql.planner.plan.PlanNode) Query(io.confluent.ksql.parser.tree.Query) DdlStatement(io.confluent.ksql.parser.tree.DdlStatement) AbstractStreamCreateStatement(io.confluent.ksql.parser.tree.AbstractStreamCreateStatement) Statement(io.confluent.ksql.parser.tree.Statement) ArrayList(java.util.ArrayList) Pair(io.confluent.ksql.util.Pair)

Example 7 with Pair

use of io.confluent.ksql.util.Pair in project ksql by confluentinc.

the class Analyzer method fetchKeyFieldNameFromExpr.

/**
 * Given an expression and the source alias detects if the expression type is
 * DereferenceExpression
 * or QualifiedNameReference and if the variable prefix matches the source Alias.
 */
private Pair<String, String> fetchKeyFieldNameFromExpr(Expression expression, String sourceAlias, Schema sourceSchema) {
    if (expression instanceof DereferenceExpression) {
        DereferenceExpression dereferenceExpression = (DereferenceExpression) expression;
        String sourceAliasVal = dereferenceExpression.getBase().toString();
        if (sourceAliasVal.equalsIgnoreCase(sourceAlias)) {
            String keyFieldName = dereferenceExpression.getFieldName();
            if (SchemaUtil.getFieldByName(sourceSchema, keyFieldName).isPresent()) {
                return new Pair<>(sourceAliasVal, keyFieldName);
            }
        }
    } else if (expression instanceof QualifiedNameReference) {
        QualifiedNameReference qualifiedNameReference = (QualifiedNameReference) expression;
        String keyFieldName = qualifiedNameReference.getName().getSuffix();
        if (SchemaUtil.getFieldByName(sourceSchema, keyFieldName).isPresent()) {
            return new Pair<>(sourceAlias, keyFieldName);
        }
    }
    return null;
}
Also used : DereferenceExpression(io.confluent.ksql.parser.tree.DereferenceExpression) QualifiedNameReference(io.confluent.ksql.parser.tree.QualifiedNameReference) Pair(io.confluent.ksql.util.Pair)

Example 8 with Pair

use of io.confluent.ksql.util.Pair in project ksql by confluentinc.

the class AggregateNode method buildStream.

@Override
public SchemaKStream buildStream(final StreamsBuilder builder, final KsqlConfig ksqlConfig, final KafkaTopicClient kafkaTopicClient, final FunctionRegistry functionRegistry, final Map<String, Object> props, final SchemaRegistryClient schemaRegistryClient) {
    final StructuredDataSourceNode streamSourceNode = getTheSourceNode();
    final SchemaKStream sourceSchemaKStream = getSource().buildStream(builder, ksqlConfig, kafkaTopicClient, functionRegistry, props, schemaRegistryClient);
    if (sourceSchemaKStream instanceof SchemaKTable) {
        throw new KsqlException("Unsupported aggregation. KSQL currently only supports aggregation on a Stream.");
    }
    // Pre aggregate computations
    final List<Pair<String, Expression>> aggArgExpansionList = new ArrayList<>();
    final Map<String, Integer> expressionNames = new HashMap<>();
    collectAggregateArgExpressions(getRequiredColumnList(), aggArgExpansionList, expressionNames);
    collectAggregateArgExpressions(getAggregateFunctionArguments(), aggArgExpansionList, expressionNames);
    final SchemaKStream aggregateArgExpanded = sourceSchemaKStream.select(aggArgExpansionList);
    KsqlTopicSerDe ksqlTopicSerDe = streamSourceNode.getStructuredDataSource().getKsqlTopic().getKsqlTopicSerDe();
    final Serde<GenericRow> genericRowSerde = ksqlTopicSerDe.getGenericRowSerde(aggregateArgExpanded.getSchema(), ksqlConfig, true, schemaRegistryClient);
    final SchemaKGroupedStream schemaKGroupedStream = aggregateArgExpanded.groupBy(Serdes.String(), genericRowSerde, getGroupByExpressions());
    // Aggregate computations
    final SchemaBuilder aggregateSchema = SchemaBuilder.struct();
    final Map<Integer, Integer> aggValToValColumnMap = createAggregateValueToValueColumnMap(aggregateArgExpanded, aggregateSchema);
    final Schema aggStageSchema = buildAggregateSchema(aggregateArgExpanded.getSchema(), functionRegistry);
    final Serde<GenericRow> aggValueGenericRowSerde = ksqlTopicSerDe.getGenericRowSerde(aggStageSchema, ksqlConfig, true, schemaRegistryClient);
    final KudafInitializer initializer = new KudafInitializer(aggValToValColumnMap.size());
    final SchemaKTable schemaKTable = schemaKGroupedStream.aggregate(initializer, new KudafAggregator(createAggValToFunctionMap(expressionNames, aggregateArgExpanded, aggregateSchema, initializer, aggValToValColumnMap.size(), functionRegistry), aggValToValColumnMap), getWindowExpression(), aggValueGenericRowSerde);
    SchemaKTable result = new SchemaKTable(aggStageSchema, schemaKTable.getKtable(), schemaKTable.getKeyField(), schemaKTable.getSourceSchemaKStreams(), schemaKTable.isWindowed(), SchemaKStream.Type.AGGREGATE, functionRegistry, schemaRegistryClient);
    if (getHavingExpressions() != null) {
        result = result.filter(getHavingExpressions());
    }
    return result.select(getFinalSelectExpressions());
}
Also used : SchemaKTable(io.confluent.ksql.structured.SchemaKTable) HashMap(java.util.HashMap) Schema(org.apache.kafka.connect.data.Schema) ArrayList(java.util.ArrayList) KudafInitializer(io.confluent.ksql.function.udaf.KudafInitializer) KudafAggregator(io.confluent.ksql.function.udaf.KudafAggregator) KsqlException(io.confluent.ksql.util.KsqlException) SchemaKGroupedStream(io.confluent.ksql.structured.SchemaKGroupedStream) GenericRow(io.confluent.ksql.GenericRow) KsqlTopicSerDe(io.confluent.ksql.serde.KsqlTopicSerDe) SchemaBuilder(org.apache.kafka.connect.data.SchemaBuilder) SchemaKStream(io.confluent.ksql.structured.SchemaKStream) Pair(io.confluent.ksql.util.Pair)

Example 9 with Pair

use of io.confluent.ksql.util.Pair in project ksql by confluentinc.

the class CommandRunnerTest method getRecordMap.

private Map<TopicPartition, List<ConsumerRecord<CommandId, Command>>> getRecordMap() {
    List<Pair<CommandId, Command>> commandList = new TestUtils().getAllPriorCommandRecords();
    List<ConsumerRecord<CommandId, Command>> recordList = new ArrayList<>();
    for (Pair commandPair : commandList) {
        recordList.add(new ConsumerRecord<>("T", 1, 1, (CommandId) commandPair.getLeft(), (Command) commandPair.getRight()));
    }
    Map<TopicPartition, List<ConsumerRecord<CommandId, Command>>> recordMap = new HashMap<>();
    recordMap.put(new TopicPartition("T", 1), recordList);
    return recordMap;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) TestUtils(io.confluent.ksql.rest.server.utils.TestUtils) TopicPartition(org.apache.kafka.common.TopicPartition) ArrayList(java.util.ArrayList) List(java.util.List) Pair(io.confluent.ksql.util.Pair)

Example 10 with Pair

use of io.confluent.ksql.util.Pair in project ksql by confluentinc.

the class RestoreCommandsTest method shouldNotHaveTerminatedQueryWhenDroppedAndRecreatedAfterTerminate.

@Test
public void shouldNotHaveTerminatedQueryWhenDroppedAndRecreatedAfterTerminate() {
    restoreCommands.addCommand(createId, createCommand);
    restoreCommands.addCommand(terminateId, terminateCommand);
    // drop
    restoreCommands.addCommand(dropId, dropCommand);
    // recreate
    restoreCommands.addCommand(createId, createCommand);
    final List<Pair<CommandId, Map<QueryId, CommandId>>> results = new ArrayList<>();
    restoreCommands.forEach((commandId, command, terminatedQueries, droppedEntities) -> {
        results.add(new Pair<>(commandId, terminatedQueries));
    });
    assertThat(results, equalTo(Arrays.asList(new Pair<>(createId, Collections.singletonMap(new QueryId("queryId"), terminateId)), new Pair<>(dropId, Collections.<QueryId, CommandId>emptyMap()), new Pair<>(createId, Collections.<QueryId, CommandId>emptyMap()))));
}
Also used : QueryId(io.confluent.ksql.query.QueryId) ArrayList(java.util.ArrayList) Pair(io.confluent.ksql.util.Pair) Test(org.junit.Test)

Aggregations

Pair (io.confluent.ksql.util.Pair)19 ArrayList (java.util.ArrayList)8 KsqlException (io.confluent.ksql.util.KsqlException)6 MetaStore (io.confluent.ksql.metastore.MetaStore)3 Statement (io.confluent.ksql.parser.tree.Statement)3 PlanNode (io.confluent.ksql.planner.plan.PlanNode)3 Test (org.junit.Test)3 MockSchemaRegistryClient (io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient)2 KsqlTopic (io.confluent.ksql.metastore.KsqlTopic)2 StructuredDataSource (io.confluent.ksql.metastore.StructuredDataSource)2 AbstractStreamCreateStatement (io.confluent.ksql.parser.tree.AbstractStreamCreateStatement)2 CreateStream (io.confluent.ksql.parser.tree.CreateStream)2 CreateTable (io.confluent.ksql.parser.tree.CreateTable)2 DdlStatement (io.confluent.ksql.parser.tree.DdlStatement)2 DereferenceExpression (io.confluent.ksql.parser.tree.DereferenceExpression)2 Expression (io.confluent.ksql.parser.tree.Expression)2 Query (io.confluent.ksql.parser.tree.Query)2 ProjectNode (io.confluent.ksql.planner.plan.ProjectNode)2 TestUtils (io.confluent.ksql.rest.server.utils.TestUtils)2 KsqlTopicSerDe (io.confluent.ksql.serde.KsqlTopicSerDe)2