Search in sources :

Example 6 with PreparedStatement

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

the class StreamedQueryResource method handleStatement.

@SuppressWarnings("unchecked")
private EndpointResponse handleStatement(final KsqlSecurityContext securityContext, final KsqlRequest request, final PreparedStatement<?> statement, final CompletableFuture<Void> connectionClosedFuture, final Optional<Boolean> isInternalRequest, final MetricsCallbackHolder metricsCallbackHolder, final Context context) {
    try {
        authorizationValidator.ifPresent(validator -> validator.checkAuthorization(securityContext, ksqlEngine.getMetaStore(), statement.getStatement()));
        final Map<String, Object> configProperties = request.getConfigOverrides();
        denyListPropertyValidator.validateAll(configProperties);
        if (statement.getStatement() instanceof Query) {
            if (shouldMigrateToQueryStream(request.getConfigOverrides())) {
                return EndpointResponse.ok(new NextHandlerOutput());
            }
            final QueryMetadataHolder queryMetadataHolder = queryExecutor.handleStatement(securityContext.getServiceContext(), request.getConfigOverrides(), request.getRequestProperties(), statement, isInternalRequest, metricsCallbackHolder, context, false);
            return handleQuery((PreparedStatement<Query>) statement, connectionClosedFuture, queryMetadataHolder);
        } else if (statement.getStatement() instanceof PrintTopic) {
            return handlePrintTopic(securityContext.getServiceContext(), configProperties, (PreparedStatement<PrintTopic>) statement, connectionClosedFuture);
        } else {
            return Errors.badRequest(String.format("Statement type `%s' not supported for this resource", statement.getClass().getName()));
        }
    } catch (final TopicAuthorizationException e) {
        return errorHandler.accessDeniedFromKafkaResponse(e);
    } catch (final KsqlStatementException e) {
        return Errors.badStatement(e.getRawMessage(), e.getSqlStatement());
    } catch (final KsqlException e) {
        return errorHandler.generateResponse(e, Errors.badRequest(e));
    }
}
Also used : QueryMetadataHolder(io.confluent.ksql.rest.server.query.QueryMetadataHolder) Query(io.confluent.ksql.parser.tree.Query) PrintTopic(io.confluent.ksql.parser.tree.PrintTopic) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) KsqlException(io.confluent.ksql.util.KsqlException) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) NextHandlerOutput(io.confluent.ksql.api.server.NextHandlerOutput)

Example 7 with PreparedStatement

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

the class DefaultSchemaInjector method buildElements.

private static TableElements buildElements(final ConfiguredStatement<CreateSource> preparedStatement, final Optional<SchemaAndId> keySchema, final Optional<SchemaAndId> valueSchema) {
    final List<TableElement> elements = new ArrayList<>();
    if (keySchema.isPresent()) {
        final ColumnConstraints constraints = getKeyConstraints(preparedStatement.getStatement());
        keySchema.get().columns.stream().map(col -> new TableElement(col.name(), new Type(col.type()), constraints)).forEach(elements::add);
    } else {
        getKeyColumns(preparedStatement).forEach(elements::add);
    }
    if (valueSchema.isPresent()) {
        valueSchema.get().columns.stream().map(col -> new TableElement(col.name(), new Type(col.type()))).forEach(elements::add);
    } else {
        getValueColumns(preparedStatement).forEach(elements::add);
    }
    return TableElements.of(elements);
}
Also used : IntStream(java.util.stream.IntStream) FormatFactory(io.confluent.ksql.serde.FormatFactory) ServiceContext(io.confluent.ksql.services.ServiceContext) SerdeFeaturesFactory(io.confluent.ksql.serde.SerdeFeaturesFactory) CommonCreateConfigs(io.confluent.ksql.properties.with.CommonCreateConfigs) TableElement(io.confluent.ksql.parser.tree.TableElement) ArrayList(java.util.ArrayList) CreateStream(io.confluent.ksql.parser.tree.CreateStream) Injector(io.confluent.ksql.statement.Injector) CreateTable(io.confluent.ksql.parser.tree.CreateTable) CreateSource(io.confluent.ksql.parser.tree.CreateSource) SchemaResult(io.confluent.ksql.schema.ksql.inference.TopicSchemaSupplier.SchemaResult) ColumnConstraints(io.confluent.ksql.parser.tree.ColumnConstraints) SimpleColumn(io.confluent.ksql.schema.ksql.SimpleColumn) SerdeFeatures(io.confluent.ksql.serde.SerdeFeatures) CreateSourceAsProperties(io.confluent.ksql.parser.properties.with.CreateSourceAsProperties) SchemaAndId(io.confluent.ksql.schema.ksql.inference.TopicSchemaSupplier.SchemaAndId) CreateSourceCommand(io.confluent.ksql.execution.ddl.commands.CreateSourceCommand) ImmutableSet(com.google.common.collect.ImmutableSet) Type(io.confluent.ksql.execution.expression.tree.Type) ImmutableMap(com.google.common.collect.ImmutableMap) SerdeFeature(io.confluent.ksql.serde.SerdeFeature) SetView(com.google.common.collect.Sets.SetView) SandboxedServiceContext(io.confluent.ksql.services.SandboxedServiceContext) SqlFormatter(io.confluent.ksql.parser.SqlFormatter) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) SourcePropertiesUtil(io.confluent.ksql.parser.properties.with.SourcePropertiesUtil) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) ErrorMessageUtil(io.confluent.ksql.util.ErrorMessageUtil) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) Objects(java.util.Objects) CreateAsSelect(io.confluent.ksql.parser.tree.CreateAsSelect) CreateSourceProperties(io.confluent.ksql.parser.properties.with.CreateSourceProperties) List(java.util.List) Stream(java.util.stream.Stream) KsqlExecutionContext(io.confluent.ksql.KsqlExecutionContext) Format(io.confluent.ksql.serde.Format) TableElements(io.confluent.ksql.parser.tree.TableElements) KsqlException(io.confluent.ksql.util.KsqlException) Optional(java.util.Optional) Statement(io.confluent.ksql.parser.tree.Statement) Column(io.confluent.ksql.schema.ksql.Column) FormatInfo(io.confluent.ksql.serde.FormatInfo) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) Type(io.confluent.ksql.execution.expression.tree.Type) ColumnConstraints(io.confluent.ksql.parser.tree.ColumnConstraints) ArrayList(java.util.ArrayList) TableElement(io.confluent.ksql.parser.tree.TableElement)

