Search in sources :

Example 6 with ExecuteResult

use of io.confluent.ksql.KsqlExecutionContext.ExecuteResult in project ksql by confluentinc.

the class KsqlEngineTest method shouldNotThrowWhenExecutingDuplicateStreamWithIfNotExists.

@Test
public void shouldNotThrowWhenExecutingDuplicateStreamWithIfNotExists() {
    // Given:
    final List<ParsedStatement> parsed = ksqlEngine.parse("CREATE STREAM FOO WITH (KAFKA_TOPIC='BAR') AS SELECT * FROM ORDERS; " + "CREATE STREAM IF NOT EXISTS FOO WITH (KAFKA_TOPIC='BAR') AS SELECT * FROM ORDERS;");
    givenStatementAlreadyExecuted(parsed.get(0));
    final PreparedStatement<?> prepared = prepare(parsed.get(1));
    // When:
    ExecuteResult executeResult = ksqlEngine.execute(serviceContext, ConfiguredStatement.of(prepared, SessionConfig.of(ksqlConfig, new HashMap<>())));
    // Then:
    assertThat(executeResult.getQuery(), is(Optional.empty()));
    assertThat(executeResult.getCommandResult(), is(Optional.of("Cannot add stream `FOO`: A stream with the same name already exists.")));
}
Also used : ParsedStatement(io.confluent.ksql.parser.KsqlParser.ParsedStatement) ExecuteResult(io.confluent.ksql.KsqlExecutionContext.ExecuteResult) Test(org.junit.Test)

Example 7 with ExecuteResult

use of io.confluent.ksql.KsqlExecutionContext.ExecuteResult in project ksql by confluentinc.

the class KsqlEngineTest method shouldNotThrowWhenExecutingDuplicateTableWithCreateOrReplaceOnSandbox.

@Test
public void shouldNotThrowWhenExecutingDuplicateTableWithCreateOrReplaceOnSandbox() {
    // Given:
    final ConfiguredStatement<?> oldQuery = configuredStatement("CREATE TABLE FOO AS SELECT * FROM TEST2;");
    final ConfiguredStatement<?> newQuery = configuredStatement("CREATE OR REPLACE TABLE FOO AS SELECT * FROM TEST2;");
    final QueryId oldQueryId = ksqlEngine.execute(serviceContext, oldQuery).getQuery().get().getQueryId();
    sandbox = ksqlEngine.createSandbox(serviceContext);
    // When:
    ExecuteResult executeResult = sandbox.execute(sandboxServiceContext, newQuery);
    // Then:
    assertThat(executeResult.getQuery().get().getQueryId(), is(oldQueryId));
}
Also used : QueryId(io.confluent.ksql.query.QueryId) ExecuteResult(io.confluent.ksql.KsqlExecutionContext.ExecuteResult) Test(org.junit.Test)

Example 8 with ExecuteResult

use of io.confluent.ksql.KsqlExecutionContext.ExecuteResult in project ksql by confluentinc.

the class KsqlEngineTest method shouldHandleMultipleStatements.

