Search in sources :

Example 36 with ParsedStatement

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

the class Cli method handleStatements.

private void handleStatements(final String line) {
    final List<ParsedStatement> statements = KSQL_PARSER.parse(line);
    // validate all before executing any
    sandboxedSessionVariables = new HashMap<>(sessionVariables);
    statements.stream().map(stmt -> this.substituteVariables(stmt, true)).forEach(parsed -> {
        final StatementContext statementContext = parsed.getStatement().statement();
        final HandlerR2<StatementContext, Cli, Void, Optional> validator = STATEMENT_VALIDATORS.get(statementContext.getClass());
        if (validator != null) {
            final Optional<?> validationError = validator.handle(this, null, statementContext);
            validationError.map(o -> (RuntimeException) o).ifPresent(error -> {
                throw error;
            });
        }
    });
    // execute statements
    final StringBuilder consecutiveStatements = new StringBuilder();
    statements.stream().map(stmt -> this.substituteVariables(stmt, false)).forEach(parsed -> {
        final StatementContext statementContext = parsed.getStatement().statement();
        final String statementText = parsed.getStatementText();
        final Handler2<StatementContext, Cli, String> handler = STATEMENT_HANDLERS.get(statementContext.getClass());
        if (handler == null) {
            consecutiveStatements.append(statementText);
        } else {
            makeKsqlRequest(consecutiveStatements.toString());
            consecutiveStatements.setLength(0);
            handler.handle(this, statementText, statementContext);
        }
    });
    if (consecutiveStatements.length() != 0) {
        makeKsqlRequest(consecutiveStatements.toString());
    }
}
Also used : StreamPublisher(io.confluent.ksql.rest.client.StreamPublisher) VertxException(io.vertx.core.VertxException) DefineVariableContext(io.confluent.ksql.parser.SqlBaseParser.DefineVariableContext) WelcomeMsgUtils(io.confluent.ksql.util.WelcomeMsgUtils) BiFunction(java.util.function.BiFunction) LoggerFactory(org.slf4j.LoggerFactory) KsqlParser(io.confluent.ksql.parser.KsqlParser) CliCommandRegisterUtil(io.confluent.ksql.cli.console.cmd.CliCommandRegisterUtil) RestResponse(io.confluent.ksql.rest.client.RestResponse) Context(io.vertx.core.Context) Map(java.util.Map) CreateConnectorContext(io.confluent.ksql.parser.SqlBaseParser.CreateConnectorContext) UndefineVariableContext(io.confluent.ksql.parser.SqlBaseParser.UndefineVariableContext) ParserUtil(io.confluent.ksql.util.ParserUtil) AppInfo(io.confluent.ksql.util.AppInfo) PrintWriter(java.io.PrintWriter) ListConnectorPluginsContext(io.confluent.ksql.parser.SqlBaseParser.ListConnectorPluginsContext) Console(io.confluent.ksql.cli.console.Console) CommandStatus(io.confluent.ksql.rest.entity.CommandStatus) Errors(io.confluent.ksql.rest.Errors) RemoteServerSpecificCommand(io.confluent.ksql.cli.console.cmd.RemoteServerSpecificCommand) StatusClosable(io.confluent.ksql.cli.console.KsqlTerminal.StatusClosable) ListConnectorsContext(io.confluent.ksql.parser.SqlBaseParser.ListConnectorsContext) Handler2(io.confluent.ksql.util.HandlerMaps.Handler2) ClassHandlerMap2(io.confluent.ksql.util.HandlerMaps.ClassHandlerMap2) KsqlConfig(io.confluent.ksql.util.KsqlConfig) UnsetPropertyContext(io.confluent.ksql.parser.SqlBaseParser.UnsetPropertyContext) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) ErrorMessageUtil(io.confluent.ksql.util.ErrorMessageUtil) Objects(java.util.Objects) BaseSubscriber(io.confluent.ksql.reactive.BaseSubscriber) List(java.util.List) KsqlRestClientException(io.confluent.ksql.rest.client.exception.KsqlRestClientException) KsqlVersion(io.confluent.ksql.util.KsqlVersion) EndOfFileException(org.jline.reader.EndOfFileException) Optional(java.util.Optional) VariableSubstitutor(io.confluent.ksql.parser.VariableSubstitutor) ClassHandlerMapR2(io.confluent.ksql.util.HandlerMaps.ClassHandlerMapR2) HandlerR2(io.confluent.ksql.util.HandlerMaps.HandlerR2) StreamedRow(io.confluent.ksql.rest.entity.StreamedRow) KsqlEntityList(io.confluent.ksql.rest.entity.KsqlEntityList) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ListVariablesContext(io.confluent.ksql.parser.SqlBaseParser.ListVariablesContext) Supplier(java.util.function.Supplier) KsqlEntity(io.confluent.ksql.rest.entity.KsqlEntity) ServerInfo(io.confluent.ksql.rest.entity.ServerInfo) KsqlRestClient(io.confluent.ksql.rest.client.KsqlRestClient) VersionType(io.confluent.ksql.util.KsqlVersion.VersionType) DefaultKsqlParser(io.confluent.ksql.parser.DefaultKsqlParser) ParsedStatement(io.confluent.ksql.parser.KsqlParser.ParsedStatement) SetPropertyContext(io.confluent.ksql.parser.SqlBaseParser.SetPropertyContext) Terminal(org.jline.terminal.Terminal) VariablesList(io.confluent.ksql.rest.entity.VariablesList) OutputFormat(io.confluent.ksql.cli.console.OutputFormat) Logger(org.slf4j.Logger) Files(java.nio.file.Files) DropConnectorContext(io.confluent.ksql.parser.SqlBaseParser.DropConnectorContext) CommandStatusEntity(io.confluent.ksql.rest.entity.CommandStatusEntity) RequestPipeliningCommand(io.confluent.ksql.cli.console.cmd.RequestPipeliningCommand) QueryStatementContext(io.confluent.ksql.parser.SqlBaseParser.QueryStatementContext) StatementContext(io.confluent.ksql.parser.SqlBaseParser.StatementContext) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) PrintTopicContext(io.confluent.ksql.parser.SqlBaseParser.PrintTopicContext) TreeMap(java.util.TreeMap) KsqlUnsupportedServerException(io.confluent.ksql.rest.client.exception.KsqlUnsupportedServerException) Paths(java.nio.file.Paths) KsqlMissingCredentialsException(io.confluent.ksql.rest.client.exception.KsqlMissingCredentialsException) Closeable(java.io.Closeable) UserInterruptException(org.jline.reader.UserInterruptException) Subscription(org.reactivestreams.Subscription) HandlerMaps(io.confluent.ksql.util.HandlerMaps) DescribeConnectorContext(io.confluent.ksql.parser.SqlBaseParser.DescribeConnectorContext) Collections(java.util.Collections) SqlBaseParser(io.confluent.ksql.parser.SqlBaseParser) Optional(java.util.Optional) ParsedStatement(io.confluent.ksql.parser.KsqlParser.ParsedStatement) QueryStatementContext(io.confluent.ksql.parser.SqlBaseParser.QueryStatementContext) StatementContext(io.confluent.ksql.parser.SqlBaseParser.StatementContext)

