use of io.confluent.ksql.parser.KsqlParser.PreparedStatement in project ksql by confluentinc.
the class KsqlParserTest method shouldParseMultiLineWithInlineComments.
@Test
public void shouldParseMultiLineWithInlineComments() {
final String statementString = "SHOW -- inline comment\n" + "STREAMS;";
final List<PreparedStatement<?>> statements = KsqlParserTestUtil.buildAst(statementString, metaStore);
assertThat(statements, hasSize(1));
assertThat(statements.get(0).getStatement(), is(instanceOf(ListStreams.class)));
}
use of io.confluent.ksql.parser.KsqlParser.PreparedStatement in project ksql by confluentinc.
the class ExplainExecutor method explainStatement.
private static QueryDescription explainStatement(final ConfiguredStatement<Explain> explain, final KsqlExecutionContext executionContext, final ServiceContext serviceContext) {
final Statement statement = explain.getStatement().getStatement().orElseThrow(() -> new KsqlStatementException("must have either queryID or statement", explain.getStatementText()));
if (!(statement instanceof Query || statement instanceof QueryContainer)) {
throw new KsqlException("The provided statement does not run a ksql query");
}
final PreparedStatement<?> preparedStatement = PreparedStatement.of(explain.getStatementText().substring("EXPLAIN ".length()), statement);
final QueryMetadata metadata;
final KsqlExecutionContext sandbox = executionContext.createSandbox(serviceContext);
if (preparedStatement.getStatement() instanceof Query) {
metadata = sandbox.executeTransientQuery(serviceContext, ConfiguredStatement.of(preparedStatement, explain.getSessionConfig()).cast(), false);
} else {
metadata = sandbox.execute(serviceContext, ConfiguredStatement.of(preparedStatement, explain.getSessionConfig())).getQuery().orElseThrow(() -> new IllegalStateException("The provided statement did not run a ksql query"));
}
return QueryDescriptionFactory.forQueryMetadata(metadata, Collections.emptyMap());
}
use of io.confluent.ksql.parser.KsqlParser.PreparedStatement in project ksql by confluentinc.
the class KsqlEngineTestUtil method execute.
@SuppressWarnings({ "rawtypes", "unchecked" })
private static ExecuteResult execute(final ServiceContext serviceContext, final KsqlExecutionContext executionContext, final ParsedStatement stmt, final KsqlConfig ksqlConfig, final Map<String, Object> overriddenProperties, final Optional<DefaultSchemaInjector> schemaInjector) {
final PreparedStatement<?> prepared = executionContext.prepare(stmt);
final ConfiguredStatement<?> configured = ConfiguredStatement.of(prepared, SessionConfig.of(ksqlConfig, overriddenProperties));
final ConfiguredStatement<?> withFormats = new DefaultFormatInjector().inject(configured);
final ConfiguredStatement<?> withSchema = schemaInjector.map(injector -> injector.inject(withFormats)).orElse((ConfiguredStatement) withFormats);
final ConfiguredStatement<?> reformatted = new SqlFormatInjector(executionContext).inject(withSchema);
try {
return executionContext.execute(serviceContext, reformatted);
} 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.PreparedStatement 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.parser.KsqlParser.PreparedStatement in project ksql by confluentinc.
the class StandaloneExecutorTest method givenQueryFileParsesTo.
private void givenQueryFileParsesTo(final PreparedStatement<?>... statements) {
final List<ParsedStatement> parsedStmts = Arrays.stream(statements).map(statement -> ParsedStatement.of(statement.getStatementText(), mock(SingleStatementContext.class))).collect(Collectors.toList());
when(ksqlEngine.parse(any())).thenReturn(parsedStmts);
IntStream.range(0, parsedStmts.size()).forEach(idx -> {
final ParsedStatement parsed = parsedStmts.get(idx);
final PreparedStatement<?> prepared = statements[idx];
when(sandBox.prepare(parsed)).thenReturn((PreparedStatement) prepared);
when(ksqlEngine.prepare(parsed)).thenReturn((PreparedStatement) prepared);
});
}
Aggregations