Search in sources :

Example 21 with Statement

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

the class KsqlParser method buildAst.

/**
 * Builds an AST from the given query string.
 */
public List<Statement> buildAst(String sql, MetaStore metaStore) {
    try {
        ParserRuleContext tree = getParseTree(sql);
        SqlBaseParser.StatementsContext statementsContext = (SqlBaseParser.StatementsContext) tree;
        List<Statement> astNodes = new ArrayList<>();
        for (SqlBaseParser.SingleStatementContext statementContext : statementsContext.singleStatement()) {
            DataSourceExtractor dataSourceExtractor = new DataSourceExtractor(metaStore);
            dataSourceExtractor.extractDataSources(statementContext);
            Node root = new AstBuilder(dataSourceExtractor).visit(statementContext);
            Statement statement = (Statement) root;
            astNodes.add(statement);
        }
        return astNodes;
    } catch (Exception e) {
        // if we fail, parse with LL mode
        throw new ParseFailedException(e.getMessage(), e);
    }
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) DataSourceExtractor(io.confluent.ksql.util.DataSourceExtractor) Statement(io.confluent.ksql.parser.tree.Statement) Node(io.confluent.ksql.parser.tree.Node) ParseFailedException(io.confluent.ksql.parser.exception.ParseFailedException) ArrayList(java.util.ArrayList) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) ParseFailedException(io.confluent.ksql.parser.exception.ParseFailedException) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Example 22 with Statement

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

the class KsqlParser method prepareStatement.

public Pair<Statement, DataSourceExtractor> prepareStatement(SqlBaseParser.SingleStatementContext statementContext, MetaStore metaStore) {
    DataSourceExtractor dataSourceExtractor = new DataSourceExtractor(metaStore);
    dataSourceExtractor.extractDataSources(statementContext);
    AstBuilder astBuilder = new AstBuilder(dataSourceExtractor);
    Node root = astBuilder.visit(statementContext);
    Statement statement = (Statement) root;
    return new Pair<>(statement, dataSourceExtractor);
}
Also used : DataSourceExtractor(io.confluent.ksql.util.DataSourceExtractor) Statement(io.confluent.ksql.parser.tree.Statement) Node(io.confluent.ksql.parser.tree.Node) Pair(io.confluent.ksql.util.Pair)

Example 23 with Statement

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

the class KsqlEngine method parseQueries.

// Visible for Testing
List<Pair<String, Statement>> parseQueries(final String queriesString, final Map<String, Object> overriddenProperties, final MetaStore tempMetaStore) {
    try {
        MetaStore tempMetaStoreForParser = tempMetaStore.clone();
        // Parse and AST creation
        KsqlParser ksqlParser = new KsqlParser();
        List<SqlBaseParser.SingleStatementContext> parsedStatements = ksqlParser.getStatements(queriesString);
        List<Pair<String, Statement>> queryList = new ArrayList<>();
        for (SqlBaseParser.SingleStatementContext singleStatementContext : parsedStatements) {
            Pair<Statement, DataSourceExtractor> statementInfo = ksqlParser.prepareStatement(singleStatementContext, tempMetaStoreForParser);
            Statement statement = statementInfo.getLeft();
            Pair<String, Statement> queryPair = buildSingleQueryAst(statement, getStatementString(singleStatementContext), tempMetaStore, tempMetaStoreForParser, overriddenProperties);
            if (queryPair != null) {
                queryList.add(queryPair);
            }
        }
        return queryList;
    } catch (Exception e) {
        throw new ParseFailedException("Parsing failed on KsqlEngine msg:" + e.getMessage(), e);
    }
}
Also used : DataSourceExtractor(io.confluent.ksql.util.DataSourceExtractor) Statement(io.confluent.ksql.parser.tree.Statement) DdlStatement(io.confluent.ksql.parser.tree.DdlStatement) ParseFailedException(io.confluent.ksql.parser.exception.ParseFailedException) ArrayList(java.util.ArrayList) ParseFailedException(io.confluent.ksql.parser.exception.ParseFailedException) KsqlException(io.confluent.ksql.util.KsqlException) MetaStore(io.confluent.ksql.metastore.MetaStore) SqlBaseParser(io.confluent.ksql.parser.SqlBaseParser) KsqlParser(io.confluent.ksql.parser.KsqlParser) Pair(io.confluent.ksql.util.Pair)

Example 24 with Statement

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

the class QueryEngine method buildLogicalPlans.

