Search in sources :

Example 81 with ConfiguredStatement

use of io.confluent.ksql.statement.ConfiguredStatement in project ksql by confluentinc.

the class RequestHandler method executeStatement.

@SuppressWarnings("unchecked")
private <T extends Statement> Optional<KsqlEntity> executeStatement(final KsqlSecurityContext securityContext, final PreparedStatement<T> prepared, final SessionProperties sessionProperties, final KsqlEntityList entities) {
    final Class<? extends Statement> statementClass = prepared.getStatement().getClass();
    commandQueueSync.waitFor(new KsqlEntityList(entities), statementClass);
    final ConfiguredStatement<T> configured = ConfiguredStatement.of(prepared, SessionConfig.of(this.ksqlEngine.getKsqlConfig(), sessionProperties.getMutableScopedProperties()));
    FeatureFlagChecker.throwOnDisabledFeatures(configured);
    final StatementExecutor<T> executor = (StatementExecutor<T>) customExecutors.getOrDefault(statementClass, (stmt, props, ctx, svcCtx) -> distributor.execute(stmt, ctx, securityContext));
    final StatementExecutorResponse response = executor.execute(configured, sessionProperties, ksqlEngine, securityContext.getServiceContext());
    if (response.isHandled()) {
        return response.getEntity();
    } else {
        return distributor.execute(configured, ksqlEngine, securityContext).getEntity();
    }
}
Also used : KsqlEntityList(io.confluent.ksql.rest.entity.KsqlEntityList) SessionProperties(io.confluent.ksql.rest.SessionProperties) KsqlEngine(io.confluent.ksql.engine.KsqlEngine) KsqlEntityList(io.confluent.ksql.rest.entity.KsqlEntityList) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) KsqlConfig(io.confluent.ksql.util.KsqlConfig) FeatureFlagChecker(io.confluent.ksql.rest.util.FeatureFlagChecker) KsqlSecurityContext(io.confluent.ksql.security.KsqlSecurityContext) DistributingExecutor(io.confluent.ksql.rest.server.computation.DistributingExecutor) SessionConfig(io.confluent.ksql.config.SessionConfig) KsqlEntity(io.confluent.ksql.rest.entity.KsqlEntity) Objects(java.util.Objects) List(java.util.List) Map(java.util.Map) ParsedStatement(io.confluent.ksql.parser.KsqlParser.ParsedStatement) Optional(java.util.Optional) Statement(io.confluent.ksql.parser.tree.Statement) Collections(java.util.Collections) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement)

Example 82 with ConfiguredStatement

use of io.confluent.ksql.statement.ConfiguredStatement in project ksql by confluentinc.

the class DefaultSchemaInjectorFunctionalTest method shouldInferSchema.

private void shouldInferSchema(final org.apache.avro.Schema avroSchema, final Schema expectedKqlSchema) {
    // Given:
    final Schema expectedSchema = new ConnectKsqlSchemaTranslator(SchemaTranslationPolicies.of(this.translationPolicy)).toKsqlSchema(expectedKqlSchema);
    try {
        if (testType == TestType.WITHOUT_SCHEMA_ID) {
            when(srClient.getLatestSchemaMetadata(any())).thenReturn(new SchemaMetadata(1, 1, avroSchema.toString()));
        }
        when(srClient.getSchemaBySubjectAndId(any(), anyInt())).thenReturn(this.avroSchema);
        when(this.avroSchema.schemaType()).thenReturn("AVRO");
        when(this.avroSchema.rawSchema()).thenReturn(avroSchema);
        constructCSASSource(expectedSchema);
    } catch (final Exception e) {
        throw new AssertionError(e);
    }
    final PreparedStatement<Statement> prepared = KsqlParserTestUtil.buildSingleAst(this.statement, metaStore, true);
    // When:
    final KsqlConfig ksqlConfig = new KsqlConfig(ImmutableMap.of());
    final ConfiguredStatement<?> inferred = schemaInjector.inject(ConfiguredStatement.of(prepared, SessionConfig.of(ksqlConfig, ImmutableMap.of())));
    // Then:
    if (testType == TestType.CSAS_WITH_SCHEMA_ID) {
        validateCsasInference((ConfiguredStatement<CreateStreamAsSelect>) inferred, this.avroSchema);
    } else {
        final Statement withSchema = KsqlParserTestUtil.buildSingleAst(inferred.getStatementText(), metaStore, true).getStatement();
        final Schema actual = getSchemaForDdlStatement((CreateSource) withSchema);
        assertThat(FORMATTER.format(actual), equalTo(FORMATTER.format(expectedSchema)));
        assertThat(actual, equalTo(expectedSchema));
    }
}
Also used : SchemaMetadata(io.confluent.kafka.schemaregistry.client.SchemaMetadata) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) Statement(io.confluent.ksql.parser.tree.Statement) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) Schema(org.apache.kafka.connect.data.Schema) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) KsqlConfig(io.confluent.ksql.util.KsqlConfig) ConnectKsqlSchemaTranslator(io.confluent.ksql.serde.connect.ConnectKsqlSchemaTranslator) CreateStreamAsSelect(io.confluent.ksql.parser.tree.CreateStreamAsSelect)

