use of io.confluent.ksql.parser.tree.ExecutableDdlStatement in project ksql by confluentinc.
the class EngineExecutor method sourceTablePlan.
@SuppressFBWarnings(value = "NP_NULL_PARAM_DEREF_NONVIRTUAL")
private KsqlPlan sourceTablePlan(final ConfiguredStatement<?> statement) {
final CreateTable createTable = (CreateTable) statement.getStatement();
final CreateTableCommand ddlCommand = (CreateTableCommand) engineContext.createDdlCommand(statement.getStatementText(), (ExecutableDdlStatement) statement.getStatement(), config);
final Relation from = new AliasedRelation(new Table(createTable.getName()), createTable.getName());
// Only VALUE or HEADER columns must be selected from the source table. When running a
// pull query, the keys are added if selecting all columns.
final Select select = new Select(createTable.getElements().stream().filter(column -> !column.getConstraints().isKey() && !column.getConstraints().isPrimaryKey()).map(column -> new SingleColumn(new UnqualifiedColumnReferenceExp(column.getName()), Optional.of(column.getName()))).collect(Collectors.toList()));
// Source table need to keep emitting changes so every new record is materialized for
// pull query availability.
final RefinementInfo refinementInfo = RefinementInfo.of(OutputRefinement.CHANGES);
// This is a plan for a `select * from <source-table> emit changes` statement,
// without a sink topic to write the results. The query is just made to materialize the
// source table.
final Query query = new Query(Optional.empty(), select, from, Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(refinementInfo), false, OptionalInt.empty());
// The source table does not exist in the current metastore, so a temporary metastore that
// contains only the source table is created here. This metastore is used later to create
// ExecutorsPlan.
final MutableMetaStore tempMetastore = new MetaStoreImpl(new InternalFunctionRegistry());
final Formats formats = ddlCommand.getFormats();
tempMetastore.putSource(new KsqlTable<>(statement.getStatementText(), createTable.getName(), ddlCommand.getSchema(), Optional.empty(), false, new KsqlTopic(ddlCommand.getTopicName(), KeyFormat.of(formats.getKeyFormat(), formats.getKeyFeatures(), Optional.empty()), ValueFormat.of(formats.getValueFormat(), formats.getValueFeatures())), true), false);
final ExecutorPlans plans = planQuery(statement, query, Optional.empty(), Optional.empty(), tempMetastore);
final KsqlBareOutputNode outputNode = (KsqlBareOutputNode) plans.logicalPlan.getNode().get();
final QueryPlan queryPlan = new QueryPlan(getSourceNames(outputNode), Optional.empty(), 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(), Optional.of(ddlCommand), queryPlan);
}
use of io.confluent.ksql.parser.tree.ExecutableDdlStatement 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.ExecutableDdlStatement in project ksql by confluentinc.
the class CommandFactoriesTest method shouldThrowOnUnsupportedStatementType.
@Test(expected = KsqlException.class)
public void shouldThrowOnUnsupportedStatementType() {
// Given:
final ExecutableDdlStatement ddlStatement = new ExecutableDdlStatement() {
};
// Then:
commandFactories.create(sqlExpression, ddlStatement, SessionConfig.of(ksqlConfig, emptyMap()));
}
use of io.confluent.ksql.parser.tree.ExecutableDdlStatement in project ksql by confluentinc.
the class InteractiveStatementExecutor method executeStatement.
@SuppressWarnings("unchecked")
private void executeStatement(final PreparedStatement<?> statement, final CommandId commandId, final Optional<CommandStatusFuture> commandStatusFuture) {
if (statement.getStatement() instanceof TerminateQuery) {
terminateQuery((PreparedStatement<TerminateQuery>) statement);
final String successMessage = "Query terminated.";
final CommandStatus successStatus = new CommandStatus(CommandStatus.Status.SUCCESS, successMessage, Optional.empty());
putFinalStatus(commandId, commandStatusFuture, successStatus);
} else if (statement.getStatement() instanceof ExecutableDdlStatement) {
throwUnsupportedStatementError();
} else if (statement.getStatement() instanceof CreateAsSelect) {
throwUnsupportedStatementError();
} else if (statement.getStatement() instanceof InsertInto) {
throwUnsupportedStatementError();
} else if (statement.getStatement() instanceof AlterSystemProperty) {
final PreparedStatement<AlterSystemProperty> alterSystemQuery = (PreparedStatement<AlterSystemProperty>) statement;
final String propertyName = alterSystemQuery.getStatement().getPropertyName();
final String propertyValue = alterSystemQuery.getStatement().getPropertyValue();
ksqlEngine.alterSystemProperty(propertyName, propertyValue);
ksqlEngine.updateStreamsPropertiesAndRestartRuntime();
final String successMessage = String.format("System property %s was set to %s.", propertyName, propertyValue);
final CommandStatus successStatus = new CommandStatus(CommandStatus.Status.SUCCESS, successMessage, Optional.empty());
putFinalStatus(commandId, commandStatusFuture, successStatus);
} else {
throw new KsqlException(String.format("Unexpected statement type: %s", statement.getClass().getName()));
}
}
Aggregations