Search in sources :

Example 6 with InsertInto

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

the class DistributingExecutorTest method shouldThrowExceptionWhenInsertIntoSourceWithHeaders.

@Test
public void shouldThrowExceptionWhenInsertIntoSourceWithHeaders() {
    // Given
    final PreparedStatement<Statement> preparedStatement = PreparedStatement.of("", new InsertInto(SourceName.of("s1"), mock(Query.class)));
    final ConfiguredStatement<Statement> configured = ConfiguredStatement.of(preparedStatement, SessionConfig.of(KSQL_CONFIG, ImmutableMap.of()));
    final DataSource dataSource = mock(DataSource.class);
    final LogicalSchema schema = mock(LogicalSchema.class);
    doReturn(dataSource).when(metaStore).getSource(SourceName.of("s1"));
    doReturn(schema).when(dataSource).getSchema();
    doReturn(ImmutableList.of(ColumnName.of("a"))).when(schema).headers();
    when(dataSource.getKafkaTopicName()).thenReturn("topic");
    // When:
    final Exception e = assertThrows(KsqlException.class, () -> distributor.execute(configured, executionContext, mock(KsqlSecurityContext.class)));
    // Then:
    assertThat(e.getMessage(), is("Cannot insert into s1 because it has header columns"));
}
Also used : ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) Statement(io.confluent.ksql.parser.tree.Statement) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) InsertInto(io.confluent.ksql.parser.tree.InsertInto) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) KsqlTopicAuthorizationException(io.confluent.ksql.exception.KsqlTopicAuthorizationException) ProducerFencedException(org.apache.kafka.common.errors.ProducerFencedException) KsqlRestException(io.confluent.ksql.rest.server.resources.KsqlRestException) KsqlException(io.confluent.ksql.util.KsqlException) TimeoutException(org.apache.kafka.common.errors.TimeoutException) KsqlServerException(io.confluent.ksql.util.KsqlServerException) DataSource(io.confluent.ksql.metastore.model.DataSource) Test(org.junit.Test)

Example 7 with InsertInto

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

the class StatementRewriterTest method shouldRewriteInsertIntoWithPartitionBy.

@Test
public void shouldRewriteInsertIntoWithPartitionBy() {
    // Given:
    final InsertInto ii = new InsertInto(location, sourceName, query, insertIntoProperties);
    when(mockRewriter.apply(query, context)).thenReturn(rewrittenQuery);
    when(expressionRewriter.apply(expression, context)).thenReturn(rewrittenExpression);
    // When:
    final AstNode rewritten = rewriter.rewrite(ii, context);
    // Then:
    assertThat(rewritten, equalTo(new InsertInto(location, sourceName, rewrittenQuery, insertIntoProperties)));
}
Also used : InsertInto(io.confluent.ksql.parser.tree.InsertInto) AstNode(io.confluent.ksql.parser.tree.AstNode) Test(org.junit.Test)

Example 8 with InsertInto

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

the class StatementRewriterTest method shouldRewriteInsertInto.

@Test
public void shouldRewriteInsertInto() {
    // Given:
    final InsertInto ii = new InsertInto(location, sourceName, query, insertIntoProperties);
    when(mockRewriter.apply(query, context)).thenReturn(rewrittenQuery);
    // When:
    final AstNode rewritten = rewriter.rewrite(ii, context);
    // Then:
    assertThat(rewritten, equalTo(new InsertInto(location, sourceName, rewrittenQuery, insertIntoProperties)));
}
Also used : InsertInto(io.confluent.ksql.parser.tree.InsertInto) AstNode(io.confluent.ksql.parser.tree.AstNode) Test(org.junit.Test)

Example 9 with InsertInto

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

the class RequestValidator method validate.

/**
 * @return the number of persistent queries that were validated
 *
 * @throws KsqlStatementException if the statement cannot be validated
 */
