Search in sources :

Example 6 with StructuredDataSource

use of io.confluent.ksql.metastore.StructuredDataSource 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 7 with StructuredDataSource

use of io.confluent.ksql.metastore.StructuredDataSource 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)

Example 8 with StructuredDataSource

use of io.confluent.ksql.metastore.StructuredDataSource in project ksql by confluentinc.

the class JoinNodeTest method shouldHaveAllFieldsFromJoinedInputs.

@Test
public void shouldHaveAllFieldsFromJoinedInputs() {
    setupTopicClientExpectations(1, 1);
    buildJoin();
    final MetaStore metaStore = MetaStoreFixture.getNewMetaStore();
    final StructuredDataSource source1 = metaStore.getSource("TEST1");
    final StructuredDataSource source2 = metaStore.getSource("TEST2");
    final Set<String> expected = source1.getSchema().fields().stream().map(field -> "T1." + field.name()).collect(Collectors.toSet());
    expected.addAll(source2.getSchema().fields().stream().map(field -> "T2." + field.name()).collect(Collectors.toSet()));
    final Set<String> fields = stream.getSchema().fields().stream().map(Field::name).collect(Collectors.toSet());
    assertThat(fields, equalTo(expected));
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) SchemaKStream(io.confluent.ksql.structured.SchemaKStream) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) SchemaKTable(io.confluent.ksql.structured.SchemaKTable) LogicalPlanBuilder(io.confluent.ksql.structured.LogicalPlanBuilder) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) EasyMock.mock(org.easymock.EasyMock.mock) HashMap(java.util.HashMap) KafkaTopicClient(io.confluent.ksql.util.KafkaTopicClient) Schema(org.apache.kafka.connect.data.Schema) TopologyDescription(org.apache.kafka.streams.TopologyDescription) Map(java.util.Map) MetaStore(io.confluent.ksql.metastore.MetaStore) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) LinkedList(java.util.LinkedList) Before(org.junit.Before) Utils(org.apache.kafka.common.utils.Utils) TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) MockSchemaRegistryClient(io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient) SOURCE_NODE(io.confluent.ksql.planner.plan.PlanTestUtil.SOURCE_NODE) Field(org.apache.kafka.connect.data.Field) FunctionRegistry(io.confluent.ksql.function.FunctionRegistry) Set(java.util.Set) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) Test(org.junit.Test) EasyMock(org.easymock.EasyMock) KsqlConfig(io.confluent.ksql.util.KsqlConfig) MetaStoreFixture(io.confluent.ksql.util.MetaStoreFixture) Collectors(java.util.stream.Collectors) List(java.util.List) MAPVALUES_NODE(io.confluent.ksql.planner.plan.PlanTestUtil.MAPVALUES_NODE) KsqlException(io.confluent.ksql.util.KsqlException) Node(org.apache.kafka.common.Node) StructuredDataSource(io.confluent.ksql.metastore.StructuredDataSource) Assert(org.junit.Assert) Collections(java.util.Collections) Topology(org.apache.kafka.streams.Topology) PlanTestUtil.getNodeByName(io.confluent.ksql.planner.plan.PlanTestUtil.getNodeByName) MetaStore(io.confluent.ksql.metastore.MetaStore) StructuredDataSource(io.confluent.ksql.metastore.StructuredDataSource) Test(org.junit.Test)

Example 9 with StructuredDataSource

use of io.confluent.ksql.metastore.StructuredDataSource in project ksql by confluentinc.

the class DataSourceExtractor method visitJoinRelation.

@Override
public Node visitJoinRelation(final SqlBaseParser.JoinRelationContext context) {
    this.isJoin = true;
    AliasedRelation left = (AliasedRelation) visit(context.left);
    AliasedRelation right;
    if (context.CROSS() != null) {
        right = (AliasedRelation) visit(context.right);
    } else {
        if (context.NATURAL() != null) {
            right = (AliasedRelation) visit(context.right);
        } else {
            right = (AliasedRelation) visit(context.rightRelation);
        }
    }
    this.leftAlias = left.getAlias();
    StructuredDataSource leftDataSource = metaStore.getSource(((Table) left.getRelation()).getName().getSuffix());
    if (leftDataSource == null) {
        throw new KsqlException(((Table) left.getRelation()).getName().getSuffix() + " does not " + "exist.");
    }
    this.joinLeftSchema = leftDataSource.getSchema();
    this.rightAlias = right.getAlias();
    StructuredDataSource rightDataSource = metaStore.getSource(((Table) right.getRelation()).getName().getSuffix());
    if (rightDataSource == null) {
        throw new KsqlException(((Table) right.getRelation()).getName().getSuffix() + " does not " + "exist.");
    }
    this.joinRightSchema = rightDataSource.getSchema();
    return null;
}
Also used : StructuredDataSource(io.confluent.ksql.metastore.StructuredDataSource) Table(io.confluent.ksql.parser.tree.Table) AliasedRelation(io.confluent.ksql.parser.tree.AliasedRelation)

