use of io.confluent.ksql.parser.tree.QueryContainer in project ksql by confluentinc.
the class ExplainExecutor method explainStatement.
private static QueryDescription explainStatement(final ConfiguredStatement<Explain> explain, final KsqlExecutionContext executionContext, final ServiceContext serviceContext) {
final Statement statement = explain.getStatement().getStatement().orElseThrow(() -> new KsqlStatementException("must have either queryID or statement", explain.getStatementText()));
if (!(statement instanceof Query || statement instanceof QueryContainer)) {
throw new KsqlException("The provided statement does not run a ksql query");
}
final PreparedStatement<?> preparedStatement = PreparedStatement.of(explain.getStatementText().substring("EXPLAIN ".length()), statement);
final QueryMetadata metadata;
final KsqlExecutionContext sandbox = executionContext.createSandbox(serviceContext);
if (preparedStatement.getStatement() instanceof Query) {
metadata = sandbox.executeTransientQuery(serviceContext, ConfiguredStatement.of(preparedStatement, explain.getSessionConfig()).cast(), false);
} else {
metadata = sandbox.execute(serviceContext, ConfiguredStatement.of(preparedStatement, explain.getSessionConfig())).getQuery().orElseThrow(() -> new IllegalStateException("The provided statement did not run a ksql query"));
}
return QueryDescriptionFactory.forQueryMetadata(metadata, Collections.emptyMap());
}
use of io.confluent.ksql.parser.tree.QueryContainer in project ksql by confluentinc.
the class EngineExecutor method plan.
// Known to be non-empty
@SuppressWarnings("OptionalGetWithoutIsPresent")
KsqlPlan plan(final ConfiguredStatement<?> statement) {
try {
throwOnNonExecutableStatement(statement);
if (statement.getStatement() instanceof ExecutableDdlStatement) {
final boolean isSourceStream = statement.getStatement() instanceof CreateStream && ((CreateStream) statement.getStatement()).isSource();
final boolean isSourceTable = statement.getStatement() instanceof CreateTable && ((CreateTable) statement.getStatement()).isSource();
if ((isSourceStream || isSourceTable) && !isSourceTableMaterializationEnabled()) {
throw new KsqlStatementException("Cannot execute command because source table " + "materialization is disabled.", statement.getStatementText());
}
if (isSourceTable) {
return sourceTablePlan(statement);
} else {
final DdlCommand ddlCommand = engineContext.createDdlCommand(statement.getStatementText(), (ExecutableDdlStatement) statement.getStatement(), config);
return KsqlPlan.ddlPlanCurrent(statement.getStatementText(), ddlCommand);
}
}
final QueryContainer queryContainer = (QueryContainer) statement.getStatement();
final ExecutorPlans plans = planQuery(statement, queryContainer.getQuery(), Optional.of(queryContainer.getSink()), queryContainer.getQueryId(), engineContext.getMetaStore());
final KsqlStructuredDataOutputNode outputNode = (KsqlStructuredDataOutputNode) plans.logicalPlan.getNode().get();
final Optional<DdlCommand> ddlCommand = maybeCreateSinkDdl(statement, outputNode);
validateResultType(outputNode.getNodeOutputType(), statement);
final QueryPlan queryPlan = new QueryPlan(getSourceNames(outputNode), outputNode.getSinkName(), plans.physicalPlan.getPhysicalPlan(), plans.physicalPlan.getQueryId(), getApplicationId(plans.physicalPlan.getQueryId(), getSourceNames(outputNode)));
engineContext.createQueryValidator().validateQuery(config, plans.physicalPlan, engineContext.getQueryRegistry().getAllLiveQueries());
return KsqlPlan.queryPlanCurrent(statement.getStatementText(), ddlCommand, queryPlan);
} catch (final KsqlStatementException e) {
throw e;
} catch (final Exception e) {
throw new KsqlStatementException(e.getMessage(), statement.getStatementText(), e);
}
}
use of io.confluent.ksql.parser.tree.QueryContainer in project ksql by confluentinc.
the class AstBuilderTest method shouldDefaultToEmitChangesForCsas.
@Test
public void shouldDefaultToEmitChangesForCsas() {
// Given:
final SingleStatementContext stmt = givenQuery("CREATE STREAM X AS SELECT * FROM TEST1;");
// When:
final Query result = ((QueryContainer) builder.buildStatement(stmt)).getQuery();
// Then:
assertThat("Should be push", result.isPullQuery(), is(false));
assertThat(result.getRefinement().get().getOutputRefinement(), is(OutputRefinement.CHANGES));
}
use of io.confluent.ksql.parser.tree.QueryContainer in project ksql by confluentinc.
the class AstBuilderTest method shouldSupportExplicitEmitChangesForInsertInto.
@Test
public void shouldSupportExplicitEmitChangesForInsertInto() {
// Given:
final SingleStatementContext stmt = givenQuery("INSERT INTO TEST1 SELECT * FROM TEST2 EMIT CHANGES;");
// When:
final Query result = ((QueryContainer) builder.buildStatement(stmt)).getQuery();
// Then:
assertThat("Should be push", result.isPullQuery(), is(false));
assertThat(result.getRefinement().get().getOutputRefinement(), is(OutputRefinement.CHANGES));
}
use of io.confluent.ksql.parser.tree.QueryContainer in project ksql by confluentinc.
the class AstBuilderTest method shouldSupportExplicitEmitChangesForCtas.
@Test
public void shouldSupportExplicitEmitChangesForCtas() {
// Given:
final SingleStatementContext stmt = givenQuery("CREATE TABLE X AS SELECT COUNT(1) FROM TEST1 GROUP BY ROWKEY EMIT CHANGES;");
// When:
final Query result = ((QueryContainer) builder.buildStatement(stmt)).getQuery();
// Then:
assertThat("Should be push", result.isPullQuery(), is(false));
assertThat(result.getRefinement().get().getOutputRefinement(), is(OutputRefinement.CHANGES));
}
Aggregations