use of io.confluent.ksql.parser.KsqlParser.ParsedStatement in project ksql by confluentinc.
the class KsqlEngineTest method shouldNotThrowWhenExecutingDuplicateStreamWithIfNotExists.
@Test
public void shouldNotThrowWhenExecutingDuplicateStreamWithIfNotExists() {
// Given:
final List<ParsedStatement> parsed = ksqlEngine.parse("CREATE STREAM FOO WITH (KAFKA_TOPIC='BAR') AS SELECT * FROM ORDERS; " + "CREATE STREAM IF NOT EXISTS FOO WITH (KAFKA_TOPIC='BAR') AS SELECT * FROM ORDERS;");
givenStatementAlreadyExecuted(parsed.get(0));
final PreparedStatement<?> prepared = prepare(parsed.get(1));
// When:
ExecuteResult executeResult = ksqlEngine.execute(serviceContext, ConfiguredStatement.of(prepared, SessionConfig.of(ksqlConfig, new HashMap<>())));
// Then:
assertThat(executeResult.getQuery(), is(Optional.empty()));
assertThat(executeResult.getCommandResult(), is(Optional.of("Cannot add stream `FOO`: A stream with the same name already exists.")));
}
use of io.confluent.ksql.parser.KsqlParser.ParsedStatement in project ksql by confluentinc.
the class KsqlEngineTest method shouldThrowWhenExecutingInsertIntoTable.
@Test
public void shouldThrowWhenExecutingInsertIntoTable() {
KsqlEngineTestUtil.execute(serviceContext, ksqlEngine, "create table bar as select * from test2;", ksqlConfig, Collections.emptyMap());
final ParsedStatement parsed = ksqlEngine.parse("insert into bar select * from test2;").get(0);
// When:
final KsqlStatementException e = assertThrows(KsqlStatementException.class, () -> prepare(parsed));
// Then:
assertThat(e, rawMessage(containsString("INSERT INTO can only be used to insert into a stream. BAR is a table.")));
assertThat(e, statementText(is("insert into bar select * from test2;")));
}
use of io.confluent.ksql.parser.KsqlParser.ParsedStatement in project ksql by confluentinc.
the class KsqlTestingTool method getSqlStatements.
private static List<String> getSqlStatements(final Path queryFilePath) {
try {
final String sqlStatements = new String(Files.readAllBytes(queryFilePath), UTF_8);
final KsqlParser ksqlParser = new DefaultKsqlParser();
final List<ParsedStatement> parsedStatements = ksqlParser.parse(sqlStatements);
return parsedStatements.stream().map(ParsedStatement::getStatementText).collect(Collectors.toList());
} catch (final IOException e) {
throw new KsqlException(String.format("Could not read the query file: %s. Details: %s", queryFilePath, e.getMessage()), e);
}
}
use of io.confluent.ksql.parser.KsqlParser.ParsedStatement 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.ParsedStatement 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