use of io.confluent.ksql.util.KsqlStatementException in project ksql by confluentinc.
the class TestExecutorUtil method execute.
/**
* @param srClient if supplied, then schemas can be inferred from the schema registry.
* @return a list of persistent queries that should be run by the test executor, if a query was
* replaced via a CREATE OR REPLACE statement it will only appear once in the output list
*/
@SuppressWarnings("OptionalGetWithoutIsPresent")
private static List<PersistentQueryAndSources> execute(final KsqlEngine engine, final TestCase testCase, final KsqlConfig ksqlConfig, final ServiceContext serviceContext, final Optional<SchemaRegistryClient> srClient, final StubKafkaService stubKafkaService, final TestExecutionListener listener) {
final Map<QueryId, PersistentQueryAndSources> queries = new LinkedHashMap<>();
int idx = 0;
final Iterator<PlannedStatement> plans = planTestCase(engine, testCase, ksqlConfig, serviceContext, srClient, stubKafkaService);
try {
while (plans.hasNext()) {
++idx;
final PlannedStatement planned = plans.next();
if (planned.insertValues.isPresent()) {
final ConfiguredStatement<InsertValues> insertValues = planned.insertValues.get();
final SessionProperties sessionProperties = new SessionProperties(insertValues.getSessionConfig().getOverrides(), new KsqlHostInfo("host", 50), buildUrl(), false);
StubInsertValuesExecutor.of(stubKafkaService).execute(insertValues, sessionProperties, engine, engine.getServiceContext());
continue;
}
final ConfiguredKsqlPlan plan = planned.plan.orElseThrow(IllegalStateException::new);
listener.acceptPlan(plan);
final ExecuteResultAndSources result = executePlan(engine, plan);
if (!result.getSources().isPresent()) {
continue;
}
final PersistentQueryMetadata query = (PersistentQueryMetadata) result.getExecuteResult().getQuery().get();
listener.acceptQuery(query);
queries.put(query.getQueryId(), new PersistentQueryAndSources(query, result.getSources().get()));
}
return ImmutableList.copyOf(queries.values());
} catch (final KsqlStatementException e) {
if (testCase.expectedException().isPresent() && plans.hasNext()) {
throw new AssertionError("Only the last statement in a negative test should fail. " + "Yet in this case statement " + idx + " failed.", e);
}
throw e;
}
}
use of io.confluent.ksql.util.KsqlStatementException in project ksql by confluentinc.
the class QueryEndpoint method createStatement.
private ConfiguredStatement<Query> createStatement(final String queryString, final Map<String, Object> properties, final Map<String, Object> sessionVariables) {
final List<ParsedStatement> statements = ksqlEngine.parse(queryString);
if ((statements.size() != 1)) {
throw new KsqlStatementException(String.format("Expected exactly one KSQL statement; found %d instead", statements.size()), queryString);
}
final PreparedStatement<?> ps = ksqlEngine.prepare(statements.get(0), sessionVariables.entrySet().stream().collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().toString())));
final Statement statement = ps.getStatement();
if (!(statement instanceof Query)) {
throw new KsqlStatementException("Not a query", queryString);
}
@SuppressWarnings("unchecked") final PreparedStatement<Query> psq = (PreparedStatement<Query>) ps;
return ConfiguredStatement.of(psq, SessionConfig.of(ksqlConfig, properties));
}
use of io.confluent.ksql.util.KsqlStatementException 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));
}
}
use of io.confluent.ksql.util.KsqlStatementException in project ksql by confluentinc.
the class WSQueryEndpoint method handleQuery.
private void handleQuery(final RequestContext info, final Query query, final long startTimeNanos, final Context context) {
final WebSocketSubscriber<StreamedRow> streamSubscriber = new WebSocketSubscriber<>(info.websocket);
attachCloseHandler(info.websocket, streamSubscriber);
final PreparedStatement<Query> statement = PreparedStatement.of(info.request.getKsql(), query);
final MetricsCallbackHolder metricsCallbackHolder = new MetricsCallbackHolder();
final QueryMetadataHolder queryMetadataHolder = queryExecutor.handleStatement(info.securityContext.getServiceContext(), info.request.getConfigOverrides(), info.request.getRequestProperties(), statement, Optional.empty(), metricsCallbackHolder, context, true);
if (queryMetadataHolder.getPullQueryResult().isPresent()) {
new PullQueryPublisher(exec, queryMetadataHolder.getPullQueryResult().get(), metricsCallbackHolder, startTimeNanos).subscribe(streamSubscriber);
} else if (queryMetadataHolder.getPushQueryMetadata().isPresent()) {
new PushQueryPublisher(exec, queryMetadataHolder.getPushQueryMetadata().get(), metricsCallbackHolder, startTimeNanos).subscribe(streamSubscriber);
} else {
throw new KsqlStatementException("Unknown query type", statement.getStatementText());
}
}
use of io.confluent.ksql.util.KsqlStatementException in project ksql by confluentinc.
the class KsqlResource method handleKsqlStatements.
public EndpointResponse handleKsqlStatements(final KsqlSecurityContext securityContext, final KsqlRequest request) {
LOG.info("Received: " + request);
throwIfNotConfigured();
activenessRegistrar.updateLastRequestTime();
try {
CommandStoreUtil.httpWaitForCommandSequenceNumber(commandRunner.getCommandQueue(), request, distributedCmdResponseTimeout);
final Map<String, Object> configProperties = request.getConfigOverrides();
denyListPropertyValidator.validateAll(configProperties);
final KsqlRequestConfig requestConfig = new KsqlRequestConfig(request.getRequestProperties());
final List<ParsedStatement> statements = ksqlEngine.parse(request.getKsql());
validator.validate(SandboxedServiceContext.create(securityContext.getServiceContext()), statements, new SessionProperties(configProperties, localHost, localUrl, requestConfig.getBoolean(KsqlRequestConfig.KSQL_REQUEST_INTERNAL_REQUEST), request.getSessionVariables()), request.getKsql());
// log validated statements for query anonymization
statements.forEach(s -> {
if (s.getStatementText().toLowerCase().contains("terminate") || s.getStatementText().toLowerCase().contains("drop")) {
QueryLogger.info("Query terminated", s.getStatementText());
} else {
QueryLogger.info("Query created", s.getStatementText());
}
});
final KsqlEntityList entities = handler.execute(securityContext, statements, new SessionProperties(configProperties, localHost, localUrl, requestConfig.getBoolean(KsqlRequestConfig.KSQL_REQUEST_INTERNAL_REQUEST), request.getSessionVariables()));
LOG.info("Processed successfully: " + request);
addCommandRunnerWarning(entities, commandRunnerWarning);
return EndpointResponse.ok(entities);
} catch (final KsqlRestException e) {
LOG.info("Processed unsuccessfully: " + request + ", reason: ", e);
throw e;
} catch (final KsqlStatementException e) {
LOG.info("Processed unsuccessfully: " + request + ", reason: ", e);
return Errors.badStatement(e.getRawMessage(), e.getSqlStatement());
} catch (final KsqlException e) {
LOG.info("Processed unsuccessfully: " + request + ", reason: ", e);
return errorHandler.generateResponse(e, Errors.badRequest(e));
} catch (final Exception e) {
LOG.info("Processed unsuccessfully: " + request + ", reason: ", e);
return errorHandler.generateResponse(e, Errors.serverErrorForStatement(e, request.getKsql()));
}
}
Aggregations