Search in sources :

Example 11 with ParsedStatement

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.")));
}
Also used : ParsedStatement(io.confluent.ksql.parser.KsqlParser.ParsedStatement) ExecuteResult(io.confluent.ksql.KsqlExecutionContext.ExecuteResult) Test(org.junit.Test)

Example 12 with ParsedStatement

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;")));
}
Also used : ParsedStatement(io.confluent.ksql.parser.KsqlParser.ParsedStatement) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) Test(org.junit.Test)

Example 13 with ParsedStatement

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);
    }
}
Also used : KsqlParser(io.confluent.ksql.parser.KsqlParser) DefaultKsqlParser(io.confluent.ksql.parser.DefaultKsqlParser) ParsedStatement(io.confluent.ksql.parser.KsqlParser.ParsedStatement) IOException(java.io.IOException) KsqlException(io.confluent.ksql.util.KsqlException) DefaultKsqlParser(io.confluent.ksql.parser.DefaultKsqlParser)

Example 14 with ParsedStatement

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));
}
Also used : Query(io.confluent.ksql.parser.tree.Query) ParsedStatement(io.confluent.ksql.parser.KsqlParser.ParsedStatement) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) Statement(io.confluent.ksql.parser.tree.Statement) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) ParsedStatement(io.confluent.ksql.parser.KsqlParser.ParsedStatement) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement)

Example 15 with ParsedStatement

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);
    });
}
Also used : Query(io.confluent.ksql.parser.tree.Query) CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) CreateTableAsSelect(io.confluent.ksql.parser.tree.CreateTableAsSelect) Arrays(java.util.Arrays) ColumnName(io.confluent.ksql.name.ColumnName) SourceName(io.confluent.ksql.name.SourceName) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) ParserMatchers.configured(io.confluent.ksql.parser.ParserMatchers.configured) BiFunction(java.util.function.BiFunction) ServiceContext(io.confluent.ksql.services.ServiceContext) InjectorChain(io.confluent.ksql.statement.InjectorChain) ArgumentMatchers.anyShort(org.mockito.ArgumentMatchers.anyShort) TableElement(io.confluent.ksql.parser.tree.TableElement) KsqlModuleType(io.confluent.ksql.version.metrics.collector.KsqlModuleType) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) ExecuteResult(io.confluent.ksql.KsqlExecutionContext.ExecuteResult) MockitoHamcrest.argThat(org.mockito.hamcrest.MockitoHamcrest.argThat) CreateTable(io.confluent.ksql.parser.tree.CreateTable) After(org.junit.After) Assert.fail(org.junit.Assert.fail) AllColumns(io.confluent.ksql.parser.tree.AllColumns) Path(java.nio.file.Path) CreateSourceAsProperties(io.confluent.ksql.parser.properties.with.CreateSourceAsProperties) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) QueryMetadata(io.confluent.ksql.util.QueryMetadata) RefinementInfo(io.confluent.ksql.serde.RefinementInfo) ImmutableMap(com.google.common.collect.ImmutableMap) UnsetProperty(io.confluent.ksql.parser.tree.UnsetProperty) TestUtils(org.apache.kafka.test.TestUtils) DropStream(io.confluent.ksql.parser.tree.DropStream) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) KsqlConfig(io.confluent.ksql.util.KsqlConfig) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) SetProperty(io.confluent.ksql.parser.tree.SetProperty) VersionCheckerAgent(io.confluent.ksql.version.metrics.VersionCheckerAgent) Mockito.inOrder(org.mockito.Mockito.inOrder) KsqlExecutionContext(io.confluent.ksql.KsqlExecutionContext) MetricsReporter(org.apache.kafka.common.metrics.MetricsReporter) KsqlException(io.confluent.ksql.util.KsqlException) Optional(java.util.Optional) Matchers.containsString(org.hamcrest.Matchers.containsString) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Mockito.mock(org.mockito.Mockito.mock) ProcessingLogConfig(io.confluent.ksql.logging.processing.ProcessingLogConfig) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) Mock(org.mockito.Mock) Assert.assertThrows(org.junit.Assert.assertThrows) RunWith(org.junit.runner.RunWith) MetricCollectors(io.confluent.ksql.metrics.MetricCollectors) CreateStreamAsSelect(io.confluent.ksql.parser.tree.CreateStreamAsSelect) KafkaTopicClient(io.confluent.ksql.services.KafkaTopicClient) OptionalInt(java.util.OptionalInt) SessionConfig(io.confluent.ksql.config.SessionConfig) CreateStream(io.confluent.ksql.parser.tree.CreateStream) ArgumentCaptor(org.mockito.ArgumentCaptor) ImmutableList(com.google.common.collect.ImmutableList) Injector(io.confluent.ksql.statement.Injector) ParsedStatement(io.confluent.ksql.parser.KsqlParser.ParsedStatement) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) InsertInto(io.confluent.ksql.parser.tree.InsertInto) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) UserFunctionLoader(io.confluent.ksql.function.UserFunctionLoader) Before(org.junit.Before) Collections.emptyMap(java.util.Collections.emptyMap) Type(io.confluent.ksql.execution.expression.tree.Type) Properties(java.util.Properties) InOrder(org.mockito.InOrder) KsqlEngine(io.confluent.ksql.engine.KsqlEngine) Files(java.nio.file.Files) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Table(io.confluent.ksql.parser.tree.Table) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) Mockito.never(org.mockito.Mockito.never) CreateSourceProperties(io.confluent.ksql.parser.properties.with.CreateSourceProperties) SingleStatementContext(io.confluent.ksql.parser.SqlBaseParser.SingleStatementContext) Paths(java.nio.file.Paths) TableElements(io.confluent.ksql.parser.tree.TableElements) OutputRefinement(io.confluent.ksql.parser.OutputRefinement) SqlTypes(io.confluent.ksql.schema.ksql.types.SqlTypes) Collections(java.util.Collections) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) Select(io.confluent.ksql.parser.tree.Select) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SingleStatementContext(io.confluent.ksql.parser.SqlBaseParser.SingleStatementContext) ParsedStatement(io.confluent.ksql.parser.KsqlParser.ParsedStatement)

Aggregations

ParsedStatement (io.confluent.ksql.parser.KsqlParser.ParsedStatement)37 Test (org.junit.Test)22 KsqlStatementException (io.confluent.ksql.util.KsqlStatementException)15 KsqlException (io.confluent.ksql.util.KsqlException)10 CreateStream (io.confluent.ksql.parser.tree.CreateStream)7 ExecuteResult (io.confluent.ksql.KsqlExecutionContext.ExecuteResult)6 KsqlEntityList (io.confluent.ksql.rest.entity.KsqlEntityList)6 Optional (java.util.Optional)6 DefaultKsqlParser (io.confluent.ksql.parser.DefaultKsqlParser)5 PreparedStatement (io.confluent.ksql.parser.KsqlParser.PreparedStatement)5 ConfiguredStatement (io.confluent.ksql.statement.ConfiguredStatement)5 Injector (io.confluent.ksql.statement.Injector)5 KsqlConfig (io.confluent.ksql.util.KsqlConfig)5 QueryMetadata (io.confluent.ksql.util.QueryMetadata)5 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 KsqlExecutionContext (io.confluent.ksql.KsqlExecutionContext)4 KsqlEntity (io.confluent.ksql.rest.entity.KsqlEntity)4 ServiceContext (io.confluent.ksql.services.ServiceContext)4 PersistentQueryMetadata (io.confluent.ksql.util.PersistentQueryMetadata)4