Search in sources :

Example 16 with ParsedStatement

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()));
    }
}
Also used : KsqlEntityList(io.confluent.ksql.rest.entity.KsqlEntityList) SessionProperties(io.confluent.ksql.rest.SessionProperties) ParsedStatement(io.confluent.ksql.parser.KsqlParser.ParsedStatement) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) KsqlRequestConfig(io.confluent.ksql.util.KsqlRequestConfig) KsqlException(io.confluent.ksql.util.KsqlException) PatternSyntaxException(java.util.regex.PatternSyntaxException) KsqlException(io.confluent.ksql.util.KsqlException) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException)

Example 17 with ParsedStatement

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

Example 18 with ParsedStatement

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

Example 19 with ParsedStatement

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

Example 20 with ParsedStatement

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());
}
Also used : Optional(java.util.Optional) ParsedStatement(io.confluent.ksql.parser.KsqlParser.ParsedStatement) DefaultSchemaInjector(io.confluent.ksql.schema.ksql.inference.DefaultSchemaInjector)

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