Search in sources :

Example 21 with KsqlStatementException

use of io.confluent.ksql.util.KsqlStatementException in project ksql by confluentinc.

the class TestExecutorUtil method execute.

/**
 * @param srClient if supplied, then schemas can be inferred from the schema registry.
 * @return a list of persistent queries that should be run by the test executor, if a query was
 *         replaced via a CREATE OR REPLACE statement it will only appear once in the output list
 */
@SuppressWarnings("OptionalGetWithoutIsPresent")
private static List<PersistentQueryAndSources> execute(final KsqlEngine engine, final TestCase testCase, final KsqlConfig ksqlConfig, final ServiceContext serviceContext, final Optional<SchemaRegistryClient> srClient, final StubKafkaService stubKafkaService, final TestExecutionListener listener) {
    final Map<QueryId, PersistentQueryAndSources> queries = new LinkedHashMap<>();
    int idx = 0;
    final Iterator<PlannedStatement> plans = planTestCase(engine, testCase, ksqlConfig, serviceContext, srClient, stubKafkaService);
    try {
        while (plans.hasNext()) {
            ++idx;
            final PlannedStatement planned = plans.next();
            if (planned.insertValues.isPresent()) {
                final ConfiguredStatement<InsertValues> insertValues = planned.insertValues.get();
                final SessionProperties sessionProperties = new SessionProperties(insertValues.getSessionConfig().getOverrides(), new KsqlHostInfo("host", 50), buildUrl(), false);
                StubInsertValuesExecutor.of(stubKafkaService).execute(insertValues, sessionProperties, engine, engine.getServiceContext());
                continue;
            }
            final ConfiguredKsqlPlan plan = planned.plan.orElseThrow(IllegalStateException::new);
            listener.acceptPlan(plan);
            final ExecuteResultAndSources result = executePlan(engine, plan);
            if (!result.getSources().isPresent()) {
                continue;
            }
            final PersistentQueryMetadata query = (PersistentQueryMetadata) result.getExecuteResult().getQuery().get();
            listener.acceptQuery(query);
            queries.put(query.getQueryId(), new PersistentQueryAndSources(query, result.getSources().get()));
        }
        return ImmutableList.copyOf(queries.values());
    } catch (final KsqlStatementException e) {
        if (testCase.expectedException().isPresent() && plans.hasNext()) {
            throw new AssertionError("Only the last statement in a negative test should fail. " + "Yet in this case statement " + idx + " failed.", e);
        }
        throw e;
    }
}
Also used : ConfiguredKsqlPlan(io.confluent.ksql.planner.plan.ConfiguredKsqlPlan) KsqlHostInfo(io.confluent.ksql.util.KsqlHostInfo) QueryId(io.confluent.ksql.query.QueryId) LinkedHashMap(java.util.LinkedHashMap) SessionProperties(io.confluent.ksql.rest.SessionProperties) InsertValues(io.confluent.ksql.parser.tree.InsertValues) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata)

Example 22 with KsqlStatementException

use of io.confluent.ksql.util.KsqlStatementException 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 23 with KsqlStatementException

use of io.confluent.ksql.util.KsqlStatementException in project ksql by confluentinc.

the class StreamedQueryResource method handleStatement.

@SuppressWarnings("unchecked")
private EndpointResponse handleStatement(final KsqlSecurityContext securityContext, final KsqlRequest request, final PreparedStatement<?> statement, final CompletableFuture<Void> connectionClosedFuture, final Optional<Boolean> isInternalRequest, final MetricsCallbackHolder metricsCallbackHolder, final Context context) {
    try {
        authorizationValidator.ifPresent(validator -> validator.checkAuthorization(securityContext, ksqlEngine.getMetaStore(), statement.getStatement()));
        final Map<String, Object> configProperties = request.getConfigOverrides();
        denyListPropertyValidator.validateAll(configProperties);
        if (statement.getStatement() instanceof Query) {
            if (shouldMigrateToQueryStream(request.getConfigOverrides())) {
                return EndpointResponse.ok(new NextHandlerOutput());
            }
            final QueryMetadataHolder queryMetadataHolder = queryExecutor.handleStatement(securityContext.getServiceContext(), request.getConfigOverrides(), request.getRequestProperties(), statement, isInternalRequest, metricsCallbackHolder, context, false);
            return handleQuery((PreparedStatement<Query>) statement, connectionClosedFuture, queryMetadataHolder);
        } else if (statement.getStatement() instanceof PrintTopic) {
            return handlePrintTopic(securityContext.getServiceContext(), configProperties, (PreparedStatement<PrintTopic>) statement, connectionClosedFuture);
        } else {
            return Errors.badRequest(String.format("Statement type `%s' not supported for this resource", statement.getClass().getName()));
        }
    } catch (final TopicAuthorizationException e) {
        return errorHandler.accessDeniedFromKafkaResponse(e);
    } catch (final KsqlStatementException e) {
        return Errors.badStatement(e.getRawMessage(), e.getSqlStatement());
    } catch (final KsqlException e) {
        return errorHandler.generateResponse(e, Errors.badRequest(e));
    }
}
Also used : QueryMetadataHolder(io.confluent.ksql.rest.server.query.QueryMetadataHolder) Query(io.confluent.ksql.parser.tree.Query) PrintTopic(io.confluent.ksql.parser.tree.PrintTopic) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) KsqlException(io.confluent.ksql.util.KsqlException) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) NextHandlerOutput(io.confluent.ksql.api.server.NextHandlerOutput)

