use of io.confluent.ksql.parser.KsqlParser.PreparedStatement in project ksql by confluentinc.
the class KsqlParserTest method shouldParseBracketedComment.
@Test
public void shouldParseBracketedComment() {
final String statementString = "/* this is a bracketed comment. */\n" + "SHOW STREAMS;" + "/*another comment!*/";
final List<PreparedStatement<?>> statements = KsqlParserTestUtil.buildAst(statementString, metaStore);
assertThat(statements, hasSize(1));
assertThat(statements.get(0).getStatement(), is(instanceOf(ListStreams.class)));
}
use of io.confluent.ksql.parser.KsqlParser.PreparedStatement in project ksql by confluentinc.
the class StreamedQueryResourceTest method setup.
@Before
public void setup() {
when(serviceContext.getTopicClient()).thenReturn(mockKafkaTopicClient);
query = PreparedStatement.of(PUSH_QUERY_STRING, mock(Query.class));
invalid = PreparedStatement.of("sql", mock(Statement.class));
when(mockStatementParser.parseSingleStatement(PUSH_QUERY_STRING)).thenReturn(invalid);
final PreparedStatement<Statement> pullQueryStatement = PreparedStatement.of(PULL_QUERY_STRING, mock(Query.class));
when(mockStatementParser.parseSingleStatement(PULL_QUERY_STRING)).thenReturn(pullQueryStatement);
when(errorsHandler.accessDeniedFromKafkaResponse(any(Exception.class))).thenReturn(AUTHORIZATION_ERROR_RESPONSE);
when(errorsHandler.generateResponse(exception.capture(), any())).thenReturn(EndpointResponse.failed(500));
when(queryExecutor.handleStatement(any(), any(), any(), any(), any(), any(), any(), anyBoolean())).thenReturn(queryMetadataHolder);
when(pullQueryResult.getPullQueryQueue()).thenReturn(pullQueryQueue);
securityContext = new KsqlSecurityContext(Optional.empty(), serviceContext);
testResource = new StreamedQueryResource(mockKsqlEngine, ksqlRestConfig, mockStatementParser, commandQueue, DISCONNECT_CHECK_INTERVAL, COMMAND_QUEUE_CATCHUP_TIMOEUT, activenessRegistrar, Optional.of(authorizationValidator), errorsHandler, denyListPropertyValidator, queryExecutor);
testResource.configure(VALID_CONFIG);
}
use of io.confluent.ksql.parser.KsqlParser.PreparedStatement in project ksql by confluentinc.
the class SqlFormatInjector method inject.
@SuppressWarnings("unchecked")
@Override
public <T extends Statement> ConfiguredStatement<T> inject(final ConfiguredStatement<T> statement) {
try {
final Statement node = statement.getStatement();
final String sql = SqlFormatter.formatSql(node);
final String sqlWithSemiColon = sql.endsWith(";") ? sql : sql + ";";
final PreparedStatement<?> prepare = executionContext.prepare(executionContext.parse(sqlWithSemiColon).get(0));
return statement.withStatement(sql, (T) prepare.getStatement());
} catch (final Exception e) {
throw new KsqlException("Unable to format statement! This is bad because " + "it means we cannot persist it onto the command topic: " + statement, e);
}
}
use of io.confluent.ksql.parser.KsqlParser.PreparedStatement in project ksql by confluentinc.
the class ExpressionParseTestUtil method parseExpression.
public static Expression parseExpression(final String asText, final MetaStore metaStore) {
final KsqlParser parser = new DefaultKsqlParser();
final String ksql = String.format("SELECT %s FROM test1;", asText);
final ParsedStatement parsedStatement = parser.parse(ksql).get(0);
final PreparedStatement preparedStatement = parser.prepare(parsedStatement, metaStore);
final SingleColumn singleColumn = (SingleColumn) ((Query) preparedStatement.getStatement()).getSelect().getSelectItems().get(0);
return singleColumn.getExpression();
}
use of io.confluent.ksql.parser.KsqlParser.PreparedStatement in project ksql by confluentinc.
the class DistributingExecutorTest method shouldThrowServerExceptionIfServerServiceContextIsDeniedAuthorization.
@Test
public void shouldThrowServerExceptionIfServerServiceContextIsDeniedAuthorization() {
// Given:
final KsqlSecurityContext userSecurityContext = new KsqlSecurityContext(Optional.empty(), SandboxedServiceContext.create(TestServiceContext.create()));
final PreparedStatement<Statement> preparedStatement = PreparedStatement.of("", new ListProperties(Optional.empty()));
final ConfiguredStatement<Statement> configured = ConfiguredStatement.of(preparedStatement, SessionConfig.of(KSQL_CONFIG, ImmutableMap.of()));
doNothing().when(authorizationValidator).checkAuthorization(eq(userSecurityContext), any(), any());
doThrow(KsqlTopicAuthorizationException.class).when(authorizationValidator).checkAuthorization(ArgumentMatchers.argThat(securityContext -> securityContext.getServiceContext() == serviceContext), any(), any());
// When:
final Exception e = assertThrows(KsqlServerException.class, () -> distributor.execute(configured, executionContext, userSecurityContext));
// Then:
assertThat(e.getCause(), (is(instanceOf(KsqlTopicAuthorizationException.class))));
}
Aggregations