Search in sources :

Example 21 with QuerySpecification

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

the class KsqlEngine method addInto.

public Query addInto(final Query query, final QuerySpecification querySpecification, final String intoName, final Map<String, Expression> intoProperties, final Optional<Expression> partitionByExpression) {
    Table intoTable = new Table(QualifiedName.of(intoName));
    if (partitionByExpression.isPresent()) {
        Map<String, Expression> newIntoProperties = new HashMap<>();
        newIntoProperties.putAll(intoProperties);
        newIntoProperties.put(DdlConfig.PARTITION_BY_PROPERTY, partitionByExpression.get());
        intoTable.setProperties(newIntoProperties);
    } else {
        intoTable.setProperties(intoProperties);
    }
    QuerySpecification newQuerySpecification = new QuerySpecification(querySpecification.getSelect(), intoTable, querySpecification.getFrom(), querySpecification.getWindowExpression(), querySpecification.getWhere(), querySpecification.getGroupBy(), querySpecification.getHaving(), querySpecification.getLimit());
    return new Query(newQuerySpecification, query.getLimit());
}
Also used : QuerySpecification(io.confluent.ksql.parser.tree.QuerySpecification) CreateTable(io.confluent.ksql.parser.tree.CreateTable) DropTable(io.confluent.ksql.parser.tree.DropTable) Table(io.confluent.ksql.parser.tree.Table) Query(io.confluent.ksql.parser.tree.Query) Expression(io.confluent.ksql.parser.tree.Expression) HashMap(java.util.HashMap)

Example 22 with QuerySpecification

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

the class KsqlEngine method buildSingleQueryAst.

private Pair<String, Statement> buildSingleQueryAst(final Statement statement, final String statementString, final MetaStore tempMetaStore, final MetaStore tempMetaStoreForParser, final Map<String, Object> overriddenProperties) {
    log.info("Building AST for {}.", statementString);
    if (statement instanceof Query) {
        return new Pair<>(statementString, statement);
    } else if (statement instanceof CreateAsSelect) {
        CreateAsSelect createAsSelect = (CreateAsSelect) statement;
        QuerySpecification querySpecification = (QuerySpecification) createAsSelect.getQuery().getQueryBody();
        Query query = addInto(createAsSelect.getQuery(), querySpecification, createAsSelect.getName().getSuffix(), createAsSelect.getProperties(), createAsSelect.getPartitionByColumn());
        tempMetaStoreForParser.putSource(queryEngine.getResultDatasource(querySpecification.getSelect(), createAsSelect.getName().getSuffix()).cloneWithTimeKeyColumns());
        return new Pair<>(statementString, query);
    } else if (statement instanceof RegisterTopic) {
        ddlCommandExec.tryExecute(new RegisterTopicCommand((RegisterTopic) statement), tempMetaStoreForParser);
        ddlCommandExec.tryExecute(new RegisterTopicCommand((RegisterTopic) statement), tempMetaStore);
        return new Pair<>(statementString, statement);
    } else if (statement instanceof CreateStream) {
        ddlCommandExec.tryExecute(new CreateStreamCommand(statementString, (CreateStream) statement, overriddenProperties, topicClient, false), tempMetaStoreForParser);
        ddlCommandExec.tryExecute(new CreateStreamCommand(statementString, (CreateStream) statement, overriddenProperties, topicClient, false), tempMetaStore);
        return new Pair<>(statementString, statement);
    } else if (statement instanceof CreateTable) {
        ddlCommandExec.tryExecute(new CreateTableCommand(statementString, (CreateTable) statement, overriddenProperties, topicClient, false), tempMetaStoreForParser);
        ddlCommandExec.tryExecute(new CreateTableCommand(statementString, (CreateTable) statement, overriddenProperties, topicClient, false), tempMetaStore);
        return new Pair<>(statementString, statement);
    } else if (statement instanceof DropStream) {
        ddlCommandExec.tryExecute(new DropSourceCommand((DropStream) statement, DataSource.DataSourceType.KSTREAM, this), tempMetaStore);
        ddlCommandExec.tryExecute(new DropSourceCommand((DropStream) statement, DataSource.DataSourceType.KSTREAM, this), tempMetaStoreForParser);
        return new Pair<>(statementString, statement);
    } else if (statement instanceof DropTable) {
        ddlCommandExec.tryExecute(new DropSourceCommand((DropTable) statement, DataSource.DataSourceType.KTABLE, this), tempMetaStore);
        ddlCommandExec.tryExecute(new DropSourceCommand((DropTable) statement, DataSource.DataSourceType.KTABLE, this), tempMetaStoreForParser);
        return new Pair<>(statementString, statement);
    } else if (statement instanceof DropTopic) {
        ddlCommandExec.tryExecute(new DropTopicCommand((DropTopic) statement), tempMetaStore);
        ddlCommandExec.tryExecute(new DropTopicCommand((DropTopic) statement), tempMetaStoreForParser);
        return new Pair<>(statementString, statement);
    } else if (statement instanceof SetProperty) {
        return new Pair<>(statementString, statement);
    }
    return null;
}
Also used : DropTopicCommand(io.confluent.ksql.ddl.commands.DropTopicCommand) Query(io.confluent.ksql.parser.tree.Query) RegisterTopic(io.confluent.ksql.parser.tree.RegisterTopic) DropSourceCommand(io.confluent.ksql.ddl.commands.DropSourceCommand) CreateTable(io.confluent.ksql.parser.tree.CreateTable) CreateStream(io.confluent.ksql.parser.tree.CreateStream) CreateAsSelect(io.confluent.ksql.parser.tree.CreateAsSelect) RegisterTopicCommand(io.confluent.ksql.ddl.commands.RegisterTopicCommand) DropTable(io.confluent.ksql.parser.tree.DropTable) CreateTableCommand(io.confluent.ksql.ddl.commands.CreateTableCommand) QuerySpecification(io.confluent.ksql.parser.tree.QuerySpecification) CreateStreamCommand(io.confluent.ksql.ddl.commands.CreateStreamCommand) DropTopic(io.confluent.ksql.parser.tree.DropTopic) DropStream(io.confluent.ksql.parser.tree.DropStream) SetProperty(io.confluent.ksql.parser.tree.SetProperty) Pair(io.confluent.ksql.util.Pair)

