use of io.confluent.ksql.parser.KsqlParser.ParsedStatement 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()));
}
}
use of io.confluent.ksql.parser.KsqlParser.ParsedStatement in project ksql by confluentinc.
the class DataSourceExtractorTest method givenQuery.
private static AstNode givenQuery(final String sql) {
final List<ParsedStatement> statements = new DefaultKsqlParser().parse(sql);
assertThat(statements, hasSize(1));
return new AstBuilder(META_STORE).buildStatement(statements.get(0).getStatement());
}
use of io.confluent.ksql.parser.KsqlParser.ParsedStatement in project ksql by confluentinc.
the class KsqlEngineTest method shouldThrowWhenExecutingDuplicateTable.
@Test
public void shouldThrowWhenExecutingDuplicateTable() {
// Given:
final List<ParsedStatement> parsed = ksqlEngine.parse("CREATE TABLE FOO AS SELECT * FROM TEST2; " + "CREATE TABLE FOO WITH (KAFKA_TOPIC='BAR') AS SELECT * FROM TEST2;");
givenStatementAlreadyExecuted(parsed.get(0));
final PreparedStatement<?> prepared = prepare(parsed.get(1));
// When:
final KsqlStatementException e = assertThrows(KsqlStatementException.class, () -> ksqlEngine.execute(serviceContext, ConfiguredStatement.of(prepared, SessionConfig.of(ksqlConfig, new HashMap<>()))));
// Then:
assertThat(e, rawMessage(is("Cannot add table 'FOO': A table with the same name already exists")));
assertThat(e, statementText(is("CREATE TABLE FOO WITH (KAFKA_TOPIC='BAR') AS SELECT * FROM TEST2;")));
}
use of io.confluent.ksql.parser.KsqlParser.ParsedStatement in project ksql by confluentinc.
the class KsqlEngineTestUtil method executeQuery.
public static TransientQueryMetadata executeQuery(final ServiceContext serviceContext, final KsqlEngine engine, final String sql, final KsqlConfig ksqlConfig, final Map<String, Object> overriddenProperties) {
final ParsedStatement stmt = engine.parse(sql).get(0);
final PreparedStatement<?> prepared = engine.prepare(stmt);
final ConfiguredStatement<Query> configured = ConfiguredStatement.of(prepared, SessionConfig.of(ksqlConfig, overriddenProperties)).cast();
try {
return engine.executeTransientQuery(serviceContext, configured, false);
} catch (final KsqlStatementException e) {
// can easily check that the failed statement is the input statement
throw new KsqlStatementException(e.getRawMessage(), stmt.getStatementText(), e.getCause());
}
}
use of io.confluent.ksql.parser.KsqlParser.ParsedStatement in project ksql by confluentinc.
the class KsqlEngineTestUtil method execute.
/**
* @param srClient if supplied, then schemas can be inferred from the schema registry.
*/
public static List<QueryMetadata> execute(final ServiceContext serviceContext, final KsqlEngine engine, final String sql, final KsqlConfig ksqlConfig, final Map<String, Object> overriddenProperties, final Optional<SchemaRegistryClient> srClient) {
final List<ParsedStatement> statements = engine.parse(sql);
final Optional<DefaultSchemaInjector> schemaInjector = srClient.map(SchemaRegistryTopicSchemaSupplier::new).map(supplier -> new DefaultSchemaInjector(supplier, engine, serviceContext));
return statements.stream().map(stmt -> execute(serviceContext, engine, stmt, ksqlConfig, overriddenProperties, schemaInjector)).map(ExecuteResult::getQuery).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
}
Aggregations