Search in sources :

Example 21 with PreparedStatement

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

the class DefaultSchemaInjectorFunctionalTest method shouldInferSchema.

private void shouldInferSchema(final org.apache.avro.Schema avroSchema, final Schema expectedKqlSchema) {
    // Given:
    final Schema expectedSchema = new ConnectKsqlSchemaTranslator(SchemaTranslationPolicies.of(this.translationPolicy)).toKsqlSchema(expectedKqlSchema);
    try {
        if (testType == TestType.WITHOUT_SCHEMA_ID) {
            when(srClient.getLatestSchemaMetadata(any())).thenReturn(new SchemaMetadata(1, 1, avroSchema.toString()));
        }
        when(srClient.getSchemaBySubjectAndId(any(), anyInt())).thenReturn(this.avroSchema);
        when(this.avroSchema.schemaType()).thenReturn("AVRO");
        when(this.avroSchema.rawSchema()).thenReturn(avroSchema);
        constructCSASSource(expectedSchema);
    } catch (final Exception e) {
        throw new AssertionError(e);
    }
    final PreparedStatement<Statement> prepared = KsqlParserTestUtil.buildSingleAst(this.statement, metaStore, true);
    // When:
    final KsqlConfig ksqlConfig = new KsqlConfig(ImmutableMap.of());
    final ConfiguredStatement<?> inferred = schemaInjector.inject(ConfiguredStatement.of(prepared, SessionConfig.of(ksqlConfig, ImmutableMap.of())));
    // Then:
    if (testType == TestType.CSAS_WITH_SCHEMA_ID) {
        validateCsasInference((ConfiguredStatement<CreateStreamAsSelect>) inferred, this.avroSchema);
    } else {
        final Statement withSchema = KsqlParserTestUtil.buildSingleAst(inferred.getStatementText(), metaStore, true).getStatement();
        final Schema actual = getSchemaForDdlStatement((CreateSource) withSchema);
        assertThat(FORMATTER.format(actual), equalTo(FORMATTER.format(expectedSchema)));
        assertThat(actual, equalTo(expectedSchema));
    }
}
Also used : SchemaMetadata(io.confluent.kafka.schemaregistry.client.SchemaMetadata) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) Statement(io.confluent.ksql.parser.tree.Statement) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) Schema(org.apache.kafka.connect.data.Schema) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) KsqlConfig(io.confluent.ksql.util.KsqlConfig) ConnectKsqlSchemaTranslator(io.confluent.ksql.serde.connect.ConnectKsqlSchemaTranslator) CreateStreamAsSelect(io.confluent.ksql.parser.tree.CreateStreamAsSelect)

Example 22 with PreparedStatement

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

the class WSQueryEndpoint method executeStreamQuery.

public void executeStreamQuery(final ServerWebSocket webSocket, final MultiMap requestParams, final KsqlSecurityContext ksqlSecurityContext, final Context context) {
    try {
        final long startTimeNanos = Time.SYSTEM.nanoseconds();
        activenessRegistrar.updateLastRequestTime();
        validateVersion(requestParams);
        final KsqlRequest request = parseRequest(requestParams);
        try {
            CommandStoreUtil.waitForCommandSequenceNumber(commandQueue, request, commandQueueCatchupTimeout);
        } catch (final InterruptedException e) {
            log.debug("Interrupted while waiting for command queue " + "to reach specified command sequence number", e);
            SessionUtil.closeSilently(webSocket, INTERNAL_SERVER_ERROR.code(), e.getMessage());
            return;
        } catch (final TimeoutException e) {
            log.debug("Timeout while processing request", e);
            SessionUtil.closeSilently(webSocket, TRY_AGAIN_LATER.code(), e.getMessage());
            return;
        }
        final PreparedStatement<?> preparedStatement = parseStatement(request);
        final Statement statement = preparedStatement.getStatement();
        authorizationValidator.ifPresent(validator -> validator.checkAuthorization(ksqlSecurityContext, ksqlEngine.getMetaStore(), statement));
        final RequestContext requestContext = new RequestContext(webSocket, request, ksqlSecurityContext);
        if (statement instanceof Query) {
            handleQuery(requestContext, (Query) statement, startTimeNanos, context);
        } else if (statement instanceof PrintTopic) {
            handlePrintTopic(requestContext, (PrintTopic) statement);
        } else {
            throw new IllegalArgumentException("Unexpected statement type " + statement);
        }
    } catch (final TopicAuthorizationException e) {
        log.debug("Error processing request", e);
        SessionUtil.closeSilently(webSocket, INVALID_MESSAGE_TYPE.code(), errorHandler.kafkaAuthorizationErrorMessage(e));
    } catch (final Exception e) {
        log.debug("Error processing request", e);
        SessionUtil.closeSilently(webSocket, INVALID_MESSAGE_TYPE.code(), e.getMessage());
    }
}
Also used : Query(io.confluent.ksql.parser.tree.Query) KsqlRequest(io.confluent.ksql.rest.entity.KsqlRequest) Statement(io.confluent.ksql.parser.tree.Statement) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) PrintTopic(io.confluent.ksql.parser.tree.PrintTopic) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) TimeoutException(java.util.concurrent.TimeoutException) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) TimeoutException(java.util.concurrent.TimeoutException)

