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());
}
}
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;
}
Aggregations