Example 83 with ConfiguredStatement

use of io.confluent.ksql.statement.ConfiguredStatement 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 84 with ConfiguredStatement

use of io.confluent.ksql.statement.ConfiguredStatement in project ksql by confluentinc.

the class QueryExecutor method handleQuery.

private QueryMetadataHolder handleQuery(final ServiceContext serviceContext, final PreparedStatement<Query> statement, final Optional<Boolean> isInternalRequest, final MetricsCallbackHolder metricsCallbackHolder, final Map<String, Object> configOverrides, final Map<String, Object> requestProperties, final Context context, final boolean excludeTombstones) {
    if (statement.getStatement().isPullQuery()) {
        final ImmutableAnalysis analysis = ksqlEngine.analyzeQueryWithNoOutputTopic(statement.getStatement(), statement.getStatementText(), configOverrides);
        final DataSource dataSource = analysis.getFrom().getDataSource();
        final DataSource.DataSourceType dataSourceType = dataSource.getDataSourceType();
        if (!ksqlConfig.getBoolean(KsqlConfig.KSQL_PULL_QUERIES_ENABLE_CONFIG)) {
            throw new KsqlStatementException("Pull queries are disabled." + PullQueryValidator.PULL_QUERY_SYNTAX_HELP + System.lineSeparator() + "Please set " + KsqlConfig.KSQL_PULL_QUERIES_ENABLE_CONFIG + "=true to enable " + "this feature." + System.lineSeparator(), statement.getStatementText());
        }
        Optional<ConsistencyOffsetVector> consistencyOffsetVector = Optional.empty();
        if (ksqlConfig.getBoolean(KsqlConfig.KSQL_QUERY_PULL_CONSISTENCY_OFFSET_VECTOR_ENABLED) && requestProperties.containsKey(KsqlRequestConfig.KSQL_REQUEST_QUERY_PULL_CONSISTENCY_OFFSET_VECTOR)) {
            final String serializedCV = (String) requestProperties.get(KsqlRequestConfig.KSQL_REQUEST_QUERY_PULL_CONSISTENCY_OFFSET_VECTOR);
            // serializedCV will be empty on the first request as the consistency vector is initialized
            // at the server
            consistencyOffsetVector = serializedCV != null && !serializedCV.equals("") ? Optional.of(ConsistencyOffsetVector.deserialize(serializedCV)) : Optional.of(ConsistencyOffsetVector.emptyVector());
        }
        switch(dataSourceType) {
            case KTABLE:
                {
                    // First thing, set the metrics callback so that it gets called, even if we hit an error
                    final AtomicReference<PullQueryResult> resultForMetrics = new AtomicReference<>(null);
                    metricsCallbackHolder.setCallback(QueryMetricsUtil.initializePullTableMetricsCallback(pullQueryMetrics, pullBandRateLimiter, resultForMetrics));
                    final SessionConfig sessionConfig = SessionConfig.of(ksqlConfig, configOverrides);
                    final ConfiguredStatement<Query> configured = ConfiguredStatement.of(statement, sessionConfig);
                    return handleTablePullQuery(analysis, serviceContext, configured, requestProperties, isInternalRequest, pullBandRateLimiter, resultForMetrics, consistencyOffsetVector);
                }
            case KSTREAM:
                {
                    // First thing, set the metrics callback so that it gets called, even if we hit an error
                    final AtomicReference<StreamPullQueryMetadata> resultForMetrics = new AtomicReference<>(null);
                    final AtomicReference<Decrementer> refDecrementer = new AtomicReference<>(null);
                    metricsCallbackHolder.setCallback(QueryMetricsUtil.initializePullStreamMetricsCallback(pullQueryMetrics, pullBandRateLimiter, analysis, resultForMetrics, refDecrementer));
                    final SessionConfig sessionConfig = SessionConfig.of(ksqlConfig, configOverrides);
                    final ConfiguredStatement<Query> configured = ConfiguredStatement.of(statement, sessionConfig);
                    return handleStreamPullQuery(analysis, serviceContext, configured, resultForMetrics, refDecrementer);
                }
            default:
                throw new KsqlStatementException("Unexpected data source type for pull query: " + dataSourceType, statement.getStatementText());
        }
    } else if (ScalablePushUtil.isScalablePushQuery(statement.getStatement(), ksqlEngine, ksqlConfig, configOverrides)) {
        // First thing, set the metrics callback so that it gets called, even if we hit an error
        final AtomicReference<ScalablePushQueryMetadata> resultForMetrics = new AtomicReference<>(null);
        metricsCallbackHolder.setCallback(QueryMetricsUtil.initializeScalablePushMetricsCallback(scalablePushQueryMetrics, scalablePushBandRateLimiter, resultForMetrics));
        final ImmutableAnalysis analysis = ksqlEngine.analyzeQueryWithNoOutputTopic(statement.getStatement(), statement.getStatementText(), configOverrides);
        QueryLogger.info("Scalable push query created", statement.getStatementText());
        return handleScalablePushQuery(analysis, serviceContext, statement, configOverrides, requestProperties, context, scalablePushBandRateLimiter, resultForMetrics);
    } else {
        // log validated statements for query anonymization
        QueryLogger.info("Transient query created", statement.getStatementText());
        return handlePushQuery(serviceContext, statement, configOverrides, excludeTombstones);
    }
}
Also used : ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) ConsistencyOffsetVector(io.confluent.ksql.util.ConsistencyOffsetVector) ImmutableAnalysis(io.confluent.ksql.analyzer.ImmutableAnalysis) SessionConfig(io.confluent.ksql.config.SessionConfig) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) AtomicReference(java.util.concurrent.atomic.AtomicReference) DataSource(io.confluent.ksql.metastore.model.DataSource)

Aggregations

ConfiguredStatement (io.confluent.ksql.statement.ConfiguredStatement)84 Test (org.junit.Test)57 KsqlException (io.confluent.ksql.util.KsqlException)23 Statement (io.confluent.ksql.parser.tree.Statement)22 PreparedStatement (io.confluent.ksql.parser.KsqlParser.PreparedStatement)19 Optional (java.util.Optional)19 ServiceContext (io.confluent.ksql.services.ServiceContext)18 PersistentQueryMetadata (io.confluent.ksql.util.PersistentQueryMetadata)18 KsqlEngine (io.confluent.ksql.engine.KsqlEngine)17 KsqlConfig (io.confluent.ksql.util.KsqlConfig)16 Map (java.util.Map)14 DataSource (io.confluent.ksql.metastore.model.DataSource)12 QueryId (io.confluent.ksql.query.QueryId)12 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)12 Collectors (java.util.stream.Collectors)12 ImmutableMap (com.google.common.collect.ImmutableMap)11 ListQueries (io.confluent.ksql.parser.tree.ListQueries)11 KsqlStatementException (io.confluent.ksql.util.KsqlStatementException)11 List (java.util.List)11 Query (io.confluent.ksql.parser.tree.Query)10