Example 10 with StructuredDataSource

use of io.confluent.ksql.metastore.StructuredDataSource in project ksql by confluentinc.

the class QueryEngine method buildQueryLogicalPlan.

private PlanNode buildQueryLogicalPlan(String sqlExpression, final Query query, final MetaStore tempMetaStore) {
    final QueryAnalyzer queryAnalyzer = new QueryAnalyzer(tempMetaStore, ksqlEngine.getFunctionRegistry());
    final Analysis analysis = queryAnalyzer.analyze(sqlExpression, query);
    final AggregateAnalysis aggAnalysis = queryAnalyzer.analyzeAggregate(query, analysis);
    final PlanNode logicalPlan = new LogicalPlanner(analysis, aggAnalysis, ksqlEngine.getFunctionRegistry()).buildPlan();
    if (logicalPlan instanceof KsqlStructuredDataOutputNode) {
        KsqlStructuredDataOutputNode ksqlStructuredDataOutputNode = (KsqlStructuredDataOutputNode) logicalPlan;
        StructuredDataSource structuredDataSource = new KsqlStream(sqlExpression, ksqlStructuredDataOutputNode.getId().toString(), ksqlStructuredDataOutputNode.getSchema(), ksqlStructuredDataOutputNode.getKeyField(), ksqlStructuredDataOutputNode.getTimestampField() == null ? ksqlStructuredDataOutputNode.getTheSourceNode().getTimestampField() : ksqlStructuredDataOutputNode.getTimestampField(), ksqlStructuredDataOutputNode.getKsqlTopic());
        tempMetaStore.putTopic(ksqlStructuredDataOutputNode.getKsqlTopic());
        tempMetaStore.putSource(structuredDataSource.cloneWithTimeKeyColumns());
    }
    return logicalPlan;
}
Also used : StructuredDataSource(io.confluent.ksql.metastore.StructuredDataSource) KsqlStream(io.confluent.ksql.metastore.KsqlStream) PlanNode(io.confluent.ksql.planner.plan.PlanNode) LogicalPlanner(io.confluent.ksql.planner.LogicalPlanner) Analysis(io.confluent.ksql.analyzer.Analysis) AggregateAnalysis(io.confluent.ksql.analyzer.AggregateAnalysis) QueryAnalyzer(io.confluent.ksql.analyzer.QueryAnalyzer) AggregateAnalysis(io.confluent.ksql.analyzer.AggregateAnalysis) KsqlStructuredDataOutputNode(io.confluent.ksql.planner.plan.KsqlStructuredDataOutputNode)

Aggregations

StructuredDataSource (io.confluent.ksql.metastore.StructuredDataSource)17 KsqlStream (io.confluent.ksql.metastore.KsqlStream)8 KsqlException (io.confluent.ksql.util.KsqlException)7 AliasedRelation (io.confluent.ksql.parser.tree.AliasedRelation)4 Table (io.confluent.ksql.parser.tree.Table)4 KsqlTable (io.confluent.ksql.metastore.KsqlTable)3 KsqlTopic (io.confluent.ksql.metastore.KsqlTopic)3 StructuredDataSourceNode (io.confluent.ksql.planner.plan.StructuredDataSourceNode)3 Test (org.junit.Test)3 CreateTable (io.confluent.ksql.parser.tree.CreateTable)2 DropTable (io.confluent.ksql.parser.tree.DropTable)2 SelectItem (io.confluent.ksql.parser.tree.SelectItem)2 SingleColumn (io.confluent.ksql.parser.tree.SingleColumn)2 PlanNode (io.confluent.ksql.planner.plan.PlanNode)2 PlanNodeId (io.confluent.ksql.planner.plan.PlanNodeId)2 SchemaKTable (io.confluent.ksql.structured.SchemaKTable)2 Pair (io.confluent.ksql.util.Pair)2 ArrayList (java.util.ArrayList)2 Topology (org.apache.kafka.streams.Topology)2 MockSchemaRegistryClient (io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient)1