Search in sources :

Example 11 with Pair

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

the class StatementExecutorTest method shouldHandlePriorStatement.

@Test
public void shouldHandlePriorStatement() throws Exception {
    TestUtils testUtils = new TestUtils();
    List<Pair<CommandId, Command>> priorCommands = testUtils.getAllPriorCommandRecords();
    final RestoreCommands restoreCommands = new RestoreCommands();
    priorCommands.forEach(pair -> restoreCommands.addCommand(pair.left, pair.right));
    CommandId topicCommandId = new CommandId(CommandId.Type.TOPIC, "_CSASTopicGen", CommandId.Action.CREATE);
    CommandId csCommandId = new CommandId(CommandId.Type.STREAM, "_CSASStreamGen", CommandId.Action.CREATE);
    CommandId csasCommandId = new CommandId(CommandId.Type.STREAM, "_CSASGen", CommandId.Action.CREATE);
    CommandId ctasCommandId = new CommandId(CommandId.Type.TABLE, "_CTASGen", CommandId.Action.CREATE);
    statementExecutor.handleRestoration(restoreCommands);
    Map<CommandId, CommandStatus> statusStore = statementExecutor.getStatuses();
    Assert.assertNotNull(statusStore);
    Assert.assertEquals(4, statusStore.size());
    Assert.assertEquals(CommandStatus.Status.SUCCESS, statusStore.get(topicCommandId).getStatus());
    Assert.assertEquals(CommandStatus.Status.SUCCESS, statusStore.get(csCommandId).getStatus());
    Assert.assertEquals(CommandStatus.Status.SUCCESS, statusStore.get(csasCommandId).getStatus());
    Assert.assertEquals(CommandStatus.Status.ERROR, statusStore.get(ctasCommandId).getStatus());
}
Also used : TestUtils(io.confluent.ksql.rest.server.utils.TestUtils) CommandStatus(io.confluent.ksql.rest.entity.CommandStatus) Pair(io.confluent.ksql.util.Pair) Test(org.junit.Test)

Example 12 with Pair

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

the class SelectValueMapperTest method createMapper.

private SelectValueMapper createMapper(final String query) throws Exception {
    final PlanNode planNode = planBuilder.buildLogicalPlan(query);
    final ProjectNode projectNode = (ProjectNode) planNode.getSources().get(0);
    final Schema schema = planNode.getTheSourceNode().getSchema();
    final List<Pair<String, Expression>> expressionPairList = projectNode.getProjectNameExpressionPairList();
    final List<ExpressionMetadata> metadata = createExpressionMetadata(expressionPairList, schema);
    return new SelectValueMapper(new GenericRowValueTypeEnforcer(schema), expressionPairList, metadata);
}
Also used : ExpressionMetadata(io.confluent.ksql.util.ExpressionMetadata) PlanNode(io.confluent.ksql.planner.plan.PlanNode) GenericRowValueTypeEnforcer(io.confluent.ksql.util.GenericRowValueTypeEnforcer) Schema(org.apache.kafka.connect.data.Schema) ProjectNode(io.confluent.ksql.planner.plan.ProjectNode) Pair(io.confluent.ksql.util.Pair)

Example 13 with Pair

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

the class ProjectNodeTest method shouldCreateProjectionWithFieldNameExpressionPairs.

@Test
public void shouldCreateProjectionWithFieldNameExpressionPairs() {
    mockSourceNode();
    final BooleanLiteral trueExpression = new BooleanLiteral("true");
    final BooleanLiteral falseExpression = new BooleanLiteral("false");
    EasyMock.expect(stream.select(Arrays.asList(new Pair<>("field1", trueExpression), new Pair<>("field2", falseExpression)))).andReturn(stream);
    EasyMock.replay(source, stream);
    final ProjectNode node = new ProjectNode(new PlanNodeId("1"), source, SchemaBuilder.struct().field("field1", Schema.STRING_SCHEMA).field("field2", Schema.STRING_SCHEMA).build(), Arrays.asList(trueExpression, falseExpression));
    node.buildStream(builder, ksqlConfig, kafkaTopicClient, functionRegistry, props, new MockSchemaRegistryClient());
    EasyMock.verify(stream);
}
Also used : BooleanLiteral(io.confluent.ksql.parser.tree.BooleanLiteral) MockSchemaRegistryClient(io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient) Pair(io.confluent.ksql.util.Pair) Test(org.junit.Test)

