Search in sources :

Example 6 with LogicalPlanNode

use of io.confluent.ksql.planner.LogicalPlanNode in project ksql by confluentinc.

the class PushPhysicalPlanBuilder method buildPushPhysicalPlan.

/**
 * Visits the logical plan top-down to build the physical plan.
 * @param logicalPlanNode the logical plan root node
 * @return the root node of the tree of physical operators
 */
public PushPhysicalPlan buildPushPhysicalPlan(final LogicalPlanNode logicalPlanNode, final Context context, final Optional<PushOffsetRange> offsetRange, final Optional<String> catchupConsumerGroup) {
    final String catchupConsumerGroupId = getConsumerGroupId(catchupConsumerGroup);
    PushDataSourceOperator dataSourceOperator = null;
    final OutputNode outputNode = logicalPlanNode.getNode().orElseThrow(() -> new IllegalArgumentException("Need an output node to build a plan"));
    if (!(outputNode instanceof KsqlBareOutputNode)) {
        throw new KsqlException("Push queries expect the root of the logical plan to be a " + "KsqlBareOutputNode.");
    }
    // We skip KsqlBareOutputNode in the translation since it only applies the LIMIT
    PlanNode currentLogicalNode = outputNode.getSource();
    AbstractPhysicalOperator prevPhysicalOp = null;
    AbstractPhysicalOperator rootPhysicalOp = null;
    while (true) {
        AbstractPhysicalOperator currentPhysicalOp = null;
        if (currentLogicalNode instanceof QueryProjectNode) {
            currentPhysicalOp = translateProjectNode((QueryProjectNode) currentLogicalNode);
        } else if (currentLogicalNode instanceof QueryFilterNode) {
            currentPhysicalOp = translateFilterNode((QueryFilterNode) currentLogicalNode);
        } else if (currentLogicalNode instanceof DataSourceNode) {
            currentPhysicalOp = translateDataSourceNode((DataSourceNode) currentLogicalNode, offsetRange, catchupConsumerGroupId);
            dataSourceOperator = (PushDataSourceOperator) currentPhysicalOp;
        } else {
            throw new KsqlException(String.format("Error in translating logical to physical plan for scalable push queries:" + " unrecognized logical node %s.", currentLogicalNode));
        }
        if (prevPhysicalOp == null) {
            rootPhysicalOp = currentPhysicalOp;
        } else {
            prevPhysicalOp.addChild(currentPhysicalOp);
        }
        prevPhysicalOp = currentPhysicalOp;
        // Exit the loop when a leaf node is reached
        if (currentLogicalNode.getSources().isEmpty()) {
            break;
        }
        if (currentLogicalNode.getSources().size() > 1) {
            throw new KsqlException("Push queries do not support joins or nested sub-queries yet.");
        }
        currentLogicalNode = currentLogicalNode.getSources().get(0);
    }
    if (dataSourceOperator == null) {
        throw new IllegalStateException("DataSourceOperator cannot be null in Push physical plan");
    }
    return new PushPhysicalPlan(rootPhysicalOp, (rootPhysicalOp).getLogicalNode().getSchema(), queryId, catchupConsumerGroupId, dataSourceOperator.getScalablePushRegistry(), dataSourceOperator, context, querySourceType);
}
Also used : PushDataSourceOperator(io.confluent.ksql.physical.scalablepush.operators.PushDataSourceOperator) KsqlBareOutputNode(io.confluent.ksql.planner.plan.KsqlBareOutputNode) OutputNode(io.confluent.ksql.planner.plan.OutputNode) DataSourceNode(io.confluent.ksql.planner.plan.DataSourceNode) AbstractPhysicalOperator(io.confluent.ksql.physical.common.operators.AbstractPhysicalOperator) KsqlException(io.confluent.ksql.util.KsqlException) QueryFilterNode(io.confluent.ksql.planner.plan.QueryFilterNode) QueryProjectNode(io.confluent.ksql.planner.plan.QueryProjectNode) KsqlBareOutputNode(io.confluent.ksql.planner.plan.KsqlBareOutputNode) PlanNode(io.confluent.ksql.planner.plan.PlanNode) LogicalPlanNode(io.confluent.ksql.planner.LogicalPlanNode)

Aggregations

LogicalPlanNode (io.confluent.ksql.planner.LogicalPlanNode)6 KsqlException (io.confluent.ksql.util.KsqlException)6 KsqlBareOutputNode (io.confluent.ksql.planner.plan.KsqlBareOutputNode)5 OutputNode (io.confluent.ksql.planner.plan.OutputNode)5 PlanNode (io.confluent.ksql.planner.plan.PlanNode)5 DataSourceNode (io.confluent.ksql.planner.plan.DataSourceNode)4 PlanInfo (io.confluent.ksql.execution.plan.PlanInfo)3 PlanInfoExtractor (io.confluent.ksql.execution.plan.PlanInfoExtractor)3 Throwables (com.google.common.base.Throwables)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Iterables (com.google.common.collect.Iterables)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 ExecuteResult (io.confluent.ksql.KsqlExecutionContext.ExecuteResult)2 ImmutableAnalysis (io.confluent.ksql.analyzer.ImmutableAnalysis)2 SessionConfig (io.confluent.ksql.config.SessionConfig)2 CreateTableCommand (io.confluent.ksql.execution.ddl.commands.CreateTableCommand)2 DdlCommand (io.confluent.ksql.execution.ddl.commands.DdlCommand)2 KsqlTopic (io.confluent.ksql.execution.ddl.commands.KsqlTopic)2 UnqualifiedColumnReferenceExp (io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp)2