Search in sources :

Example 1 with InsertValues

use of io.confluent.ksql.parser.tree.InsertValues in project ksql by confluentinc.

the class InsertValuesExecutor method execute.

// Part of required API.
@SuppressWarnings("unused")
public void execute(final ConfiguredStatement<InsertValues> statement, final SessionProperties sessionProperties, final KsqlExecutionContext executionContext, final ServiceContext serviceContext) {
    final InsertValues insertValues = statement.getStatement();
    final MetaStore metaStore = executionContext.getMetaStore();
    final KsqlConfig config = statement.getSessionConfig().getConfig(true);
    final DataSource dataSource = getDataSource(config, metaStore, insertValues);
    validateInsert(insertValues.getColumns(), dataSource);
    final ProducerRecord<byte[], byte[]> record = buildRecord(statement, metaStore, dataSource, serviceContext);
    try {
        producer.sendRecord(record, serviceContext, config.getProducerClientConfigProps());
    } catch (final TopicAuthorizationException e) {
        // TopicAuthorizationException does not give much detailed information about why it failed,
        // except which topics are denied. Here we just add the ACL to make the error message
        // consistent with other authorization error messages.
        final Exception rootCause = new KsqlTopicAuthorizationException(AclOperation.WRITE, e.unauthorizedTopics());
        throw new KsqlException(createInsertFailedExceptionMessage(insertValues), rootCause);
    } catch (final Exception e) {
        throw new KsqlException(createInsertFailedExceptionMessage(insertValues), e);
    }
}
Also used : KsqlTopicAuthorizationException(io.confluent.ksql.exception.KsqlTopicAuthorizationException) MetaStore(io.confluent.ksql.metastore.MetaStore) InsertValues(io.confluent.ksql.parser.tree.InsertValues) KsqlConfig(io.confluent.ksql.util.KsqlConfig) KsqlException(io.confluent.ksql.util.KsqlException) KsqlTopicAuthorizationException(io.confluent.ksql.exception.KsqlTopicAuthorizationException) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) KsqlTopicAuthorizationException(io.confluent.ksql.exception.KsqlTopicAuthorizationException) RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException) KsqlException(io.confluent.ksql.util.KsqlException) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) ExecutionException(java.util.concurrent.ExecutionException) KsqlSchemaAuthorizationException(io.confluent.ksql.exception.KsqlSchemaAuthorizationException) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) DataSource(io.confluent.ksql.metastore.model.DataSource)

Example 2 with InsertValues

use of io.confluent.ksql.parser.tree.InsertValues 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 3 with InsertValues

use of io.confluent.ksql.parser.tree.InsertValues in project ksql by confluentinc.

the class AssertExecutor method assertValues.

public static void assertValues(final KsqlExecutionContext engine, final KsqlConfig config, final AssertValues assertValues, final TestDriverPipeline driverPipeline) {
    final InsertValues values = assertValues.getStatement();
    assertContent(engine, config, values, driverPipeline, false);
}
Also used : InsertValues(io.confluent.ksql.parser.tree.InsertValues)

Example 4 with InsertValues

use of io.confluent.ksql.parser.tree.InsertValues in project ksql by confluentinc.

the class AssertExecutor method assertTombstone.

public static void assertTombstone(final KsqlExecutionContext engine, final KsqlConfig config, final AssertTombstone assertTombstone, final TestDriverPipeline driverPipeline) {
    final InsertValues values = assertTombstone.getStatement();
    assertContent(engine, config, values, driverPipeline, true);
}
Also used : InsertValues(io.confluent.ksql.parser.tree.InsertValues)

Example 5 with InsertValues

use of io.confluent.ksql.parser.tree.InsertValues in project ksql by confluentinc.

the class InsertValuesExecutorTest method shouldThrowOnTopicAuthorizationException.

@Test
public void shouldThrowOnTopicAuthorizationException() {
    // Given:
    final ConfiguredStatement<InsertValues> statement = givenInsertValues(allAndPseudoColumnNames(SCHEMA), ImmutableList.of(new LongLiteral(1L), new StringLiteral("str"), new StringLiteral("str"), new LongLiteral(2L)));
    doThrow(new TopicAuthorizationException(Collections.singleton("t1"))).when(producer).send(any());
    // When:
    final Exception e = assertThrows(KsqlException.class, () -> executor.execute(statement, mock(SessionProperties.class), engine, serviceContext));
    // Then:
    assertThat(e.getCause(), (hasMessage(containsString("Authorization denied to Write on topic(s): [t1]"))));
}
Also used : StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) InsertValues(io.confluent.ksql.parser.tree.InsertValues) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) SerializationException(org.apache.kafka.common.errors.SerializationException) RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException) KsqlException(io.confluent.ksql.util.KsqlException) ExecutionException(java.util.concurrent.ExecutionException) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) Test(org.junit.Test)

Aggregations

InsertValues (io.confluent.ksql.parser.tree.InsertValues)32 Test (org.junit.Test)25 KsqlException (io.confluent.ksql.util.KsqlException)24 RestClientException (io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException)22 ExecutionException (java.util.concurrent.ExecutionException)22 TopicAuthorizationException (org.apache.kafka.common.errors.TopicAuthorizationException)22 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)21 LongLiteral (io.confluent.ksql.execution.expression.tree.LongLiteral)20 SerializationException (org.apache.kafka.common.errors.SerializationException)20 KsqlConfig (io.confluent.ksql.util.KsqlConfig)8 SessionProperties (io.confluent.ksql.rest.SessionProperties)5 SchemaMetadata (io.confluent.kafka.schemaregistry.client.SchemaMetadata)4 GenericRecordFactory (io.confluent.ksql.engine.generic.GenericRecordFactory)3 KsqlGenericRecord (io.confluent.ksql.engine.generic.KsqlGenericRecord)3 NullLiteral (io.confluent.ksql.execution.expression.tree.NullLiteral)3 DataSource (io.confluent.ksql.metastore.model.DataSource)3 KsqlSchemaAuthorizationException (io.confluent.ksql.exception.KsqlSchemaAuthorizationException)2 KsqlTopicAuthorizationException (io.confluent.ksql.exception.KsqlTopicAuthorizationException)2 DoubleLiteral (io.confluent.ksql.execution.expression.tree.DoubleLiteral)2 KsqlStatementException (io.confluent.ksql.util.KsqlStatementException)2