Example 14 with Pair

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

the class KsqlEngine method buildSingleQueryAst.

private Pair<String, Statement> buildSingleQueryAst(final Statement statement, final String statementString, final MetaStore tempMetaStore, final MetaStore tempMetaStoreForParser, final Map<String, Object> overriddenProperties) {
    log.info("Building AST for {}.", statementString);
    if (statement instanceof Query) {
        return new Pair<>(statementString, statement);
    } else if (statement instanceof CreateAsSelect) {
        CreateAsSelect createAsSelect = (CreateAsSelect) statement;
        QuerySpecification querySpecification = (QuerySpecification) createAsSelect.getQuery().getQueryBody();
        Query query = addInto(createAsSelect.getQuery(), querySpecification, createAsSelect.getName().getSuffix(), createAsSelect.getProperties(), createAsSelect.getPartitionByColumn());
        tempMetaStoreForParser.putSource(queryEngine.getResultDatasource(querySpecification.getSelect(), createAsSelect.getName().getSuffix()).cloneWithTimeKeyColumns());
        return new Pair<>(statementString, query);
    } else if (statement instanceof RegisterTopic) {
        ddlCommandExec.tryExecute(new RegisterTopicCommand((RegisterTopic) statement), tempMetaStoreForParser);
        ddlCommandExec.tryExecute(new RegisterTopicCommand((RegisterTopic) statement), tempMetaStore);
        return new Pair<>(statementString, statement);
    } else if (statement instanceof CreateStream) {
        ddlCommandExec.tryExecute(new CreateStreamCommand(statementString, (CreateStream) statement, overriddenProperties, topicClient, false), tempMetaStoreForParser);
        ddlCommandExec.tryExecute(new CreateStreamCommand(statementString, (CreateStream) statement, overriddenProperties, topicClient, false), tempMetaStore);
        return new Pair<>(statementString, statement);
    } else if (statement instanceof CreateTable) {
        ddlCommandExec.tryExecute(new CreateTableCommand(statementString, (CreateTable) statement, overriddenProperties, topicClient, false), tempMetaStoreForParser);
        ddlCommandExec.tryExecute(new CreateTableCommand(statementString, (CreateTable) statement, overriddenProperties, topicClient, false), tempMetaStore);
        return new Pair<>(statementString, statement);
    } else if (statement instanceof DropStream) {
        ddlCommandExec.tryExecute(new DropSourceCommand((DropStream) statement, DataSource.DataSourceType.KSTREAM, this), tempMetaStore);
        ddlCommandExec.tryExecute(new DropSourceCommand((DropStream) statement, DataSource.DataSourceType.KSTREAM, this), tempMetaStoreForParser);
        return new Pair<>(statementString, statement);
    } else if (statement instanceof DropTable) {
        ddlCommandExec.tryExecute(new DropSourceCommand((DropTable) statement, DataSource.DataSourceType.KTABLE, this), tempMetaStore);
        ddlCommandExec.tryExecute(new DropSourceCommand((DropTable) statement, DataSource.DataSourceType.KTABLE, this), tempMetaStoreForParser);
        return new Pair<>(statementString, statement);
    } else if (statement instanceof DropTopic) {
        ddlCommandExec.tryExecute(new DropTopicCommand((DropTopic) statement), tempMetaStore);
        ddlCommandExec.tryExecute(new DropTopicCommand((DropTopic) statement), tempMetaStoreForParser);
        return new Pair<>(statementString, statement);
    } else if (statement instanceof SetProperty) {
        return new Pair<>(statementString, statement);
    }
    return null;
}
Also used : DropTopicCommand(io.confluent.ksql.ddl.commands.DropTopicCommand) Query(io.confluent.ksql.parser.tree.Query) RegisterTopic(io.confluent.ksql.parser.tree.RegisterTopic) DropSourceCommand(io.confluent.ksql.ddl.commands.DropSourceCommand) CreateTable(io.confluent.ksql.parser.tree.CreateTable) CreateStream(io.confluent.ksql.parser.tree.CreateStream) CreateAsSelect(io.confluent.ksql.parser.tree.CreateAsSelect) RegisterTopicCommand(io.confluent.ksql.ddl.commands.RegisterTopicCommand) DropTable(io.confluent.ksql.parser.tree.DropTable) CreateTableCommand(io.confluent.ksql.ddl.commands.CreateTableCommand) QuerySpecification(io.confluent.ksql.parser.tree.QuerySpecification) CreateStreamCommand(io.confluent.ksql.ddl.commands.CreateStreamCommand) DropTopic(io.confluent.ksql.parser.tree.DropTopic) DropStream(io.confluent.ksql.parser.tree.DropStream) SetProperty(io.confluent.ksql.parser.tree.SetProperty) Pair(io.confluent.ksql.util.Pair)