Example 23 with PreparedStatement

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

the class KsqlContextTest method shouldUnsetProperty.

@SuppressWarnings("unchecked")
@Test
public void shouldUnsetProperty() {
    // Given:
    when(ksqlEngine.parse(any())).thenReturn(ImmutableList.of(PARSED_STMT_0, PARSED_STMT_0));
    final Map<String, Object> properties = ImmutableMap.of(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
    final PreparedStatement<UnsetProperty> unset = PreparedStatement.of("UNSET SOMETHING", new UnsetProperty(Optional.empty(), ConsumerConfig.AUTO_OFFSET_RESET_CONFIG));
    when(ksqlEngine.prepare(any())).thenReturn((PreparedStatement) unset).thenReturn(PREPARED_STMT_0);
    // When:
    ksqlContext.sql("SQL;", properties);
    // Then:
    verify(ksqlEngine).execute(serviceContext, ConfiguredStatement.of(PREPARED_STMT_0, SessionConfig.of(SOME_CONFIG, ImmutableMap.of())));
}
Also used : UnsetProperty(io.confluent.ksql.parser.tree.UnsetProperty) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 24 with PreparedStatement

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

the class KsqlContextTest method shouldSetPropertyOnlyOnCommandsFollowingTheSetStatement.

@SuppressWarnings("unchecked")
@Test
public void shouldSetPropertyOnlyOnCommandsFollowingTheSetStatement() {
    // Given:
    when(ksqlEngine.parse(any())).thenReturn(ImmutableList.of(PARSED_STMT_0, PARSED_STMT_0));
    final PreparedStatement<SetProperty> set = PreparedStatement.of("SET SOMETHING", new SetProperty(Optional.empty(), ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"));
    when(ksqlEngine.prepare(any())).thenReturn((PreparedStatement) PREPARED_STMT_0).thenReturn(set);
    // When:
    ksqlContext.sql("SQL;", ImmutableMap.of());
    // Then:
    verify(ksqlEngine).execute(serviceContext, ConfiguredStatement.of(PREPARED_STMT_0, SessionConfig.of(SOME_CONFIG, ImmutableMap.of())));
}
Also used : PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) SetProperty(io.confluent.ksql.parser.tree.SetProperty) Test(org.junit.Test)

Example 25 with PreparedStatement

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

the class KsqlContextTest method shouldSetProperty.

@SuppressWarnings("unchecked")
@Test
public void shouldSetProperty() {
    // Given:
    when(ksqlEngine.parse(any())).thenReturn(ImmutableList.of(PARSED_STMT_0, PARSED_STMT_0));
    final PreparedStatement<SetProperty> set = PreparedStatement.of("SET SOMETHING", new SetProperty(Optional.empty(), ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"));
    when(ksqlEngine.prepare(any())).thenReturn((PreparedStatement) set).thenReturn(PREPARED_STMT_0);
    // When:
    ksqlContext.sql("SQL;", ImmutableMap.of());
    // Then:
    verify(ksqlEngine).execute(serviceContext, ConfiguredStatement.of(PREPARED_STMT_0, SessionConfig.of(SOME_CONFIG, ImmutableMap.of(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"))));
}
Also used : PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) SetProperty(io.confluent.ksql.parser.tree.SetProperty) Test(org.junit.Test)

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