Example 8 with PreparedStatement

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

the class DistributingExecutorTest method shouldThrowExceptionWhenInsertIntoReadOnlyTopic.

@Test
public void shouldThrowExceptionWhenInsertIntoReadOnlyTopic() {
    // Given
    final PreparedStatement<Statement> preparedStatement = PreparedStatement.of("", new InsertInto(SourceName.of("s1"), mock(Query.class)));
    final ConfiguredStatement<Statement> configured = ConfiguredStatement.of(preparedStatement, SessionConfig.of(KSQL_CONFIG, ImmutableMap.of()));
    final DataSource dataSource = mock(DataSource.class);
    doReturn(dataSource).when(metaStore).getSource(SourceName.of("s1"));
    when(dataSource.getKafkaTopicName()).thenReturn("_confluent-ksql-default__command-topic");
    // When:
    final Exception e = assertThrows(KsqlException.class, () -> distributor.execute(configured, executionContext, mock(KsqlSecurityContext.class)));
    // Then:
    assertThat(e.getMessage(), containsString("Cannot insert into read-only topic: " + "_confluent-ksql-default__command-topic"));
}
Also used : ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) Statement(io.confluent.ksql.parser.tree.Statement) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) InsertInto(io.confluent.ksql.parser.tree.InsertInto) KsqlTopicAuthorizationException(io.confluent.ksql.exception.KsqlTopicAuthorizationException) ProducerFencedException(org.apache.kafka.common.errors.ProducerFencedException) KsqlRestException(io.confluent.ksql.rest.server.resources.KsqlRestException) KsqlException(io.confluent.ksql.util.KsqlException) TimeoutException(org.apache.kafka.common.errors.TimeoutException) KsqlServerException(io.confluent.ksql.util.KsqlServerException) DataSource(io.confluent.ksql.metastore.model.DataSource) Test(org.junit.Test)

Example 9 with PreparedStatement

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

the class DistributingExecutorTest method shouldThrowExceptionWhenInsertIntoProcessingLogTopic.

@Test
public void shouldThrowExceptionWhenInsertIntoProcessingLogTopic() {
    // Given
    final PreparedStatement<Statement> preparedStatement = PreparedStatement.of("", new InsertInto(SourceName.of("s1"), mock(Query.class)));
    final ConfiguredStatement<Statement> configured = ConfiguredStatement.of(preparedStatement, SessionConfig.of(KSQL_CONFIG, ImmutableMap.of()));
    final DataSource dataSource = mock(DataSource.class);
    doReturn(dataSource).when(metaStore).getSource(SourceName.of("s1"));
    when(dataSource.getKafkaTopicName()).thenReturn("default_ksql_processing_log");
    // When:
    final Exception e = assertThrows(KsqlException.class, () -> distributor.execute(configured, executionContext, mock(KsqlSecurityContext.class)));
    // Then:
    assertThat(e.getMessage(), containsString("Cannot insert into read-only topic: " + "default_ksql_processing_log"));
}
Also used : ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) Statement(io.confluent.ksql.parser.tree.Statement) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) InsertInto(io.confluent.ksql.parser.tree.InsertInto) KsqlTopicAuthorizationException(io.confluent.ksql.exception.KsqlTopicAuthorizationException) ProducerFencedException(org.apache.kafka.common.errors.ProducerFencedException) KsqlRestException(io.confluent.ksql.rest.server.resources.KsqlRestException) KsqlException(io.confluent.ksql.util.KsqlException) TimeoutException(org.apache.kafka.common.errors.TimeoutException) KsqlServerException(io.confluent.ksql.util.KsqlServerException) DataSource(io.confluent.ksql.metastore.model.DataSource) Test(org.junit.Test)

Example 10 with PreparedStatement

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

the class KsqlParserTest method shouldParseSimpleComment.

@Test
public void shouldParseSimpleComment() {
    final String statementString = "--this is a comment.\n" + "SHOW 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)

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