Search in sources :

Example 1 with PreparedStatement

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)));
}
Also used : PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 2 with PreparedStatement

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

Example 3 with PreparedStatement

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());
    }
}
Also used : Query(io.confluent.ksql.parser.tree.Query) SequentialQueryIdGenerator(io.confluent.ksql.query.id.SequentialQueryIdGenerator) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) ServiceContext(io.confluent.ksql.services.ServiceContext) SchemaRegistryTopicSchemaSupplier(io.confluent.ksql.schema.ksql.inference.SchemaRegistryTopicSchemaSupplier) ProcessingLogContext(io.confluent.ksql.logging.processing.ProcessingLogContext) MutableMetaStore(io.confluent.ksql.metastore.MutableMetaStore) MetricCollectors(io.confluent.ksql.metrics.MetricCollectors) Function(java.util.function.Function) SessionConfig(io.confluent.ksql.config.SessionConfig) TransientQueryMetadata(io.confluent.ksql.util.TransientQueryMetadata) ExecuteResult(io.confluent.ksql.KsqlExecutionContext.ExecuteResult) Map(java.util.Map) ParsedStatement(io.confluent.ksql.parser.KsqlParser.ParsedStatement) DefaultFormatInjector(io.confluent.ksql.format.DefaultFormatInjector) QueryIdGenerator(io.confluent.ksql.query.id.QueryIdGenerator) QueryMetadata(io.confluent.ksql.util.QueryMetadata) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) KsqlConfig(io.confluent.ksql.util.KsqlConfig) DefaultSchemaInjector(io.confluent.ksql.schema.ksql.inference.DefaultSchemaInjector) Collectors(java.util.stream.Collectors) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) List(java.util.List) KsqlExecutionContext(io.confluent.ksql.KsqlExecutionContext) KsqlEngineMetrics(io.confluent.ksql.internal.KsqlEngineMetrics) Optional(java.util.Optional) Collections(java.util.Collections) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) DefaultFormatInjector(io.confluent.ksql.format.DefaultFormatInjector) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException)

Example 4 with PreparedStatement

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));
}
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 5 with PreparedStatement

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);
    });
}
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

PreparedStatement (io.confluent.ksql.parser.KsqlParser.PreparedStatement)32 ConfiguredStatement (io.confluent.ksql.statement.ConfiguredStatement)21 Test (org.junit.Test)20 Statement (io.confluent.ksql.parser.tree.Statement)18 KsqlException (io.confluent.ksql.util.KsqlException)15 Query (io.confluent.ksql.parser.tree.Query)9 Matchers.containsString (org.hamcrest.Matchers.containsString)8 KsqlTopicAuthorizationException (io.confluent.ksql.exception.KsqlTopicAuthorizationException)7 ParsedStatement (io.confluent.ksql.parser.KsqlParser.ParsedStatement)7 InsertInto (io.confluent.ksql.parser.tree.InsertInto)7 KsqlRestException (io.confluent.ksql.rest.server.resources.KsqlRestException)7 ServiceContext (io.confluent.ksql.services.ServiceContext)7 KsqlConfig (io.confluent.ksql.util.KsqlConfig)7 Optional (java.util.Optional)7 KsqlExecutionContext (io.confluent.ksql.KsqlExecutionContext)6 SessionConfig (io.confluent.ksql.config.SessionConfig)6 DataSource (io.confluent.ksql.metastore.model.DataSource)6 KsqlServerException (io.confluent.ksql.util.KsqlServerException)6 KsqlStatementException (io.confluent.ksql.util.KsqlStatementException)6 List (java.util.List)6