Example 37 with ParsedStatement

use of io.confluent.ksql.parser.KsqlParser.ParsedStatement 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)

Aggregations

ParsedStatement (io.confluent.ksql.parser.KsqlParser.ParsedStatement)37 Test (org.junit.Test)22 KsqlStatementException (io.confluent.ksql.util.KsqlStatementException)15 KsqlException (io.confluent.ksql.util.KsqlException)10 CreateStream (io.confluent.ksql.parser.tree.CreateStream)7 ExecuteResult (io.confluent.ksql.KsqlExecutionContext.ExecuteResult)6 KsqlEntityList (io.confluent.ksql.rest.entity.KsqlEntityList)6 Optional (java.util.Optional)6 DefaultKsqlParser (io.confluent.ksql.parser.DefaultKsqlParser)5 PreparedStatement (io.confluent.ksql.parser.KsqlParser.PreparedStatement)5 ConfiguredStatement (io.confluent.ksql.statement.ConfiguredStatement)5 Injector (io.confluent.ksql.statement.Injector)5 KsqlConfig (io.confluent.ksql.util.KsqlConfig)5 QueryMetadata (io.confluent.ksql.util.QueryMetadata)5 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 KsqlExecutionContext (io.confluent.ksql.KsqlExecutionContext)4 KsqlEntity (io.confluent.ksql.rest.entity.KsqlEntity)4 ServiceContext (io.confluent.ksql.services.ServiceContext)4 PersistentQueryMetadata (io.confluent.ksql.util.PersistentQueryMetadata)4