Search in sources :

Example 11 with KsqlExecutionContext

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

the class RequestValidator method validate.

/**
 * Validates the messages against a snapshot in time of the KSQL engine.
 *
 * @param statements          the list of statements to validate
 * @param sessionProperties   session properties for this validation
 * @param sql                 the sql that generated the list of statements, used for
 *                            generating more useful debugging information
 *
 * @return the number of new persistent queries that would be created by {@code statements}
 * @throws KsqlException if any of the statements cannot be validated, or the number
 *                       of requested statements would cause the execution context
 *                       to exceed the number of persistent queries that it was configured
 *                       to support
 */
public int validate(final ServiceContext serviceContext, final List<ParsedStatement> statements, final SessionProperties sessionProperties, final String sql) {
    requireSandbox(serviceContext);
    final KsqlExecutionContext ctx = requireSandbox(snapshotSupplier.apply(serviceContext));
    final Injector injector = injectorFactory.apply(ctx, serviceContext);
    final KsqlConfig ksqlConfig = ctx.getKsqlConfig();
    int numPersistentQueries = 0;
    for (final ParsedStatement parsed : statements) {
        final PreparedStatement<?> prepared = ctx.prepare(parsed, (isVariableSubstitutionEnabled(sessionProperties, ksqlConfig) ? sessionProperties.getSessionVariables() : Collections.emptyMap()));
        final ConfiguredStatement<?> configured = ConfiguredStatement.of(prepared, SessionConfig.of(ksqlConfig, sessionProperties.getMutableScopedProperties()));
        numPersistentQueries += validate(serviceContext, configured, sessionProperties, ctx, injector);
        if (QueryCapacityUtil.exceedsPersistentQueryCapacity(ctx, ksqlConfig)) {
            QueryCapacityUtil.throwTooManyActivePersistentQueriesException(ctx, ksqlConfig, sql);
        }
    }
    return numPersistentQueries;
}
Also used : Injector(io.confluent.ksql.statement.Injector) KsqlConfig(io.confluent.ksql.util.KsqlConfig) ParsedStatement(io.confluent.ksql.parser.KsqlParser.ParsedStatement) KsqlExecutionContext(io.confluent.ksql.KsqlExecutionContext)

Example 12 with KsqlExecutionContext

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

the class ListSourceExecutor method sourceDescriptionList.

private static Optional<KsqlEntity> sourceDescriptionList(final ConfiguredStatement<? extends StatementWithExtendedClause> statement, final SessionProperties sessionProperties, final KsqlExecutionContext executionContext, final ServiceContext serviceContext, final List<? extends DataSource> sources, final boolean extended) {
    final RemoteHostExecutor remoteHostExecutor = RemoteHostExecutor.create(statement, sessionProperties, executionContext, serviceContext.getKsqlClient());
    final Multimap<String, SourceDescription> remoteSourceDescriptions = extended ? RemoteSourceDescriptionExecutor.fetchSourceDescriptions(remoteHostExecutor) : ImmutableMultimap.of();
    final List<SourceDescriptionWithWarnings> descriptions = sources.stream().map(s -> describeSource(statement.getSessionConfig().getConfig(false), executionContext, serviceContext, s.getName(), extended, statement, sessionProperties, remoteSourceDescriptions.get(s.getName().toString()))).collect(Collectors.toList());
    return Optional.of(new SourceDescriptionList(statement.getStatementText(), descriptions.stream().map(d -> d.description).collect(Collectors.toList()), descriptions.stream().flatMap(d -> d.warnings.stream()).collect(Collectors.toList())));
}
Also used : DataSource(io.confluent.ksql.metastore.model.DataSource) StreamsList(io.confluent.ksql.rest.entity.StreamsList) SessionProperties(io.confluent.ksql.rest.SessionProperties) SourceName(io.confluent.ksql.name.SourceName) QueryOffsetSummary(io.confluent.ksql.rest.entity.QueryOffsetSummary) RunningQuery(io.confluent.ksql.rest.entity.RunningQuery) DescribeTables(io.confluent.ksql.parser.tree.DescribeTables) ServiceContext(io.confluent.ksql.services.ServiceContext) KafkaException(org.apache.kafka.common.KafkaException) QueryTopicOffsetSummary(io.confluent.ksql.rest.entity.QueryTopicOffsetSummary) SourceDescription(io.confluent.ksql.rest.entity.SourceDescription) Map(java.util.Map) QueryId(io.confluent.ksql.query.QueryId) SourceDescriptionList(io.confluent.ksql.rest.entity.SourceDescriptionList) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) KsqlTable(io.confluent.ksql.metastore.model.KsqlTable) TopicPartition(org.apache.kafka.common.TopicPartition) ImmutableSet(com.google.common.collect.ImmutableSet) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) KsqlConfig(io.confluent.ksql.util.KsqlConfig) KafkaResponseGetFailedException(io.confluent.ksql.exception.KafkaResponseGetFailedException) Collectors(java.util.stream.Collectors) List(java.util.List) KsqlExecutionContext(io.confluent.ksql.KsqlExecutionContext) Entry(java.util.Map.Entry) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) Optional(java.util.Optional) KsqlConstants(io.confluent.ksql.util.KsqlConstants) KsqlRestApplication(io.confluent.ksql.rest.server.KsqlRestApplication) HashMap(java.util.HashMap) Multimap(com.google.common.collect.Multimap) ArrayList(java.util.ArrayList) KsqlEntity(io.confluent.ksql.rest.entity.KsqlEntity) HashSet(java.util.HashSet) QueryStatusCount(io.confluent.ksql.rest.entity.QueryStatusCount) ImmutableList(com.google.common.collect.ImmutableList) SourceDescriptionEntity(io.confluent.ksql.rest.entity.SourceDescriptionEntity) MetaStore(io.confluent.ksql.metastore.MetaStore) ShowColumns(io.confluent.ksql.parser.tree.ShowColumns) DescribeStreams(io.confluent.ksql.parser.tree.DescribeStreams) TablesList(io.confluent.ksql.rest.entity.TablesList) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) LinkedList(java.util.LinkedList) ListTables(io.confluent.ksql.parser.tree.ListTables) TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) Table(io.confluent.ksql.rest.entity.SourceInfo.Table) ListStreams(io.confluent.ksql.parser.tree.ListStreams) QueryApplicationId(io.confluent.ksql.util.QueryApplicationId) SourceDescriptionFactory(io.confluent.ksql.rest.entity.SourceDescriptionFactory) KsqlStream(io.confluent.ksql.metastore.model.KsqlStream) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) StatementWithExtendedClause(io.confluent.ksql.parser.tree.StatementWithExtendedClause) ConsumerPartitionOffsets(io.confluent.ksql.rest.entity.ConsumerPartitionOffsets) Stream(io.confluent.ksql.rest.entity.SourceInfo.Stream) Collections(java.util.Collections) KsqlWarning(io.confluent.ksql.rest.entity.KsqlWarning) SourceDescriptionList(io.confluent.ksql.rest.entity.SourceDescriptionList) SourceDescription(io.confluent.ksql.rest.entity.SourceDescription)

