Search in sources :

Example 1 with KsqlStatementException

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

the class FilterTypeValidator method getExpressionReturnType.

private SqlType getExpressionReturnType(final Expression exp) {
    final ExpressionTypeManager expressionTypeManager = new ExpressionTypeManager(schema, functionRegistry);
    // Rewrite the expression with magic timestamps, so type checking can pass
    final Expression magicTimestampRewrite = new StatementRewriteForMagicPseudoTimestamp().rewrite(exp);
    try {
        return expressionTypeManager.getExpressionSqlType(magicTimestampRewrite);
    } catch (KsqlException e) {
        throw new KsqlStatementException("Error in " + filterType.name() + " expression: " + e.getMessage(), exp.toString());
    }
}
Also used : ExpressionTypeManager(io.confluent.ksql.execution.util.ExpressionTypeManager) Expression(io.confluent.ksql.execution.expression.tree.Expression) StatementRewriteForMagicPseudoTimestamp(io.confluent.ksql.engine.rewrite.StatementRewriteForMagicPseudoTimestamp) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) KsqlException(io.confluent.ksql.util.KsqlException)

Example 2 with KsqlStatementException

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

the class ListSourceExecutor method describeSource.

private static SourceDescriptionWithWarnings describeSource(final KsqlConfig ksqlConfig, final KsqlExecutionContext ksqlExecutionContext, final ServiceContext serviceContext, final SourceName name, final boolean extended, final ConfiguredStatement<? extends StatementWithExtendedClause> statement, final SessionProperties sessionProperties, final Collection<SourceDescription> remoteSourceDescriptions) {
    final DataSource dataSource = ksqlExecutionContext.getMetaStore().getSource(name);
    if (dataSource == null) {
        throw new KsqlStatementException(String.format("Could not find STREAM/TABLE '%s' in the Metastore", name.text()), statement.getStatementText());
    }
    final List<RunningQuery> readQueries = getQueries(ksqlExecutionContext, q -> q.getSourceNames().contains(dataSource.getName()));
    final List<RunningQuery> writeQueries = getQueries(ksqlExecutionContext, q -> q.getSinkName().equals(Optional.of(dataSource.getName())));
    Optional<TopicDescription> topicDescription = Optional.empty();
    List<QueryOffsetSummary> queryOffsetSummaries = Collections.emptyList();
    List<String> sourceConstraints = Collections.emptyList();
    final List<KsqlWarning> warnings = new LinkedList<>();
    try {
        topicDescription = Optional.of(serviceContext.getTopicClient().describeTopic(dataSource.getKafkaTopicName()));
        sourceConstraints = getSourceConstraints(name, ksqlExecutionContext.getMetaStore());
    } catch (final KafkaException | KafkaResponseGetFailedException e) {
        warnings.add(new KsqlWarning("Error from Kafka: " + e.getMessage()));
    }
    if (extended) {
        queryOffsetSummaries = queryOffsetSummaries(ksqlConfig, serviceContext, writeQueries);
        return new SourceDescriptionWithWarnings(warnings, SourceDescriptionFactory.create(dataSource, extended, readQueries, writeQueries, topicDescription, queryOffsetSummaries, sourceConstraints, remoteSourceDescriptions.stream().flatMap(sd -> sd.getClusterStatistics().stream()), remoteSourceDescriptions.stream().flatMap(sd -> sd.getClusterErrorStats().stream()), sessionProperties.getKsqlHostInfo(), ksqlExecutionContext.metricCollectors()));
    }
    return new SourceDescriptionWithWarnings(warnings, SourceDescriptionFactory.create(dataSource, extended, readQueries, writeQueries, topicDescription, queryOffsetSummaries, sourceConstraints, java.util.stream.Stream.empty(), java.util.stream.Stream.empty(), sessionProperties.getKsqlHostInfo(), ksqlExecutionContext.metricCollectors()));
}
Also used : KsqlWarning(io.confluent.ksql.rest.entity.KsqlWarning) LinkedList(java.util.LinkedList) DataSource(io.confluent.ksql.metastore.model.DataSource) RunningQuery(io.confluent.ksql.rest.entity.RunningQuery) QueryOffsetSummary(io.confluent.ksql.rest.entity.QueryOffsetSummary) KafkaResponseGetFailedException(io.confluent.ksql.exception.KafkaResponseGetFailedException) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) KafkaException(org.apache.kafka.common.KafkaException)

Example 3 with KsqlStatementException

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

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

the class KsqlResourceTest method shouldFailWhenTopicInferenceFailsDuringValidate.

@Test
public void shouldFailWhenTopicInferenceFailsDuringValidate() {
    // Given:
    givenSource(DataSourceType.KSTREAM, "ORDERS1", "ORDERS1", SOME_SCHEMA);
    when(sandboxTopicInjector.inject(any())).thenThrow(new KsqlStatementException("boom", "sql"));
    // When:
    final KsqlErrorMessage result = makeFailingRequest("CREATE STREAM orders2 AS SELECT * FROM orders1;", BAD_REQUEST.code());
    // Then:
    assertThat(result.getErrorCode(), is(Errors.ERROR_CODE_BAD_STATEMENT));
    assertThat(result.getMessage(), is("boom"));
}
Also used : KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) KsqlErrorMessage(io.confluent.ksql.rest.entity.KsqlErrorMessage) Test(org.junit.Test)

Example 5 with KsqlStatementException

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

the class KsqlResourceTest method shouldFailWhenAvroInferenceFailsDuringExecute.

@Test
public void shouldFailWhenAvroInferenceFailsDuringExecute() {
    // Given:
    when(sandboxSchemaInjector.inject(any())).thenReturn((ConfiguredStatement) CFG_0_WITH_SCHEMA);
    when(schemaInjector.inject(any())).thenThrow(new KsqlStatementException("boom", "some-sql"));
    // When:
    final KsqlRestException e = assertThrows(KsqlRestException.class, () -> makeRequest("CREATE STREAM S WITH(KEY_FORMAT='KAFKA', VALUE_FORMAT='AVRO', KAFKA_TOPIC='orders-topic');"));
    // Then:
    assertThat(e, exceptionStatusCode(is(BAD_REQUEST.code())));
    assertThat(e, exceptionErrorMessage(errorCode(is(Errors.ERROR_CODE_BAD_STATEMENT))));
    assertThat(e, exceptionStatementErrorMessage(errorMessage(is("boom"))));
}
Also used : KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) Test(org.junit.Test)

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