use of io.confluent.ksql.statement.Injector in project ksql by confluentinc.
the class KsqlContext method sql.
public List<QueryMetadata> sql(final String sql, final Map<String, ?> overriddenProperties) {
final List<ParsedStatement> statements = ksqlEngine.parse(sql);
final KsqlExecutionContext sandbox = ksqlEngine.createSandbox(ksqlEngine.getServiceContext());
final Map<String, Object> validationOverrides = new HashMap<>(overriddenProperties);
for (final ParsedStatement stmt : statements) {
execute(sandbox, stmt, ksqlConfig, validationOverrides, injectorFactory.apply(sandbox, sandbox.getServiceContext()));
}
final List<QueryMetadata> queries = new ArrayList<>();
final Injector injector = injectorFactory.apply(ksqlEngine, serviceContext);
final Map<String, Object> executionOverrides = new HashMap<>(overriddenProperties);
for (final ParsedStatement parsed : statements) {
execute(ksqlEngine, parsed, ksqlConfig, executionOverrides, injector).getQuery().ifPresent(queries::add);
}
for (final QueryMetadata queryMetadata : queries) {
if (queryMetadata instanceof PersistentQueryMetadata) {
queryMetadata.start();
} else {
LOG.warn("Ignoring statemenst: {}", sql);
LOG.warn("Only CREATE statements can run in KSQL embedded mode.");
}
}
return queries;
}
use of io.confluent.ksql.statement.Injector in project ksql by confluentinc.
the class StandaloneExecutor method processesQueryFile.
private void processesQueryFile(final String queries) {
final List<ParsedStatement> preparedStatements = ksqlEngine.parse(queries);
validateStatements(preparedStatements);
final Injector injector = injectorFactory.apply(ksqlEngine, serviceContext);
executeStatements(preparedStatements, new StatementExecutor(serviceContext, ksqlEngine, injector, ksqlConfig));
ksqlEngine.getPersistentQueries().forEach(QueryMetadata::start);
}
use of io.confluent.ksql.statement.Injector 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.");
}
}
use of io.confluent.ksql.statement.Injector 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