@SuppressWarnings("unchecked")
private <T extends Statement> int validate(final ServiceContext serviceContext, final ConfiguredStatement<T> configured, final SessionProperties sessionProperties, final KsqlExecutionContext executionContext, final Injector injector) throws KsqlStatementException {
    final Statement statement = configured.getStatement();
    final Class<? extends Statement> statementClass = statement.getClass();
    final StatementValidator<T> customValidator = (StatementValidator<T>) customValidators.get(statementClass);
    if (customValidator != null) {
        customValidator.validate(configured, sessionProperties, executionContext, serviceContext);
    } else if (KsqlEngine.isExecutableStatement(configured.getStatement()) || configured.getStatement() instanceof TerminateQuery) {
        final ConfiguredStatement<?> statementInjected = injector.inject(configured);
        distributedStatementValidator.create(statementInjected, serviceContext, executionContext);
    } else {
        throw new KsqlStatementException("Do not know how to validate statement of type: " + statementClass + " Known types: " + customValidators.keySet(), configured.getStatementText());
    }
    return (statement instanceof CreateAsSelect || statement instanceof InsertInto) ? 1 : 0;
}
Also used : ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) TerminateQuery(io.confluent.ksql.parser.tree.TerminateQuery) 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) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) InsertInto(io.confluent.ksql.parser.tree.InsertInto) CreateAsSelect(io.confluent.ksql.parser.tree.CreateAsSelect)

Example 10 with InsertInto

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

the class KsqlParserTest method testInsertInto.

@Test
public void testInsertInto() {
    final String insertIntoString = "INSERT INTO test0 " + "SELECT col0, col2, col3 FROM test1 WHERE col0 > 100;";
    final Statement statement = KsqlParserTestUtil.buildSingleAst(insertIntoString, metaStore).getStatement();
    assertThat(statement, instanceOf(InsertInto.class));
    final InsertInto insertInto = (InsertInto) statement;
    assertThat(insertInto.getTarget(), equalTo(SourceName.of("TEST0")));
    final Query query = insertInto.getQuery();
    assertThat(query.getSelect().getSelectItems(), hasSize(3));
    assertThat(query.getFrom(), not(nullValue()));
    assertThat(query.getWhere().isPresent(), equalTo(true));
    assertThat(query.getWhere().get(), instanceOf(ComparisonExpression.class));
    final ComparisonExpression comparisonExpression = (ComparisonExpression) query.getWhere().get();
    assertThat(comparisonExpression.getType().getValue(), equalTo(">"));
}
Also used : ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) Query(io.confluent.ksql.parser.tree.Query) TerminateQuery(io.confluent.ksql.parser.tree.TerminateQuery) Statement(io.confluent.ksql.parser.tree.Statement) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) InsertInto(io.confluent.ksql.parser.tree.InsertInto) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Aggregations

InsertInto (io.confluent.ksql.parser.tree.InsertInto)12 Test (org.junit.Test)9 PreparedStatement (io.confluent.ksql.parser.KsqlParser.PreparedStatement)7 Statement (io.confluent.ksql.parser.tree.Statement)6 KsqlException (io.confluent.ksql.util.KsqlException)6 KsqlRestException (io.confluent.ksql.rest.server.resources.KsqlRestException)5 ConfiguredStatement (io.confluent.ksql.statement.ConfiguredStatement)5 KsqlServerException (io.confluent.ksql.util.KsqlServerException)5 ProducerFencedException (org.apache.kafka.common.errors.ProducerFencedException)5 TimeoutException (org.apache.kafka.common.errors.TimeoutException)5 KsqlTopicAuthorizationException (io.confluent.ksql.exception.KsqlTopicAuthorizationException)4 DataSource (io.confluent.ksql.metastore.model.DataSource)3 TerminateQuery (io.confluent.ksql.parser.tree.TerminateQuery)3 AstNode (io.confluent.ksql.parser.tree.AstNode)2 CreateAsSelect (io.confluent.ksql.parser.tree.CreateAsSelect)2 CommandStatus (io.confluent.ksql.rest.entity.CommandStatus)2 ComparisonExpression (io.confluent.ksql.execution.expression.tree.ComparisonExpression)1 ParsedStatement (io.confluent.ksql.parser.KsqlParser.ParsedStatement)1 AlterSystemProperty (io.confluent.ksql.parser.tree.AlterSystemProperty)1 ExecutableDdlStatement (io.confluent.ksql.parser.tree.ExecutableDdlStatement)1