Example 13 with KsqlExecutionContext

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

the class StandaloneExecutor method validateStatements.

private void validateStatements(final List<ParsedStatement> statements) {
    final KsqlExecutionContext sandboxEngine = ksqlEngine.createSandbox(serviceContext);
    final Injector injector = injectorFactory.apply(sandboxEngine, sandboxEngine.getServiceContext());
    final StatementExecutor sandboxExecutor = new StatementExecutor(sandboxEngine.getServiceContext(), sandboxEngine, injector, ksqlConfig);
    final boolean hasQueries = executeStatements(statements, sandboxExecutor);
    if (failOnNoQueries && !hasQueries) {
        throw new KsqlException("The SQL file does not contain any persistent queries. " + "i.e. it contains no 'INSERT INTO', 'CREATE TABLE x AS SELECT' or " + "'CREATE STREAM x AS SELECT' style statements.");
    }
}
Also used : Injector(io.confluent.ksql.statement.Injector) KsqlException(io.confluent.ksql.util.KsqlException) KsqlExecutionContext(io.confluent.ksql.KsqlExecutionContext)

Aggregations

KsqlExecutionContext (io.confluent.ksql.KsqlExecutionContext)13 ConfiguredStatement (io.confluent.ksql.statement.ConfiguredStatement)7 ServiceContext (io.confluent.ksql.services.ServiceContext)6 SessionProperties (io.confluent.ksql.rest.SessionProperties)5 List (java.util.List)5 Optional (java.util.Optional)5 PersistentQueryMetadata (io.confluent.ksql.util.PersistentQueryMetadata)4 QueryMetadata (io.confluent.ksql.util.QueryMetadata)4 Collectors (java.util.stream.Collectors)4 ParsedStatement (io.confluent.ksql.parser.KsqlParser.ParsedStatement)3 KsqlWarning (io.confluent.ksql.rest.entity.KsqlWarning)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 ImmutableList (com.google.common.collect.ImmutableList)2 PreparedStatement (io.confluent.ksql.parser.KsqlParser.PreparedStatement)2 Query (io.confluent.ksql.parser.tree.Query)2 ConnectResponse (io.confluent.ksql.services.ConnectClient.ConnectResponse)2 Injector (io.confluent.ksql.statement.Injector)2 KsqlConfig (io.confluent.ksql.util.KsqlConfig)2 KsqlStatementException (io.confluent.ksql.util.KsqlStatementException)2