Search in sources :

Example 26 with PreparedStatement

use of io.confluent.ksql.parser.KsqlParser.PreparedStatement in project ksql by confluentinc.

the class KsqlParserTest method shouldParseMultiLineWithInlineBracketedComments.

@Test
public void shouldParseMultiLineWithInlineBracketedComments() {
    final String statementString = "SHOW /* inline\n" + "comment */\n" + "STREAMS;";
    final List<PreparedStatement<?>> statements = KsqlParserTestUtil.buildAst(statementString, metaStore);
    assertThat(statements, hasSize(1));
    assertThat(statements.get(0).getStatement(), is(instanceOf(ListStreams.class)));
}
Also used : PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 27 with PreparedStatement

use of io.confluent.ksql.parser.KsqlParser.PreparedStatement in project ksql by confluentinc.

the class KsqlResourceTest method shouldSupportTopicInferenceInVerification.

@Test
public void shouldSupportTopicInferenceInVerification() {
    // Given:
    givenMockEngine();
    givenSource(DataSourceType.KSTREAM, "ORDERS1", "ORDERS1", SOME_SCHEMA);
    final String sql = "CREATE STREAM orders2 AS SELECT * FROM orders1;";
    final String sqlWithTopic = "CREATE STREAM orders2 WITH(kafka_topic='orders2') AS SELECT * FROM orders1;";
    final PreparedStatement<?> statementWithTopic = ksqlEngine.prepare(ksqlEngine.parse(sqlWithTopic).get(0), Collections.emptyMap());
    final ConfiguredStatement<?> configuredStatement = ConfiguredStatement.of(statementWithTopic, SessionConfig.of(ksqlConfig, ImmutableMap.of()));
    when(sandboxTopicInjector.inject(argThat(is(configured(preparedStatementText(sql)))))).thenReturn((ConfiguredStatement<Statement>) configuredStatement);
    // When:
    makeRequest(sql);
    // Then:
    verify(sandbox).plan(any(SandboxedServiceContext.class), eq(configuredStatement));
    verify(commandStore).enqueueCommand(any(), argThat(is(commandWithStatement(sql))), any(Producer.class));
}
Also used : Producer(org.apache.kafka.clients.producer.Producer) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) Statement(io.confluent.ksql.parser.tree.Statement) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SandboxedServiceContext(io.confluent.ksql.services.SandboxedServiceContext) Test(org.junit.Test)

Example 28 with PreparedStatement

use of io.confluent.ksql.parser.KsqlParser.PreparedStatement in project ksql by confluentinc.

the class KsqlResourceTest method shouldSupportTopicInferenceInExecution.

@Test
public void shouldSupportTopicInferenceInExecution() {
    // Given:
    givenMockEngine();
    givenSource(DataSourceType.KSTREAM, "ORDERS1", "ORDERS1", SOME_SCHEMA);
    final String sql = "CREATE STREAM orders2 AS SELECT * FROM orders1;";
    final String sqlWithTopic = "CREATE STREAM orders2 WITH(kafka_topic='orders2') AS SELECT * FROM orders1;";
    final PreparedStatement<?> statementWithTopic = ksqlEngine.prepare(ksqlEngine.parse(sqlWithTopic).get(0), Collections.emptyMap());
    final ConfiguredStatement<?> configured = ConfiguredStatement.of(statementWithTopic, SessionConfig.of(ksqlConfig, ImmutableMap.of()));
    when(topicInjector.inject(argThat(is(configured(preparedStatementText(sql)))))).thenReturn((ConfiguredStatement<Statement>) configured);
    // When:
    makeRequest(sql);
    // Then:
    verify(commandStore).enqueueCommand(any(), argThat(is(commandWithStatement(sqlWithTopic))), any());
}
Also used : ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) Statement(io.confluent.ksql.parser.tree.Statement) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 29 with PreparedStatement

use of io.confluent.ksql.parser.KsqlParser.PreparedStatement in project ksql by confluentinc.

the class RequestHandler method executeStatement.

@SuppressWarnings("unchecked")
private <T extends Statement> Optional<KsqlEntity> executeStatement(final KsqlSecurityContext securityContext, final PreparedStatement<T> prepared, final SessionProperties sessionProperties, final KsqlEntityList entities) {
    final Class<? extends Statement> statementClass = prepared.getStatement().getClass();
    commandQueueSync.waitFor(new KsqlEntityList(entities), statementClass);
    final ConfiguredStatement<T> configured = ConfiguredStatement.of(prepared, SessionConfig.of(this.ksqlEngine.getKsqlConfig(), sessionProperties.getMutableScopedProperties()));
    FeatureFlagChecker.throwOnDisabledFeatures(configured);
    final StatementExecutor<T> executor = (StatementExecutor<T>) customExecutors.getOrDefault(statementClass, (stmt, props, ctx, svcCtx) -> distributor.execute(stmt, ctx, securityContext));
    final StatementExecutorResponse response = executor.execute(configured, sessionProperties, ksqlEngine, securityContext.getServiceContext());
    if (response.isHandled()) {
        return response.getEntity();
    } else {
        return distributor.execute(configured, ksqlEngine, securityContext).getEntity();
    }
}
Also used : KsqlEntityList(io.confluent.ksql.rest.entity.KsqlEntityList) SessionProperties(io.confluent.ksql.rest.SessionProperties) KsqlEngine(io.confluent.ksql.engine.KsqlEngine) KsqlEntityList(io.confluent.ksql.rest.entity.KsqlEntityList) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) KsqlConfig(io.confluent.ksql.util.KsqlConfig) FeatureFlagChecker(io.confluent.ksql.rest.util.FeatureFlagChecker) KsqlSecurityContext(io.confluent.ksql.security.KsqlSecurityContext) DistributingExecutor(io.confluent.ksql.rest.server.computation.DistributingExecutor) SessionConfig(io.confluent.ksql.config.SessionConfig) KsqlEntity(io.confluent.ksql.rest.entity.KsqlEntity) Objects(java.util.Objects) List(java.util.List) Map(java.util.Map) ParsedStatement(io.confluent.ksql.parser.KsqlParser.ParsedStatement) Optional(java.util.Optional) Statement(io.confluent.ksql.parser.tree.Statement) Collections(java.util.Collections) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement)

