Search in sources :

Example 1 with StructuredDataSourceNode

use of io.confluent.ksql.planner.plan.StructuredDataSourceNode in project ksql by confluentinc.

the class Analyzer method visitJoin.

@Override
protected Node visitJoin(final Join node, final AnalysisContext context) {
    AliasedRelation left = (AliasedRelation) process(node.getLeft(), context);
    AliasedRelation right = (AliasedRelation) process(node.getRight(), context);
    String leftSideName = ((Table) left.getRelation()).getName().getSuffix();
    StructuredDataSource leftDataSource = metaStore.getSource(leftSideName);
    if (leftDataSource == null) {
        throw new KsqlException(format("Resource %s does not exist.", leftSideName));
    }
    leftDataSource = timestampColumn(left, leftDataSource);
    String rightSideName = ((Table) right.getRelation()).getName().getSuffix();
    StructuredDataSource rightDataSource = metaStore.getSource(rightSideName);
    if (rightDataSource == null) {
        throw new KsqlException(format("Resource %s does not exist.", rightSideName));
    }
    rightDataSource = timestampColumn(right, rightDataSource);
    String leftAlias = left.getAlias();
    String rightAlias = right.getAlias();
    JoinNode.Type joinType = getJoinType(node);
    if (!node.getCriteria().isPresent()) {
        throw new KsqlException(String.format("%s Join criteria is not set.", node.getLocation().isPresent() ? node.getLocation().get().toString() : ""));
    }
    JoinOn joinOn = (JoinOn) (node.getCriteria().get());
    ComparisonExpression comparisonExpression = (ComparisonExpression) joinOn.getExpression();
    Pair<String, String> leftSide = fetchKeyFieldName(comparisonExpression, leftAlias, leftDataSource.getSchema());
    Pair<String, String> rightSide = fetchKeyFieldName(comparisonExpression, rightAlias, rightDataSource.getSchema());
    String leftKeyFieldName = leftSide.getRight();
    String rightKeyFieldName = rightSide.getRight();
    if (comparisonExpression.getType() != ComparisonExpression.Type.EQUAL) {
        throw new KsqlException("Only equality join criteria is supported.");
    }
    StructuredDataSourceNode leftSourceKafkaTopicNode = new StructuredDataSourceNode(new PlanNodeId("KafkaTopic_Left"), leftDataSource, leftDataSource.getSchema());
    StructuredDataSourceNode rightSourceKafkaTopicNode = new StructuredDataSourceNode(new PlanNodeId("KafkaTopic_Right"), rightDataSource, rightDataSource.getSchema());
    JoinNode joinNode = new JoinNode(new PlanNodeId("Join"), joinType, leftSourceKafkaTopicNode, rightSourceKafkaTopicNode, leftKeyFieldName, rightKeyFieldName, leftAlias, rightAlias);
    analysis.setJoin(joinNode);
    return null;
}
Also used : PlanNodeId(io.confluent.ksql.planner.plan.PlanNodeId) StructuredDataSource(io.confluent.ksql.metastore.StructuredDataSource) ComparisonExpression(io.confluent.ksql.parser.tree.ComparisonExpression) JoinNode(io.confluent.ksql.planner.plan.JoinNode) StructuredDataSourceNode(io.confluent.ksql.planner.plan.StructuredDataSourceNode) KsqlException(io.confluent.ksql.util.KsqlException) JoinOn(io.confluent.ksql.parser.tree.JoinOn) AliasedRelation(io.confluent.ksql.parser.tree.AliasedRelation)

Example 2 with StructuredDataSourceNode

use of io.confluent.ksql.planner.plan.StructuredDataSourceNode in project ksql by confluentinc.

the class LogicalPlanner method buildSourceNode.

private StructuredDataSourceNode buildSourceNode() {
    Pair<StructuredDataSource, String> dataSource = analysis.getFromDataSource(0);
    Schema fromSchema = SchemaUtil.buildSchemaWithAlias(dataSource.left.getSchema(), dataSource.right);
    if (dataSource.left instanceof KsqlStream || dataSource.left instanceof KsqlTable) {
        return new StructuredDataSourceNode(new PlanNodeId("KsqlTopic"), dataSource.left, fromSchema);
    }
    throw new RuntimeException("Data source is not supported yet.");
}
Also used : PlanNodeId(io.confluent.ksql.planner.plan.PlanNodeId) StructuredDataSource(io.confluent.ksql.metastore.StructuredDataSource) KsqlStream(io.confluent.ksql.metastore.KsqlStream) Schema(org.apache.kafka.connect.data.Schema) KsqlTable(io.confluent.ksql.metastore.KsqlTable) StructuredDataSourceNode(io.confluent.ksql.planner.plan.StructuredDataSourceNode)

Example 3 with StructuredDataSourceNode

use of io.confluent.ksql.planner.plan.StructuredDataSourceNode in project ksql by confluentinc.

the class LogicalPlannerTest method shouldCreatePlanWithTableAsSource.

@Test
public void shouldCreatePlanWithTableAsSource() {
    PlanNode planNode = buildLogicalPlan("select col0 from TEST2 limit 5;");
    assertThat(planNode.getSources().size(), equalTo(1));
    StructuredDataSource structuredDataSource = ((StructuredDataSourceNode) planNode.getSources().get(0).getSources().get(0)).getStructuredDataSource();
    assertThat(structuredDataSource.getDataSourceType(), equalTo(DataSource.DataSourceType.KTABLE));
    assertThat(structuredDataSource.getName(), equalTo("TEST2"));
}
Also used : StructuredDataSource(io.confluent.ksql.metastore.StructuredDataSource) PlanNode(io.confluent.ksql.planner.plan.PlanNode) StructuredDataSourceNode(io.confluent.ksql.planner.plan.StructuredDataSourceNode) Test(org.junit.Test)

Aggregations

StructuredDataSource (io.confluent.ksql.metastore.StructuredDataSource)3 StructuredDataSourceNode (io.confluent.ksql.planner.plan.StructuredDataSourceNode)3 PlanNodeId (io.confluent.ksql.planner.plan.PlanNodeId)2 KsqlStream (io.confluent.ksql.metastore.KsqlStream)1 KsqlTable (io.confluent.ksql.metastore.KsqlTable)1 AliasedRelation (io.confluent.ksql.parser.tree.AliasedRelation)1 ComparisonExpression (io.confluent.ksql.parser.tree.ComparisonExpression)1 JoinOn (io.confluent.ksql.parser.tree.JoinOn)1 JoinNode (io.confluent.ksql.planner.plan.JoinNode)1 PlanNode (io.confluent.ksql.planner.plan.PlanNode)1 KsqlException (io.confluent.ksql.util.KsqlException)1 Schema (org.apache.kafka.connect.data.Schema)1 Test (org.junit.Test)1