List<Pair<String, PlanNode>> buildLogicalPlans(final MetaStore metaStore, final List<Pair<String, Statement>> statementList) {
    List<Pair<String, PlanNode>> logicalPlansList = new ArrayList<>();
    // TODO: the purpose of tempMetaStore here
    MetaStore tempMetaStore = metaStore.clone();
    for (Pair<String, Statement> statementQueryPair : statementList) {
        if (statementQueryPair.getRight() instanceof Query) {
            PlanNode logicalPlan = buildQueryLogicalPlan(statementQueryPair.getLeft(), (Query) statementQueryPair.getRight(), tempMetaStore);
            logicalPlansList.add(new Pair<>(statementQueryPair.getLeft(), logicalPlan));
        } else {
            logicalPlansList.add(new Pair<>(statementQueryPair.getLeft(), null));
        }
        log.info("Build logical plan for {}.", statementQueryPair.getLeft());
    }
    return logicalPlansList;
}
Also used : MetaStore(io.confluent.ksql.metastore.MetaStore) PlanNode(io.confluent.ksql.planner.plan.PlanNode) Query(io.confluent.ksql.parser.tree.Query) DdlStatement(io.confluent.ksql.parser.tree.DdlStatement) AbstractStreamCreateStatement(io.confluent.ksql.parser.tree.AbstractStreamCreateStatement) Statement(io.confluent.ksql.parser.tree.Statement) ArrayList(java.util.ArrayList) Pair(io.confluent.ksql.util.Pair)

Example 25 with Statement

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

the class QueryEngine method buildPhysicalPlans.

List<QueryMetadata> buildPhysicalPlans(final List<Pair<String, PlanNode>> logicalPlans, final List<Pair<String, Statement>> statementList, final Map<String, Object> overriddenProperties, final boolean updateMetastore) throws Exception {
    List<QueryMetadata> physicalPlans = new ArrayList<>();
    for (int i = 0; i < logicalPlans.size(); i++) {
        Pair<String, PlanNode> statementPlanPair = logicalPlans.get(i);
        if (statementPlanPair.getRight() == null) {
            Statement statement = statementList.get(i).getRight();
            if (!(statement instanceof DdlStatement)) {
                throw new KsqlException("expecting a statement implementing DDLStatement but got: " + statement.getClass());
            }
            handleDdlStatement(statementPlanPair.getLeft(), (DdlStatement) statement, overriddenProperties);
        } else {
            buildQueryPhysicalPlan(physicalPlans, statementPlanPair, overriddenProperties, updateMetastore);
        }
    }
    return physicalPlans;
}
Also used : QueryMetadata(io.confluent.ksql.util.QueryMetadata) PlanNode(io.confluent.ksql.planner.plan.PlanNode) DdlStatement(io.confluent.ksql.parser.tree.DdlStatement) AbstractStreamCreateStatement(io.confluent.ksql.parser.tree.AbstractStreamCreateStatement) Statement(io.confluent.ksql.parser.tree.Statement) ArrayList(java.util.ArrayList) DdlStatement(io.confluent.ksql.parser.tree.DdlStatement) KsqlException(io.confluent.ksql.util.KsqlException)

Aggregations

Statement (io.confluent.ksql.parser.tree.Statement)54 Test (org.junit.Test)38 Query (io.confluent.ksql.parser.tree.Query)24 QuerySpecification (io.confluent.ksql.parser.tree.QuerySpecification)18 AliasedRelation (io.confluent.ksql.parser.tree.AliasedRelation)7 SingleColumn (io.confluent.ksql.parser.tree.SingleColumn)7 KsqlException (io.confluent.ksql.util.KsqlException)7 Analysis (io.confluent.ksql.analyzer.Analysis)6 AnalysisContext (io.confluent.ksql.analyzer.AnalysisContext)6 Analyzer (io.confluent.ksql.analyzer.Analyzer)6 DdlStatement (io.confluent.ksql.parser.tree.DdlStatement)5 Expression (io.confluent.ksql.parser.tree.Expression)5 ArrayList (java.util.ArrayList)5 ComparisonExpression (io.confluent.ksql.parser.tree.ComparisonExpression)4 PlanNode (io.confluent.ksql.planner.plan.PlanNode)4 AggregateAnalysis (io.confluent.ksql.analyzer.AggregateAnalysis)3 AggregateAnalyzer (io.confluent.ksql.analyzer.AggregateAnalyzer)3 ParseFailedException (io.confluent.ksql.parser.exception.ParseFailedException)3 AbstractStreamCreateStatement (io.confluent.ksql.parser.tree.AbstractStreamCreateStatement)3 DereferenceExpression (io.confluent.ksql.parser.tree.DereferenceExpression)3