@SuppressWarnings("unchecked")
@Test
public void shouldHandleMultipleStatements() {
    // Given:
    final String sql = "" + "-- single line comment\n" + "/*\n" + "   Multi-line comment\n" + "*/\n" + "CREATE STREAM S0 (a INT, b VARCHAR) " + "      WITH (kafka_topic='s0_topic', value_format='DELIMITED', key_format='KAFKA');\n" + "\n" + "CREATE TABLE T1 (f0 BIGINT PRIMARY KEY, f1 DOUBLE) " + "     WITH (kafka_topic='t1_topic', value_format='JSON', key_format='KAFKA');\n" + "\n" + "CREATE STREAM S1 AS SELECT * FROM S0;\n" + "\n" + "CREATE STREAM S2 AS SELECT * FROM S0;\n" + "\n" + "DROP TABLE T1;";
    givenTopicsExist("s0_topic", "t1_topic");
    final List<QueryMetadata> queries = new ArrayList<>();
    // When:
    final List<PreparedStatement<?>> preparedStatements = ksqlEngine.parse(sql).stream().map(stmt -> {
        final PreparedStatement<?> prepared = ksqlEngine.prepare(stmt);
        final ExecuteResult result = ksqlEngine.execute(serviceContext, ConfiguredStatement.of(prepared, SessionConfig.of(ksqlConfig, new HashMap<>())));
        result.getQuery().ifPresent(queries::add);
        return prepared;
    }).collect(Collectors.toList());
    // Then:
    final List<?> statements = preparedStatements.stream().map(PreparedStatement::getStatement).collect(Collectors.toList());
    assertThat(statements, contains(instanceOf(CreateStream.class), instanceOf(CreateTable.class), instanceOf(CreateStreamAsSelect.class), instanceOf(CreateStreamAsSelect.class), instanceOf(DropTable.class)));
    assertThat(queries, hasSize(2));
}
Also used : Query(io.confluent.ksql.parser.tree.Query) Arrays(java.util.Arrays) InternalFunctionRegistry(io.confluent.ksql.function.InternalFunctionRegistry) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) SourceName(io.confluent.ksql.name.SourceName) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Matchers.not(org.hamcrest.Matchers.not) ServiceContext(io.confluent.ksql.services.ServiceContext) KsqlExceptionMatcher.statementText(io.confluent.ksql.util.KsqlExceptionMatcher.statementText) SandboxedTransientQueryMetadata(io.confluent.ksql.util.SandboxedTransientQueryMetadata) MutableMetaStore(io.confluent.ksql.metastore.MutableMetaStore) Matchers.hasItems(org.hamcrest.Matchers.hasItems) TransientQueryMetadata(io.confluent.ksql.util.TransientQueryMetadata) QueryCleanupTask(io.confluent.ksql.engine.QueryCleanupService.QueryCleanupTask) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) ExecuteResult(io.confluent.ksql.KsqlExecutionContext.ExecuteResult) CreateTable(io.confluent.ksql.parser.tree.CreateTable) KsqlEngineTestUtil.execute(io.confluent.ksql.engine.KsqlEngineTestUtil.execute) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Map(java.util.Map) After(org.junit.After) Spy(org.mockito.Spy) SandboxedPersistentQueryMetadataImpl(io.confluent.ksql.util.SandboxedPersistentQueryMetadataImpl) Assert.fail(org.junit.Assert.fail) RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException) QueryId(io.confluent.ksql.query.QueryId) FakeKafkaTopicClient(io.confluent.ksql.services.FakeKafkaTopicClient) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) QueryMetadata(io.confluent.ksql.util.QueryMetadata) MockSchemaRegistryClient(io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient) FakeKafkaConsumerGroupClient(io.confluent.ksql.services.FakeKafkaConsumerGroupClient) SystemColumns(io.confluent.ksql.schema.ksql.SystemColumns) Schema(org.apache.avro.Schema) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) KsqlConfig(io.confluent.ksql.util.KsqlConfig) MetaStoreFixture(io.confluent.ksql.util.MetaStoreFixture) Collectors(java.util.stream.Collectors) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) TestServiceContext(io.confluent.ksql.services.TestServiceContext) List(java.util.List) ParseFailedException(io.confluent.ksql.parser.exception.ParseFailedException) Matchers.contains(org.hamcrest.Matchers.contains) KsqlExecutionContext(io.confluent.ksql.KsqlExecutionContext) Matchers.equalTo(org.hamcrest.Matchers.equalTo) KsqlException(io.confluent.ksql.util.KsqlException) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) Matchers.containsString(org.hamcrest.Matchers.containsString) KsqlConstants(io.confluent.ksql.util.KsqlConstants) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Mockito.mock(org.mockito.Mockito.mock) KsqlExceptionMatcher.rawMessage(io.confluent.ksql.util.KsqlExceptionMatcher.rawMessage) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) StreamsConfig(org.apache.kafka.streams.StreamsConfig) Iterables(com.google.common.collect.Iterables) Assert.assertThrows(org.junit.Assert.assertThrows) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) CreateStreamAsSelect(io.confluent.ksql.parser.tree.CreateStreamAsSelect) ReservedInternalTopics(io.confluent.ksql.util.ReservedInternalTopics) Supplier(java.util.function.Supplier) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) ArrayList(java.util.ArrayList) SessionConfig(io.confluent.ksql.config.SessionConfig) CreateStream(io.confluent.ksql.parser.tree.CreateStream) ParsedStatement(io.confluent.ksql.parser.KsqlParser.ParsedStatement) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Before(org.junit.Before) Collections.emptyMap(java.util.Collections.emptyMap) DropTable(io.confluent.ksql.parser.tree.DropTable) Matchers.empty(org.hamcrest.Matchers.empty) Assert.assertTrue(org.junit.Assert.assertTrue) FieldMatchers.hasFullName(io.confluent.ksql.metastore.model.MetaStoreMatchers.FieldMatchers.hasFullName) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) SandboxedBinPackedPersistentQueryMetadataImpl(io.confluent.ksql.util.SandboxedBinPackedPersistentQueryMetadataImpl) ConfigException(org.apache.kafka.common.config.ConfigException) Mockito.verify(org.mockito.Mockito.verify) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) SchemaBuilder(org.apache.avro.SchemaBuilder) KsqlConfigTestUtil(io.confluent.ksql.KsqlConfigTestUtil) Mockito.never(org.mockito.Mockito.never) Matchers.hasItem(org.hamcrest.Matchers.hasItem) KafkaStreams(org.apache.kafka.streams.KafkaStreams) Assert(org.junit.Assert) Collections(java.util.Collections) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) SandboxedTransientQueryMetadata(io.confluent.ksql.util.SandboxedTransientQueryMetadata) TransientQueryMetadata(io.confluent.ksql.util.TransientQueryMetadata) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) QueryMetadata(io.confluent.ksql.util.QueryMetadata) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) Matchers.containsString(org.hamcrest.Matchers.containsString) ExecuteResult(io.confluent.ksql.KsqlExecutionContext.ExecuteResult) Test(org.junit.Test)