Example 30 with PreparedStatement

use of io.confluent.ksql.parser.KsqlParser.PreparedStatement in project ksql by confluentinc.

the class InteractiveStatementExecutor method executeStatement.

@SuppressWarnings("unchecked")
private void executeStatement(final PreparedStatement<?> statement, final CommandId commandId, final Optional<CommandStatusFuture> commandStatusFuture) {
    if (statement.getStatement() instanceof TerminateQuery) {
        terminateQuery((PreparedStatement<TerminateQuery>) statement);
        final String successMessage = "Query terminated.";
        final CommandStatus successStatus = new CommandStatus(CommandStatus.Status.SUCCESS, successMessage, Optional.empty());
        putFinalStatus(commandId, commandStatusFuture, successStatus);
    } else if (statement.getStatement() instanceof ExecutableDdlStatement) {
        throwUnsupportedStatementError();
    } else if (statement.getStatement() instanceof CreateAsSelect) {
        throwUnsupportedStatementError();
    } else if (statement.getStatement() instanceof InsertInto) {
        throwUnsupportedStatementError();
    } else if (statement.getStatement() instanceof AlterSystemProperty) {
        final PreparedStatement<AlterSystemProperty> alterSystemQuery = (PreparedStatement<AlterSystemProperty>) statement;
        final String propertyName = alterSystemQuery.getStatement().getPropertyName();
        final String propertyValue = alterSystemQuery.getStatement().getPropertyValue();
        ksqlEngine.alterSystemProperty(propertyName, propertyValue);
        ksqlEngine.updateStreamsPropertiesAndRestartRuntime();
        final String successMessage = String.format("System property %s was set to %s.", propertyName, propertyValue);
        final CommandStatus successStatus = new CommandStatus(CommandStatus.Status.SUCCESS, successMessage, Optional.empty());
        putFinalStatus(commandId, commandStatusFuture, successStatus);
    } else {
        throw new KsqlException(String.format("Unexpected statement type: %s", statement.getClass().getName()));
    }
}
Also used : TerminateQuery(io.confluent.ksql.parser.tree.TerminateQuery) AlterSystemProperty(io.confluent.ksql.parser.tree.AlterSystemProperty) CommandStatus(io.confluent.ksql.rest.entity.CommandStatus) InsertInto(io.confluent.ksql.parser.tree.InsertInto) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) ExecutableDdlStatement(io.confluent.ksql.parser.tree.ExecutableDdlStatement) CreateAsSelect(io.confluent.ksql.parser.tree.CreateAsSelect) KsqlException(io.confluent.ksql.util.KsqlException)

Aggregations

PreparedStatement (io.confluent.ksql.parser.KsqlParser.PreparedStatement)32 ConfiguredStatement (io.confluent.ksql.statement.ConfiguredStatement)21 Test (org.junit.Test)20 Statement (io.confluent.ksql.parser.tree.Statement)18 KsqlException (io.confluent.ksql.util.KsqlException)15 Query (io.confluent.ksql.parser.tree.Query)9 Matchers.containsString (org.hamcrest.Matchers.containsString)8 KsqlTopicAuthorizationException (io.confluent.ksql.exception.KsqlTopicAuthorizationException)7 ParsedStatement (io.confluent.ksql.parser.KsqlParser.ParsedStatement)7 InsertInto (io.confluent.ksql.parser.tree.InsertInto)7 KsqlRestException (io.confluent.ksql.rest.server.resources.KsqlRestException)7 ServiceContext (io.confluent.ksql.services.ServiceContext)7 KsqlConfig (io.confluent.ksql.util.KsqlConfig)7 Optional (java.util.Optional)7 KsqlExecutionContext (io.confluent.ksql.KsqlExecutionContext)6 SessionConfig (io.confluent.ksql.config.SessionConfig)6 DataSource (io.confluent.ksql.metastore.model.DataSource)6 KsqlServerException (io.confluent.ksql.util.KsqlServerException)6 KsqlStatementException (io.confluent.ksql.util.KsqlStatementException)6 List (java.util.List)6