Search in sources :

Example 16 with InsertValues

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

the class InsertValuesExecutorTest method shouldSupportInsertIntoWithSchemaInfereceMatch.

@Test
public void shouldSupportInsertIntoWithSchemaInfereceMatch() throws Exception {
    // Given:
    when(srClient.getLatestSchemaMetadata(Mockito.any())).thenReturn(new SchemaMetadata(1, 1, "\"string\""));
    givenDataSourceWithSchema(TOPIC_NAME, SCHEMA, SerdeFeatures.of(SerdeFeature.SCHEMA_INFERENCE, SerdeFeature.UNWRAP_SINGLES), SerdeFeatures.of(), FormatInfo.of(FormatFactory.AVRO.name()), FormatInfo.of(FormatFactory.AVRO.name()), false, false);
    final ConfiguredStatement<InsertValues> statement = givenInsertValues(ImmutableList.of(K0, COL0), ImmutableList.of(new StringLiteral("foo"), new StringLiteral("bar")));
    // When:
    executor.execute(statement, mock(SessionProperties.class), engine, serviceContext);
    // Then:
    verify(keySerializer).serialize(TOPIC_NAME, genericKey("foo"));
    verify(valueSerializer).serialize(TOPIC_NAME, genericRow("bar", null));
    verify(producer).send(new ProducerRecord<>(TOPIC_NAME, null, 1L, KEY, VALUE));
}
Also used : SessionProperties(io.confluent.ksql.rest.SessionProperties) SchemaMetadata(io.confluent.kafka.schemaregistry.client.SchemaMetadata) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) InsertValues(io.confluent.ksql.parser.tree.InsertValues) Test(org.junit.Test)

Example 17 with InsertValues

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

the class InsertValuesExecutorTest method shouldHandleNegativeValueExpression.

@Test
public void shouldHandleNegativeValueExpression() {
    // Given:
    givenSourceStreamWithSchema(SCHEMA, SerdeFeatures.of(), SerdeFeatures.of());
    final ConfiguredStatement<InsertValues> statement = givenInsertValues(ImmutableList.of(COL0, COL1), ImmutableList.of(new StringLiteral("str"), ArithmeticUnaryExpression.negative(Optional.empty(), new LongLiteral(1))));
    // When:
    executor.execute(statement, mock(SessionProperties.class), engine, serviceContext);
    // Then:
    verify(keySerializer).serialize(TOPIC_NAME, genericKey((String) null));
    verify(valueSerializer).serialize(TOPIC_NAME, genericRow("str", -1L));
    verify(producer).send(new ProducerRecord<>(TOPIC_NAME, null, 1L, KEY, VALUE));
}
Also used : SessionProperties(io.confluent.ksql.rest.SessionProperties) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) InsertValues(io.confluent.ksql.parser.tree.InsertValues) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 18 with InsertValues

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

the class InsertValuesExecutorTest method shouldThrowWhenNotAuthorizedToWriteValSchemaToSR.

@Test
public void shouldThrowWhenNotAuthorizedToWriteValSchemaToSR() throws Exception {
    // Given:
    when(srClient.getLatestSchemaMetadata(Mockito.any())).thenReturn(new SchemaMetadata(1, 1, "\"string\""));
    givenDataSourceWithSchema(TOPIC_NAME, SCHEMA, SerdeFeatures.of(SerdeFeature.UNWRAP_SINGLES), SerdeFeatures.of(), FormatInfo.of(FormatFactory.AVRO.name()), FormatInfo.of(FormatFactory.AVRO.name()), false, false);
    final ConfiguredStatement<InsertValues> statement = givenInsertValues(allColumnNames(SCHEMA), ImmutableList.of(new StringLiteral("key"), new StringLiteral("str"), new LongLiteral(2L)));
    when(valueSerializer.serialize(any(), any())).thenThrow(new RuntimeException(new RestClientException("foo", 401, 1)));
    // When:
    final Exception e = assertThrows(KsqlException.class, () -> executor.execute(statement, mock(SessionProperties.class), engine, serviceContext));
    // Then:
    assertThat(e.getMessage(), containsString("Authorization denied to Write on Schema Registry subject: [" + KsqlConstants.getSRSubject(TOPIC_NAME, false)));
}
Also used : SchemaMetadata(io.confluent.kafka.schemaregistry.client.SchemaMetadata) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) InsertValues(io.confluent.ksql.parser.tree.InsertValues) RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException) 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 19 with InsertValues

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

the class InsertValuesExecutorTest method shouldThrowOnInsertKeyHeaders.

@Test
public void shouldThrowOnInsertKeyHeaders() {
    // Given:
    givenSourceStreamWithSchema(SCHEMA_WITH_KEY_HEADERS, SerdeFeatures.of(), SerdeFeatures.of());
    final ConfiguredStatement<InsertValues> statement = givenInsertValues(allColumnNames(SCHEMA_WITH_KEY_HEADERS), ImmutableList.of(new StringLiteral("key"), new StringLiteral("str"), new LongLiteral(2L), new NullLiteral(), new NullLiteral()));
    // When:
    final Exception e = assertThrows(KsqlException.class, () -> executor.execute(statement, mock(SessionProperties.class), engine, serviceContext));
    // Then:
    assertThat(e.getMessage(), is("Cannot insert into HEADER columns: HEAD0, HEAD1"));
}
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) NullLiteral(io.confluent.ksql.execution.expression.tree.NullLiteral) 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 20 with InsertValues

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

the class InsertValuesExecutorTest method shouldThrowOnSchemaInferenceMismatchForKey.

@Test
public void shouldThrowOnSchemaInferenceMismatchForKey() throws Exception {
    // Given:
    when(srClient.getLatestSchemaMetadata(Mockito.any())).thenReturn(new SchemaMetadata(1, 1, "schema"));
    givenDataSourceWithSchema(TOPIC_NAME, SCHEMA, SerdeFeatures.of(SerdeFeature.SCHEMA_INFERENCE), SerdeFeatures.of(), FormatInfo.of(FormatFactory.AVRO.name()), FormatInfo.of(FormatFactory.AVRO.name()), false, false);
    final ConfiguredStatement<InsertValues> statement = givenInsertValues(ImmutableList.of(K0, COL0), ImmutableList.of(new StringLiteral("foo"), new StringLiteral("bar")));
    // When:
    final Exception e = assertThrows(KsqlException.class, () -> executor.execute(statement, mock(SessionProperties.class), engine, serviceContext));
    // Then:
    assertThat(e.getMessage(), containsString("ksqlDB generated schema would overwrite existing key schema"));
    assertThat(e.getMessage(), containsString("Existing Schema: schema"));
    assertThat(e.getMessage(), containsString("ksqlDB Generated: {\"type\":"));
}
Also used : SchemaMetadata(io.confluent.kafka.schemaregistry.client.SchemaMetadata) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) 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)

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