Example 9 with ExecuteResult

use of io.confluent.ksql.KsqlExecutionContext.ExecuteResult in project ksql by confluentinc.

the class KsqlEngineTest method shouldNotThrowWhenExecutingDuplicateTableWithCreateOrReplace.

@Test
public void shouldNotThrowWhenExecutingDuplicateTableWithCreateOrReplace() {
    // Given:
    final ConfiguredStatement<?> oldQuery = configuredStatement("CREATE TABLE FOO AS SELECT * FROM TEST2;");
    final ConfiguredStatement<?> newQuery = configuredStatement("CREATE OR REPLACE TABLE FOO AS SELECT * FROM TEST2;");
    final QueryId oldQueryId = ksqlEngine.execute(serviceContext, oldQuery).getQuery().get().getQueryId();
    // When:
    ExecuteResult executeResult = ksqlEngine.execute(sandboxServiceContext, newQuery);
    // Then:
    assertThat(executeResult.getQuery().get().getQueryId(), is(oldQueryId));
}
Also used : QueryId(io.confluent.ksql.query.QueryId) ExecuteResult(io.confluent.ksql.KsqlExecutionContext.ExecuteResult) Test(org.junit.Test)

Example 10 with ExecuteResult

use of io.confluent.ksql.KsqlExecutionContext.ExecuteResult in project ksql by confluentinc.

the class KsqlEngineTest method shouldExecuteInsertIntoStreamOnSandBox.

@Test
public void shouldExecuteInsertIntoStreamOnSandBox() {
    // Given:
    final List<ParsedStatement> statements = parse("create stream bar as select * from orders;" + "insert into bar select * from orders;");
    givenStatementAlreadyExecuted(statements.get(0));
    // When:
    final ExecuteResult result = sandbox.execute(sandboxServiceContext, ConfiguredStatement.of(sandbox.prepare(statements.get(1)), SessionConfig.of(ksqlConfig, Collections.emptyMap())));
    // Then:
    assertThat(result.getQuery(), is(not(Optional.empty())));
}
Also used : ParsedStatement(io.confluent.ksql.parser.KsqlParser.ParsedStatement) ExecuteResult(io.confluent.ksql.KsqlExecutionContext.ExecuteResult) Test(org.junit.Test)

Aggregations

ExecuteResult (io.confluent.ksql.KsqlExecutionContext.ExecuteResult)14 Test (org.junit.Test)10 ParsedStatement (io.confluent.ksql.parser.KsqlParser.ParsedStatement)6 QueryId (io.confluent.ksql.query.QueryId)6 KsqlConfig (io.confluent.ksql.util.KsqlConfig)5 SessionConfig (io.confluent.ksql.config.SessionConfig)4 ServiceContext (io.confluent.ksql.services.ServiceContext)4 ConfiguredStatement (io.confluent.ksql.statement.ConfiguredStatement)4 PersistentQueryMetadata (io.confluent.ksql.util.PersistentQueryMetadata)4 Collections (java.util.Collections)4 Map (java.util.Map)4 Optional (java.util.Optional)4 Collectors (java.util.stream.Collectors)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Iterables (com.google.common.collect.Iterables)3 QueryMetadata (io.confluent.ksql.util.QueryMetadata)3 List (java.util.List)3 ImmutableList (com.google.common.collect.ImmutableList)2 MockSchemaRegistryClient (io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient)2 SchemaRegistryClient (io.confluent.kafka.schemaregistry.client.SchemaRegistryClient)2