Search in sources :

Example 11 with InsertValues

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

the class InsertValuesExecutorTest method shouldThrowOnTablesWithKeyFieldAndNullKeyFieldValueProvided.

@Test
public void shouldThrowOnTablesWithKeyFieldAndNullKeyFieldValueProvided() {
    // Given:
    givenSourceTableWithSchema(SerdeFeatures.of(), SerdeFeatures.of());
    final ConfiguredStatement<InsertValues> statement = givenInsertValues(ImmutableList.of(COL1), ImmutableList.of(new LongLiteral(2L)));
    // When:
    final Exception e = assertThrows(KsqlException.class, () -> executor.execute(statement, mock(SessionProperties.class), engine, serviceContext));
    // Then:
    assertThat(e.getMessage(), containsString("Failed to insert values into 'TOPIC'. Value for primary key column(s) k0 is required for tables"));
}
Also used : LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) InsertValues(io.confluent.ksql.parser.tree.InsertValues) 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)

Example 12 with InsertValues

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

the class InsertValuesExecutorTest method shouldThrowWhenInsertValuesOnProcessingLogTopic.

@Test
public void shouldThrowWhenInsertValuesOnProcessingLogTopic() {
    // Given
    givenDataSourceWithSchema("default_ksql_processing_log", SCHEMA, SerdeFeatures.of(), SerdeFeatures.of(), false, false);
    final KsqlConfig ksqlConfig = new KsqlConfig(ImmutableMap.of());
    final ConfiguredStatement<InsertValues> statement = ConfiguredStatement.of(PreparedStatement.of("", new InsertValues(SourceName.of("TOPIC"), allAndPseudoColumnNames(SCHEMA), ImmutableList.of(new LongLiteral(1L), new StringLiteral("str"), new StringLiteral("str"), new LongLiteral(2L)))), SessionConfig.of(ksqlConfig, ImmutableMap.of()));
    // When:
    final Exception e = assertThrows(KsqlException.class, () -> executor.execute(statement, mock(SessionProperties.class), engine, serviceContext));
    // Then:
    assertThat(e.getMessage(), containsString("Cannot insert values into read-only topic: default_ksql_processing_log"));
}
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) KsqlConfig(io.confluent.ksql.util.KsqlConfig) 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)

Example 13 with InsertValues

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

the class InsertValuesExecutorTest method shouldThrowOnSerializingValueError.

@Test
public void shouldThrowOnSerializingValueError() {
    // Given:
    final ConfiguredStatement<InsertValues> statement = givenInsertValues(allAndPseudoColumnNames(SCHEMA), ImmutableList.of(new LongLiteral(1L), new StringLiteral("str"), new StringLiteral("str"), new LongLiteral(2L)));
    when(valueSerializer.serialize(any(), any())).thenThrow(new SerializationException("Jibberish!"));
    // When:
    final Exception e = assertThrows(KsqlException.class, () -> executor.execute(statement, mock(SessionProperties.class), engine, serviceContext));
    // Then:
    assertThat(e.getCause(), (hasMessage(containsString("Could not serialize value"))));
}
Also used : SerializationException(org.apache.kafka.common.errors.SerializationException) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) InsertValues(io.confluent.ksql.parser.tree.InsertValues) 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)

Example 14 with InsertValues

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

the class KsqlTesterTest method pipeInput.

private void pipeInput(final ConfiguredStatement<InsertValues> statement) {
    final InsertValues insertValues = statement.getStatement();
    final DataSource dataSource = engine.getMetaStore().getSource(insertValues.getTarget());
    if (dataSource == null) {
        throw new KsqlException("Unknown data source " + insertValues.getTarget());
    }
    final KsqlGenericRecord record = new GenericRecordFactory(config, engine.getMetaStore(), System::currentTimeMillis).build(insertValues.getColumns(), insertValues.getValues(), dataSource.getSchema(), dataSource.getDataSourceType());
    driverPipeline.pipeInput(dataSource.getKafkaTopicName(), record.key, record.value, record.ts);
}
Also used : GenericRecordFactory(io.confluent.ksql.engine.generic.GenericRecordFactory) InsertValues(io.confluent.ksql.parser.tree.InsertValues) KsqlException(io.confluent.ksql.util.KsqlException) DataSource(io.confluent.ksql.metastore.model.DataSource) KsqlGenericRecord(io.confluent.ksql.engine.generic.KsqlGenericRecord)