Example 24 with KsqlStatementException

use of io.confluent.ksql.util.KsqlStatementException in project ksql by confluentinc.

the class WSQueryEndpoint method handleQuery.

private void handleQuery(final RequestContext info, final Query query, final long startTimeNanos, final Context context) {
    final WebSocketSubscriber<StreamedRow> streamSubscriber = new WebSocketSubscriber<>(info.websocket);
    attachCloseHandler(info.websocket, streamSubscriber);
    final PreparedStatement<Query> statement = PreparedStatement.of(info.request.getKsql(), query);
    final MetricsCallbackHolder metricsCallbackHolder = new MetricsCallbackHolder();
    final QueryMetadataHolder queryMetadataHolder = queryExecutor.handleStatement(info.securityContext.getServiceContext(), info.request.getConfigOverrides(), info.request.getRequestProperties(), statement, Optional.empty(), metricsCallbackHolder, context, true);
    if (queryMetadataHolder.getPullQueryResult().isPresent()) {
        new PullQueryPublisher(exec, queryMetadataHolder.getPullQueryResult().get(), metricsCallbackHolder, startTimeNanos).subscribe(streamSubscriber);
    } else if (queryMetadataHolder.getPushQueryMetadata().isPresent()) {
        new PushQueryPublisher(exec, queryMetadataHolder.getPushQueryMetadata().get(), metricsCallbackHolder, startTimeNanos).subscribe(streamSubscriber);
    } else {
        throw new KsqlStatementException("Unknown query type", statement.getStatementText());
    }
}
Also used : StreamedRow(io.confluent.ksql.rest.entity.StreamedRow) QueryMetadataHolder(io.confluent.ksql.rest.server.query.QueryMetadataHolder) Query(io.confluent.ksql.parser.tree.Query) MetricsCallbackHolder(io.confluent.ksql.api.server.MetricsCallbackHolder) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException)

Example 25 with KsqlStatementException

use of io.confluent.ksql.util.KsqlStatementException 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)

Aggregations

KsqlStatementException (io.confluent.ksql.util.KsqlStatementException)54 Test (org.junit.Test)28 KsqlException (io.confluent.ksql.util.KsqlException)14 Query (io.confluent.ksql.parser.tree.Query)11 ParsedStatement (io.confluent.ksql.parser.KsqlParser.ParsedStatement)10 ConfiguredStatement (io.confluent.ksql.statement.ConfiguredStatement)7 KsqlConfig (io.confluent.ksql.util.KsqlConfig)6 PersistentQueryMetadata (io.confluent.ksql.util.PersistentQueryMetadata)6 SessionConfig (io.confluent.ksql.config.SessionConfig)5 Statement (io.confluent.ksql.parser.tree.Statement)5 ServiceContext (io.confluent.ksql.services.ServiceContext)5 HashMap (java.util.HashMap)5 DataSource (io.confluent.ksql.metastore.model.DataSource)4 PreparedStatement (io.confluent.ksql.parser.KsqlParser.PreparedStatement)4 QueryId (io.confluent.ksql.query.QueryId)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 ExecuteResult (io.confluent.ksql.KsqlExecutionContext.ExecuteResult)3 ImmutableAnalysis (io.confluent.ksql.analyzer.ImmutableAnalysis)3 DdlCommand (io.confluent.ksql.execution.ddl.commands.DdlCommand)3 CreateAsSelect (io.confluent.ksql.parser.tree.CreateAsSelect)3