Example 15 with Pair

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

the class QueryEngine method maybeAddFieldsFromSchemaRegistry.

private Pair<DdlStatement, String> maybeAddFieldsFromSchemaRegistry(AbstractStreamCreateStatement streamCreateStatement) {
    if (streamCreateStatement.getProperties().containsKey(DdlConfig.TOPIC_NAME_PROPERTY)) {
        String ksqlRegisteredTopicName = StringUtil.cleanQuotes(streamCreateStatement.getProperties().get(DdlConfig.TOPIC_NAME_PROPERTY).toString().toUpperCase());
        KsqlTopic ksqlTopic = ksqlEngine.getMetaStore().getTopic(ksqlRegisteredTopicName);
        if (ksqlTopic == null) {
            throw new KsqlException(String.format("Could not find %s topic in the metastore.", ksqlRegisteredTopicName));
        }
        Map<String, Expression> newProperties = new HashMap<>();
        newProperties.put(DdlConfig.KAFKA_TOPIC_NAME_PROPERTY, new StringLiteral(ksqlTopic.getKafkaTopicName()));
        newProperties.put(DdlConfig.VALUE_FORMAT_PROPERTY, new StringLiteral(ksqlTopic.getKsqlTopicSerDe().getSerDe().toString()));
        streamCreateStatement = streamCreateStatement.copyWith(streamCreateStatement.getElements(), newProperties);
    }
    Pair<AbstractStreamCreateStatement, String> avroCheckResult = new AvroUtil().checkAndSetAvroSchema(streamCreateStatement, new HashMap<>(), ksqlEngine.getSchemaRegistryClient());
    if (avroCheckResult.getRight() != null) {
        if (avroCheckResult.getLeft() instanceof CreateStream) {
            return new Pair<>((CreateStream) avroCheckResult.getLeft(), avroCheckResult.getRight());
        } else if (avroCheckResult.getLeft() instanceof CreateTable) {
            return new Pair<>((CreateTable) avroCheckResult.getLeft(), avroCheckResult.getRight());
        }
    }
    return new Pair<>(null, null);
}
Also used : HashMap(java.util.HashMap) CreateTable(io.confluent.ksql.parser.tree.CreateTable) CreateStream(io.confluent.ksql.parser.tree.CreateStream) AbstractStreamCreateStatement(io.confluent.ksql.parser.tree.AbstractStreamCreateStatement) KsqlException(io.confluent.ksql.util.KsqlException) AvroUtil(io.confluent.ksql.util.AvroUtil) StringLiteral(io.confluent.ksql.parser.tree.StringLiteral) Expression(io.confluent.ksql.parser.tree.Expression) KsqlTopic(io.confluent.ksql.metastore.KsqlTopic) Pair(io.confluent.ksql.util.Pair)

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