Example 15 with InsertValues

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

the class KsqlTesterTest method execute.

@SuppressWarnings("unchecked")
private void execute(final ParsedStatement parsedStatement) {
    final PreparedStatement<?> engineStatement = engine.prepare(parsedStatement);
    final ConfiguredStatement<?> configured = ConfiguredStatement.of(engineStatement, SessionConfig.of(config, overrides));
    createTopics(engineStatement);
    if (engineStatement.getStatement() instanceof InsertValues) {
        pipeInput((ConfiguredStatement<InsertValues>) configured);
        return;
    } else if (engineStatement.getStatement() instanceof SetProperty) {
        PropertyOverrider.set((ConfiguredStatement<SetProperty>) configured, overrides);
        return;
    } else if (engineStatement.getStatement() instanceof UnsetProperty) {
        PropertyOverrider.unset((ConfiguredStatement<UnsetProperty>) configured, overrides);
        return;
    }
    final ConfiguredStatement<?> injected = formatInjector.inject(configured);
    final ExecuteResult result = engine.execute(serviceContext, injected);
    // is DDL statement
    if (!result.getQuery().isPresent()) {
        return;
    }
    final PersistentQueryMetadata query = (PersistentQueryMetadata) result.getQuery().get();
    final Topology topology = query.getTopology();
    final Properties properties = new Properties();
    properties.putAll(query.getStreamsProperties());
    properties.put(StreamsConfig.STATE_DIR_CONFIG, tmpFolder.getRoot().getAbsolutePath());
    final TopologyTestDriver driver = new TopologyTestDriver(topology, properties);
    final List<TopicInfo> inputTopics = query.getSourceNames().stream().map(sn -> engine.getMetaStore().getSource(sn)).map(ds -> new TopicInfo(ds.getKafkaTopicName(), keySerde(ds), valueSerde(ds))).collect(Collectors.toList());
    // Sink may be Optional for source tables. Once source table query execution is supported, then
    // we would need have a condition to not create an output topic info
    final DataSource output = engine.getMetaStore().getSource(query.getSinkName().get());
    final TopicInfo outputInfo = new TopicInfo(output.getKafkaTopicName(), keySerde(output), valueSerde(output));
    driverPipeline.addDriver(driver, inputTopics, outputInfo);
    drivers.put(query.getQueryId(), new DriverAndProperties(driver, properties));
}
Also used : DataSource(io.confluent.ksql.metastore.model.DataSource) NoSuchFileException(java.nio.file.NoSuchFileException) SequentialQueryIdGenerator(io.confluent.ksql.query.id.SequentialQueryIdGenerator) ServiceContext(io.confluent.ksql.services.ServiceContext) LoggerFactory(org.slf4j.LoggerFactory) ExecuteResult(io.confluent.ksql.KsqlExecutionContext.ExecuteResult) InsertValues(io.confluent.ksql.parser.tree.InsertValues) Serde(org.apache.kafka.common.serialization.Serde) Map(java.util.Map) After(org.junit.After) DefaultFormatInjector(io.confluent.ksql.format.DefaultFormatInjector) QueryId(io.confluent.ksql.query.QueryId) FakeKafkaTopicClient(io.confluent.ksql.services.FakeKafkaTopicClient) Path(java.nio.file.Path) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) Parameterized(org.junit.runners.Parameterized) QueryMetadata(io.confluent.ksql.util.QueryMetadata) MockSchemaRegistryClient(io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient) ImmutableMap(com.google.common.collect.ImmutableMap) UnsetProperty(io.confluent.ksql.parser.tree.UnsetProperty) TestDirective(io.confluent.ksql.test.parser.TestDirective) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) KsqlConfig(io.confluent.ksql.util.KsqlConfig) Collectors(java.util.stream.Collectors) TestServiceContext(io.confluent.ksql.services.TestServiceContext) Objects(java.util.Objects) MetaStoreImpl(io.confluent.ksql.metastore.MetaStoreImpl) List(java.util.List) SetProperty(io.confluent.ksql.parser.tree.SetProperty) NoopProcessingLogContext(io.confluent.ksql.logging.processing.NoopProcessingLogContext) PropertyOverrider(io.confluent.ksql.properties.PropertyOverrider) KsqlException(io.confluent.ksql.util.KsqlException) Optional(java.util.Optional) TopicInfo(io.confluent.ksql.test.driver.TestDriverPipeline.TopicInfo) Topology(org.apache.kafka.streams.Topology) StreamsConfig(org.apache.kafka.streams.StreamsConfig) Iterables(com.google.common.collect.Iterables) FormatOptions(io.confluent.ksql.schema.utils.FormatOptions) AssertTable(io.confluent.ksql.parser.AssertTable) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) KsqlGenericRecord(io.confluent.ksql.engine.generic.KsqlGenericRecord) MetricCollectors(io.confluent.ksql.metrics.MetricCollectors) AssertStream(io.confluent.ksql.parser.tree.AssertStream) AssertStatement(io.confluent.ksql.parser.tree.AssertStatement) SessionConfig(io.confluent.ksql.config.SessionConfig) ImmutableList(com.google.common.collect.ImmutableList) Injector(io.confluent.ksql.statement.Injector) KsqlTestException(io.confluent.ksql.test.KsqlTestException) CreateSource(io.confluent.ksql.parser.tree.CreateSource) ParsedStatement(io.confluent.ksql.parser.KsqlParser.ParsedStatement) AssertValues(io.confluent.ksql.parser.tree.AssertValues) KsqlTestFolder(io.confluent.ksql.test.util.KsqlTestFolder) GenericRowSerDe(io.confluent.ksql.serde.GenericRowSerDe) Before(org.junit.Before) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) TestStatement(io.confluent.ksql.test.parser.TestStatement) TestFunctionRegistry(io.confluent.ksql.test.tools.TestFunctionRegistry) Properties(java.util.Properties) Logger(org.slf4j.Logger) KsqlEngine(io.confluent.ksql.engine.KsqlEngine) GenericRecordFactory(io.confluent.ksql.engine.generic.GenericRecordFactory) ServiceInfo(io.confluent.ksql.ServiceInfo) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) SqlTestLoader(io.confluent.ksql.test.parser.SqlTestLoader) AssertTombstone(io.confluent.ksql.parser.tree.AssertTombstone) File(java.io.File) CreateAsSelect(io.confluent.ksql.parser.tree.CreateAsSelect) GenericKeySerDe(io.confluent.ksql.serde.GenericKeySerDe) Rule(org.junit.Rule) Paths(java.nio.file.Paths) GenericRow(io.confluent.ksql.GenericRow) QueryEventListener(io.confluent.ksql.engine.QueryEventListener) GenericKey(io.confluent.ksql.GenericKey) PersistenceSchema(io.confluent.ksql.schema.ksql.PersistenceSchema) Collections(java.util.Collections) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) TemporaryFolder(org.junit.rules.TemporaryFolder) UnsetProperty(io.confluent.ksql.parser.tree.UnsetProperty) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) Topology(org.apache.kafka.streams.Topology) Properties(java.util.Properties) TopicInfo(io.confluent.ksql.test.driver.TestDriverPipeline.TopicInfo) DataSource(io.confluent.ksql.metastore.model.DataSource) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) InsertValues(io.confluent.ksql.parser.tree.InsertValues) ExecuteResult(io.confluent.ksql.KsqlExecutionContext.ExecuteResult) SetProperty(io.confluent.ksql.parser.tree.SetProperty) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata)

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