Example 23 with QuerySpecification

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

the class StatementExecutor method handleCreateAsSelect.

private String handleCreateAsSelect(final CreateAsSelect statement, final Command command, final CommandId commandId, final Map<QueryId, CommandId> terminatedQueries, final String statementStr, final boolean wasDropped) throws Exception {
    QuerySpecification querySpecification = (QuerySpecification) statement.getQuery().getQueryBody();
    Query query = ksqlEngine.addInto(statement.getQuery(), querySpecification, statement.getName().getSuffix(), statement.getProperties(), statement.getPartitionByColumn());
    if (startQuery(statementStr, query, commandId, terminatedQueries, command, wasDropped)) {
        return statement instanceof CreateTableAsSelect ? "Table created and running" : "Stream created and running";
    }
    return null;
}
Also used : QuerySpecification(io.confluent.ksql.parser.tree.QuerySpecification) Query(io.confluent.ksql.parser.tree.Query) TerminateQuery(io.confluent.ksql.parser.tree.TerminateQuery) CreateTableAsSelect(io.confluent.ksql.parser.tree.CreateTableAsSelect)

Aggregations

QuerySpecification (io.confluent.ksql.parser.tree.QuerySpecification)23 Query (io.confluent.ksql.parser.tree.Query)19 Statement (io.confluent.ksql.parser.tree.Statement)18 Test (org.junit.Test)18 AliasedRelation (io.confluent.ksql.parser.tree.AliasedRelation)8 SingleColumn (io.confluent.ksql.parser.tree.SingleColumn)7 ComparisonExpression (io.confluent.ksql.parser.tree.ComparisonExpression)3 CreateStreamAsSelect (io.confluent.ksql.parser.tree.CreateStreamAsSelect)3 CreateTable (io.confluent.ksql.parser.tree.CreateTable)3 DropTable (io.confluent.ksql.parser.tree.DropTable)3 Join (io.confluent.ksql.parser.tree.Join)3 Table (io.confluent.ksql.parser.tree.Table)3 CreateTableAsSelect (io.confluent.ksql.parser.tree.CreateTableAsSelect)2 Expression (io.confluent.ksql.parser.tree.Expression)2 Relation (io.confluent.ksql.parser.tree.Relation)2 CreateStreamCommand (io.confluent.ksql.ddl.commands.CreateStreamCommand)1 CreateTableCommand (io.confluent.ksql.ddl.commands.CreateTableCommand)1 DropSourceCommand (io.confluent.ksql.ddl.commands.DropSourceCommand)1 DropTopicCommand (io.confluent.ksql.ddl.commands.DropTopicCommand)1 RegisterTopicCommand (io.confluent.ksql.ddl.commands.RegisterTopicCommand)1